1 |
#include <stdio.h> |
2 |
#include "ukcprog.h" |
3 |
#include <unistd.h> |
4 |
#ifdef SOLARIS |
5 |
#include <kstat.h> |
6 |
#include <sys/sysinfo.h> |
7 |
#endif |
8 |
#ifdef LINUX |
9 |
#include <string.h> |
10 |
#endif |
11 |
#ifdef FREEBSD |
12 |
#include <sys/types.h> |
13 |
#include <sys/sysctl.h> |
14 |
#endif |
15 |
|
16 |
#define WAIT_TIME_IN_SECS 1 |
17 |
|
18 |
char *get_page_stats(){ |
19 |
char *xml_page_stats; |
20 |
int swap_pageins; |
21 |
int swap_pageouts; |
22 |
#ifdef SOLARIS |
23 |
kstat_ctl_t *kc; |
24 |
kstat_t *ksp; |
25 |
cpu_stat_t cs; |
26 |
uint_t swapin, swapout; |
27 |
#endif |
28 |
#ifdef LINUX |
29 |
FILE *f; |
30 |
char *line; |
31 |
long swapin[2], swapout[2]; |
32 |
#endif |
33 |
#ifdef FREEBSD |
34 |
size_t size; |
35 |
long swapin[2], swapout[2]; |
36 |
#endif |
37 |
|
38 |
|
39 |
#ifdef SOLARIS |
40 |
if ((kc = kstat_open()) == NULL) { |
41 |
errf("kstat_open failure (%m)"); |
42 |
return NULL; |
43 |
} |
44 |
for (ksp = kc->kc_chain; ksp!=NULL; ksp = ksp->ks_next) { |
45 |
if ((strcmp(ksp->ks_module, "cpu_stat")) != 0) continue; |
46 |
if (kstat_read(kc, ksp, &cs) == -1) { |
47 |
errf("kstat read failure"); |
48 |
continue; |
49 |
} |
50 |
} |
51 |
|
52 |
swapin=cs.cpu_vminfo.pgswapin; |
53 |
swapout=cs.cpu_vminfo.pgswapout; |
54 |
|
55 |
sleep(WAIT_TIME_IN_SECS); |
56 |
|
57 |
if (kstat_chain_update(kc) == -1) { |
58 |
errf("Kstat update failure (%m)"); |
59 |
return NULL; |
60 |
} |
61 |
for (ksp = kc->kc_chain; ksp!=NULL; ksp = ksp->ks_next) { |
62 |
if ((strcmp(ksp->ks_module, "cpu_stat")) != 0) continue; |
63 |
if (kstat_read(kc, ksp, &cs) == -1) { |
64 |
errf("kstat read failure"); |
65 |
continue; |
66 |
} |
67 |
} |
68 |
if((kstat_close(kc)) != 0){ |
69 |
errf("Failed to close kstat control structure (%m)"); |
70 |
return NULL; |
71 |
} |
72 |
swap_pageins=(cs.cpu_vminfo.pgswapin-swapin)/WAIT_TIME_IN_SECS; |
73 |
swap_pageouts=(cs.cpu_vminfo.pgswapout-swapout)/WAIT_TIME_IN_SECS; |
74 |
#endif |
75 |
#ifdef LINUX |
76 |
if ((f=fopen("/proc/stat", "r" ))==NULL) { |
77 |
errf("Failed to open /proc/stat (%m)"); |
78 |
return NULL; |
79 |
} |
80 |
while((line=fpgetline(f)) != NULL){ |
81 |
if (((strncmp(line,"swap",4)) == 0)) { |
82 |
if((sscanf(line, "%*s %ld %ld", &swapin[0], &swapout[0])) != 2){ |
83 |
errf("Failed to read swap paging details (%m)"); |
84 |
return NULL; |
85 |
} |
86 |
break; |
87 |
} |
88 |
} |
89 |
if ((fclose(f)) != 0) { |
90 |
errf("Failed to close file (%m)"); |
91 |
return NULL; |
92 |
} |
93 |
sleep(WAIT_TIME_IN_SECS); |
94 |
|
95 |
if ((f=fopen("/proc/stat", "r" ))==NULL) { |
96 |
errf("Failed to open /proc/stat (%m)"); |
97 |
return NULL; |
98 |
} |
99 |
while((line=fpgetline(f)) != NULL){ |
100 |
if (((strncmp(line,"swap",4)) == 0)) { |
101 |
if((sscanf(line, "%*s %ld %ld", &swapin[1], &swapout[1])) != 2){ |
102 |
errf("Failed to read swap paging details (%m)"); |
103 |
return NULL; |
104 |
} |
105 |
break; |
106 |
} |
107 |
} |
108 |
if ((fclose(f)) != 0) { |
109 |
errf("Failed to close file (%m)"); |
110 |
return NULL; |
111 |
} |
112 |
|
113 |
swap_pageins=(swapin[1]-swapin[0])/WAIT_TIME_IN_SECS; |
114 |
swap_pageouts=(swapout[1]-swapout[0])/WAIT_TIME_IN_SECS; |
115 |
#endif |
116 |
#ifdef FREEBSD |
117 |
#define SWAP_IN_NAME "vm.stats.vm.v_swappgsin" |
118 |
#define SWAP_OUT_NAME "vm.stats.vm.v_swappgsout" |
119 |
if (sysctlbyname(SWAP_IN_NAME, NULL, &size, NULL, NULL) < 0){ |
120 |
errf("sysctlbyname failed to get total memory (%m)"); |
121 |
return NULL; |
122 |
} |
123 |
if (sysctlbyname(SWAP_IN_NAME, &swapin[0], &size, NULL, NULL) < 0){ |
124 |
errf("Failed to get total memory stats (%m)"); |
125 |
return NULL; |
126 |
} |
127 |
if (sysctlbyname(SWAP_OUT_NAME, NULL, &size, NULL, NULL) < 0){ |
128 |
errf("sysctlbyname failed to get total memory (%m)"); |
129 |
return NULL; |
130 |
} |
131 |
if (sysctlbyname(SWAP_OUT_NAME, &swapout[0], &size, NULL, NULL) < 0){ |
132 |
errf("Failed to get total memory stats (%m)"); |
133 |
return NULL; |
134 |
} |
135 |
|
136 |
sleep(WAIT_TIME_IN_SECS); |
137 |
|
138 |
if (sysctlbyname(SWAP_IN_NAME, NULL, &size, NULL, NULL) < 0){ |
139 |
errf("sysctlbyname failed to get total memory (%m)"); |
140 |
return NULL; |
141 |
} |
142 |
if (sysctlbyname(SWAP_IN_NAME, &swapin[1], &size, NULL, NULL) < 0){ |
143 |
errf("Failed to get total memory stats (%m)"); |
144 |
return NULL; |
145 |
} |
146 |
if (sysctlbyname(SWAP_OUT_NAME, NULL, &size, NULL, NULL) < 0){ |
147 |
errf("sysctlbyname failed to get total memory (%m)"); |
148 |
return NULL; |
149 |
} |
150 |
if (sysctlbyname(SWAP_OUT_NAME, &swapout[1], &size, NULL, NULL) < 0){ |
151 |
errf("Failed to get total memory stats (%m)"); |
152 |
return NULL; |
153 |
} |
154 |
|
155 |
swap_pageins=(swapin[1]-swapin[0])/WAIT_TIME_IN_SECS; |
156 |
swap_pageouts=(swapout[1]-swapout[0])/WAIT_TIME_IN_SECS; |
157 |
|
158 |
#endif |
159 |
|
160 |
|
161 |
if((xml_page_stats=strf("<pages><swapins>%d</swapins><swapouts>%d</swapouts></pages>", swap_pageins, swap_pageouts)) == NULL){ |
162 |
errf("strf failed (%m)"); |
163 |
return NULL; |
164 |
} |
165 |
return xml_page_stats; |
166 |
} |