mincore 0.0.1

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.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/mincore.rb +159 -0
  3. metadata +44 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 1bb4b14ef445176843ee1cce364ee2f76d1d42cb
4
+ data.tar.gz: 71e82676c23b42496e4d32a47d77ceaf29ac48dd
5
+ SHA512:
6
+ metadata.gz: 9e09fa3ddc45c88094142ec6b39b4a93760f349496032563d74893aee8df764d67464b4710e31d74db04435bb122d70c0fcf1c25a6f307b1471763226ea2b5a3
7
+ data.tar.gz: 0811d685882f4c008c5a665eaffcdf97f5e8b92156d01b04e8a072b01b6f8757799ac3af66e8ab6664a523ebc1035e7dedd2f42faab63543360164da1dd1ac98
data/lib/mincore.rb ADDED
@@ -0,0 +1,159 @@
1
+ require 'inline'
2
+
3
+
4
+ class File
5
+
6
+
7
+ inline do |builder|
8
+
9
+ builder.c_raw_singleton "
10
+ static VALUE PAGESIZE(int argc, VALUE *argv, VALUE self) {
11
+ int size = getpagesize();
12
+ return rb_int_new(size);
13
+ }
14
+ "
15
+ end
16
+
17
+
18
+ inline do |builder|
19
+ builder.include("<stdio.h>")
20
+ builder.include("<stdlib.h>")
21
+ builder.include("<sys/stat.h>")
22
+
23
+ builder.include("<sys/types.h>")
24
+ builder.include("<fcntl.h>")
25
+ builder.include("<unistd.h>")
26
+ builder.include("<sys/mman.h>")
27
+ # builder.include("<fcntl.h>")
28
+ # builder.include("<fcntl.h>")
29
+
30
+ builder.prefix("#define exiterr(s) { perror(s); exit(-1); }")
31
+
32
+ builder.c_singleton <<-C_CODE
33
+ static VALUE _mincore(char *filename) {
34
+ int fd;
35
+ struct stat st;
36
+ int pages;
37
+ int PAGESIZE;
38
+ unsigned char *pageinfo = NULL;
39
+ void *file = NULL;
40
+ VALUE val = rb_ary_new2(2);
41
+ VALUE pageinfo_arr;
42
+ int ret;
43
+ int i;
44
+
45
+ PAGESIZE = getpagesize();
46
+
47
+ fd = open(filename, O_RDONLY);
48
+ // fprintf(stderr, "running fstat() on %s", filename);
49
+
50
+
51
+
52
+ if(fstat(fd, &st) == -1)
53
+ exiterr("fstat");
54
+ if(!S_ISREG(st.st_mode)) {
55
+ fprintf(stderr, "%s: S_ISREG: not a regular file", filename);
56
+ return EXIT_FAILURE;
57
+ }
58
+
59
+ if ( st.st_size == 0 ) {
60
+ pageinfo_arr = rb_ary_new2(1);
61
+ rb_ary_push(val, INT2FIX(0));
62
+ rb_ary_push(val, pageinfo_arr);
63
+ return val; // return [0, []]
64
+ }
65
+
66
+ pages = (st.st_size + PAGESIZE - 1) / PAGESIZE;
67
+ pageinfo = calloc(sizeof(*pageinfo), pages);
68
+
69
+ if(!pageinfo) {
70
+ exiterr("calloc");
71
+ }
72
+
73
+ file = mmap(NULL, st.st_size, PROT_NONE, MAP_SHARED, fd, 0);
74
+
75
+ if(file == MAP_FAILED) {
76
+ exiterr("mmap");
77
+ }
78
+
79
+ ret=mincore(file, st.st_size, pageinfo);
80
+
81
+ rb_ary_push(val, INT2FIX(ret));
82
+
83
+ if( ret == -1) {
84
+ rb_ary_push(val, Qnil);
85
+ }
86
+
87
+ pageinfo_arr = rb_ary_new2(pages);
88
+ for(i=0; i<pages; i++) {
89
+ // VALUE status = ((pageinfo[i] & 1)?Qtrue:Qfalse);
90
+ VALUE status = Qtrue;
91
+ rb_ary_push(pageinfo_arr, status);
92
+ }
93
+
94
+
95
+ rb_ary_push(val, pageinfo_arr);
96
+
97
+ // val = (long long) st.st_size;
98
+ // fprintf (stderr, "val=%d\\n", val);
99
+
100
+ munmap(file, st.st_size);
101
+ return val;
102
+ }
103
+ C_CODE
104
+ end
105
+
106
+ inline do |builder|
107
+ builder.include("<sys/types.h>")
108
+ builder.include("<sys/stat.h>")
109
+ builder.include("<fcntl.h>")
110
+ builder.include("<error.h>")
111
+ builder.include("<stdio.h>")
112
+ builder.include("<stdlib.h>")
113
+ builder.include("<string.h>")
114
+
115
+ builder.prefix("#define exiterr(s) { perror(s); exit(-1); }")
116
+
117
+ builder.c_singleton <<-C_CODE
118
+ static VALUE _cachedel(char *filename, int count) {
119
+ int ret=0;
120
+ int i, fd;
121
+ struct stat st;
122
+
123
+ fd = open(filename, O_RDONLY);
124
+
125
+ if(fd == -1) {
126
+ exiterr("open");
127
+ }
128
+
129
+ if(fstat(fd, &st) == -1) {
130
+ exiterr("fstat");
131
+ }
132
+
133
+ if(!S_ISREG(st.st_mode)) {
134
+ fprintf(stderr, "%s: S_ISREG: not a regular file", filename);
135
+ ret = -1;
136
+ }
137
+
138
+ for(i = 0; i < count; i++) {
139
+ if(posix_fadvise(fd, 0, 0, POSIX_FADV_DONTNEED) == -1) {
140
+ exiterr("posix_fadvise");
141
+ }
142
+ }
143
+
144
+ return rb_int_new(ret);
145
+ }
146
+ C_CODE
147
+ end
148
+
149
+ def self.cachedel(filename, count=1)
150
+ self._cachedel(filename, count)
151
+ end
152
+
153
+ #this should work: http://stackoverflow.com/questions/13408136/how-can-i-dynamically-define-an-alias-method-for-a-class-method
154
+ class << self
155
+ alias_method :mincore, :_mincore
156
+ end
157
+
158
+ end
159
+
metadata ADDED
@@ -0,0 +1,44 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mincore
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Reda NOUSHI
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-11-06 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: provides cache inspection and deletion for a specific file.
14
+ email: reda_noushi@yahoo.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/mincore.rb
20
+ homepage: http://rubygems.org/gems/mincore
21
+ licenses:
22
+ - GPLv2
23
+ metadata: {}
24
+ post_install_message:
25
+ rdoc_options: []
26
+ require_paths:
27
+ - lib
28
+ required_ruby_version: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ required_rubygems_version: !ruby/object:Gem::Requirement
34
+ requirements:
35
+ - - '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ requirements: []
39
+ rubyforge_project:
40
+ rubygems_version: 2.1.10
41
+ signing_key:
42
+ specification_version: 4
43
+ summary: Ruby bindings for Linux cache manipulation
44
+ test_files: []