linux_stat 1.6.0 → 2.2.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.
@@ -86,7 +86,7 @@ module LinuxStat
86
86
 
87
87
  begin
88
88
  h.merge!(x =>
89
- case IO.read("/proc/#{x}/stat").split(/(\(.*\))/)[-1][/\s.+?/].strip
89
+ case LinuxStat::ProcFS.ps_state(x)
90
90
  when ?S.freeze then :sleeping
91
91
  when ?I.freeze then :idle
92
92
  when ?Z.freeze then :zombie
@@ -110,11 +110,7 @@ module LinuxStat
110
110
  # The return type is an Array of Integers.
111
111
  def sleeping
112
112
  list.select { |x|
113
- begin
114
- IO.read("/proc/#{x}/stat").split(/(\(.*\))/)[-1][/\s.+?/].strip == ?S.freeze
115
- rescue StandardError
116
- false
117
- end
113
+ LinuxStat::ProcFS.ps_state(x) == ?S.freeze
118
114
  }
119
115
  end
120
116
 
@@ -123,11 +119,7 @@ module LinuxStat
123
119
  # The return type is an Array of Integers.
124
120
  def idle
125
121
  list.select { |x|
126
- begin
127
- IO.read("/proc/#{x}/stat").split(/(\(.*\))/)[-1][/\s.+?/].strip == ?I.freeze
128
- rescue StandardError
129
- false
130
- end
122
+ LinuxStat::ProcFS.ps_state(x) == ?I.freeze
131
123
  }
132
124
  end
133
125
 
@@ -136,11 +128,7 @@ module LinuxStat
136
128
  # The return type is an Array of Integers.
137
129
  def zombie
138
130
  list.select { |x|
139
- begin
140
- IO.read("/proc/#{x}/stat").split(/(\(.*\))/)[-1][/\s.+?/].strip == ?Z.freeze
141
- rescue StandardError
142
- false
143
- end
131
+ LinuxStat::ProcFS.ps_state(x) == ?Z.freeze
144
132
  }
145
133
  end
146
134
 
@@ -149,11 +137,7 @@ module LinuxStat
149
137
  # The return type is an Array of Integers.
150
138
  def running
151
139
  list.select { |x|
152
- begin
153
- IO.read("/proc/#{x}/stat").split(/(\(.*\))/)[-1][/\s.+?/].strip == ?R.freeze
154
- rescue StandardError
155
- false
156
- end
140
+ LinuxStat::ProcFS.ps_state(x) == ?R.freeze
157
141
  }
158
142
  end
159
143
 
@@ -162,14 +146,20 @@ module LinuxStat
162
146
  # The return type is an Array of Integers.
163
147
  def stopped
164
148
  list.select { |x|
165
- begin
166
- v = IO.read("/proc/#{x}/stat").split(/(\(.*\))/)[-1][/\s.+?/].strip
167
- v == ?T.freeze || v == ?t.freeze
168
- rescue StandardError
169
- false
170
- end
149
+ v = LinuxStat::ProcFS.ps_state(x)
150
+ v == ?T.freeze || v == ?t.freeze
171
151
  }
172
152
  end
153
+
154
+ ##
155
+ # Returns the last_pid of the system.
156
+ # It directly calls LS::ProcFS.last_pid
157
+ #
158
+ # The return value is Integer, but if the status
159
+ # isn't available, it will return nil
160
+ def last_pid
161
+ LinuxStat::ProcFS.last_pid
162
+ end
173
163
  end
174
164
  end
175
165
  end
@@ -149,20 +149,7 @@ module LinuxStat
149
149
  #
150
150
  # If the info isn't available it will return an empty Hash.
151
151
  def mem_stat(pid = $$)
152
- statm = "/proc/#{pid}/statm".freeze
153
- return {} unless File.readable?(statm)
154
-
155
- data = IO.read(statm).split
156
-
157
- _rss_anon = (data[1] && data[2]) ? data[1].to_i.-(data[2].to_i).*(pagesize).fdiv(1000) : nil
158
- _virtual_memory = data[0] ? data[0].to_i*(pagesize).fdiv(1000) : nil
159
- _resident_memory = data[1] ? data[1].to_i.*(pagesize).fdiv(1000) : nil
160
-
161
- {
162
- memory: _rss_anon,
163
- virtual_memory: _virtual_memory,
164
- resident_memory: _resident_memory
165
- }
152
+ LinuxStat::ProcFS.statm(pid)
166
153
  end
167
154
 
168
155
  ##
@@ -182,11 +169,7 @@ module LinuxStat
182
169
  #
183
170
  # If the info isn't available it will return nil.
184
171
  def memory(pid = $$)
185
- file = "/proc/#{pid}/statm".freeze
186
- return nil unless File.readable?(file)
187
-
188
- data = IO.read(file).split
189
- (data[1] && data[2]) ? data[1].to_i.-(data[2].to_i).*(pagesize).fdiv(1000) : nil
172
+ LinuxStat::ProcFS.statm_memory(pid) &.fdiv(1000)
190
173
  end
191
174
 
192
175
  ##
@@ -207,11 +190,7 @@ module LinuxStat
207
190
  #
208
191
  # If the info isn't available it will return nil.
209
192
  def virtual_memory(pid = $$)
210
- file = "/proc/#{pid}/statm".freeze
211
- return nil unless File.readable?(file)
212
-
213
- _virtual_memory = IO.read(file).split[0]
214
- _virtual_memory ? _virtual_memory.to_i.*(pagesize).fdiv(1000) : nil
193
+ LinuxStat::ProcFS.statm_virtual(pid) &.fdiv(1000)
215
194
  end
216
195
 
217
196
  ##
@@ -226,17 +205,34 @@ module LinuxStat
226
205
  # The value is in kilobytes.
227
206
  #
228
207
  # The output is an Integer. For example:
229
- # LinuxStat::ProcessInfo.cpu_stat
208
+ # LinuxStat::ProcessInfo.resident_memory
230
209
  #
231
210
  # => 13996.032
232
211
  #
233
212
  # If the info isn't available it will return nil.
234
213
  def resident_memory(pid = $$)
235
- file = "/proc/#{pid}/statm".freeze
236
- return nil unless File.readable?(file)
214
+ LinuxStat::ProcFS.statm_resident(pid) &.fdiv(1000)
215
+ end
237
216
 
238
- _vm_rss = IO.read(file).split[1]
239
- _vm_rss ? _vm_rss.to_i.*(pagesize).fdiv(1000) : nil
217
+ ##
218
+ # = shared_memory(pid = $$)
219
+ #
220
+ # Where pid is the process ID.
221
+ #
222
+ # By default it is the id of the current process ($$)
223
+ #
224
+ # It retuns the shared memory for the process.
225
+ #
226
+ # The value is in kilobytes.
227
+ #
228
+ # The output is an Integer. For example:
229
+ # LinuxStat::ProcessInfo.shared_memory
230
+ #
231
+ # => 13996.032
232
+ #
233
+ # If the info isn't available it will return nil.
234
+ def shared_memory(pid = $$)
235
+ LinuxStat::ProcFS.statm_shared(pid) &.fdiv(1000)
240
236
  end
241
237
 
242
238
  ##
@@ -282,37 +278,31 @@ module LinuxStat
282
278
  #
283
279
  # The :last_executed_cpu also returns an Integer indicating the last executed cpu of the process.
284
280
  def cpu_stat(pid: $$, sleep: ticks_to_ms_t5)
285
- file = "/proc/#{pid}/stat"
286
281
  ticks = get_ticks
282
+ stat = LinuxStat::ProcFS.ps_stat(pid)
283
+ uptime = LS::ProcFS.uptime_f
284
+ return {} unless uptime && !stat.empty?
287
285
 
288
- return {} unless File.readable?(file)
289
-
290
- stat = IO.read(file).split(/(\(.*\))/)[-1] &.split
291
- return {} unless stat
292
-
293
- utime, stime, starttime = *stat.values_at(11, 12, 19).map(&:to_f)
294
-
295
- uptime = IO.read('/proc/uptime'.freeze).to_f * ticks
286
+ utime, stime, starttime = *stat.values_at(10, 11, 18).map(&:to_f)
287
+ uptime *= ticks
296
288
 
297
289
  total_time = utime + stime
298
290
  idle1 = uptime - starttime - total_time
299
291
 
300
292
  sleep(sleep)
301
293
 
302
- return {} unless File.readable?(file)
303
-
304
- stat = IO.read(file).split(/(\(.*\))/)[-1] &.split
305
- return {} unless stat
306
-
307
- utime2, stime2, starttime2 = *stat.values_at(11, 12, 19).map(&:to_f)
294
+ stat = LinuxStat::ProcFS.ps_stat(pid)
295
+ uptime = LS::ProcFS.uptime_f
296
+ return {} unless uptime && !stat.empty?
308
297
 
309
- uptime = IO.read('/proc/uptime'.freeze).to_f * ticks
298
+ utime2, stime2, starttime2 = *stat.values_at(10, 11, 18).map(&:to_f)
299
+ uptime *= ticks
310
300
 
311
301
  total_time2 = utime2 + stime2
312
302
  idle2 = uptime - starttime2 - total_time2
313
303
 
314
304
  totald = idle2.+(total_time2).-(idle1 + total_time)
315
- cpu_u = totald.-(idle2 - idle1).fdiv(totald).abs.*(100)./(cpu_count)
305
+ cpu_u = totald.-(idle2 - idle1).fdiv(totald).abs.*(100)./(LinuxStat::CPU.count)
316
306
 
317
307
  {
318
308
  cpu_usage: cpu_u > 100 ? 100.0 : cpu_u.round(2),
@@ -348,37 +338,10 @@ module LinuxStat
348
338
  #
349
339
  # This method is more efficient than running LinuxStat::ProcessInfo.cpu_stat()
350
340
  def cpu_usage(pid: $$, sleep: ticks_to_ms_t5)
351
- file = "/proc/#{pid}/stat"
352
- ticks = get_ticks
353
-
354
- return nil unless File.readable?(file)
355
-
356
- stat = IO.read(file).split(/(\(.*\))/)[-1] &.split
357
- return nil unless stat
358
-
359
- utime, stime, starttime = *stat.values_at(11, 12, 19).map(&:to_f)
360
-
361
- uptime = IO.read('/proc/uptime'.freeze).to_f * ticks
362
-
363
- total_time = utime + stime
364
- idle1 = uptime - starttime - total_time
365
-
366
- sleep(sleep)
367
-
368
- return nil unless File.readable?(file)
369
-
370
- stat = IO.read(file).split(/(\(.*\))/)[-1] &.split
371
- return nil unless stat
341
+ u = cpu_usage_thread(pid, sleep)
342
+ return nil unless u
372
343
 
373
- utime2, stime2, starttime2 = *stat.values_at(11, 12, 19).map(&:to_f)
374
- uptime = IO.read('/proc/uptime'.freeze).to_f * ticks
375
-
376
- total_time2 = utime2 + stime2
377
- idle2 = uptime - starttime2 - total_time2
378
-
379
- totald = idle2.+(total_time2).-(idle1 + total_time)
380
-
381
- u = totald.-(idle2 - idle1).fdiv(totald).abs.*(100)./(cpu_count)
344
+ u /= LinuxStat::CPU.count
382
345
  u > 100 ? 100.0 : u.round(2)
383
346
  end
384
347
 
@@ -405,40 +368,10 @@ module LinuxStat
405
368
  #
406
369
  # But if the info isn't available, it will return nil.
407
370
  def thread_usage(pid: $$, sleep: ticks_to_ms_t5)
408
- file = "/proc/#{pid}/stat"
409
- ticks = get_ticks
410
-
411
- return nil unless File.readable?(file)
412
-
413
- stat = IO.read(file).split(/(\(.*\))/)[-1] &.split
414
- return nil unless stat
415
-
416
- utime, stime, starttime = *stat.values_at(11, 12, 19).map(&:to_f)
417
-
418
- uptime = IO.read('/proc/uptime'.freeze).to_f * ticks
419
-
420
- total_time = utime + stime
421
- idle1 = uptime - starttime - total_time
422
-
423
- sleep(sleep)
371
+ u = cpu_usage_thread(pid, sleep)
372
+ return nil unless u
424
373
 
425
- return nil unless File.readable?(file)
426
-
427
- stat = IO.read(file).split(/(\(.*\))/)[-1] &.split
428
- return nil unless stat
429
-
430
- utime2, stime2, starttime2 = *stat.values_at(11, 12, 19).map(&:to_f)
431
-
432
- uptime = IO.read('/proc/uptime'.freeze).to_f * ticks
433
-
434
- total_time2 = utime2 + stime2
435
- idle2 = uptime - starttime2 - total_time2
436
-
437
- totald = idle2.+(total_time2).-(idle1 + total_time)
438
-
439
- u = totald.-(idle2 - idle1).fdiv(totald).abs.*(100)
440
-
441
- cpu_count_t100 = cpu_count * 100
374
+ cpu_count_t100 = LinuxStat::CPU.count * 100
442
375
  u > cpu_count_t100 ? cpu_count_t100 : u.round(2)
443
376
  end
444
377
 
@@ -460,11 +393,7 @@ module LinuxStat
460
393
  #
461
394
  # This method is way more efficient than running LinuxStat::ProcessInfo.cpu_stat()
462
395
  def threads(pid = $$)
463
- file = "/proc/#{pid}/stat".freeze
464
- return nil unless File.readable?(file)
465
-
466
- data = IO.read(file).split(/(\(.*\))/)[-1] &.split &.at(17)
467
- data ? data.to_i : nil
396
+ LinuxStat::ProcFS.ps_stat(pid)[16]
468
397
  end
469
398
 
470
399
  ##
@@ -485,11 +414,7 @@ module LinuxStat
485
414
  #
486
415
  # This method is way more efficient than running LinuxStat::ProcessInfo.cpu_stat()
487
416
  def last_executed_cpu(pid = $$)
488
- file = "/proc/#{pid}/stat".freeze
489
- return nil unless File.readable?(file)
490
-
491
- data = IO.read(file).split(/(\(.*\))/)[-1] &.split &.at(36)
492
- data ? data.to_i : nil
417
+ LinuxStat::ProcFS.ps_stat(pid)[35]
493
418
  end
494
419
 
495
420
  ##
@@ -566,21 +491,15 @@ module LinuxStat
566
491
  #
567
492
  # If the info isn't available or the argument passed doesn't exist as a process ID, it will return nil.
568
493
  def start_time_epoch(pid = $$)
569
- stat_file = "/proc/#{pid}/stat".freeze
570
- uptime = "/proc/uptime".freeze
571
-
572
- @@u_readable ||= File.readable?(uptime)
573
- return nil unless @@u_readable && File.readable?(stat_file)
494
+ uptime = LS::ProcFS.uptime_f
495
+ stat = LinuxStat::ProcFS.ps_stat(pid)[18]
574
496
 
575
- stat = IO.read(stat_file).split(/(\(.*\))/)[-1] &.split
576
- return nil unless stat
577
-
578
- u = IO.foreach(uptime, ' '.freeze).next.to_f
579
- st = stat[19].to_f / get_ticks
497
+ return nil unless uptime && stat
498
+ st = stat.to_f / get_ticks
580
499
 
581
500
  # Getting two Time objects and dealing with floating point numbers
582
- # Just to make sure the time goes monotonically
583
- Time.now.-(u - st).to_i
501
+ # Just to make sure the time goes monotonically unless the clock changes
502
+ Time.now.-(uptime - st).to_i
584
503
  end
585
504
 
586
505
  ##
@@ -619,17 +538,10 @@ module LinuxStat
619
538
  #
620
539
  # If the info isn't available or the argument passed doesn't exist as a process ID, it will return nil.
621
540
  def running_time(pid = $$)
622
- stat_file = "/proc/#{pid}/stat".freeze
623
- uptime = "/proc/uptime".freeze
624
-
625
- @@u_readable ||= File.readable?(uptime)
626
- return nil unless @@u_readable && File.readable?(stat_file)
627
-
628
- stat = IO.read(stat_file).split(/(\(.*\))/)[-1] &.split
629
- return nil unless stat
630
-
631
- IO.foreach(uptime, ' '.freeze).next.to_f
632
- .-(stat[19].to_f / get_ticks).round(2)
541
+ uptime = LS::ProcFS.uptime_f
542
+ stat = LinuxStat::ProcFS.ps_stat(pid)[18]
543
+ return nil unless uptime && stat
544
+ uptime.-(stat.to_f / get_ticks).round(2)
633
545
  end
634
546
 
635
547
  ##
@@ -651,13 +563,7 @@ module LinuxStat
651
563
  # If the info isn't available or the argument passed doesn't exist as a process ID,
652
564
  # it will return an empty String.
653
565
  def state(pid = $$)
654
- file = "/proc/#{pid}/stat".freeze
655
- return ''.freeze unless File.readable?(file)
656
-
657
- stat = IO.read(file).split(/(\(.*\))/)[-1]
658
- return '' unless stat
659
-
660
- stat[/\s.+?/].strip
566
+ LinuxStat::ProcFS.ps_state(pid)
661
567
  end
662
568
 
663
569
  ##
@@ -670,13 +576,7 @@ module LinuxStat
670
576
  #
671
577
  # If the info isn't available or the argument passed doesn't exist as a process ID, it will return nil.
672
578
  def nice(pid = $$)
673
- file = "/proc/#{pid}/stat"
674
- return nil unless File.readable?(file)
675
-
676
- stat = IO.read(file).split(/(\(.*\))/)[-1] &.split
677
- return nil unless stat
678
-
679
- stat[16].to_i
579
+ LinuxStat::ProcFS.ps_stat(pid)[15]
680
580
  end
681
581
 
682
582
  ##
@@ -709,6 +609,47 @@ module LinuxStat
709
609
  LinuxStat::Nproc.count_cpu_for_pid(pid)
710
610
  end
711
611
 
612
+ ##
613
+ # = def cpu_times_i(pid = $$)
614
+ #
615
+ # Shows the CPU time used by the process.
616
+ #
617
+ # The return value is a Float.
618
+ # But if the info isn't available, it will return nil.
619
+ def cpu_time(pid = $$)
620
+ LinuxStat::ProcFS.ps_times(pid)
621
+ end
622
+
623
+ ##
624
+ # = def cpu_times(pid = $$)
625
+ #
626
+ # Shows the CPU time used by the process.
627
+ #
628
+ # The return value is a Hash formatted like this:
629
+ # LS::ProcessInfo.cpu_times($$)
630
+ #
631
+ # => {:hour=>0, :minute=>39, :second=>12, :jiffy=>0.42}
632
+ #
633
+ # But if the info isn't available, it will return an empty Hash..
634
+ def cpu_times(pid = $$)
635
+ v = LinuxStat::ProcFS.ps_times(pid)
636
+ return {} unless v
637
+
638
+ v_i = v.to_i
639
+
640
+ hour = v_i / 3600
641
+ min = v_i % 3600 / 60
642
+ sec = v_i % 60
643
+ jiffy = v.-(v_i) * 100
644
+
645
+ {
646
+ hour: hour,
647
+ minute: min,
648
+ second: sec,
649
+ jiffy: jiffy.to_i
650
+ }
651
+ end
652
+
712
653
  alias count_cpu nproc
713
654
 
714
655
  private
@@ -728,8 +669,33 @@ module LinuxStat
728
669
  @@pagesize ||= LinuxStat::Sysconf.pagesize.to_i
729
670
  end
730
671
 
731
- def cpu_count
732
- @@nprocessors_conf ||= LinuxStat::CPU.count
672
+ def cpu_usage_thread(pid, delay)
673
+ ticks = get_ticks
674
+ stat = LinuxStat::ProcFS.ps_stat(pid)
675
+ uptime = LS::ProcFS.uptime_f
676
+ return nil unless uptime && !stat.empty?
677
+
678
+ utime, stime, starttime = *stat.values_at(10, 11, 18).map(&:to_f)
679
+ uptime *= ticks
680
+
681
+ total_time = utime + stime
682
+ idle1 = uptime - starttime - total_time
683
+
684
+ sleep(delay)
685
+
686
+ stat = LinuxStat::ProcFS.ps_stat(pid)
687
+ uptime = LS::ProcFS.uptime_f
688
+ return nil unless uptime && !stat.empty?
689
+
690
+ utime2, stime2, starttime2 = *stat.values_at(10, 11, 18).map(&:to_f)
691
+ uptime *= ticks
692
+
693
+ total_time2 = utime2 + stime2
694
+ idle2 = uptime - starttime2 - total_time2
695
+
696
+ totald = idle2.+(total_time2).-(idle1 + total_time)
697
+
698
+ totald.-(idle2 - idle1).fdiv(totald).abs.*(100)
733
699
  end
734
700
  end
735
701
  end