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
41 #include "wrappers-dirent.h"
42 #include "wrappers-vserver.h"
49 #include <sys/types.h>
58 #define PROC_DIR_NAME "/proc"
59 #define CTX_DIR_NAME "/var/run/vservers/"
60 #define CTX_NAME_MAX_LEN 50
62 int wrapper_exit_code = 1;
70 long start_time_oldest;
71 long stime_total, utime_total;
72 char name[CTX_NAME_MAX_LEN];
73 struct ctx_list *next;
78 long VmSize; // number of pages of virtual memory
79 long VmRSS; // resident set size from /proc/#/stat
80 long start_time; // start time of process -- seconds since 1-1-70
81 long stime, utime; // kernel & user-mode CPU time accumulated by process
82 long cstime, cutime; // cumulative time of process and reaped children
89 enum { tpUNSET, tpCTX, tpPID } type;
92 unsigned int interval;
100 showHelp(int fd, char const *cmd, int res)
102 WRITE_MSG(fd, "Usage: ");
106 "Show informations about all the active context.\n\n"
107 " CTX# Context number\n"
108 " #0 = root context\n"
109 " #1 = monitoring context\n"
110 " PROC QTY Quantity of processes in each context\n"
111 " VSZ Number of pages of virtual memory\n"
112 " RSS Resident set size\n"
113 " userTIME User-mode CPU time accumulated\n"
114 " sysTIME Kernel-mode CPU time accumulated\n"
115 " UPTIME Uptime/context\n"
116 " NAME Virtual server name\n"
125 "vserver-stat " VERSION " -- show virtual context statistics\n"
126 "This program is part of " PACKAGE_STRING "\n\n"
127 "Copyright (C) 2003 Enrico Scholz\n"
128 VERSION_COPYRIGHT_DISCLAIMER);
133 // return uptime (in ms) from /proc/uptime
144 // open the /proc/uptime file
145 fd = Eopen("/proc/uptime", O_RDONLY, 0);
146 len = Eread(fd, buffer, sizeof buffer);
148 if (len==sizeof(buffer)) {
149 WRITE_MSG(2, "Too much data in /proc/uptime; aborting...\n");
154 while (len>0 && buffer[len-1]=='\n') --len;
157 secs = strtol(buffer, &errptr, 10);
158 if (*errptr!='.') errptr = buffer;
159 else msecs = strtol(errptr+1, &errptr, 10);
161 if ((*errptr!='\0' && *errptr!=' ') || errptr==buffer) {
162 WRITE_MSG(2, "Bad data in /proc/uptime\n");
166 return secs*100 + msecs;
169 // insert a new record to the list
170 static struct ctx_list *
171 insert_ctx(int ctx, struct ctx_list *next)
173 struct ctx_list *new;
175 new = (struct ctx_list *)malloc(sizeof(struct ctx_list));
177 new->process_count = 0;
178 new->VmSize_total = 0;
179 new->VmRSS_total = 0;
180 new->utime_total = 0;
181 new->stime_total = 0;
182 new->start_time_oldest = 0;
189 // compute the process info into the list
191 add_ctx(struct ctx_list *list, struct process_info *process)
193 list->process_count ++;
194 list->VmSize_total += process->VmSize;
195 list->VmRSS_total += process->VmRSS;
196 list->utime_total += process->utime + process->cutime;
197 list->stime_total += process->stime + process->cstime;
199 if (list->start_time_oldest == 0) // first entry
200 list->start_time_oldest = process->start_time;
202 if (list->start_time_oldest > process->start_time)
203 list->start_time_oldest = process->start_time;
206 // increment the count number in the ctx record using ctx number
208 count_ctx(struct ctx_list **ptr, struct process_info *process)
211 if (*ptr==0 || (*ptr)->ctx > process->s_context) {
212 *ptr = insert_ctx(process->s_context, *ptr);
216 if ((*ptr)->ctx == process->s_context)
222 add_ctx(*ptr, process);
227 free_ctx(struct ctx_list *list)
229 struct ctx_list *prev;
231 for(;list != NULL; list = prev)
239 // open the process's status file to get the ctx number, and other stat
240 struct process_info *get_process_info(char *pid)
245 size_t idx, l=strlen(pid);
246 static struct process_info process;
248 process.s_context = vc_get_task_xid(atoi(pid));
250 if (process.s_context==VC_NOCTX) {
252 WRITE_MSG(2, "vc_get_task_xid(");
255 WRITE_STR(2, strerror(err));
259 memcpy(buffer, "/proc/", 6); idx = 6;
260 memcpy(buffer+idx, pid, l); idx += l;
261 memcpy(buffer+idx, "/stat", 6);
263 // open the /proc/#/stat file
264 if ((fd = open(buffer, O_RDONLY, 0)) == -1)
266 // put the file in a buffer
267 if (read(fd, buffer, sizeof(buffer)) < 1)
272 p = strchr(buffer, ')'); // go after the PID (process_name)
273 for (idx = 0; idx<12 && *p!='\0'; ++p)
274 if ((*p)==' ') ++idx;
276 process.utime = strtol(p, &p, 10);
277 process.stime = strtol(p+1, &p, 10);
278 process.cutime = strtol(p+1, &p, 10);
279 process.cstime = strtol(p+1, &p, 10);
281 for (idx = 0; idx<5 && *p!='\0'; ++p)
282 if ((*p)==' ') ++idx;
284 process.start_time = strtol(p, &p, 10);
285 process.VmSize = strtol(p+1, &p, 10);
286 process.VmRSS = strtol(p+1, &p, 10);
294 fillUintZero(char *buf, unsigned long val, size_t cnt)
298 l = utilvserver_fmt_ulong(buf, val);
300 memmove(buf+cnt-l, buf, l);
301 memset(buf, '0', cnt-l);
309 shortenMem(char *buf, unsigned long val)
311 char const * SUFFIXES[] = { " ", "K", "M", "G", "T", "+" };
313 char const * suffix = "+";
315 unsigned int mod = 0;
317 for (i=0; i<6; ++i) {
319 suffix = SUFFIXES[i];
322 mod = 10*(val & 1023)/1024;
326 if (val >9999) val=9999;
327 if (val>=1000) mod=0;
329 l = utilvserver_fmt_ulong(tmp, val);
332 l += utilvserver_fmt_ulong(tmp+l, mod);
334 i = 7-l-strlen(suffix);
336 memcpy(buf+i, tmp, l);
337 memcpy(buf+i+l, suffix, strlen(suffix));
341 shortenTime(char *buf, uint64_t t)
346 unsigned long hh, mm, ss, ms;
359 ptr += utilvserver_fmt_ulong(ptr, t);
361 ptr += fillUintZero(ptr, hh, 2);
363 ptr += fillUintZero(ptr, mm, 2);
366 ptr += utilvserver_fmt_ulong(ptr, hh);
368 ptr += fillUintZero(ptr, mm, 2);
370 ptr += fillUintZero(ptr, ss, 2);
373 ptr += utilvserver_fmt_ulong(ptr, mm);
375 ptr += fillUintZero(ptr, ss, 2);
377 ptr += fillUintZero(ptr, ms, 2);
381 memcpy(buf+10-(ptr-tmp), tmp, ptr-tmp);
384 void showContexts(struct ctx_list *list)
386 uint64_t uptime = getUptime();
388 WRITE_MSG(1, "CTX PROC VSZ RSS userTIME sysTIME UPTIME NAME\n");
390 char buf[sizeof(xid_t)*3 + 512];
391 char tmp[sizeof(int)*3 + 2];
394 memset(buf, ' ', sizeof(buf));
395 l = utilvserver_fmt_long(buf, list->ctx);
396 l = utilvserver_fmt_long(tmp, list->process_count);
397 memcpy(buf+10-l, tmp, l);
399 shortenMem (buf+10, list->VmSize_total);
400 shortenMem (buf+17, list->VmRSS_total);
401 shortenTime(buf+24, list->utime_total);
402 shortenTime(buf+34, list->stime_total);
403 shortenTime(buf+44, uptime - list->start_time_oldest);
406 case 0 : strncpy(buf+55, "root server", 20); break;
407 case 1 : strncpy(buf+55, "monitoring server", 20); break;
411 vcCfgStyle cfgstyle = vcCFG_AUTO;
413 if ((cfgpath = vc_getVserverByCtx(list->ctx, &cfgstyle, 0))!=0 &&
414 (name = vc_getVserverName(cfgpath, cfgstyle))!=0)
415 strncpy(buf+55, name, 20);
428 int main(int argc, char **argv)
431 struct dirent *dir_entry;
435 process_name = argv[0];
438 if (strcmp(argv[1], "--help") ==0) showHelp(1, argv[0], 0);
439 if (strcmp(argv[1], "--version")==0) showVersion();
442 WRITE_MSG(2, "Unknown parameter, use '--help' for more information\n");
446 // do not include own stat
450 // try to switch in context 1
451 if (vc_get_task_xid(0)!=1)
452 Evc_new_s_context(1, 0,0);
455 // create the fist...
456 my_ctx_list = insert_ctx(0, NULL);
457 // init with the default name for the context 0
458 strncpy(my_ctx_list->name, "root server", CTX_NAME_MAX_LEN);
461 Echdir(PROC_DIR_NAME);
462 proc_dir = Eopendir(".");
463 while ((dir_entry = readdir(proc_dir)) != NULL)
465 // select only process file
466 if (!isdigit(*dir_entry->d_name))
469 if (atoi(dir_entry->d_name) != my_pid)
470 count_ctx(&my_ctx_list, get_process_info(dir_entry->d_name));
475 // output the ctx_list
476 showContexts(my_ctx_list);
479 free_ctx(my_ctx_list);