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

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model, rate, enable_procs: true) ⇒ Main

Returns a new instance of Main.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/osctl/cli/top/tui/main.rb', line 9

def initialize(model, rate, enable_procs: true)
  @rate = rate
  @last_count = nil
  @sort_index = 0
  @sort_desc = true
  @current_row = nil
  @view_page = 0
  @search_string = ''
  @highlighted_cts = []
  @status_bar_cols = 0
  @model_thread = Tui::ModelThread.new(model, rate)
  @model_thread.start
  @enable_procs = enable_procs
  if enable_procs
    @procs_thread = Tui::ProcessThread.new(rate)
    @procs_thread.start
  end
  @last_measurement = nil
  @last_generation = -1
  @last_procs_check = nil
  @last_mode = model_thread.mode
end

Instance Attribute Details

#current_rowObject (protected)

Returns the value of attribute current_row.



176
177
178
# File 'lib/osctl/cli/top/tui/main.rb', line 176

def current_row
  @current_row
end

#enable_procsObject (readonly, protected)

Returns the value of attribute enable_procs.



173
174
175
# File 'lib/osctl/cli/top/tui/main.rb', line 173

def enable_procs
  @enable_procs
end

#highlighted_ctsObject (readonly, protected)

Returns the value of attribute highlighted_cts.



173
174
175
# File 'lib/osctl/cli/top/tui/main.rb', line 173

def highlighted_cts
  @highlighted_cts
end

#last_countObject (protected)

Returns the value of attribute last_count.



176
177
178
# File 'lib/osctl/cli/top/tui/main.rb', line 176

def last_count
  @last_count
end

#last_dataObject (protected)

Returns the value of attribute last_data.



176
177
178
# File 'lib/osctl/cli/top/tui/main.rb', line 176

def last_data
  @last_data
end

#last_generationObject (readonly, protected)

Returns the value of attribute last_generation.



173
174
175
# File 'lib/osctl/cli/top/tui/main.rb', line 173

def last_generation
  @last_generation
end

#last_measurementObject (readonly, protected)

Returns the value of attribute last_measurement.



173
174
175
# File 'lib/osctl/cli/top/tui/main.rb', line 173

def last_measurement
  @last_measurement
end

#last_modeObject (readonly, protected)

Returns the value of attribute last_mode.



173
174
175
# File 'lib/osctl/cli/top/tui/main.rb', line 173

def last_mode
  @last_mode
end

#last_procs_checkObject (readonly, protected)

Returns the value of attribute last_procs_check.



173
174
175
# File 'lib/osctl/cli/top/tui/main.rb', line 173

def last_procs_check
  @last_procs_check
end

#model_threadObject (readonly, protected)

Returns the value of attribute model_thread.



173
174
175
# File 'lib/osctl/cli/top/tui/main.rb', line 173

def model_thread
  @model_thread
end

#procs_threadObject (readonly, protected)

Returns the value of attribute procs_thread.



173
174
175
# File 'lib/osctl/cli/top/tui/main.rb', line 173

def procs_thread
  @procs_thread
end

#rateObject (readonly, protected)

Returns the value of attribute rate.



173
174
175
# File 'lib/osctl/cli/top/tui/main.rb', line 173

def rate
  @rate
end

#view_pageObject (readonly, protected)

Returns the value of attribute view_page.



173
174
175
# File 'lib/osctl/cli/top/tui/main.rb', line 173

def view_page
  @view_page
end

#view_page_maxObject (readonly, protected)

Returns the value of attribute view_page_max.



173
174
175
# File 'lib/osctl/cli/top/tui/main.rb', line 173

def view_page_max
  @view_page_max
end

Instance Method Details

#boldObject (protected)



896
897
898
899
900
# File 'lib/osctl/cli/top/tui/main.rb', line 896

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

#cursorObject (protected)



908
909
910
911
912
913
914
915
916
917
918
919
920
921
# File 'lib/osctl/cli/top/tui/main.rb', line 908

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)



902
903
904
905
906
# File 'lib/osctl/cli/top/tui/main.rb', line 902

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

#format_ctid(ctid) ⇒ Object (protected)



871
872
873
874
875
876
877
# File 'lib/osctl/cli/top/tui/main.rb', line 871

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

#format_loadavg(lavg) ⇒ Object (protected)



883
884
885
886
887
888
889
890
891
892
893
894
# File 'lib/osctl/cli/top/tui/main.rb', line 883

def format_loadavg(lavg)
  fmt =
    if lavg < 100
      '%5.2f'
    elsif lavg < 1000
      '%5.1f'
    else
      '%4.0f'
    end

  sprintf(fmt, lavg)
