runtime-info: Add memory and cpu information apis 19/40719/1
authorJiyoung Yun <jy910.yun@samsung.com>
Tue, 10 Mar 2015 14:22:54 +0000 (23:22 +0900)
committerTaeyoung Kim <ty317.kim@samsung.com>
Mon, 8 Jun 2015 09:53:44 +0000 (18:53 +0900)
New apis:
runtime_info_get_process_memory_info
runtime_info_get_system_memory_info
runtime_info_get_process_cpu_usage
runtime_info_get_cpu_usage

Signed-off-by: Jiyoung Yun <jy910.yun@samsung.com>
Change-Id: Iadce6559fab28fa3f1a37d0f842d17b017f4a73e

include/runtime_info.h
src/runtime_info_usage.c [new file with mode: 0644]

index 78e7d38..53cc112 100644 (file)
@@ -256,6 +256,134 @@ int runtime_info_set_changed_cb(runtime_info_key_e key, runtime_info_changed_cb
 int runtime_info_unset_changed_cb(runtime_info_key_e key);
 
 /**
+ * @brief Structure for memory information.
+ * @since_tizen 2.4
+ */
+typedef struct {
+       int total;  /**< Total memory (KiB) */
+       int used;   /**< Used memory (KiB) */
+       int free;   /**< Free memory (KiB) */
+       int cache;  /**< Cache memory (KiB) */
+       int swap;   /**< Swap memory (KiB) */
+} runtime_memory_info_s;
+
+/**
+ * @brief   Gets system memory information
+ * @since_tizen 2.4
+ *
+ * @param[out] info The system memory information structure
+ *
+ * @return  @c 0 on success,
+ *          otherwise a negative error value
+ *
+ * @retval  #RUNTIME_INFO_ERROR_NONE              Successful
+ * @retval  #RUNTIME_INFO_ERROR_INVALID_PARAMETER Invalid parameter
+ * @retval  #RUNTIME_INFO_ERROR_IO_ERROR          An Input/Output error occured while reading from system
+ *
+ * @see runtime_info_get_process_memory_info()
+ */
+int runtime_info_get_system_memory_info(runtime_memory_info_s *info);
+
+/**
+ * @brief Structure for memory information per processes.
+ * @since_tizen 2.4
+ */
+typedef struct {
+       int vsz;            /**< Virtual memory size (KiB) */
+       int rss;            /**< Resident set size (KiB) */
+       int pss;            /**< Proportional set size (KiB) */
+       int shared_clean;   /**< Not modified and mapped by other processes (KiB) */
+       int shared_dirty;   /**< Modified and mapped by other processes (KiB) */
+       int private_clean;  /**< Not modified and available only to that process (KiB) */
+       int private_dirty;  /**< Modified and available only to that process (KiB) */
+} process_memory_info_s;
+
+/**
+ * @brief   Gets memory information per processes
+ * @since_tizen 2.4
+ * @privlevel public
+ * @privilege %http://tizen.org/privilege/systemmonitor
+ *
+ * @remarks You must release @a s value using free(). \n
+ *          The size of @a s is the same with @a size.
+ *
+ * @param[in]  pid The process unique id array
+ * @param[in]  size The size of pid array
+ * @param[out] info The memory information structure array of the processes
+ *
+ * @return  @c 0 on success,
+ *          otherwise a negative error value
+ *
+ * @retval  #RUNTIME_INFO_ERROR_NONE              Successful
+ * @retval  #RUNTIME_INFO_ERROR_INVALID_PARAMETER Invalid parameter
+ * @retval  #RUNTIME_INFO_ERROR_PERMISSION_DENIED Permission denied
+ *
+ * @see runtime_info_get_system_memory_info()
+ */
+int runtime_info_get_process_memory_info(int *pid, int size, process_memory_info_s **info);
+
+/**
+ * @brief Structure for cpu usage.
+ * @since_tizen 2.4
+ */
+typedef struct {
+       double user;   /**< Time running un-niced user processes (Percent) */
+       double system; /**< Time running kernel processes (Percent) */
+       double nice;   /**< Time running niced user processes (Percent) */
+       double iowait; /**< Time waiting for I/O completion (Percent) */
+} runtime_cpu_usage_s;
+
+/**
+ * @brief   Gets cpu information
+ * @since_tizen 2.4
+ *
+ * @param[out] usage The cpu usage structure
+ *
+ * @return  @c 0 on success,
+ *          otherwise a negative error value
+ *
+ * @retval  #RUNTIME_INFO_ERROR_NONE              Successful
+ * @retval  #RUNTIME_INFO_ERROR_INVALID_PARAMETER Invalid parameter
+ * @retval  #RUNTIME_INFO_ERROR_IO_ERROR          An input/output error occured while reading from system
+ *
+ * @see runtime_info_get_process_cpu_usage()
+ */
+int runtime_info_get_cpu_usage(runtime_cpu_usage_s *usage);
+
+/**
+ * @brief Structure for cpu usage per processes.
+ * @since_tizen 2.4
+ */
+typedef struct {
+       int utime;    /**< Amount of time that this process has been scheduled in user mode (clock ticks) */
+       int stime;    /**< Amount of time that this process has been scheduled in kernel mode (clock ticks) */
+} process_cpu_usage_s;
+
+/**
+ * @brief   Gets cpu usage per processes
+ * @since_tizen 2.4
+ * @privlevel public
+ * @privilege %http://tizen.org/privilege/systemmonitor
+ *
+ * @remarks You must release @a s value using free(). \n
+ *          The size of @a s is the same with @a size.
+ *
+ * @param[in]  pid The process unique id array
+ * @param[in]  size The size of pid array
+ * @param[out] usage The cpu usage structure array of the processes
+ *
+ * @return  @c 0 on success,
+ *          otherwise a negative error value
+ *
+ * @retval  #RUNTIME_INFO_ERROR_NONE              Successful
+ * @retval  #RUNTIME_INFO_ERROR_INVALID_PARAMETER Invalid parameter
+ * @retval  #RUNTIME_INFO_ERROR_PERMISSION_DENIED Permission denied
+ *
+ * @see runtime_info_get_cpu_usage()
+ */
+int runtime_info_get_process_cpu_usage(int *pid, int size, process_cpu_usage_s **usage);
+
+/**
  * @}
  */
 
diff --git a/src/runtime_info_usage.c b/src/runtime_info_usage.c
new file mode 100644 (file)
index 0000000..517f852
--- /dev/null
@@ -0,0 +1,116 @@
+/*
+ * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the License);
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an AS IS BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <stdio.h>
+#include <string.h>
+
+#include <dlog.h>
+
+#include <runtime_info.h>
+#include <runtime_info_private.h>
+
+#ifdef LOG_TAG
+#undef LOG_TAG
+#endif
+
+#define LOG_TAG "CAPI_SYSTEM_RUNTIME_INFO"
+
+#define kBtoKiB(val) (int)(((long long)val*1024)/1000)
+
+API int runtime_info_get_system_memory_info(runtime_memory_info_s *info)
+{
+       FILE *meminfo_fp;
+       char buf[256];
+       unsigned long swap_total, swap_free, value;
+
+       if (info == NULL) {
+               LOGE("INVALID_PARAMETER(0x%08x) : invalid output param", RUNTIME_INFO_ERROR_INVALID_PARAMETER);
+               return RUNTIME_INFO_ERROR_INVALID_PARAMETER;
+       }
+
+       swap_total = swap_free = 0;
+
+       meminfo_fp = fopen("/proc/meminfo", "r");
+       if (meminfo_fp == NULL) {
+               LOGE("IO_ERROR(0x%08x) : failed to open file to read memory usage", RUNTIME_INFO_ERROR_IO_ERROR);
+               return RUNTIME_INFO_ERROR_IO_ERROR;
+       }
+
+       while (fgets(buf, sizeof(buf), meminfo_fp) != NULL) {
+               if (sscanf(buf, "MemTotal: %lu", &value) == 1)
+                       info->total = kBtoKiB(value);
+               else if (sscanf(buf, "MemFree: %lu", &value) == 1)
+                       info->free = kBtoKiB(value);
+               else if (sscanf(buf, "Cached: %lu", &value) == 1)
+                       info->cache = kBtoKiB(value);
+               else if (sscanf(buf, "SwapTotal: %lu", &value) == 1)
+                       swap_total = value;
+               else if (sscanf(buf, "SwapFree: %lu", &value) == 1)
+                       swap_free = value;
+       }
+       fclose(meminfo_fp);
+
+       info->used = (info->total > info->free) ? (info->total - info->free) : 0;
+       info->swap = kBtoKiB(((swap_total > swap_free) ? (int)(swap_total - swap_free) : 0));
+
+       return RUNTIME_INFO_ERROR_NONE;
+}
+
+API int runtime_info_get_process_memory_info(int *pid, int size, process_memory_info_s **info)
+{
+       return RUNTIME_INFO_ERROR_NONE;
+}
+
+API int runtime_info_get_cpu_usage(runtime_cpu_usage_s *usage)
+{
+       FILE *cpuinfo_fp;
+       char buf[256];
+       unsigned long long user, nice, system, idle, iowait, irq, softirq, total_uptime;
+
+       if (usage == NULL) {
+               LOGE("INVALID_PARAMETER(0x%08x) : invalid output param", RUNTIME_INFO_ERROR_INVALID_PARAMETER);
+               return RUNTIME_INFO_ERROR_INVALID_PARAMETER;
+       }
+
+       cpuinfo_fp = fopen("/proc/stat", "r");
+       if (cpuinfo_fp == NULL) {
+               LOGE("IO_ERROR(0x%08x) : failed to open file to read cpu usage", RUNTIME_INFO_ERROR_IO_ERROR);
+               return RUNTIME_INFO_ERROR_IO_ERROR;
+       }
+
+       while (fgets(buf, sizeof(buf), cpuinfo_fp) != NULL) {
+               if (!strncmp(buf, "cpu ", 4) &&
+                   sscanf(buf+4, "%llu %llu %llu %llu %llu %llu %llu",
+                               &user, &nice, &system, &idle,
+                               &iowait, &irq, &softirq) == 7)
+                       break;
+       }
+       fclose(cpuinfo_fp);
+
+       total_uptime = user+nice+system+idle+iowait+irq+softirq;
+
+       usage->user = (double)user*100/total_uptime;
+       usage->nice = (double)nice*100/total_uptime;
+       usage->system = (double)system*100/total_uptime;
+       usage->iowait = (double)iowait*100/total_uptime;
+
+       return RUNTIME_INFO_ERROR_NONE;
+}
+
+API int runtime_info_get_process_cpu_usage(int *pid, int size, process_cpu_usage_s **usage)
+{
+       return RUNTIME_INFO_ERROR_NONE;
+}