From: Vitaliy Cherepanov Date: Fri, 26 Jul 2013 08:16:38 +0000 (+0400) Subject: [FIX] implement disk read/write, network send/recv data to message system X-Git-Tag: Tizen_SDK_2.3~238 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=870a8df43d1578feada563a70ecfab9cb7cd3d42;p=platform%2Fcore%2Fsystem%2Fswap-manager.git [FIX] implement disk read/write, network send/recv data to message system --- diff --git a/daemon/sys_stat.c b/daemon/sys_stat.c index 0d0efb7..5acc50c 100644 --- a/daemon/sys_stat.c +++ b/daemon/sys_stat.c @@ -44,6 +44,9 @@ #include #include +#include +#include + #ifndef LOCALTEST #include #include @@ -2069,6 +2072,79 @@ int fill_cpu_frequecy(int event_num) return -0; } +static void skip_lines(FILE * fp, unsigned int count) +{ + char *buffer = NULL; + size_t buflen; + unsigned int index; + for (index = 0; index != count; ++index) + getline(&buffer, &buflen, fp); + free(buffer); +} + +static void skip_tokens(FILE * fp, unsigned int count) +{ + unsigned int index; + + for (index = 0; index != count; ++index) + fscanf(fp, "%*s"); +} + +static void get_network_stat(uint32_t * recv, uint32_t * send) +{ + FILE *fp = fopen("/proc/net/dev", "r"); + uintmax_t irecv, isend; + char ifname[64]; + + *recv = *send = 0; + skip_lines(fp, 2); /* strip header */ + + while (fscanf(fp, "%s", ifname) != EOF) + if (strcmp("lo:", ifname)) { + fscanf(fp, "%" SCNuMAX, &irecv); + skip_tokens(fp, 7); + + fscanf(fp, "%" SCNuMAX, &isend); + skip_tokens(fp, 7); + + *recv += irecv; + *send += isend; + } else + skip_tokens(fp, 16); +} + +static void get_disk_stat(uint32_t * read, uint32_t * write) +{ + enum { partition_name_maxlength = 128 }; + FILE *fp = fopen("/proc/diskstats", "r"); + char master_partition[partition_name_maxlength] = { 0 }; + + *read = *write = 0; + while (!feof(fp)) { + char partition[partition_name_maxlength]; + uintmax_t pread, pwrite; + skip_tokens(fp, 2); + fscanf(fp, "%s", partition); + if (*master_partition + && !strncmp(master_partition, partition, + strlen(master_partition))) { + /* subpartition */ + skip_tokens(fp, 11); + } else { + skip_tokens(fp, 2); + fscanf(fp, "%" SCNuMAX, &pread); + skip_tokens(fp, 3); + fscanf(fp, "%" SCNuMAX, &pwrite); + skip_tokens(fp, 4); + + memcpy(master_partition, partition, + partition_name_maxlength); + *read += pread; + *write += pwrite; + } + } +} + // return log length (>0) for normal case // return negative value for error @@ -2202,10 +2278,12 @@ int get_system_info(struct system_info_t *sys_info, int* pidarray, int pidcount) sys_info->system_memory_total = sysmemtotal; sys_info->system_memory_used = sysmemused; sys_info->total_used_drive = get_total_used_drive(); - sys_info->disk_read_size = 0; // TODO - sys_info->disk_write_size = 0; // TODO - sys_info->network_send_size = 0; // TODO - sys_info->network_receive_size = 0; // TODO + + get_network_stat(&sys_info->network_send_size, + &sys_info->network_receive_size); + + get_disk_stat(&sys_info->disk_read_size, + &sys_info->disk_write_size); #else /* LOCALTEST */