end

#format_loadavgs(lavgs) ⇒ Object (protected)



879
880
881
# File 'lib/osctl/cli/top/tui/main.rb', line 879

def format_loadavgs(lavgs)
  lavgs.map { |lavg| format_loadavg(lavg) }.join(', ')
end

#get_dataObject (protected)



607
608
609
610
611
612
613
614
615
616
# File 'lib/osctl/cli/top/tui/main.rb', line 607

def get_data
  ret, measured_at, generation = model_thread.get_data

  @last_data = ret
  @last_measurement = measured_at
  @last_generation = generation

  run_sort
  ret
end

#header(pos) ⇒ Object (protected)



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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
# File 'lib/osctl/cli/top/tui/main.rb', line 384

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

    ret << sprintf(
      '%-14s %9s %8s %6s %27s %27s %17s',
      'Container',
      'CPU',
      'Memory',
      'Proc',
      'ZFSIO          ',
      'Network        ',
      'LoadAvg    ',
    )

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

    ret << sprintf(
      '%-14s %9s %8s %6s %6s %6s %6s %6s %6s %6s %6s %6s %5s %5s %5s',
      'ID',
      '',
      '',
      '',
      'Bytes',
      rt? ? 'IOPS' : 'IO',
      'Bytes',
      rt? ? 'IOPS' : 'IO',
      rt? ? 'bps' : 'Bytes',
      rt? ? 'pps' : 'Packet',
      rt? ? 'bps' : 'Bytes',
      rt? ? 'pps' : 'Packet',
      '1m', '5m', '15m',
    )

    # 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

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



854
855
856
857
858
859
860
861
# File 'lib/osctl/cli/top/tui/main.rb', line 854

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



924
925
926
# File 'lib/osctl/cli/top/tui/main.rb', line 924

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

#modeObject (protected)



863
864
865
# File 'lib/osctl/cli/top/tui/main.rb', line 863

def mode
  model_thread.mode
end

#openObject



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

def open
  empty_data = {containers: []}
  data = {containers: []}
  procs_stats, @last_procs_check = enable_procs && procs_thread.get_stats

  # Initial render
  render(Time.now, procs_stats, empty_data) if last_generation == -1

  # 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 last_generation != model_thread.generation \
       && (!last_data || hold_data == 0) \
       && !paused?
      data = get_data

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

    if last_mode != model_thread.mode
      @header = nil
      Curses.clear
      @last_mode = model_thread.mode
    elsif last_count != data[:containers].count
      Curses.clear
    end

    self.last_count = data[:containers].count

    if enable_procs && !paused?
      procs_stats, @last_procs_check = procs_thread.get_stats
    end

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

    if search_in_focus?
      case input
      when Curses::Key::ENTER, Tui::Key::ENTER
        search_end_focus
      when Tui::Key::ESCAPE
        search_cancel
      when Curses::Key::BACKSPACE, Tui::Key::BACKSPACE
        search_chop
      else
        if input && input.is_a?(String)
          search_add(input)
          Curses.clear
        end
      end

      next
    end

    case input
    when 'q'
      procs_thread.stop if enable_procs
      model_thread.stop
      return

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

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

    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, Tui::Key::ENTER, 't'
      selection_open_top

    when Curses::Key::NPAGE # Page Down
      view_page_down

    when Curses::Key::PPAGE # Page Up
      view_page_up

    when Curses::Key::HOME
      view_page_reset

    when Curses::Key::END
      view_page_end

    when 'h'
      selection_open_htop

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

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

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

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

    when 'p'
      paused? ? unpause : pause

    when '/'
      search_start_focus

    when Tui::Key::ESCAPE

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

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

#pauseObject (protected)



928
929
930
# File 'lib/osctl/cli/top/tui/main.rb', line 928

def pause
  @paused = true
end

#paused?Boolean (protected)

Returns:

  • (Boolean)


936
937
938
# File 'lib/osctl/cli/top/tui/main.rb', line 936

def paused?
  @paused
end


443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
# File 'lib/osctl/cli/top/tui/main.rb', line 443

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

  cpu = (rt? ? format_percent(ct[:cpu_usage]) : humanize_time_us(ct[:cpu_us])).to_s
  cpu << "/#{ct[:cpu_package_inuse]}" if ct[:cpu_package_inuse]

  print_row_data([
    cpu,
    humanize_data(ct[:memory]),
    ct[:nproc],
    humanize_data(ct[:zfsio][:bytes][:r]),
    humanize_number(ct[:zfsio][:ios][:r]),
    humanize_data(ct[:zfsio][:bytes][:w]),
    humanize_number(ct[:zfsio][:ios][:w]),
    humanize_data(rt? ? ct[:tx][:bytes] * 8 : ct[:tx][:bytes]),
    humanize_data(ct[:tx][:packets]),
    humanize_data(rt? ? ct[:rx][:bytes] * 8 : ct[:rx][:bytes]),
    humanize_data(ct[:rx][:packets]),
    format_loadavg(ct[:loadavg][0]),
    format_loadavg(ct[:loadavg][1]),
    format_loadavg(ct[:loadavg][2]),
  ])
