-
Notifications
You must be signed in to change notification settings - Fork 9
/
vzfree.c
180 lines (163 loc) · 5.06 KB
/
vzfree.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
struct resource_t {
char name[32];
long long held;
long long maxheld;
long long barrier;
long long limit;
long long failcnt;
struct resource_t *next;
};
struct resource_t *
get_resource(char* buffer) {
struct resource_t *resource;
char *buffer2 = buffer;
char buffer3[80];
int state = 0;
int count = 0;
resource = malloc(sizeof(struct resource_t));
resource->next = NULL;
while (1) {
switch (state) {
case 0:
if (*buffer2 == ' ') {
strncpy(resource->name, buffer, (int) (buffer2 - buffer));
resource->name[(int) (buffer2 - buffer)] = '\0';
state = 1;
}
break;
case 1:
if (*buffer2 != ' ') {
buffer = buffer2;
state = 2;
}
break;
case 2:
if (*buffer2 == ' ' || !*buffer2) {
if (count < 5) {
sscanf(buffer, "%lld", (&(resource->held) + count));
count ++;
}
state = 1;
}
break;
}
if (!*buffer2) {
break;
}
buffer2 ++;
}
if (strcmp(resource->name, "dummy") == 0) {
free(resource);
return NULL;
} else if (count == 5) {
return resource;
} else {
free(resource);
return NULL;
}
}
struct resource_t*
find_resource(struct resource_t* resource, char* name) {
while (resource) {
if (strcmp(name, resource->name) == 0) {
return resource;
} else {
resource = resource->next;
}
}
return resource;
}
void
print_memory(struct resource_t *resource) {
struct resource_t *kmemsize, *privvmpages, *physpages,
*oomguarpages, *vmguarpages;
float committed_total;
float committed_used;
float pagesize; /* pagesize in mega bytes */
kmemsize = find_resource(resource, "kmemsize");
privvmpages = find_resource(resource, "privvmpages");
physpages = find_resource(resource, "physpages");
oomguarpages = find_resource(resource, "oomguarpages");
vmguarpages = find_resource(resource, "vmguarpages");
pagesize = (float) sysconf(_SC_PAGESIZE) / 1048576.0;
committed_total = oomguarpages->barrier * pagesize;
committed_used = (oomguarpages->held * pagesize) + (kmemsize->held / 1048576.0);
printf(" Total Used Free\n");
printf("Kernel: %9.2fM %9.2fM %9.2fM\n",
kmemsize->barrier / 1048576.0,
kmemsize->held / 1048576.0,
(kmemsize->barrier - kmemsize->held) / 1048576.0);
printf("Allocate: %9.2fM %9.2fM %9.2fM (%lldM Guaranteed)\n",
privvmpages->barrier * pagesize,
privvmpages->held * pagesize,
(privvmpages->barrier - privvmpages->held) * pagesize,
(long long int) (vmguarpages->barrier * pagesize));
printf("Commit: %9.2fM %9.2fM %9.2fM (%.1f%% of Allocated)\n",
committed_total,
committed_used,
committed_total - committed_used,
oomguarpages->held * 100.0 / privvmpages->held);
printf("Swap: %9.2fM (%.1f%% of Committed)\n",
(oomguarpages->held - physpages->held) * pagesize,
(oomguarpages->held - physpages->held) /
(float) (oomguarpages->held) * 100.0);
}
struct resource_t *
get_meminfo(char* beanfile) {
FILE *fp;
char buffer[256];
char* tmpchar;
int state = 0, offset = 0;
struct resource_t *resource = NULL, *resource2, *resource3;
if (!( fp = fopen(beanfile, "r"))) {
return 0;
}
while (fgets(buffer, 256, fp)) {
switch (state) {
case 0:
if (strncasecmp(buffer, "Version:", 8) == 0) {
state = 1;
}
break;
case 1:
tmpchar = strstr(buffer, "resource");
if (tmpchar) {
offset = (int) (tmpchar - buffer);
state = 2;
}
break;
case 2:
if (resource2 = get_resource((char*) (buffer + offset))) {
if (resource) {
resource3->next = resource2;
resource3 = resource2;
} else {
resource = resource3 = resource2;
}
}
break;
}
}
return resource;
}
int
main(int argc, char **argv) {
struct resource_t* resource;
char* beanfile;
if (argc > 1) {
beanfile = argv[1];
} else {
beanfile = "/proc/user_beancounters";
}
if (resource = get_meminfo(beanfile)) {
print_memory(resource);
return 0;
} else {
fprintf(stderr, "Unable to read %s\n", beanfile);
return -1;
}
}