linux_stat 1.5.1 → 2.1.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.
data/bin/console CHANGED
@@ -1,4 +1,10 @@
1
1
  #!/usr/bin/env ruby
2
2
  $-v = true
3
- %w(linux_stat irb).each(&method(:require))
3
+
4
+ begin
5
+ %w(linux_stat irb).each(&method(:require))
6
+ rescue LoadError
7
+ abort "LinuxStat and IRB shoud be installed before you can run LinuxStat console"
8
+ end
9
+
4
10
  IRB.start(__FILE__)
data/exe/linuxstat.rb CHANGED
@@ -7,6 +7,12 @@ rescue LoadError
7
7
  abort "The Gem needs to be installed before this test can be run!"
8
8
  end
9
9
 
10
+ Integer.class_exec do
11
+ define_method(:clamp) { |min, max|
12
+ self < min ? min : self > max ? max : self
13
+ }
14
+ end unless 1.respond_to?(:clamp)
15
+
10
16
  # Gradient colour to strings
11
17
  class String
12
18
  def colourize(colour = 1, flip: false)
@@ -138,7 +144,8 @@ execute.sort.each do |c|
138
144
  param << "#{p[1] || 'arg'}#{_arg}, "
139
145
  end
140
146
  end
141
- param.delete_suffix!(", ")
147
+
148
+ param.chomp!(", ")
142
149
 
143
150
  disp_meth = "#{meth}"
144
151
  disp_meth.concat(arg ? "(#{param})" : "(#{param})")
data/ext/nproc/nproc.c CHANGED
@@ -18,9 +18,9 @@
18
18
  static VALUE count_cpu_for_pid(VALUE obj, VALUE pid) {
19
19
  cpu_set_t set ;
20
20
  CPU_ZERO(&set) ;
21
- short int stat = sched_getaffinity(FIX2INT(pid), sizeof(set), &set) ;
21
+ char status = sched_getaffinity(FIX2INT(pid), sizeof(set), &set) ;
22
22
 
23
- if (stat < 0) return Qnil ;
23
+ if (status < 0) return Qnil ;
24
24
  return INT2FIX(CPU_COUNT(&set)) ;
25
25
  }
26
26
 
