Class: Tweek::Section

Inherits:
Array
  • Object
show all
Defined in:
lib/tweek/section.rb

Overview

A section is an array of lines, each line is an Entry The section line itself is stored in a separate Entry object

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(twf, entry) ⇒ Section

Returns a new instance of Section.



8
9
10
11
# File 'lib/tweek/section.rb', line 8

def initialize twf, entry
  @twf = twf
  @section_entry = entry
end

Instance Attribute Details

#section_entryObject (readonly)

Returns the value of attribute section_entry.



6
7
8
# File 'lib/tweek/section.rb', line 6

def section_entry
  @section_entry
end

#twfObject (readonly)

Returns the value of attribute twf.



6
7
8
# File 'lib/tweek/section.rb', line 6

def twf
  @twf
end

Class Method Details

.initial(twf) ⇒ Object

Return an initial section used to hold anything (such as comments) prior to the first real section



15
16
17
# File 'lib/tweek/section.rb', line 15

def self.initial twf
  Tweek::Section.new(twf, Tweek::Entry.new(self, :section, :section_type => :initial) )
end

Instance Method Details

#each_entry(&block) ⇒ Object

Helper method to easily iterate through each actionable entry in the section.



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/tweek/section.rb', line 21

def each_entry &block
  self.each do |entry|
    case entry.type
    when :literal
      @twf.generate entry
    when :parameter
      begin
        if entry.condfail
          @twf.generate entry
        else
          yield entry if block_given?
        end
      rescue Exception => e
        @twf.error e.message, entry.lineno
        STDERR.puts "#{e.message}\n#{e.backtrace.join("\n")}" if ENV['DEBUG_TWEEK']
      end
    else
      raise "Unknown entry type: #{entry.type}"
    end
  end
end

#handle_irqs(netdev, entry) ⇒ Object



222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
# File 'lib/tweek/section.rb', line 222

def handle_irqs netdev, entry
  irqs = Tweek.irqs(netdev)

  if irqs.empty?
    @twf.warn "#{netdev} IRQ check skipped: none found", entry.lineno
    entry.actual = 'null'
    return
  end

  ncores = Tweek.ncores
  mismatches = 0
  expected_rps = []
  expected_rss = []
  irqs.each_with_index do |irq, ix|
    case entry.value
    when 'null','none','ignore'
      next

    when 'aws'
      case ncores
      when 8..64
        expected_rps << ("00000000," * 3) + "0000ff00"
        expected_rss << ("00000000," * 3) + sprintf("%08x", 1<<ix)
      when 1..8
        expected_rps << "0000"
        expected_rss << sprintf("%04x", 1<<ix)
      else
        @twf.warn "#{netdev} IRQ strategy '#{entry.value}' can't handle #{ncores} cores", entry.lineno
        return
      end

    when 'pin'
      case ncores
      when 1..8 # not a lot of choice
        expected_rps << "0000"
        expected_rss << "0001"

      when 8..32 # RPS on cores 10-15 and 18-31, RSS tied to 0-7
        case ix
        when 0..7
          expected_rps << ("00000000," * 3) + "fffefe00"
          expected_rss << ("00000000," * 3) + sprintf("%08x", 1<<(ix+1))
        else
          @twf.warn "#{netdev} IRQ strategy '#{entry.value}' can't handle IRQ#{ix}", entry.lineno
          return
        end

      when 32..64 # Split the work across the two nodes
        case ix
        when 0..3 # alloc on node 0 (cpus 0-3)
          expected_rps << ("00000000," * 2) + "0000ffff," + "0000ffe0"
          expected_rss << ("00000000," * 3) + sprintf("%08x", 1<<(ix+1))
        when 4..7 # alloc on node 1 (
          expected_rps << ("00000000," * 2) + "ffff0000," + "ffe00000"
          expected_rss << ("00000000," * 3) + sprintf("%08x", 1<<(ix + 9))
        else
          @twf.warn "#{netdev} IRQ strategy '#{entry.value}' can't handle IRQ#{ix}", entry.lineno
          return
        end

      else
        @twf.error "Can't handle #{ncores} cores in IRQ strategy '#{entry.value}'", entry.lineno
        return
      end

    else
      @twf.error "Unrecognized #{netdev} IRQ strategy '#{entry.value}'", entry.lineno
      return
    end
  end
  rss = [] # sadly map.with_index doesn't work in 1.8.7, no other compatible way
  irqs.each_with_index do |irq, ix|
    rss << Tweek::Entry.new(self, :parameter, :param => "#{netdev}_irq_#{irq}_rss",
                            :actual => Tweek.net_rss(irq),
                            :value => expected_rss[ix])
  end
  rps = []
  irqs.each_with_index do |irq, ix|
    rps << Tweek::Entry.new(self, :parameter, :param => "#{netdev}_irq_rx-#{ix}_rps",
                            :actual => Tweek.net_rps(netdev, ix),
                            :value => expected_rps[ix])
  end
  mismatches = rss.reduce(0){|sum, r| sum += 1 if r.mismatch}
  mismatches += rps.reduce(0){|sum, r| sum += 1 if r.mismatch}
  entry.actual = 'explicit' if mismatches > 0
  @twf.generate entry
  (rss+rps).each {|e| @twf.generate e}
  return
