use '-n' flag
[util-vserver.git] / util-vserver / src / vserver-stat.c
1 // $Id$
2
3 // Copyright (C) 2003 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
4 // based on vserver-stat.cc by Guillaum Dallaire and Jacques Gelinas
5 //  
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)
9 // any later version.
10 //  
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.
15 //  
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.
19
20 /*
21         vserver-stat help you to see all the active context currently in the kernel
22         with some useful stat
23
24         Changelog:
25
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
32 */
33 #ifdef HAVE_CONFIG_H
34 #  include <config.h>
35 #endif
36
37 #include "vserver.h"
38 #include "util.h"
39 #include "internal.h"
40
41 #include <ensc_vector/vector.h>
42
43 #include <stdio.h>
44 #include <stdlib.h>
45 #include <unistd.h>
46 #include <fcntl.h>
47 #include <ctype.h>
48 #include <sys/types.h>
49 #include <sys/stat.h>
50 #include <dirent.h>
51 #include <string.h>
52 #include <errno.h>
53 #include <syscall.h>
54 #include <time.h>
55 #include <stdbool.h>
56
57 #define ENSC_WRAPPERS_DIRENT    1
58 #define ENSC_WRAPPERS_VSERVER   1
59 #define ENSC_WRAPPERS_FCNTL     1
60 #define ENSC_WRAPPERS_UNISTD    1
61 #include "wrappers.h"
62
63 #define PROC_DIR_NAME "/proc"
64 #define CTX_DIR_NAME "/var/run/vservers/"
65 #define CTX_NAME_MAX_LEN 50
66
67 int     wrapper_exit_code = 1;
68
69 struct XidData
70 {
71     xid_t       xid;
72     int         process_count;
73     int         VmSize_total;
74     int         VmRSS_total;
75     long        start_time_oldest;
76     long        stime_total, utime_total;
77 };
78
79 struct process_info
80 {
81         long VmSize;            // number of pages of virtual memory
82         long VmRSS;             // resident set size from /proc/#/stat
83         long start_time;        // start time of process -- seconds since 1-1-70
84         long stime, utime;      // kernel & user-mode CPU time accumulated by process
85         long cstime, cutime;    // cumulative time of process and reaped children
86         xid_t s_context;
87 };
88
89 struct ArgInfo {
90     enum { tpUNSET, tpCTX, tpPID }      type;
91     xid_t               ctx;
92     pid_t               pid;
93     unsigned int        interval;
94     bool                shutdown;
95     bool                omit_init;
96     size_t              argc;
97     char * const *      argv;
98 };
99
100 static void
101 showHelp(int fd, char const *cmd, int res)
102 {
103   WRITE_MSG(fd, "Usage:  ");
104   WRITE_STR(fd, cmd);
105   WRITE_MSG(fd,
106             "\n"
107             "Show informations about all the active context.\n\n"
108             "   CTX#            Context number\n"
109             "                   #0 = root context\n"
110             "                   #1 = monitoring context\n"
111             "   PROC QTY        Quantity of processes in each context\n"
112             "   VSZ             Number of pages of virtual memory\n"
113             "   RSS             Resident set size\n"
114             "   userTIME        User-mode CPU time accumulated\n"
115             "   sysTIME         Kernel-mode CPU time accumulated\n"
116             "   UPTIME          Uptime/context\n"
117             "   NAME            Virtual server name\n"
118             "\n");
119   exit(res);
120 }
121
122 static void
123 showVersion()
124 {
125   WRITE_MSG(1,
126             "vserver-stat " VERSION " -- show virtual context statistics\n"
127             "This program is part of " PACKAGE_STRING "\n\n"
128             "Copyright (C) 2003 Enrico Scholz\n"
129             VERSION_COPYRIGHT_DISCLAIMER);
130   exit(0);
131 }
132
133
134 // return uptime (in ms) from /proc/uptime
135 static uint64_t
136 getUptime()
137 {
138   int           fd;
139   char          buffer[64];
140   char *        errptr;
141   size_t        len;
142   time_t        secs;
143   uint32_t      msecs=0;
144
145     // open the /proc/uptime file
146   fd  = Eopen("/proc/uptime", O_RDONLY, 0);
147   len = Eread(fd, buffer, sizeof buffer);
148
149   if (len==sizeof(buffer)) {
150     WRITE_MSG(2, "Too much data in /proc/uptime; aborting...\n");
151     exit(1);
152   }
153   Eclose(fd);
154
155   while (len>0 && buffer[len-1]=='\n') --len;
156   buffer[len] = '\0';
157
158   secs = strtol(buffer, &errptr, 10);
159   if (*errptr!='.') errptr = buffer;
160   else              msecs = strtol(errptr+1, &errptr, 10);
161
162   if ((*errptr!='\0' && *errptr!=' ') || errptr==buffer) {
163     WRITE_MSG(2, "Bad data in /proc/uptime\n");
164     exit(1);
165   }
166
167   return secs*100 + msecs;
168 }
169
170 static int
171 cmpData(void const *xid_v, void const *map_v)
172 {
173   xid_t const * const                   xid = xid_v;
174   struct XidData const * const          map = map_v;
175   int   res = *xid - map->xid;
176
177   return res;
178 }
179
180 static void
181 registerXid(struct Vector *vec, struct process_info *process)
182 {
183   struct XidData        *res;
184
185   res = Vector_search(vec, &process->s_context, cmpData);
186   if (res==0) {
187     res = Vector_insert(vec, &process->s_context, cmpData);
188     res->xid           = process->s_context;
189     res->process_count = 0;
190     res->VmSize_total  = 0;
191     res->VmRSS_total   = 0;
192     res->utime_total   = 0;
193     res->stime_total   = 0;
194     res->start_time_oldest = 0;
195   }
196
197   ++res->process_count;
198   res->VmSize_total += process->VmSize;
199   res->VmRSS_total  += process->VmRSS;
200   res->utime_total  += process->utime + process->cutime;
201   res->stime_total  += process->stime + process->cstime;
202
203   if (res->start_time_oldest == 0) // first entry
204     res->start_time_oldest = process->start_time;
205   else
206     if (res->start_time_oldest > process->start_time)
207       res->start_time_oldest = process->start_time;
208 }
209
210 // open the process's status file to get the ctx number, and other stat
211 struct process_info *get_process_info(char *pid)
212 {
213   int                           fd;
214   char                          buffer[1024];
215   char                          *p;
216   size_t                        idx, l=strlen(pid);
217   static struct process_info    process;
218
219 #if 1
220   process.s_context = vc_get_task_xid(atoi(pid));
221 #else
222 #  warning Compiling in debug-code
223   process.s_context = random()%6;
224 #endif
225
226   if (process.s_context==VC_NOCTX) {
227     int         err=errno;
228     WRITE_MSG(2, "vc_get_task_xid(");
229     WRITE_STR(2, pid);
230     WRITE_MSG(2, "): ");
231     WRITE_STR(2, strerror(err));
232     WRITE_MSG(2, "\n");
233   }
234   
235   memcpy(buffer,     "/proc/", 6); idx  = 6;
236   memcpy(buffer+idx, pid,      l); idx += l;
237   memcpy(buffer+idx, "/stat",  6);
238         
239     // open the /proc/#/stat file
240   if ((fd = open(buffer, O_RDONLY, 0)) == -1)
241     return NULL;
242     // put the file in a buffer
243   if (read(fd, buffer, sizeof(buffer)) < 1)
244     return NULL;
245
246   close(fd);
247
248   p   = strchr(buffer, ')');            // go after the PID (process_name)
249   for (idx = 0; idx<12 && *p!='\0'; ++p)
250     if ((*p)==' ') ++idx;
251
252   process.utime  = strtol(p,   &p, 10);
253   process.stime  = strtol(p+1, &p, 10);
254   process.cutime = strtol(p+1, &p, 10);
255   process.cstime = strtol(p+1, &p, 10);
256
257   for (idx = 0; idx<5 && *p!='\0'; ++p)
258     if ((*p)==' ') ++idx;
259
260   process.start_time = strtol(p,   &p, 10);
261   process.VmSize     = strtol(p+1, &p, 10);
262   process.VmRSS      = strtol(p+1, &p, 10);
263         
264   return &process;
265 }
266
267 static size_t
268 fillUintZero(char *buf, unsigned long val, size_t cnt)
269 {
270   size_t        l;
271   
272   l = utilvserver_fmt_ulong(buf, val);
273   if (l<cnt) {
274     memmove(buf+cnt-l, buf, l);
275     memset(buf, '0', cnt-l);
276   }
277   buf[cnt] = '\0';
278
279   return cnt;
280 }
281
282 static void
283 shortenMem(char *buf, unsigned long val)
284 {
285   char const *  SUFFIXES[] = { " ", "K", "M", "G", "T", "+" };
286   char          tmp[16];
287   char const *  suffix = "+";
288   size_t        i, l;
289   unsigned int  mod = 0;
290
291   for (i=0; i<6; ++i) {
292     if (val<1000) {
293       suffix = SUFFIXES[i];
294       break;
295     }
296     mod   = 10*(val & 1023)/1024;
297     val >>= 10;
298   }
299
300   if (val >9999) val=9999;
301   if (val>=1000) mod=0;
302
303   l = utilvserver_fmt_ulong(tmp, val);
304   if (mod!=0) {
305     tmp[l++] = '.';
306     l += utilvserver_fmt_ulong(tmp+l, mod);
307   }
308   i = 7-l-strlen(suffix);
309   
310   memcpy(buf+i,   tmp, l);
311   memcpy(buf+i+l, suffix, strlen(suffix));
312 }
313
314 static void
315 shortenTime(char *buf, uint64_t t)
316 {
317   char          tmp[32];
318   char          *ptr = tmp;
319
320   unsigned long hh, mm, ss, ms;
321
322   ms = t % 100;
323   t /= 100;
324
325   ss = t%60;
326   t /= 60;
327   mm = t%60;
328   t /= 60;
329   hh = t%60;
330   t /= 24;
331
332   if (t>0) {
333     ptr   += utilvserver_fmt_ulong(ptr, t);
334     *ptr++ = 'd';
335     ptr   += fillUintZero(ptr, hh, 2);
336     *ptr++ = 'h';
337     ptr   += fillUintZero(ptr, mm, 2);
338   }
339   else if (hh>0) {
340     ptr   += utilvserver_fmt_ulong(ptr, hh);
341     *ptr++ = 'h';
342     ptr   += fillUintZero(ptr, mm, 2);
343     *ptr++ = 'm';
344     ptr   += fillUintZero(ptr, ss, 2);    
345   }
346   else {
347     ptr   += utilvserver_fmt_ulong(ptr, mm);
348     *ptr++ = 'm';
349     ptr   += fillUintZero(ptr, ss, 2);
350     *ptr++ = 's';
351     ptr   += fillUintZero(ptr, ms, 2);
352   }
353
354   *ptr = ' ';
355   memcpy(buf+10-(ptr-tmp), tmp, ptr-tmp);
356 }
357
358 static void
359 showContexts(struct Vector *vec)
360 {
361   uint64_t      uptime = getUptime();
362   struct XidData const *        ptr     = Vector_begin_const(vec);
363   struct XidData const * const  end_ptr = Vector_end_const(vec);
364   
365
366   WRITE_MSG(1, "CTX   PROC    VSZ    RSS  userTIME   sysTIME    UPTIME NAME\n");
367   for (; ptr<end_ptr; ++ptr) {
368     char        buf[sizeof(xid_t)*3 + 512];
369     char        tmp[sizeof(int)*3 + 2];
370     size_t      l;
371
372     memset(buf, ' ', sizeof(buf));
373     l = utilvserver_fmt_long(buf, ptr->xid);
374     l = utilvserver_fmt_long(tmp, ptr->process_count);
375     memcpy(buf+10-l, tmp, l);
376
377     shortenMem (buf+10, ptr->VmSize_total);
378     shortenMem (buf+17, ptr->VmRSS_total);
379     shortenTime(buf+24, ptr->utime_total);
380     shortenTime(buf+34, ptr->stime_total);
381     shortenTime(buf+44, uptime - ptr->start_time_oldest);
382
383     switch (ptr->xid) {
384       case 0            :  strncpy(buf+55, "root server",       20); break;
385       case 1            :  strncpy(buf+55, "monitoring server", 20); break;
386       default           : {
387         char *          name     = 0;
388         char *          cfgpath  = 0;
389         vcCfgStyle      cfgstyle = vcCFG_AUTO;
390
391         if ((cfgpath = vc_getVserverByCtx(ptr->xid, &cfgstyle, 0))!=0 &&
392             (name    = vc_getVserverName(cfgpath, cfgstyle))!=0)
393           strncpy(buf+55, name, 20);
394         free(name);
395         free(cfgpath);
396       }
397     }
398
399     write(1, buf, 80);
400     write(1, "\n", 1);
401   }
402 }
403
404 int main(int argc, char **argv)
405 {
406   DIR *                 proc_dir;
407   struct dirent*        dir_entry;
408   pid_t                 my_pid;
409   struct Vector         xid_data;
410
411   if (argc==2) {
412     if (strcmp(argv[1], "--help")   ==0) showHelp(1, argv[0], 0);
413     if (strcmp(argv[1], "--version")==0) showVersion();
414   }
415   if (argc!=1) {
416     WRITE_MSG(2, "Unknown parameter, use '--help' for more information\n");
417     return EXIT_FAILURE;
418   }
419
420     // do not include own stat
421   my_pid = getpid();
422
423 #if 1
424     // try to switch in context 1
425   if (vc_get_task_xid(0)!=1)
426     Evc_new_s_context(1, 0,0);
427 #else
428 #  warning Compiling in debug-code
429 #endif
430
431   Vector_init(&xid_data, sizeof(struct XidData));
432
433   Echdir(PROC_DIR_NAME);
434   proc_dir = Eopendir(".");
435   while ((dir_entry = readdir(proc_dir)) != NULL)
436   {
437       // select only process file
438     if (!isdigit(*dir_entry->d_name))
439       continue;
440
441     if (atoi(dir_entry->d_name) != my_pid)
442       registerXid(&xid_data, get_process_info(dir_entry->d_name));
443   }
444   closedir(proc_dir);
445
446     // output the ctx_list      
447   showContexts(&xid_data);
448
449 #ifndef NDEBUG
450   Vector_free(&xid_data);
451 #endif
452   
453   return 0;
454 }