linux_stat 0.6.1 → 0.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,7 +1,9 @@
1
1
  module LinuxStat
2
2
  module Process
3
3
  class << self
4
+ ##
4
5
  # Returns the list of processes from /proc/.
6
+ #
5
7
  # The return type is an Array of Integers.
6
8
  def list
7
9
  Dir['/proc/*'].select! { |x|
@@ -10,12 +12,15 @@ module LinuxStat
10
12
  }.map! { |x| File.split(x)[-1].to_i }
11
13
  end
12
14
 
15
+ ##
13
16
  # Counts and returns the total number of process running on the system.
17
+ #
14
18
  # The return type is Integer.
15
19
  def count
16
20
  list.count
17
21
  end
18
22
 
23
+ ##
19
24
  # Returns all the id of processes mapped with their names as a Hash.
20
25
  def names
21
26
  list.reduce({}) { |h, x|
@@ -27,6 +32,7 @@ module LinuxStat
27
32
  }
28
33
  end
29
34
 
35
+ ##
30
36
  # Returns all the id of processes mapped with their status as a Hash.
31
37
  def types
32
38
  list.reduce({}) { |h, x|
@@ -46,6 +52,7 @@ module LinuxStat
46
52
  }
47
53
  end
48
54
 
55
+ ##
49
56
  # Returns all the id of processes that are sleeping.
50
57
  # The return type is an Array of Integers.
51
58
  def sleeping
@@ -58,6 +65,7 @@ module LinuxStat
58
65
  }
59
66
  end
60
67
 
68
+ ##
61
69
  # Returns all the id of processes that are idle.
62
70
  # The return type is an Array of Integers.
63
71
  def idle
@@ -70,6 +78,7 @@ module LinuxStat
70
78
  }
71
79
  end
72
80
 
81
+ ##
73
82
  # Returns all the id of processes that are zombies.
74
83
  # The return type is an Array of Integers.
75
84
  def zombie
@@ -82,6 +91,7 @@ module LinuxStat
82
91
  }
83
92
  end
84
93
 
94
+ ##
85
95
  # Returns all the id of processes that are running.
86
96
  # The return type is an Array of Integers.
87
97
  def running
@@ -1,12 +1,20 @@
1
1
  module LinuxStat
2
2
  module ProcessInfo
3
3
  class << self
4
- # total_io(pid = $$)
4
+ ##
5
+ # = total_io(pid = $$)
6
+ #
5
7
  # Where pid is the process ID.
8
+ #
6
9
  # By default it is the id of the current process ($$)
7
10
  #
8
11
  # It retuns the total read/write caused by a process.
9
- # The output is Hash. For example, a sample output:
12
+ #
13
+ # The output is Hash.
14
+ #
15
+ # For example:
16
+ # LinuxStat::ProcessInfo.total_io
17
+ #
10
18
  # {:read_bytes=>0, :write_bytes=>0}
11
19
  #
12
20
  # The output is only based on the total disk IO the process has done.
@@ -28,12 +36,18 @@ module LinuxStat
28
36
  out
29
37
  end
30
38
 
31
- # cmdline(pid = $$)
39
+ ##
40
+ # = cmdline(pid = $$)
41
+ #
32
42
  # Where pid is the process ID.
43
+ #
33
44
  # By default it is the id of the current process ($$)
34
45
  #
35
46
  # It retuns the total command of the process.
36
- # The output is String. For example, a sample output:
47
+ #
48
+ # The output is String. For example:
49
+ # LinuxStat::ProcessInfo.cmdline
50
+ #
37
51
  # "ruby -r linux_stat -e p LinuxStat::ProcessInfo.cmdline"
38
52
  #
39
53
  # If the info isn't available it will return an empty frozen String.
@@ -46,12 +60,18 @@ module LinuxStat
46
60
  _cmdline.tap(&:strip!)
47
61
  end
48
62
 
49
- # command_name(pid = $$)
63
+ ##
64
+ # = command_name(pid = $$)
65
+ #
50
66
  # Where pid is the process ID.
67
+ #
51
68
  # By default it is the id of the current process ($$)
52
69
  #
53
70
  # It retuns the total command name of the process.
