51 #include "safec_lib.h"
60 #define MEM_STRING_SIZE 20
61 #define PROC_PATH_SIZE 50
66 #ifndef ENABLE_XCAM_SUPPORT
69 char processName[BUF_LEN];
77 int main(
int argc,
char *argv[]) {
79 char *processName = NULL;
89 processName = argv[1];
91 if (processName != NULL) {
92 rc = memcpy_s(pInfo.processName,
sizeof(pInfo.processName), processName, strlen(processName)+1);
104 cout <<
"{\"Failed\":\"0\"}";
108 cout <<
"{\"mem_" << processName <<
"\":\"" << pInfo.memUse <<
"\"},";
109 cout <<
"{\"cpu_" << processName <<
"\":\"" << pInfo.cpuUse <<
"\"}";
133 char procPath[PROC_PATH_SIZE];
134 static char retMem[MEM_STRING_SIZE];
135 int intStr = 0,intValue = 0;
136 long pageSizeInKb = sysconf(_SC_PAGE_SIZE) / 1024;
145 rc = sprintf_s(procPath,
sizeof(procPath),
"/proc/%d/statm", *processPid);
152 ifstream statm(procPath);
153 int totalMemory, residentMemory;
154 statm >> totalMemory >> residentMemory;
157 residentMemory *= pageSizeInKb;
158 intStr = (int)residentMemory;
160 if (intValue >= 1024)
161 intStr = intStr/1024;
162 rc = sprintf_s(retMem,
sizeof(retMem),
"%d%s", intStr,(intValue >= 1024) ?
"m" :
"k");
176 char command[CMD_LEN] = {
'\0'};
177 char *pcpu = NULL, *pmem = NULL;
178 char var1[BUF_LEN] = {
'\0'};
179 char var2[BUF_LEN] = {
'\0'};
180 char var3[BUF_LEN] = {
'\0'};
181 char var4[BUF_LEN] = {
'\0'};
182 char var5[BUF_LEN] = {
'\0'};
183 char var6[BUF_LEN] = {
'\0'};
184 char var7[BUF_LEN] = {
'\0'};
185 char var8[512]= {
'\0'};
186 char var9[512]= {
'\0'};
187 char var10[512]= {
'\0'};
196 rc = sprintf_s(command,
sizeof(command),
"pidof %s", pInfo->processName);
202 if(!(inFp = popen(command,
"r"))){
205 fscanf(inFp,
"%d",&pid);
214 rc = sprintf_s(command,
sizeof(command),
"top -b -n 1 | sed 's/^[ \t]*//' | grep \"^%d\"", pid);
218 rc = sprintf_s(command,
sizeof(command),
"top -n 1 | grep -i '%s'", pInfo->processName);
224 rc = sprintf_s(command,
sizeof(command),
"top -b -n 1 | grep -i '%s'", pInfo->processName);
232 if(!(inFp = popen(command,
"r"))){
239 if (fscanf(inFp,
"%s %s %s %s %s %s %s %s", var1, var2, var3, var4, var5, var6, var7, var8) == 8) {
249 if (fscanf(inFp,
"%s %s %s %s %s %s %s %s %s %s", var1, var2, var3, var4, var5, var6, var7, var8, var9, var10) == 10) {
257 if ((pcpu != NULL) && (pmem != NULL)) {
258 rc = strcpy_s(pInfo->cpuUse,
sizeof(pInfo->cpuUse),pcpu);
264 rc = strcpy_s(pInfo->memUse,
sizeof(pInfo->memUse),pmem);
276 #else //ENABLE_XCAM_SUPPORT
297 char szFileName [CMD_LEN],szStatStr [2048],*s, *t;
300 int ppid, pgrp, session, tty, tpgid, counter, priority, starttime, signal, blocked, sigignore, sigcatch;
301 char exName [CMD_LEN], state;
303 unsigned int flags, minflt, cminflt, majflt, cmajflt, timeout, itrealvalue, vsize, rlim, startcode, endcode, startstack, kstkesp, kstkeip, wchan, rss;
308 cout <<
"Invalid input(pinfo=NULL) to get process info"<<endl;
312 rc = sprintf_s(szFileName,
sizeof(szFileName),
"/proc/%u/stat", (
unsigned) pid);
319 if (-1 == access (szFileName, R_OK))
321 cout <<
"Unable to access file in get proc info"<<endl;
325 if (-1 != stat (szFileName, &st))
335 if ((fp = fopen (szFileName,
"r")) == NULL)
337 cout <<
"Failed to open file in get process info"<<endl;
341 if ((s = fgets (szStatStr, 2048, fp)) == NULL)
347 s = strchr (szStatStr,
'(') + 1;
348 t = strchr (szStatStr,
')');
349 rc = strncpy_s(exName,
sizeof(exName),s,t - s);
356 exName [t - s] =
'\0';
359 sscanf (t + 2,
"%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",
361 &(state), &(ppid), &(pgrp), &(session), &(tty), &(tpgid), &(flags),
362 &(minflt), &(cminflt), &(majflt), &(cmajflt), &(pinfo->
utime),
364 &(priority), &(timeout), &(itrealvalue), &(starttime),
365 &(vsize), &(pinfo->
rss), &(rlim), &(startcode), &(endcode),
366 &(startstack), &(kstkesp), &(kstkeip), &(signal), &(blocked),
367 &(sigignore), &(sigcatch), &(wchan));
382 int getTotalCpuTimes(
int * totalTime)
388 fp = fopen(
"/proc/stat",
"r");
393 fscanf(fp,
"%*s %Lf %Lf %Lf %Lf %Lf %Lf %Lf %Lf %Lf %Lf",
394 &a[0],&a[1],&a[2],&a[3],&a[4],&a[5],&a[6],&a[7],&a[8],&a[9]);
396 total = (a[0]+a[1]+a[2]+a[3]+a[4]+a[5]+a[6]+a[7]+a[8]+a[9]);
411 bool getProcessCpuUtilization(
int pid,
float *procCpuUtil)
413 char cpuUtilizationProc[MAXLEN]={
'\0'};
416 float total_time_process[2],time[2];
423 no_cpu=sysconf(_SC_NPROCESSORS_ONLN);
427 total_time_process[0]= pinfo1.
utime +
433 if( !getTotalCpuTimes(&t[0]) )
442 total_time_process[1]= pinfo1.
utime +
447 if(
false == getTotalCpuTimes(&t[1]) )
451 sub1 = total_time_process[1]-total_time_process[0];
452 time1= time[1] - time[0];
454 util = (sub1/time1)*100;
457 util = roundf(util * 100)/100;
469 int main(
int argc,
char *argv[]) {
470 char *processName = NULL;
472 char command[CMD_LEN] = {
'\0'};
481 cout<<
"No Arguments passed!"<<endl;
485 processName = argv[1];
486 rc = sprintf_s(command,
sizeof(command),
"pidof %s", processName);
494 if(!(inFp = popen(command,
"r"))){
498 fscanf(inFp,
"%d",&pid);
505 cout <<
"{\"Failed\":\"0\"}";
514 getProcessCpuUtilization(pid,&cpu);
516 pagesize_kb = getpagesize()/1024;
517 cout <<
"{\"mem_" << processName <<
"\":\"" << (pInfo.
rss) * pagesize_kb <<
"\"},";
518 cout <<
"{\"cpu_" << processName <<
"\":\"" << cpu <<
"\"}";
524 #endif //ENABLE_XCAM_SUPPORT