sigar 0.7.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (66) hide show
  1. data/README +2 -0
  2. data/Rakefile +105 -0
  3. data/bindings/SigarBuild.pm +310 -0
  4. data/bindings/SigarWrapper.pm +2978 -0
  5. data/bindings/ruby/examples/arp.rb +24 -0
  6. data/bindings/ruby/examples/cpu_info.rb +35 -0
  7. data/bindings/ruby/examples/df.rb +49 -0
  8. data/bindings/ruby/examples/free.rb +36 -0
  9. data/bindings/ruby/examples/ifconfig.rb +101 -0
  10. data/bindings/ruby/examples/logging.rb +58 -0
  11. data/bindings/ruby/examples/net_info.rb +31 -0
  12. data/bindings/ruby/examples/netstat.rb +71 -0
  13. data/bindings/ruby/examples/pargs.rb +35 -0
  14. data/bindings/ruby/examples/penv.rb +31 -0
  15. data/bindings/ruby/examples/route.rb +48 -0
  16. data/bindings/ruby/examples/version.rb +40 -0
  17. data/bindings/ruby/examples/who.rb +30 -0
  18. data/bindings/ruby/extconf.rb +128 -0
  19. data/bindings/ruby/rbsigar.c +888 -0
  20. data/bindings/ruby/test/cpu_test.rb +40 -0
  21. data/bindings/ruby/test/file_system_test.rb +43 -0
  22. data/bindings/ruby/test/helper.rb +57 -0
  23. data/bindings/ruby/test/loadavg_test.rb +30 -0
  24. data/bindings/ruby/test/mem_test.rb +45 -0
  25. data/bindings/ruby/test/swap_test.rb +36 -0
  26. data/bindings/ruby/test/uptime_test.rb +26 -0
  27. data/include/sigar.h +939 -0
  28. data/include/sigar_fileinfo.h +157 -0
  29. data/include/sigar_format.h +65 -0
  30. data/include/sigar_getline.h +18 -0
  31. data/include/sigar_log.h +80 -0
  32. data/include/sigar_private.h +422 -0
  33. data/include/sigar_ptql.h +53 -0
  34. data/include/sigar_util.h +191 -0
  35. data/src/os/aix/aix_sigar.c +2151 -0
  36. data/src/os/aix/sigar_os.h +73 -0
  37. data/src/os/darwin/Info.plist.in +27 -0
  38. data/src/os/darwin/darwin_sigar.c +3709 -0
  39. data/src/os/darwin/sigar_os.h +80 -0
  40. data/src/os/hpux/hpux_sigar.c +1342 -0
  41. data/src/os/hpux/sigar_os.h +49 -0
  42. data/src/os/linux/linux_sigar.c +2782 -0
  43. data/src/os/linux/sigar_os.h +82 -0
  44. data/src/os/solaris/get_mib2.c +321 -0
  45. data/src/os/solaris/get_mib2.h +127 -0
  46. data/src/os/solaris/kstats.c +181 -0
  47. data/src/os/solaris/procfs.c +97 -0
  48. data/src/os/solaris/sigar_os.h +224 -0
  49. data/src/os/solaris/solaris_sigar.c +2717 -0
  50. data/src/os/win32/peb.c +212 -0
  51. data/src/os/win32/sigar.rc.in +40 -0
  52. data/src/os/win32/sigar_os.h +653 -0
  53. data/src/os/win32/sigar_pdh.h +47 -0
  54. data/src/os/win32/win32_sigar.c +3911 -0
  55. data/src/sigar.c +2428 -0
  56. data/src/sigar_cache.c +179 -0
  57. data/src/sigar_fileinfo.c +815 -0
  58. data/src/sigar_format.c +696 -0
  59. data/src/sigar_getline.c +1849 -0
  60. data/src/sigar_ptql.c +1967 -0
  61. data/src/sigar_signal.c +216 -0
  62. data/src/sigar_util.c +1060 -0
  63. data/src/sigar_version.c.in +22 -0
  64. data/src/sigar_version_autoconf.c.in +22 -0
  65. data/version.properties +11 -0
  66. metadata +131 -0