end

#processObject



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/tweek/section.rb', line 43

def process

  return if self.empty?

  case section_entry.section_type

  when :initial
    self.each do |entry|
      case entry.type
      when :literal
        @twf.generate entry
      else
        @twf.warn "Parameter line ignored outside section", entry.lineno
      end
    end

  when :BLKDEV # BLOCK DEVICE PARAMETERS
    @twf.warn "Set/Reset is not currently implemented for #{section_entry.section_type} section" if [:set, :reset].include? @twf.mflag
    Dir.chdir('/dev')
    devices = Dir.glob(section_entry.list.split(' ').map{|dev| dev.gsub(/^\/dev\//,'')})
    @twf.warn("Block device check skipped: no devices found", section_entry.lineno) if devices.empty?
    devices.each do |blk|
      section_entry.list_element = blk
      @twf.generate section_entry
      each_entry do |entry|
        entry.actual = Tweek.blk_param(blk, entry.param)
        if entry.param == 'scheduler'
          entry.actual = entry.actual.slice(/\[(.*?)\]/,1) unless entry.actual == 'none'
        end
        @twf.generate entry
      end
    end

  when :KERNEL # KERNEL PARAMETERS
    @twf.warn "Set/Reset is not currently implemented for #{section_entry.section_type} section" if [:set, :reset].include? @twf.mflag
    @twf.generate section_entry
    each_entry do |entry|
      actual = /\b(#{entry.param})(=\S+)?\b/.match(Tweek.bootline)
      case
      when (entry.value == 'true' and actual)
        entry.actual = actual[2].nil? ? 'true' : actual[2][1..-1]
      when (entry.value == 'true' and actual.nil?)
        entry.actual = 'false'
      when (entry.value == 'false' and actual)
        entry.actual = 'true'
      when (entry.value == 'false' and actual.nil?)
        entry.actual = 'false'
      else
       entry.actual = actual.nil? ? 'false' : actual[2][1..-1]
      end
      @twf.generate entry
    end

  when :CLOCKSOURCE
    @twf.warn "Set/Reset is not currently implemented for #{section_entry.section_type} section" if [:set, :reset].include? @twf.mflag
    @twf.generate section_entry
    each_entry do |entry|
      entry.actual = Tweek.clocksource(entry.param)
      @twf.generate entry
    end

  when :SYSCTL # SYSCTL SETTINGS
    @twf.generate section_entry
    each_entry do |entry|
      file = entry.param.gsub(/\./,'/')
      entry.actual = Tweek.sysctl(file)
      case @twf.mflag
      when :set
        entry.value = Tweek.set_sysctl(file, entry.value)
        entry.mode = :set
      when :reset
        if entry.was
          if entry.actual == entry.value
            entry.value = Tweek.set_sysctl(file, entry.was)
            entry.mode = :reset
          else
            @twf.warn "Current value of #{entry.param.bold} does not match, cannot reset", entry.lineno
          end
        else
          @twf.warn "Previous value of #{entry.param.bold} not found, cannot reset", entry.lineno
        end
      end
      @twf.generate entry
    end

  when :NET  # NETWORK DEVICES
    @twf.warn "Set/Reset is not currently implemented for #{section_entry.section_type} section" if [:set, :reset].include? @twf.mflag
    devices = section_entry.list.split(' ').map{|dev| dev.gsub(/^\/dev\//,'')}
    @twf.warn("Network device check skipped: no devices found", section_entry.lineno) if devices.empty?

    devices.each do |netdev|
      unless Tweek.net_dev_exist? netdev
        @twf.warn("NET device #{netdev} not found", section_entry.lineno)
        next
      end
      section_entry.list_element = netdev
      @twf.generate section_entry
      each_entry do |entry|

 case entry.param
 when 'driver'
          entry.actual = Tweek.net_driver(netdev)
          @twf.generate entry

 when 'irqs'
          balpid = Tweek.balance_pid
          if entry.value == 'balance'
            entry.actual = balpid.zero? ? 'IRQ balance daemon not running' : 'balance'
          else
            if balpid.zero?
              handle_irqs(netdev, entry)
            else
              entry.actual = 'IRQ balance daemon running'
              @twf.generate entry
            end
          end

 else # treat the parameter as a name
          entry.actual = Tweek.net_param(netdev, entry.param)
          @twf.generate entry
        end

      end
    end

  when :EXT4 # EXT4 filesystems info via `dumpe2fs -h /dev/#{device}`
    @twf.warn "Set/Reset is not currently implemented for #{section_entry.section_type} section" if [:set, :reset].include? @twf.mflag
    mounted = Tweek.mounted
    mounts = Dir.glob(section_entry.list.split(' '))
    @twf.warn("EXT4 check skipped: no mountpoints found", section_entry.lineno) if mounts.empty?
    mounts.each do |mount|
      unless this = mounted.select{|m| (mount == m[0] or mount == m[1]) and m[2] == 'ext4'}.first
        @twf.warn "EXT4 path #{mount} is not mounted or is not mounted as ext4", section_entry.lineno
        next
      end
      section_entry.list_element = mount
      @twf.generate section_entry
      device = this[0].gsub('/dev/','')
      optstring = File.open("/proc/fs/ext4/#{device}/options",&:read)
      options = Hash[optstring.scan(/([^=\s]+)=([^=\s,]+)/)] # options a=b
      optstring.split(/\n/).reject{|o| o.index('=')}.each{|o| options[o] = 'true'}
      each_entry do |entry|
        entry.actual = options[entry.param] || "<not found>"
        @twf.generate entry
      end
    end

  when :XFS # XFS filesystems info  via `xfs_info`
    @twf.warn "Set/Reset is not currently implemented for #{section_entry.section_type} section" if [:set, :reset].include? @twf.mflag
    # Dynamically via: /proc/fs/xfs/... ?
    mounts = section_entry.list.split(' ')
    @twf.warn("XFS device check skipped: no mountpoints found", section_entry.lineno) if mounts.empty?
    mounts.each do |mount|
      xfsinfo = Tweek.xfsinfo(mount)
      unless xfsinfo
        @twf.warn "No XFS filesystem at #{mount}", section_entry.lineno
        next
      end
      section_entry.list_element = mount
      @twf.generate section_entry
      data = Hash[xfsinfo.slice!(/^meta-data.*(?=^naming)/m).scan(/([^=\s]+)=([^=\s,]+)/)]
      naming = Hash[xfsinfo.slice!(/^naming.*(?=^log)/m).scan(/([^=\s]+)=([^=\s,]+)/)]
      log = Hash[xfsinfo.slice!(/^log.*(?=^realtime)/m).scan(/([^=\s]+)=([^=\s,]+)/)]
      realtime = Hash[xfsinfo.slice!(/^realtime.*$/m).scan(/([^=\s]+)=([^=\s,]+)/)]
      xfsparms = { 'data' => data, 'naming' => naming, 'log' => log, 'realtime' => realtime }

      each_entry do |entry|
        parameter = entry.param.split('.',2)
        entry.actual = xfsparms[parameter[0]][parameter[1]] rescue "<invalid name>"
        @twf.generate entry
      end
    end

  else
    @twf.error "Unknown type #{section_entry.section_type}", section_entry.lineno
  end

end