Add new dbus method "ProcSwapUsage" 36/253536/2 accepted/tizen/unified/20210219.134906 submit/tizen/20210217.044338 submit/tizen/20210218.041307
authorYunmi Ha <yunmi.ha@samsung.com>
Mon, 15 Feb 2021 06:36:05 +0000 (15:36 +0900)
committerYunmi Ha <yunmi.ha@samsung.com>
Mon, 15 Feb 2021 10:03:33 +0000 (19:03 +0900)
- method name: ProcSwapUsage
- in: 'ai'
- out: 'a(i)'

Change-Id: I673db6c06fc85eb648840298a64f1938ac0f4e17
Signed-off-by: Yunmi Ha <yunmi.ha@samsung.com>
src/common/procfs.h
src/proc-stat/include/proc-usage-stats-helper.h
src/proc-stat/include/proc-usage-stats.h
src/proc-stat/proc-monitor.c
src/proc-stat/proc-usage-stats-helper.c
src/proc-stat/proc-usage-stats.c

index 46eb2fb..0ebcb0d 100644 (file)
@@ -275,7 +275,7 @@ int proc_get_uss(pid_t pid, unsigned int *uss);
  * @desc get VmRSS and VmSwap from /proc/{pid}/status file.
  * @return negative value if error or pid doesn't exist
  */
-int proc_get_mem_status(pid_t pid, unsigned int *rss, unsigned int *swap);
+int proc_get_mem_status(pid_t pid, unsigned int *swap, unsigned int *rss);
 
 /**
  * @desc get aproximated usage of Zram for pid
index 035e6f5..18e761f 100644 (file)
@@ -55,7 +55,8 @@ struct process_cpu_usage_s {
 
 typedef enum {
        RUNTIME_INFO_TASK_MEMORY,       /**< Represents memory usage requests */
-       RUNTIME_INFO_TASK_CPU           /**< Represents cpu usage requests */
+       RUNTIME_INFO_TASK_CPU,          /**< Represents cpu usage requests */
+       RUNTIME_INFO_TASK_SWAP          /**< Represents swap usage requests */
 } runtime_info_task_type;
 
 /* Runtime info task struct. Represents each request received by the runtime-info library */
index ae371a2..21fb3d6 100644 (file)
@@ -65,4 +65,20 @@ void dbus_proc_memory_usage(GDBusMethodInvocation *invocation, GVariant *params)
  */
 void dbus_proc_cpu_usage(GDBusMethodInvocation *invocation, GVariant *params);
 
+/**
+ * @brief       DBus method to return the swap usage information of input processes
+ * @since_tizen 6.5
+ *
+ * @param[in] invocation       The dbus invocation
+ * @param[in] params   Parameters sent by the runtime info API
+ *                     This should be an array of process IDs.
+ *
+ * @retval              The response contains an array of data.
+ *                     The data contains the swap usage info for the processes (KB).
+ *                     For invalid process IDs, the data will be INVALID_PROCESS_INFO_FIELD_VALUE.
+ *                     If the input dbus message does not contain array of integers or if there
+ *                     are errors in computation, collection and sending of usage info, then the
+ *                     response dbus message contains only an integer whose value will the error value.
+ */
+void dbus_proc_swap_usage(GDBusMethodInvocation *invocation, GVariant *params);
 #endif /* __PROC_USAGE_STATS_H__ */
index 5b786d2..bc67195 100644 (file)
@@ -1094,6 +1094,10 @@ static const char dbus_methods_xml[] =
 "                      <arg type='ai' name='ProcList' direction='in'/>"
 "                      <arg type='a(ii)' name='CpuUsage' direction='out'/>"
 "              </method>"
+"              <method name='ProcSwapUsage'>"
+"                      <arg type='ai' name='ProcList' direction='in'/>"
+"                      <arg type='a(i)' name='SwapUsage' direction='out'/>"
+"              </method>"
 "              <method name='CheckAppStatus'>"
 "                      <arg type='i' name='Pid' direction='in'/>"
 "                      <arg type='s' name='Type' direction='in'/>"
@@ -1123,6 +1127,7 @@ static struct d_bus_method dbus_methods[] = {
        { "ProcCpuUsage", dbus_proc_cpu_usage },
        { "CheckAppStatus", dbus_get_checkappstatus },
        { "GetRamSize", dbus_get_get_ram_size },
+       { "ProcSwapUsage", dbus_proc_swap_usage },
        /* Add methods here */
 };
 
index 8307e14..2f44338 100644 (file)
@@ -182,12 +182,15 @@ int proc_read_from_usage_struct(void *usage_info_list, int index,
                result[4] = mem_info[index].shared_dirty;
                result[5] = mem_info[index].private_clean;
                result[6] = mem_info[index].private_dirty;
-       } else {
+       } else if (type == RUNTIME_INFO_TASK_CPU) {
                struct process_cpu_usage_s *cpu_usage;
 
                cpu_usage = (struct process_cpu_usage_s *)usage_info_list;
                result[0] = cpu_usage[index].utime;
                result[1] = cpu_usage[index].stime;
+       } else {
+               int *swap_usage = (int *)usage_info_list;
+               result[0] = swap_usage[index];
        }
 
        return RESOURCED_ERROR_NONE;
@@ -203,7 +206,8 @@ void proc_get_task_name(char *task_name, int size,
        char buf[TASK_NAME_SIZE];
 
        snprintf(buf, sizeof(buf), TASK_NAME_BASE,
-                       ((task_type == RUNTIME_INFO_TASK_MEMORY) ? "memory" : "cpu"));
+                       ((task_type == RUNTIME_INFO_TASK_MEMORY) ? "memory" :
+                        (task_type == RUNTIME_INFO_TASK_CPU) ? "cpu" : "swap" ));
 
        if (!task_name || size <= 0)
                return;