end


467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
# File 'lib/osctl/cli/top/tui/main.rb', line 467

def print_row_data(values)
  fmts = %w(%9s %8s %6s %6s %6s %6s %6s %6s %6s %6s %6s %5s %5s %5s)
  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, procs_stats, data) ⇒ Object (protected)



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

def render(t, procs_stats, data)
  Curses.setpos(0, 0)
  Curses.addstr("#{File.basename($0)} ct top - #{t.strftime('%H:%M:%S')}")
  Curses.addstr(sprintf(" [%.1fs]", last_measurement - t)) if last_measurement

  lavg = data[:loadavg] ? format_loadavgs(data[:loadavg]) : '?'
  Curses.addstr(" #{model_thread.mode} mode, load average #{lavg}")

  i = status_bar(1, t, procs_stats, data)

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

  ct_count = data[:containers].length
  offset = 0
  view_ct_count = Curses.lines - stats_rows - i

  @view_page_max = ((ct_count - view_ct_count) / (view_ct_count / 2).to_f).ceil

  ct_view =
    if view_page > 0
      offset = (view_ct_count / 2) * view_page

      if offset >= ct_count - view_ct_count
        offset = ct_count - view_ct_count
      end

      data[:containers][offset..-1]
    else
      data[:containers]
    end

  ct_view.each do |ct|
    next if search_active? && !ct[:id].include?(search_string)

    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
    j += 1

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

  stats(data, [offset, view_ct_count, data[:containers].length])

  Curses.refresh
end

#rt?Boolean (protected)

Returns:

  • (Boolean)


867
868
869
# File 'lib/osctl/cli/top/tui/main.rb', line 867

def rt?
  model_thread.mode == :realtime
end

#run_sortObject (protected)



660
661
662
663
664
665
666
# File 'lib/osctl/cli/top/tui/main.rb', line 660

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

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

#search_active?Boolean (protected)

Returns:

  • (Boolean)


827
828
829
# File 'lib/osctl/cli/top/tui/main.rb', line 827

def search_active?
  !@search_string.empty?
end

#search_add(input) ⇒ Object (protected)



831
832
833
# File 'lib/osctl/cli/top/tui/main.rb', line 831

def search_add(input)
  @search_string << input
end

#search_cancelObject (protected)



822
823
824
825
# File 'lib/osctl/cli/top/tui/main.rb', line 822

def search_cancel
  search_end_focus
  @search_string = ''
end

#search_chopObject (protected)



835
836
837
# File 'lib/osctl/cli/top/tui/main.rb', line 835

def search_chop
  @search_string.chop!
end

#search_end_focusObject (protected)



817
818
819
820
# File 'lib/osctl/cli/top/tui/main.rb', line 817

def search_end_focus
  Curses.curs_set(0)  # hide cursor
  @search_input = false
end

#search_in_focus?Boolean (protected)

Returns:

  • (Boolean)


813
814
815
# File 'lib/osctl/cli/top/tui/main.rb', line 813

def search_in_focus?
  @search_input
end

#search_start_focusObject (protected)



806
807
808
809
810
811
# File 'lib/osctl/cli/top/tui/main.rb', line 806

def search_start_focus
  @search_input = true

  Curses.curs_set(1)  # show cursor
  Curses.clear
end

#search_stringObject (protected)



839
840
841
# File 'lib/osctl/cli/top/tui/main.rb', line 839

def search_string
  @search_string
end

#selection_downObject (protected)



692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
# File 'lib/osctl/cli/top/tui/main.rb', line 692

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)



711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
# File 'lib/osctl/cli/top/tui/main.rb', line 711

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_open_htopObject (protected)



731
732
733
# File 'lib/osctl/cli/top/tui/main.rb', line 731

def selection_open_htop
  selection_open_program { %w(htop) }
end

#selection_open_program(&block) ⇒ Object (protected)



735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
# File 'lib/osctl/cli/top/tui/main.rb', line 735

