Class: OsCtl::Cli::Top::Tui::Main

Inherits:
Screen
  • Object
show all
Includes:
Utils::Humanize
Defined in:
lib/osctl/cli/top/tui/main.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utils::Humanize

#break_interval, #format_long_duration, #format_percent, #format_short_duration, #humanize_data, #humanize_time_ns, #parse_data

Constructor Details

#initialize(model, rate) ⇒ Main

Returns a new instance of Main



8
9
10
11
12
13
14
15
16
17
18
# File 'lib/osctl/cli/top/tui/main.rb', line 8

def initialize(model, rate)
  @model = model
  @rate = rate
  @last_measurement = nil
  @last_count = nil
  @sort_index = 0
  @sort_desc = true
  @current_row = nil
  @highlighted_cts = []
  @status_bar_cols = 0
end

Instance Attribute Details

#current_rowObject (protected)

Returns the value of attribute current_row



111
112
113
# File 'lib/osctl/cli/top/tui/main.rb', line 111

def current_row
  @current_row
end

#highlighted_ctsObject (readonly, protected)

Returns the value of attribute highlighted_cts



110
111
112
# File 'lib/osctl/cli/top/tui/main.rb', line 110

def highlighted_cts
  @highlighted_cts
end

#last_countObject (protected)

Returns the value of attribute last_count



111
112
113
# File 'lib/osctl/cli/top/tui/main.rb', line 111

def last_count
  @last_count
end

#last_dataObject (protected)

Returns the value of attribute last_data



111
112
113
# File 'lib/osctl/cli/top/tui/main.rb', line 111

def last_data
  @last_data
end

#last_measurementObject (protected)

Returns the value of attribute last_measurement



111
112
113
# File 'lib/osctl/cli/top/tui/main.rb', line 111

def last_measurement
  @last_measurement
end

#modelObject (readonly, protected)

Returns the value of attribute model



110
111
112
# File 'lib/osctl/cli/top/tui/main.rb', line 110

def model
  @model
end

#rateObject (readonly, protected)

Returns the value of attribute rate



110
111
112
# File 'lib/osctl/cli/top/tui/main.rb', line 110

def rate
  @rate
end

Instance Method Details

#boldObject (protected)



599
600
601
602
603
# File 'lib/osctl/cli/top/tui/main.rb', line 599

def bold
  Curses.attron(Curses::A_BOLD)
  yield
  Curses.attroff(Curses::A_BOLD)
end

#cursorObject (protected)



611
612
613
614
615
616
617
618
619
620
621
622
623
624
# File 'lib/osctl/cli/top/tui/main.rb', line 611

def cursor
  res = ''

  $stdin.raw do |stdin|
    $stdout << "\e[6n"
    $stdout.flush
    while (c = stdin.getc) != 'R'
      res << c if c
    end
  end

  m = res.match /(?<row>\d+);(?<column>\d+)/
  [m[:column].to_i, m[:row].to_i]
end

#fillRowObject (protected)



605
606
607
608
609
# File 'lib/osctl/cli/top/tui/main.rb', line 605

def fillRow
  yield
  x, y = cursor
  Curses.addstr(' ' * (Curses.cols - x))
end

#format_ctid(ctid) ⇒ Object (protected)



591
592
593
594
595
596
597
# File 'lib/osctl/cli/top/tui/main.rb', line 591

def format_ctid(ctid)
  if ctid.length > 12
    ctid[0..11] + '..'
  else
    ctid
  end
end

#get_dataObject (protected)



422
423
424
425
426
427
428
429
430
431
# File 'lib/osctl/cli/top/tui/main.rb', line 422

def get_data
  ret = model.data

  ret[:containers].sort! do |a, b|
    sortable_value(a) <=> sortable_value(b)
  end

  ret[:containers].reverse! if @sort_desc
  ret
end

