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

def initialize(model, rate, enable_procs: true)
  super()
  @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.



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

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.



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

def last_count
  @last_count
end

#last_dataObject (protected)

Returns the value of attribute last_data.



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

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

#search_stringObject (readonly, protected)

Returns the value of attribute search_string.



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

def search_string
  @search_string
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)



890
891
892
893
894
# File 'lib/osctl/cli/top/tui/main.rb', line 890

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

#cursorObject (protected)



902
903
904
905
906
907
908
909
910
911
912
913
914
915
# File 'lib/osctl/cli/top/tui/main.rb', line 902

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

#fill_rowObject (protected)



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

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

#format_ctid(ctid) ⇒ Object (protected)



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

def format_ctid(ctid)
  if ctid.length > 12
    "#{ctid[0..11]}.."
  else
    ctid
  end
end

#format_loadavg(lavg) ⇒ Object (protected)



877
878
879
880
881
882
883
884
885
886
887
888
# File 'lib/osctl/cli/top/tui/main.rb', line 877

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

  format(fmt, lavg)
end

#format_loadavgs(lavgs) ⇒ Object (protected)



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

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

#get_dataObject (protected)



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

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)



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

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

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

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

    ret << format(
      '%-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)



848
849
850
851
852
853
854
855
# File 'lib/osctl/cli/top/tui/main.rb', line 848

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



918
919
920
# File 'lib/osctl/cli/top/tui/main.rb', line 918

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

#modeObject (protected)



857
858
859
# File 'lib/osctl/cli/top/tui/main.rb', line 857

def mode
  model_thread.mode
end

#openObject



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

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)

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

                          else
                            modes[i + 1]
                          end

    when 'p'
      paused? ? unpause : pause

    when '/'
      search_start_focus

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

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

#pauseObject (protected)



922
923
924
# File 'lib/osctl/cli/top/tui/main.rb', line 922

def pause
  @paused = true
end

#paused?Boolean (protected)

Returns:

  • (Boolean)


930
931
932
# File 'lib/osctl/cli/top/tui/main.rb', line 930

def paused?
  @paused
end


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

def print_row(ct)
  Curses.addstr(format('%-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


463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
# File 'lib/osctl/cli/top/tui/main.rb', line 463

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 = format("#{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)



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

def render(t, procs_stats, data)
  Curses.setpos(0, 0)
  Curses.addstr("#{File.basename($0)} ct top - #{t.strftime('%H:%M:%S')}")
  Curses.addstr(format(' [%.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..]
    else
      data[:containers]
    end

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

    Curses.setpos(i, 0)

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

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

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

           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)


861
862
863
# File 'lib/osctl/cli/top/tui/main.rb', line 861

def rt?
  model_thread.mode == :realtime
end

#run_sortObject (protected)



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

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)


825
826
827
# File 'lib/osctl/cli/top/tui/main.rb', line 825

def search_active?
  !@search_string.empty?
end

#search_add(input) ⇒ Object (protected)



829
830
831
# File 'lib/osctl/cli/top/tui/main.rb', line 829

def search_add(input)
  @search_string << input
end

#search_cancelObject (protected)



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

def search_cancel
  search_end_focus
  @search_string = ''
end

#search_chopObject (protected)



833
834
835
# File 'lib/osctl/cli/top/tui/main.rb', line 833

def search_chop
  @search_string.chop!
end

#search_end_focusObject (protected)



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

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

#search_in_focus?Boolean (protected)

Returns:

  • (Boolean)


811
812
813
# File 'lib/osctl/cli/top/tui/main.rb', line 811

def search_in_focus?
  @search_input
end

#search_start_focusObject (protected)



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

def search_start_focus
  @search_input = true

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

#selection_downObject (protected)



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

def selection_down
  if @current_row
    new_row = @current_row + 1

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

                   elsif last_data[:containers].any?
                     0

                   end

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

#selection_highlightObject (protected)



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

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)



729
730
731
# File 'lib/osctl/cli/top/tui/main.rb', line 729

def selection_open_htop
  selection_open_program { %w[htop] }
end

#selection_open_program(&block) ⇒ Object (protected)



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

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)



725
726
727
# File 'lib/osctl/cli/top/tui/main.rb', line 725

def selection_open_top
  selection_open_program { %w[top] }
end

#selection_upObject (protected)



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 670

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

  if @current_row
    new_row = @current_row - 1

    @current_row = if new_row >= 0
                     new_row

                   elsif new_row == -1
                     nil

                   elsif last_data[:containers].any?
                     last_row

                   end

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

#sort_inverseObject (protected)



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

def sort_inverse
  @sort_desc = !@sort_desc
end

#sort_next(n) ⇒ Object (protected)



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

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)



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

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

