Class: Tweek::Section

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

Overview

A section is an array of the parsed lines, each element is an OpenStruct There are the following types of Entry With line attributes - single lines With section type and list attributes With parameter value and conditional attributes All entries may have an optional comment attribute

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(lineno, type, list, comment) ⇒ Section

Returns a new instance of Section.



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

def initialize lineno, type, list, comment
  @lineno = lineno
  @type = type
  @list = list
  @comment = comment
end

Instance Attribute Details

#commentObject (readonly)

Returns the value of attribute comment.



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

def comment
  @comment
end

#linenoObject (readonly)

Returns the value of attribute lineno.



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

def lineno
  @lineno
end

#listObject (readonly)

Returns the value of attribute list.



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

def list
  @list
end

#typeObject (readonly)

Returns the value of attribute type.



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

def type
  @type
end

Instance Method Details

#each_entry(twf, &block) ⇒ Object

Iterate through each entry in the section. If it’s a comment then pass to the generate method If it has a condition which is not met then pass to generate If it passes yield the entry



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

def each_entry twf, &block
  self.each do |entry|
    if entry.line
      twf.generate :line, entry
      next
    end
    begin
      if twf.condfail entry.cond
        twf.generate :entry, entry
        twf.skipcond entry
        next
      end
      yield entry
    rescue Exception => e
      twf.error "#{entry.lineno}: #{e.message}"
    end
  end
end

#handle_irqs(netdev, twf, entry) ⇒ Object



215
216
217
218
219
220
221
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
# File 'lib/tweek/section.rb', line 215

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

  if irqs.empty?
    twf.warn "#{netdev} IRQ check skipped: none found"
    entry.actual = 'null'
    twf.generate :entry, entry
    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"
        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}"
          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}"
          return
        end

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

    else
      twf.error "#{entry.lineno}: Unrecognized #{netdev} IRQ strategy '#{entry.value}'"
      return
    end
  end
  irqs.each_with_index do |irq, ix|
    actual_rss = Tweek.net_rss(irq)
    mismatches += twf.assert_equal expected_rss[ix], actual_rss, "#{netdev}_irq_#{irq}_rss"
  end
  irqs.each_with_index do |irq, ix|
    actual_rps = Tweek.net_rps(netdev, ix)
    mismatches += twf.assert_equal expected_rps[ix], actual_rps, "#{netdev}_irq_#{irq}_rps"
  end
  entry.actual = 'null' if mismatches > 0
  return
end

#process(twf) ⇒ Object



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
# File 'lib/tweek/section.rb', line 43

def process twf

  twf.generate :section, Tweek::Entry.new( :lineno => lineno, :type => type, :list => list, :comment => comment )

  return if self.empty?

  case self.type
  when nil
      each_entry (twf) do |entry|
      end

  when 'BLKDEV' # BLOCK DEVICE PARAMETERS

    twf.warn "Set is not currently implemented for #{type} section" if twf.sflag

    Dir.chdir('/dev')
    devices = Dir.glob(self.list.split(' ').map{|dev| dev.gsub(/^\/dev\//,'')})

    twf.warn "Block device check skipped: no devices found" if devices.empty?

    devices.each do |blk|
      each_entry (twf) 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.assert_equal entry.value, entry.actual, "/dev/#{blk} #{entry.param}"
        twf.generate :entry, entry
      end
    end

  when 'KERNEL' # KERNEL PARAMETERS
    twf.warn "Set is not currently implemented for #{type} section" if twf.sflag
    each_entry (twf) do |entry|
      actual = /\b(#{entry.param})(=\S+)?\b/.match(Tweek.bootline)
      case
      when (entry.value == 'true' and actual)
        observed = actual.to_s
        expected = entry.param
      when (entry.value == 'true' and actual.nil?)
        expected = 'to be found'
        observed = 'was not found'
      when (entry.value == 'false' and actual)
        expected = 'to not be found'
        observed = 'was found'
      when (entry.value == 'false' and actual.nil?)
        expected = nil
        observed = nil
  else
 expected = entry.value
        observed = actual.nil? ? 'was not found' : actual[2][1..-1]
      end
      twf.assert_equal expected, observed, "Kernel parameter #{entry.param}"
      entry.actual = !actual.nil?
      twf.generate :entry, entry
    end

  when 'CLOCKSOURCE'
    twf.warn "Set is not currently implemented for #{type} section" if twf.sflag
    each_entry (twf) do |entry|
      entry.actual = Tweek.clocksource(entry.param)
      twf.assert_equal entry.value, entry.actual, entry.param
      twf.generate :entry, entry
    end

  when 'SYSCTL' # SYSCTL SETTINGS
    each_entry (twf) do |entry|
      file = entry.param.gsub(/\./,'/')
      entry.actual = Tweek.sysctl(file)
      twf.assert_equal entry.value, entry.actual, entry.param
      if twf.sflag
        entry.value = Tweek.set_sysctl(file, entry.value)
        entry.set = true
      end
      twf.generate :entry, entry
    end

  when 'NET'  # NETWORK DEVICES
    twf.warn "Set is not currently implemented for #{type} section" if twf.sflag
    devices = self.list.split(' ').map{|dev| dev.gsub(/^\/dev\//,'')}
    twf.warn "Network device check skipped: no devices found" if devices.empty?

    devices.each do |netdev|
      each_entry (twf) do |entry|

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

 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, twf, entry)
              next
            else
              entry.actual = 'IRQ balance daemon running'
            end
          end

 else # treat the parameter as a name
   begin
            entry.actual = Tweek.net_param(netdev, entry.param)
   rescue
     twf.error "#{entry.lineno}: Network parameter #{entry.param} not handled: #{$!.message}"
     next
   end
        end

        twf.assert_equal entry.value, entry.actual, entry.param
 twf.generate :entry, entry
      end
    end

  when 'EXT4' # EXT4 filesystems info via `dumpe2fs -h /dev/#{device}`
    twf.warn "Set is not currently implemented for #{type} section" if twf.sflag
    mounted = Tweek.mounted
    mounts = Dir.glob(self.list.split(' '))
    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"
        next
      end
      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 (twf) do |entry|
        entry.actual = options[entry.param] || "<not found>"
        twf.assert_equal entry.value, entry.actual, "EXT4 #{mount}: #{entry.param}"
        twf.generate :entry, entry
      end
    end

  when 'XFS' # XFS filesystems info  via `xfs_info`
    twf.warn "Set is not currently implemented for #{type} section" if twf.sflag
    # Dynamically via: /proc/fs/xfs/... ?
    mounts = self.list.split(' ')
    if mounts.empty?
      twf.warn "#{self.lineno}: XFS device check skipped: no mountpoints found"
    end
    mounts.each do |mount|
      xfsinfo = Tweek.xfsinfo(mount)
      unless xfsinfo
        twf.warn "No XFS filesystem at #{mount}"
        each_entry (twf) { |entry| twf.generate :entry, entry }
        next
      end
      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 (twf) do |entry|
        parameter = entry.param.split('.',2)
        entry.actual = xfsparms[parameter[0]][parameter[1]] rescue "<invalid name>"
        twf.assert_equal entry.value, entry.actual, "XFS #{mount}: #{entry.param}"
        twf.generate :entry, entry
      end
    end

  else
    twf.error "#{self.lineno}: Unknown type #{self.type}"
  end

end