#header(pos) ⇒ Object (protected)



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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
# File 'lib/osctl/cli/top/tui/main.rb', line 272

def header(pos)
  unless @header
    ret = []

    ret << sprintf(
      '%-14s %7s %8s %6s %27s %27s',
      'Container',
      'CPU',
      'Memory',
      'Proc',
      'BlkIO          ',
      'Network        '
    )

    ret << sprintf(
      '%-14s %7s %7s %6s %13s %13s %13s %13s',
      '',
      '',
      '',
      '',
      'Read   ',
      'Write   ',
      'TX    ',
      'RX    '
    )

    ret << sprintf(
      '%-14s %7s %7s %6s %6s %6s %6s %6s %6s %6s %6s %6s',
      'ID',
      '',
      '',
      '',
      'Bytes',
      rt? ? 'IOPS' : 'IO',
      'Bytes',
      rt? ? 'IOPS' : 'IO',
      rt? ? 'bps' : 'Bytes',
      rt? ? 'pps' : 'Packet',
      rt? ? 'bps' : 'Bytes',
      rt? ? 'pps' : 'Packet'
    )

    # Fill to the edge of the screen
    @header = ret.map do |line|
      line << (' ' * (Curses.cols - line.size)) << "\n"
    end
  end

  @header.each do |line|
    Curses.setpos(pos, 0)
    Curses.addstr(line)
    pos += 1
  end

  pos
end

#loadavgObject (protected)



579
580
581
# File 'lib/osctl/cli/top/tui/main.rb', line 579

def loadavg
  File.read('/proc/loadavg').strip.split(' ')[0..2].join(', ')
end

#lookup_field(ct, field) ⇒ Object (protected)



570
571
572
573
574
575
576
577
# File 'lib/osctl/cli/top/tui/main.rb', line 570

def lookup_field(ct, field)
  if field.is_a?(Array)
    field.reduce(ct) { |acc, v| acc[v] }

  else
    ct[field]
  end
end

#max_rowsObject (protected)

Screen without header and footer



627
628
629
# File 'lib/osctl/cli/top/tui/main.rb', line 627

def max_rows
  Curses.lines - @status_bar_cols - 1 - @header.size - 5 - 1
end

#modeObject (protected)



583
584
585
# File 'lib/osctl/cli/top/tui/main.rb', line 583

def mode
  model.mode
end

#openObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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
# File 'lib/osctl/cli/top/tui/main.rb', line 20

def open
  unless last_measurement
    render(Time.now, {containers: []})
    sleep(0.5)
  end

  # At each loop pass, model is queried for data, unless `hold_data` is
  # greater than zero. In that case, `hold_data` is decremented by 1
  # on each pass.
  hold_data = 0

  loop do
    now = Time.now

    if !paused? && (last_measurement.nil? || (now - last_measurement) >= rate)
      model.measure
      self.last_measurement = now
    end

    if !last_data || hold_data == 0
      @last_data = data = get_data

    elsif hold_data > 0
      hold_data -= 1
      data = last_data
    end

    Curses.clear if last_count != data[:containers].count
    self.last_count = data[:containers].count

    render(now, data)
    Curses.timeout = rate * 1000

    case Curses.getch
    when 'q'
      return

    when Curses::Key::LEFT, '<'
      Curses.clear
      sort_next(-1)

    when Curses::Key::RIGHT, '>'
      Curses.clear
      sort_next(+1)

    when Curses::Key::UP
      selection_up
      hold_data = 1

    when Curses::Key::DOWN
      selection_down
      hold_data = 1

    when ' '
      selection_highlight

    when Curses::Key::ENTER, 10
      selection_open

    when 'r', 'R'
      Curses.clear
      sort_inverse

    when 'm'
      modes = model.class::MODES
      i = modes.index(mode)

      if i+1 >= modes.count
        model.mode = modes[0]

      else
        model.mode = modes[i+1]
      end

      @header = nil
      Curses.clear

    when 'p'
      paused? ? unpause : pause

    when '?'
      return Tui::Help.new(self)

    when Curses::Key::RESIZE
      Curses.clear
    end
  end