54
- # The output is String. For example, a sample output:
71
+ #
72
+ # The output is String. For example:
73
+ # LinuxStat::ProcessInfo.command_name
74
+ #
55
75
  # "ruby"
56
76
  #
57
77
  # If the info isn't available it will return an empty frozen String.
@@ -65,147 +85,165 @@ module LinuxStat
65
85
  File.split(_cmdline.tap(&:strip!).split[0])[-1]
66
86
  end
67
87
 
68
- # mem_stat(pid = $$)
88
+ ##
89
+ # = mem_stat(pid = $$)
90
+ #
69
91
  # Where pid is the process ID.
92
+ #
70
93
  # By default it is the id of the current process ($$)
71
94
  #
72
95
  # It retuns the memory, virtual memory, and resident memory of the process.
73
- # All values are in Kilobytes.
74
96
  #
75
- # The output is a Hash. For example, a sample output:
76
- # {:memory=>8656, :virtual_memory=>78272, :resident_memory=>14072}
97
+ # All values are in kilobytes.
98
+ #
99
+ # The output is a Hash. For example:
100
+ # LinuxStat::ProcessInfo.mem_stat
101
+ #
102
+ # {:memory=>8515.584, :virtual_memory=>79781.888, :resident_memory=>13955.072}
77
103
  #
78
- # Note:
79
- # If you need only memory usage of a process, run LinuxStat::ProcessInfo.memory(pid)
80
- # If you need only virtual memory for a process, run LinuxStat::ProcessInfo.virtual_memory(pid)
81
- # If you need only resident memory of a process, run LinuxStat::ProcessInfo.resident_memory(pid)
104
+ # * Note:
105
+ # 1. If you need only memory usage of a process, run LinuxStat::ProcessInfo.memory(pid)
106
+ # 2. If you need only virtual memory for a process, run LinuxStat::ProcessInfo.virtual_memory(pid)
107
+ # 3. If you need only resident memory of a process, run LinuxStat::ProcessInfo.resident_memory(pid)
82
108
  #
83
109
  # This method opens opens multiple files.
110
+ #
84
111
  # But if you need all of the info, then running this method once is efficient.
85
112
  #
86
113
  # If the info isn't available it will return an empty Hash.
87
114
  def mem_stat(pid = $$)
88
- stat_file = "/proc/#{pid}/stat".freeze
89
- status_file = "/proc/#{pid}/status".freeze
90
-
91
- stat = if File.readable?(stat_file)
92
- IO.read(stat_file).split
93
- else
94
- []
95
- end
115
+ statm = "/proc/#{pid}/statm".freeze
116
+ return {} unless File.readable?(statm)
96
117
 
97
- status = if File.readable?(status_file)
98
- IO.readlines(status_file)
99
- else
100
- []
101
- end
118
+ data = IO.read(statm).split
102
119
 
103
- _rss_anon = status.find { |x| x.start_with?('RssAnon') }
104
- rss_anon = _rss_anon ? _rss_anon.split[1].to_i : nil
105
-
106
- _virtual_memory = stat[22]
107
- vm = _virtual_memory ? _virtual_memory.to_i.fdiv(1024).to_i : nil
108
-
109
- _vm_rss = status.find { |x| x.start_with?('VmRSS') }
110
- vm_rss = _vm_rss ? _vm_rss.split[1].to_i : nil
120
+ _rss_anon = (data[1] && data[2]) ? data[1].to_i.-(data[2].to_i).*(pagesize).fdiv(1000) : nil
121
+ _virtual_memory = data[0] ? data[0].to_i*(pagesize).fdiv(1000) : nil
122
+ _resident_memory = data[1] ? data[1].to_i.*(pagesize).fdiv(1000) : nil
111
123
 
112
124
  {
113
- memory: rss_anon,
114
- virtual_memory: vm,
115
- resident_memory: vm_rss
125
+ memory: _rss_anon,
126
+ virtual_memory: _virtual_memory,
127
+ resident_memory: _resident_memory
116
128
  }
117
129
  end
118
130
 
119
- # memory(pid = $$)
131
+ ##
132
+ # = memory(pid = $$)
133
+ #
120
134
  # Where pid is the process ID.
135
+ #
121
136
  # By default it is the id of the current process ($$)
122
137
  #
123
138
  # It retuns the memory of the process.