index c9ba2a4..d4cb9e8 100644 (file)
@@ -50,6 +50,7 @@
 #include <unistd.h>
 #include <errno.h>
 
+#include "procfs.h"
 #include "proc-main.h"
 #include "proc-usage-stats.h"
 #include "proc-usage-stats-helper.h"
@@ -91,8 +92,10 @@ static bool proc_runtime_info_task_cb(int fd, void *data)
 
        if (rt_task->task_type == RUNTIME_INFO_TASK_MEMORY)
                g_variant_builder_init(&builder, G_VARIANT_TYPE("a(iiiiiii)"));
-       else
+       else if (rt_task->task_type == RUNTIME_INFO_TASK_CPU)
                g_variant_builder_init(&builder, G_VARIANT_TYPE("a(ii)"));
+       else
+               g_variant_builder_init(&builder, G_VARIANT_TYPE("a(i)"));
 
        /* Populate the reply message with the usage info */
        for (i = 0; i < rt_task->task_size; ++i) {
@@ -107,8 +110,10 @@ static bool proc_runtime_info_task_cb(int fd, void *data)
                if (rt_task->task_type == RUNTIME_INFO_TASK_MEMORY)
                        usage = g_variant_new("(iiiiiii)", result[0], result[1], result[2],
                                        result[3], result[4], result[5], result[6]);
-               else
+               else if (rt_task->task_type == RUNTIME_INFO_TASK_CPU)
                        usage = g_variant_new("(ii)", result[0], result[1]);
+               else
+                       usage = g_variant_new("(i)", result[0]);
 
                g_variant_builder_add_value(&builder, usage);
        }
@@ -128,9 +133,13 @@ send_message:
        if (rt_task->task_type == RUNTIME_INFO_TASK_MEMORY)
                g_dbus_method_invocation_return_value(rt_task->task_invocation,
                                g_variant_new("(a(iiiiiii))", &builder));
-       else
+       else if (rt_task->task_type == RUNTIME_INFO_TASK_CPU)
                g_dbus_method_invocation_return_value(rt_task->task_invocation,
                                g_variant_new("(a(ii))", &builder));
+       else
+               g_dbus_method_invocation_return_value(rt_task->task_invocation,
+                               g_variant_new("(a(i))", &builder));
+
        proc_free_runtime_info_task(rt_task);
 
        return false;
@@ -148,12 +157,21 @@ static int proc_runtime_info_task(struct runtime_info_task *rt_task)
                mem_info = (struct process_memory_info_s *)rt_task->usage_info_list;
                for (i = 0; i < rt_task->task_size; ++i)
                        proc_get_memory_usage(rt_task->pid_list[i], &mem_info[i]);
-       } else {
+       } else if (rt_task->task_type == RUNTIME_INFO_TASK_CPU) {
                struct process_cpu_usage_s *cpu_usage;
 
                cpu_usage = (struct process_cpu_usage_s *)rt_task->usage_info_list;
                for (i = 0; i < rt_task->task_size; ++i)
                        proc_get_cpu_usage(rt_task->pid_list[i], &cpu_usage[i]);
+       } else {
+               int *swap_usage = (int *)rt_task->usage_info_list;
+               int swap;
+
+               for (i = 0; i < rt_task->task_size; ++i) {
+                       if (proc_get_mem_status(rt_task->pid_list[i], (unsigned int *)&swap, NULL) < 0)
+                               swap = INVALID_PROCESS_INFO_FIELD_VALUE;
+                       swap_usage[i] = swap;
+               }
        }
 
        /* Write to the write end of the pipe depending on the success of
@@ -230,7 +248,8 @@ void proc_runtime_info_request_handler(GDBusMethodInvocation *invocation,
        rt_task->task_invocation = invocation;
 
        _D("Received %s usage request, task name is %s",
-                       (rt_task->task_type == RUNTIME_INFO_TASK_MEMORY) ? "memory" : "cpu",
+                       (rt_task->task_type == RUNTIME_INFO_TASK_MEMORY) ? "memory" :
+                       (rt_task->task_type == RUNTIME_INFO_TASK_CPU) ? "cpu" : "swap",
                        rt_task->task_name);
 
        /* Parse arguments to the pids and count it */
@@ -244,8 +263,11 @@ void proc_runtime_info_request_handler(GDBusMethodInvocation *invocation,
 
        if (rt_task->task_type == RUNTIME_INFO_TASK_MEMORY)
                rt_task->usage_info_list = (void *)malloc(sizeof(struct process_memory_info_s) * rt_task->task_size);
-       else
+       else if (rt_task->task_type == RUNTIME_INFO_TASK_CPU)
                rt_task->usage_info_list = (void *)malloc(sizeof(struct process_cpu_usage_s) * rt_task->task_size);
+       else
+               rt_task->usage_info_list = (void *)malloc(sizeof(int) * rt_task->task_size);
+
        if (!rt_task->usage_info_list) {
                _E("task %s: out of memory: not able to create usage_info_list of rt_task", rt_task->task_name);
                ret = -ENOMEM;
@@ -299,6 +321,10 @@ void dbus_proc_cpu_usage(GDBusMethodInvocation *invocation, GVariant *params)
        proc_runtime_info_request_handler(invocation, params, RUNTIME_INFO_TASK_CPU);
 }
 
+void dbus_proc_swap_usage(GDBusMethodInvocation *invocation, GVariant *params)
+{
+       proc_runtime_info_request_handler(invocation, params, RUNTIME_INFO_TASK_SWAP);
+}
 /*
  * These D-Bus methods are registered at proc-monitor.c
  * because gdbus allows to register object