From: Vitaliy Cherepanov Date: Fri, 23 May 2014 11:26:16 +0000 (+0400) Subject: [PROTO] new MSG_SYSTEM format message X-Git-Tag: Tizen_SDK_2.3~55 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=refs%2Fchanges%2F10%2F21610%2F10;p=platform%2Fcore%2Fsystem%2Fswap-manager.git [PROTO] new MSG_SYSTEM format message Change-Id: Iea6b8ec87dc072cbbdbbf81bc2174a3b79dec9c8 Signed-off-by: Vitaliy Cherepanov --- diff --git a/daemon/buffer.c b/daemon/buffer.c index 100961d..ba2615e 100644 --- a/daemon/buffer.c +++ b/daemon/buffer.c @@ -40,6 +40,28 @@ #define SUBBUF_SIZE 64 * 1024 #define SUBBUF_NUM 32 * 16 +static int open_tasks_dev(void) +{ + if (manager.fd.inst_tasks == NULL) { + manager.fd.inst_tasks = fopen(INST_PID_FILENAME, "r"); + if (manager.fd.inst_tasks == NULL) { + LOGE("Cannot open tasks dev: %s\n", strerror(errno)); + return 1; + } + LOGI("tasks dev opened: %s, %d\n", BUF_FILENAME, manager.buf_fd); + } else { + LOGW("tasks dev double open try: <%s>\n", INST_PID_FILENAME); + } + + return 0; +} + +static void close_tasks_dev(void) +{ + LOGI("close tasks dev (%d)\n", manager.fd.inst_tasks); + fclose(manager.fd.inst_tasks); +} + static int open_buf_ctl(void) { manager.buf_fd = open(BUF_FILENAME, O_RDONLY | O_CLOEXEC); @@ -101,6 +123,11 @@ int init_buf(void) return 1; } + if (open_tasks_dev() != 0) { + LOGE("Cannot open tasks: %s\n", strerror(errno)); + return 1; + } + return 0; } @@ -111,6 +138,7 @@ void exit_buf(void) LOGW("Cannot uninit driver: %s\n", strerror(errno)); close_buf_ctl(); + close_tasks_dev(); } void flush_buf(void) diff --git a/daemon/buffer.h b/daemon/buffer.h index 2f3a5de..f509e07 100644 --- a/daemon/buffer.h +++ b/daemon/buffer.h @@ -33,6 +33,7 @@ #define USER_EVENT_FILENAME "/sys/kernel/debug/swap/writer/raw" #define BUF_FILENAME "/dev/swap_device" +#define INST_PID_FILENAME "/sys/kernel/debug/swap/us_manager/tasks" int init_buf(void); void exit_buf(void); diff --git a/daemon/da_data.c b/daemon/da_data.c index b64b1c7..8d27f15 100644 --- a/daemon/da_data.c +++ b/daemon/da_data.c @@ -78,7 +78,9 @@ int print_sys_info(struct system_info_t * sys_info) sys_info->network_receive_size=0x33; */ - LOGI("isysinfo:\n\ + // TODO do it + /* + LOGI("sysinfo:\n\ \ energy = 0x%X\n\ wifi_status = 0x%X\n\ @@ -198,7 +200,7 @@ int print_sys_info(struct system_info_t * sys_info) } // char *p = sys_info; printBuf((char*)sys_info, sizeof(*sys_info)); - +*/ return 0; } diff --git a/daemon/da_protocol.c b/daemon/da_protocol.c index e74cba0..4eb0098 100644 --- a/daemon/da_protocol.c +++ b/daemon/da_protocol.c @@ -151,8 +151,6 @@ static char *msgErrStr(enum ErrorCode err) #define print_feature_0(f) print_feature(f, feature0, to, ", \n\t") static void feature_code_str(uint64_t feature0, uint64_t feature1, char *to) { - print_feature_0(FL_CPU); - print_feature_0(FL_MEMORY); print_feature_0(FL_FUNCTION_PROFILING); print_feature_0(FL_MEMORY_ALLOC_PROBING); print_feature_0(FL_FILE_API_PROBING); @@ -170,12 +168,22 @@ static void feature_code_str(uint64_t feature0, uint64_t feature1, char *to) print_feature_0(FL_CONTEXT_SWITCH); print_feature_0(FL_NETWORK_API_PROBING); print_feature_0(FL_OPENGL_API_PROBING); + print_feature_0(FL_FUNCTION_SAMPLING); print_feature_0(FL_MEMORY_ALLOC_ALWAYS_PROBING); print_feature_0(FL_FILE_API_ALWAYS_PROBING); print_feature_0(FL_THREAD_API_ALWAYS_PROBING); print_feature_0(FL_OSP_UI_API_ALWAYS_PROBING); print_feature_0(FL_NETWORK_API_ALWAYS_PROBING); print_feature_0(FL_OPENGL_API_ALWAYS_PROBING); + print_feature_0(FL_SYSTEM_CPU); + print_feature_0(FL_SYSTEM_MEMORY); + print_feature_0(FL_SYSTEM_PROCESS); + print_feature_0(FL_SYSTEM_THREAD_LOAD); + print_feature_0(FL_SYSTEM_PROCESSES_LOAD); + print_feature_0(FL_SYSTEM_DISK); + print_feature_0(FL_SYSTEM_NETWORK); + print_feature_0(FL_SYSTEM_DEVICE); + print_feature_0(FL_SYSTEM_ENERGY); } @@ -474,10 +482,6 @@ static void reset_app_inst(struct user_space_inst_t *us_inst) void reset_system_info(struct system_info_t *sys_info) { - if (sys_info->thread_load) - free(sys_info->thread_load); - if (sys_info->process_load) - free(sys_info->process_load); if (sys_info->cpu_frequency) free(sys_info->cpu_frequency); if (sys_info->cpu_load) @@ -979,6 +983,19 @@ int host_message_handler(struct msg_t *msg) return -1; } //write to device + + // TODO make it normally + // Attention!!! convert feature to old format!!! + uint64_t feature0 = *((uint64_t *)msg->payload); + if (feature0 & FL_SYSTEM_ENERGY) { + feature0 &= ~FL_SYSTEM_ENERGY; + feature0 |= FL_SYSTEM_ENERGY_OLD; + } else { + feature0 &= ~FL_SYSTEM_ENERGY; + feature0 &= ~FL_SYSTEM_ENERGY_OLD; + } + *((uint64_t *)msg->payload) = feature0; + if (ioctl_send_msg(msg) != 0) { LOGE("ioctl send error\n"); sendACKToHost(msg->id, ERR_UNKNOWN, 0, 0); diff --git a/daemon/da_protocol.h b/daemon/da_protocol.h index 86a245b..441bfc7 100644 --- a/daemon/da_protocol.h +++ b/daemon/da_protocol.h @@ -90,46 +90,59 @@ enum ErrorCode{ ERR_UNKNOWN =-999 //unknown error }; +#define FL_SYSTEM_ENERGY_OLD (1<<26) + enum feature_code{ - FL_RESERVED1 = 0x000000001, - FL_RESERVED2 = 0x000000002, - FL_FUNCTION_PROFILING = 0x000000004, //On/Off the UserSpaceInst - FL_MEMORY_ALLOC_PROBING = 0x000000008, //memory allocation API (glibc) - FL_FILE_API_PROBING = 0x000000010, //file API (glibc, OSP) - FL_THREAD_API_PROBING = 0x000000020, //thread API (glibc, OSP) - FL_OSP_UI_API_PROBING = 0x000000040, //UI API (OSP) - FL_SCREENSHOT = 0x000000080, //Screenshot - FL_USER_EVENT = 0x000000100, //events of Touch, Gesture, Orientation, Key - FL_RECORDING = 0x000000200, //recording the user event - FL_SYSTCALL_FILE = 0x000000400, //File operation syscalls tracing - FL_SYSTCALL_IPC = 0x000000800, //IPC syscall tracing - FL_SYSTCALL_PROCESS = 0x000001000, //Process syscalls tracing - FL_SYSTCALL_SIGNAL = 0x000002000, //Signal syscalls tracing - FL_SYSTCALL_NETWORK = 0x000004000, //Network syscalls tracing - FL_SYSTCALL_DESC = 0x000008000, //Descriptor syscalls tracing - FL_CONTEXT_SWITCH = 0x000010000, //Context switch tracing - FL_NETWORK_API_PROBING = 0x000020000, //network API (glibc, OSP, libsoap, openssl) - FL_OPENGL_API_PROBING = 0x000040000, //openGL API - FL_FUNCTION_SAMPLING = 0x000080000, //Function sampling - FL_CPU = 0x000100000, //CPU core load, frequency - FL_PROCESSES = 0x000200000, //Process load - FL_MEMORY = 0x000400000, //Process size(VSS, PSS. RSS), heap usage(application, library), physical memory in use - FL_DISK = 0x000800000, - FL_NETWORK = 0x001000000, - FL_DEVICE = 0x002000000, - FL_ENERGY = 0x004000000, - FL_RESERVED3 = 0x008000000, - FL_MEMORY_ALLOC_ALWAYS_PROBING = 0x010000000, //memory allocation API (glibc) always - FL_FILE_API_ALWAYS_PROBING = 0x020000000, //file API (glibc, OSP) always - FL_THREAD_API_ALWAYS_PROBING = 0x040000000, //thread API (glibc, OSP) always - FL_OSP_UI_API_ALWAYS_PROBING = 0x080000000, //UI API (OSP) always - FL_NETWORK_API_ALWAYS_PROBING = 0x100000000ULL, //network API (glibc, OSP, libsoap, openssl) always - FL_OPENGL_API_ALWAYS_PROBING = 0x200000000ULL, //openGL API always - - FL_ALL_FEATURES = 0x3FFFFFFFFULL & - (~FL_RESERVED1) & - (~FL_RESERVED2) & - (~FL_RESERVED3) + FL_RESERVED1 = 0x000000000003ULL, // reserved 0011 + + FL_FUNCTION_PROFILING = 0x000000000004ULL, // On/Off the UserSpaceInst + FL_MEMORY_ALLOC_PROBING = 0x000000000008ULL, // memory allocation API (glibc) + FL_FILE_API_PROBING = 0x000000000010ULL, // file API (glibc, OSP) + FL_THREAD_API_PROBING = 0x000000000020ULL, // thread API (glibc, OSP) + FL_OSP_UI_API_PROBING = 0x000000000040ULL, // UI API (OSP) + FL_SCREENSHOT = 0x000000000080ULL, // Screenshot + FL_USER_EVENT = 0x000000000100ULL, // events of Touch, Gesture, Orientation, Key + FL_RECORDING = 0x000000000200ULL, // recording the user event + FL_SYSTCALL_FILE = 0x000000000400ULL, // File operation syscalls tracing + FL_SYSTCALL_IPC = 0x000000000800ULL, // IPC syscall tracing + FL_SYSTCALL_PROCESS = 0x000000001000ULL, // Process syscalls tracing + FL_SYSTCALL_SIGNAL = 0x000000002000ULL, // Signal syscalls tracing + FL_SYSTCALL_NETWORK = 0x000000004000ULL, // Network syscalls tracing + FL_SYSTCALL_DESC = 0x000000008000ULL, // Descriptor syscalls tracing + FL_CONTEXT_SWITCH = 0x000000010000ULL, // Context switch tracing + FL_NETWORK_API_PROBING = 0x000000020000ULL, // network API (glibc, OSP, libsoap, openssl) + FL_OPENGL_API_PROBING = 0x000000040000ULL, // openGL API + FL_FUNCTION_SAMPLING = 0x000000080000ULL, // Function sampling + + FL_RESERVED2 = 0x00000FF00000ULL, // reserved + + FL_MEMORY_ALLOC_ALWAYS_PROBING = 0x000010000000ULL, // all (include external) memory allocation API (glibc) always + FL_FILE_API_ALWAYS_PROBING = 0x000020000000ULL, // all (include external) file API (glibc, OSP) always + FL_THREAD_API_ALWAYS_PROBING = 0x000040000000ULL, // all (include external) thread API (glibc, OSP) always + FL_OSP_UI_API_ALWAYS_PROBING = 0x000080000000ULL, // all (include external) UI API (OSP) always + FL_NETWORK_API_ALWAYS_PROBING = 0x000100000000ULL, // all (include external) network API (glibc, OSP, libsoap, openssl) always + FL_OPENGL_API_ALWAYS_PROBING = 0x000200000000ULL, // all (include external) openGL API always + + FL_RESERVED3 = 0x000c00000000ULL, // reserved + + FL_SYSTEM_CPU = 0x001000000000ULL, // CPU core load, frequency + FL_SYSTEM_MEMORY = 0x002000000000ULL, // System memory used + FL_SYSTEM_PROCESS = 0x004000000000ULL, // Info for profilling processes (VSS, PSS, RSS, etc) + FL_SYSTEM_THREAD_LOAD = 0x008000000000ULL, // Thread loading for profiling processes + FL_SYSTEM_PROCESSES_LOAD = 0x010000000000ULL, // Non instrumented process load + FL_SYSTEM_DISK = 0x020000000000ULL, // /proc/diskstats - reads, sectors read, writes, sectors written + FL_SYSTEM_NETWORK = 0x040000000000ULL, // network send/recv size + FL_SYSTEM_DEVICE = 0x080000000000ULL, // + FL_SYSTEM_ENERGY = 0x100000000000ULL, // + + FL_RESERVED4 = 0xe00000000000ULL, // reserved 1110 + + FL_ALL_FEATURES = 0x3FFFFFFFFFFFULL & + (~FL_RESERVED1) & + (~FL_RESERVED2) & + (~FL_RESERVED3) & + (~FL_RESERVED4) + }; #define IS_OPT_SET_IN(OPT, reg) (reg & (OPT)) @@ -264,11 +277,24 @@ struct thread_info_t { float load; }; -struct process_info_t { +struct other_process_info_t { uint32_t id; float load; }; +struct inst_process_info_t { + uint32_t id; + float load; + uint64_t virtual_memory; + uint64_t resident_memory; + uint64_t shared_memory; + uint64_t pss_memory; + uint64_t total_alloc_size; + uint32_t count_of_threads; + +}; + + /** * NOTE: * @@ -279,25 +305,23 @@ struct process_info_t { */ struct system_info_t { // system_cpu - float app_cpu_usage; float *cpu_frequency; float *cpu_load; - uint32_t count_of_threads; - struct thread_info_t *thread_load; - // system_processes - uint32_t count_of_processes; - struct process_info_t *process_load; + // total thread count to calculate msg size + uint32_t count_of_threads; // system_memory - uint32_t virtual_memory; - uint32_t resident_memory; - uint32_t shared_memory; - uint32_t pss_memory; - uint64_t total_alloc_size; - uint64_t system_memory_total; uint64_t system_memory_used; + // system_processes + uint32_t count_of_inst_processes; + uint32_t count_of_other_processes; + + //system fd + uint32_t fd_count; + uint32_t fd_peration_count; + // system_disk uint32_t total_used_drive; uint32_t disk_reads; diff --git a/daemon/daemon.c b/daemon/daemon.c index 2cbb424..e31e0c4 100644 --- a/daemon/daemon.c +++ b/daemon/daemon.c @@ -69,17 +69,17 @@ #define MAX_APP_LAUNCH_TIME 60 #define MAX_CONNECT_TIMEOUT_TIME 5*60 -uint64_t get_total_alloc_size(void) +uint64_t get_total_alloc_size_by_pid(pid_t pid) { int i; - uint64_t allocsize = 0; for (i = 0; i < MAX_TARGET_COUNT; i++) { if (manager.target[i].socket != -1 && + manager.target[i].pid == pid && manager.target[i].allocmem > 0) - allocsize += manager.target[i].allocmem; + return manager.target[i].allocmem; } - return allocsize; + return 0; } static int getEmptyTargetSlot() diff --git a/daemon/daemon.h b/daemon/daemon.h index 40441c2..01982f3 100644 --- a/daemon/daemon.h +++ b/daemon/daemon.h @@ -166,6 +166,7 @@ typedef struct FILE *procstat; FILE *networkstat; FILE *diskstats; + FILE *inst_tasks; } __file_descriptors; typedef struct diff --git a/daemon/sys_stat.c b/daemon/sys_stat.c index f82edcf..723a291 100644 --- a/daemon/sys_stat.c +++ b/daemon/sys_stat.c @@ -317,14 +317,15 @@ static void get_cpu_frequency(float *freqs) // ======================================================================== typedef struct _proc_node { proc_t proc_data; + void *thread_prochead; unsigned long long saved_utime; unsigned long long saved_stime; int found; struct _proc_node *next; } procNode; -static procNode *prochead = NULL; -static procNode *thread_prochead = NULL; +static procNode *inst_prochead = NULL; +static procNode *other_prochead = NULL; static procNode* find_node(procNode *head, pid_t pid) { @@ -351,6 +352,7 @@ static procNode* add_node(procNode **head, pid_t pid) return NULL; } + n->thread_prochead = NULL; n->proc_data.pid = pid; n->found = 1; n->next = *head; @@ -426,7 +428,7 @@ static int reset_found_node(procNode *head) // return 0 for normal case // return negative value for error case -static int parse_proc_stat_file_bypid(char *path, proc_t* P) +static int parse_proc_stat_file_bypid(char *path, proc_t* P, int is_inst_process) { char filename[PROCPATH_MAX]; char buf[BUFFER_MAX]; @@ -465,33 +467,51 @@ static int parse_proc_stat_file_bypid(char *path, proc_t* P) abuf = bbuf + 2; // scan data - - sscanf(abuf, - "%c " - "%d %d %d %d %d " - "%lu %lu %lu %lu %lu " - "%Lu %Lu %Lu %Lu " // utime stime cutime cstime - "%ld %ld " - "%d " - "%ld " - "%Lu " // start_time - "%lu " - "%ld", - &P->state, - &P->ppid, &P->pgrp, &P->sid, &P->tty_nr, &P->tty_pgrp, - &P->flags, &P->minor_fault, &P->cminor_fault, &P->major_fault, &P->cmajor_fault, - &P->utime, &P->stime, &P->cutime, &P->cstime, - &P->priority, &P->nice, - &P->numofthread, - &P->dummy, - &P->start_time, - &P->vir_mem, - &P->res_memblock - ); + if (is_inst_process == 1) { + // TODO do not scan unnecessary params + sscanf(abuf, + "%c " + "%d %d %d %d %d " + "%lu %lu %lu %lu %lu " + "%Lu %Lu %Lu %Lu " // utime stime cutime cstime + "%ld %ld " + "%d " + "%ld " + "%Lu " // start_time + "%lu " + "%ld", + &P->state, + &P->ppid, &P->pgrp, &P->sid, &P->tty_nr, &P->tty_pgrp, + &P->flags, &P->minor_fault, &P->cminor_fault, &P->major_fault, &P->cmajor_fault, + &P->utime, &P->stime, &P->cutime, &P->cstime, + &P->priority, &P->nice, + &P->numofthread, + &P->dummy, + &P->start_time, + &P->vir_mem, + &P->res_memblock + ); + } else { + // TODO do not scan unnecessary params + P->numofthread = 0; + sscanf(abuf, + "%c " + "%d %d %d %d %d " + "%lu %lu %lu %lu %lu " + "%Lu %Lu ", // utime stime cutime cstime + &P->state, + &P->ppid, &P->pgrp, &P->sid, &P->tty_nr, &P->tty_pgrp, + &P->flags, &P->minor_fault, &P->cminor_fault, &P->major_fault, &P->cmajor_fault, + &P->utime, &P->stime + ); + } if(P->numofthread == 0) P->numofthread = 1; + //convert to bytes + P->res_memblock = P->res_memblock * getpagesize(); + return 0; } @@ -584,9 +604,13 @@ static int parse_proc_smaps_file_bypid(char *path, proc_t* P) } } } - + // TODO that probe_so_size calculation maybe wrong. check it. fix it P->pss -= probe_so_size; + // convert to bytes + P->pss *= 1024; + P->sh_mem *= 1024; + fclose(fp); return 0; @@ -595,16 +619,15 @@ static int parse_proc_smaps_file_bypid(char *path, proc_t* P) // return 0 for normal case // return positive value for non critical case // return negative value for critical case -static int update_process_data(int* pidarray, int pidcount, enum PROCESS_DATA datatype) +static int update_process_data(procNode **prochead, pid_t* pidarray, int pidcount, enum PROCESS_DATA datatype, int is_inst_process) { static struct stat sb; - int i, ret = 0; + int i, ret = 0, is_new_node = 0; char buf[PROCPATH_MAX]; procNode* procnode; for(i = 0; i < pidcount; i++) { -/* LOGI("#%d\n", i); */ if (pidarray[i] == 0) // pid is invalid { ret = 1; @@ -612,80 +635,48 @@ static int update_process_data(int* pidarray, int pidcount, enum PROCESS_DATA da } sprintf(buf, "/proc/%d", pidarray[i]); -/* LOGI("#->%s\n", buf); */ if (unlikely(stat(buf, &sb) == -1)) // cannot access anymore { -/* LOGI("#del from prochead=%d\n", prochead); */ - del_node(&prochead, pidarray[i]); + del_node(prochead, pidarray[i]); ret = errno; continue; } -/* LOGI("find node = %d\n", procnode); */ - if ((procnode = find_node(prochead, pidarray[i])) == NULL) // new process - { -/* LOGI("proc node1 = %d\n", procnode); */ - procnode = add_node(&prochead, pidarray[i]); - if(datatype == PROCDATA_STAT) - { - if (unlikely((ret = parse_proc_stat_file_bypid(buf, &(procnode->proc_data))) < 0)) - { - LOGE("Failed to get proc stat file by pid(%d)\n", pidarray[i]); - } - else - { - procnode->saved_utime = procnode->proc_data.utime; - procnode->saved_stime = procnode->proc_data.stime; - } - } - else if(datatype == PROCDATA_SMAPS) - { - if (unlikely((ret = parse_proc_smaps_file_bypid(buf, &(procnode->proc_data))) < 0)) - { - LOGE("Failed to get proc smaps file by pid(%d)\n", pidarray[i]); - } - else - { // do nothing - } - } - else - { // impossible - } + if ((procnode = find_node(*prochead, pidarray[i])) == NULL) { + // new process + procnode = add_node(prochead, pidarray[i]); + is_new_node = 1; } - else - { -/* LOGI("proc node2 = %d\n", procnode); */ - if(datatype == PROCDATA_STAT) - { - if (unlikely((ret = parse_proc_stat_file_bypid(buf, &(procnode->proc_data))) < 0)) - { - LOGE("Failed to get proc stat file by pid(%d)\n", pidarray[i]); - } - else - { // do nothing - } - } - else if(datatype == PROCDATA_SMAPS) - { - if (unlikely((ret = parse_proc_smaps_file_bypid(buf, &(procnode->proc_data))) < 0)) - { - LOGE("Failed to get proc smaps file by pid(%d)\n", pidarray[i]); - } - else - { // do nothing - } - } - else - { // impossible + + if (datatype == PROCDATA_STAT) { + ret = parse_proc_stat_file_bypid(buf, + &(procnode->proc_data), + is_inst_process); + if (unlikely(ret < 0)) { + //parse fail. log it + LOGE("Failed to get proc stat file by pid(%d)\n", pidarray[i]); + } else if (is_new_node == 1) { + //update data for new node + procnode->saved_utime = procnode->proc_data.utime; + procnode->saved_stime = procnode->proc_data.stime; } + + } else if (datatype == PROCDATA_SMAPS) { + //parse smaps + ret = parse_proc_smaps_file_bypid(buf, + &(procnode->proc_data)); + + if (unlikely( ret < 0)) + LOGE("Failed to get proc smaps file by pid(%d)\n", pidarray[i]); + + } else { + // impossible } + } -/* LOGI("del_notfound_node\n"); */ - del_notfound_node(&prochead); -/* LOGI("reset_found_node\n"); */ - reset_found_node(prochead); + del_notfound_node(prochead); + reset_found_node(*prochead); -/* LOGI("ret %d\n", ret); */ return ret; } @@ -1081,6 +1072,8 @@ static int update_thread_data(int pid) DIR *taskdir = NULL; struct dirent *entry = NULL; unsigned int tid; + procNode *node = NULL; + procNode **thread_prochead = NULL; sprintf(path, "/proc/%d/task", pid); @@ -1090,6 +1083,13 @@ static int update_thread_data(int pid) return -1; } + node = find_node(inst_prochead, pid); + if (node == NULL) { + LOGE("inst node task not found '%s' pid = %d\n", path, pid); + return -1; + } + thread_prochead = (procNode **)&(node->thread_prochead); + while((entry = readdir(taskdir)) != NULL) { if(*entry->d_name > '0' && *entry->d_name <= '9') @@ -1099,15 +1099,15 @@ static int update_thread_data(int pid) if(unlikely(stat(buf, &sb) == -1)) { - del_node(&thread_prochead, tid); + del_node(thread_prochead, tid); ret = errno; continue; } - if((procnode = find_node(thread_prochead, tid)) == NULL) + if((procnode = find_node(*thread_prochead, tid)) == NULL) { - procnode = add_node(&thread_prochead, tid); - if (unlikely((ret = parse_proc_stat_file_bypid(buf, &(procnode->proc_data))) < 0)) + procnode = add_node(thread_prochead, tid); + if (unlikely((ret = parse_proc_stat_file_bypid(buf, &(procnode->proc_data), 0)) < 0)) { LOGE("Failed to get proc stat file by tid(%d). add node\n", tid); } @@ -1115,21 +1115,21 @@ static int update_thread_data(int pid) { procnode->saved_utime = procnode->proc_data.utime; procnode->saved_stime = procnode->proc_data.stime; -/* LOGI("data updated\n"); */ + LOGI_th_samp("data created %s\n", buf); } } else { - if (unlikely((ret = parse_proc_stat_file_bypid(buf, &(procnode->proc_data))) < 0)) - { + if (unlikely((ret = parse_proc_stat_file_bypid(buf, &(procnode->proc_data), 0)) < 0)) LOGE("Failed to get proc stat file by tid(%d). node exist\n", tid); - } + else + LOGI_th_samp("data updated %s\n", buf); } } } - del_notfound_node(&thread_prochead); - reset_found_node(thread_prochead); + del_notfound_node(thread_prochead); + reset_found_node(*thread_prochead); closedir(taskdir); return ret; @@ -1216,11 +1216,7 @@ static int update_cpus_info(int event_num, float elapsed) cpuptr->cpu_usage = (1.0f - ((float)cpuptr->idle_ticks / (float)cpuptr->total_ticks)) * 100.0f; } -// if(i != num_of_cpu) -// { -// idle_tick_sum += cpuptr->idle_ticks; -// total_tick_sum += cpuptr->total_ticks; -// } + LOGI_th_samp("System cpu usage log : %d, %Ld, %Ld\n", i, cpuptr->idle_ticks, cpuptr->total_ticks); if(unlikely(cpuptr->cpu_usage < 0)) @@ -1267,61 +1263,22 @@ static int update_cpus_info(int event_num, float elapsed) } } - // calculate for cpu core load that failed to get tick information -/* - if(failed_cpu != 0) - { - LOGI("ticks1 : %Ld, %Ld\n", idle_tick_sum, total_tick_sum); - idle_tick_sum = cpus[num_of_cpu].idle_ticks - idle_tick_sum; - total_tick_sum = cpus[num_of_cpu].total_ticks - total_tick_sum; - LOGI("ticks2 : %Ld, %Ld\n", idle_tick_sum, total_tick_sum); - if(total_tick_sum >= MIN_TICKS_FOR_LOAD) - sys_usage = (1.0f - ((float)idle_tick_sum / (float)total_tick_sum)) * 100.0f; - else - sys_usage = 0.0f; - if(sys_usage < 0.0f) sys_usage = 0.0f; - else if(sys_usage > 100.0f) sys_usage = 100.0f; - - for(i = 0; i < num_of_cpu; i++) - { - if(failed_cpu & (1 << i)) - { - cpus[i].cpu_usage = sys_usage; - } - } - } -*/ return 0; } -static int fill_system_processes_info(float factor, struct system_info_t * sys_info) +static int fill_system_processes_info(procNode *prochead, float factor, + struct system_info_t *sys_info, + uint32_t *count) { procNode* proc; - int i = 0; float thread_load; - // data variable - unsigned long virtual = 0; - unsigned long resident = 0; - unsigned long shared = 0; - unsigned long pssmem = 0; - uint64_t ticks = 0; - float app_cpu_usage = 0.0; + uint32_t app_count = 0; LOGI_th_samp("prochead = %X\n", (unsigned int)prochead); - for(proc = prochead; proc != NULL; proc = proc->next) - { - //increment process count - sys_info->count_of_processes++; //maybe wrong - } - sys_info->process_load = malloc ( - sys_info->count_of_processes * - sizeof(*sys_info->process_load) - ); - i = 0; - for(proc = prochead; proc != NULL; proc = proc->next) - { + + for(proc = prochead; proc != NULL; proc = proc->next) { LOGI_th_samp("proc#%d (%d %d),(%d %d) (%d) %f\n", - i, + app_count, (unsigned int)proc->proc_data.utime, (unsigned int)proc->proc_data.stime , (unsigned int)proc->saved_utime, (unsigned int)proc->saved_stime, (int)(proc->proc_data.utime + proc->proc_data.stime - proc->saved_utime - proc->saved_stime), @@ -1335,38 +1292,16 @@ static int fill_system_processes_info(float factor, struct system_info_t * sys_i if(thread_load > 100.0f) thread_load = 100.0f; - //num_thread += proc->proc_data.numofthread; - virtual += proc->proc_data.vir_mem; // Byte - resident += (proc->proc_data.res_memblock * 4); // KByte - pssmem += (proc->proc_data.pss); // KByte - shared += (proc->proc_data.sh_mem); // KByte - ticks += (proc->proc_data.utime + proc->proc_data.stime - - proc->saved_utime - proc->saved_stime); - proc->saved_utime = proc->proc_data.utime; proc->saved_stime = proc->proc_data.stime; + proc->proc_data.cpu_load = thread_load; - sys_info->process_load[i].id = proc->proc_data.pid; - sys_info->process_load[i].load = thread_load; - i++; - + //increment process count + app_count++; } - app_cpu_usage = (float)ticks * factor; - if(app_cpu_usage > 100.0f) - app_cpu_usage = 100.0f; - resident = resident * 1024; // change to Byte - pssmem = pssmem * 1024; // change to Byte - shared = shared * 1024; // change to Byte - - sys_info->virtual_memory = virtual; - sys_info->resident_memory = resident; - sys_info->shared_memory = shared; - sys_info->pss_memory = pssmem; - - sys_info->app_cpu_usage = app_cpu_usage; - + *count = app_count; return 0; } @@ -1374,39 +1309,26 @@ static int fill_system_processes_info(float factor, struct system_info_t * sys_i // fill threads information static int fill_system_threads_info(float factor, struct system_info_t * sys_info) { + procNode* inst_proc; procNode* proc; float thread_load; - for(proc = thread_prochead; proc != NULL; proc = proc->next) - //increment thread count - sys_info->count_of_threads++; //maybe wrong - -/* LOGI_th_samp("thread load\n"); */ - struct thread_info_t *pthread; - if (sys_info->count_of_threads != 0) - { - sys_info->thread_load = malloc( sys_info->count_of_threads * sizeof(*sys_info->thread_load) ); - pthread = sys_info->thread_load; - } - - for(proc = thread_prochead; proc != NULL; proc = proc->next) - { - thread_load = (float)(proc->proc_data.utime + proc->proc_data.stime - - proc->saved_utime - proc->saved_stime) - * factor; - if(thread_load > 100.0f) - thread_load = 100.0f; + for (inst_proc = inst_prochead; inst_proc != NULL; inst_proc = inst_proc->next) + for (proc = inst_proc->thread_prochead; proc != NULL; proc = proc->next) { + sys_info->count_of_threads++; //maybe wrong - pthread->pid = proc->proc_data.pid; - pthread->load = thread_load; - pthread++; + thread_load = (float)(proc->proc_data.utime + + proc->proc_data.stime - + proc->saved_utime - + proc->saved_stime) * factor; + if(thread_load > 100.0f) + thread_load = 100.0f; -// sprintf(thread_loadtmpbuf, "%d,%.1f,", proc->proc_data.pid, thread_load); -// strcat(thread_loadbuf, thread_loadtmpbuf); + proc->proc_data.cpu_load = thread_load; - proc->saved_utime = proc->proc_data.utime; - proc->saved_stime = proc->proc_data.stime; - } + proc->saved_utime = proc->proc_data.utime; + proc->saved_stime = proc->proc_data.stime; + } return 0; } @@ -1733,11 +1655,31 @@ static uint32_t pop_app_energy_per_device(enum supported_device dev) } } -static int get_pid_array(int arr[], const int n) +static int get_inst_pid_array(pid_t *arr, const int n) +{ + int pid_count = 0; + + if (manager.fd.inst_tasks == NULL) + return 0; + + rewind(manager.fd.inst_tasks); + fflush(manager.fd.inst_tasks); + + while (fscanf(manager.fd.inst_tasks, "%lu", arr) == 1) { + LOGI_th_samp("PID scaned %d\n", *arr); + arr++; + pid_count++; + } + + return pid_count; +} + +static int get_other_pid_array(pid_t inst_pid[], const int inst_n, pid_t arr[], + const int n) { DIR *d = opendir("/proc"); struct dirent *dirent; - int count = 0; + int count = 0, i = 0; if (!d) { LOGW("Cannot open /proc dir (%s)\n", strerror(errno)); @@ -1748,8 +1690,14 @@ static int get_pid_array(int arr[], const int n) if (dirent->d_type == DT_DIR) { char *tmp; pid_t pid = strtol(dirent->d_name, &tmp, 10); - if (*tmp == '\0') - arr[count++] = pid; + if (*tmp == '\0') { + for (i = 0; i < inst_n; i++) { + if (inst_pid[i] == pid) + break; + } + if (i == inst_n || inst_n == 0) + arr[count++] = pid; + } } } @@ -1784,27 +1732,45 @@ int get_system_info(struct system_info_t *sys_info) int res = 0; float elapsed; float factor; + int i = 0; LOGI_th_samp("start\n"); memset(sys_info, 0, sizeof(*sys_info)); // common (cpu, processes, memory) - if (IS_OPT_SET(FL_CPU) || - IS_OPT_SET(FL_PROCESSES) || - IS_OPT_SET(FL_MEMORY)) { + if (IS_OPT_SET(FL_SYSTEM_CPU) || + IS_OPT_SET(FL_SYSTEM_PROCESS) || + IS_OPT_SET(FL_SYSTEM_PROCESSES_LOAD) || + IS_OPT_SET(FL_SYSTEM_THREAD_LOAD) || + IS_OPT_SET(FL_SYSTEM_MEMORY)) { const int max_pid_num = 1024; /* ugly hardcode */ - int pidarray[max_pid_num]; - int pidcount = 0; - pid_t first_target_pid = -1; - - pidcount = get_pid_array(pidarray, max_pid_num); - LOGI_th_samp("PID count : %d\n", pidcount); + pid_t other_pidarray[max_pid_num]; + int other_pidcount = 0; + pid_t inst_pidarray[max_pid_num]; + int inst_pidcount = 0; + + inst_pidcount = get_inst_pid_array(inst_pidarray, + max_pid_num); + other_pidcount = get_other_pid_array(inst_pidarray, + inst_pidcount, + other_pidarray, + max_pid_num); + LOGI_th_samp("PID count : inst %d, other %d\n", inst_pidcount, + other_pidcount); + + if (update_process_data(&inst_prochead, inst_pidarray, + inst_pidcount, PROCDATA_STAT, 1) < 0) { + LOGE("Failed to update inst process stat data\n"); + goto fail_exit; + } - if (update_process_data(pidarray, pidcount, PROCDATA_STAT) < 0) { - LOGE("Failed to update process stat data\n"); + if (update_process_data(&other_prochead, other_pidarray, + other_pidcount, PROCDATA_STAT, 0) < 0) { + LOGE("Failed to update other process stat data\n"); goto fail_exit; } + /** * This position is optimized position of timestamp. Just * before get system cpu data and just after get process cpu @@ -1829,14 +1795,20 @@ int get_system_info(struct system_info_t *sys_info) * remarkable, so memory data is less related with timestamp * then cpu data */ - if (update_process_data(pidarray, pidcount, PROCDATA_SMAPS) < 0) { - LOGE("Failed to update process smaps data\n"); + if (update_process_data(&inst_prochead, inst_pidarray, + inst_pidcount, PROCDATA_SMAPS, 1) < 0) { + LOGE("Failed to update inst process smaps data\n"); goto fail_exit; } - - first_target_pid = get_first_target_process(); - if (first_target_pid > 0) - if (update_thread_data(first_target_pid) < 0) { +/* + if (update_process_data(&other_prochead, other_pidarray, + other_pidcount, PROCDATA_SMAPS) < 0) { + LOGE("Failed to update other process smaps data\n"); + goto fail_exit; + } +*/ + for (i = 0; i < inst_pidcount; i++) + if (update_thread_data(inst_pidarray[i]) < 0) { LOGE("Failed to update thread stat data\n"); goto fail_exit; } @@ -1852,7 +1824,15 @@ int get_system_info(struct system_info_t *sys_info) } /* calculate process load, memory, app_cpu_usage */ - if (fill_system_processes_info(factor, sys_info) < 0) { + if (fill_system_processes_info(inst_prochead, factor, sys_info, + &(sys_info->count_of_inst_processes)) < 0) { + LOGE("Failed to fill processes info\n"); + goto fail_exit; + } + + /* calculate process load, memory, app_cpu_usage */ + if (fill_system_processes_info(other_prochead, factor, sys_info, + &(sys_info->count_of_other_processes)) < 0) { LOGE("Failed to fill processes info\n"); goto fail_exit; } @@ -1869,15 +1849,12 @@ int get_system_info(struct system_info_t *sys_info) } } - if (IS_OPT_SET(FL_MEMORY)) { - sys_info->total_alloc_size = get_total_alloc_size(); - sys_info->system_memory_total = sysmemtotal; + if (IS_OPT_SET(FL_SYSTEM_MEMORY)) sys_info->system_memory_used = sysmemused; - } LOGI_th_samp("Fill result structure\n"); - if (IS_OPT_SET(FL_DISK)) { + if (IS_OPT_SET(FL_SYSTEM_DISK)) { sys_info->total_used_drive = get_total_used_drive(); peek_disk_stat_diff(&sys_info->disk_reads, &sys_info->disk_sectors_read, @@ -1885,11 +1862,11 @@ int get_system_info(struct system_info_t *sys_info) &sys_info->disk_sectors_write); } - if (IS_OPT_SET(FL_NETWORK)) + if (IS_OPT_SET(FL_SYSTEM_NETWORK)) peek_network_stat_diff(&sys_info->network_send_size, &sys_info->network_receive_size); - if (IS_OPT_SET(FL_DEVICE)) { + if (IS_OPT_SET(FL_SYSTEM_DEVICE)) { sys_info->wifi_status = get_wifi_status(); sys_info->bt_status = get_bt_status(); sys_info->gps_status = get_gps_status(); @@ -1905,12 +1882,14 @@ int get_system_info(struct system_info_t *sys_info) sys_info->dnet_status = get_dnet_status(); } - if (IS_OPT_SET(FL_ENERGY)) { + if (IS_OPT_SET(FL_SYSTEM_ENERGY)) { int i; - sys_info->energy = 0; /* not implemented */ + uint32_t energy; + sys_info->energy = 0; for (i = 0; i != supported_devices_count; ++i) { - sys_info->energy_per_device[i] = - pop_sys_energy_per_device(i); + energy = pop_sys_energy_per_device(i); + sys_info->energy_per_device[i] = energy; + sys_info->energy += energy; sys_info->app_energy_per_device[i] = (i == DEVICE_LCD) ? sys_info->energy_per_device[i] : pop_app_energy_per_device(i); @@ -2090,25 +2069,31 @@ static uint32_t msg_data_payload_length(const struct system_info_t *sys_info) /* subtract pointers */ len -= sizeof(sys_info->cpu_frequency) + sizeof(sys_info->cpu_load); - len -= sizeof(sys_info->thread_load) + sizeof(sys_info->process_load); - if (IS_OPT_SET(FL_CPU)) - len += sys_info->count_of_threads * - sizeof(*sys_info->thread_load); + if (IS_OPT_SET(FL_SYSTEM_THREAD_LOAD)) + len += sys_info->count_of_threads * sizeof(struct thread_info_t); - if (IS_OPT_SET(FL_PROCESSES)) - len += sys_info->count_of_processes * - sizeof(*sys_info->process_load); + if (IS_OPT_SET(FL_SYSTEM_PROCESS)) + len += sys_info->count_of_inst_processes * + sizeof(struct inst_process_info_t); + + if (IS_OPT_SET(FL_SYSTEM_PROCESSES_LOAD)) + len += sys_info->count_of_other_processes * + sizeof(struct other_process_info_t); return len; } struct msg_data_t *pack_system_info(struct system_info_t *sys_info) { - const int len = msg_data_payload_length(sys_info); + const int len = msg_data_payload_length(sys_info) + 0x100; struct msg_data_t *msg = NULL; char *p = NULL; + procNode *proc = NULL; + procNode *thread_proc = NULL; int i = 0; + int thread_count = 0; + char *thread_count_p; msg = malloc(MSG_DATA_HDR_LEN + len); if (!msg) { @@ -2120,9 +2105,8 @@ struct msg_data_t *pack_system_info(struct system_info_t *sys_info) p = msg->payload; // CPU - if (IS_OPT_SET(FL_CPU)) { - pack_float(p, sys_info->app_cpu_usage); - + if (IS_OPT_SET(FL_SYSTEM_CPU)) { + //CPU frequency for (i = 0; i < num_of_cpu; i++) { if (sys_info->cpu_frequency) pack_float(p, sys_info->cpu_frequency[i]); @@ -2130,67 +2114,74 @@ struct msg_data_t *pack_system_info(struct system_info_t *sys_info) pack_float(p, 0.0); } + //CPU load for (i = 0; i < num_of_cpu; i++) { if (sys_info->cpu_load) pack_float(p, sys_info->cpu_load[i]); else pack_float(p, 0.0); } - // thread - pack_int32(p, sys_info->count_of_threads); - for (i = 0; i < sys_info->count_of_threads; i++) { - if (sys_info->thread_load) { - pack_int32(p, sys_info->thread_load[i].pid); - pack_float(p, sys_info->thread_load[i].load); - } else { - pack_int32(p, 0); - pack_float(p, 0.0); - } - } } else { - pack_float(p, 0.0); /* pack app_cpu_usage */ - for (i = 0; i < num_of_cpu; i++) { pack_float(p, 0.0); /* pack cpu_frequency */ pack_float(p, 0.0); /* pack cpu_load */ } - /* thread */ - pack_int32(p, 0); /* pack count_of_threads */ } - /* process */ - if (IS_OPT_SET(FL_PROCESSES)) { - pack_int32(p, sys_info->count_of_processes); - for (i = 0; i < sys_info->count_of_processes; i++) { - if (sys_info->process_load) { - pack_int32(p, sys_info->process_load[i].id); - pack_float(p, sys_info->process_load[i].load); + /* memory */ + pack_int64(p, sys_info->system_memory_used); + + /* inst process / target process */ + if (IS_OPT_SET(FL_SYSTEM_PROCESS)) { + pack_int32(p, sys_info->count_of_inst_processes); + proc = inst_prochead; + for (i = 0; i < sys_info->count_of_inst_processes; i++) { + pack_int32(p, (uint32_t)proc->proc_data.pid); + pack_float(p, proc->proc_data.cpu_load); + pack_int64(p, (uint64_t)proc->proc_data.vir_mem); + pack_int64(p, (uint64_t)proc->proc_data.res_memblock); + pack_int64(p, (uint64_t)proc->proc_data.sh_mem); + pack_int64(p, (uint64_t)proc->proc_data.pss); + + /* TODO total alloc for not ld preloaded processes */ + pack_int64(p, (uint64_t)get_total_alloc_size_by_pid(proc->proc_data.pid)); + //pack threads + + if (IS_OPT_SET(FL_SYSTEM_THREAD_LOAD)) { + thread_count_p = p; //save real thread_count position + pack_int32(p, 0); //dummy thread_count + thread_count = 0; + for (thread_proc = proc->thread_prochead; + thread_proc != NULL; + thread_proc = thread_proc->next) { + thread_count++; + pack_int32(p, thread_proc->proc_data.pid); + pack_float(p, thread_proc->proc_data.cpu_load); + } + //pack real thread count + pack_int32(thread_count_p, thread_count); } else { - pack_int32(p, 0); - pack_float(p, 0.0); + pack_int32(p, 0); //thread_count } + proc = proc->next; } + } else { pack_int32(p, 0); /* pack count_of_processes */ } - /* memory */ - if (IS_OPT_SET(FL_MEMORY)) { - pack_int32(p, sys_info->virtual_memory); - pack_int32(p, sys_info->resident_memory); - pack_int32(p, sys_info->shared_memory); - pack_int32(p, sys_info->pss_memory); - pack_int64(p, sys_info->total_alloc_size); - pack_int64(p, sys_info->system_memory_total); - pack_int64(p, sys_info->system_memory_used); + + /* other process */ + if (IS_OPT_SET(FL_SYSTEM_PROCESSES_LOAD)) { + pack_int32(p, sys_info->count_of_other_processes); + proc = other_prochead; + for (i = 0; i < sys_info->count_of_other_processes; i++) { + pack_int32(p, proc->proc_data.pid); + pack_float(p, proc->proc_data.cpu_load); + proc = proc->next; + } } else { - pack_int32(p, 0); /* pack virtual_memory */ - pack_int32(p, 0); /* pack resident_memory */ - pack_int32(p, 0); /* pack shared_memory */ - pack_int32(p, 0); /* pack pss_memory */ - pack_int64(p, (uint64_t) 0); /* pack total_alloc_size */ - pack_int64(p, (uint64_t) 0); /* pack system_memory_total */ - pack_int64(p, (uint64_t) 0); /* pack system_memory_used */ + pack_int32(p, 0); /* pack count_of_processes */ } pack_int32(p, sys_info->total_used_drive); @@ -2217,9 +2208,9 @@ struct msg_data_t *pack_system_info(struct system_info_t *sys_info) pack_int32(p, sys_info->dnet_status); pack_int32(p, sys_info->energy); - for (i = 0; i != supported_devices_count; ++i) + for (i = 0; i < supported_devices_count; i++) pack_int32(p, sys_info->energy_per_device[i]); - for (i = 0; i != supported_devices_count; ++i) + for (i = 0; i < supported_devices_count; i++) pack_int32(p, sys_info->app_energy_per_device[i]); return msg; diff --git a/daemon/sys_stat.h b/daemon/sys_stat.h index a5acecb..fc01095 100644 --- a/daemon/sys_stat.h +++ b/daemon/sys_stat.h @@ -110,6 +110,7 @@ typedef struct { unsigned long sh_mem; long res_memblock; unsigned long pss; + float cpu_load; } proc_t; typedef struct {