124
- # The value is in Kilobytes.
125
- # The output is an Integer. For example, a sample output:
126
- # 8664
139
+ # The value is in kilobytes.
140
+ #
141
+ # The output is an Integer. For example:
142
+ # LinuxStat::ProcessInfo.memory
143
+ #
144
+ # 8523.776
127
145
  #
128
146
  # If the info isn't available it will return nil.
129
147
  def memory(pid = $$)
130
- file = "/proc/#{pid}/status".freeze
148
+ file = "/proc/#{pid}/statm".freeze
131
149
  return nil unless File.readable?(file)
132
150
 
133
- _rss_anon = IO.readlines(file).find { |x| x.start_with?('RssAnon') }
134
- _rss_anon ? _rss_anon.split[1].to_i : nil
151
+ data = IO.read(file).split
152
+ (data[1] && data[2]) ? data[1].to_i.-(data[2].to_i).*(pagesize).fdiv(1000) : nil
135
153
  end
136
154
 
137
- # virtual_memory(pid = $$)
155
+ ##
156
+ # = virtual_memory(pid = $$)
157
+ #
138
158
  # Where pid is the process ID.
159
+ #
139
160
  # By default it is the id of the current process ($$)
140
161
  #
141
162
  # It retuns the virtual memory for the process.
142
- # The value is in Kilobytes.
143
- # The output is an Integer. For example, a sample output:
144
- # 78376
163
+ #
164
+ # The value is in kilobytes.
165
+ #
166
+ # The output is an Integer. For example:
167
+ # LinuxStat::ProcessInfo.virtual_memory
168
+ #
169
+ # 79781.888
145
170
  #
146
171
  # If the info isn't available it will return nil.
147
172
  def virtual_memory(pid = $$)
148
- file = "/proc/#{pid}/stat".freeze
173
+ file = "/proc/#{pid}/statm".freeze
149
174
  return nil unless File.readable?(file)
150
175
 
151
- _virtual_memory = IO.read(file).split[22]
152
- _virtual_memory ? _virtual_memory.to_i.fdiv(1024).to_i : nil
176
+ _virtual_memory = IO.read(file).split[0]
177
+ _virtual_memory ? _virtual_memory.to_i.*(pagesize).fdiv(1000) : nil
153
178
  end
154
179
 
155
- # resident_memory(pid = $$)
180
+ ##
181
+ # = resident_memory(pid = $$)
182
+ #
156
183
  # Where pid is the process ID.
184
+ #
157
185
  # By default it is the id of the current process ($$)
158
186
  #
159
187
  # It retuns the resident memory for the process.
160
- # The value is in Kilobytes.
161
- # The output is an Integer. For example, a sample output:
162
- # 14012
188
+ #
189
+ # The value is in kilobytes.
190
+ #
191
+ # The output is an Integer. For example:
192
+ # LinuxStat::ProcessInfo.cpu_stat
193
+ #
194
+ # => 13996.032
163
195
  #
164
196
  # If the info isn't available it will return nil.
165
197
  def resident_memory(pid = $$)
166
- file = "/proc/#{pid}/status".freeze
198
+ file = "/proc/#{pid}/statm".freeze
167
199
  return nil unless File.readable?(file)
168
200
 
169
- _vm_rss = IO.readlines(file)
170
- .find { |x| x.start_with?('VmRSS') }
171
-
172
- _vm_rss ? _vm_rss.split[1].to_i : nil
201
+ _vm_rss = IO.read(file).split[1]
202
+ _vm_rss ? _vm_rss.to_i.*(pagesize).fdiv(1000) : nil
173
203
  end
174
204
 
175
- # cpu_stat(pid: $$, sleep: 1.0 / LinuxStat::Sysconf.sc_clk_tck)
205
+ ##
206
+ # = cpu_stat(pid: $$, sleep: 1.0 / LinuxStat::Sysconf.sc_clk_tck)
207
+ #
176
208
  # Where pid is the process ID and sleep time is the interval between measurements.
177
209
  #
178
210
  # By default it is the id of the current process ($$), and sleep is LinuxStat::Sysconf.sc_clk_tck
211
+ #
179
212
  # The smallest amount of available sleep time is 1.0 / LinuxStat::Sysconf.sc_clk_tck.