end

#pauseObject (protected)



631
632
633
# File 'lib/osctl/cli/top/tui/main.rb', line 631

def pause
  @paused = true
end

#paused?Boolean (protected)

Returns:

  • (Boolean)


639
640
641
# File 'lib/osctl/cli/top/tui/main.rb', line 639

def paused?
  @paused
end


329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
# File 'lib/osctl/cli/top/tui/main.rb', line 329

def print_row(ct)
  Curses.addstr(sprintf('%-14s ', format_ctid(ct[:id])))

  print_row_data([
    rt? ? format_percent(ct[:cpu_usage]) : humanize_time_ns(ct[:cpu_time]),
    humanize_data(ct[:memory]),
    ct[:nproc],
    humanize_data(ct[:blkio][:bytes][:r]),
    ct[:blkio][:iops][:r],
    humanize_data(ct[:blkio][:bytes][:w]),
    ct[:blkio][:iops][:w],
    humanize_data(ct[:tx][:bytes]),
    humanize_data(ct[:tx][:packets]),
    humanize_data(ct[:rx][:bytes]),
    humanize_data(ct[:rx][:packets])
  ])
end


347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
# File 'lib/osctl/cli/top/tui/main.rb', line 347

def print_row_data(values)
  fmts = %w(%7s %8s %6s %6s %6s %6s %6s %6s %6s %6s %6s)
  w = 15 # container ID is printed in {#print_row}

  fmts.zip(values).each_with_index do |pair, i|
    f, v = pair
    s = sprintf("#{f} ", v)
    w += s.length

    Curses.attron(Curses::A_BOLD) if i == @sort_index
    Curses.addstr(s)
    Curses.attroff(Curses::A_BOLD) if i == @sort_index
  end

  # Fill space to the edge of the screen, needed for selected rows
  Curses.addstr(' ' * (Curses.cols - w)) if Curses.cols > w
end

#render(t, data) ⇒ Object (protected)



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
# File 'lib/osctl/cli/top/tui/main.rb', line 113

def render(t, data)
  Curses.setpos(0, 0)
  Curses.addstr("#{File.basename($0)} ct top - #{t.strftime('%H:%M:%S')}")
  Curses.addstr(" #{model.mode} mode, load average #{loadavg}")

  i = status_bar(1, data)

  Curses.attron(Curses.color_pair(1))
  i = header(i+1)
  Curses.attroff(Curses.color_pair(1))

  data[:containers].each_with_index do |ct, j|
    Curses.setpos(i, 0)

    if current_row == j && highlighted_cts.include?(ct[:id])
      attr = Curses.color_pair(Tui::SELECTED_HIGHLIGHTED)

    elsif current_row == j
      attr = Curses.color_pair(Tui::SELECTED)

    elsif highlighted_cts.include?(ct[:id])
      attr = Curses.color_pair(Tui::HIGHLIGHTED)

    else
      attr = nil
    end

    Curses.attron(attr) if attr
    print_row(ct)
    Curses.attroff(attr) if attr

    i += 1

    break if i >= (Curses.lines - 5)
  end

  stats(data[:containers])

  Curses.refresh
end

#rt?Boolean (protected)

Returns:

  • (Boolean)


587
588
589
# File 'lib/osctl/cli/top/tui/main.rb', line 587

def rt?
  model.mode == :realtime
end

#selection_downObject (protected)



496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
# File 'lib/osctl/cli/top/tui/main.rb', line 496

def selection_down
  if @current_row
    new_row = @current_row + 1

    if new_row < last_data[:containers].size && new_row < max_rows
      @current_row = new_row

    elsif last_data[:containers].any?
      @current_row = 0

    else
      @current_row = nil
    end

  elsif last_data[:containers].any?
    @current_row = 0
  end
