ProcStats.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005 #ifdef __ICC
00006
00007
00008 #pragma warning(disable:2259)
00009 #endif
00010
00011 #include "ProcStats.h"
00012
00013 #ifdef __linux
00014 #include <unistd.h>
00015 #include <iostream>
00016 #include <sstream>
00017 #include <fcntl.h>
00018 #include <sys/types.h>
00019 #include <sys/signal.h>
00020 #include <sys/syscall.h>
00021 #include <sys/procfs.h>
00022 #include <cstdio>
00023
00024 using std::cerr;
00025 using std::cout;
00026 using std::endl;
00027
00028 struct linux_proc {
00029 int pid;
00030 char comm[400];
00031 char state;
00032 int ppid;
00033 int pgrp;
00034 int session;
00035 int tty;
00036 int tpgid;
00037 unsigned int flags;
00038 unsigned int minflt;
00039 unsigned int cminflt;
00040 unsigned int majflt;
00041 unsigned int cmajflt;
00042 int utime;
00043 int stime;
00044 int cutime;
00045 int cstime;
00046 int counter;
00047 int priority;
00048 unsigned int timeout;
00049 unsigned int itrealvalue;
00050 int starttime;
00051 unsigned int vsize;
00052 unsigned int rss;
00053 unsigned int rlim;
00054 unsigned int startcode;
00055 unsigned int endcode;
00056 unsigned int startstack;
00057 unsigned int kstkesp;
00058 unsigned int kstkeip;
00059 int signal;
00060 int blocked;
00061 int sigignore;
00062 int sigcatch;
00063 unsigned int wchan;
00064 };
00065 #endif // __linux
00066
00067 ProcStats::cleanup::~cleanup() {
00068 if(ProcStats::inst!=0) {
00069 delete ProcStats::inst;
00070 ProcStats::inst=0;
00071 }
00072 }
00073
00074 ProcStats* ProcStats::instance() {
00075 static cleanup c;
00076 if(inst==0)
00077 inst = new ProcStats;
00078 return inst;
00079 }
00080
00081 ProcStats* ProcStats::inst = 0;
00082
00083 ProcStats::ProcStats():valid(false)
00084 {
00085 #ifdef __linux
00086 pg_size = sysconf(_SC_PAGESIZE);
00087 std::ostringstream ost;
00088
00089 ost << "/proc/" << getpid() << "/stat";
00090 fname = ost.str();
00091 if((fd=open(fname.c_str(),O_RDONLY))<0)
00092 {
00093 cerr << "Failed to open " << ost.str() << endl;
00094 return;
00095 }
00096 #endif
00097 valid=true;
00098 }
00099
00100 ProcStats::~ProcStats()
00101 {
00102 #ifdef __linux
00103 close(fd);
00104 #endif
00105 }
00106
00107 bool ProcStats::fetch(procInfo& f)
00108 {
00109 if( valid == false ) return false;
00110
00111 #ifdef __linux
00112 double pr_size, pr_rssize;
00113 linux_proc pinfo;
00114 int cnt;
00115
00116 lseek(fd,0,SEEK_SET);
00117
00118 if((cnt=read(fd,buf,sizeof(buf)))<0)
00119 {
00120 cout << "LINUX Read of Proc file failed:" << endl;
00121 return false;
00122 }
00123
00124 if(cnt>0)
00125 {
00126 buf[cnt]='\0';
00127
00128 sscanf(buf,
00129 "%d %s %c %d %d %d %d %d %u %u %u %u %u %d %d %d %d %d %d %u %u %d %u %u %u %u %u %u %u %u %d %d %d %d %u",
00130 &pinfo.pid,
00131 pinfo.comm,
00132 &pinfo.state,
00133 &pinfo.ppid,
00134 &pinfo.pgrp,
00135 &pinfo.session,
00136 &pinfo.tty,
00137 &pinfo.tpgid,
00138 &pinfo.flags,
00139 &pinfo.minflt,
00140 &pinfo.cminflt,
00141 &pinfo.majflt,
00142 &pinfo.cmajflt,
00143 &pinfo.utime,
00144 &pinfo.stime,
00145 &pinfo.cutime,
00146 &pinfo.cstime,
00147 &pinfo.counter,
00148 &pinfo.priority,
00149 &pinfo.timeout,
00150 &pinfo.itrealvalue,
00151 &pinfo.starttime,
00152 &pinfo.vsize,
00153 &pinfo.rss,
00154 &pinfo.rlim,
00155 &pinfo.startcode,
00156 &pinfo.endcode,
00157 &pinfo.startstack,
00158 &pinfo.kstkesp,
00159 &pinfo.kstkeip,
00160 &pinfo.signal,
00161 &pinfo.blocked,
00162 &pinfo.sigignore,
00163 &pinfo.sigcatch,
00164 &pinfo.wchan
00165 );
00166
00167
00168 pr_size = (double)pinfo.vsize;
00169 pr_rssize = (double)pinfo.rss;
00170
00171 f.vsize = pr_size / (1024*1024);
00172 f.rss = pr_rssize * pg_size / (1024*1024);
00173 }
00174
00175 #else
00176 f.vsize = 0;
00177 f.rss = 0;
00178 #endif
00179
00180 bool rc = (curr==f)?false:true;
00181
00182 curr.rss=f.rss;
00183 curr.vsize=f.vsize;
00184
00185 return rc;
00186 }
00187