180
213
  #
181
- # Note 1:
182
- # Do note that the sleep time can slow down your application.
183
- # And it's only needed for the cpu usage calculation.
214
+ # * Note 1:
215
+ # 1. Do note that the sleep time can slow down your application.
216
+ # 2. And it's only needed for the cpu usage calculation.
184
217
  #
185
218
  # It retuns the CPU usage, threads, and the last executed CPU in Hash.
219
+ #
186
220
  # For example:
187
- # {:cpu_usage=>0.0, :threads=>1, :last_executed_cpu=>1}
221
+ # LinuxStat::ProcessInfo.cpu_stat
222
+ #
223
+ # => {:cpu_usage=>0.0, :threads=>1, :last_executed_cpu=>1}
188
224
  #
189
225
  # But if the info isn't available, it will return an empty Hash.
190
226
  #
191
- # The :cpu_usage is in percentage. It's also divided with the number
192
- # of CPU. :cpu_usage for example, will return 25.0 if the CPU count
227
+ # The :cpu_usage is in percentage.
228
+ # It's also divided with the number of CPU.
229
+ #
230
+ # :cpu_usage for example, will return 25.0 if the CPU count
193
231
  # is 4, and the process is using 100% of a thread / core.
194
- # A value of 100.0 indicates it is using 100% processing power.
232
+ #
233
+ # A value of 100.0 indicates it is using 100% processing power available to the system.
195
234
  #
196
235
  # The :threads returns the number of threads for the process.
197
236
  # The value is a Integer.
198
237
  #
199
- # Note 2:
200
- # If you just need the CPU usage run LinuxStat::ProcessInfo.cpu_usage(pid = $$)
201
- # If you just need the threads run LinuxStat::ProcessInfo.threads(pid = $$)
202
- # If you just need the last executed CPU run LinuxStat::ProcessInfo.last_executed_cpu(pid = $$)
203
- # Running this method is slower and it opens multiple files at once
238
+ # * Note 2:
239
+ # 1. If you just need the CPU usage run LinuxStat::ProcessInfo.cpu_usage(pid = $$)
240
+ # 2. If you just need the threads run LinuxStat::ProcessInfo.threads(pid = $$)
241
+ # 3. If you just need the last executed CPU run LinuxStat::ProcessInfo.last_executed_cpu(pid = $$)
242
+ # 4. Running this method is slower and it opens multiple files at once
204
243
  #
205
244
  # Only use this method if you need all of the data at once, in such case, it's more efficient to use this method.
206
245
  #
207
- # The :last_executed_cpu also returns an Integer indicating
208
- # the last executed cpu of the process.
246
+ # The :last_executed_cpu also returns an Integer indicating the last executed cpu of the process.
209
247
  def cpu_stat(pid: $$, sleep: ticks_to_ms)
210
248
  file = "/proc/#{pid}/stat"
211
249
  return {} unless File.readable?(file)
@@ -238,16 +276,24 @@ module LinuxStat
238
276
  }
239
277
  end
240
278
 
241
- # cpu_usage(pid: $$, sleep: 1.0 / LinuxStat::Sysconf.sc_clk_tck)
279
+ ##
280
+ # = cpu_usage(pid: $$, sleep: 1.0 / LinuxStat::Sysconf.sc_clk_tck)
281
+ #
242
282
  # Where pid is the process ID and sleep time is the interval between measurements.
243
283
  #
244
284
  # By default it is the id of the current process ($$), and sleep is 1.0 / LinuxStat::Sysconf.sc_clk_tck
285
+ #
245
286
  # The smallest amount of available sleep time is LinuxStat::Sysconf.sc_clk_tck.
246
287
  #
247
288
  # It retuns the CPU usage in Float.
289
+ #
248
290
  # For example:
249
- # 10.0
250
- # A value of 100.0 indicates it is using 100% processing power.
291
+ #
292
+ # LinuxStat::ProcessInfo.cpu_usage
293
+ #
294
+ # => 10.0
295
+ #
296
+ # A value of 100.0 indicates it is using 100% processing power available to the system.
251
297
  #
252
298
  # But if the info isn't available, it will return nil.
253
299
  #
