CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_0/src/EventFilter/Processor/src/procUtils.cc

Go to the documentation of this file.
00001     /*
00002  * Displays linux /proc/pid/stat in human-readable format
00003  *
00004  * Build: gcc -o procstat procstat.c
00005  * Usage: procstat pid
00006  *        cat /proc/pid/stat | procstat
00007  *
00008  * Homepage: https://www.brokestream.com/procstat.html
00009  * Version : 2009-03-05
00010  *
00011  * Ivan Tikhonov, https://www.brokestream.com, kefeer@netangels.ru
00012  *
00013  * 2007-09-19 changed HZ=100 error to warning
00014  *
00015  * 2009-03-05 tickspersec are taken from sysconf (Sabuj Pattanayek)
00016  *
00017  */
00018 
00019 
00020 /* Copyright (C) 2009 Ivan Tikhonov
00021 
00022   This software is provided 'as-is', without any express or implied
00023   warranty.  In no event will the authors be held liable for any damages
00024   arising from the use of this software.
00025 
00026   Permission is granted to anyone to use this software for any purpose,
00027   including commercial applications, and to alter it and redistribute it
00028   freely, subject to the following restrictions:
00029 
00030   1. The origin of this software must not be misrepresented; you must not
00031      claim that you wrote the original software. If you use this software
00032      in a product, an acknowledgment in the product documentation would be
00033      appreciated but is not required.
00034   2. Altered source versions must be plainly marked as such, and must not be
00035      misrepresented as being the original software.
00036   3. This notice may not be removed or altered from any source distribution.
00037 
00038   Ivan Tikhonov, kefeer@brokestream.com
00039 
00040 */
00041 
00042 
00043 #define FSHIFT          16              /* nr of bits of precision */
00044 #define FIXED_1         (1<<FSHIFT)     /* 1.0 as fixed-point */
00045 #define LOAD_INT(x) ((x) >> FSHIFT)
00046 #define LOAD_FRAC(x) LOAD_INT(((x) & (FIXED_1-1)) * 100)
00047 
00048 #ifdef linux
00049 #include <sys/sysinfo.h>
00050 #endif
00051 #include <errno.h>
00052 #include <iostream>
00053 #include <iomanip>
00054 
00055 #include <stdio.h>
00056 #include <unistd.h>
00057 #include <time.h>
00058 #ifdef linux
00059 #include <linux/limits.h>
00060 #endif
00061 #include <sys/times.h>
00062 #include <sstream>
00063 #include "procUtils.h"
00064 
00065 namespace evf{
00066   namespace utils{
00067     
00068 
00069 
00070 
00071     typedef long long int num;
00072 
00073     num pid;
00074     char tcomm[FILENAME_MAX];
00075     char state;
00076     
00077     num ppid;
00078     num pgid;
00079     num sid;
00080     num tty_nr;
00081     num tty_pgrp;
00082     
00083     num flags;
00084     num min_flt;
00085     num cmin_flt;
00086     num maj_flt;
00087     num cmaj_flt;
00088     num utime;
00089     num stimev;
00090     
00091     num cutime;
00092     num cstime;
00093     num priority;
00094     num nicev;
00095     num num_threads;
00096     num it_real_value;
00097     
00098     unsigned long long start_time;
00099     
00100     num vsize;
00101     num rss;
00102     num rsslim;
00103     num start_code;
00104     num end_code;
00105     num start_stack;
00106     num esp;
00107     num eip;
00108     
00109     num pending;
00110     num blocked;
00111     num sigign;
00112     num sigcatch;
00113     num wchan;
00114     num zero1;
00115     num zero2;
00116     num exit_signal;
00117     num cpu;
00118     num rt_priority;
00119     num policy;
00120     
00121     long tickspersec;
00122     char obuf[4096];
00123     FILE *input;
00124 
00125     void readone(num *x) { fscanf(input, "%lld ", x); }
00126     void readunsigned(unsigned long long *x) { fscanf(input, "%llu ", x); }
00127     void readstr(char *x) {  fscanf(input, "%s ", x);}
00128     void readchar(char *x) {  fscanf(input, "%c ", x);}
00129     
00130     void printone(const char *name, num x) {  sprintf(obuf,"%20s: %lld\n", name, x);}
00131     void printonex(const char *name, num x) {  sprintf(obuf,"%20s: %016llx\n", name, x);}
00132     void printunsigned(const char *name, unsigned long long x) {  sprintf(obuf,"%20s: %llu\n", name, x);}
00133     void printchar(const char *name, char x) {  sprintf(obuf,"%20s: %c\n", name, x);}
00134     void printstr(const char *name, char *x) {  sprintf(obuf,"%20s: %s\n", name, x);}
00135     void printtime(const char *name, num x) {  sprintf(obuf,"%20s: %f\n", name, (((double)x) / tickspersec));}
00136     
00137     int gettimesinceboot() {
00138       FILE *procuptime;
00139       int sec, ssec;
00140       
00141       procuptime = fopen("/proc/uptime", "r");
00142       fscanf(procuptime, "%d.%ds", &sec, &ssec);
00143       fclose(procuptime);
00144       return (sec*tickspersec)+ssec;
00145     }
00146 
00147     void printtimediff(const char *name, num x) {
00148       int sinceboot = gettimesinceboot();
00149       int running = sinceboot - x;
00150       time_t rt = time(NULL) - (running / tickspersec);
00151       char buf[1024];
00152       
00153       strftime(buf, sizeof(buf), "%m.%d %H:%M", localtime(&rt));
00154       sprintf(obuf,"%20s: %s (%lu.%lus)\n", name, buf, running / tickspersec, running % tickspersec);
00155     }
00156     
00157     void procStat(std::ostringstream *out) {
00158       tickspersec = sysconf(_SC_CLK_TCK);
00159       input = NULL;
00160       
00161       std::ostringstream ost; 
00162       ost << "/proc/" << getpid() << "/stat";
00163       input = fopen(ost.str().c_str(), "r");
00164       
00165 
00166       readone(&pid);
00167       readstr(tcomm);
00168       readchar(&state);
00169       readone(&ppid);
00170       readone(&pgid);
00171       readone(&sid);
00172       readone(&tty_nr);
00173       readone(&tty_pgrp);
00174       readone(&flags);
00175       readone(&min_flt);
00176       readone(&cmin_flt);
00177       readone(&maj_flt);
00178       readone(&cmaj_flt);
00179       readone(&utime);
00180       readone(&stimev);
00181       readone(&cutime);
00182       readone(&cstime);
00183       readone(&priority);
00184       readone(&nicev);
00185       readone(&num_threads);
00186       readone(&it_real_value);
00187       readunsigned(&start_time);
00188       readone(&vsize);
00189       readone(&rss);
00190       readone(&rsslim);
00191       readone(&start_code);
00192       readone(&end_code);
00193       readone(&start_stack);
00194       readone(&esp);
00195       readone(&eip);
00196       readone(&pending);
00197       readone(&blocked);
00198       readone(&sigign);
00199       readone(&sigcatch);
00200       readone(&wchan);
00201       readone(&zero1);
00202       readone(&zero2);
00203       readone(&exit_signal);
00204       readone(&cpu);
00205       readone(&rt_priority);
00206       readone(&policy);
00207       
00208       {
00209         printone("pid", pid); *out << obuf;
00210         printstr("tcomm", tcomm); *out << obuf;
00211         printchar("state", state); *out << obuf;
00212         printone("ppid", ppid); *out << obuf;
00213         printone("pgid", pgid); *out << obuf;
00214         printone("sid", sid); *out << obuf;
00215         printone("tty_nr", tty_nr); *out << obuf;
00216         printone("tty_pgrp", tty_pgrp); *out << obuf;
00217         printone("flags", flags); *out << obuf;
00218         printone("min_flt", min_flt); *out << obuf;
00219         printone("cmin_flt", cmin_flt); *out << obuf;
00220         printone("maj_flt", maj_flt); *out << obuf;
00221         printone("cmaj_flt", cmaj_flt); *out << obuf;
00222         printtime("utime", utime); *out << obuf;
00223         printtime("stime", stimev); *out << obuf;
00224         printtime("cutime", cutime); *out << obuf;
00225         printtime("cstime", cstime); *out << obuf;
00226         printone("priority", priority); *out << obuf;
00227         printone("nice", nicev); *out << obuf;
00228         printone("num_threads", num_threads); *out << obuf;
00229         printtime("it_real_value", it_real_value); *out << obuf;
00230         printtimediff("start_time", start_time); *out << obuf;
00231         printone("vsize", vsize); *out << obuf;
00232         printone("rss", rss); *out << obuf;
00233         printone("rsslim", rsslim); *out << obuf;
00234         printone("start_code", start_code); *out << obuf;
00235         printone("end_code", end_code); *out << obuf;
00236         printone("start_stack", start_stack); *out << obuf;
00237         printone("esp", esp); *out << obuf;
00238         printone("eip", eip); *out << obuf;
00239         printonex("pending", pending); *out << obuf;
00240         printonex("blocked", blocked); *out << obuf;
00241         printonex("sigign", sigign); *out << obuf;
00242         printonex("sigcatch", sigcatch); *out << obuf;
00243         printone("wchan", wchan); *out << obuf;
00244         printone("zero1", zero1); *out << obuf;
00245         printone("zero2", zero2); *out << obuf;
00246         printonex("exit_signal", exit_signal); *out << obuf;
00247         printone("cpu", cpu); *out << obuf;
00248         printone("rt_priority", rt_priority); *out << obuf;
00249         printone("policy", policy); *out << obuf;
00250       }
00251       fclose(input);
00252     }
00253 
00254 
00255 
00256 
00257 
00258     //Derived from:
00259     /* vi: set sw=4 ts=4: */
00260     /*
00261      * Mini uptime implementation for busybox
00262      *
00263      * Copyright (C) 1999,2000 by Lineo, inc.
00264      * Written by Erik Andersen <andersen@lineo.com>, <andersee@debian.org>
00265      *
00266      * This program is free software; you can redistribute it and/or modify
00267      * it under the terms of the GNU General Public License as published by
00268      * the Free Software Foundation; either version 2 of the License, or
00269      * (at your option) any later version.
00270      *
00271      * This program is distributed in the hope that it will be useful,
00272      * but WITHOUT ANY WARRANTY; without even the implied warranty of
00273      * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
00274      * General Public License for more details.
00275      *
00276      * You should have received a copy of the GNU General Public License
00277      * along with this program; if not, write to the Free Software
00278      * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
00279      *
00280      */
00281 
00282     /* This version of uptime doesn't display the number of users on the system,
00283      * since busybox init doesn't mess with utmp.  For folks using utmp that are
00284      * just dying to have # of users reported, feel free to write it as some type
00285      * of BB_FEATURE_UMTP_SUPPORT #define
00286      */
00287 
00288 
00289 
00290     void uptime(std::ostringstream *out)
00291     {
00292 #ifdef linux
00293       int updays, uphours, upminutes;
00294       struct sysinfo info;
00295       struct tm *current_time;
00296       time_t current_secs;
00297 
00298       time(&current_secs);
00299       current_time = localtime(&current_secs);
00300 
00301       sysinfo(&info);
00302 
00303       *out << std::setw(2) 
00304            << (current_time->tm_hour%12 ? current_time->tm_hour%12 : 12)
00305            << ":"
00306            << current_time->tm_min
00307            << (current_time->tm_hour > 11 ? " pm, " : " am, ")
00308            << " up ";
00309       updays = (int) info.uptime / (60*60*24);
00310       if (updays)
00311         *out <<  updays << " day" << ((updays != 1) ? "s " : " ");
00312       upminutes = (int) info.uptime / 60;
00313       uphours = (upminutes / 60) % 24;
00314       upminutes %= 60;
00315       if(uphours)
00316         *out << std::setw(2) << uphours << ":" << upminutes;
00317       else
00318         *out << upminutes << " minutes ";
00319       
00320       *out << " - load average "
00321            << LOAD_INT(info.loads[0]) << " " 
00322            << LOAD_FRAC(info.loads[0]) << " " 
00323            << LOAD_INT(info.loads[1]) << " " 
00324            << LOAD_FRAC(info.loads[1]) << " " 
00325            << LOAD_INT(info.loads[2]) << " " 
00326            << LOAD_FRAC(info.loads[2]) << " ";
00327       *out << " used memory " << std::setw(3) 
00328            << (float(info.totalram-info.freeram)/float(info.totalram))*100 << "%";
00329 #else
00330       // FIXME: one could probably use `clock_get_uptime` and similar on 
00331       // macosx to obtain at least part of the information.
00332       *out << "Unable to retrieve uptime information on this platform.";
00333 #endif
00334     }
00335     void mDiv(std::ostringstream *out, std::string name){
00336       *out << "<div id=\"" << name << "\">";
00337     }
00338     void cDiv(std::ostringstream *out){
00339       *out << "</div>";
00340     } 
00341     void mDiv(std::ostringstream *out, std::string name, std::string value){
00342       mDiv(out,name);
00343       *out << value;
00344       cDiv(out);
00345     }
00346     void mDiv(std::ostringstream *out, std::string name, unsigned int value){
00347       mDiv(out,name);
00348       *out << value;
00349       cDiv(out);
00350     } 
00351   } // namespace utils
00352 } //namespace evf