#sortable_value(ct) ⇒ Object (protected)



620
621
622
# File 'lib/osctl/cli/top/tui/main.rb', line 620

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

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



487
488
489
490
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
606
607
# File 'lib/osctl/cli/top/tui/main.rb', line 487

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(format('%-14s ', 'Containers:'))
  print_row_data([
                   if rt?
                     format_percent(sum(cts, :cpu_usage, false))
                   else
                     humanize_time_us(sum(cts, :cpu_us, false))
                   end,
                   humanize_data(sum(cts, :memory, false)),
                   sum(cts, :nproc, false),
                   humanize_data(sum(cts, %i[zfsio bytes r], false)),
                   humanize_number(sum(cts, %i[zfsio ios r], false)),
                   humanize_data(sum(cts, %i[zfsio bytes w], false)),
                   humanize_number(sum(cts, %i[zfsio ios w], false)),
                   humanize_data(sum(cts, %i[tx bytes], false) * (rt? ? 8 : 1)),
                   humanize_data(sum(cts, %i[tx packets], false)),
                   humanize_data(sum(cts, %i[rx bytes], false) * (rt? ? 8 : 1)),
                   humanize_data(sum(cts, %i[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(format('%-14s ', 'All:'))
  print_row_data([
                   if rt?
                     format_percent(sum(cts, :cpu_usage, true))
                   else
                     humanize_time_us(sum(cts, :cpu_us, true))
                   end,
                   humanize_data(sum(cts, :memory, true)),
                   sum(cts, :nproc, true),
                   humanize_data(sum(cts, %i[zfsio bytes r], true)),
                   humanize_number(sum(cts, %i[zfsio ios r], true)),
                   humanize_data(sum(cts, %i[zfsio bytes w], true)),
                   humanize_number(sum(cts, %i[zfsio ios w], true)),
                   humanize_data(sum(cts, %i[tx bytes], true) * (rt? ? 8 : 1)),
                   humanize_data(sum(cts, %i[tx packets], true)),
                   humanize_data(sum(cts, %i[rx bytes], true) * (rt? ? 8 : 1)),
                   humanize_data(sum(cts, %i[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(format('%-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

  fill_row 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 = format('[%d-%d/%d]', offset + 1, offset + view_count, ct_count)

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

  return unless search_in_focus?

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

#stats_rowsObject (protected)



481
482
483
484
485
# File 'lib/osctl/cli/top/tui/main.rb', line 481

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

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



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

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(format(' [%.1fs]', @last_procs_check - t)) if @last_procs_check
    Curses.addstr(': ')
    bold { Curses.addstr(format('%5d', procs_stats['TOTAL'])) }
    Curses.addstr(' total, ')
    bold { Curses.addstr(format('%3d', procs_stats['R'])) }
    Curses.addstr(' running, ')
    bold { Curses.addstr(format('%3d', procs_stats['D'])) }
    Curses.addstr(' blocked, ')
    bold { Curses.addstr(format('%5d', procs_stats['S'])) }
    Curses.addstr(' sleeping, ')
    bold { Curses.addstr(format('%2d', procs_stats['T'])) }
    Curses.addstr(' stopped, ')
    bold { Curses.addstr(format('%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(format('%5.1f', format_percent(cpu[:user]))) }
    Curses.addstr(' us, ')
    bold { Curses.addstr(format('%5.1f', format_percent(cpu[:system]))) }
    Curses.addstr(' sy, ')
    bold { Curses.addstr(format('%5.1f', format_percent(cpu[:nice]))) }
    Curses.addstr(' ni, ')
    bold { Curses.addstr(format('%5.1f', format_percent(cpu[:idle]))) }
    Curses.addstr(' id, ')
    bold { Curses.addstr(format('%5.1f', format_percent(cpu[:iowait]))) }
    Curses.addstr(' wa, ')
    bold { Curses.addstr(format('%5.1f', format_percent(cpu[:irq]))) }
    Curses.addstr(' hi, ')
    bold { Curses.addstr(format('%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(format('%8s', humanize_data(mem[:total]))) }
    Curses.addstr(' total, ')
    bold { Curses.addstr(format('%8s', humanize_data(mem[:free]))) }
    Curses.addstr(' free, ')
    bold { Curses.addstr(format('%8s', humanize_data(mem[:used]))) }
    Curses.addstr(' used, ')
    bold { Curses.addstr(format('%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(format('%8s', humanize_data(mem[:swap_total]))) }
      Curses.addstr(' total, ')
      bold { Curses.addstr(format('%8s', humanize_data(mem[:swap_free]))) }
      Curses.addstr(' free, ')
      bold { Curses.addstr(format('%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(format('%8s', humanize_data(arc[:c_max]))) }
    Curses.addstr(' c_max, ')
    bold { Curses.addstr(format('%8s', humanize_data(arc[:c]))) }
    Curses.addstr(' c,    ')
    bold { Curses.addstr(format('%8s', humanize_data(arc[:size]))) }
    Curses.addstr(' size, ')
    bold { Curses.addstr(format('%8.2f', format_percent(arc[:hit_rate]))) }
    Curses.addstr(' hitrate, ')
    bold { Curses.addstr(format('%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(format('%8s', humanize_data(l2arc[:size]))) }
    Curses.addstr(' size, ')
    bold { Curses.addstr(format('%8s', humanize_data(l2arc[:asize]))) }
    Curses.addstr(' asize,')
    bold { Curses.addstr(format('%8.2f', humanize_data(l2arc[:hit_rate]))) }
    Curses.addstr(' hitrate, ')
    bold { Curses.addstr(format('%6d', l2arc[:misses])) }
    Curses.addstr(' missed ')
  end

  # Containers
  Curses.setpos(pos += 1, 0)
  Curses.addstr('Containers: ')
  bold { Curses.addstr(format('%3d', model_thread.containers.count)) }
  Curses.addstr(' total, ')
  bold { Curses.addstr(format('%3d', model_thread.containers.count { |ct| ct.state == :starting })) }
  Curses.addstr(' starting, ')
  bold { Curses.addstr(format('%3d', data[:containers].count - 1)) } # -1 for [host]
  Curses.addstr(' running, ')
  bold { Curses.addstr(format('%3d', model_thread.containers.count { |ct| ct.state == :stopping })) }
  Curses.addstr(' stopping, ')
  bold { Curses.addstr(format('%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)



837
838
839
840
841
842
843
844
845
846
# File 'lib/osctl/cli/top/tui/main.rb', line 837

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)



926
927
928
# File 'lib/osctl/cli/top/tui/main.rb', line 926

def unpause
  @paused = false
end

#view_page_downObject (protected)



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

def view_page_down
  @view_page += 1

  return unless @view_page_max && @view_page > @view_page_max

  @view_page = @view_page_max
end

#view_page_endObject (protected)



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

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

#view_page_resetObject (protected)



792
793
794
# File 'lib/osctl/cli/top/tui/main.rb', line 792

def view_page_reset
  @view_page = 0
end

#view_page_upObject (protected)



788
789
790
# File 'lib/osctl/cli/top/tui/main.rb', line 788

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