1 // $Id: vserver-stat.c,v 1.1.4.1 2003/10/14 00:45:04 ensc Exp $
3 // Copyright (C) 2003 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
4 // based on vserver-stat.cc by Guillaum Dallaire and Jacques Gelinas
6 // This program is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation; either version 2, or (at your option)
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 vserver-stat help you to see all the active context currently in the kernel
26 2003-01-08 Jacques Gelinas: Shows vserver description
27 2002-02-28 Jacques Gelinas: Use dynamic system call
28 2002-06-05 Martial Rioux : fix memory output error
29 2002-12-05 Martial Rioux : fix output glitch
30 2001-11-29 added uptime/ctx stat
31 2001-11-20 added vmsize, rss, stime and utime stat
46 #include <sys/types.h>
54 #define PROC_DIR_NAME "/proc"
55 #define CTX_DIR_NAME "/var/run/vservers/"
56 #define CTX_NAME_MAX_LEN 50
64 long start_time_oldest;
65 long stime_total, utime_total;
66 char name[CTX_NAME_MAX_LEN];
67 struct ctx_list *next;
72 long VmSize; // number of pages of virtual memory
73 long VmRSS; // resident set size from /proc/#/stat
74 long start_time; // start time of process -- seconds since 1-1-70
75 long stime, utime; // kernel & user-mode CPU time accumulated by process
76 long cstime, cutime; // cumulative time of process and reaped children
84 fprintf(stderr, "%s: from vserver kit version %s\n", process_name, VERSION);
85 fprintf(stderr, "(no argument needed)\n\n");
86 fprintf(stderr, "Show informations about all the active context.\n\n");
87 fprintf(stderr, " CTX# Context number\n");
88 fprintf(stderr, " #0 = root context\n");
89 fprintf(stderr, " #1 = monitoring context\n");
90 fprintf(stderr, " PROC QTY Quantity of processes in each context\n");
91 fprintf(stderr, " VSZ Number of pages of virtual memory\n");
92 fprintf(stderr, " RSS Resident set size\n");
93 fprintf(stderr, " userTIME User-mode CPU time accumulated\n");
94 fprintf(stderr, " sysTIME Kernel-mode CPU time accumulated\n");
95 fprintf(stderr, " UPTIME Uptime/context\n");
96 fprintf(stderr, " NAME Virtual server name\n");
97 fprintf(stderr, "\n");
101 // return uptime (in ms) from /proc/uptime
108 // open the /proc/uptime file
109 if ((fd = open("/proc/uptime", O_RDONLY, 0)) == -1)
112 if (read(fd, buffer, sizeof(buffer)) < 1)
117 if (sscanf(buffer, "%lf", &up) < 1)
119 fprintf(stderr, "%s: bad data in /proc/uptime\n", process_name);
126 // insert a new record to the list
127 struct ctx_list *insert_ctx(int ctx, struct ctx_list *next)
129 struct ctx_list *new;
131 new = (struct ctx_list *)malloc(sizeof(struct ctx_list));
133 new->process_count = 0;
134 new->VmSize_total = 0;
135 new->VmRSS_total = 0;
136 new->utime_total = 0;
137 new->stime_total = 0;
138 new->start_time_oldest = 0;
145 // find the ctx record with the ctx number
146 struct ctx_list *find_ctx(struct ctx_list *list, int ctx)
148 // very simple search engine...
152 if (list->ctx == ctx)
161 // compute the process info into the list
162 void add_ctx(struct ctx_list *list, struct process_info *process)
164 list->process_count ++;
165 list->VmSize_total += process->VmSize;
166 list->VmRSS_total += process->VmRSS;
167 list->utime_total += process->utime + process->cutime;
168 list->stime_total += process->stime + process->cstime;
170 if (list->start_time_oldest == 0) // first entry
171 list->start_time_oldest = process->start_time;
173 if (list->start_time_oldest > process->start_time)
174 list->start_time_oldest = process->start_time;
177 // increment the count number in the ctx record using ctx number
178 void count_ctx(struct ctx_list *list, struct process_info *process)
180 struct ctx_list *prev = list;
182 if (process == NULL) return;
188 if (list->ctx == process->s_context)
190 add_ctx(list, process);
194 if (list->ctx > process->s_context)
196 prev->next = insert_ctx(process->s_context, list);
197 add_ctx(prev->next, process);
205 prev->next = insert_ctx(process->s_context, NULL);
206 add_ctx(prev->next, process);
210 void free_ctx(struct ctx_list *list)
212 struct ctx_list *prev;
214 for(;list != NULL; list = prev)
222 Read the vserver description
224 static void read_description(
225 const char *name, // Vserver name
231 snprintf (conf,sizeof(conf)-1,"/etc/vservers/%s.conf",name);
232 fin = fopen (conf,"r");
235 while (fgets(line,sizeof(line)-1,fin)!=NULL){
238 while (isspace(*pt)) pt++;
239 if (strncmp(pt,"Description:",12)==0){
242 while (isspace(*pt)) pt++;
244 last = strlen(descrip)-1;
245 if (last >=0 && descrip[last] == '\n'){
246 descrip[last] = '\0';
255 // show the ctx_list with name from /var/run/servers/*.ctx
256 void show_ctx(struct ctx_list *list)
258 // fill the ctx_list using the /var/run/servers/*.ctx file(s)
259 __extension__ int bind_ctx_name(struct ctx_list *list)
261 // fetch the context number in /var/run/vservers/'filename'
262 int fetch_ctx_number(char *filename)
269 if ((fd = open(filename, O_RDONLY, 0)) == -1)
271 // put the file in a small buffer
272 if (read(fd, buf, sizeof(buf)) < 1)
277 sscanf(buf, "S_CONTEXT=%d", &ctx);
281 /* begin bind_ctx_name */
284 struct dirent *dir_entry;
286 char ctx_name[CTX_NAME_MAX_LEN];
287 struct ctx_list *ctx;
290 // open the /var/run/vservers directory
291 if ((ctx_dir = opendir(CTX_DIR_NAME)) == NULL)
293 fprintf(stderr, "%s: in openning %s: %s\n", process_name, CTX_DIR_NAME, strerror(errno));
298 while ((dir_entry = readdir(ctx_dir)) != NULL)
300 strncpy(ctx_name, dir_entry->d_name, sizeof(ctx_name));
301 p = strstr(ctx_name, ".ctx");
302 if (p != NULL) // make sure that it is a .ctx file..
304 *p = '\0'; // remove the .ctx in the file name
305 if ((ctx_number = fetch_ctx_number(dir_entry->d_name)) > 1)
307 if ((ctx = find_ctx(list, ctx_number)) != NULL)
308 strncpy(ctx->name, ctx_name, CTX_NAME_MAX_LEN);
311 // else fprintf(stderr, "invalid file %s in %s\n", dir_entry->d_name, CTX_DIR_NAME);
317 __extension__ char *convert_time(unsigned t, char *str)
319 unsigned hh, mm, ss, ms;
331 if (t > 0) // day > 0
333 snprintf(str, 25, "%3.ud%02uh%02u", t, (hh%12) ? hh%12 : 12, mm);
337 if (hh > 0) // hour > 0
338 snprintf(str, 25, " %2.uh%02um%02u", hh, mm, ss);
341 snprintf(str, 25, " %2.um%02u.%02u", mm, ss, ms);
347 __extension__ char *convert_mem(unsigned long total, char *str)
352 snprintf(str, 25, "%luB", total);
356 total >>= 10; // kByte
359 snprintf(str, 25, "%lukB", total);
363 total >>= 10; // MByte
366 snprintf(str, 25, "%luMB", total);
370 total >>= 10; // GByte
373 snprintf(str, 25, "%luGB", total);
376 total >>= 10; // TByte
377 snprintf(str, 25, "%luTB", total);
382 char utime[25], stime[25], ctx_uptime[25];
383 char vmsize[25], vmrss[25];
384 long uptime = get_uptime();
386 // now we have all the active context, fetch the name
387 // from /var/run/vservers/*.ctx
390 printf("CTX PROC VSZ RSS userTIME sysTIME UPTIME NAME DESCRIPTION\n");
395 strncpy(list->name, "monitoring server", CTX_NAME_MAX_LEN);
397 read_description (list->name,descrip);
399 printf("%-4d %4d %6s %6s %9s %9s %9s %-8s %s\n", list->ctx, list->process_count,
400 convert_mem(list->VmSize_total, vmsize), convert_mem(list->VmRSS_total, vmrss),
401 convert_time(list->utime_total, utime), convert_time(list->stime_total, stime), convert_time(uptime - list->start_time_oldest, ctx_uptime)
402 , list->name,descrip);
407 // open the process's status file to get the ctx number, and other stat
408 struct process_info *get_process_info(char *pid)
413 static struct process_info process;
415 // open the proc/#/status file
416 snprintf(buffer, sizeof(buffer), "/proc/%s/status", pid);
417 if ((fd = open(buffer, O_RDONLY, 0)) == -1)
419 // put the file in a buffer
420 if (read(fd, buffer, sizeof(buffer)) < 1)
425 // find the s_context entry
426 if ((p = strstr(buffer, "s_context:")) == NULL)
429 sscanf(p, "s_context: %d", &process.s_context);
431 // open the /proc/#/stat file
432 snprintf(buffer, sizeof(buffer), "/proc/%s/stat", pid);
433 if ((fd = open(buffer, O_RDONLY, 0)) == -1)
435 // put the file in a buffer
436 if (read(fd, buffer, sizeof(buffer)) < 1)
441 p = strchr(buffer, ')'); // go after the PID (process_name)
444 "%*s %*s %*s %*s %*s "
445 "%*s %*s %*s %*s %*s %ld %ld "
446 "%ld %ld %*s %*s %*s %*s "
448 "%ld ", &process.utime, &process.stime,
449 &process.cutime, &process.cstime,
451 &process.VmSize, &process.VmRSS);
456 int main(int argc, char **argv)
459 struct dirent *dir_entry;
463 process_name = argv[0];
471 // do not include own stat
474 // try to switch in context 1
475 if (vc_new_s_context(1,0, 0) < 0)
477 fprintf(stderr, "%s: unable to switch in context security #1\n", process_name);
481 // create the fist...
482 my_ctx_list = insert_ctx(0, NULL);
483 // init with the default name for the context 0
484 strncpy(my_ctx_list->name, "root server", CTX_NAME_MAX_LEN);
486 // open the /proc dir
487 if ((proc_dir = opendir(PROC_DIR_NAME)) == NULL)
489 fprintf(stderr, "%s: %s\n", process_name, strerror(errno));
493 chdir(PROC_DIR_NAME);
494 while ((dir_entry = readdir(proc_dir)) != NULL)
496 // select only process file
497 if (!isdigit(*dir_entry->d_name))
500 if (atoi(dir_entry->d_name) != my_pid)
501 count_ctx(my_ctx_list, get_process_info(dir_entry->d_name));
506 // output the ctx_list
507 show_ctx(my_ctx_list);
510 free_ctx(my_ctx_list);