ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/cms/source/ihost/libstatgrab/page_stats.c
Revision: 1.1
Committed: Thu May 16 14:55:38 2002 UTC (22 years, 6 months ago) by pajs
Content type: text/plain
Branch: MAIN
Log Message:
Swap paging stats. Should give a better idea when the system is really out of physical ram as it would then
start swapping.

File Contents

# Content
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
69 swap_pageins=(cs.cpu_vminfo.pgswapin-swapin)/WAIT_TIME_IN_SECS;
70 swap_pageouts=(cs.cpu_vminfo.pgswapout-swapout)/WAIT_TIME_IN_SECS;
71 #endif
72 #ifdef LINUX
73 if ((f=fopen("/proc/stat", "r" ))==NULL) {
74 errf("Failed to open /proc/stat (%m)");
75 return NULL;
76 }
77 while((line=fpgetline(f)) != NULL){
78 if (((strncmp(line,"swap",4)) == 0)) {
79 if((sscanf(line, "%*s %ld %ld", &swapin[0], &swapout[0])) != 2){
80 errf("Failed to read swap paging details (%m)");
81 return NULL;
82 }
83 break;
84 }
85 }
86 if ((fclose(f)) != 0) {
87 errf("Failed to close file (%m)");
88 return NULL;
89 }
90 sleep(WAIT_TIME_IN_SECS);
91
92 if ((f=fopen("/proc/stat", "r" ))==NULL) {
93 errf("Failed to open /proc/stat (%m)");
94 return NULL;
95 }
96 while((line=fpgetline(f)) != NULL){
97 if (((strncmp(line,"swap",4)) == 0)) {
98 if((sscanf(line, "%*s %ld %ld", &swapin[1], &swapout[1])) != 2){
99 errf("Failed to read swap paging details (%m)");
100 return NULL;
101 }
102 break;
103 }
104 }
105 if ((fclose(f)) != 0) {
106 errf("Failed to close file (%m)");
107 return NULL;
108 }
109
110 swap_pageins=(swapin[1]-swapin[0])/WAIT_TIME_IN_SECS;
111 swap_pageouts=(swapout[1]-swapout[0])/WAIT_TIME_IN_SECS;
112 #endif
113 #ifdef FREEBSD
114 #define SWAP_IN_NAME "vm.stats.vm.v_swappgsin"
115 #define SWAP_OUT_NAME "vm.stats.vm.v_swappgsout"
116 if (sysctlbyname(SWAP_IN_NAME, NULL, &size, NULL, NULL) < 0){
117 errf("sysctlbyname failed to get total memory (%m)");
118 return NULL;
119 }
120 if (sysctlbyname(SWAP_IN_NAME, &swapin[0], &size, NULL, NULL) < 0){
121 errf("Failed to get total memory stats (%m)");
122 return NULL;
123 }
124 if (sysctlbyname(SWAP_OUT_NAME, NULL, &size, NULL, NULL) < 0){
125 errf("sysctlbyname failed to get total memory (%m)");
126 return NULL;
127 }
128 if (sysctlbyname(SWAP_OUT_NAME, &swapout[0], &size, NULL, NULL) < 0){
129 errf("Failed to get total memory stats (%m)");
130 return NULL;
131 }
132
133 sleep(WAIT_TIME_IN_SECS);
134
135 if (sysctlbyname(SWAP_IN_NAME, NULL, &size, NULL, NULL) < 0){
136 errf("sysctlbyname failed to get total memory (%m)");
137 return NULL;
138 }
139 if (sysctlbyname(SWAP_IN_NAME, &swapin[1], &size, NULL, NULL) < 0){
140 errf("Failed to get total memory stats (%m)");
141 return NULL;
142 }
143 if (sysctlbyname(SWAP_OUT_NAME, NULL, &size, NULL, NULL) < 0){
144 errf("sysctlbyname failed to get total memory (%m)");
145 return NULL;
146 }
147 if (sysctlbyname(SWAP_OUT_NAME, &swapout[1], &size, NULL, NULL) < 0){
148 errf("Failed to get total memory stats (%m)");
149 return NULL;
150 }
151
152 swap_pageins=(swapin[1]-swapin[0])/WAIT_TIME_IN_SECS;
153 swap_pageouts=(swapout[1]-swapout[0])/WAIT_TIME_IN_SECS;
154
155 #endif
156
157
158 if((xml_page_stats=strf("<pages><swapins>%d</swapins><swapouts>%d</swapouts></pages>", swap_pageins, swap_pageouts)) == NULL){
159 errf("strf failed (%m)");
160 return NULL;
161 }
162 return xml_page_stats;
163 }
164
165 int main(){
166 printf("%s\n",get_page_stats());
167 exit(0);
168 }
169