end

#selection_highlightObject (protected)



515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
# File 'lib/osctl/cli/top/tui/main.rb', line 515

def selection_highlight
  return unless @current_row

  ct = last_data[:containers][@current_row]
  return unless ct

  ctid = ct[:id]

  if highlighted_cts.include?(ctid)
    highlighted_cts.delete(ctid)

  else
    highlighted_cts << ctid
  end
end

#selection_openObject (protected)



531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
# File 'lib/osctl/cli/top/tui/main.rb', line 531

def selection_open
  return unless @current_row

  ct = last_data[:containers][@current_row]
  return unless ct

  if ct[:id] == '[host]'
    ctid = 'host'

  else
    ctid = "#{ct[:pool]}:#{ct[:id]}"
  end

  pid = Process.fork do
    Process.exec('htop', '-c', ctid)
  end

  Process.wait(pid)

  # The screen needs to be reinitialized after htop
  Curses.init_screen
  Curses.start_color
  Curses.crmode
  Curses.stdscr.keypad = true
  Curses.curs_set(0)  # hide cursor
  Curses.clear
end

#selection_upObject (protected)



472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
# File 'lib/osctl/cli/top/tui/main.rb', line 472

def selection_up
  last_row = [max_rows - 1, last_data[:containers].size - 1].min

  if @current_row
    new_row = @current_row - 1

    if new_row >= 0
      @current_row = new_row

    elsif new_row == -1
      @current_row = nil

    elsif last_data[:containers].any?
      @current_row = last_row

    else
      @current_row = nil
    end

  elsif last_data[:containers].any?
    @current_row = last_row
  end
end

#sort_inverseObject (protected)



451
452
453
# File 'lib/osctl/cli/top/tui/main.rb', line 451

def sort_inverse
  @sort_desc = !@sort_desc
end

#sort_next(n) ⇒ Object (protected)



437
438
439
440
441
442
443
444
445
446
447
448
449
# File 'lib/osctl/cli/top/tui/main.rb', line 437

def sort_next(n)
  next_i = @sort_index + n
  fields = sortable_fields

  if next_i < 0
    next_i = fields.count - 1

  elsif next_i >= fields.count
    next_i = 0
  end

  @sort_index = next_i
end

#sortable_fieldsObject (protected)



455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
# File 'lib/osctl/cli/top/tui/main.rb', line 455

def sortable_fields
  ret = []
  ret << (rt? ? :cpu_usage : :cpu_time)
  ret.concat([
    :memory,
    :nproc,
    [:blkio, :bytes, :r],
    [:blkio, :iops, :r],
    [:blkio, :bytes, :w],
    [:blkio, :iops, :w],
    [:tx, :bytes],
    [:tx, :packets],
    [:rx, :bytes],
    [:rx, :packets],
  ])
end

#sortable_value(ct) ⇒ Object (protected)



433
434
435
# File 'lib/osctl/cli/top/tui/main.rb', line 433

def sortable_value(ct)
  lookup_field(ct, sortable_fields[@sort_index])
end

#stats(cts) ⇒ Object (protected)



365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
# File 'lib/osctl/cli/top/tui/main.rb', line 365

