[PROTO] Implement reading energy from sysfs
authorDmitry Bogatov <d.bogatov@samsung.com>
Mon, 30 Sep 2013 07:14:41 +0000 (11:14 +0400)
committerGerrit Code Review <gerrit@gerrit.vlan144.tizendev.org>
Thu, 10 Oct 2013 06:13:18 +0000 (06:13 +0000)
Change-Id: I5a81bede27bd0bd65a5190ded8693c22e18f9ef4
Signed-off-by: Dmitry Bogatov <d.bogatov@samsung.com>
daemon/da_protocol.c
daemon/da_protocol.h
daemon/sys_stat.c

index 13acc88..9677856 100644 (file)
@@ -881,6 +881,20 @@ static struct msg_t *gen_binary_info_reply(struct app_info_t *app_info)
        return msg;
 }
 
+static size_t str_array_getsize(const char **strings, size_t len)
+{
+       /*!
+        * Calculate about of memory to place array
+        * of \0 delimited strings
+        */
+       size_t size = 0;
+       unsigned int index;
+       for (index = 0; index != len; ++index)
+               size += strlen(strings[index]) + 1;
+       return size;
+}
+
+
 static struct msg_t *gen_target_info_reply(struct target_info_t *target_info)
 {
        struct msg_t *msg;
@@ -888,10 +902,13 @@ static struct msg_t *gen_target_info_reply(struct target_info_t *target_info)
        uint32_t ret_id = ERR_NO;
 
        msg = malloc(sizeof(*msg) +
-                             sizeof(ret_id) +
-                             sizeof(*target_info) -
-                             sizeof(target_info->network_type) +
-                             strlen(target_info->network_type) + 1);
+                    sizeof(ret_id) +
+                    sizeof(*target_info) -
+                    sizeof(target_info->network_type) +
+                    strlen(target_info->network_type) + 1 +
+                    sizeof(uint32_t) + /* devices count */
+                    str_array_getsize(supported_devices_strings,
+                                      supported_devices_count));
        if (!msg) {
                LOGE("Cannot alloc target info msg\n");
                free(msg);
@@ -911,6 +928,9 @@ static struct msg_t *gen_target_info_reply(struct target_info_t *target_info)
        pack_str(p, target_info->network_type);
        pack_int(p, target_info->max_brightness);
        pack_int(p, target_info->cpu_core_count);
+       pack_int32(p, supported_devices_count);
+       p = pack_str_array(p, supported_devices_strings,
+                          supported_devices_count);
 
        msg->len = p - msg->payload;
 
index 0746501..dbce0c9 100644 (file)
@@ -155,6 +155,16 @@ enum app_type{
        AT_LAUNCHED     =0x02,
        AT_COMMON       =0x03
 };
+enum supported_device {
+  DEVICE_FLASH,
+  DEVICE_CPU
+};
+static const char *supported_devices_strings[] = {
+       "FLASH",
+       "CPU"
+};
+#define array_size(x) (sizeof(x)/sizeof((x)[0]))
+enum { supported_devices_count = array_size(supported_devices_strings) };
 
 #define MAX_FILENAME 128
 
@@ -319,6 +329,8 @@ struct system_info_t {
        uint32_t disk_sectors_write;
        uint32_t network_send_size;
        uint32_t network_receive_size;
+       uint32_t energy_per_device[supported_devices_count];
+       uint32_t app_energy_per_device[supported_devices_count];
 };
 
 struct recorded_event_t{
@@ -364,6 +376,15 @@ struct recorded_event_t{
                to += strlen(n) + 1;            \
        } while (0)
 
+static inline void* pack_str_array(void *buffer, const char **strings,
+                                  size_t count)
+{
+       int index;
+       for (index = 0; index != count; ++index)
+               pack_str(buffer, strings[index]);
+       return buffer;
+}
+
 struct msg_data_t *pack_system_info(struct system_info_t *sys_info);
 int write_to_buf(struct msg_data_t *msg);
 void free_msg_data(struct msg_data_t *msg);
index df2e247..4776808 100644 (file)
@@ -43,7 +43,7 @@
 #include <unistd.h>
 #include <dirent.h>
 #include <errno.h>
-
+#include <assert.h>
 #include <inttypes.h>
 #include <stdint.h>
 
@@ -2310,6 +2310,51 @@ static float get_factor(float elapsed)
        return 100.0f / ((float)Hertz * elapsed * num_of_cpu);
 }
 
+static uint32_t read_int_from_file(const char *fname)
+{
+       FILE *fp = fopen(fname, "r");
+       int value;
+       if (!fp)
+               return 0;
+       if (fscanf(fp, "%d", &value) != 1)
+               value = 0;
+       fclose(fp);
+       return value;
+}
+
+#define swap_sysfs_relpath(x) ("/sys/kernel/debug/swap/energy/" #x)
+#define swap_read_int(x) (read_int_from_file(swap_sysfs_relpath(x)))
+
+static uint32_t get_energy_per_device(enum supported_device dev)
+{
+       switch (dev) {
+       case DEVICE_CPU:
+               return swap_read_int(cpu_idle/system) +
+                       swap_read_int(cpu_running/system);
+
+       case DEVICE_FLASH:
+               return swap_read_int(flash_read/system) +
+                       swap_read_int(flash_write/system);
+       default:
+               assert(0 && "Unknown device. This should not happen");
+               return -41;
+       }
+}
+
+static uint32_t app_get_energy_per_device(enum supported_device dev)
+{
+       switch (dev) {
+       case DEVICE_CPU:
+               return swap_read_int(cpu_running/apps);
+       case DEVICE_FLASH:
+               return swap_read_int(flash_read/apps) +
+                       swap_read_int(flash_write/apps);
+       default:
+               assert(0 && "Unknown device. This should not happen");
+               return -41;
+       }
+}
+
 // return log length (>0) for normal case
 // return negative value for error
 int get_system_info(struct system_info_t *sys_info, int* pidarray, int pidcount)
@@ -2466,8 +2511,16 @@ int get_system_info(struct system_info_t *sys_info, int* pidarray, int pidcount)
        }
 #endif /* LOCALTEST */
        // energy
-       if (IS_OPT_SET(FL_ENERGY))
+       if (IS_OPT_SET(FL_ENERGY)) {
+               int i;
                sys_info->energy = 0; // not implemented
+               for (i = 0; i != supported_devices_count; ++i) {
+                       sys_info->energy_per_device[i] =
+                               get_energy_per_device(i);
+                       sys_info->app_energy_per_device[i] =
+                               app_get_energy_per_device(i);
+               }
+       }
 
 #ifdef THREAD_SAMPLING_DEBUG
        print_sys_info(sys_info);
@@ -2727,7 +2780,10 @@ struct msg_data_t *pack_system_info(struct system_info_t *sys_info)
        pack_int(p, sys_info->call_status);
        pack_int(p, sys_info->dnet_status);
        pack_int(p, sys_info->energy);
-
+       for (i = 0; i != supported_devices_count; ++i)
+               pack_int(p, sys_info->energy_per_device[i]);
+       for (i = 0; i != supported_devices_count; ++i)
+               pack_int(p, sys_info->app_energy_per_device[i]);
 
 //     pack_int(p, sys_info->total_used_drive);