hce-node application  1.4.3
HCE Hierarchical Cluster Engine node application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
ProcFileSystem.cpp
Go to the documentation of this file.
1 #include <ProcFileSystem.h>
2 
3 char *ProcFileSystem::file_get_contents(const char *filename, size_t & size)
4 {
5  FILE *file;
6  char *data = NULL;
7  size_t data_size = 1024;
8  size = 0;
9  if ((file = fopen(filename, "rb")) != NULL)
10  {
11  data = (char *) malloc(data_size);
12  if(!data)
13  {
14  return NULL;
15  }
16  while (!feof(file))
17  {
18  size += fread(data + size, 1, 512, file);
19  if (size + 512 > data_size)
20  {
21  data_size += 512;
22  data = (char *) realloc(data, data_size);
23  if(!data)
24  {
25  fclose(file);
26  return NULL;
27  }
28  }
29  }
30  fclose(file);
31  data[size] = '\0';
32  }
33  return data;
34 }
35 
37 {
38  size_t size;
39  return file_get_contents("/proc/loadavg", size);
40 }
41 
42 unsigned long long ProcFileSystem::getTotalMem()
43 {
44  size_t size = 0;
45  unsigned long long total_mem = (unsigned long long)(-1);
46  char *data = NULL;
47  int pos;
48  data = file_get_contents("/proc/meminfo", size);
49  if (!data)
50  return (unsigned long long)(-1);
51  pos = fast_str_pos(data, "MemTotal:");
52  if (pos >= 0)
53  {
54  pos += 9;
55  for (; pos < (int) size && (data[pos] == ' ' || data[pos] == '\t'); pos++); //skip spaces
56  int numeric_start = pos;
57  for (; data[pos] >= '0' && data[pos] <= '9' && pos < (int) size; pos++);
58  data[pos] = '\0';
59  total_mem = atoll(data + numeric_start);
60  }
61  free(data);
62  return total_mem;
63 }
64 
65 unsigned long long ProcFileSystem::getVmSize()
66 {
67  size_t size = 0;
68  unsigned long long total_mem = (unsigned long long)(-1);
69  char *data = NULL;
70  int pos;
71  data = file_get_contents("/proc/self/status", size);
72  if (!data)
73  return (unsigned long long)(-1);
74  pos = fast_str_pos(data, "VmSize:");
75  if (pos >= 0)
76  {
77  pos += 7;
78  for (; pos < (int) size && (data[pos] == ' ' || data[pos] == '\t'); pos++); //skip spaces
79  int numeric_start = pos;
80  for (; data[pos] >= '0' && data[pos] <= '9' && pos < (int) size; pos++);
81  data[pos] = '\0';
82  total_mem = atoll(data + numeric_start);
83  }
84  free(data);
85  return total_mem;
86 }
87 
88 unsigned long long ProcFileSystem::getFreeMem()
89 {
90  size_t size = 0;
91  unsigned long long total_mem = (unsigned long long)(-1);
92  char *data = NULL;
93  int pos;
94  data = file_get_contents("/proc/meminfo", size);
95  if (!data)
96  return (unsigned long long)(-1);
97  pos = fast_str_pos(data, "MemFree:");
98  if (pos >= 0)
99  {
100  pos += 9;
101  for (; pos < (int) size && (data[pos] == ' ' || data[pos] == '\t'); pos++); //skip spaces
102  int numeric_start = pos;
103  for (; data[pos] >= '0' && data[pos] <= '9' && pos < (int) size; pos++);
104  total_mem = atoll(data + numeric_start);
105  }
106  pos = fast_str_pos(data, "Cached:");
107  if (pos >= 0)
108  {
109  pos += 8;
110  for (; pos < (int) size && (data[pos] == ' ' || data[pos] == '\t'); pos++); //skip spaces
111  int numeric_start = pos;
112  for (; data[pos] >= '0' && data[pos] <= '9' && pos < (int) size; pos++);
113  data[pos] = '\0';
114  total_mem += atoll(data + numeric_start);
115  }
116  pos = fast_str_pos(data, "Buffers:");
117  if (pos >= 0)
118  {
119  pos += 9;
120  for (; pos < (int) size && (data[pos] == ' ' || data[pos] == '\t'); pos++); //skip spaces
121  int numeric_start = pos;
122  for (; data[pos] >= '0' && data[pos] <= '9' && pos < (int) size; pos++);
123  data[pos] = '\0';
124  total_mem += atoll(data + numeric_start);
125  }
126 
127  free(data);
128  return total_mem;
129 }
130 
132 {
133  size_t size = 0;
134  char *data = file_get_contents("/proc/self/stat", size);
135  if (!data)
136  return (unsigned long)(-1);
137  int pid = 0;
138  char com[256];
139  char state;
140  int ppid, pgrp, session, tty_nr, tpgid;
141  unsigned int flags;
142  unsigned long minflt, cminflt, majflt, cmajflt, utime, stime;
143  sscanf(data, "%d %s %c %d %d %d %d %d %u %lu %lu %lu %lu %lu %lu", &pid, com, &state, &ppid, &pgrp, &session, &tty_nr, &tpgid, &flags, &minflt, &cminflt, &majflt, &cmajflt, &utime, &stime);
144  free(data);
145  return utime;
146 }
147 
149 {
150  size_t size = 0;
151  char *data = file_get_contents("/proc/self/stat", size);
152  if (!data)
153  return (unsigned long)(-1);
154  int pid = 0;
155  char com[256];
156  char state;
157  int ppid, pgrp, session, tty_nr, tpgid;
158  unsigned int flags;
159  unsigned long minflt, cminflt, majflt, cmajflt, utime, stime;
160  sscanf(data, "%d %s %c %d %d %d %d %d %u %lu %lu %lu %lu %lu %lu", &pid, com, &state, &ppid, &pgrp, &session, &tty_nr, &tpgid, &flags, &minflt, &cminflt, &majflt, &cmajflt, &utime, &stime);
161  free(data);
162  return stime;
163 }
164 
165 unsigned long long ProcFileSystem::getRunTime()
166 {
167  char *data = NULL;
168  long long uptime = 0;
169  size_t size = 0;
170  size_t pos = 0;
171  data = file_get_contents("/proc/uptime", size);
172  if (!data)
173  return 0;
174  for (; data[pos] >= '0' && data[pos] <= '9' && pos < size; pos++);
175  data[pos] = '\0';
176  uptime = atoll(data);
177  free(data);
178  return uptime;
179 }
180 
182 {
183  size_t size;
184  return file_get_contents("/proc/sys/kernel/hostname", size);
185 }
186 
187 int ProcFileSystem::setHostName(char *hostname)
188 {
189  FILE *file;
190  size_t size = 0;
191  file = fopen("/proc/sys/kernel/hostname", "wb");
192  if (file)
193  {
194  size = fwrite(hostname, 1, strlen(hostname), file);
195  fclose(file);
196  }
197  return size;
198 }
199 
200 unsigned long long ProcFileSystem::getShmAll()
201 {
202  char *data;
203  size_t size;
204  long long shmall = 0;
205  data = file_get_contents("/proc/sys/kernel/shmall", size);
206  if (data)
207  {
208  shmall = atoll(data);
209  free(data);
210  }
211  return shmall;
212 }
213 
214 int ProcFileSystem::setShmAll(unsigned long long shmall)
215 {
216  FILE *file;
217  char body[20];
218  size_t size = 0;
219  file = fopen("/proc/sys/kernel/shmall", "wb");
220  if (file)
221  {
222  sprintf(body, "%llu", shmall);
223  size = fwrite(body, 1, strlen(body), file);
224  fclose(file);
225  }
226  return size;
227 }
228 
229 unsigned long long ProcFileSystem::getShmMax()
230 {
231  char *data;
232  size_t size;
233  long long shmall = 0;
234  data = file_get_contents("/proc/sys/kernel/shmmax", size);
235  if (data)
236  {
237  shmall = atoll(data);
238  free(data);
239  }
240  return shmall;
241 }
242 
243 int ProcFileSystem::setShmMax(unsigned long long shmmax)
244 {
245  FILE *file;
246  char body[20];
247  size_t size = 0;
248  file = fopen("/proc/sys/kernel/shmmax", "wb");
249  if (file)
250  {
251  sprintf(body, "%llu", shmmax);
252  size = fwrite(body, 1, strlen(body), file);
253  fclose(file);
254  }
255  return size;
256 }
257 
258 int ProcFileSystem::fast_str_pos(const char *str, const char *pattern)
259 {
260  int plen;
261  int d[256];
262  register int c;
263  int slen = strlen(str);
264  register int pindx;
265  register int cmppos;
266  register int endpos;
267  plen = strlen(pattern);
268  for (c = 0; c < 256; c++)
269  d[c] = plen;
270  for (c = 0; c < plen - 1; c++)
271  d[pattern[c] & 0377] = plen - 1 - c;
272  for (endpos = plen - 1; endpos < slen;)
273  {
274  for (cmppos = endpos, pindx = (plen - 1); pindx >= 0; cmppos--, pindx--)
275  if (str[cmppos] != pattern[pindx])
276  {
277  endpos += d[str[endpos] & 0377];
278  break;
279  }
280  if (pindx < 0)
281  return endpos - (plen - 1);
282  }
283  return -1;
284 }
285 
286 unsigned long long ProcFileSystem::getVmRSS()
287 {
288  size_t size = 0;
289  unsigned long long total_mem = (unsigned long long)(-1);
290  char *data = NULL;
291  int pos;
292  data = file_get_contents("/proc/self/status", size);
293  if (!data)
294  return (unsigned long long)(-1);
295  pos = fast_str_pos(data, "VmRSS:");
296  if (pos >= 0)
297  {
298  pos += 6;
299  for (; pos < (int) size && (data[pos] == ' ' || data[pos] == '\t'); pos++); //skip spaces
300  int numeric_start = pos;
301  for (; data[pos] >= '0' && data[pos] <= '9' && pos < (int) size; pos++);
302  data[pos] = '\0';
303  total_mem = atoll(data + numeric_start);
304  }
305  free(data);
306  return total_mem;
307 }
308 
309 unsigned long long ProcFileSystem::getFreeMemSelf()
310 {
311  unsigned long long total = getTotalMem();
312  unsigned long long vm = getVmSize();
313  if(total > vm) return total - vm;
314  return 0;
315 }