linux_stat 0.6.2 → 0.7.2

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,12 +1,21 @@
1
1
  module LinuxStat
2
2
  module PrettifyBytes
3
3
  class << self
4
+ ##
4
5
  # Converts a number to decimal byte units and outputs with the metric prefix
5
6
  # For example,
6
7
  #
7
- # LinuxStat::PrettifyBytes.convert_decimal(1000) # => "1.0 kilobyte"
8
- # LinuxStat::PrettifyBytes.convert_decimal(1000 ** 3) # => "1.0 gigabyte"
9
- # LinuxStat::PrettifyBytes.convert_decimal(1024 ** 3) # => "1.07 gigabytes"
8
+ # LinuxStat::PrettifyBytes.convert_decimal(1000)
9
+ #
10
+ # => "1.0 kilobyte"
11
+ #
12
+ # LinuxStat::PrettifyBytes.convert_decimal(1000 ** 3)
13
+ #
14
+ # => "1.0 gigabyte"
15
+ #
16
+ # LinuxStat::PrettifyBytes.convert_decimal(1024 ** 3)
17
+ #
18
+ # => "1.07 gigabytes"
10
19
  def convert_decimal(n)
11
20
  @@d_units ||= %W(#{''} kilo mega giga tera peta exa zetta)
12
21
  .map.with_index { |x, i| [x, 1000.**(i + 1)] }
@@ -19,9 +28,17 @@ module LinuxStat
19
28
  # Converts a number to binary byte units and outputs with the IEC prefix
20
29
  # For example,
21
30
  #
22
- # LinuxStat::PrettifyBytes.convert_binary(1000) # => "1000.0 bytes"
23
- # LinuxStat::PrettifyBytes.convert_binary(1000 ** 3) # => "953.67 mebibytes"
24
- # LinuxStat::PrettifyBytes.convert_binary(1024 ** 3) # => "1.0 gibibyte"
31
+ # LinuxStat::PrettifyBytes.convert_binary(1000)
32
+ #
33
+ # => "1000.0 bytes"
34
+ #
35
+ # LinuxStat::PrettifyBytes.convert_binary(1000 ** 3)
36
+ #
37
+ # => "953.67 mebibytes"
38
+ #
39
+ # LinuxStat::PrettifyBytes.convert_binary(1024 ** 3)
40
+ #
41
+ # => "1.0 gibibyte"
25
42
  def convert_binary(n)
26
43
  @@b_units ||= %W(#{''} kibi mebi gibi tebi pebi exbi zebi)
27
44
  .map.with_index { |x, i| [x, 1024.**(i + 1)] }
@@ -34,9 +51,17 @@ module LinuxStat
34
51
  # Converts a number to decimal byte units
35
52
  # For example,
36
53
  #
37
- # LinuxStat::PrettifyBytes.convert_short_decimal(1000) # => "1.0 kB"
38
- # LinuxStat::PrettifyBytes.convert_short_decimal(1000 ** 3) # => "1.0 GB"
39
- # LinuxStat::PrettifyBytes.convert_short_decimal(1024 ** 3) # => "1.07 GB"
54
+ # LinuxStat::PrettifyBytes.convert_short_decimal(1000)
55
+ #
56
+ # => "1.0 kB"
57
+ #
58
+ # LinuxStat::PrettifyBytes.convert_short_decimal(1000 ** 3)
59
+ #
60
+ # => "1.0 GB"
61
+ #
62
+ # LinuxStat::PrettifyBytes.convert_short_decimal(1024 ** 3)
63
+ #
64
+ # => "1.07 GB"
40
65
  def convert_short_decimal(n)
41
66
  @@sd_units ||= %W(#{''} k M G T P E Z)
42
67
  .map.with_index { |x, i| [x, 1000.**(i + 1)] }
@@ -46,12 +71,22 @@ module LinuxStat
46
71
  "#{pad_left(converted)} #{unit[0]}B"
47
72
  end
48
73
 
74
+ ##
49
75
  # Converts a number to binary byte units
76
+ #
50
77
  # For example,
51
78
  #
52
- # LinuxStat::PrettifyBytes.convert_short_binary(1000) # => "1000 B"
53
- # LinuxStat::PrettifyBytes.convert_short_binary(1000 ** 3) # => "953.67 MiB"
54
- # LinuxStat::PrettifyBytes.convert_short_binary(1024 ** 3) # => "1.0 GiB"
79
+ # LinuxStat::PrettifyBytes.convert_short_binary(1000)
80
+ #
81
+ # => "1000 B"
82
+ #
83
+ # LinuxStat::PrettifyBytes.convert_short_binary(1000 ** 3)
84
+ #
85
+ # => "953.67 MiB"
86
+ #
87
+ # LinuxStat::PrettifyBytes.convert_short_binary(1024 ** 3)
88
+ #
89
+ # => "1.0 GiB"
55
90
  def convert_short_binary(n)
56
91
  return "#{n} B" if n < 1024
57
92
 
@@ -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,33 +85,37 @@ 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.
96
+ #
73
97
  # All values are in kilobytes.
74
98
  #
75
- # The output is a Hash. For example, a sample output:
99
+ # The output is a Hash. For example:
100
+ # LinuxStat::ProcessInfo.mem_stat
101
+ #
76
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
115
  statm = "/proc/#{pid}/statm".freeze
116
+ return {} unless File.readable?(statm)
89
117
 
90
- data = if File.readable?(statm)
91
- IO.read(statm).split
92
- else
93
- return {}
94
- end
118
+ data = IO.read(statm).split
95
119
 
96
120
  _rss_anon = (data[1] && data[2]) ? data[1].to_i.-(data[2].to_i).*(pagesize).fdiv(1000) : nil
97
121
  _virtual_memory = data[0] ? data[0].to_i*(pagesize).fdiv(1000) : nil
@@ -104,13 +128,19 @@ module LinuxStat
104
128
  }
105
129
  end
106
130
 
107
- # memory(pid = $$)
131
+ ##
132
+ # = memory(pid = $$)
133
+ #
108
134
  # Where pid is the process ID.
135
+ #
109
136
  # By default it is the id of the current process ($$)
110
137
  #
111
138
  # It retuns the memory of the process.
112
139
  # The value is in kilobytes.
113
- # The output is an Integer. For example, a sample output:
140
+ #
141
+ # The output is an Integer. For example:
142
+ # LinuxStat::ProcessInfo.memory
143
+ #
114
144
  # 8523.776
115
145
  #
116
146
  # If the info isn't available it will return nil.
@@ -122,13 +152,20 @@ module LinuxStat
122
152
  (data[1] && data[2]) ? data[1].to_i.-(data[2].to_i).*(pagesize).fdiv(1000) : nil
123
153
  end
124
154
 
125
- # virtual_memory(pid = $$)
155
+ ##
156
+ # = virtual_memory(pid = $$)
157
+ #
126
158
  # Where pid is the process ID.
159
+ #
127
160
  # By default it is the id of the current process ($$)
128
161
  #
129
162
  # It retuns the virtual memory for the process.
163
+ #
130
164
  # The value is in kilobytes.
131
- # The output is an Integer. For example, a sample output:
165
+ #
166
+ # The output is an Integer. For example:
167
+ # LinuxStat::ProcessInfo.virtual_memory
168
+ #
132
169
  # 79781.888
133
170
  #
134
171
  # If the info isn't available it will return nil.
@@ -140,14 +177,21 @@ module LinuxStat
140
177
  _virtual_memory ? _virtual_memory.to_i.*(pagesize).fdiv(1000) : nil
141
178
  end
142
179
 
143
- # resident_memory(pid = $$)
180
+ ##
181
+ # = resident_memory(pid = $$)
182
+ #
144
183
  # Where pid is the process ID.
184
+ #
145
185
  # By default it is the id of the current process ($$)
146
186
  #
147
187
  # It retuns the resident memory for the process.
188
+ #
148
189
  # The value is in kilobytes.
149
- # The output is an Integer. For example, a sample output:
150
- # 13996.032
190
+ #
191
+ # The output is an Integer. For example:
192
+ # LinuxStat::ProcessInfo.cpu_stat
193
+ #
194
+ # => 13996.032
151
195
  #
152
196
  # If the info isn't available it will return nil.
153
197
  def resident_memory(pid = $$)
@@ -158,40 +202,48 @@ module LinuxStat
158
202
  _vm_rss ? _vm_rss.to_i.*(pagesize).fdiv(1000) : nil
159
203
  end
160
204
 
161
- # 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
+ #
162
208
  # Where pid is the process ID and sleep time is the interval between measurements.
163
209
  #
164
210
  # By default it is the id of the current process ($$), and sleep is LinuxStat::Sysconf.sc_clk_tck
211
+ #
165
212
  # The smallest amount of available sleep time is 1.0 / LinuxStat::Sysconf.sc_clk_tck.
166
213
  #
167
- # Note 1:
168
- # Do note that the sleep time can slow down your application.
169
- # 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.
170
217
  #
171
218
  # It retuns the CPU usage, threads, and the last executed CPU in Hash.
219
+ #
172
220
  # For example:
173
- # {: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}
174
224
  #
175
225
  # But if the info isn't available, it will return an empty Hash.
176
226
  #
177
- # The :cpu_usage is in percentage. It's also divided with the number
178
- # 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
179
231
  # is 4, and the process is using 100% of a thread / core.
180
- # 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.
181
234
  #
182
235
  # The :threads returns the number of threads for the process.
183
236
  # The value is a Integer.
184
237
  #
185
- # Note 2:
186
- # If you just need the CPU usage run LinuxStat::ProcessInfo.cpu_usage(pid = $$)
187
- # If you just need the threads run LinuxStat::ProcessInfo.threads(pid = $$)
188
- # If you just need the last executed CPU run LinuxStat::ProcessInfo.last_executed_cpu(pid = $$)
189
- # 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
190
243
  #
191
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.
192
245
  #
193
- # The :last_executed_cpu also returns an Integer indicating
194
- # the last executed cpu of the process.
246
+ # The :last_executed_cpu also returns an Integer indicating the last executed cpu of the process.
195
247
  def cpu_stat(pid: $$, sleep: ticks_to_ms)
196
248
  file = "/proc/#{pid}/stat"
197
249
  return {} unless File.readable?(file)
@@ -224,16 +276,24 @@ module LinuxStat
224
276
  }
225
277
  end
226
278
 
227
- # 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
+ #
228
282
  # Where pid is the process ID and sleep time is the interval between measurements.
229
283
  #
230
284
  # By default it is the id of the current process ($$), and sleep is 1.0 / LinuxStat::Sysconf.sc_clk_tck
285
+ #
231
286
  # The smallest amount of available sleep time is LinuxStat::Sysconf.sc_clk_tck.
232
287
  #
233
288
  # It retuns the CPU usage in Float.
289
+ #
234
290
  # For example:
235
- # 10.0
236
- # 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.
237
297
  #
238
298
  # But if the info isn't available, it will return nil.
239
299
  #
@@ -264,13 +324,20 @@ module LinuxStat
264
324
  totald.-(idle2 - idle1).fdiv(totald).*(100).round(2).abs./(LinuxStat::CPU.count)
265
325
  end
266
326
 
267
- # threads(pid = $$)
327
+ ##
328
+ # = threads(pid = $$)
329
+ #
268
330
  # Where pid is the process ID.
331
+ #
269
332
  # By default it is the id of the current process ($$)
270
333
  #
271
334
  # It retuns the threads for the current process in Integer.
335
+ #
272
336
  # For example:
273
- # 1
337
+ # LinuxStat::ProcessInfo.threads
338
+ #
339
+ # => 2
340
+ #
274
341
  # But if the info isn't available, it will return nil.
275
342
  #
276
343
  # This method is way more efficient than running LinuxStat::ProcessInfo.cpu_stat()
@@ -278,17 +345,24 @@ module LinuxStat
278
345
  file = "/proc/#{pid}/stat".freeze
279
346
  return nil unless File.readable?(file)
280
347
 
281
- data = IO.read(file).split[19]
348
+ data = IO.foreach(file, ' '.freeze).first(20)[-1]
282
349
  data ? data.to_i : nil
283
350
  end
284
351
 
285
- # last_executed_cpu(pid = $$)
352
+ ##
353
+ # = last_executed_cpu(pid = $$)
354
+ #
286
355
  # Where pid is the process ID.
356
+ #
287
357
  # By default it is the id of the current process ($$)
288
358
  #
289
359
  # It retuns the last executed CPU in Integer.
360
+ #
290
361
  # For example:
291
- # 2
362
+ # LinuxStat::ProcessInfo.last_executed_cpu
363
+ #
364
+ # => 2
365
+ #
292
366
  # But if the info isn't available, it will return nil.
293
367
  #
294
368
  # This method is way more efficient than running LinuxStat::ProcessInfo.cpu_stat()
@@ -299,7 +373,9 @@ module LinuxStat
299
373
  IO.read(file).split[38].to_i
300
374
  end
301
375
 
302
- # uid(pid = $$)
376
+ ##
377
+ # = uid(pid = $$)
378
+ #
303
379
  # returns the UIDs of the process as an Array of Integers.
304
380
  #
305
381
  # If the info isn't available it returns an empty Array.
@@ -307,7 +383,7 @@ module LinuxStat
307
383
  file = "/proc/#{pid}/status".freeze
308
384
  return nil unless File.readable?(file)
309
385
 
310
- data = IO.readlines(file.freeze).find { |x|
386
+ data = IO.foreach(file.freeze).find { |x|
311
387
  x[/Uid.*\d*/]
312
388
  }.to_s.split.drop(1)
313
389
 
@@ -319,16 +395,19 @@ module LinuxStat
319
395
  }
320
396
  end
321
397
 
322
- # gid(pid = $$)
398
+ ##
399
+ # = gid(pid = $$)
400
+ #
323
401
  # returns the GIDs of the process as an Hash containing the following data:
324
- # :real, :effective, :saved_set, :filesystem_uid
325
402
  #
326
- # 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.
327
406
  def gid(pid = $$)
328
407
  file = "/proc/#{pid}/status".freeze
329
408
  return nil unless File.readable?(file)
330
409
 
331
- data = IO.readlines(file.freeze).find { |x|
410
+ data = IO.foreach(file.freeze).find { |x|
332
411
  x[/Gid.*\d*/]
333
412
  }.split.drop(1)
334
413
 
@@ -340,20 +419,131 @@ module LinuxStat
340
419
  }
341
420
  end
342
421
 
343
- # owner(pid = $$)
422
+ ##
423
+ # = owner(pid = $$)
424
+ #
344
425
  # Returns the owner of the process
345
426
  # But if the status is not available, it will return an empty frozen String.
346
427
  def owner(pid = $$)
347
428
  file = "/proc/#{pid}/status".freeze
348
429
  return ''.freeze unless File.readable?(file)
349
430
 
350
- gid = IO.readlines(file.freeze).find { |x|
431
+ gid = IO.foreach(file.freeze).find { |x|
351
432
  x[/Gid.*\d*/]
352
433
  }.split.drop(1)[2].to_i
353
434
 
354
435
  LinuxStat::User.username_by_gid(gid)
355
436
  end
356
437
 
438
+ ##
439
+ # = start_time_epoch(pid = $$)
440
+ #
441
+ # Returns the epoch time (as Integer) the process was started.
442
+ #
443
+ # For example:
444
+ # LinuxStat::ProcessInfo.start_time_epoch 526
445
+ #
446
+ # => 1608097744
447
+ #
448
+ # If the info isn't available or the argument passed doesn't exist as a process ID, it will return nil.
449
+ def start_time_epoch(pid = $$)
450
+ stat_file = "/proc/#{pid}/stat".freeze
451
+ uptime = "/proc/uptime".freeze
452
+
453
+ @@u_readable ||= File.readable?(uptime)
454
+ return nil unless @@u_readable && File.readable?(stat_file)
455
+
456
+ u = IO.foreach(uptime, ' '.freeze).next.to_f
457
+ st = (IO.foreach(stat_file, ' '.freeze).first(22)[-1].to_f / get_ticks)
458
+
459
+ # Getting two Time objects and dealing with floating point numbers
460
+ # Just to make sure the time goes monotonically
461
+ Time.now.-(u - st).to_i
462
+ end
463
+
464
+ ##
465
+ # = start_time(pid = $$)
466
+ #
467
+ # Returns the time (as Time object) the process was started.
468
+ #
469
+ # For example:
470
+ # LinuxStat::ProcessInfo.start_time 14183
471
+ #
472
+ # => 2020-12-16 13:31:43 +0000
473
+ #
474
+ # If the info isn't available or the argument passed doesn't exist as a process ID, it will return nil.
475
+ #
476
+ # The timezone returned based on current TZ.
477
+ # Thus the timezone could be affected by changing the ENV['TZ'] variable.
478
+ #
479
+ # Don't trust the timezone returned by the time.
480
+ def start_time(pid = $$)
481
+ # Getting two Time objects and dealing with floating point numbers
482
+ # Just to make sure the time goes monotonically
483
+ Time.at(start_time_epoch(pid))
484
+ end
485
+
486
+ ##
487
+ # = running_time(pid = $$)
488
+ #
489
+ # Returns the time (in seconds, as Float) the process is running for.
490
+ #
491
+ # For example:
492
+ # LinuxStat::ProcessInfo.running_time 14183
493
+ #
494
+ # => 1947.619999999999
495
+ #
496
+ # If the info isn't available or the argument passed doesn't exist as a process ID, it will return nil.
497
+ def running_time(pid = $$)
498
+ stat_file = "/proc/#{pid}/stat".freeze
499
+ uptime = "/proc/uptime".freeze
500
+
501
+ @@u_readable ||= File.readable?(uptime)
502
+ return nil unless @@u_readable && File.readable?(stat_file)
503
+
504
+ IO.foreach(uptime, ' '.freeze).next.to_f - (IO.read(stat_file).split[21].to_i / get_ticks)
505
+ end
506
+
507
+ ##
508
+ # = state(pid = $$)
509
+ # Returns the state of the process as a frozen String
510
+ #
511
+ # * A process could have multiple states:
512
+ #
513
+ # 1. S => Sleeping
514
+ #
515
+ # 2. R => Running
516
+ #
517
+ # 3. I => Idle
518
+ #
519
+ # 4. Z => Zombie
520
+ #
521
+ # It returns any one of them.
522
+ #
523
+ # If the info isn't available or the argument passed doesn't exist as a process ID,
524
+ # it will return an empty String.
525
+ def state(pid = $$)
526
+ file = "/proc/#{pid}/stat".freeze
527
+ return ''.freeze unless File.readable?(file)
528
+ IO.foreach(file, ' '.freeze).first(3)[-1].tap(&:rstrip!).freeze
529
+ end
530
+
531
+ ##
532
+ # = nice(pid = $$)
533
+ # Returns the nice of the process
534
+ #
535
+ # The output value is an Integer ranging from -20 to 19
536
+ #
537
+ # -20 means the process has high priority, and 19 means the process has low priority
538
+ #
539
+ # If the info isn't available or the argument passed doesn't exist as a process ID, it will return nil.
540
+ def nice(pid = $$)
541
+ file = "/proc/#{pid}/stat"
542
+ return nil unless File.readable?(file)
543
+
544
+ IO.foreach(file, ' ').first(19)[-1].to_i
545
+ end
546
+
357
547
  private
358
548
  def get_ticks
359
549
  @@ticks ||= Sysconf.sc_clk_tck