def selection_open_program(&block)
  return unless @current_row

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

  if ct[:id] == '[host]'
    pid = Process.fork do
      Curses.close_screen
      Process.exec(*block.call)
    end
  elsif ct[:init_pid]
    pid = Process.fork do
      sys = OsCtl::Lib::Sys.new

      sys.setns_path(
        File.join('/proc', ct[:init_pid].to_s, 'ns/pid'),
        OsCtl::Lib::Sys::CLONE_NEWPID,
      )
      sys.unshare_ns(OsCtl::Lib::Sys::CLONE_NEWNS)

      # Enter the PID namespace in a child process
      child = Process.fork do
        sys.mount_proc('/proc')

        Curses.close_screen
        Process.exec(*block.call)
      end

      Process.wait(child)
      exit($?.exitstatus)
    end
  else
    return
  end

  Process.wait(pid)

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

#selection_open_topObject (protected)



727
728
729
# File 'lib/osctl/cli/top/tui/main.rb', line 727

def selection_open_top
  selection_open_program { %w(top) }
end

#selection_upObject (protected)



668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
# File 'lib/osctl/cli/top/tui/main.rb', line 668

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)



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

def sort_inverse
  @sort_desc = !@sort_desc
end

#sort_next(n) ⇒ Object (protected)



622
623
624
625
626
627
628
629
630
631
632
633
634
# File 'lib/osctl/cli/top/tui/main.rb', line 622

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)



640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
# File 'lib/osctl/cli/top/tui/main.rb', line 640

def sortable_fields
  ret = []
  ret << (rt? ? :cpu_usage : :cpu_us)
  ret.concat([
    :memory,
    :nproc,
    [:zfsio, :bytes, :r],
    [:zfsio, :ios, :r],
    [:zfsio, :bytes, :w],
    [:zfsio, :ios, :w],
    [:tx, :bytes],
    [:tx, :packets],
    [:rx, :bytes],
    [:rx, :packets],
    [:loadavg, 0],
    [:loadavg, 1],
    [:loadavg, 2],
  ])
end

#sortable_value(ct) ⇒ Object (protected)



618
619
620
# File 'lib/osctl/cli/top/tui/main.rb', line 618

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

#stats(data, ct_view) ⇒ Object (protected)



491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
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
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
# File 'lib/osctl/cli/top/tui/main.rb', line 491

def stats(data, ct_view)
  cts = data[:containers]
  pos = stats_rows

  Curses.setpos(Curses.lines - pos, 0)
  pos -= 1
  Curses.addstr('─' * Curses.cols)
  #Curses.addstr('-' * Curses.cols)

  Curses.setpos(Curses.lines - pos, 0)
  pos -= 1
  Curses.addstr(sprintf('%-14s ', 'Containers:'))
  print_row_data([
    rt? ? format_percent(sum(cts, :cpu_usage, false)) \
        : humanize_time_us(sum(cts, :cpu_us, false)),
    humanize_data(sum(cts, :memory, false)),
    sum(cts, :nproc, false),
    humanize_data(sum(cts, [:zfsio, :bytes, :r], false)),
    humanize_number(sum(cts, [:zfsio, :ios, :r], false)),
    humanize_data(sum(cts, [:zfsio, :bytes, :w], false)),
    humanize_number(sum(cts, [:zfsio, :ios, :w], false)),
    humanize_data(sum(cts, [:tx, :bytes], false) * (rt? ? 8 : 1)),
    humanize_data(sum(cts, [:tx, :packets], false)),
    humanize_data(sum(cts, [:rx, :bytes], false) * (rt? ? 8 : 1)),
    humanize_data(sum(cts, [:rx, :packets], false)),
    format_loadavg(sum(cts, [:loadavg, 0], false)),
    format_loadavg(sum(cts, [:loadavg, 1], false)),
    format_loadavg(sum(cts, [:loadavg, 2], false)),
  ])

  Curses.setpos(Curses.lines - pos, 0)
  pos -= 1
  Curses.addstr(sprintf('%-14s ', 'All:'))
  print_row_data([
    rt? ? format_percent(sum(cts, :cpu_usage, true)) \
        : humanize_time_us(sum(cts, :cpu_us, true)),
    humanize_data(sum(cts, :memory, true)),
    sum(cts, :nproc, true),
    humanize_data(sum(cts, [:zfsio, :bytes, :r], true)),
    humanize_number(sum(cts, [:zfsio, :ios, :r], true)),
    humanize_data(sum(cts, [:zfsio, :bytes, :w], true)),
    humanize_number(sum(cts, [:zfsio, :ios, :w], true)),
    humanize_data(sum(cts, [:tx, :bytes], true) * (rt? ? 8 : 1)),
    humanize_data(sum(cts, [:tx, :packets], true)),
    humanize_data(sum(cts, [:rx, :bytes], true) * (rt? ? 8 : 1)),
    humanize_data(sum(cts, [:rx, :packets], true)),
    data[:loadavg] ? format_loadavg(data[:loadavg][0]) : '-',
    data[:loadavg] ? format_loadavg(data[:loadavg][1]) : '-',
    data[:loadavg] ? format_loadavg(data[:loadavg][2]) : '-',
  ])

  if model_thread.iostat_enabled?
    iostat = data[:zfs] && data[:zfs][:iostat]

    Curses.setpos(Curses.lines - pos, 0)
    pos -= 1
    Curses.addstr(sprintf('%-14s ', 'iostat:'))

    if iostat
      print_row_data([
        '-',
        '-',
        '-',
        humanize_data(iostat[:bytes_read]),
        humanize_data(iostat[:io_read]),
        humanize_data(iostat[:bytes_written]),
        humanize_data(iostat[:io_written]),
        '-',
        '-',
        '-',
        '-',
        '-',
        '-',
        '-',
      ])
    end
  end

  Curses.setpos(Curses.lines - pos, 0)
  pos -= 1
  Curses.addstr('─' * Curses.cols)
  Curses.setpos(Curses.lines - pos, 0)

  search_msg = nil

  fillRow do
    if search_in_focus? || search_active?
      search_msg = "Search: #{search_string}"
      Curses.addstr(search_msg)
      next
    end

    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

  offset, view_count, ct_count = ct_view
  page = sprintf('[%d-%d/%d]', offset + 1, offset + view_count, ct_count)

  Curses.setpos(Curses.lines - pos, Curses.cols - page.length)
  Curses.addstr(page)

  if search_in_focus?
    Curses.setpos(Curses.lines - pos, search_msg.length)
  end
