19 #include <sys/socket.h>
20 #include <linux/netlink.h>
21 #include <linux/connector.h>
22 #include <linux/cn_proc.h>
31 #include <sys/types.h>
38 #ifdef PROCANALYZER_BROADBAND
39 #include <telemetry_busmessage_sender.h>
40 #include <json-c/json.h>
43 #ifndef PROCANALYZER_EXTENDER
46 #include <bits/stdc++.h>
49 #ifndef PROCANALYZER_EXTENDER
52 #include <cimplog/cimplog.h>
53 #define LOGGING_MODULE "CPUPROCANALYZER"
83 #define LINE_LIMIT 256
86 #define TIME_TO_RUN_SECS 0
87 #define BUFF_SIZE_64 64
88 #define BUFF_SIZE_16 16
89 #define DEFAULT_MEM_THRESHOLD 1536 //Max filesize limit
90 #define MONITOR_ALL_PROC_DEF 0 //Default Monitor all process flag
91 #define DEFAULT_DYNAMIC 1 //Default for Dynamic
92 #define ITERATION_THRESHOLD 25 //Max number of runs
93 #define TELEMETRY_ONLY_DEF 0 // Only telemetry reporting default flag
96 #define MEMORY_MASK 0x08
97 #define FDCOUNT_MASK 0x04
98 #define THREADCOUNT_MASK 0x02
99 #define LOADAVG_MASK 0x02
100 #define CLICOUNT_MASK 0x01
102 #define SYS_DEF_MASK 0x1F
103 #define PROC_DEF_MASK 0x1E
105 typedef unsigned int uint;
107 #ifdef PROCANALYZER_BROADBAND
108 uint PROC_MASK = 0x00;
109 uint SYS_MASK = 0x00;
111 uint PROC_MASK = 0x1E;
112 uint SYS_MASK = 0x1F;
115 #define MAX(x,y) ((x>y)?x:y)
117 #define PROC_EVENT_NONE 0x00000000
118 #define PROC_EVENT_FORK 0x00000001
119 #define PROC_EVENT_EXEC 0x00000002
120 #ifdef PROCANALYZER_EXTENDER
121 #define RDK_LOG_ERROR 0
122 #define RDK_LOG_DEBUG 1
123 #define RDK_LOG_INFO 2
124 #define RDK_LOG_TRACE1 3
125 #define RDK_LOG get_proclog
126 #define EXTENDER_VENDOR_NAME_STR "vendor_name"
127 #define EXTENDER_MODEL_NAME_STR "model"
137 unsigned int prevTotalMajFaultsRaised;
138 unsigned long prevTotalUsedCPUTime;
139 unsigned long prevUserUsedCPUTime;
140 unsigned long prevSystemUsedCPUTime;
141 double prevTotalCPUTime_usec;
152 unsigned long long total;
153 unsigned long long idle;
164 map<unsigned int, struct stPrevData> prevData;
166 FILE* fp_selectedps = NULL;
167 FILE* fp_stat = NULL;
168 FILE* fp_dataOut = NULL;
170 #if defined PROCANALYZER_BROADBAND
171 #define CONFIG_PATH "/nvram"
172 #define LOG_PATH "/tmp"
173 #elif defined PROCANALYZER_EXTENDER
174 #define CONFIG_PATH "/usr/opensync/scripts"
175 #define LOG_PATH "/tmp"
177 #define CONFIG_PATH "/opt"
178 #define LOG_PATH "/opt/logs"
181 string outputDir = LOG_PATH
"/cpuprocanalyzer/";
182 string outputDynamicDir = LOG_PATH
"/cpuprocanalyzer/dynamic/";
184 long totalTimeElapsed_sec = 0;
186 list<string> exclude_process_list;
187 list<string> :: iterator it;
188 #ifdef PROCANALYZER_EXTENDER
191 void get_proclog(
int log_level,
const char * log_module ,
const char *format, ...)
197 else if(log_level==1){
200 else if(log_level==2){
203 else if(log_level==3){
204 printf(
"<TRACE1>\t");
207 printf(
"%s\t",log_module);
208 va_start(args, format);
209 vprintf(format, args);
215 int get_device_param(
char* param,
char* value)
217 char tmp_string[BUFF_SIZE_64] =
"\0";
218 char buf1[BUFF_SIZE_64] =
"\0";
219 sprintf(tmp_string,
" ovsh s AWLAN_Node %s -j | grep :",param);
220 FILE * fp = popen(tmp_string,
"r");
223 RDK_LOG(RDK_LOG_ERROR,
"LOG.RDK.CPUPROCANALYZER",
" popen failed.Failed to run the command.\n");
226 fgets(buf1, BUFF_SIZE_64, fp);
227 sscanf(buf1,
" \"%[^'\"''']\": \"%[^'\"']\"",tmp_string,value);
233 char* removespaces(
char *str);
234 int read_config_param(
const char *paramname,
const char *filename,
char *res)
236 FILE *fp = fopen(filename,
"r");
237 char tmp_string[128] = {0};
242 memset(tmp_string,0,128);
243 while(fgets(tmp_string,128,fp)!= NULL)
245 if(strstr(tmp_string,paramname))
247 tmp=removespaces(tmp_string);
250 strncpy(res,pch,BUFF_SIZE_64);
253 memset(tmp_string,0,128);
260 char* removespaces(
char *str)
263 while(str[i] != NULL)
291 while(!feof(fp) && fgetc(fp) !=
'(');
295 if(ch ==
')' || ferror(fp) || feof(fp))
break;
297 if(ch == 0 || ch ==
' ' || ch==
':' || ch ==
'\\' || ch ==
'/' || ch ==
'[' || ch ==
']' || ch ==
'{' || ch ==
'}' || ch ==
'(' || ch ==
')')
319 if(ferror(fp) || feof(fp))
break;
320 if(ch == 0) ch =
' ';
331 char*
GetValuesFromFile (
char *fname,
char *searchStr,
char *strValue,
unsigned int strValueLen)
333 char buf1[BUFF_SIZE_64] =
"\0";
334 char tmpStr[BUFF_SIZE_64] =
"\0";
335 char srch[BUFF_SIZE_64] =
"\0";
337 FILE *fp = fopen (fname,
"r" );
340 RDK_LOG(RDK_LOG_ERROR,
"LOG.RDK.CPUPROCANALYZER",
"fopen failed.Failed to read %s\n", searchStr);
344 memset(buf1, 0, BUFF_SIZE_64);
345 memset(tmpStr, 0, BUFF_SIZE_64);
347 while (fgets(buf1,BUFF_SIZE_64,fp)) {
349 if (strstr(buf1, searchStr)) {
350 memset (srch, 0,
sizeof(srch));
351 sprintf(srch,
"%s%%s", searchStr);
352 sscanf (buf1, srch, tmpStr);
353 strncpy (strValue, tmpStr, strValueLen-1);
355 if (strValue[strlen(strValue)] ==
'\n') {
356 strValue[strlen(strValue)] =
'\0';
365 #if defined PROCANALYZER_BROADBAND
368 string rfc_filename = outputDir +
"rfc_list.txt";
369 static const char filename[] =
"/tmp/rfc-current.json";
371 if(access(filename, R_OK))
373 RDK_LOG(RDK_LOG_INFO,
"LOG.RDK.CPUPROCANALYZER",
"rfc-current.json is not accessible.\n");
377 FILE *fp1 = fopen(rfc_filename.c_str(),
"a");
378 struct json_object *obj, *feature_obj, *feaCtrl_obj, *featArr_obj, *name_obj, *enable_obj;
380 obj = json_object_from_file(filename);
381 feaCtrl_obj = json_object_object_get(obj,
"featureControl");
382 feature_obj = json_object_object_get(feaCtrl_obj,
"features");
384 int arrlen = json_object_array_length(feature_obj);
388 for(
int i=0; i<arrlen; i++)
390 featArr_obj = json_object_array_get_idx(feature_obj, i);
391 name_obj = json_object_object_get(featArr_obj,
"name");
392 enable_obj = json_object_object_get(featArr_obj,
"enable");
393 fprintf(fp1,
"%s : %s\n", json_object_get_string(name_obj), json_object_get_string(enable_obj));
399 RDK_LOG(RDK_LOG_INFO,
"LOG.RDK.CPUPROCANALYZER",
"Cannot open RFC file.\n");
401 json_object_put(obj);
405 void GetNumOfClientsConnected(
unsigned int *cliCount)
410 fp = popen(
"dmcli eRT getv Device.Hosts.HostNumberOfEntries | grep value | awk '{print $5}'",
"r");
413 if(fgets(buf, 8, fp))
415 RDK_LOG(RDK_LOG_INFO,
"LOG.RDK.CPUPROCANALYZER",
"Number of Clients connected : %d\n", atoi(buf));
416 *cliCount = atoi(buf);
422 RDK_LOG(RDK_LOG_INFO,
"LOG.RDK.CPUPROCANALYZER",
"Popen failure\n");
431 char mfgname[BUFF_SIZE_16] =
"\0";
432 char devicename[BUFF_SIZE_16] =
"\0";
433 char buildname[BUFF_SIZE_64] =
"\0";
434 char name[100] =
"\0";
436 #if defined PROCANALYZER_BROADBAND
437 memset (mfgname, 0, BUFF_SIZE_16);
438 GetValuesFromFile (
"/etc/device.properties",
"MANUFACTURE=", mfgname,
sizeof(mfgname));
440 if (strncmp(mfgname,
"UNKNOWN",strlen(mfgname))) {
441 GetValuesFromFile (
"/etc/device.properties",
"MFG_NAME=", mfgname,
sizeof(mfgname));
444 memset(devicename,0,BUFF_SIZE_16);
445 GetValuesFromFile (
"/etc/device.properties",
"BOX_TYPE=", devicename,
sizeof(devicename));
446 #elif defined PROCANALYZER_EXTENDER
447 get_device_param(EXTENDER_VENDOR_NAME_STR,mfgname);
448 get_device_param(EXTENDER_MODEL_NAME_STR,devicename);
451 memset (mfgname, 0, BUFF_SIZE_16);
452 GetValuesFromFile (
"/etc/device.properties",
"MFG_NAME=", mfgname,
sizeof(mfgname));
454 memset(devicename,0,BUFF_SIZE_16);
455 GetValuesFromFile (
"/etc/device.properties",
"DEVICE_NAME=", devicename,
sizeof(devicename));
459 memset (name, 0,
sizeof(name));
460 snprintf (name, BUFF_SIZE_64,
"%s%s", mfgname, devicename);
462 memset(buildname, 0,
sizeof(buildname));
465 if (!strcmp(buildname,
"")) {
470 FILE *fpout = fopen(LOG_PATH
"/cpuprocanalyzer/deviceinfo.data",
"w");
473 RDK_LOG(RDK_LOG_ERROR,
"LOG.RDK.CPUPROCANALYZER",
"Could not open deviceInfo.data.\n");
476 fprintf(fpout,
"DEVICE_NAME:%s\nBUILD_NAME:%s\n ", name, buildname);
477 RDK_LOG(RDK_LOG_INFO,
"LOG.RDK.CPUPROCANALYZER",
"Device name %s and Build name %s written successfully.\n",name, buildname);
502 char s_comm[1000], c_state;
503 int d_ppid, d_pgrp, d_session, d_tty_nr, d_tpgid;
505 unsigned long lu_minflt, lu_cminflt, lu_majflt, lu_cmajflt, lu_utime, lu_stime;
506 long ld_cutime, ld_cstime, ld_priority, ld_nice, ld_num_threads, ld_itrealvalue;
507 unsigned long long llu_starttime;
508 unsigned long lu_vsize;
510 unsigned long lu_rsslim, lu_startcode, lu_endcode, lu_startstack, lu_kstkesp, lu_kstkeip, lu_signal, lu_blocked, lu_sigignore, lu_sigcatch, lu_wchan, lu_nswap, lu_cnswap;
511 int d_exit_signal, d_processor;
512 unsigned int u_rt_priority, u_policy;
513 unsigned long long llu_delayacct_blkio_ticks;
514 unsigned long lu_guest_time;
535 fscanf(fp_procStat,
"%d", &d_pid);
537 fscanf(fp_procStat,
" %c %d %d %d %d %d %u %lu %lu %lu %lu %lu %lu %ld %ld %ld %ld %ld %ld %llu %lu %ld %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %d %d %u %u %llu %lu %ld", &c_state, &d_ppid, &d_pgrp, &d_session, &d_tty_nr, &d_tpgid, &u_flags, &lu_minflt, &lu_cminflt, &lu_majflt, &lu_cmajflt, &lu_utime, &lu_stime, &ld_cutime, &ld_cstime, &ld_priority, &ld_nice, &ld_num_threads, &ld_itrealvalue, &llu_starttime, &lu_vsize, &ld_rss, &lu_rsslim, &lu_startcode, &lu_endcode, &lu_startstack, &lu_kstkesp, &lu_kstkeip, &lu_signal, &lu_blocked, &lu_sigignore, &lu_sigcatch, &lu_wchan, &lu_nswap, &lu_cnswap, &d_exit_signal, &d_processor, &u_rt_priority, &u_policy, &llu_delayacct_blkio_ticks, &lu_guest_time, &ld_cguest_time);
539 RDK_LOG(RDK_LOG_TRACE1,
"LOG.RDK.CPUPROCANALYZER",
"%s(%d): %d %s %c %d %d %d %d %d %u %lu %lu %lu %lu %lu %lu %ld %ld %ld %ld %ld %ld %llu %lu %ld %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %d %d %u %u %llu %lu %ld\n", __func__, __LINE__, d_pid, s_comm, c_state, d_ppid, d_pgrp, d_session, d_tty_nr, d_tpgid, u_flags, lu_minflt, lu_cminflt, lu_majflt, lu_cmajflt, lu_utime, lu_stime, ld_cutime, ld_cstime, ld_priority, ld_nice, ld_num_threads, ld_itrealvalue, llu_starttime, lu_vsize, ld_rss, lu_rsslim, lu_startcode, lu_endcode, lu_startstack, lu_kstkesp, lu_kstkeip, lu_signal, lu_blocked, lu_sigignore, lu_sigcatch, lu_wchan, lu_nswap, lu_cnswap, d_exit_signal, d_processor, u_rt_priority, u_policy, llu_delayacct_blkio_ticks, lu_guest_time, ld_cguest_time);
553 sprintf(outProcFilename,
"%s%d_%s/%d_%s.data", outputDynamicDir.c_str(), d_pid, s_comm,d_pid,s_comm);
557 sprintf(outProcFilename,
"%s%d_%s/%d_%s.data", outputDir.c_str(), d_pid, s_comm,d_pid, s_comm);
571 sprintf(outProcFilename,
"%s%d_%s/threads/%d_%s.data", outputDir.c_str(), ppid, pname, d_pid, s_comm);
583 *outTotalTime = lu_utime + lu_stime;
595 *outUserTime = lu_utime;
607 *outSystemTime = lu_stime;
619 *outTotalMjrFlts = lu_majflt;
634 timeinfo = localtime(&rawtime);
636 strftime(strTime,80,
"%Y-%m-%d %H:%M:%S",timeinfo);
649 void GetMemParams(
char* filename,
unsigned long* memParam,
char* param)
651 char line[128]= {
'\0'};
654 FILE* fp_status = fopen(filename,
"r");
657 while(!feof(fp_status))
659 fgets(line,
sizeof(line)-1, fp_status);
660 if(strncmp(line, param, strlen(param)) == 0)
662 stringstream ss(line);
663 for (
int idx=0; idx<2; idx++)
665 *memParam = atoi(str.c_str());
668 memset(line,0,
sizeof(line));
674 RDK_LOG(RDK_LOG_ERROR,
"LOG.RDK.CPUPROCANALYZER",
"%s(%d): ERROR opening the file: %s\n", __func__, __LINE__, filename);
696 if ((fp = fopen(
"/proc/loadavg",
"r")) == NULL)
698 RDK_LOG(RDK_LOG_ERROR,
"LOG.RDK.CPUPROCANALYZER",
"%s(%d): ERROR reading file: /proc/loadavg", __func__, __LINE__);
702 fscanf(fp,
"%f", loadavg);
718 char line[64]= {
'\0'};
721 unsigned long memTotal = 0;
722 unsigned long memFree = 0;
723 FILE* fp = fopen(
"/proc/meminfo",
"r");
728 fgets(line,
sizeof(line), fp);
729 if(strncmp(line,
"MemTotal:", 8) == 0)
731 stringstream ss(line);
732 for (
int idx=0; idx<2; idx++)
734 memTotal = atoi(str.c_str());
737 else if(strncmp(line,
"MemFree:", 7) == 0)
739 stringstream ss(line);
740 for (
int idx=0; idx<2; idx++)
742 memFree = atoi(str.c_str());
745 if(count == 2)
break;
746 memset(line,0,
sizeof(line));
748 *mem = memTotal - memFree;
753 RDK_LOG(RDK_LOG_ERROR,
"LOG.RDK.CPUPROCANALYZER",
"%s(%d): ERROR opening the file: /proc/meminfo\n", __func__, __LINE__);
768 unsigned long u, n, s, i, w, x, y, z;
770 FILE* fp = fopen(
"/proc/stat",
"r");
773 fgets(buf,
sizeof(buf), fp);
774 sscanf(buf,
"cpu %lu %lu %lu %lu %lu %lu %lu %lu", &u, &n, &s, &i, &w, &x, &y, &z);
775 currentCPUInfo.total = u + n + s + i + w + x + y + z;
776 currentCPUInfo.idle = i;
778 *idlePercent = (float)(100 * (currentCPUInfo.idle - prevCPUInfo.idle)/(currentCPUInfo.total - prevCPUInfo.total));
780 prevCPUInfo.total = currentCPUInfo.total;
781 prevCPUInfo.idle = currentCPUInfo.idle;
792 sprintf(tmp_string,
"cd ../../bin");
794 sprintf(tmp_string,
"busybox --list");
795 FILE * fp = popen( tmp_string,
"r" );
797 RDK_LOG(RDK_LOG_ERROR,
"LOG.RDK.CPUPROCANALYZER",
"Could not get busy box list\n");
800 char buf1[ BUFF_SIZE_64 ];
801 while(fgets(buf1,BUFF_SIZE_64,fp)!= NULL)
803 buf1[strlen(buf1) - 1] =
'\0';
804 exclude_process_list.push_back(
string(buf1));
805 memset(buf1,0,
sizeof(buf1));
820 FILE * fp = popen( filename,
"r" );
824 RDK_LOG(RDK_LOG_ERROR,
"LOG.RDK.CPUPROCANALYZER",
"Could not get FD Count\n");
828 fscanf(fp,
"%d" , FDCount);
842 char tmp_string[64] = {0};
843 snprintf(tmp_string,
sizeof(tmp_string),
"/proc/sys/fs/file-nr" );
844 FILE * fp = fopen( tmp_string,
"r" );
848 RDK_LOG(RDK_LOG_ERROR,
"LOG.RDK.CPUPROCANALYZER",
"Could not get FD Count System Level\n");
852 fscanf(fp,
"%d" , FDCountSystem);
863 uint bit_mask = 0x00;
864 RDK_LOG(RDK_LOG_INFO,
"LOG.RDK.CPUPROCANALYZER",
"Input param = %s, strlen(res) = %d, sizeof() = %d\n",
865 res, strlen(res),
sizeof(res));
867 char *newline = strchr( res,
'\n' );
873 char *token = strtok(res,
",");
876 RDK_LOG(RDK_LOG_INFO,
"LOG.RDK.CPUPROCANALYZER",
"Token = %s, Strlen = %d, sizeof() = %d\n", token, strlen(token),
sizeof(token));
877 if(strncmp(token,
"cpu",strlen(token)) == 0)
879 bit_mask |= CPU_MASK;
880 RDK_LOG(RDK_LOG_INFO,
"LOG.RDK.CPUPROCANALYZER",
"Cpu mask is set : %d\n", bit_mask);
882 else if(strncmp(token,
"memory",strlen(token)) == 0)
884 bit_mask |= MEMORY_MASK;
885 RDK_LOG(RDK_LOG_INFO,
"LOG.RDK.CPUPROCANALYZER",
"Memory mask is set : %d\n", bit_mask);
887 else if(strncmp(token,
"fd",strlen(token)) == 0)
889 bit_mask |= FDCOUNT_MASK;
890 RDK_LOG(RDK_LOG_INFO,
"LOG.RDK.CPUPROCANALYZER",
"FD mask is set : %d\n", bit_mask);
892 else if(strncmp(token,
"loadavg",strlen(token)) == 0)
894 bit_mask |= LOADAVG_MASK;
895 RDK_LOG(RDK_LOG_INFO,
"LOG.RDK.CPUPROCANALYZER",
"LoadAvg mask is set : %d\n", bit_mask);
897 else if(strncmp(token,
"thread",strlen(token)) == 0)
899 bit_mask |= THREADCOUNT_MASK;
900 RDK_LOG(RDK_LOG_INFO,
"LOG.RDK.CPUPROCANALYZER",
"Thread mask is set : %d\n", bit_mask);
902 else if(strncmp(token,
"cliconnected",strlen(token)) == 0)
904 bit_mask |= CLICOUNT_MASK;
905 RDK_LOG(RDK_LOG_INFO,
"LOG.RDK.CPUPROCANALYZER",
"Cli count mask is set : %d\n", bit_mask);
907 token = strtok(NULL,
",");
908 RDK_LOG(RDK_LOG_INFO,
"LOG.RDK.CPUPROCANALYZER",
"token is : %s\n", token);
925 char tmp_string[1024] = {0};
926 unsigned long vmStack=0;
927 unsigned long vmSize=0;
928 unsigned long vmRSS=0;
931 string searchstr =
".sh";
932 string searchstr_1 =
"kworker";
934 memset(tmp_string,0,1024);
935 (ppid != 0) ? sprintf(tmp_string,
"/proc/%d/task/%d/stat", ppid, procData->d_pid)
936 : sprintf(tmp_string,
"/proc/%d/stat", procData->d_pid);
938 fp_stat = fopen(tmp_string,
"r");
945 RDK_LOG(RDK_LOG_ERROR,
"LOG.RDK.CPUPROCANALYZER",
"Failed to open %s file\n", tmp_string);
952 for(it=exclude_process_list.begin();it!=exclude_process_list.end();it++)
955 if(strncmp(procData->s_comm,s.c_str(),MAX(strlen(procData->s_comm),strlen(s.c_str()))) == 0)
964 if(strlen(procData->s_comm) == 0)
970 for(it=exclude_process_list.begin();it!=exclude_process_list.end();it++)
973 if(strncmp(procData->s_comm,s.c_str(),MAX(strlen(procData->s_comm),strlen(s.c_str()))) == 0)
981 if(PROC_MASK & MEMORY_MASK)
983 (ppid != 0) ? sprintf(tmp_string,
"/proc/%d/task/%d/status", ppid, procData->d_pid)
984 : sprintf(tmp_string,
"/proc/%d/status", procData->d_pid);
989 if(PROC_MASK & FDCOUNT_MASK)
991 (ppid != 0) ? snprintf(tmp_string,
sizeof(tmp_string),
"ls /proc/%d/task/%d/fd | wc -l", ppid, procData->d_pid)
992 : snprintf(tmp_string,
sizeof(tmp_string),
"ls /proc/%d/fd | wc -l", procData->d_pid);
1006 sprintf(tmp_string,
"/proc/%d/cmdline", procData->d_pid);
1007 FILE* fp_cmd = fopen(tmp_string,
"r");
1010 fgets(tmp_string,
sizeof(tmp_string), fp_cmd);
1013 if ( (strncmp(tmp_string,
"/bin/sh",strlen(tmp_string)) == 0) ||
1014 (strncmp(tmp_string,
"sh",strlen(tmp_string)) ==0 ) ||
1015 (strncmp(tmp_string,
"-sh",strlen(tmp_string)) == 0) ||
1016 (strncmp(tmp_string,
"-bash",strlen(tmp_string)) == 0) ||
1017 (strncmp(tmp_string,
"/bin/bash",strlen(tmp_string)) == 0) )
1019 RDK_LOG(RDK_LOG_DEBUG,
"LOG.RDK.CPUPROCANALYZER",
"Rejecting process : %s\n", procData->s_comm);
1021 }
else if ( strstr(tmp_string,searchstr.c_str()) ) {
1022 sprintf(tmp_string,
"/proc/%d/comm", procData->d_pid);
1023 FILE* fp_cmd = fopen(tmp_string,
"r");
1026 fgets(tmp_string,
sizeof(tmp_string), fp_cmd);
1028 if( (strncmp(tmp_string,
"sh",strlen(tmp_string)) == 0) || (strncmp(tmp_string,
"bash",strlen(tmp_string)) == 0) )
1030 RDK_LOG(RDK_LOG_DEBUG,
"LOG.RDK.CPUPROCANALYZER",
"Rejecting process : %s\n", procData->s_comm);
1038 sprintf(tmp_string,
"/proc/%d/comm", procData->d_pid);
1039 fp_cmd = fopen(tmp_string,
"r");
1042 fgets(tmp_string,
sizeof(tmp_string), fp_cmd);
1044 if( strstr(tmp_string,searchstr_1.c_str()) )
1051 sprintf(tmp_string,
"%s/%d_%s/threads/", outputDir.c_str(), ppid, pname);
1057 sprintf(tmp_string,
"%s%d_%s", outputDir.c_str(), procData->d_pid, procData->s_comm);
1062 sprintf(tmp_string,
"%s%d_%s", outputDynamicDir.c_str(), procData->d_pid, procData->s_comm);
1066 mkdir(tmp_string, S_IRWXU | S_IRWXG | S_IRWXO);
1067 (ppid != 0) ? procData->
OutFilename(tmp_string, ppid, pname) : procData->
OutFilename(tmp_string,is_dynamic);
1068 fp_dataOut = fopen(tmp_string,
"a+");
1071 unsigned long currentTotalUsedCPUTime = 0;
1072 unsigned int currentTotalMajorFaultsRaised = 0;
1073 unsigned long currentUserUsedCPUTime = 0;
1074 unsigned long currentSystemUsedCPUTime = 0;
1075 struct timeval currentTime;
1076 double currentTime_usec, timeDiff_usec, cpuUseRaise, cpuUseRaise_User, cpuUseRaise_System;
1077 unsigned int majorFaultsRaise;
1083 gettimeofday(¤tTime, NULL);
1084 currentTime_usec = (currentTime.tv_sec*1000000.0)+currentTime.tv_usec;
1086 timeDiff_usec = currentTime_usec - prevData[procData->d_pid].prevTotalCPUTime_usec;
1087 if(timeDiff_usec == 0) timeDiff_usec = 1;
1088 cpuUseRaise = 100*(currentTotalUsedCPUTime - prevData[procData->d_pid].prevTotalUsedCPUTime)/(sysconf(_SC_CLK_TCK)*timeDiff_usec/1000000);
1089 cpuUseRaise_User = 100*(currentUserUsedCPUTime - prevData[procData->d_pid].prevUserUsedCPUTime)/(sysconf(_SC_CLK_TCK)*timeDiff_usec/1000000);
1090 cpuUseRaise_System = 100*(currentSystemUsedCPUTime - prevData[procData->d_pid].prevSystemUsedCPUTime)/(sysconf(_SC_CLK_TCK)*timeDiff_usec/1000000);
1092 RDK_LOG(RDK_LOG_TRACE1,
"LOG.RDK.CPUPROCANALYZER",
"%s(%d): %d %0.2lf %0.2lf %0.2lf %ld %ld %0.2lf %0.2lf\n", __func__, __LINE__, procData->d_pid, cpuUseRaise, cpuUseRaise_User, cpuUseRaise_System, currentTotalUsedCPUTime, prevData[procData->d_pid].prevTotalUsedCPUTime, sysconf(_SC_CLK_TCK), timeDiff_usec);
1093 majorFaultsRaise = (currentTotalMajorFaultsRaised - prevData[procData->d_pid].prevTotalMajFaultsRaised);
1095 prevData[procData->d_pid].prevTotalUsedCPUTime = currentTotalUsedCPUTime;
1096 prevData[procData->d_pid].prevUserUsedCPUTime = currentUserUsedCPUTime;
1097 prevData[procData->d_pid].prevSystemUsedCPUTime = currentSystemUsedCPUTime;
1098 prevData[procData->d_pid].prevTotalCPUTime_usec = currentTime_usec;
1099 prevData[procData->d_pid].prevTotalMajFaultsRaised = currentTotalMajorFaultsRaised;
1101 if(prevData[procData->d_pid].status != 1)
1103 sprintf(tmp_string,
"/proc/%d/cmdline", procData->d_pid);
1104 FILE* fp_cmd = fopen(tmp_string,
"r");
1110 if(telemetryOnly ==
false)
1112 fprintf(fp_dataOut,
"Command-Line : %s\n\n", tmp_string);
1115 fprintf(fp_dataOut,
"El-Time\tTimeStamp\t");
1116 if(PROC_MASK & CPU_MASK)
1118 fprintf(fp_dataOut,
"\tCPU%\tCPU%:U\tCPU%:S\tMjrFlts");
1120 if(PROC_MASK & MEMORY_MASK)
1122 fprintf(fp_dataOut,
"\tVmSize\tVmRSS\tVmStk");
1124 if(PROC_MASK & THREADCOUNT_MASK)
1126 fprintf(fp_dataOut,
"\tThreadCount");
1128 if(PROC_MASK & FDCOUNT_MASK)
1130 fprintf(fp_dataOut,
"\tFDCount");
1132 fprintf(fp_dataOut,
"\n");
1136 fprintf(fp_dataOut,
"El-Time\tTimeStamp\t\tCPU%\tCPU%:U\tCPU%:S\tMjrFlts\tVmSize\tVmRSS\tVmStk\tThreadCount\tFDCount\n");
1144 sprintf(tmp_string,
"/proc/%d/stat", procData->d_pid);
1145 FILE *f = fopen(tmp_string,
"r");
1147 RDK_LOG(RDK_LOG_ERROR,
"LOG.RDK.CPUPROCANALYZER",
"Failed to open /proc/%d/stat/ of dynamic process\n", procData->d_pid);
1151 fscanf(f,
" %c %d %d %d %d %d %u %lu %lu %lu %lu %lu %lu %ld %ld %ld %ld %ld %ld %llu %lu %ld %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %d %d %u %u %llu %lu %ld", &procData->c_state, &procData->d_ppid, &procData->d_pgrp, &procData->d_session, &procData->d_tty_nr, &procData->d_tpgid, &procData->u_flags, &procData->lu_minflt, &procData->lu_cminflt, &procData->lu_majflt, &procData->lu_cmajflt, &procData->lu_utime, &procData->lu_stime, &procData->ld_cutime, &procData->ld_cstime, &procData->ld_priority, &procData->ld_nice, &procData->ld_num_threads, &procData->ld_itrealvalue, &procData->llu_starttime, &procData->lu_vsize, &procData->ld_rss, &procData->lu_rsslim, &procData->lu_startcode, &procData->lu_endcode, &procData->lu_startstack, &procData->lu_kstkesp, &procData->lu_kstkeip, &procData->lu_signal, &procData->lu_blocked, &procData->lu_sigignore, &procData->lu_sigcatch, &procData->lu_wchan, &procData->lu_nswap, &procData->lu_cnswap, &procData->d_exit_signal, &procData->d_processor, &procData->u_rt_priority, &procData->u_policy, &procData->llu_delayacct_blkio_ticks, &procData->lu_guest_time,&procData->ld_cguest_time);
1152 cpuUseRaise= procData->lu_utime + procData->lu_stime;
1153 cpuUseRaise_User = procData->lu_utime;
1154 cpuUseRaise_System = procData->lu_stime;
1156 if(telemetryOnly ==
false)
1160 fprintf(fp_dataOut,
"%ld\t%s", totalTimeElapsed_sec,
GetCurTimeStamp());
1161 if(PROC_MASK & CPU_MASK)
1163 fprintf(fp_dataOut,
"\t%0.2lf\t%0.2lf\t%0.2lf\t%d", cpuUseRaise, cpuUseRaise_User, cpuUseRaise_System, majorFaultsRaise);
1164 RDK_LOG(RDK_LOG_TRACE1,
"LOG.RDK.CPUPROCANALYZER",
"%s(%d): %0.2lf %0.2lf %0.2lf %d \n", __func__, __LINE__, cpuUseRaise, cpuUseRaise_User, cpuUseRaise_System, majorFaultsRaise);
1166 if(PROC_MASK & MEMORY_MASK)
1168 fprintf(fp_dataOut,
"\t%ld\t%ld\t%ld", vmSize, vmRSS, vmStack);
1169 RDK_LOG(RDK_LOG_TRACE1,
"LOG.RDK.CPUPROCANALYZER",
"%s(%d): %ld %ld %ld \n", __func__, __LINE__, vmSize, vmRSS, vmStack);
1171 if(PROC_MASK & THREADCOUNT_MASK)
1173 fprintf(fp_dataOut,
"\t%ld\t", ((procData->ld_num_threads)-1));
1174 RDK_LOG(RDK_LOG_TRACE1,
"LOG.RDK.CPUPROCANALYZER",
"%s(%d): %ld \n", __func__, __LINE__, ((procData->ld_num_threads)-1));
1176 if(PROC_MASK & FDCOUNT_MASK)
1178 fprintf(fp_dataOut,
"\t%d\t", FDCount);
1179 RDK_LOG(RDK_LOG_TRACE1,
"LOG.RDK.CPUPROCANALYZER",
"%s(%d): %d \n", __func__, __LINE__, FDCount);
1181 fprintf(fp_dataOut,
"\n");
1185 fprintf(fp_dataOut,
"%ld\t%s\t%0.2lf\t%0.2lf\t%0.2lf\t%d\t%ld\t%ld\t%ld\t%ld\t\t%d\t\n", totalTimeElapsed_sec,
GetCurTimeStamp(), cpuUseRaise, cpuUseRaise_User, cpuUseRaise_System, majorFaultsRaise, vmSize, vmRSS, vmStack, ((procData->ld_num_threads)-1), FDCount);
1186 RDK_LOG(RDK_LOG_TRACE1,
"LOG.RDK.CPUPROCANALYZER",
"%s(%d): %0.2lf %0.2lf %0.2lf %d %ld %ld %ld %ld %d \n", __func__, __LINE__, cpuUseRaise, cpuUseRaise_User, cpuUseRaise_System, majorFaultsRaise, vmSize, vmRSS, vmStack, ((procData->ld_num_threads)-1), FDCount);
1193 if(prevData[procData->d_pid].status)
1195 if(telemetryOnly ==
false)
1199 fprintf(fp_dataOut,
"%ld\t%s", totalTimeElapsed_sec,
GetCurTimeStamp());
1200 if(PROC_MASK & CPU_MASK)
1202 fprintf(fp_dataOut,
"\t%0.2lf\t%0.2lf\t%0.2lf\t%d", cpuUseRaise, cpuUseRaise_User, cpuUseRaise_System, majorFaultsRaise);
1203 RDK_LOG(RDK_LOG_TRACE1,
"LOG.RDK.CPUPROCANALYZER",
"%s(%d): %0.2lf %0.2lf %0.2lf %d \n", __func__, __LINE__, cpuUseRaise, cpuUseRaise_User, cpuUseRaise_System, majorFaultsRaise);
1205 if(PROC_MASK & MEMORY_MASK)
1207 fprintf(fp_dataOut,
"\t%ld\t%ld\t%ld", vmSize, vmRSS, vmStack);
1208 RDK_LOG(RDK_LOG_TRACE1,
"LOG.RDK.CPUPROCANALYZER",
"%s(%d): %ld %ld %ld \n", __func__, __LINE__, vmSize, vmRSS, vmStack);
1210 if(PROC_MASK & THREADCOUNT_MASK)
1212 fprintf(fp_dataOut,
"\t%ld\t", ((procData->ld_num_threads)-1));
1213 RDK_LOG(RDK_LOG_TRACE1,
"LOG.RDK.CPUPROCANALYZER",
"%s(%d): %ld \n", __func__, __LINE__, ((procData->ld_num_threads)-1));
1215 if(PROC_MASK & FDCOUNT_MASK)
1217 fprintf(fp_dataOut,
"\t%d\t", FDCount);
1218 RDK_LOG(RDK_LOG_TRACE1,
"LOG.RDK.CPUPROCANALYZER",
"%s(%d): %d \n", __func__, __LINE__, FDCount);
1220 fprintf(fp_dataOut,
"\n");
1224 fprintf(fp_dataOut,
"%ld\t%s\t%0.2lf\t%0.2lf\t%0.2lf\t%d\t%ld\t%ld\t%ld\t%ld\t\t%d\t\n", totalTimeElapsed_sec,
GetCurTimeStamp(), cpuUseRaise, cpuUseRaise_User, cpuUseRaise_System, majorFaultsRaise, vmSize, vmRSS, vmStack, ((procData->ld_num_threads)-1), FDCount);
1225 RDK_LOG(RDK_LOG_TRACE1,
"LOG.RDK.CPUPROCANALYZER",
"%s(%d): %0.2lf %0.2lf %0.2lf %d %ld %ld %ld %ld %d \n", __func__, __LINE__, cpuUseRaise, cpuUseRaise_User, cpuUseRaise_System, majorFaultsRaise, vmSize, vmRSS, vmStack, ((procData->ld_num_threads)-1), FDCount);
1231 #if defined PROCANALYZER_BROADBAND
1235 char eventName[32]={
'\0'};
1236 char telemetry_buf[128] = {
'\0'};
1237 snprintf(eventName,
sizeof(eventName),
"CPA_INFO_%s", procData->s_comm);
1238 snprintf(telemetry_buf,
sizeof(telemetry_buf),
"%ld,%0.2lf,%d,%d", vmRSS, cpuUseRaise, FDCount, procData->ld_num_threads-1);
1239 RDK_LOG(RDK_LOG_INFO,
"LOG.RDK.CPUPROCANALYZER",
"Event : %s, Value : %s\n", eventName, telemetry_buf);
1240 t2_event_s(eventName, telemetry_buf);
1246 if(telemetryOnly ==
false)
1248 RDK_LOG(RDK_LOG_ERROR,
"LOG.RDK.CPUPROCANALYZER",
"%s(%d): ERROR opening the file: %s\n", __func__, __LINE__, tmp_string);
1253 prevData[procData->d_pid].status = 1;
1257 static volatile bool need_exit =
false;
1263 static int netlink_connect()
1267 struct sockaddr_nl nl_sockaddr;
1269 netlink_sock = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_CONNECTOR);
1270 if (netlink_sock == -1) {
1271 RDK_LOG(RDK_LOG_ERROR,
"LOG.RDK.CPUPROCANALYZER",
"Failed to create socket. Error code = %d \n", errno);
1275 nl_sockaddr.nl_family = AF_NETLINK;
1276 nl_sockaddr.nl_groups = CN_IDX_PROC;
1277 nl_sockaddr.nl_pid = getpid();
1279 rc = bind(netlink_sock, (
struct sockaddr *)&nl_sockaddr,
sizeof(nl_sockaddr));
1281 RDK_LOG(RDK_LOG_ERROR,
"LOG.RDK.CPUPROCANALYZER",
"bind operation failed, closing the socket\n");
1282 close(netlink_sock);
1286 return netlink_sock;
1292 static int subscribe_proc_events(
int netlink_sock,
bool enable)
1295 struct __attribute__ ((aligned(NLMSG_ALIGNTO))) {
1296 struct nlmsghdr nl_header;
1297 struct __attribute__ ((__packed__)) {
1298 struct cn_msg cn_message;
1299 enum proc_cn_mcast_op cn_mcast;
1304 memset(&netlink_msg, 0,
sizeof(netlink_msg));
1305 netlink_msg.nl_header.nlmsg_len =
sizeof(netlink_msg);
1306 netlink_msg.nl_header.nlmsg_pid = getpid();
1307 netlink_msg.nl_header.nlmsg_type = NLMSG_DONE;
1309 netlink_msg.cn_message.id.idx = CN_IDX_PROC;
1310 netlink_msg.cn_message.id.val = CN_VAL_PROC;
1311 netlink_msg.cn_message.len =
sizeof(
enum proc_cn_mcast_op);
1313 netlink_msg.cn_mcast = enable ? PROC_CN_MCAST_LISTEN : PROC_CN_MCAST_IGNORE;
1315 retCode = send(netlink_sock, &netlink_msg,
sizeof(netlink_msg), 0);
1316 if (retCode == -1) {
1317 RDK_LOG(RDK_LOG_ERROR,
"LOG.RDK.CPUPROCANALYZER",
"netlink send failed\n");
1328 static int handle_process_events(
int netlink_sock)
1330 RDK_LOG(RDK_LOG_DEBUG,
"LOG.RDK.CPUPROCANALYZER",
" handle proc ev entered \n");
1334 struct __attribute__ ((aligned(NLMSG_ALIGNTO))) {
1335 struct nlmsghdr nl_header;
1336 struct __attribute__ ((__packed__)) {
1337 struct cn_msg cn_message;
1338 struct proc_event process_event;
1343 while (!need_exit) {
1344 retCode = recv(netlink_sock, &netlink_msg,
sizeof(netlink_msg), 0);
1345 memset(&dProcData,0,
sizeof(dProcData));
1349 }
else if (retCode == -1) {
1350 if (errno == EINTR)
continue;
1351 RDK_LOG(RDK_LOG_ERROR,
"LOG.RDK.CPUPROCANALYZER",
"netlink recv failed \n");
1354 switch (netlink_msg.process_event.what) {
1355 case PROC_EVENT_NONE:
1356 RDK_LOG(RDK_LOG_DEBUG,
"LOG.RDK.CPUPROCANALYZER",
"Setting Multicast listen success.\n");
1359 case PROC_EVENT_FORK:
1360 RDK_LOG(RDK_LOG_INFO,
"LOG.RDK.CPUPROCANALYZER",
"Forked new proc : form parent TID=%d PID=%d => child TID=%d PID=%d\n",
1361 netlink_msg.process_event.event_data.fork.parent_pid,
1362 netlink_msg.process_event.event_data.fork.parent_tgid,
1363 netlink_msg.process_event.event_data.fork.child_pid,
1364 netlink_msg.process_event.event_data.fork.child_tgid);
1365 dProcData.d_pid = netlink_msg.process_event.event_data.fork.child_pid;
1366 pthread_mutex_lock(&mtx);
1368 pthread_mutex_unlock(&mtx);
1371 case PROC_EVENT_EXEC:
1372 RDK_LOG(RDK_LOG_INFO,
"LOG.RDK.CPUPROCANALYZER",
"Exec Proc: TID=%d PID=%d\n",
1373 netlink_msg.process_event.event_data.exec.process_pid,
1374 netlink_msg.process_event.event_data.exec.process_tgid);
1377 RDK_LOG(RDK_LOG_INFO,
"LOG.RDK.CPUPROCANALYZER",
"unhandled nelink proc event\n");
1389 static void *handle_proc_ev_thread(
void *arg )
1391 int netlink_sock = netlink_connect();
1392 if(netlink_sock == -1)
1394 RDK_LOG(RDK_LOG_ERROR,
"LOG.RDK.CPUPROCANALYZER",
"Failed to create netlink socket \n");
1397 int rc = subscribe_proc_events(netlink_sock,
true);
1400 RDK_LOG(RDK_LOG_ERROR,
"LOG.RDK.CPUPROCANALYZER",
" Setting of event listen failed \n");
1401 close(netlink_sock);
1404 rc = handle_process_events(netlink_sock);
1407 RDK_LOG(RDK_LOG_ERROR,
"LOG.RDK.CPUPROCANALYZER",
"Subscription request failed\n");
1413 int checkifdigit(
char* ch,
int size)
1432 bool CheckMemLimit (
int itr,
unsigned long memLimit)
1434 char buf1[BUFF_SIZE_64]={0};
1435 static int prev_mem=0;
1438 char tmp_filename[128]={0};
1439 sprintf(tmp_filename,
"du -s %s | awk '{print$1}'",outputDir.c_str());
1441 FILE * fp = popen(tmp_filename,
"r");
1445 RDK_LOG(RDK_LOG_ERROR,
"LOG.RDK.CPUPROCANALYZER",
"%s(%d): popen failed.Failed to read tmp details.\n", __func__, __LINE__);
1449 memset(buf1, 0, BUFF_SIZE_64);
1450 fgets(buf1, BUFF_SIZE_64, fp);
1456 size_diff=(cur_mem - prev_mem);
1458 RDK_LOG(RDK_LOG_INFO,
"LOG.RDK.CPUPROCANALYZER",
"Mem Limits curr : %d | prev : %d | diff : %d\n", cur_mem, prev_mem, size_diff);
1461 if ((cur_mem + size_diff) >= memLimit)
1474 const char* pDebugConfig = NULL;
1475 const char* pEnvConfig = NULL;
1476 const char* DEBUG_ACTUAL_PATH =
"/etc/debug.ini";
1477 const char* ENV_ACTUAL_PATH =
"/etc/procanalyzerconfig.ini";
1478 const char* ENV_OVERRIDE_PATH = CONFIG_PATH
"/procanalyzerconfig.ini";
1479 const char* DEBUG_OVERRIDE_PATH = CONFIG_PATH
"/debug.ini";
1480 const char* PROCESSES_LIST_PATH = CONFIG_PATH
"/processes.list";
1484 int returncode = EXIT_SUCCESS;
1485 string dynamicFolder;
1487 pthread_t process_handler_tid;
1488 #ifndef PROCANALYZER_EXTENDER
1489 if (access(DEBUG_OVERRIDE_PATH, F_OK) != -1)
1490 pDebugConfig = DEBUG_OVERRIDE_PATH;
1492 pDebugConfig = DEBUG_ACTUAL_PATH;
1496 if (access(ENV_OVERRIDE_PATH, F_OK) != -1)
1497 pEnvConfig = ENV_OVERRIDE_PATH;
1499 pEnvConfig = ENV_ACTUAL_PATH;
1501 #if defined PROCANALYZER_BROADBAND
1502 t2_init(
"cpuprocanalyzer");
1505 char tmp_string[256]={0};
1506 unsigned long timeToRun_sec ;
1507 unsigned int sleepInterval_ms;
1508 unsigned long memoryLimit;
1509 bool monitorAllProcess =
false;
1510 bool enableDynamic =
true;
1511 bool telemetryOnly =
false;
1512 string grepProcesses;
1516 string ps_filename = outputDir +
"selectedps.list";
1518 ((env=read_config_param(
"FEATURE.CPUPROCANALYZER.SleepInterval",pEnvConfig,res)) == 0) ? sleepInterval_ms =
SLEEP_SECS*1000 : sleepInterval_ms = strtol(res,&ptr,10)*1000;
1520 ((env=read_config_param(
"FEATURE.CPUPROCANALYZER.TimeToRun",pEnvConfig,res)) == 0) ? timeToRun_sec =
TIME_TO_RUN_SECS : timeToRun_sec = strtol(res,&ptr,10);
1522 ((env=read_config_param(
"FEATURE.CPUPROCANALYZER.DynamicProcess",pEnvConfig,res)) == 0) ? enableDynamic = DEFAULT_DYNAMIC : enableDynamic = res[0] -
'0';
1524 ((env=read_config_param(
"FEATURE.CPUPROCANALYZER.MonitorAllProcess",pEnvConfig,res)) == 0) ? monitorAllProcess = MONITOR_ALL_PROC_DEF : monitorAllProcess = res[0] -
'0';
1526 ((env=read_config_param(
"FEATURE.CPUPROCANALYZER.TelemetryOnly",pEnvConfig,res)) == 0) ? telemetryOnly = TELEMETRY_ONLY_DEF : telemetryOnly = res[0] -
'0';
1528 ((env=read_config_param(
"FEATURE.CPUPROCANALYZER.MemoryLimit",pEnvConfig,res)) == 0) ? memoryLimit = DEFAULT_MEM_THRESHOLD : memoryLimit = strtol(res,&ptr,10);
1529 RDK_LOG(RDK_LOG_INFO,
"LOG.RDK.CPUPROCANALYZER",
"%s(%d):\nSleep Interval(secs): %d\nTime To Run(secs): %ld\n", __func__, __LINE__, sleepInterval_ms/1000, timeToRun_sec);
1530 RDK_LOG(RDK_LOG_INFO,
"LOG.RDK.CPUPROCANALYZER",
"Monitor All Process = %d, MemLimit = %ld\nDynamic = %d\ntelemetryOnly = %d\n", monitorAllProcess, memoryLimit, enableDynamic, telemetryOnly);
1532 #if defined PROCANALYZER_BROADBAND
1534 if((read_config_param(
"FEATURE.CPUPROCANALYZER.SystemStatsToMonitor",pEnvConfig,res)) == 0)
1536 RDK_LOG(RDK_LOG_INFO,
"LOG.RDK.CPUPROCANALYZER",
"SYSStats config absent, res value = %s\n", res);
1537 SYS_MASK = SYS_DEF_MASK;
1538 RDK_LOG(RDK_LOG_INFO,
"LOG.RDK.CPUPROCANALYZER",
"SYSStats config absent, set to DEF MASK = %d\n", SYS_MASK);
1542 RDK_LOG(RDK_LOG_INFO,
"LOG.RDK.CPUPROCANALYZER",
"SYSStats config present, res value = %s\n", res);
1544 RDK_LOG(RDK_LOG_INFO,
"LOG.RDK.CPUPROCANALYZER",
"SYS MASK is set\n");
1548 if((read_config_param(
"FEATURE.CPUPROCANALYZER.ProcessStatsToMonitor",pEnvConfig,res)) == 0)
1550 RDK_LOG(RDK_LOG_INFO,
"LOG.RDK.CPUPROCANALYZER",
"PROCStats config absent, res value = %s\n", res);
1551 PROC_MASK = PROC_DEF_MASK;
1552 RDK_LOG(RDK_LOG_INFO,
"LOG.RDK.CPUPROCANALYZER",
"PROCStats Config absent, set to DEF MASK = %d\n", PROC_MASK);
1556 RDK_LOG(RDK_LOG_INFO,
"LOG.RDK.CPUPROCANALYZER",
"PROCStats config present, res value = %s\n", res);
1558 RDK_LOG(RDK_LOG_INFO,
"LOG.RDK.CPUPROCANALYZER",
"PROC MASK is set\n");
1563 sprintf(tmp_string,
"cd %s && rm -rf *", outputDir.c_str());
1565 RDK_LOG(RDK_LOG_DEBUG,
"LOG.RDK.CPUPROCANALYZER",
"Create this directory always: %s\n", tmp_string);
1566 mkdir(outputDir.c_str(), S_IRWXU | S_IRWXG | S_IRWXO);
1567 if(telemetryOnly ==
false)
1569 mkdir(outputDynamicDir.c_str(), S_IRWXU | S_IRWXG | S_IRWXO);
1570 RDK_LOG(RDK_LOG_DEBUG,
"LOG.RDK.CPUPROCANALYZER",
"Creating if telemetryOnly false, outputDynamicDir.c_str()\n");
1575 #if defined PROCANALYZER_BROADBAND
1576 if(telemetryOnly ==
false)
1582 char tmpstring[1024],tmpstring1[128];
1583 char buf1[64],buf2[64];
1586 unsigned long long startTime_sec = time(NULL);
1587 unsigned long long currentTime_sec = time(NULL);
1588 unsigned long timeElapsed_sec = 0;
1589 bool terminate =
false;
1592 unsigned int cliCount = 0;
1594 unsigned long usedMemory;
1596 bool firstIter =
true;
1597 bool monitorSysLevel =
false;
1598 int FDCountSystem=0;
1602 returncode = pthread_create(&process_handler_tid,NULL,handle_proc_ev_thread,NULL);
1604 if (returncode == -1) {
1605 returncode = EXIT_FAILURE;
1606 RDK_LOG(RDK_LOG_ERROR,
"LOG.RDK.CPUPROCANALYZER",
"Dynamic process thread creation failed \n");
1610 while (iteration++, !terminate && CheckMemLimit (iteration,memoryLimit))
1614 RDK_LOG(RDK_LOG_INFO,
"LOG.RDK.CPUPROCANALYZER",
"Inside while.....\n");
1615 if(telemetryOnly ==
false)
1619 RDK_LOG(RDK_LOG_INFO,
"LOG.RDK.CPUPROCANALYZER",
"SYS MASK NOT NULL, %d\n", SYS_MASK);
1620 if(SYS_MASK & CPU_MASK)
1623 RDK_LOG(RDK_LOG_INFO,
"LOG.RDK.CPUPROCANALYZER",
"%s(%d): Idle: %0.2f\n", __func__, __LINE__, idlePercent);
1625 if(SYS_MASK & MEMORY_MASK)
1628 RDK_LOG(RDK_LOG_INFO,
"LOG.RDK.CPUPROCANALYZER",
"%s(%d): Mem: %ld\n", __func__, __LINE__, usedMemory);
1630 if(SYS_MASK & FDCOUNT_MASK)
1633 RDK_LOG(RDK_LOG_INFO,
"LOG.RDK.CPUPROCANALYZER",
"%s(%d): FDCountSystem: %d\n", __func__, __LINE__, FDCountSystem);
1635 if(SYS_MASK & LOADAVG_MASK)
1638 RDK_LOG(RDK_LOG_INFO,
"LOG.RDK.CPUPROCANALYZER",
"%s(%d): Load: %0.2f\n", __func__, __LINE__, loadAvg);
1640 if(SYS_MASK & CLICOUNT_MASK)
1642 GetNumOfClientsConnected(&cliCount);
1643 RDK_LOG(RDK_LOG_INFO,
"LOG.RDK.CPUPROCANALYZER",
"%s(%d): ClientCount: %d\n", __func__, __LINE__, cliCount);
1648 RDK_LOG(RDK_LOG_DEBUG,
"LOG.RDK.CPUPROCANALYZER",
"DEFAULT MASK is set\n");
1653 GetNumOfClientsConnected(&cliCount);
1654 RDK_LOG(RDK_LOG_INFO,
"LOG.RDK.CPUPROCANALYZER",
"%s(%d): Load: %0.2f Mem: %ld \
1655 Idle: %0.2f FDCountSystem: %d ClientCount = %d\n",
1656 __func__, __LINE__, loadAvg, usedMemory, idlePercent,
1657 FDCountSystem, cliCount);
1660 fp = fopen(LOG_PATH
"/cpuprocanalyzer/loadandmem.data",
"a+");
1666 RDK_LOG(RDK_LOG_INFO,
"LOG.RDK.CPUPROCANALYZER",
"First Iter = %d\n", firstIter);
1669 RDK_LOG(RDK_LOG_INFO,
"LOG.RDK.CPUPROCANALYZER",
"SYS_MASK NOT NULL, write data to loadandmem file\n");
1670 fprintf(fp,
"TimeStamp\t\t");
1671 if(SYS_MASK & LOADAVG_MASK)
1673 fprintf(fp,
"LoadAvg\t");
1675 if(SYS_MASK & MEMORY_MASK)
1677 fprintf(fp,
"UsedMem\t");
1679 if(SYS_MASK & CPU_MASK)
1681 fprintf(fp,
"Idle%\t");
1683 if(SYS_MASK & FDCOUNT_MASK)
1685 fprintf(fp,
"FDCountSystem\t");
1687 if(SYS_MASK & CLICOUNT_MASK)
1689 fprintf(fp,
"ClientCount\t");
1695 fprintf(fp,
"TimeStamp\t\tLoadAvg\tUsedMem\tIdle%\tFDCountSystem\tClientCount\n");
1703 RDK_LOG(RDK_LOG_INFO,
"LOG.RDK.CPUPROCANALYZER",
"Start printing values to loadandmem file\n");
1705 if(SYS_MASK & LOADAVG_MASK)
1707 fprintf(fp,
"\t%0.2f", loadAvg);
1709 if(SYS_MASK & MEMORY_MASK)
1711 fprintf(fp,
"\t%ld", usedMemory);
1713 if(SYS_MASK & CPU_MASK)
1715 fprintf(fp,
"\t%0.2f", idlePercent);
1717 if(SYS_MASK & FDCOUNT_MASK)
1719 fprintf(fp,
"\t%d", FDCountSystem);
1721 if(SYS_MASK & CLICOUNT_MASK)
1723 fprintf(fp,
"\t\t%d", cliCount);
1730 loadAvg, usedMemory, idlePercent, FDCountSystem, cliCount);
1735 RDK_LOG(RDK_LOG_ERROR,
"LOG.RDK.CPUPROCANALYZER",
"%s(%d):ERROR opening loadandmem.data file\n", __func__, __LINE__);
1738 if(monitorAllProcess ==
true)
1741 memset (tstr, 0,
sizeof(tstr));
1742 RDK_LOG(RDK_LOG_INFO,
"LOG.RDK.CPUPROCANALYZER",
"Monitoring all process...\n");
1743 sprintf (tstr,
"ps -w > %s", ps_filename.c_str());
1744 if (system(tstr) == -1)
1746 RDK_LOG(RDK_LOG_ERROR,
"LOG.RDK.CPUPROCANALYZER",
"%s(%d): FAILED ps command\n", __func__, __LINE__);
1747 RDK_LOG(RDK_LOG_ERROR,
"LOG.RDK.CPUPROCANALYZER",
"%s(%d): Monitoring at system level only...\n", __func__, __LINE__);
1751 else if(access(PROCESSES_LIST_PATH, F_OK) != -1)
1753 FILE* ptr1=fopen(ps_filename.c_str(),
"w");
1759 fp = fopen(PROCESSES_LIST_PATH,
"r");
1762 RDK_LOG(RDK_LOG_INFO,
"LOG.RDK.CPUPROCANALYZER",
"Read from process list and populate Selected ps file\n");
1765 while (fgets(buf1,64,fp)!= NULL)
1767 sprintf(tmpstring1,
"pidof %s",buf1);
1768 FILE *ptr2 = popen(tmpstring1,
"r");
1771 while (fscanf(ptr2,
"%s",buf2) != EOF)
1773 fprintf(ptr1,
"%s\n",buf2);
1778 RDK_LOG(RDK_LOG_INFO,
"LOG.RDK.CPUPROCANALYZER",
"%s(%d): Popen failure for process, %s\n", __func__, __LINE__, buf1);
1784 RDK_LOG(RDK_LOG_INFO,
"LOG.RDK.CPUPROCANALYZER",
"%s(%d): Process file open failure\n", __func__, __LINE__);
1790 if(telemetryOnly ==
false)
1792 RDK_LOG(RDK_LOG_ERROR,
"LOG.RDK.CPUPROCANALYZER",
" Error opening the file: %s",ps_filename.c_str());
1799 RDK_LOG(RDK_LOG_INFO,
"LOG.RDK.CPUPROCANALYZER",
"%s(%d): Monitoring at system level only...\n", __func__, __LINE__);
1800 monitorSysLevel =
true;
1803 if(!monitorSysLevel)
1805 fp_selectedps = fopen(ps_filename.c_str(),
"r");
1808 while(!feof(fp_selectedps))
1810 fgets(tmp_string,
LINE_LIMIT, fp_selectedps);
1811 if(ferror(fp_selectedps) || feof(fp_selectedps))
1813 sscanf(tmp_string,
"%d", &procData.d_pid);
1814 pthread_mutex_lock(&mtx);
1815 RDK_LOG(RDK_LOG_INFO,
"LOG.RDK.CPUPROCANALYZER",
"Calling LogProcData\n");
1818 pthread_mutex_unlock(&mtx);
1822 pthread_mutex_unlock(&mtx);
1823 if(telemetryOnly ==
false)
1825 sprintf(tmp_string,
"ls /proc/%d/task/ > %s%d_%s/threads.list", procData.d_pid, outputDir.c_str(), procData.d_pid, procData.s_comm);
1827 sprintf(tmp_string,
"%s%d_%s/threads.list", outputDir.c_str(), procData.d_pid, procData.s_comm);
1828 FILE* fp_thread_list = fopen(tmp_string,
"r");
1831 while(!feof(fp_thread_list))
1833 fgets(tmp_string,
LINE_LIMIT, fp_thread_list);
1834 if( ferror(fp_thread_list) || feof(fp_thread_list) )
1836 sscanf(tmp_string,
"%d", &threadData.d_pid);
1837 if(threadData.d_pid != procData.d_pid)
1839 pthread_mutex_lock(&mtx);
1840 LogProcData(&threadData, procData.d_pid, procData.s_comm);
1841 pthread_mutex_unlock(&mtx);
1844 fclose(fp_thread_list);
1846 memset(tmp_string,0,
sizeof(tmp_string));
1849 fclose(fp_selectedps);
1850 RDK_LOG(RDK_LOG_INFO,
"LOG.RDK.CPUPROCANALYZER",
"MAP erase logic\n");
1851 map<unsigned int, struct stPrevData>::iterator it = prevData.begin();
1852 while(it != prevData.end())
1855 if(preData.status == 0)
1857 RDK_LOG(RDK_LOG_ERROR,
"LOG.RDK.CPUPROCANALYZER",
"%s(%d): ***Removing %d from map***\n", __func__, __LINE__, it->first);
1867 if(telemetryOnly ==
false)
1869 RDK_LOG(RDK_LOG_ERROR,
"LOG.RDK.CPUPROCANALYZER",
"%s(%d): ERROR opening the file: %s\n", __func__, __LINE__, ps_filename.c_str());
1873 RDK_LOG(RDK_LOG_INFO,
"LOG.RDK.CPUPROCANALYZER",
"[%d]No. of process monitored : %d\n\n", iteration, pCount);
1879 RDK_LOG(RDK_LOG_INFO,
"LOG.RDK.CPUPROCANALYZER",
"Check time to run...\n");
1880 currentTime_sec = time(NULL);
1881 timeElapsed_sec = difftime(currentTime_sec, startTime_sec);
1882 if(timeElapsed_sec >= timeToRun_sec)
1884 RDK_LOG(RDK_LOG_INFO,
"LOG.RDK.CPUPROCANALYZER",
"Time elapsed, Terminate = true\n");
1891 RDK_LOG(RDK_LOG_INFO,
"LOG.RDK.CPUPROCANALYZER",
"Terminate NOT TRUE, sleep\n");
1892 usleep(sleepInterval_ms*1000);
1893 totalTimeElapsed_sec += sleepInterval_ms/1000;
1897 #if defined PROCANALYZER_BROADBAND
1898 RDK_LOG(RDK_LOG_INFO,
"LOG.RDK.CPUPROCANALYZER",
"Triggering RunCPUProcAnalyzer.sh stop...\n");
1899 if (telemetryOnly ==
false)
1901 system(
"/lib/rdk/RunCPUProcAnalyzer.sh stop 1");
1905 system(
"/lib/rdk/RunCPUProcAnalyzer.sh stop 0");
1908 #elif defined PROCANALYZER_EXTENDER
1909 RDK_LOG(RDK_LOG_INFO,
"LOG.RDK.CPUPROCANALYZER",
"Stop CPU Proc Analyzer ...\n");
1910 system(
"/usr/opensync/scripts/run_procanalyzer.sh stop");
1914 RDK_LOG(RDK_LOG_INFO,
"LOG.RDK.CPUPROCANALYZER",
"%s(%d): ***Exiting the application***\n", __func__, __LINE__);
1918 #ifndef PROCANALYZER_EXTENDER
1921 return "LOG.RDK.CPUPROCANALYZER";