@@ -0,0 +1,11 @@
1
+ require 'mkmf'
2
+
3
+ unless have_const('linux') || RbConfig::CONFIG['arch'].to_s[/linux/]
4
+ abort('Platform is not linux')
5
+ end
6
+
7
+ unless have_header('unistd.h') && have_header('ruby.h')
8
+ abort('Missing header')
9
+ end
10
+
11
+ create_makefile 'linux_stat/procfs'
@@ -0,0 +1,11 @@
1
+ VALUE last_pid(VALUE obj) {
2
+ FILE *f = fopen("/proc/loadavg", "r") ;
3
+ if (!f) return Qnil ;
4
+
5
+ unsigned long long _last_pid ;
6
+ char status = fscanf(f, "%*f %*f %*f %*s %llu", &_last_pid) ;
7
+ fclose(f) ;
8
+ if (status != 1) return Qnil ;
9
+
10
+ return ULL2NUM(_last_pid) ;
11
+ }
@@ -0,0 +1,42 @@
1
+ // ProcFS doesn't include all the /proc/ files.
2
+ // It includes some of them that can add potential
3
+ // performance boost to the LinuxStat modules.
4
+ //
5
+ // Some LinuxStat modules reads /proc/ files
6
+ // that are not present here. That is because
7
+ // Ruby can handle them well with little wastage.
8
+ // Adding C may be a potential risk in such places.
9
+ //
10
+ // In this ProcFS module, we will define the methods
11
+ // that really gets the benefit.
12
+
13
+ #include <stdio.h>
14
+ #include <unistd.h>
15
+ #include <string.h>
16
+ #include "ruby.h"
17
+ #include "uptime.h"
18
+ #include "statm.h"
19
+ #include "stat.h"
20
+ #include "loadavg_pid.h"
21
+
22
+ int Init_procfs() {
23
+ VALUE _linux_stat = rb_define_module("LinuxStat") ;
24
+ VALUE _procfs = rb_define_module_under(_linux_stat, "ProcFS") ;
25
+
26
+ // uptime
27
+ rb_define_module_function(_procfs, "uptime_f", uptime_f, 0) ;
28
+
29
+ // statm
30
+ rb_define_module_function(_procfs, "statm_memory", statm_memory, 1) ;
31
+ rb_define_module_function(_procfs, "statm", statm, 1) ;
32
+ rb_define_module_function(_procfs, "statm_virtual", statm_virtual, 1) ;
33
+ rb_define_module_function(_procfs, "statm_resident", statm_resident, 1) ;
34
+ rb_define_module_function(_procfs, "statm_shared", statm_shared, 1) ;
35
+
36
+ // loadavg last PID
37
+ rb_define_module_function(_procfs, "last_pid", last_pid, 0) ;
38
+
39
+ // stat
40
+ rb_define_module_function(_procfs, "ps_state", ps_state, 1) ;
41
+ rb_define_module_function(_procfs, "ps_stat", ps_stat, 1) ;
42
+ }
data/ext/procfs/stat.h ADDED
@@ -0,0 +1,98 @@
1
+ VALUE ps_state(VALUE obj, VALUE pid) {
2
+ int _pid = FIX2INT(pid) ;
3
+ if (_pid < 0) return rb_str_new_cstr("") ;
4
+
5
+ char _path[22] ;
6
+ sprintf(_path, "/proc/%d/stat", _pid) ;
7
+
8
+ FILE *f = fopen(_path, "r") ;
9
+ if (!f) return rb_str_new_cstr("") ;
10
+
11
+ char _s[1] ;
12
+
13
+ char status = fscanf(f, "%*llu (%*[^)]%*[)] %s", _s) ;
14
+ fclose(f) ;
15
+
16
+ if (status != 1) return rb_str_new_cstr("") ;
17
+ return rb_str_new_cstr(_s) ;
18
+ }
19
+
20
+ VALUE ps_stat(VALUE obj, VALUE pid) {
21
+ int _pid = FIX2INT(pid) ;
22
+ if (_pid < 0) return rb_str_new_cstr("") ;
23
+
24
+ char _path[22] ;
25
+ sprintf(_path, "/proc/%d/stat", _pid) ;
26
+
27
+ FILE *f = fopen(_path, "r") ;
28
+
29
+ if (!f)
30
+ return rb_ary_new() ;
31
+
32
+ // ?? JEEZ !!
33
+ // We need to do this because the datatypes are different
34
+ // Can't make an array like Ruby out of String
35
+ // This is hard to write and check, but tested, and works.
36
+ //
37
+ // For this struct,
38
+ // follow https://man7.org/linux/man-pages/man5/proc.5.html
39
+ int ppid, pgrp, session, tty_nr, tpgid ;
40
+ unsigned flags ;
41
+ long unsigned minflt, cminflt, majflt, cmajflt, utime, stime ;
42
+ long cutime, cstime, priority, nice, num_threads, itrealvalue ;
43
+ long long unsigned starttime ;
44
+ long unsigned vsize ;
45
+ long rss ;
46
+ long unsigned rsslim, startcode, endcode, startstack, kstkesp, kstkeip ;
47
+ long unsigned signal, blocked, sigignore, sigcatch, wchan, nswap, cnswap ;
48
+ int exit_signal, processor ;
49
+ unsigned rt_priority, policy ;
50
+ long long unsigned delayacct_blkio_ticks ;
51
+ long unsigned guest_time ;
52
+ long cguest_time ;
53
+ long unsigned start_data, end_data, start_brk, arg_start, arg_end ;
54
+ long unsigned env_start, env_end ;
55
+ int exit_code ;
56
+
57
+ char status = fscanf(
58
+ f, "%*llu (%*[^)]%*[)] %*c "
59
+ "%d %d %d %d %d %u "
60
+ "%lu %lu %lu %lu %lu %lu "
61
+ "%ld %ld %ld %ld %ld %ld "
62
+ "%llu %lu %ld %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu "
63
+ "%d %d %u %u %llu %lu %ld %lu %lu %lu %lu %lu %lu %lu %d", &ppid, &pgrp,
64
+ &session, &tty_nr, &tpgid, &flags, &minflt, &cminflt, &majflt, &cmajflt, &utime, &stime, &cutime,
65
+ &cstime, &priority, &nice, &num_threads, &itrealvalue, &starttime, &vsize, &rss, &rsslim,
66
+ &startcode, &endcode, &startstack, &kstkesp, &kstkeip, &signal, &blocked, &sigignore,
67
+ &sigcatch, &wchan, &nswap, &cnswap, &exit_signal, &processor, &rt_priority, &policy,
68
+ &delayacct_blkio_ticks, &guest_time, &cguest_time, &start_data, &end_data,
69
+ &start_brk, &arg_start, &arg_end, &env_start, &env_end, &exit_code
70
+ ) ;
71
+
72
+ fclose(f) ;
73
+
74
+ if (status != 49)
75
+ return rb_ary_new() ;
76
+
77
+ return rb_ary_new_from_args(49,
78
+ INT2NUM(ppid), INT2NUM(pgrp), INT2NUM(session), INT2NUM(tty_nr), INT2NUM(tpgid),
79
+ UINT2NUM(flags),
80
+ ULONG2NUM(minflt), ULONG2NUM(cminflt), ULONG2NUM(majflt), ULONG2NUM(cmajflt),
81
+ ULONG2NUM(utime), ULONG2NUM(stime),
82
+ LONG2NUM(cutime), LONG2NUM(cstime), LONG2NUM(priority),
83
+ LONG2NUM(nice), LONG2NUM(num_threads), LONG2NUM(itrealvalue),
84
+ ULL2NUM(starttime),
85
+ ULONG2NUM(vsize),
86
+ LONG2NUM(rss),
87
+ ULONG2NUM(rsslim), ULONG2NUM(startcode), ULONG2NUM(endcode),
88
+ ULONG2NUM(startstack), ULONG2NUM(kstkesp), ULONG2NUM(kstkeip),
89
+ ULONG2NUM(signal), ULONG2NUM(blocked), ULONG2NUM(sigignore), ULONG2NUM(sigcatch),
90
+ ULONG2NUM(wchan), ULONG2NUM(nswap), ULONG2NUM(cnswap),
91
+ INT2NUM(exit_signal), INT2NUM(processor),
92
+ UINT2NUM(rt_priority), UINT2NUM(policy),
93
+ ULL2NUM(delayacct_blkio_ticks), ULONG2NUM(guest_time), LONG2NUM(cguest_time), ULONG2NUM(start_data),
94
+ ULONG2NUM(end_data), ULONG2NUM(start_brk), ULONG2NUM(arg_start), ULONG2NUM(arg_end),
95
+ ULONG2NUM(env_start), ULONG2NUM(env_end),
96
+ INT2NUM(exit_code)
97
+ ) ;
98
+ }
@@ -0,0 +1,109 @@
1
+ #define PAGESIZE sysconf(_SC_PAGESIZE)
2
+
3
+ VALUE statm(VALUE obj, VALUE pid) {
4
+ VALUE hash = rb_hash_new() ;
5
+
6
+ int _pid = FIX2INT(pid) ;
7
+ if (_pid < 0) return hash ;
8
+
9
+ char _path[22] ;
10
+ sprintf(_path, "/proc/%d/statm", _pid) ;
11
+
12
+ FILE *f = fopen(_path, "r") ;
13
+ if (!f) return hash ;
14
+
15
+ unsigned int _virtual, resident, shared ;
16
+ char status = fscanf(f, "%u %u %u", &_virtual, &resident, &shared) ;
17
+ fclose(f) ;
18
+
19
+ if (status != 3) return hash ;
20
+
21
+ int pagesize = PAGESIZE ;
22
+
23
+ _virtual *= pagesize ;
24
+ resident *= pagesize ;
25
+ shared *= pagesize ;
26
+
27
+ unsigned int v = resident - shared ;
28
+
29
+ rb_hash_aset(hash, ID2SYM(rb_intern("memory")), UINT2NUM(v)) ;
30
+ rb_hash_aset(hash, ID2SYM(rb_intern("virtual_memory")), UINT2NUM(_virtual)) ;
31
+ rb_hash_aset(hash, ID2SYM(rb_intern("resident_memory")), UINT2NUM(resident)) ;
32
+ rb_hash_aset(hash, ID2SYM(rb_intern("shared_memory")), UINT2NUM(shared)) ;
33
+
34
+ return hash ;
35
+ }
36
+
37
+ VALUE statm_virtual(VALUE obj, VALUE pid) {
38
+ int _pid = FIX2INT(pid) ;
39
+ if (_pid < 0) return Qnil ;
40
+
41
+ char _path[22] ;
42
+ sprintf(_path, "/proc/%d/statm", _pid) ;
43
+
44
+ FILE *f = fopen(_path, "r") ;
45
+ if (!f) return Qnil ;
46
+
47
+ unsigned int _virtual ;
48
+ char status = fscanf(f, "%u", &_virtual) ;
49
+ fclose(f) ;
50
+
51
+ if (status != 1) return Qnil ;
52
+ return UINT2NUM(_virtual * PAGESIZE) ;
53
+ }
54
+
55
+ VALUE statm_resident(VALUE obj, VALUE pid) {
56
+ int _pid = FIX2INT(pid) ;
57
+ if (_pid < 0) return Qnil ;
58
+
59
+ char _path[22] ;
60
+ sprintf(_path, "/proc/%d/statm", _pid) ;
61
+
62
+ FILE *f = fopen(_path, "r") ;
63
+ if (!f) return Qnil ;
64
+
65
+ unsigned int resident ;
66
+ char status = fscanf(f, "%*u %u", &resident) ;
67
+ fclose(f) ;
68
+
69
+ if (status != 1) return Qnil ;
70
+ return UINT2NUM(resident * PAGESIZE) ;
71
+ }
72
+
73
+ VALUE statm_shared(VALUE obj, VALUE pid) {
74
+ int _pid = FIX2INT(pid) ;
75
+ if (_pid < 0) return Qnil ;
76
+
77
+ char _path[22] ;
78
+ sprintf(_path, "/proc/%d/statm", _pid) ;
79
+
80
+ FILE *f = fopen(_path, "r") ;
81
+ if (!f) return Qnil ;
82
+
83
+ unsigned int shared ;
84
+ char status = fscanf(f, "%*u %*u %u", &shared) ;
85
+ fclose(f) ;
86
+
87
+ if (status != 1) return Qnil ;
88
+ return UINT2NUM(shared * PAGESIZE) ;
89
+ }
90
+
91
+ VALUE statm_memory(VALUE obj, VALUE pid) {
92
+ int _pid = FIX2INT(pid) ;
93
+ if (_pid < 0) return Qnil ;
94
+
95
+ char _path[22] ;
96
+ sprintf(_path, "/proc/%d/statm", _pid) ;
97
+
98
+ FILE *f = fopen(_path, "r") ;
99
+ if (!f) return Qnil ;
100
+
101
+ unsigned int resident, shared ;
102
+ char status = fscanf(f, "%*u %u %u", &resident, &shared) ;
103
+ fclose(f) ;
104
+
105
+ if (status != 2) return Qnil ;
106
+
107
+ unsigned int v = (resident - shared) * PAGESIZE ;
108
+ return UINT2NUM(v) ;
109
+ }
@@ -0,0 +1,12 @@
1
+ VALUE uptime_f(VALUE obj) {
2
+ FILE *f = fopen("/proc/uptime", "r") ;
3
+ if (!f) return Qnil ;
4
+
5
+ double up_f ;
6
+ char status = fscanf(f, "%lf", &up_f) ;
7
+ fclose(f) ;
8
+
9
+ if (status != 1) return Qnil ;
10
+
11
+ return DBL2NUM(up_f) ;
12
+ }
@@ -1,5 +1,4 @@
1
1
  #include <unistd.h>
