Class: OsCtl::Cli::Top::Tui::ProcessThread

Inherits:
Object
  • Object
show all
Defined in:
lib/osctl/cli/top/tui/process_thread.rb

Instance Method Summary collapse

Constructor Details

#initialize(rate) ⇒ ProcessThread

Returns a new instance of ProcessThread.



5
6
7
8
9
10
11
# File 'lib/osctl/cli/top/tui/process_thread.rb', line 5

def initialize(rate)
  @rate = rate
  @stats = empty_stats
  @last_probe = nil
  @queue = OsCtl::Lib::Queue.new
  @mutex = Mutex.new
end

Instance Method Details

#empty_statsObject (protected)



69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/osctl/cli/top/tui/process_thread.rb', line 69

def empty_stats
  {
    'TOTAL' => 0,
    'R' => 0,
    'S' => 0,
    'D' => 0,
    'Z' => 0,
    'T' => 0,
    't' => 0,
    'X' => 0
  }
end

#get_statsObject



27
28
29
30
31
# File 'lib/osctl/cli/top/tui/process_thread.rb', line 27

def get_stats
  @mutex.synchronize do
    [@stats, @last_probe]
  end
end

#probe_processesObject (protected)



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/osctl/cli/top/tui/process_thread.rb', line 46

def probe_processes
  new_stats = empty_stats
  cnt = 0

  OsCtl::Lib::ProcessList.each(parse_status: false) do |p|
    break if @stop

    st = p.state

    cnt += 1
    new_stats[st] += 1 if new_stats.has_key?(st)
  end

  return if @stop

  new_stats['TOTAL'] = cnt

  @mutex.synchronize do
    @stats = new_stats
    @last_probe = Time.now
  end
end

#startObject



13
14
15
16
# File 'lib/osctl/cli/top/tui/process_thread.rb', line 13

def start
  @stop = false
  @thread = Thread.new { work }
end

#stopObject



18
19
20
21
22
23
24
25
# File 'lib/osctl/cli/top/tui/process_thread.rb', line 18

def stop
  return unless @thread

  @stop = true
  @queue << :stop
  @thread.join
  @thread = nil
end

#workObject (protected)



35
36
37
38
39
40
41
42
43
44
# File 'lib/osctl/cli/top/tui/process_thread.rb', line 35

def work
  probe_processes

  loop do
    cmd = @queue.pop(timeout: @rate)
    return if cmd == :stop

    probe_processes
  end
end