def stats(cts)
  Curses.setpos(Curses.lines - 5, 0)
  Curses.addstr('' * Curses.cols)
  #Curses.addstr('-' * Curses.cols)

  Curses.setpos(Curses.lines - 4, 0)
  Curses.addstr(sprintf('%-14s ', 'Containers:'))
  print_row_data([
    rt? ? format_percent(sum(cts, :cpu_usage, false)) \
        : humanize_time_ns(sum(cts, :cpu_time, false)),
    humanize_data(sum(cts, :memory, false)),
    sum(cts, :nproc, false),
    humanize_data(sum(cts, [:blkio, :bytes, :r], false)),
    sum(cts, [:blkio, :iops, :r], false),
    humanize_data(sum(cts, [:blkio, :bytes, :w], false)),
    sum(cts, [:blkio, :iops, :w], false),
    humanize_data(sum(cts, [:tx, :bytes], false)),
    humanize_data(sum(cts, [:tx, :packets], false)),
    humanize_data(sum(cts, [:rx, :bytes], false)),
    humanize_data(sum(cts, [:rx, :packets], false))
  ])

  Curses.setpos(Curses.lines - 3, 0)
  Curses.addstr(sprintf('%-14s ', 'All:'))
  print_row_data([
    rt? ? format_percent(sum(cts, :cpu_usage, true)) \
        : humanize_time_ns(sum(cts, :cpu_time, true)),
    humanize_data(sum(cts, :memory, true)),
    sum(cts, :nproc, true),
    humanize_data(sum(cts, [:blkio, :bytes, :r], true)),
    sum(cts, [:blkio, :iops, :r], true),
    humanize_data(sum(cts, [:blkio, :bytes, :w], true)),
    sum(cts, [:blkio, :iops, :w], true),
    humanize_data(sum(cts, [:tx, :bytes], true)),
    humanize_data(sum(cts, [:tx, :packets], true)),
    humanize_data(sum(cts, [:rx, :bytes], true)),
    humanize_data(sum(cts, [:rx, :packets], true))
  ])

  Curses.setpos(Curses.lines - 2, 0)
  Curses.addstr('' * Curses.cols)
  Curses.setpos(Curses.lines - 1, 0)
  fillRow do
    Curses.addstr('Selected container: ')

    if @current_row && (ct = last_data[:containers][@current_row])
      if ct[:id] == '[host]'
        Curses.addstr('host system')
      else
        Curses.addstr("#{ct[:pool]}:#{ct[:id]}")
      end
    else
      Curses.addstr('none')
    end
  end
end

#status_bar(orig_pos, data) ⇒ Object (protected)



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
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
# File 'lib/osctl/cli/top/tui/main.rb', line 154