2
- #include <inttypes.h>
3
2
  #include "ruby.h"
4
3
 
5
4
  #if defined(__GNUC__) && !defined(__clang__) && !defined(__INTEL_COMPILER)
@@ -13,94 +12,94 @@
13
12
  #endif
14
13
 
15
14
  static VALUE getTick(VALUE obj) {
16
- int16_t val = sysconf(_SC_CLK_TCK) ;
15
+ int val = sysconf(_SC_CLK_TCK) ;
17
16
  if (val < 0) return Qnil ;
18
17
 
19
18
  return INT2FIX(val) ;
20
19
  }
21
20
 
22
21
  static VALUE getChildMax(VALUE obj) {
23
- int64_t val = sysconf(_SC_CHILD_MAX) ;
22
+ long long int val = sysconf(_SC_CHILD_MAX) ;
24
23
  if (val < 0) return Qnil ;
25
24
 
26
- return INT2NUM(val) ;
25
+ return LL2NUM(val) ;
27
26
  }
28
27
 
29
28
  static VALUE getHostnameMax(VALUE obj) {
30
- int32_t val = sysconf(_SC_HOST_NAME_MAX) ;
29
+ long long val = sysconf(_SC_HOST_NAME_MAX) ;
31
30
  if (val < 0) return Qnil ;
32
31
 
33
- return INT2NUM(val) ;
32
+ return LL2NUM(val) ;
34
33
  }
