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
45 #include <sys/types.h>
53 #define PROC_DIR_NAME "/proc"
54 #define CTX_DIR_NAME "/var/run/vservers/"
55 #define CTX_NAME_MAX_LEN 50
63 long start_time_oldest;
64 long stime_total, utime_total;
65 char name[CTX_NAME_MAX_LEN];
66 struct ctx_list *next;
71 long VmSize; // number of pages of virtual memory
72 long VmRSS; // resident set size from /proc/#/stat
73 long start_time; // start time of process -- seconds since 1-1-70
74 long stime, utime; // kernel & user-mode CPU time accumulated by process
75 long cstime, cutime; // cumulative time of process and reaped children
83 fprintf(stderr, "%s: from vserver kit version %s\n", process_name, VERSION);
84 fprintf(stderr, "(no argument needed)\n\n");
85 fprintf(stderr, "Show informations about all the active context.\n\n");
86 fprintf(stderr, " CTX# Context number\n");
87 fprintf(stderr, " #0 = root context\n");
88 fprintf(stderr, " #1 = monitoring context\n");
89 fprintf(stderr, " PROC QTY Quantity of processes in each context\n");
90 fprintf(stderr, " VSZ Number of pages of virtual memory\n");
91 fprintf(stderr, " RSS Resident set size\n");
92 fprintf(stderr, " userTIME User-mode CPU time accumulated\n");
93 fprintf(stderr, " sysTIME Kernel-mode CPU time accumulated\n");
94 fprintf(stderr, " UPTIME Uptime/context\n");
95 fprintf(stderr, " NAME Virtual server name\n");
96 fprintf(stderr, "\n");
100 // return uptime (in ms) from /proc/uptime
107 // open the /proc/uptime file
108 if ((fd = open("/proc/uptime", O_RDONLY, 0)) == -1)
111 if (read(fd, buffer, sizeof(buffer)) < 1)
116 if (sscanf(buffer, "%lf", &up) < 1)
118 fprintf(stderr, "%s: bad data in /proc/uptime\n", process_name);
125 // insert a new record to the list
126 struct ctx_list *insert_ctx(int ctx, struct ctx_list *next)
128 struct ctx_list *new;
130 new = (struct ctx_list *)malloc(sizeof(struct ctx_list));
132 new->process_count = 0;
133 new->VmSize_total = 0;
134 new->VmRSS_total = 0;
135 new->utime_total = 0;
136 new->stime_total = 0;
137 new->start_time_oldest = 0;
144 // find the ctx record with the ctx number
145 struct ctx_list *find_ctx(struct ctx_list *list, int ctx)
147 // very simple search engine...
151 if (list->ctx == ctx)
160 // compute the process info into the list
161 void add_ctx(struct ctx_list *list, struct process_info *process)
163 list->process_count ++;
164 list->VmSize_total += process->VmSize;
165 list->VmRSS_total += process->VmRSS;
166 list->utime_total += process->utime + process->cutime;
167 list->stime_total += process->stime + process->cstime;
169 if (list->start_time_oldest == 0) // first entry
170 list->start_time_oldest = process->start_time;
172 if (list->start_time_oldest > process->start_time)
173 list->start_time_oldest = process->start_time;
176 // increment the count number in the ctx record using ctx number
177 void count_ctx(struct ctx_list *list, struct process_info *process)
179 struct ctx_list *prev = list;
181 if (process == NULL) return;
187 if (list->ctx == process->s_context)
189 add_ctx(list, process);
193 if (list->ctx > process->s_context)
195 prev->next = insert_ctx(process->s_context, list);
196 add_ctx(prev->next, process);
204 prev->next = insert_ctx(process->s_context, NULL);
205 add_ctx(prev->next, process);
209 void free_ctx(struct ctx_list *list)
211 struct ctx_list *prev;
213 for(;list != NULL; list = prev)
221 Read the vserver description
223 static void read_description(
224 const char *name, // Vserver name
230 snprintf (conf,sizeof(conf)-1,"/etc/vservers/%s.conf",name);
231 fin = fopen (conf,"r");
234 while (fgets(line,sizeof(line)-1,fin)!=NULL){
237 while (isspace(*pt)) pt++;
238 if (strncmp(pt,"Description:",12)==0){
241 while (isspace(*pt)) pt++;
243 last = strlen(descrip)-1;
244 if (last >=0 && descrip[last] == '\n'){
245 descrip[last] = '\0';
254 // show the ctx_list with name from /var/run/servers/*.ctx
255 void show_ctx(struct ctx_list *list)
257 // fill the ctx_list using the /var/run/servers/*.ctx file(s)
258 __extension__ int bind_ctx_name(struct ctx_list *list)
260 // fetch the context number in /var/run/vservers/'filename'
261 int fetch_ctx_number(char *filename)
268 if ((fd = open(filename, O_RDONLY, 0)) == -1)
270 // put the file in a small buffer
271 if (read(fd, buf, sizeof(buf)) < 1)
276 sscanf(buf, "S_CONTEXT=%d", &ctx);
280 /* begin bind_ctx_name */
283 struct dirent *dir_entry;
285 char ctx_name[CTX_NAME_MAX_LEN];
286 struct ctx_list *ctx;
289 // open the /var/run/vservers directory
290 if ((ctx_dir = opendir(CTX_DIR_NAME)) == NULL)
292 fprintf(stderr, "%s: in openning %s: %s\n", process_name, CTX_DIR_NAME, strerror(errno));
297 while ((dir_entry = readdir(ctx_dir)) != NULL)
299 strncpy(ctx_name, dir_entry->d_name, sizeof(ctx_name));
300 p = strstr(ctx_name, ".ctx");
301 if (p != NULL) // make sure that it is a .ctx file..
303 *p = '\0'; // remove the .ctx in the file name
304 if ((ctx_number = fetch_ctx_number(dir_entry->d_name)) > 1)
306 if ((ctx = find_ctx(list, ctx_number)) != NULL)
307 strncpy(ctx->name, ctx_name, CTX_NAME_MAX_LEN);
310 // else fprintf(stderr, "invalid file %s in %s\n", dir_entry->d_name, CTX_DIR_NAME);
316 __extension__ char *convert_time(unsigned t, char *str)
318 unsigned hh, mm, ss, ms;
330 if (t > 0) // day > 0
332 snprintf(str, 25, "%3.ud%02uh%02u", t, (hh%12) ? hh%12 : 12, mm);
336 if (hh > 0) // hour > 0
337 snprintf(str, 25, " %2.uh%02um%02u", hh, mm, ss);
340 snprintf(str, 25, " %2.um%02u.%02u", mm, ss, ms);
346 __extension__ char *convert_mem(unsigned long total, char *str)
351 snprintf(str, 25, "%luB", total);
355 total >>= 10; // kByte
358 snprintf(str, 25, "%lukB", total);
362 total >>= 10; // MByte
365 snprintf(str, 25, "%luMB", total);
369 total >>= 10; // GByte
372 snprintf(str, 25, "%luGB", total);
375 total >>= 10; // TByte
376 snprintf(str, 25, "%luTB", total);
381 char utime[25], stime[25], ctx_uptime[25];
382 char vmsize[25], vmrss[25];
383 long uptime = get_uptime();
385 // now we have all the active context, fetch the name
386 // from /var/run/vservers/*.ctx
389 printf("CTX PROC VSZ RSS userTIME sysTIME UPTIME NAME DESCRIPTION\n");
394 strncpy(list->name, "monitoring server", CTX_NAME_MAX_LEN);
396 read_description (list->name,descrip);
398 printf("%-4d %4d %6s %6s %9s %9s %9s %-8s %s\n", list->ctx, list->process_count,
399 convert_mem(list->VmSize_total, vmsize), convert_mem(list->VmRSS_total, vmrss),
400 convert_time(list->utime_total, utime), convert_time(list->stime_total, stime), convert_time(uptime - list->start_time_oldest, ctx_uptime)
401 , list->name,descrip);
406 // open the process's status file to get the ctx number, and other stat
407 struct process_info *get_process_info(char *pid)
412 static struct process_info process;
414 // open the proc/#/status file
415 snprintf(buffer, sizeof(buffer), "/proc/%s/status", pid);
416 if ((fd = open(buffer, O_RDONLY, 0)) == -1)
418 // put the file in a buffer
419 if (read(fd, buffer, sizeof(buffer)) < 1)
424 // find the s_context entry
425 if ((p = strstr(buffer, "s_context:")) == NULL)
428 sscanf(p, "s_context: %d", &process.s_context);
430 // open the /proc/#/stat file
431 snprintf(buffer, sizeof(buffer), "/proc/%s/stat", pid);
432 if ((fd = open(buffer, O_RDONLY, 0)) == -1)
434 // put the file in a buffer
435 if (read(fd, buffer, sizeof(buffer)) < 1)
440 p = strchr(buffer, ')'); // go after the PID (process_name)
443 "%*s %*s %*s %*s %*s "
444 "%*s %*s %*s %*s %*s %ld %ld "
445 "%ld %ld %*s %*s %*s %*s "
447 "%ld ", &process.utime, &process.stime,
448 &process.cutime, &process.cstime,
450 &process.VmSize, &process.VmRSS);
455 int main(int argc, char **argv)
458 struct dirent *dir_entry;
462 process_name = argv[0];
470 // do not include own stat
473 // try to switch in context 1
474 if (vc_new_s_context(1,0, 0) < 0)
476 fprintf(stderr, "%s: unable to switch in context security #1\n", process_name);
480 // create the fist...
481 my_ctx_list = insert_ctx(0, NULL);
482 // init with the default name for the context 0
483 strncpy(my_ctx_list->name, "root server", CTX_NAME_MAX_LEN);
485 // open the /proc dir
486 if ((proc_dir = opendir(PROC_DIR_NAME)) == NULL)
488 fprintf(stderr, "%s: %s\n", process_name, strerror(errno));
492 chdir(PROC_DIR_NAME);
493 while ((dir_entry = readdir(proc_dir)) != NULL)
495 // select only process file
496 if (!isdigit(*dir_entry->d_name))
499 if (atoi(dir_entry->d_name) != my_pid)
500 count_ctx(my_ctx_list, get_process_info(dir_entry->d_name));
505 // output the ctx_list
506 show_ctx(my_ctx_list);
509 free_ctx(my_ctx_list);