end

#stats_rowsObject (protected)



485
486
487
488
489
# File 'lib/osctl/cli/top/tui/main.rb', line 485

def stats_rows
  i = 5
  i += 1 if model_thread.iostat_enabled?
  i
end

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



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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
# File 'lib/osctl/cli/top/tui/main.rb', line 245

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

  # Processes
  if enable_procs
    Curses.setpos(pos, 0)
    Curses.addstr('Tasks')
    Curses.addstr(sprintf(" [%.1fs]", @last_procs_check - t)) if @last_procs_check
    Curses.addstr(': ')
    bold { Curses.addstr(sprintf('%5d', procs_stats['TOTAL'])) }
    Curses.addstr(' total, ')
    bold { Curses.addstr(sprintf('%3d', procs_stats['R'])) }
    Curses.addstr(' running, ')
    bold { Curses.addstr(sprintf('%3d', procs_stats['D'])) }
    Curses.addstr(' blocked, ')
    bold { Curses.addstr(sprintf('%5d', procs_stats['S'])) }
    Curses.addstr(' sleeping, ')
    bold { Curses.addstr(sprintf('%2d', procs_stats['T'])) }
    Curses.addstr(' stopped, ')
    bold { Curses.addstr(sprintf('%2d', procs_stats['Z'])) }
    Curses.addstr(' zombie ')
    pos += 1
  end

  # 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][:arcstats][: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][:arcstats][: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_thread.containers.count)) }
  Curses.addstr(' total, ')
  bold { Curses.addstr(sprintf('%3d', model_thread.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_thread.containers.count{ |ct| ct.state == :stopping })) }
  Curses.addstr(' stopping, ')
  bold { Curses.addstr(sprintf('%3d', model_thread.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)



843
844
845
846
847
848
849
850
851
852
# File 'lib/osctl/cli/top/tui/main.rb', line 843

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)



932
933
934
# File 'lib/osctl/cli/top/tui/main.rb', line 932

def unpause
  @paused = false
end

#view_page_downObject (protected)



782
783
784
785
786
787
788
# File 'lib/osctl/cli/top/tui/main.rb', line 782

def view_page_down
  @view_page += 1

  if @view_page_max && @view_page > @view_page_max
    @view_page = @view_page_max
  end
end

#view_page_endObject (protected)



798
799
800
801
802
803
804
# File 'lib/osctl/cli/top/tui/main.rb', line 798

def view_page_end
  if view_page_max
    @view_page = view_page_max
  else
    view_page_down
  end
end

#view_page_resetObject (protected)



794
795
796
# File 'lib/osctl/cli/top/tui/main.rb', line 794

def view_page_reset
  @view_page = 0
end

#view_page_upObject (protected)



790
791
792
# File 'lib/osctl/cli/top/tui/main.rb', line 790

def view_page_up
  @view_page -= 1 if @view_page > 0
end