35
34
 
36
35
  static VALUE getLoginNameMax(VALUE obj) {
37
- int32_t val = sysconf(_SC_LOGIN_NAME_MAX) ;
36
+ long long val = sysconf(_SC_LOGIN_NAME_MAX) ;
38
37
  if (val < 0) return Qnil ;
39
38
 
40
- return INT2NUM(val) ;
39
+ return LL2NUM(val) ;
41
40
  }
42
41
 
43
42
  static VALUE getOpenMax(VALUE obj) {
44
- int64_t val = sysconf(_SC_OPEN_MAX) ;
43
+ long long val = sysconf(_SC_OPEN_MAX) ;
45
44
  if (val < 0) return Qnil ;
46
45
 
47
- return INT2NUM(val) ;
46
+ return LL2NUM(val) ;
48
47
  }
49
48
 
50
49
  static VALUE getPageSize(VALUE obj) {
51
- int32_t val = sysconf(_SC_PAGESIZE) ;
50
+ int val = sysconf(_SC_PAGESIZE) ;
52
51
  if (val < 0) return Qnil ;
53
52
 
54
- return INT2NUM(val) ;
53
+ return INT2FIX(val) ;
55
54
  }
56
55
 
57
56
  static VALUE getStreamMax(VALUE obj) {
58
- int64_t val = sysconf(_SC_STREAM_MAX) ;
57
+ long long val = sysconf(_SC_STREAM_MAX) ;
59
58
  if (val < 0) return Qnil ;
60
59
 
61
- return INT2NUM(val) ;
60
+ return LL2NUM(val) ;
62
61
  }