def status_bar(orig_pos, data)
  pos = orig_pos
  @status_bar_cols = 0

  # CPU
  cpu = data[:cpu]

  Curses.setpos(pos, 0)
  Curses.addstr('%CPU: ')

  if cpu
    bold { Curses.addstr(sprintf('%5.1f', format_percent(cpu[:user]))) }
    Curses.addstr(' us, ')
    bold { Curses.addstr(sprintf('%5.1f', format_percent(cpu[:system]))) }
    Curses.addstr(' sy, ')
    bold { Curses.addstr(sprintf('%5.1f', format_percent(cpu[:nice]))) }
    Curses.addstr(' ni, ')
    bold { Curses.addstr(sprintf('%5.1f', format_percent(cpu[:idle]))) }
    Curses.addstr(' id, ')
    bold { Curses.addstr(sprintf('%5.1f', format_percent(cpu[:iowait]))) }
    Curses.addstr(' wa, ')
    bold { Curses.addstr(sprintf('%5.1f', format_percent(cpu[:irq]))) }
    Curses.addstr(' hi, ')
    bold { Curses.addstr(sprintf('%5.1f', format_percent(cpu[:softirq]))) }
    Curses.addstr(' si')

  else
    Curses.addstr('calculating')
  end

  # Memory
  mem = data[:memory]

  Curses.setpos(pos += 1, 0)
  Curses.addstr('Memory: ')

  if mem
    bold { Curses.addstr(sprintf('%8s', humanize_data(mem[:total]))) }
    Curses.addstr(' total, ')
    bold { Curses.addstr(sprintf('%8s', humanize_data(mem[:free]))) }
    Curses.addstr(' free, ')
    bold { Curses.addstr(sprintf('%8s', humanize_data(mem[:used]))) }
    Curses.addstr(' used, ')
    bold { Curses.addstr(sprintf('%8s', humanize_data(mem[:buffers] + mem[:cached]))) }
    Curses.addstr(' buff/cache')

    if mem[:swap_total] > 0
      Curses.setpos(pos += 1, 0)
      Curses.addstr('Swap:   ')

      bold { Curses.addstr(sprintf('%8s', humanize_data(mem[:swap_total]))) }
      Curses.addstr(' total, ')
      bold { Curses.addstr(sprintf('%8s', humanize_data(mem[:swap_free]))) }
      Curses.addstr(' free, ')
      bold { Curses.addstr(sprintf('%8s', humanize_data(mem[:swap_used]))) }
      Curses.addstr(' used')
    end

  else
    Curses.addstr('calculating')
  end

  # ZFS ARC
  arc = data[:zfs] && data[:zfs][:arc]

  Curses.setpos(pos += 1, 0)
  Curses.addstr('ARC:    ')

  if arc
    bold { Curses.addstr(sprintf('%8s', humanize_data(arc[:c_max]))) }
    Curses.addstr(' c_max, ')
    bold { Curses.addstr(sprintf('%8s', humanize_data(arc[:c]))) }
    Curses.addstr(' c,    ')
    bold { Curses.addstr(sprintf('%8s', humanize_data(arc[:size]))) }
    Curses.addstr(' size, ')
    bold { Curses.addstr(sprintf('%8.2f', format_percent(arc[:hit_rate]))) }
    Curses.addstr(' hitrate, ')
    bold { Curses.addstr(sprintf('%6d', arc[:misses])) }
    Curses.addstr(' missed ')

  else
    Curses.addstr('calculating')
  end

  l2arc = data[:zfs] && data[:zfs][:l2arc]

  if l2arc && l2arc[:size] > 0
    Curses.setpos(pos += 1, 0)
    Curses.addstr('L2ARC:  ' + ' ' * 16)

    bold { Curses.addstr(sprintf('%8s', humanize_data(l2arc[:size]))) }
    Curses.addstr(' size, ')
    bold { Curses.addstr(sprintf('%8s', humanize_data(l2arc[:asize]))) }
    Curses.addstr(' asize,')
    bold { Curses.addstr(sprintf('%8.2f', humanize_data(l2arc[:hit_rate]))) }
    Curses.addstr(' hitrate, ')
    bold { Curses.addstr(sprintf('%6d', l2arc[:misses])) }
    Curses.addstr(' missed ')
  end

  # Containers
  Curses.setpos(pos += 1, 0)
  Curses.addstr('Containers: ')
  bold { Curses.addstr(sprintf('%3d', model.containers.count)) }
  Curses.addstr(' total, ')
  bold { Curses.addstr(sprintf('%3d', model.containers.count{ |ct| ct.state == :starting })) }
  Curses.addstr(' starting, ')
  bold { Curses.addstr(sprintf('%3d', data[:containers].count-1)) } # -1 for [host]
  Curses.addstr(' running, ')
  bold { Curses.addstr(sprintf('%3d', model.containers.count{ |ct| ct.state == :stopping })) }
  Curses.addstr(' stopping, ')
  bold { Curses.addstr(sprintf('%3d', model.containers.count{ |ct| ct.state == :stopped })) }
  Curses.addstr(' stopped')

  @status_bar_cols += pos - orig_pos + 1
  pos + 1
end

#sum(cts, field, host) ⇒ Object (protected)



559
560
561
562
563
564
565
566
567
568
# File 'lib/osctl/cli/top/tui/main.rb', line 559

def sum(cts, field, host)
  cts.inject(0) do |acc, ct|
    if ct[:id] == '[host]' && !host
      acc

    else
      acc + lookup_field(ct, field)
    end
  end
end

#unpauseObject (protected)



635
636
637
# File 'lib/osctl/cli/top/tui/main.rb', line 635

def unpause
  @paused = false
end