22 |
|
#include "config.h" |
23 |
|
#endif |
24 |
|
|
25 |
– |
#include <stdio.h> |
26 |
– |
#include "ukcprog.h" |
25 |
|
#include "statgrab.h" |
26 |
|
#ifdef SOLARIS |
27 |
|
#include <sys/stat.h> |
28 |
|
#include <sys/swap.h> |
29 |
|
#include <unistd.h> |
30 |
|
#endif |
31 |
+ |
#ifdef LINUX |
32 |
+ |
#include <stdio.h> |
33 |
+ |
#include "tools.h" |
34 |
+ |
#endif |
35 |
+ |
#ifdef FREEBSD |
36 |
+ |
#include <unistd.h> |
37 |
+ |
#include <sys/types.h> |
38 |
+ |
#include <fcntl.h> |
39 |
+ |
#include <kvm.h> |
40 |
+ |
#endif |
41 |
|
|
42 |
|
swap_stat_t *get_swap_stats(){ |
43 |
|
|
44 |
|
static swap_stat_t swap_stat; |
45 |
|
|
46 |
+ |
#ifdef SOLARIS |
47 |
|
struct anoninfo ai; |
48 |
|
int pagesize; |
49 |
+ |
#endif |
50 |
+ |
#ifdef LINUX |
51 |
+ |
FILE *f; |
52 |
+ |
char *line_ptr; |
53 |
+ |
#endif |
54 |
+ |
#ifdef FREEBSD |
55 |
+ |
struct kvm_swap swapinfo; |
56 |
+ |
int pagesize; |
57 |
+ |
kvm_t *kvmd = NULL; |
58 |
+ |
#endif |
59 |
|
|
60 |
+ |
#ifdef SOLARIS |
61 |
|
if((pagesize=sysconf(_SC_PAGESIZE)) == -1){ |
62 |
|
return NULL; |
63 |
|
} |
67 |
|
swap_stat.total = (long long)ai.ani_max * (long long)pagesize; |
68 |
|
swap_stat.used = (long long)ai.ani_resv * (long long)pagesize; |
69 |
|
swap_stat.free = swap_stat.total - swap_stat.used; |
70 |
+ |
#endif |
71 |
+ |
#ifdef LINUX |
72 |
+ |
if ((f=fopen("/proc/meminfo", "r" ))==NULL) { |
73 |
+ |
return NULL; |
74 |
+ |
} |
75 |
+ |
if((line_ptr=f_read_line(f, "Swap:"))==NULL){ |
76 |
+ |
fclose(f); |
77 |
+ |
return NULL; |
78 |
+ |
} |
79 |
+ |
if((sscanf(line_ptr, "Swap: %lld %lld %lld", &swap_stat.total, &swap_stat.used, &swap_stat.free))!=3){ |
80 |
+ |
fclose(f); |
81 |
+ |
return NULL; |
82 |
+ |
} |
83 |
+ |
fclose(f); |
84 |
+ |
#endif |
85 |
+ |
#ifdef FREEBSD |
86 |
+ |
if((kvmd = kvm_openfiles(NULL, NULL, NULL, O_RDONLY, NULL)) == NULL){ |
87 |
+ |
return NULL; |
88 |
+ |
} |
89 |
+ |
if ((kvm_getswapinfo(kvmd, &swapinfo, 1,0)) == -1){ |
90 |
+ |
return NULL; |
91 |
+ |
} |
92 |
+ |
pagesize=getpagesize(); |
93 |
|
|
94 |
+ |
swap_stat.total= (long long)swapinfo.ksw_total * (long long)pagesize; |
95 |
+ |
swap_stat.used = (long long)swapinfo.ksw_used * (long long)pagesize; |
96 |
+ |
swap_stat.free = swap_stat.total-swap_stat.used; |
97 |
+ |
|
98 |
+ |
kvm_close(kvmd); |
99 |
+ |
#endif |
100 |
|
return &swap_stat; |
101 |
|
|
102 |
|
} |