@@ -0,0 +1,181 @@
1
+ /*
2
+ * Copyright (c) 2004-2007 Hyperic, Inc.
3
+ * Copyright (c) 2009 SpringSource, Inc.
4
+ *
5
+ * Licensed under the Apache License, Version 2.0 (the "License");
6
+ * you may not use this file except in compliance with the License.
7
+ * You may obtain a copy of the License at
8
+ *
9
+ * http://www.apache.org/licenses/LICENSE-2.0
10
+ *
11
+ * Unless required by applicable law or agreed to in writing, software
12
+ * distributed under the License is distributed on an "AS IS" BASIS,
13
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ * See the License for the specific language governing permissions and
15
+ * limitations under the License.
16
+ */
17
+
18
+ #include "sigar.h"
19
+ #include "sigar_private.h"
20
+ #include "sigar_util.h"
21
+ #include "sigar_os.h"
22
+
23
+ int sigar_get_kstats(sigar_t *sigar)
24
+ {
25
+ kstat_ctl_t *kc = sigar->kc;
26
+ unsigned int i, id, ncpu = sysconf(_SC_NPROCESSORS_CONF);
27
+ int is_debug = SIGAR_LOG_IS_DEBUG(sigar);
28
+
29
+ if (ncpu != sigar->ncpu) {
30
+ if (!sigar->ks.lcpu) {
31
+ /* init */
32
+ sigar->ks.lcpu = ncpu;
33
+ sigar->ks.cpu = malloc(sizeof(*(sigar->ks.cpu)) * ncpu);
34
+ sigar->ks.cpu_info = malloc(sizeof(*(sigar->ks.cpu_info)) * ncpu);
35
+ sigar->ks.cpuid = malloc(sizeof(*(sigar->ks.cpuid)) * ncpu);
36
+ }
37
+ else {
38
+ sigar_log_printf(sigar, SIGAR_LOG_INFO,
39
+ "ncpu changed from %d to %d",
40
+ sigar->ncpu, ncpu);
41
+ if (ncpu > sigar->ks.lcpu) {
42
+ /* one or more cpus have been added */
43
+ sigar->ks.cpu = realloc(sigar->ks.cpu,
44
+ sizeof(*(sigar->ks.cpu)) * ncpu);
45
+ sigar->ks.cpu_info = realloc(sigar->ks.cpu_info,
46
+ sizeof(*(sigar->ks.cpu_info)) * ncpu);
47
+ sigar->ks.cpuid = realloc(sigar->ks.cpuid,
48
+ sizeof(*(sigar->ks.cpuid)) * ncpu);
49
+ sigar->ks.lcpu = ncpu;
50
+ }
51
+ /* else or more cpus have been removed */
52
+ }
53
+
54
+ sigar->ncpu = ncpu;
55
+
56
+ /* from man p_online:
57
+ * ``Processor numbers are integers,
58
+ * greater than or equal to 0,
59
+ * and are defined by the hardware platform.
60
+ * Processor numbers are not necessarily contiguous,
61
+ * but "not too sparse."``
62
+ * so we maintain our own mapping in ks.cpuid[]
63
+ */
64
+
65
+ /* lookup in order, which kstat chain may not be in */
66
+ for (i=0, id=0; i<ncpu; id++) {
67
+ kstat_t *cpu_info, *cpu_stat;
68
+
69
+ if (!(cpu_info = kstat_lookup(kc, "cpu_info", id, NULL))) {
70
+ continue;
71
+ }
72
+
73
+ if (!(cpu_stat = kstat_lookup(kc, "cpu_stat", id, NULL))) {
74
+ /* XXX warn, faulted cpu? */
75
+ }
76
+
77
+ sigar->ks.cpu_info[i] = cpu_info;
78
+ sigar->ks.cpu[i] = cpu_stat;
79
+ sigar->ks.cpuid[i] = id;
80
+
81
+ if (is_debug) {
82
+ sigar_log_printf(sigar, SIGAR_LOG_DEBUG,
83
+ "cpu %d id=%d", i, sigar->ks.cpuid[i]);
84
+ }
85
+ i++;
86
+ }
87
+ }
88
+
89
+ sigar->ks.system = kstat_lookup(kc, "unix", -1, "system_misc");
90
+ sigar->ks.syspages = kstat_lookup(kc, "unix", -1, "system_pages");
91
+ sigar->ks.mempages = kstat_lookup(kc, "bunyip", -1, "mempages");
92
+
93
+ return SIGAR_OK;
94
+ }
95
+
96
+ SIGAR_INLINE kid_t sigar_kstat_update(sigar_t *sigar)
97
+ {
98
+ kid_t id = kstat_chain_update(sigar->kc);
99
+
100
+ switch (id) {
101
+ case -1:
102
+ sigar_log_printf(sigar, SIGAR_LOG_ERROR,
103
+ "kstat_chain_update error: %s",
104
+ sigar_strerror(sigar, errno));
105
+ break;
106
+ case 0:
107
+ /* up-to-date */
108
+ break;
109
+ default:
110
+ sigar_get_kstats(sigar);
111
+ sigar_log(sigar, SIGAR_LOG_DEBUG,
112
+ "kstat chain updated");
113
+ break;
114
+ }
115
+
116
+ return id;
117
+ }
118
+
119
+ /*
120
+ * bincompat is not possible with certain kstat data structures between
121
+ * solaris 2.6, 2.7, 2.8, etc. alternative is to use kstat_data_lookup()
122
+ * which means everytime we want a stat, must do a linear search
123
+ * of ksp->ks_data. eek. so we meet half way and do the search for
124
+ * each key once per sigar_t instance. once the initial search has
125
+ * been done, we have a table of offsets to quickly access the stats via
126
+ * ksp->ks_data + offset. this gives us bincompat without the overhead
127
+ * of many kstat_data_lookup calls.
128
+ */
129
+ static SIGAR_INLINE int kstat_named_offset(kstat_t *ksp, const char *name)
130
+ {
131
+ unsigned int i;
132
+ kstat_named_t *kn;
133
+
134
+ for (i=0, kn=ksp->ks_data;
135
+ i<ksp->ks_ndata;
136
+ i++, kn++)
137
+ {
138
+ if (strEQ(kn->name, name)) {
139
+ return i;
140
+ }
141
+ }
142
+
143
+ return -2; /* not found */
144
+ }
145
+
146
+ static char *kstat_keys_system[] = {
147
+ "boot_time",
148
+ "avenrun_1min",
149
+ "avenrun_5min",
150
+ "avenrun_15min",
151
+ NULL
152
+ };
153
+
154
+ static char *kstat_keys_mempages[] = {
155
+ "pages_anon",
156
+ "pages_exec",
157
+ "pages_vnode",
158
+ NULL
159
+ };
160
+
161
+ static char *kstat_keys_syspages[] = {
162
+ "pagesfree",
163
+ NULL
164
+ };
165
+
166
+ static char **kstat_keys[] = {
167
+ kstat_keys_system,
168
+ kstat_keys_mempages,
169
+ kstat_keys_syspages,
170
+ };
171
+
172
+ void sigar_koffsets_lookup(kstat_t *ksp, int *offsets, int kidx)
173
+ {
174
+ int i;
175
+ char **keys = kstat_keys[kidx];
176
+
177
+ for (i=0; keys[i]; i++) {
178
+ offsets[i] = kstat_named_offset(ksp, keys[i]);
179
+ }
180
+ }
181
+
@@ -0,0 +1,97 @@
1
+ /*
2
+ * Copyright (c) 2004, 2006 Hyperic, Inc.
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+
17
+ #include "sigar.h"
18
+ #include "sigar_private.h"
19
+ #include "sigar_util.h"
20
+ #include "sigar_os.h"
21
+
22
+ #define my_pread(fd, ptr, type, offset) \
23
+ (pread(fd, ptr, sizeof(type), offset) == sizeof(type))
24
+
25
+ int sigar_proc_psinfo_get(sigar_t *sigar, sigar_pid_t pid)
26
+ {
27
+ int fd, retval = SIGAR_OK;
28
+ char buffer[BUFSIZ];
29
+ time_t timenow = time(NULL);
30
+
31
+ if (sigar->pinfo == NULL) {
32
+ sigar->pinfo = malloc(sizeof(*sigar->pinfo));
33
+ }
34
+
35
+ if (sigar->last_pid == pid) {
36
+ if ((timenow - sigar->last_getprocs) < SIGAR_LAST_PROC_EXPIRE) {
37
+ return SIGAR_OK;
38
+ }
39
+ }
40
+
41
+ sigar->last_pid = pid;
42
+ sigar->last_getprocs = timenow;
43
+
44
+ (void)SIGAR_PROC_FILENAME(buffer, pid, "/psinfo");
45
+
46
+ if ((fd = open(buffer, O_RDONLY)) < 0) {
47
+ return ESRCH;
48
+ }
49
+
50
+ if (!my_pread(fd, sigar->pinfo, psinfo_t, 0)) {
51
+ retval = errno;
52
+ }
53
+
54
+ close(fd);
55
+
56
+ return retval;
57
+ }
58
+
59
+ int sigar_proc_usage_get(sigar_t *sigar, prusage_t *prusage, sigar_pid_t pid)
60
+ {
61
+ int fd, retval = SIGAR_OK;
62
+ char buffer[BUFSIZ];
63
+
64
+ (void)SIGAR_PROC_FILENAME(buffer, pid, "/usage");
65
+
66
+ if ((fd = open(buffer, O_RDONLY)) < 0) {
67
+ return ESRCH;
68
+ }
69
+
70
+ if (!my_pread(fd, prusage, prusage_t, 0)) {
71
+ retval = errno;
72
+ }
73
+
74
+ close(fd);
75
+
76
+ return retval;
77
+ }
78
+
79
+ int sigar_proc_status_get(sigar_t *sigar, pstatus_t *pstatus, sigar_pid_t pid)
80
+ {
81
+ int fd, retval = SIGAR_OK;
82
+ char buffer[BUFSIZ];
83
+
84
+ (void)SIGAR_PROC_FILENAME(buffer, pid, "/status");
85
+
86
+ if ((fd = open(buffer, O_RDONLY)) < 0) {
87
+ return ESRCH;
88
+ }
89
+
90
+ if (!my_pread(fd, pstatus, pstatus_t, 0)) {
91
+ retval = errno;
92
+ }
93
+
94
+ close(fd);
95
+
96
+ return retval;
97
+ }
@@ -0,0 +1,224 @@
1
+ /*
2
+ * Copyright (c) 2004-2007 Hyperic, Inc.
3
+ * Copyright (c) 2009 SpringSource, Inc.
4
+ *
5
+ * Licensed under the Apache License, Version 2.0 (the "License");
6
+ * you may not use this file except in compliance with the License.
7
+ * You may obtain a copy of the License at
8
+ *
9
+ * http://www.apache.org/licenses/LICENSE-2.0
10
+ *
11
+ * Unless required by applicable law or agreed to in writing, software
12
+ * distributed under the License is distributed on an "AS IS" BASIS,
13
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ * See the License for the specific language governing permissions and
15
+ * limitations under the License.
16
+ */
17
+
18
+ #ifndef SIGAR_OS_H
19
+ #define SIGAR_OS_H
20
+
21
+ #ifndef _POSIX_PTHREAD_SEMANTICS
22
+ #define _POSIX_PTHREAD_SEMANTICS
23
+ #endif
24
+
25
+ typedef unsigned long long int u_int64_t;
26
+
27
+ #include <ctype.h>
28
+ #include <assert.h>
29
+ #ifndef DMALLOC
30
+ #include <malloc.h>
31
+ #endif
32
+ #include <unistd.h>
33
+ #include <fcntl.h>
34
+ #include <stdio.h>
35
+ #include <errno.h>
36
+ #include <stdlib.h>
37
+ #include <sys/types.h>
38
+ #include <sys/processor.h>
39
+ #include <sys/sysinfo.h>
40
+ #include <sys/param.h>
41
+
42
+ #include <kstat.h>
43
+ #include <procfs.h>
44
+
45
+ #include "get_mib2.h"
46
+
47
+ /* avoid -Wall warning since solaris doesnt have a prototype for this */
48
+ int getdomainname(char *, int);
49
+
50
+ typedef struct {
51
+ kstat_t **ks;
52
+ int num;
53
+ char *name;
54
+ int nlen;
55
+ } kstat_list_t;
56
+
57
+ SIGAR_INLINE kid_t sigar_kstat_update(sigar_t *sigar);
58
+
59
+ int sigar_get_kstats(sigar_t *sigar);
60
+
61
+ void sigar_init_multi_kstats(sigar_t *sigar);
62
+
63
+ void sigar_free_multi_kstats(sigar_t *sigar);
64
+
65
+ int sigar_get_multi_kstats(sigar_t *sigar,
66
+ kstat_list_t *kl,
67
+ const char *name,
68
+ kstat_t **retval);
69
+
70
+ void sigar_koffsets_lookup(kstat_t *ksp, int *offsets, int kidx);
71
+
72
+ int sigar_proc_psinfo_get(sigar_t *sigar, sigar_pid_t pid);
73
+
74
+ int sigar_proc_usage_get(sigar_t *sigar, prusage_t *prusage, sigar_pid_t pid);
75
+
76
+ int sigar_proc_status_get(sigar_t *sigar, pstatus_t *pstatus, sigar_pid_t pid);
77
+
78
+ #define CPU_ONLINE(n) \
79
+ (p_online(n, P_STATUS) == P_ONLINE)
80
+
81
+ typedef enum {
82
+ KSTAT_SYSTEM_BOOT_TIME,
83
+ KSTAT_SYSTEM_LOADAVG_1,
84
+ KSTAT_SYSTEM_LOADAVG_2,
85
+ KSTAT_SYSTEM_LOADAVG_3,
86
+ KSTAT_SYSTEM_MAX
87
+ } kstat_system_off_e;
88
+
89
+ typedef enum {
90
+ KSTAT_MEMPAGES_ANON,
91
+ KSTAT_MEMPAGES_EXEC,
92
+ KSTAT_MEMPAGES_VNODE,
93
+ KSTAT_MEMPAGES_MAX
94
+ } kstat_mempages_off_e;
95
+
96
+ typedef enum {
97
+ KSTAT_SYSPAGES_FREE,
98
+ KSTAT_SYSPAGES_MAX
99
+ } kstat_syspages_off_e;
100
+
101
+ enum {
102
+ KSTAT_KEYS_system,
103
+ KSTAT_KEYS_mempages,
104
+ KSTAT_KEYS_syspages,
105
+ } kstat_keys_e;
106
+
107
+ typedef struct ps_prochandle * (*proc_grab_func_t)(pid_t, int, int *);
108
+
109
+ typedef void (*proc_free_func_t)(struct ps_prochandle *);
110
+
111
+ typedef int (*proc_create_agent_func_t)(struct ps_prochandle *);
112
+
113
+ typedef void (*proc_destroy_agent_func_t)(struct ps_prochandle *);
114
+
115
+ typedef void (*proc_objname_func_t)(struct ps_prochandle *,
116
+ uintptr_t, const char *, size_t);
117
+
118
+ typedef char * (*proc_dirname_func_t)(const char *, char *, size_t);
119
+
120
+ typedef char * (*proc_exename_func_t)(struct ps_prochandle *, char *, size_t);
121
+
122
+ typedef int (*proc_fstat64_func_t)(struct ps_prochandle *, int, void *);
123
+
124
+ typedef int (*proc_getsockopt_func_t)(struct ps_prochandle *,
125
+ int, int, int, void *, int *);
126
+
127
+ typedef int (*proc_getsockname_func_t)(struct ps_prochandle *,
128
+ int, struct sockaddr *, socklen_t *);
129
+
130
+ struct sigar_t {
131
+ SIGAR_T_BASE;
132
+
133
+ int solaris_version;
134
+ int use_ucb_ps;
135
+
136
+ kstat_ctl_t *kc;
137
+
138
+ /* kstat_lookup() as needed */
139
+ struct {
140
+ kstat_t **cpu;
141
+ kstat_t **cpu_info;
142
+ processorid_t *cpuid;
143
+ unsigned int lcpu; /* number malloced slots in the cpu array above */
144
+ kstat_t *system;
145
+ kstat_t *syspages;
146
+ kstat_t *mempages;
147
+ } ks;
148
+
149
+ struct {
150
+ int system[KSTAT_SYSTEM_MAX];
151
+ int mempages[KSTAT_MEMPAGES_MAX];
152
+ int syspages[KSTAT_SYSPAGES_MAX];
153
+ } koffsets;
154
+
155
+ int pagesize;
156
+
157
+ time_t last_getprocs;
158
+ sigar_pid_t last_pid;
159
+ psinfo_t *pinfo;
160
+ sigar_cpu_list_t cpulist;
161
+
162
+ /* libproc.so interface */
163
+ void *plib;
164
+ proc_grab_func_t pgrab;
165
+ proc_free_func_t pfree;
166
+ proc_create_agent_func_t pcreate_agent;
167
+ proc_destroy_agent_func_t pdestroy_agent;
168
+ proc_objname_func_t pobjname;
169
+ proc_dirname_func_t pdirname;
170
+ proc_exename_func_t pexename;
171
+ proc_fstat64_func_t pfstat64;
172
+ proc_getsockopt_func_t pgetsockopt;
173
+ proc_getsockname_func_t pgetsockname;
174
+
175
+ sigar_cache_t *pargs;
176
+
177
+ solaris_mib2_t mib2;
178
+ };
179
+
180
+ #ifdef SIGAR_64BIT
181
+ #define KSTAT_UINT ui64
182
+ #else
183
+ #define KSTAT_UINT ui32
184
+ #endif
185
+
186
+ #define kSTAT_exists(v, type) \
187
+ (sigar->koffsets.type[v] != -2)
188
+
189
+ #define kSTAT_ptr(v, type) \
190
+ ((kstat_named_t *)ksp->ks_data + sigar->koffsets.type[v])
191
+
192
+ #define kSTAT_uint(v, type) \
193
+ (kSTAT_exists(v, type) ? kSTAT_ptr(v, type)->value.KSTAT_UINT : 0)
194
+
195
+ #define kSTAT_ui32(v, type) \
196
+ (kSTAT_exists(v, type) ? kSTAT_ptr(v, type)->value.ui32 : 0)
197
+
198
+ #define kSYSTEM(v) kSTAT_ui32(v, system)
199
+
200
+ #define kMEMPAGES(v) kSTAT_uint(v, mempages)
201
+
202
+ #define kSYSPAGES(v) kSTAT_uint(v, syspages)
203
+
204
+ #define sigar_koffsets_init(sigar, ksp, type) \
205
+ if (sigar->koffsets.type[0] == -1) \
206
+ sigar_koffsets_lookup(ksp, sigar->koffsets.type, KSTAT_KEYS_##type)
207
+
208
+ #define sigar_koffsets_init_system(sigar, ksp) \
209
+ sigar_koffsets_init(sigar, ksp, system)
210
+
211
+ #define sigar_koffsets_init_mempages(sigar, ksp) \
212
+ sigar_koffsets_init(sigar, ksp, mempages)
213
+
214
+ #define sigar_koffsets_init_syspages(sigar, ksp) \
215
+ sigar_koffsets_init(sigar, ksp, syspages)
216
+
217
+ #define HAVE_READDIR_R
218
+ #define HAVE_GETPWNAM_R
219
+ #define HAVE_GETPWUID_R
220
+
221
+ #define SIGAR_EMIB2 (SIGAR_OS_START_ERROR+1)
222
+
223
+ #endif /* SIGAR_OS_H */
224
+