Class: OsCtl::Cli::Ps::Main
- Inherits:
-
Command
- Object
- Lib::Cli::Command
- Command
- OsCtl::Cli::Ps::Main
show all
- Defined in:
- lib/osctl/cli/ps/main.rb
Defined Under Namespace
Classes: SlowOsProcess
Constant Summary
collapse
- READ_TIMEOUT =
1
Instance Method Summary
collapse
Methods inherited from Command
#cli_opt, #format_output, #osctld_call, #osctld_fmt, #osctld_open, #osctld_resp, run
Instance Method Details
#get_process_list(ctids, filters) ⇒ Object
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
|
# File 'lib/osctl/cli/ps/main.rb', line 104
def get_process_list(ctids, filters)
spinner = TTY::Spinner.new('[:spinner] :title', clear: true)
spinner.update(title: 'Listing processes...')
spinner.auto_spin
list_queue = OsCtl::Lib::Queue.new
list_thread = Thread.new { list_processes(list_queue, ctids, filters) }
update_queue = OsCtl::Lib::Queue.new
update_thread = Thread.new { update_loop(update_queue) }
last_os_proc = nil
last_time = nil
slow_os_procs = []
@update_count = false
total = 0
error = false
ret = nil
loop do
v = list_queue.pop(timeout: READ_TIMEOUT)
if v.is_a?(Array)
ret = v
ret << slow_os_procs
break
end
os_proc = v
if os_proc.nil? && !error
msg =
if last_os_proc.nil?
'Taking long to list /proc entries'
else
format('Taking long to process pid %d', last_os_proc.pid)
end
set_title(spinner, msg)
if last_os_proc
slow_os_procs << SlowOsProcess.new(last_os_proc, 0)
last_time = Time.now - READ_TIMEOUT
end
error = true
next
end
if os_proc && last_time
slow_os_procs.last.duration = Time.now - last_time
last_time = nil
end
total += 1
last_os_proc = os_proc
if os_proc && error
set_title(spinner, format('Listing processes... %8d', total))
error = false
elsif os_proc
set_title(spinner, format('Listing processes... %8d', total))
@update_count = false
end
end
if last_time
slow_os_procs.last.duration = Time.now - last_time
last_time = nil
end
update_queue << :stop
list_thread.join
update_thread.join
spinner.stop
ret
end
|
#list_processes(queue, ctids, filters) ⇒ Object
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
|
# File 'lib/osctl/cli/ps/main.rb', line 184
def list_processes(queue, ctids, filters)
pools = {}
pl = OsCtl::Lib::ProcessList.new(parse_stat: false, parse_status: false) do |p|
queue << p
pool, id = p.ct_id
if !ctids.empty? \
&& !(pool.nil? && ctids[:host]) \
&& !(pool && (ctids.has_key?(id) || ctids.has_key?("#{pool}:#{id}")))
next(false)
end
p.parse
p.cmdline
next(false) if filters.detect { |f| !f.match?(p) }
pools[pool] = true if pool
true
end
queue << [pl, pools]
end
|
#print_slow_processes(slow_os_procs) ⇒ Object
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
|
# File 'lib/osctl/cli/ps/main.rb', line 82
def print_slow_processes(slow_os_procs)
warn "\nSlow processes:"
slow_os_procs.sort do |a, b|
b.duration <=> a.duration
end.each do |slow_proc|
begin
pool, id = slow_proc.os_process.ct_id
ctid = pool.nil? ? '[host]' : "#{pool}:#{id}"
rescue OsCtl::Lib::Exceptions::OsProcessNotFound
ctid = '[unknown]'
end
warn(format(
'%16s: %10d %-18s %.2fs',
ctid,
slow_proc.os_process.pid,
slow_proc.os_process.name,
slow_proc.duration
))
end
end
|
#run ⇒ Object
10
11
12
13
14
15
16
17
18
19
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
|
# File 'lib/osctl/cli/ps/main.rb', line 10
def run
param_selector = OsCtl::Lib::Cli::ParameterSelector.new(
all_params: Ps::Columns::COLS
)
if opts[:list]
puts param_selector
return
end
require_args!(optional: %w[id], strict: false)
filters = opts[:parameter].map { |v| Ps::Filter.new(v) }
ctids = {}
args.each do |arg|
if arg == '-'
ctids[:host] = true
elsif arg.index(':')
pool, id = arg.split(':')
ctids["#{pool}:#{id}"] = true
else
pool = gopts[:pool]
id = arg
if pool
ctids["#{pool}:#{id}"] = true
else
ctids[id] = true
end
end
end
pl, pools, slow_os_procs = get_process_list(ctids, filters)
default_cols =
if ctids.size == 1
Ps::Columns::DEFAULT_ONE_CT
elsif pools.size == 1
Ps::Columns::DEFAULT_ONE_POOL
else
Ps::Columns::DEFAULT_MULTIPLE_POOLS
end
out_cols, out_data = Ps::Columns.generate(
pl,
param_selector.parse_option(opts[:output], default_params: default_cols),
gopts[:parsable]
)
OsCtl::Lib::Cli::OutputFormatter.print(
out_data,
cols: out_cols,
layout: :columns,
header: opts['hide-header'] ? false : true,
sort: opts[:sort] && param_selector.parse_option(opts[:sort])
)
if pl.size > 1
warn "#{pl.size} processes"
elsif pl.size == 1
warn "#{pl.size} process"
else
warn 'No processes found'
end
print_slow_processes(slow_os_procs) if slow_os_procs.any?
end
|
#set_title(spinner, str) ⇒ Object
180
181
182
|
# File 'lib/osctl/cli/ps/main.rb', line 180
def set_title(spinner, str)
spinner.tokens[:title] = format('%-40s', str)
end
|
#update_loop(queue) ⇒ Object
212
213
214
215
216
217
218
|
# File 'lib/osctl/cli/ps/main.rb', line 212
def update_loop(queue)
loop do
return if queue.pop(timeout: 0.2) == :stop
@update_count = true
end
end
|