@@ -278,13 +324,20 @@ module LinuxStat
278
324
  totald.-(idle2 - idle1).fdiv(totald).*(100).round(2).abs./(LinuxStat::CPU.count)
279
325
  end
280
326
 
281
- # threads(pid = $$)
327
+ ##
328
+ # = threads(pid = $$)
329
+ #
282
330
  # Where pid is the process ID.
331
+ #
283
332
  # By default it is the id of the current process ($$)
284
333
  #
285
334
  # It retuns the threads for the current process in Integer.
335
+ #
286
336
  # For example:
287
- # 1
337
+ # LinuxStat::ProcessInfo.threads
338
+ #
339
+ # => 2
340
+ #
288
341
  # But if the info isn't available, it will return nil.
289
342
  #
290
343
  # This method is way more efficient than running LinuxStat::ProcessInfo.cpu_stat()
@@ -292,16 +345,24 @@ module LinuxStat
292
345
  file = "/proc/#{pid}/stat".freeze
293
346
  return nil unless File.readable?(file)
294
347
 
295
- IO.read(file).split[19].to_i
348
+ data = IO.foreach(file, ' '.freeze).first(20)[-1]
349
+ data ? data.to_i : nil
296
350
  end
297
351
 
298
- # last_executed_cpu(pid = $$)
352
+ ##
353
+ # = last_executed_cpu(pid = $$)
354
+ #
299
355
  # Where pid is the process ID.
356
+ #
300
357
  # By default it is the id of the current process ($$)
301
358
  #
302
359
  # It retuns the last executed CPU in Integer.
360
+ #
303
361
  # For example:
304
- # 2
362
+ # LinuxStat::ProcessInfo.last_executed_cpu
363
+ #
364
+ # => 2
365
+ #
305
366
  # But if the info isn't available, it will return nil.
306
367
  #
307
368
  # This method is way more efficient than running LinuxStat::ProcessInfo.cpu_stat()
@@ -312,7 +373,9 @@ module LinuxStat
312
373
  IO.read(file).split[38].to_i
313
374
  end
314
375
 
315
- # uid(pid = $$)
376
+ ##
377
+ # = uid(pid = $$)
378
+ #
316
379
  # returns the UIDs of the process as an Array of Integers.
317
380
  #
318
381
  # If the info isn't available it returns an empty Array.
@@ -320,7 +383,7 @@ module LinuxStat
320
383
  file = "/proc/#{pid}/status".freeze
321
384
  return nil unless File.readable?(file)
322
385
 
323
- data = IO.readlines(file.freeze).find { |x|
386
+ data = IO.foreach(file.freeze).find { |x|
324
387
  x[/Uid.*\d*/]
325
388
  }.to_s.split.drop(1)
326
389
 
@@ -332,16 +395,19 @@ module LinuxStat
332
395
  }
333
396
  end
334
397
 
335
- # gid(pid = $$)
398
+ ##
399
+ # = gid(pid = $$)
400
+ #
336
401
  # returns the GIDs of the process as an Hash containing the following data:
337
- # :real, :effective, :saved_set, :filesystem_uid
338
402
  #
339
- # If the info isn't available it returns an empty Hash.
403
+ # :real, :effective, :saved_set, :filesystem_uid
404
+ #
405
+ # If the info isn't available or the argument passed doesn't exist as a process ID, it will return an empty Hash.
340
406
  def gid(pid = $$)
341
407
  file = "/proc/#{pid}/status".freeze
342
408
  return nil unless File.readable?(file)
343
409
 
344
- data = IO.readlines(file.freeze).find { |x|
410
+ data = IO.foreach(file.freeze).find { |x|
345
411
  x[/Gid.*\d*/]
346
412
  }.split.drop(1)
347
413
 
@@ -353,20 +419,109 @@ module LinuxStat
353
419
  }
354
420
  end
355
421
 
356
- # owner(pid = $$)
422
+ ##
423
+ # = owner(pid = $$)
424
+ #
357
425
  # Returns the owner of the process
358
426
  # But if the status is not available, it will return an empty frozen String.
359
427
  def owner(pid = $$)
360
428
  file = "/proc/#{pid}/status".freeze
361
429
  return ''.freeze unless File.readable?(file)
362
430
 