63
62
 
64
63
  static VALUE getTTYNameMax(VALUE obj) {
65
- int32_t val = sysconf(_SC_TTY_NAME_MAX) ;
64
+ long long val = sysconf(_SC_TTY_NAME_MAX) ;
66
65
  if (val < 0) return Qnil ;
67
66
 
68
- return INT2NUM(val) ;
67
+ return LL2NUM(val) ;
69
68
  }
70
69
 
71
70
  static VALUE getPosixVersion(VALUE obj) {
72
- int32_t val = sysconf(_SC_VERSION) ;
71
+ long long val = sysconf(_SC_VERSION) ;
73
72
  if (val < 0) return Qnil ;
74
73
 
75
- return INT2NUM(val) ;
74
+ return LL2NUM(val) ;
76
75
  }
77
76
 
78
77
  static VALUE getLineMax(VALUE obj) {
79
- int32_t val = sysconf(_SC_LINE_MAX) ;
78
+ long long val = sysconf(_SC_LINE_MAX) ;
80
79
  if (val < 0) return Qnil ;
81
80
 
82
- return INT2NUM(val) ;
81
+ return LL2NUM(val) ;
83
82
  }
84
83
 
85
84
  static VALUE getExprNestMax(VALUE obj) {
86
- int32_t val = sysconf(_SC_EXPR_NEST_MAX) ;
85
+ long long val = sysconf(_SC_EXPR_NEST_MAX) ;
87
86
  if (val < 0) return Qnil ;
88
87
 
89
- return INT2NUM(val) ;
88
+ return LL2NUM(val) ;
90
89
  }
91
90
 
92
91
  static VALUE getProcessorConfigured(VALUE obj) {
93
- int32_t val = sysconf(_SC_NPROCESSORS_CONF) ;
92
+ long val = sysconf(_SC_NPROCESSORS_CONF) ;
94
93
  if (val < 0) return Qnil ;
95
94
 
96
- return INT2NUM(val) ;
95
+ return LONG2NUM(val) ;
97
96
  }
98
97
 
99
98
  static VALUE getProcessorOnline(VALUE obj) {
100
- int32_t val = sysconf(_SC_NPROCESSORS_ONLN) ;
99
+ long val = sysconf(_SC_NPROCESSORS_ONLN) ;
101
100
  if (val < 0) return Qnil ;
102
101
 
103
- return INT2NUM(val) ;
102
+ return LONG2NUM(val) ;
104
103
  }
105
104
 
106
105
  static VALUE getUser(VALUE obj) {
@@ -109,22 +108,22 @@ static VALUE getUser(VALUE obj) {
109
108
  }
110
109
 
111
110
  static VALUE getUID(VALUE obj) {
112
- return INT2NUM(getuid()) ;
111
+ return UINT2NUM((unsigned int) getuid()) ;
113
112
  }
114
113
 
115
114
  static VALUE getGID(VALUE obj) {
116
- return INT2NUM(getgid()) ;
115
+ return UINT2NUM((unsigned int) getgid()) ;
117
116
  }
118
117
 
119
118
  static VALUE getEUID(VALUE obj) {
120
- return INT2NUM(geteuid()) ;
119
+ return UINT2NUM((unsigned int) geteuid()) ;
121
120
  }
122
121
 
123
122
  static VALUE getHostname(VALUE obj) {
124
123
  int h_max = sysconf(_SC_HOST_NAME_MAX) + 1 ;
125
124
  char hostname[h_max] ;
126
125
 
127
- short int status = gethostname(hostname, h_max) ;
126
+ char status = gethostname(hostname, h_max) ;
128
127
 
129
128
  return (status < 0) ? rb_str_new_cstr("") : rb_str_new_cstr(hostname) ;
130
129
  }