44 #include "safec_lib.h"
49 #define BUF_LEN 20 // increase the BUF_LEN, because getting the source length is high
54 #define MEM_STRING_SIZE 20
55 #define PROC_PATH_SIZE 50
63 #ifdef LIBSYSWRAPPER_BUILD
64 #include "secure_wrapper.h"
67 #define MEM_KEY_PREFIX "mem_"
68 #define CPU_KEY_PREFIX "cpu_"
80 char processName[BUF_LEN];
107 if (processName != NULL) {
109 char pidofCommand[PIDOF_SIZE];
110 #if defined (ENABLE_PS_PROCESS_SEARCH)
111 char psCommand[CMD_LEN];
114 char *mem_key = NULL, *cpu_key = NULL;
119 int cpu_key_length = 0, mem_key_length = 0;
127 memcpy(pInfo.processName, processName, strlen(processName)+1);
129 sprintf(pidofCommand,
"pidof %s", processName);
131 #ifdef LIBSYSWRAPPER_BUILD
132 if (!(cmdPid = v_secure_popen(
"r",
"pidof %s", processName)))
134 if (!(cmdPid = popen(pidofCommand,
"r")))
137 LOG(
"Failed to execute %s", pidofCommand);
141 pid = (pid_t *) malloc (
sizeof(pid_t));
144 #ifdef LIBSYSWRAPPER_BUILD
145 v_secure_pclose(cmdPid);
152 while(fscanf(cmdPid,
"%d",(pid+index)) == 1)
154 if ((*(pid+index)) <= 0)
159 temp = (pid_t *) realloc (pid,((index+1)*
sizeof(pid_t)) );
163 #ifdef LIBSYSWRAPPER_BUILD
164 v_secure_pclose(cmdPid);
173 #ifdef LIBSYSWRAPPER_BUILD
174 v_secure_pclose(cmdPid);
179 #if defined (ENABLE_PS_PROCESS_SEARCH)
184 sprintf(psCommand,
"busybox ps | grep %s | grep -v grep | awk '{ print $1 }' | tail -n1", processName);
186 #ifdef LIBSYSWRAPPER_BUILD
187 if (!(cmdPid = v_secure_popen(
"r",
"busybox ps | grep %s | grep -v grep | awk '{ print $1 }' | tail -n1", processName)))
189 if (!(cmdPid = popen(psCommand,
"r")))
197 while(fscanf(cmdPid,
"%d",(pid+index)) == 1)
199 if ((*(pid+index)) <= 0)
204 temp = (pid_t *) realloc (pid,((index+1)*
sizeof(pid_t)) );
208 #ifdef LIBSYSWRAPPER_BUILD
209 v_secure_pclose(cmdPid);
218 #ifdef LIBSYSWRAPPER_BUILD
219 v_secure_pclose(cmdPid);
240 pInfo.total_instance=index;
244 LOG(
"Failed to get procInfo for %s", processName);
248 mem_key_length = strlen(processName) + strlen(MEM_KEY_PREFIX) + 1;
249 cpu_key_length = strlen(processName) + strlen(CPU_KEY_PREFIX) + 1;
250 mem_key = malloc(mem_key_length);
251 cpu_key = malloc(cpu_key_length);
252 if (NULL != mem_key && NULL != cpu_key)
260 snprintf(cpu_key,cpu_key_length,
"%s%s", CPU_KEY_PREFIX,processName);
262 snprintf(mem_key,mem_key_length,
"%s%s", MEM_KEY_PREFIX,processName);
292 char szFileName [CMD_LEN],szStatStr [2048],*s, *t;
294 int ppid, pgrp, session, tty, tpgid, counter, priority, starttime, signal, blocked, sigignore, sigcatch,fd, read_len;
295 char exName [CMD_LEN], state;
297 unsigned int flags, minflt, cminflt, majflt, cmajflt, timeout, itrealvalue, vsize, rlim, startcode, endcode, startstack, kstkesp, kstkeip, wchan;
302 LOG(
"Invalid input(pinfo=NULL) to get process info");
306 rc = sprintf_s(szFileName,
sizeof(szFileName),
"/proc/%u/stat", (
unsigned) pid);
313 if ((fd = open(szFileName, O_RDONLY)) == -1)
315 LOG(
"Unable to access file in get proc info");
320 if(-1 != fstat(fd, &st)) {
328 read_len = read(fd, szStatStr, 2047);
333 szStatStr[read_len++] =
'\0';
336 s = strchr (szStatStr,
'(') + 1;
337 t = strchr (szStatStr,
')');
339 rc = strncpy_s(exName,
sizeof(exName),s,t - s);
346 exName [t - s] =
'\0';
347 if(s != NULL && t != NULL && (t-s) > 0){
348 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",
419 static char retMem[MEM_STRING_SIZE];
420 int intStr = 0,intValue = 0;
421 double residentMemory = 0.0;
423 long pageSizeInKb = sysconf(_SC_PAGE_SIZE) / 1024;
424 unsigned int total_memory=0;
427 int proc_struct_size =
sizeof(
procinfo);
428 for(index=0;index<(pmInfo->total_instance);index++)
430 rc = memset_s(&pinfo,proc_struct_size,0,proc_struct_size);
435 total_memory+=pinfo.
rss;
438 residentMemory = total_memory * pageSizeInKb;
439 intStr = (int)residentMemory;
441 if (intValue >= 1024)
442 intStr = intStr/1024;
443 rc = sprintf_s(retMem,
sizeof(retMem),
"%d%s", intStr,(intValue >= 1024) ?
"m" :
"k");
450 rc = strncpy_s(pmInfo->memUse,
sizeof(pmInfo->memUse),retMem,strlen(retMem)+1);
459 #if !defined(ENABLE_XCAM_SUPPORT) && !defined(ENABLE_RDKB_SUPPORT)
472 char command[CMD_LEN] = {
'\0'};
473 char var1[BUF_LEN] = {
'\0'};
474 char var2[BUF_LEN] = {
'\0'};
475 char var3[BUF_LEN] = {
'\0'};
476 char var4[BUF_LEN] = {
'\0'};
477 char var5[BUF_LEN] = {
'\0'};
478 char var6[BUF_LEN] = {
'\0'};
479 char var7[BUF_LEN] = {
'\0'};
480 char var8[512]= {
'\0'};
481 char var9[512]= {
'\0'};
482 char var10[512]= {
'\0'};
483 int total_cpu_usage=0;
484 char top_op[2048]= {
'\0'};
494 ret = system(
" top -c -n 1 2> /dev/null 1> /dev/null");
500 if ( 1 == cmd_option ) {
501 rc = sprintf_s(command,
sizeof(command),
"top -n 1 -c | grep -v grep |grep -i '%s'", pInfo->processName);
508 rc = sprintf_s(command,
sizeof(command),
"top -n 1 | grep -i '%s'", pInfo->processName);
517 if ( 1 == cmd_option ) {
518 rc = sprintf_s(command,
sizeof(command),
"COLUMNS=1000 top -b -n 1 -c | grep -v grep | grep -i '%s'", pInfo->processName);
525 rc = sprintf_s(command,
sizeof(command),
"COLUMNS=1000 top -b -n 1 | grep -i '%s'", pInfo->processName);
536 if ( 1 == cmd_option ) {
537 rc = sprintf_s(command,
sizeof(command),
"top -b -n 1 -c | grep -v grep | grep -i '%s'", pInfo->processName);
544 rc = sprintf_s(command,
sizeof(command),
"top -b -n 1 | grep -i '%s'", pInfo->processName);
556 if(!(inFp = popen(command,
"r"))){
563 while(fgets(top_op,2048,inFp)!=NULL) {
564 if(sscanf(top_op,
"%s %s %s %s %s %s %s %s", var1, var2, var3, var4, var5, var6, var7, var8) == 8) {
565 total_cpu_usage += atoi(var7);
571 while(fgets(top_op,2048,inFp)!=NULL) {
572 if(sscanf(top_op,
"%16s %16s %16s %16s %16s %16s %16s %512s %512s %512s", var1, var2, var3, var4, var5, var6, var7, var8, var9, var10) == 10) {
573 total_cpu_usage += atoi(var9);
579 rc = sprintf_s(pInfo->cpuUse,
sizeof(pInfo->cpuUse),
"%d", total_cpu_usage);
591 #else //ENABLE_XCAM_SUPPORT & ENABLE_RDKB_SUPPORT
601 int getTotalCpuTimes(
int * totalTime)
607 fp = fopen(
"/proc/stat",
"r");
612 if(fscanf(fp,
"%*s %Lf %Lf %Lf %Lf %Lf %Lf %Lf %Lf %Lf %Lf",
613 &a[0],&a[1],&a[2],&a[3],&a[4],&a[5],&a[6],&a[7],&a[8],&a[9]) != 10 )
615 LOG(
"Failed in fscanf()\n");
619 total = (a[0]+a[1]+a[2]+a[3]+a[4]+a[5]+a[6]+a[7]+a[8]+a[9]);
634 int getProcessCpuUtilization(
int pid,
float *procCpuUtil)
638 float total_time_process[2],time[2];
645 sysconf(_SC_NPROCESSORS_ONLN);
649 total_time_process[0]= pinfo1.
utime +
655 if( !getTotalCpuTimes(&t[0]) )
664 total_time_process[1]= pinfo1.
utime +
669 if( 0 == getTotalCpuTimes(&t[1]) )
673 sub1 = total_time_process[1]-total_time_process[0];
674 time1= time[1] - time[0];
675 util = (sub1/time1)*100;
692 for(index=0;index<(pmInfo->total_instance);index++)
694 if (0 == getProcessCpuUtilization(pmInfo->pid[index], &cpu))
701 rc = sprintf_s(pmInfo->cpuUse,
sizeof(pmInfo->cpuUse),
"%.1f", (
float)total_cpu);
710 #endif //ENABLE_XCAM_SUPPORT & ENABLE_RDKB_SUPPORT