363
- gid = IO.readlines(file.freeze).find { |x|
431
+ gid = IO.foreach(file.freeze).find { |x|
364
432
  x[/Gid.*\d*/]
365
433
  }.split.drop(1)[2].to_i
366
434
 
367
435
  LinuxStat::User.username_by_gid(gid)
368
436
  end
369
437
 
438
+ ##
439
+ # = start_time(pid = $$)
440
+ #
441
+ # Returns the time (as Time object) the process was started.
442
+ #
443
+ # For example:
444
+ # LinuxStat::ProcessInfo.start_time 14183
445
+ #
446
+ # => 2020-12-16 13:31:43.559061275 +0000
447
+ #
448
+ # If the info isn't available or the argument passed doesn't exist as a process ID, it will return nil.
449
+ #
450
+ # The timezone returned based on current TZ.
451
+ # Thus the timezone could be affected by changing the ENV['TZ'] variable.
452
+ #
453
+ # Don't trust the timezone returned by the time.
454
+ def start_time(pid = $$)
455
+ stat_file = "/proc/#{pid}/stat".freeze
456
+ uptime = "/proc/uptime".freeze
457
+
458
+ @@u_readable ||= File.readable?(uptime)
459
+ return nil unless @@u_readable && File.readable?(stat_file)
460
+
461
+ Time.now.-(IO.foreach(uptime, ' '.freeze).next.to_i - (IO.read(stat_file).split[21].to_i / get_ticks))
462
+ end
463
+
464
+ ##
465
+ # = running_time(pid = $$)
466
+ #
467
+ # Returns the time (in seconds, as Float) the process is running for.
468
+ #
469
+ # For example:
470
+ # LinuxStat::ProcessInfo.running_time 14183
471
+ #
472
+ # => 1947.619999999999
473
+ #
474
+ # If the info isn't available or the argument passed doesn't exist as a process ID, it will return nil.
475
+ def running_time(pid = $$)
476
+ stat_file = "/proc/#{pid}/stat".freeze
477
+ uptime = "/proc/uptime".freeze
478
+
479
+ @@u_readable ||= File.readable?(uptime)
480
+ return nil unless @@u_readable && File.readable?(stat_file)
481
+
482
+ IO.foreach(uptime, ' '.freeze).next.to_f - (IO.read(stat_file).split[21].to_i / get_ticks)
483
+ end
484
+
485
+ ##
486
+ # = state(pid = $$)
487
+ # Returns the state of the process as a frozen String
488
+ #
489
+ # * A process could have multiple states:
490
+ #
491
+ # 1. S => Sleeping
492
+ #
493
+ # 2. R => Running
494
+ #
495
+ # 3. I => Idle
496
+ #
497
+ # 4. Z => Zombie
498
+ #
499
+ # It returns any one of them.
500
+ #
501
+ # If the info isn't available or the argument passed doesn't exist as a process ID,
502
+ # it will return an empty String.
503
+ def state(pid = $$)
504
+ file = "/proc/#{pid}/stat".freeze
505
+ return ''.freeze unless File.readable?(file)
506
+ IO.foreach(file, ' '.freeze).first(3)[-1].tap(&:rstrip!).freeze
507
+ end
508
+
509
+ ##
510
+ # = nice(pid = $$)
511
+ # Returns the nice of the process
512
+ #
513
+ # The output value is an Integer ranging from -20 to 19
514
+ #
515
+ # -20 means the process has high priority, and 19 means the process has low priority
516
+ #
517
+ # If the info isn't available or the argument passed doesn't exist as a process ID, it will return nil.
518
+ def nice(pid = $$)
519
+ file = "/proc/#{pid}/stat"
520
+ return nil unless File.readable?(file)
521
+
522
+ IO.foreach(file, ' ').first(19)[-1].to_i
523
+ end
524
+
370
525
  private
371
526
  def get_ticks
372
527
  @@ticks ||= Sysconf.sc_clk_tck
@@ -376,6 +531,10 @@ module LinuxStat
376
531
  def ticks_to_ms
377
532
  @@ms ||= 1.0 / get_ticks
378
533
  end
534
+
535
+ def pagesize
536
+ @@pagesize ||= LinuxStat::Sysconf.pagesize
537
+ end
379
538
  end
380
539
  end
381
540
  end