linux_stat 2.5.3 → 2.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 75ee9aa13037e326a0c04626cd28e0903b06372ce24344ffa38870f98dd6f0dd
4
- data.tar.gz: d275807c8a9572fa9faaa1c562102302697f52b6a1cb36907fa71b2576c533b0
3
+ metadata.gz: f81f89f0e4f50ff1565a5b6dba272c9ac6fab336a4e3c72888355f199fd216e1
4
+ data.tar.gz: e3d4daabdcacdfa1d54bf899dc5c98b5fc3354ecaee3ead4bfa546462d64cfb2
5
5
  SHA512:
6
- metadata.gz: 3bd3330e9135cffee27a82fc661462a9c94e421ee5eebe24fb322ad9241e5498bdfd84914fd58dab93481366969f414bf2aa93e9507bb967ca18a462c91fc007
7
- data.tar.gz: 686e7f1935fea8cde6ae24624522ef642f41e21304fdae7e758f3249f3b026ea6261a77fca76bbc2f5153d1d8426f9112a1b66f124e7c3d03106c0992ca731aa
6
+ metadata.gz: 2d8ed40932920cc10ef10a083b2be1d5bad58dfba658f0b14643826e258f4fdc6f5e89aaac1a711e3fbf674feae7d2e9be777608d02c1ab30df4621d56768ee1
7
+ data.tar.gz: 2fc069a7134118010affd0354aa7bde2615f8183cf7a8b7defd42342084b3f6a7bf0a5e5774e78aa262302b259dc16c542c06f46d1642e8e3376be308343a93e
data/README.md CHANGED
@@ -147,6 +147,7 @@ So LinuxStat::USB can be replaced with LS::USB for example.
147
147
  | [LinuxStat::CPU](https://github.com/Souravgoswami/linux_stat/blob/master/Usages.md#linuxstatcpu) | System's CPU usage and other related information |
148
148
  | [LinuxStat::FS](https://github.com/Souravgoswami/linux_stat/blob/master/Usages.md#linuxstatfs) | System's file system related information. It's used by Filesystem module. |
149
149
  | [LinuxStat::Filesystem](https://github.com/Souravgoswami/linux_stat/blob/master/Usages.md#linuxstatfilesystem) | System's file system usage and other related information |
150
+ | [LinuxStat::FTW](https://github.com/Souravgoswami/linux_stat/blob/master/Usages.md#linuxstatftw) | File Tree Walk: Walks through a file and gives you data related to it's own and sub files and directories |
150
151
  | [LinuxStat::Kernel](https://github.com/Souravgoswami/linux_stat/blob/master/Usages.md#linuxstatkernel) | System's kernel related information |
151
152
  | [LinuxStat::Memory](https://github.com/Souravgoswami/linux_stat/blob/master/Usages.md#linuxstatmemory) | System's memory (RAM) usage and other related information |
152
153
  | [LinuxStat::Mounts](https://github.com/Souravgoswami/linux_stat/blob/master/Usages.md#linuxstatmounts) | System's mount point related information |
data/exe/linuxstat.rb CHANGED
@@ -97,6 +97,7 @@ constants = LinuxStat.constants
97
97
  # Modules to delete from documentation and testing
98
98
  %i(
99
99
  Nproc
100
+ NFTW
100
101
  ).each(&constants.method(:delete))
101
102
 
102
103
  execute = constants.map(&:downcase).map.with_index { |x, i|
@@ -1,25 +1,27 @@
1
- static VALUE getDiskStats (volatile VALUE obj, volatile VALUE path) {
2
- FILE *file = fopen("/proc/diskstats", "r") ;
3
- if(!file) return rb_ary_new() ;
1
+ static VALUE getDiskStats (VALUE obj, VALUE path) {
2
+ FILE *file = fopen("/proc/diskstats", "r");
3
+ if(!file) return rb_ary_new();
4
4
 
5
- char lines[120] ;
6
- unsigned long long read, write ;
7
- char *p = StringValuePtr(path) ;
5
+ char lines[120];
6
+ unsigned long long read, write;
7
+ char *p = StringValuePtr(path);
8
8
 
9
- while(fgets(lines, 119, file)) {
10
- sscanf(lines, "%*s %*s %s %*s %*s %llu %*s %*s %*s %llu", lines, &read, &write) ;
9
+ while (fgets(lines, sizeof(lines) - 1, file)) {
10
+ if (sscanf(lines, "%*s %*s %119s %*s %*s %llu %*s %*s %*s %llu", lines, &read, &write) == 3) {
11
+ if(strcmp(lines, p) == 0) {
12
+ fclose(file);
11
13
 
12
- if(strcmp(lines, p) == 0) {
13
- fclose(file) ;
14
-
15
- return rb_ary_new_from_args(
16
- 2,
17
- ULL2NUM(read),
18
- ULL2NUM(write)
19
- ) ;
14
+ return rb_ary_new_from_args(
15
+ 2,
16
+ ULL2NUM(read),
17
+ ULL2NUM(write)
18
+ );
19
+ }
20
+ } else {
21
+ return rb_ary_new();
20
22
  }
21
23
  }
22
24
 
23
- fclose(file) ;
24
- return rb_ary_new() ;
25
+ fclose(file);
26
+ return rb_ary_new();
25
27
  }
@@ -9,4 +9,6 @@ unless have_header('sys/statvfs.h') && have_header('sys/ioctl.h') &&
9
9
  abort('Missing header')
10
10
  end
11
11
 
12
+ $CFLAGS << ' -O3 -march=native -mtune=native'
13
+
12
14
  create_makefile 'linux_stat/fs_stat'
@@ -8,41 +8,31 @@
8
8
  #include "sector_size.h"
9
9
  #include "disk_stat.h"
10
10
 
11
- #if defined(__GNUC__) && !defined(__clang__) && !defined(__INTEL_COMPILER)
12
- #pragma GCC optimize ("O3")
13
- #pragma GCC diagnostic warning "-Wall"
14
- #elif defined(__clang__)
15
- #pragma clang optimize on
16
- #pragma clang diagnostic warning "-Wall"
17
- #elif defined(__INTEL_COMPILER)
18
- #pragma intel optimization_level 3
19
- #endif
11
+ static VALUE statfs(VALUE obj, VALUE dir) {
12
+ struct statvfs buf;
13
+ char *d = StringValuePtr(dir);
14
+ VALUE hash = rb_hash_new();
20
15
 
21
- static VALUE statfs(volatile VALUE obj, volatile VALUE dir) {
22
- struct statvfs buf ;
23
- char *d = StringValuePtr(dir) ;
24
- VALUE hash = rb_hash_new() ;
16
+ if(statvfs(d, &buf) < 0) return hash;
25
17
 
26
- if(statvfs(d, &buf) < 0) return hash ;
18
+ rb_hash_aset(hash, ID2SYM(rb_intern("block_size")), INT2FIX((unsigned int)buf.f_bsize));
19
+ rb_hash_aset(hash, ID2SYM(rb_intern("fragment_size")), ULL2NUM((unsigned long long)buf.f_frsize));
20
+ rb_hash_aset(hash, ID2SYM(rb_intern("blocks")), ULL2NUM((unsigned long long)buf.f_blocks));
21
+ rb_hash_aset(hash, ID2SYM(rb_intern("block_free")), ULL2NUM((unsigned long long)buf.f_bfree));
22
+ rb_hash_aset(hash, ID2SYM(rb_intern("block_avail_unpriv")), ULL2NUM((unsigned long long)buf.f_bavail));
23
+ rb_hash_aset(hash, ID2SYM(rb_intern("inodes")), ULL2NUM((unsigned long long)buf.f_files));
24
+ rb_hash_aset(hash, ID2SYM(rb_intern("free_inodes")), ULL2NUM((unsigned long long)buf.f_ffree));
25
+ rb_hash_aset(hash, ID2SYM(rb_intern("filesystem_id")), ULL2NUM((unsigned long long)buf.f_fsid));
26
+ rb_hash_aset(hash, ID2SYM(rb_intern("mount_flags")), ULL2NUM((unsigned long long)buf.f_flag));
27
+ rb_hash_aset(hash, ID2SYM(rb_intern("max_filename_length")), ULL2NUM((unsigned long long)buf.f_namemax));
27
28
 
28
- rb_hash_aset(hash, ID2SYM(rb_intern("block_size")), INT2FIX((unsigned int)buf.f_bsize)) ;
29
- rb_hash_aset(hash, ID2SYM(rb_intern("fragment_size")), ULL2NUM((unsigned long long)buf.f_frsize)) ;
30
- rb_hash_aset(hash, ID2SYM(rb_intern("blocks")), ULL2NUM((unsigned long long)buf.f_blocks)) ;
31
- rb_hash_aset(hash, ID2SYM(rb_intern("block_free")), ULL2NUM((unsigned long long)buf.f_bfree)) ;
32
- rb_hash_aset(hash, ID2SYM(rb_intern("block_avail_unpriv")), ULL2NUM((unsigned long long)buf.f_bavail)) ;
33
- rb_hash_aset(hash, ID2SYM(rb_intern("inodes")), ULL2NUM((unsigned long long)buf.f_files)) ;
34
- rb_hash_aset(hash, ID2SYM(rb_intern("free_inodes")), ULL2NUM((unsigned long long)buf.f_ffree)) ;
35
- rb_hash_aset(hash, ID2SYM(rb_intern("filesystem_id")), ULL2NUM((unsigned long long)buf.f_fsid)) ;
36
- rb_hash_aset(hash, ID2SYM(rb_intern("mount_flags")), ULL2NUM((unsigned long long)buf.f_flag)) ;
37
- rb_hash_aset(hash, ID2SYM(rb_intern("max_filename_length")), ULL2NUM((unsigned long long)buf.f_namemax)) ;
38
-
39
- return hash ;
29
+ return hash;
40
30
  }
41
31
 
42
32
  void Init_fs_stat() {
43
- VALUE _linux_stat = rb_define_module("LinuxStat") ;
44
- VALUE fs = rb_define_module_under(_linux_stat, "FS") ;
45
- rb_define_module_function(fs, "stat", statfs, 1) ;
46
- rb_define_module_function(fs, "sectors", getSectorSize, 1) ;
47
- rb_define_module_function(fs, "total_io", getDiskStats, 1) ;
33
+ VALUE _linux_stat = rb_define_module("LinuxStat");
34
+ VALUE fs = rb_define_module_under(_linux_stat, "FS");
35
+ rb_define_module_function(fs, "stat", statfs, 1);
36
+ rb_define_module_function(fs, "sectors", getSectorSize, 1);
37
+ rb_define_module_function(fs, "total_io", getDiskStats, 1);
48
38
  }
@@ -1,13 +1,13 @@
1
- static VALUE getSectorSize(volatile VALUE obj, volatile VALUE path) {
2
- char *dev = StringValuePtr(path) ;
1
+ static VALUE getSectorSize(VALUE obj, VALUE path) {
2
+ char *dev = StringValuePtr(path);
3
3
 
4
- unsigned int fd = open(dev, O_RDONLY | O_NONBLOCK) ;
5
- if(fd < 0) return Qnil ;
4
+ unsigned int fd = open(dev, O_RDONLY | O_NONBLOCK);
5
+ if(fd < 0) return Qnil;
6
6
 
7
- unsigned int sSize = 0 ;
8
- short status = ioctl(fd, BLKSSZGET, &sSize) ;
9
- close(fd) ;
10
- if(status < 0) return Qnil ;
7
+ unsigned int sSize = 0;
8
+ short status = ioctl(fd, BLKSSZGET, &sSize);
9
+ close(fd);
10
+ if(status < 0) return Qnil;
11
11
 
12
- return UINT2NUM(sSize) ;
12
+ return UINT2NUM(sSize);
13
13
  }
@@ -1,2 +1,5 @@
1
1
  require 'mkmf'
2
+
3
+ $CFLAGS << ' -O3 -march=native -mtune=native'
4
+
2
5
  create_makefile 'linux_stat/misc/integer'
@@ -12,52 +12,44 @@
12
12
  - It doesn't raise any error. Handing nil is enough to indicate that it failed.
13
13
  */
14
14
 
15
- #if defined(__GNUC__) && !defined(__clang__) && !defined(__INTEL_COMPILER)
16
- #pragma GCC optimize ("O3")
17
- #pragma GCC diagnostic warning "-Wall"
18
- #elif defined(__clang__)
19
- #pragma clang optimize on
20
- #pragma clang diagnostic warning "-Wall"
21
- #elif defined(__INTEL_COMPILER)
22
- #pragma intel optimization_level 3
23
- #endif
24
-
25
15
  #include <limits.h>
26
16
  #include "ruby.h"
27
17
 
28
- VALUE isNumber(volatile VALUE obj, volatile VALUE val) {
29
- // But we don't expect anything other than String though as Argument.
30
- // Note that raising ArgumentError or any kind of Error shouldn't be done here
31
- // Otherwise Integer(n) is the best method in Ruby.
18
+ VALUE isNumber(VALUE obj, VALUE val) {
19
+ // Expecting a String as input, return Qnil for any other type
32
20
  if (!RB_TYPE_P(val, T_STRING))
33
- return Qnil ;
21
+ return Qnil;
34
22
 
35
- char *str = StringValuePtr(val) ;
36
- char ch = str[0] ;
23
+ char *str = StringValuePtr(val);
24
+ size_t len = RSTRING_LEN(val);
37
25
 
38
26
  // If the string is empty, return false
39
- if (!ch) return Qfalse ;
27
+ if (len == 0) return Qfalse;
40
28
 
41
- unsigned char i = ch == '-' ? 1 : 0 ;
42
- if (!str[i]) return Qfalse ;
29
+ size_t i = 0;
30
+ char ch = str[0];
43
31
 
44
- unsigned char max = UCHAR_MAX ;
32
+ // If the string starts with '-', skip it but ensure there are digits after it
33
+ if (ch == '-') {
34
+ i = 1;
35
+ if (i == len) return Qfalse;
36
+ }
45
37
 
46
- # pragma GCC unroll 4
47
- while((ch = str[i++])) {
48
- if (ch < 48 || ch > 57)
49
- return Qfalse ;
38
+ // Iterate through each character to check if it's a digit
39
+ for (; i < len; i++) {
40
+ ch = str[i];
50
41
 
51
- if (i == max)
52
- return Qnil ;
42
+ if (ch < '0' || ch > '9') {
43
+ return Qfalse;
44
+ }
53
45
  }
54
46
 
55
- return Qtrue ;
47
+ return Qtrue;
56
48
  }
57
49
 
58
50
  void Init_integer() {
59
- VALUE linuxStat = rb_define_module("LinuxStat") ;
60
- VALUE misc = rb_define_module_under(linuxStat, "Misc") ;
51
+ VALUE linuxStat = rb_define_module("LinuxStat");
52
+ VALUE misc = rb_define_module_under(linuxStat, "Misc");
61
53
 
62
- rb_define_module_function(misc, "integer?", isNumber, 1) ;
54
+ rb_define_module_function(misc, "integer?", isNumber, 1);
63
55
  }
@@ -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
+ abort('Missing header') unless have_header('ftw.h')
8
+
9
+ $CFLAGS << ' -O3 -march=native -mtune=native'
10
+
11
+ create_makefile 'linux_stat/nftw'
data/ext/nftw/nftw.c ADDED
@@ -0,0 +1,306 @@
1
+ #define _GNU_SOURCE 1
2
+
3
+ #include <ftw.h>
4
+ #include <stdint.h>
5
+ #include <string.h>
6
+
7
+ #include "ruby.h"
8
+
9
+ VALUE LIST;
10
+ intmax_t TOTAL_FILES;
11
+
12
+ static int storeInfo(const char *fpath, const struct stat *sb, int tflag, struct FTW *ftwbuf) {
13
+ VALUE hash = rb_hash_new();
14
+
15
+ // Raw type flag
16
+ VALUE typeFlag = (tflag == FTW_D) ? ID2SYM(rb_intern("FTW_D")) : (tflag == FTW_DNR) ? ID2SYM(rb_intern("FTW_DNR")) :
17
+ (tflag == FTW_DP) ? ID2SYM(rb_intern("FTW_DP")) : (tflag == FTW_F) ? ID2SYM(rb_intern("FTW_F")) :
18
+ (tflag == FTW_NS) ? ID2SYM(rb_intern("FTW_NS")) : (tflag == FTW_SL) ? ID2SYM(rb_intern("FTW_SL")) :
19
+ (tflag == FTW_SLN) ? ID2SYM(rb_intern("FTW_SLN")) : Qnil;
20
+
21
+ rb_hash_aset(
22
+ hash,
23
+ ID2SYM(rb_intern("type_flag")),
24
+ typeFlag
25
+ );
26
+
27
+ // Depth
28
+ rb_hash_aset(
29
+ hash,
30
+ ID2SYM(rb_intern("level")),
31
+ INT2FIX(ftwbuf -> level)
32
+ );
33
+
34
+ // Size
35
+ rb_hash_aset(
36
+ hash,
37
+ ID2SYM(rb_intern("st_size")),
38
+ INT2FIX((intmax_t) sb->st_size)
39
+ );
40
+
41
+ // Path
42
+ rb_hash_aset(
43
+ hash,
44
+ ID2SYM(rb_intern("path")),
45
+ rb_str_new_cstr(fpath)
46
+ );
47
+
48
+ // Basename
49
+ rb_hash_aset(
50
+ hash,
51
+ ID2SYM(rb_intern("basename")),
52
+ rb_str_new_cstr(fpath + ftwbuf->base)
53
+ );
54
+
55
+ rb_ary_push(LIST, hash);
56
+ return 0;
57
+ }
58
+
59
+ static int storeFilesInfo(const char *fpath, const struct stat *sb, int tflag, struct FTW *ftwbuf) {
60
+ if (!(tflag == FTW_F || tflag == FTW_SL || tflag == FTW_SLN))
61
+ return 0;
62
+
63
+ VALUE hash = rb_hash_new();
64
+
65
+ // Raw type flag
66
+ VALUE typeFlag = (tflag == FTW_F) ? ID2SYM(rb_intern("FTW_F")) :
67
+ (tflag == FTW_SL) ? ID2SYM(rb_intern("FTW_SL")) :
68
+ (tflag == FTW_SLN) ? ID2SYM(rb_intern("FTW_SLN")) : Qnil;
69
+
70
+ rb_hash_aset(
71
+ hash,
72
+ ID2SYM(rb_intern("type_flag")),
73
+ typeFlag
74
+ );
75
+
76
+ // Depth
77
+ rb_hash_aset(
78
+ hash,
79
+ ID2SYM(rb_intern("level")),
80
+ INT2FIX(ftwbuf -> level)
81
+ );
82
+
83
+ // Size
84
+ rb_hash_aset(
85
+ hash,
86
+ ID2SYM(rb_intern("st_size")),
87
+ INT2FIX((intmax_t) sb->st_size)
88
+ );
89
+
90
+ // Path
91
+ rb_hash_aset(
92
+ hash,
93
+ ID2SYM(rb_intern("path")),
94
+ rb_str_new_cstr(fpath)
95
+ );
96
+
97
+ // Dirname
98
+ char dirname[ftwbuf->base];
99
+ *dirname = '\0';
100
+ strncat(dirname, fpath, ftwbuf->base - 1);
101
+
102
+ rb_hash_aset(
103
+ hash,
104
+ ID2SYM(rb_intern("dirname")),
105
+ rb_str_new_cstr(dirname)
106
+ );
107
+
108
+ // Basename
109
+ rb_hash_aset(
110
+ hash,
111
+ ID2SYM(rb_intern("basename")),
112
+ rb_str_new_cstr(fpath + ftwbuf->base)
113
+ );
114
+
115
+ rb_ary_push(LIST, hash);
116
+ return 0;
117
+ }
118
+
119
+ static int countChildren(const char *fpath, const struct stat *sb, int tflag, struct FTW *ftwbuf) {
120
+ TOTAL_FILES++;
121
+ return 0;
122
+ }
123
+
124
+ static int countFiles(const char *fpath, const struct stat *sb, int tflag, struct FTW *ftwbuf) {
125
+ if(tflag == FTW_F || tflag == FTW_SL || tflag == FTW_SLN) TOTAL_FILES++;
126
+ return 0;
127
+ }
128
+
129
+ static int countDirectories(const char *fpath, const struct stat *sb, int tflag, struct FTW *ftwbuf) {
130
+ if(tflag == FTW_D || tflag == FTW_DNR) TOTAL_FILES++;
131
+ return 0;
132
+ }
133
+
134
+ VALUE getChildrenCount(VALUE obj, VALUE rb_dir, VALUE rb_flags) {
135
+ TOTAL_FILES = 0;
136
+ rb_ary_clear(LIST);
137
+
138
+ int flags = FIX2INT(rb_flags);
139
+ char *dir = StringValuePtr(rb_dir);
140
+ VALUE returnValue = rb_hash_new();
141
+
142
+ if (nftw(dir, countChildren, 20, flags) == -1) {
143
+ rb_hash_aset(returnValue, ID2SYM(rb_intern("value")), ULL2NUM(TOTAL_FILES));
144
+ rb_hash_aset(returnValue, ID2SYM(rb_intern("error")), Qtrue);
145
+ } else {
146
+ rb_hash_aset(returnValue, ID2SYM(rb_intern("value")), ULL2NUM(TOTAL_FILES));
147
+ rb_hash_aset(returnValue, ID2SYM(rb_intern("error")), Qfalse);
148
+ }
149
+
150
+ return returnValue;
151
+ }
152
+
153
+ VALUE getFilesCount(VALUE obj, VALUE rb_dir) {
154
+ TOTAL_FILES = 0;
155
+
156
+ int flags = FTW_PHYS;
157
+ #ifdef FTW_CONTINUE
158
+ flags |= FTW_CONTINUE;
159
+ #endif
160
+
161
+ if (nftw(StringValuePtr(rb_dir), countFiles, 20, flags) == -1)
162
+ return Qnil;
163
+
164
+ return ULL2NUM(TOTAL_FILES);
165
+ }
166
+
167
+ VALUE getDirectoriesCount(VALUE obj, VALUE rb_dir) {
168
+ TOTAL_FILES = 0;
169
+
170
+ int flags = FTW_PHYS;
171
+ #ifdef FTW_CONTINUE
172
+ flags |= FTW_CONTINUE;
173
+ #endif
174
+
175
+ if (nftw(StringValuePtr(rb_dir), countDirectories, 20, flags) == -1)
176
+ return Qnil;
177
+
178
+ return ULL2NUM(--TOTAL_FILES);
179
+ }
180
+
181
+ VALUE getStat(VALUE obj, VALUE rb_dir, VALUE rb_flags) {
182
+ rb_ary_clear(LIST);
183
+
184
+ int flags = FIX2INT(rb_flags);
185
+ char *dir = StringValuePtr(rb_dir);
186
+ VALUE returnValue = rb_hash_new();
187
+
188
+ if (nftw(dir, storeInfo, 20, flags) == -1) {
189
+ rb_hash_aset(returnValue, ID2SYM(rb_intern("value")), LIST);
190
+ rb_hash_aset(returnValue, ID2SYM(rb_intern("error")), Qtrue);
191
+ } else {
192
+ rb_hash_aset(returnValue, ID2SYM(rb_intern("value")), LIST);
193
+ rb_hash_aset(returnValue, ID2SYM(rb_intern("error")), Qfalse);
194
+ }
195
+
196
+ return returnValue;
197
+ }
198
+
199
+ VALUE getFilesStat(VALUE obj, VALUE rb_dir) {
200
+ rb_ary_clear(LIST);
201
+
202
+ int flags = FTW_PHYS;
203
+ #ifdef FTW_CONTINUE
204
+ flags |= FTW_CONTINUE;
205
+ #endif
206
+
207
+ char *dir = StringValuePtr(rb_dir);
208
+ VALUE returnValue = rb_hash_new();
209
+
210
+ if (nftw(dir, storeFilesInfo, 20, flags) == -1) {
211
+ rb_hash_aset(returnValue, ID2SYM(rb_intern("value")), LIST);
212
+ rb_hash_aset(returnValue, ID2SYM(rb_intern("error")), Qtrue);
213
+ } else {
214
+ rb_hash_aset(returnValue, ID2SYM(rb_intern("value")), LIST);
215
+ rb_hash_aset(returnValue, ID2SYM(rb_intern("error")), Qfalse);
216
+ }
217
+
218
+ return returnValue;
219
+ }
220
+
221
+ // Return all flags as hash
222
+ VALUE flags_hash(VALUE nftw) {
223
+ VALUE all_flags = rb_hash_new();
224
+ rb_ary_clear(LIST);
225
+
226
+ // All Flags
227
+ VALUE flags = rb_hash_new();
228
+ #ifdef FTW_ACTIONRETVAL
229
+ rb_hash_aset(flags, ID2SYM(rb_intern("FTW_ACTIONRETVAL")), INT2FIX(FTW_ACTIONRETVAL));
230
+ #endif
231
+
232
+ rb_hash_aset(flags, ID2SYM(rb_intern("FTW_CHDIR")), INT2FIX(FTW_CHDIR));
233
+ rb_hash_aset(flags, ID2SYM(rb_intern("FTW_DEPTH")), INT2FIX(FTW_DEPTH));
234
+ rb_hash_aset(flags, ID2SYM(rb_intern("FTW_MOUNT")), INT2FIX(FTW_MOUNT));
235
+ rb_hash_aset(flags, ID2SYM(rb_intern("FTW_PHYS")), INT2FIX(FTW_PHYS));
236
+ rb_hash_aset(all_flags, ID2SYM(rb_intern("flags")), flags);
237
+
238
+ // Actionretval Flags
239
+ #ifdef FTW_ACTIONRETVAL
240
+ VALUE actionretval_flags = rb_hash_new();
241
+ rb_hash_aset(actionretval_flags, ID2SYM(rb_intern("FTW_CONTINUE")), INT2FIX(FTW_CONTINUE));
242
+ rb_hash_aset(actionretval_flags, ID2SYM(rb_intern("FTW_SKIP_SIBLINGS")), INT2FIX(FTW_SKIP_SIBLINGS));
243
+ rb_hash_aset(actionretval_flags, ID2SYM(rb_intern("FTW_SKIP_SUBTREE")), INT2FIX(FTW_SKIP_SUBTREE));
244
+ rb_hash_aset(actionretval_flags, ID2SYM(rb_intern("FTW_STOP")), INT2FIX(FTW_STOP));
245
+ rb_hash_aset(all_flags, ID2SYM(rb_intern("actionretval_flags")), actionretval_flags);
246
+ #endif
247
+
248
+ // Type Flags
249
+ VALUE type_flags = rb_hash_new();
250
+ rb_hash_aset(type_flags, ID2SYM(rb_intern("FTW_D")), INT2FIX(FTW_D));
251
+ rb_hash_aset(type_flags, ID2SYM(rb_intern("FTW_DNR")), INT2FIX(FTW_DNR));
252
+ rb_hash_aset(type_flags, ID2SYM(rb_intern("FTW_DP")), INT2FIX(FTW_DP));
253
+ rb_hash_aset(type_flags, ID2SYM(rb_intern("FTW_F")), INT2FIX(FTW_F));
254
+ rb_hash_aset(type_flags, ID2SYM(rb_intern("FTW_NS")), INT2FIX(FTW_NS));
255
+ rb_hash_aset(type_flags, ID2SYM(rb_intern("FTW_SL")), INT2FIX(FTW_SL));
256
+ rb_hash_aset(type_flags, ID2SYM(rb_intern("FTW_SLN")), INT2FIX(FTW_SLN));
257
+ rb_hash_aset(all_flags, ID2SYM(rb_intern("type_flags")), type_flags);
258
+
259
+ return all_flags;
260
+ }
261
+
262
+ void Init_nftw() {
263
+ // Initialize globals
264
+ LIST = rb_ary_new();
265
+ rb_global_variable(&LIST);
266
+
267
+ // Initialize main LinuxStat and NFTW modules
268
+ VALUE _linux_stat = rb_define_module("LinuxStat");
269
+ VALUE nftw = rb_define_module_under(_linux_stat, "NFTW");
270
+
271
+ // Methods
272
+ rb_define_module_function(nftw, "stat", getStat, 2);
273
+ rb_define_module_function(nftw, "stat_files", getFilesStat, 1);
274
+ rb_define_module_function(nftw, "count_children", getChildrenCount, 2);
275
+ rb_define_module_function(nftw, "count_files", getFilesCount, 1);
276
+ rb_define_module_function(nftw, "count_directories", getDirectoriesCount, 1);
277
+
278
+ // Constants
279
+ rb_define_const(nftw, "FLAGS", flags_hash(nftw));
280
+
281
+ // Flags
282
+ #ifdef FTW_ACTIONRETVAL
283
+ rb_define_const(nftw, "FTW_ACTIONRETVAL", INT2FIX(FTW_ACTIONRETVAL));
284
+ #endif
285
+ rb_define_const(nftw, "FTW_CHDIR", INT2FIX(FTW_CHDIR));
286
+ rb_define_const(nftw, "FTW_DEPTH", INT2FIX(FTW_DEPTH));
287
+ rb_define_const(nftw, "FTW_MOUNT", INT2FIX(FTW_MOUNT));
288
+ rb_define_const(nftw, "FTW_PHYS", INT2FIX(FTW_PHYS));
289
+
290
+ // ActionRetval flags
291
+ #ifdef FTW_ACTIONRETVAL
292
+ rb_define_const(nftw, "FTW_CONTINUE", INT2FIX(FTW_CONTINUE));
293
+ rb_define_const(nftw, "FTW_SKIP_SIBLINGS", INT2FIX(FTW_SKIP_SIBLINGS));
294
+ rb_define_const(nftw, "FTW_SKIP_SUBTREE", INT2FIX(FTW_SKIP_SUBTREE));
295
+ rb_define_const(nftw, "FTW_STOP", INT2FIX(FTW_STOP));
296
+ #endif
297
+
298
+ // typeflags
299
+ rb_define_const(nftw, "FTW_D", INT2FIX(FTW_D));
300
+ rb_define_const(nftw, "FTW_DNR", INT2FIX(FTW_DNR));
301
+ rb_define_const(nftw, "FTW_DP", INT2FIX(FTW_DP));
302
+ rb_define_const(nftw, "FTW_F", INT2FIX(FTW_F));
303
+ rb_define_const(nftw, "FTW_NS", INT2FIX(FTW_NS));
304
+ rb_define_const(nftw, "FTW_SL", INT2FIX(FTW_SL));
305
+ rb_define_const(nftw, "FTW_SLN", INT2FIX(FTW_SLN));
306
+ }
data/ext/nproc/extconf.rb CHANGED
@@ -8,4 +8,6 @@ unless have_header('sched.h') && have_header('ruby.h')
8
8
  abort('Missing header')
9
9
  end
10
10
 
11
+ $CFLAGS << ' -O3 -march=native -mtune=native'
12
+
11
13
  create_makefile 'linux_stat/nproc'
data/ext/nproc/nproc.c CHANGED
@@ -5,27 +5,19 @@
5
5
  #include <sched.h>
6
6
  #include "ruby.h"
7
7
 
8
- #if defined(__GNUC__) && !defined(__clang__) && !defined(__INTEL_COMPILER)
9
- #pragma GCC optimize ("O3")
10
- #pragma GCC diagnostic warning "-Wall"
11
- #elif defined(__clang__)
12
- #pragma clang optimize on
13
- #pragma clang diagnostic warning "-Wall"
14
- #elif defined(__INTEL_COMPILER)
15
- #pragma intel optimization_level 3
16
- #endif
8
+ static VALUE count_cpu_for_pid(VALUE obj, VALUE pid) {
9
+ cpu_set_t set;
10
+ CPU_ZERO(&set);
11
+ pid_t pid_int = (pid_t)NUM2LONG(pid);
17
12
 
18
- static VALUE count_cpu_for_pid(volatile VALUE obj, volatile VALUE pid) {
19
- cpu_set_t set ;
20
- CPU_ZERO(&set) ;
21
- char status = sched_getaffinity(FIX2INT(pid), sizeof(set), &set) ;
13
+ if (sched_getaffinity(pid_int, sizeof(set), &set) == -1)
14
+ return Qnil;
22
15
 
23
- if (status < 0) return Qnil ;
24
- return INT2FIX(CPU_COUNT(&set)) ;
16
+ return INT2FIX(CPU_COUNT(&set));
25
17
  }
26
18
 
27
19
  void Init_nproc() {
28
- VALUE _linux_stat = rb_define_module("LinuxStat") ;
29
- VALUE _nproc = rb_define_module_under(_linux_stat, "Nproc") ;
30
- rb_define_module_function(_nproc, "count_cpu_for_pid", count_cpu_for_pid, 1) ;
20
+ VALUE _linux_stat = rb_define_module("LinuxStat");
21
+ VALUE _nproc = rb_define_module_under(_linux_stat, "Nproc");
22
+ rb_define_module_function(_nproc, "count_cpu_for_pid", count_cpu_for_pid, 1);
31
23
  }
@@ -8,4 +8,6 @@ unless have_header('unistd.h') && have_header('ruby.h')
8
8
  abort('Missing header')
9
9
  end
10
10
 
11
+ $CFLAGS << ' -O3 -march=native -mtune=native'
12
+
11
13
  create_makefile 'linux_stat/procfs'
@@ -1,11 +1,11 @@
1
- static VALUE last_pid(volatile VALUE obj) {
2
- FILE *f = fopen("/proc/loadavg", "r") ;
3
- if (!f) return Qnil ;
1
+ static VALUE last_pid(VALUE obj) {
2
+ FILE *f = fopen("/proc/loadavg", "r");
3
+ if (!f) return Qnil;
4
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 ;
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
9
 
10
- return ULL2NUM(_last_pid) ;
10
+ return ULL2NUM(_last_pid);
11
11
  }