ea2a007de64b930acfb599fc5c2c44d876638126
[platform/core/api/runtime-info.git] / src / runtime_info_usage.c
1 /*
2  * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the License);
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an AS IS BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <stdio.h>
18 #include <string.h>
19 #include <unistd.h>
20
21 #include <dlog.h>
22
23 #include <runtime_info.h>
24 #include <runtime_info_private.h>
25
26 #include <E_DBus.h>
27
28 #define RESOURCED_BUS_NAME "org.tizen.resourced"
29 #define RESOURCED_USAGE_OBJECT_NAME "/Org/Tizen/ResourceD/Process"
30 #define RESOURCED_USAGE_INTERFACE_NAME "org.tizen.resourced.process"
31 #define RESOURCED_MEMORY_USAGE_METHOD "ProcMemoryUsage"
32 #define RESOURCED_CPU_USAGE_METHOD "ProcCpuUsage"
33 #define DBUS_REPLY_TIMEOUT (120 * 1000)
34 #define MEMORY_USAGE 1
35 #define CPU_USAGE 2
36
37 #define kBtoKiB(val) (int)(((long long)val * 1024)/1000)
38
39 //LCOV_EXCL_START : system error
40 static int runtime_info_get_dbus_error(const char *err_name)
41 {
42         int size;
43
44         if (!err_name)
45                 return RUNTIME_INFO_ERROR_INVALID_PARAMETER;
46
47         size = strlen(err_name);
48         if (!strncmp(err_name, DBUS_ERROR_IO_ERROR, size))
49                 return RUNTIME_INFO_ERROR_IO_ERROR;
50         else if (!strncmp(err_name, DBUS_ERROR_NO_MEMORY, size))
51                 return RUNTIME_INFO_ERROR_OUT_OF_MEMORY;
52         else if (!strncmp(err_name, DBUS_ERROR_ACCESS_DENIED, size))
53                 return RUNTIME_INFO_ERROR_PERMISSION_DENIED;
54         else
55                 return RUNTIME_INFO_ERROR_REMOTE_IO;
56 }
57 //LCOV_EXCL_STOP
58
59 /* Handler function which handles dbus related instructions
60  * for both per process memory and cpu requests.
61  * Creates the method call to resourced and receives the reply (if successful)
62  * Return the received reply (if received) else NULL to signify failed call to resourced
63  */
64 static DBusMessage *runtime_info_dbus_process_usage_info(int *pid, int size, int info, int *error)
65 {
66         DBusConnection *conn;
67         DBusMessage *msg;
68         DBusMessage *reply;
69         DBusError err;
70         int ret;
71
72         if (!pid || !error) {
73                 //LCOV_EXCL_START : system error
74                 _E("INVALID_PARAMETER(0x%08x): pid list and error params cannot be null");
75
76                 if (error)
77                         *error = RUNTIME_INFO_ERROR_INVALID_PARAMETER;
78                 return NULL;
79                 //LCOV_EXCL_STOP
80         }
81
82         conn = dbus_bus_get(DBUS_BUS_SYSTEM, NULL);
83         if (!conn) {
84                 //LCOV_EXCL_START : system error
85                 _E("DBUS_CONNECTION_ERROR");
86                 *error = RUNTIME_INFO_ERROR_REMOTE_IO;
87                 return NULL;
88                 //LCOV_EXCL_STOP
89         }
90
91         if (info == MEMORY_USAGE) {
92                 _D("Process %d: received query to get process memory info of %d processes",
93                                 getpid(), size);
94                 msg = dbus_message_new_method_call(RESOURCED_BUS_NAME,
95                                 RESOURCED_USAGE_OBJECT_NAME,
96                                 RESOURCED_USAGE_INTERFACE_NAME,
97                                 RESOURCED_MEMORY_USAGE_METHOD);
98         } else if (info == CPU_USAGE) {
99                 _D("Process %d: received query to get process cpu usage of %d processes",
100                                 getpid(), size);
101                 msg = dbus_message_new_method_call(RESOURCED_BUS_NAME,
102                                 RESOURCED_USAGE_OBJECT_NAME,
103                                 RESOURCED_USAGE_INTERFACE_NAME,
104                                 RESOURCED_CPU_USAGE_METHOD);
105         } else {
106                 _E("INVALID_PARAMETER(0x%08x): info parameter should be %d or %d",
107                                 MEMORY_USAGE, CPU_USAGE, RUNTIME_INFO_ERROR_INVALID_PARAMETER);
108                 *error = RUNTIME_INFO_ERROR_INVALID_PARAMETER;
109                 return NULL;
110         }
111
112         if (!msg) {
113                 _E("DBUS_METHOD_CALL: not able to create method call (%s:%s-%s)",
114                                 RESOURCED_USAGE_OBJECT_NAME, RESOURCED_USAGE_INTERFACE_NAME,
115                                 RESOURCED_MEMORY_USAGE_METHOD);
116                 *error = RUNTIME_INFO_ERROR_OUT_OF_MEMORY;
117                 return NULL;
118         }
119
120         ret = dbus_message_append_args(msg, DBUS_TYPE_ARRAY, DBUS_TYPE_INT32, &pid,
121                         size, DBUS_TYPE_INVALID);
122         if (!ret) {
123                 //LCOV_EXCL_START : system error
124                 _E("DBUS_METHOD_CALL: not able to append pid array to message");
125                 *error = RUNTIME_INFO_ERROR_IO_ERROR;
126                 dbus_message_unref(msg);
127                 return NULL;
128                 //LCOV_EXCL_STOP
129         }
130
131         dbus_error_init(&err);
132         *error = 0;
133
134         if (info == MEMORY_USAGE)
135                 _D("Process %d: Sending dbus message to resourced for process memory usage info",
136                                 getpid());
137         else
138                 _D("Process %d: Sending dbus message to resourced for process cpu usage info",
139                                 getpid());
140         reply = dbus_connection_send_with_reply_and_block(conn, msg,
141                         DBUS_REPLY_TIMEOUT, &err);
142         if (!reply)
143                 _E("DBUS_METHOD_CALL: not able to send message");       //LCOV_EXCL_LINE : system error
144
145         if (dbus_error_is_set(&err)) {
146                 //LCOV_EXCL_START : system error
147                 _E("DBUS_METHOD_CALL: dbus_connection_send error(%s:%s)", err.name, err.message);
148                 *error = runtime_info_get_dbus_error(err.name);
149                 dbus_error_free(&err);
150                 reply = NULL;
151                 //LCOV_EXCL_STOP
152         }
153
154         dbus_message_unref(msg);
155         return reply;
156 }
157
158 API int runtime_info_get_system_memory_info(runtime_memory_info_s *info)
159 {
160         FILE *meminfo_fp;
161         char buf[256];
162         unsigned long swap_total, swap_free, value;
163
164         if (info == NULL) {
165                 _E("INVALID_PARAMETER(0x%08x) : invalid output param",
166                                 RUNTIME_INFO_ERROR_INVALID_PARAMETER);
167                 return RUNTIME_INFO_ERROR_INVALID_PARAMETER;
168         }
169
170         swap_total = swap_free = 0;
171
172         meminfo_fp = fopen("/proc/meminfo", "r");
173         if (meminfo_fp == NULL) {
174                 _E("IO_ERROR(0x%08x) : failed to open file to read memory usage",
175                                 RUNTIME_INFO_ERROR_IO_ERROR);
176                 return RUNTIME_INFO_ERROR_IO_ERROR;
177         }
178
179         while (fgets(buf, sizeof(buf), meminfo_fp) != NULL) {
180                 if (sscanf(buf, "MemTotal: %lu", &value) == 1)
181                         info->total = kBtoKiB(value);
182                 else if (sscanf(buf, "MemFree: %lu", &value) == 1)
183                         info->free = kBtoKiB(value);
184                 else if (sscanf(buf, "Cached: %lu", &value) == 1)
185                         info->cache = kBtoKiB(value);
186                 else if (sscanf(buf, "SwapTotal: %lu", &value) == 1)
187                         swap_total = value;
188                 else if (sscanf(buf, "SwapFree: %lu", &value) == 1)
189                         swap_free = value;
190         }
191         fclose(meminfo_fp);
192
193         info->used = (info->total > info->free) ? (info->total - info->free) : 0;
194         info->swap = kBtoKiB(((swap_total > swap_free) ? (int)(swap_total - swap_free) : 0));
195
196         return RUNTIME_INFO_ERROR_NONE;
197 }
198
199 API int runtime_info_get_process_memory_info(int *pid, int size, process_memory_info_s **info)
200 {
201         DBusMessageIter iter, iter_array, iter_struct;
202         int index, value;
203         int error;
204         process_memory_info_s *proc_info;
205         DBusMessage *replymsg;
206
207         if (!pid || size <= 0) {
208                 _E("INVALID_PARAMETER(0x%08x) : invalid input param",
209                                 RUNTIME_INFO_ERROR_INVALID_PARAMETER);
210                 return RUNTIME_INFO_ERROR_INVALID_PARAMETER;
211         }
212
213         if (!info) {
214                 _E("INVALID_PARAMETER(0x%08x) : invalid output param",
215                                 RUNTIME_INFO_ERROR_INVALID_PARAMETER);
216                 return RUNTIME_INFO_ERROR_INVALID_PARAMETER;
217         }
218
219         *info = NULL;
220
221         /* Get the needed information from resourced daemon using dbus */
222         replymsg = runtime_info_dbus_process_usage_info(pid, size, MEMORY_USAGE, &error);
223         if (!replymsg) {
224                 //LCOV_EXCL_START : system error
225                 _E("DBUS_METHOD_CALL: call to resourced not successful");
226                 return error;
227                 //LCOV_EXCL_STOP
228         }
229
230         /* Check if the message is an error message or not in expected
231          * format and return error value */
232         dbus_message_iter_init(replymsg, &iter);
233         if (dbus_message_iter_get_arg_type(&iter) == DBUS_TYPE_INT32) {
234                 //LCOV_EXCL_START : system error
235                 dbus_message_iter_get_basic(&iter, &error);
236                 _E("DBUS_METHOD_CALL: call to resourced returned error message");
237                 dbus_message_unref(replymsg);
238                 return error;
239                 //LCOV_EXCL_STOP
240         } else if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY) {
241                 //LCOV_EXCL_START : system error
242                 _E("DBUS_METHOD_CALL: received dbus message is not in expected format");
243                 dbus_message_unref(replymsg);
244                 return RUNTIME_INFO_ERROR_REMOTE_IO;
245                 //LCOV_EXCL_STOP
246         }
247
248         /* Populate the entries of info array using the data received from resourced */
249         *info = (process_memory_info_s *)malloc(size * sizeof(process_memory_info_s));
250         if (!info) {
251                 _E("OUT_OF_MEMORY(0x%08x)", RUNTIME_INFO_ERROR_OUT_OF_MEMORY);
252                 dbus_message_unref(replymsg);
253                 return RUNTIME_INFO_ERROR_OUT_OF_MEMORY;
254         }
255
256         dbus_message_iter_recurse(&iter, &iter_array);
257         for (index = 0; index < size; ++index) {
258                 proc_info = &(*info)[index];
259
260                 dbus_message_iter_recurse(&iter_array, &iter_struct);
261
262                 dbus_message_iter_get_basic(&iter_struct, &value);
263                 proc_info->vsz = value;
264                 dbus_message_iter_next(&iter_struct);
265                 dbus_message_iter_get_basic(&iter_struct, &value);
266                 proc_info->rss = value;
267                 dbus_message_iter_next(&iter_struct);
268                 dbus_message_iter_get_basic(&iter_struct, &value);
269                 proc_info->pss = value;
270                 dbus_message_iter_next(&iter_struct);
271                 dbus_message_iter_get_basic(&iter_struct, &value);
272                 proc_info->shared_clean = value;
273                 dbus_message_iter_next(&iter_struct);
274                 dbus_message_iter_get_basic(&iter_struct, &value);
275                 proc_info->shared_dirty = value;
276                 dbus_message_iter_next(&iter_struct);
277                 dbus_message_iter_get_basic(&iter_struct, &value);
278                 proc_info->private_clean = value;
279                 dbus_message_iter_next(&iter_struct);
280                 dbus_message_iter_get_basic(&iter_struct, &value);
281                 proc_info->private_dirty = value;
282
283                 dbus_message_iter_next(&iter_array);
284         }
285
286         dbus_message_unref(replymsg);
287         return RUNTIME_INFO_ERROR_NONE;
288 }
289
290 API int runtime_info_get_cpu_usage(runtime_cpu_usage_s *usage)
291 {
292         FILE *cpuinfo_fp;
293         char buf[256];
294         unsigned long long user, nice, system, idle, iowait, irq, softirq, total_uptime;
295
296         if (usage == NULL) {
297                 _E("INVALID_PARAMETER(0x%08x) : invalid output param",
298                                 RUNTIME_INFO_ERROR_INVALID_PARAMETER);
299                 return RUNTIME_INFO_ERROR_INVALID_PARAMETER;
300         }
301
302         cpuinfo_fp = fopen("/proc/stat", "r");
303         if (cpuinfo_fp == NULL) {
304                 _E("IO_ERROR(0x%08x) : failed to open file to read cpu usage",
305                                 RUNTIME_INFO_ERROR_IO_ERROR);
306                 return RUNTIME_INFO_ERROR_IO_ERROR;
307         }
308
309         while (fgets(buf, sizeof(buf), cpuinfo_fp) != NULL) {
310                 if (!strncmp(buf, "cpu ", 4) &&
311                     sscanf(buf+4, "%llu %llu %llu %llu %llu %llu %llu",
312                                 &user, &nice, &system, &idle,
313                                 &iowait, &irq, &softirq) == 7)
314                         break;
315         }
316         fclose(cpuinfo_fp);
317
318         total_uptime = user+nice+system+idle+iowait+irq+softirq;
319
320         usage->user = (double)user*100/total_uptime;
321         usage->nice = (double)nice*100/total_uptime;
322         usage->system = (double)system*100/total_uptime;
323         usage->iowait = (double)iowait*100/total_uptime;
324
325         return RUNTIME_INFO_ERROR_NONE;
326 }
327
328 API int runtime_info_get_process_cpu_usage(int *pid, int size, process_cpu_usage_s **usage)
329 {
330         DBusMessageIter iter, iter_array, iter_struct;
331         int index, value;
332         int error;
333         process_cpu_usage_s *proc_usage;
334         DBusMessage *replymsg;
335
336         if (!pid || size <= 0) {
337                 _E("INVALID_PARAMETER(0x%08x) : invalid input param",
338                                 RUNTIME_INFO_ERROR_INVALID_PARAMETER);
339                 return RUNTIME_INFO_ERROR_INVALID_PARAMETER;
340         }
341
342         if (!usage) {
343                 _E("INVALID_PARAMETER(0x%08x) : invalid output param",
344                                 RUNTIME_INFO_ERROR_INVALID_PARAMETER);
345                 return RUNTIME_INFO_ERROR_INVALID_PARAMETER;
346         }
347
348         *usage = NULL;
349
350         /* Get the needed information from resourced daemon using dbus */
351         replymsg = runtime_info_dbus_process_usage_info(pid, size, CPU_USAGE, &error);
352
353         if (!replymsg) {
354                 //LCOV_EXCL_START : system error
355                 _E("DBUS_METHOD_CALL: call to resourced not successful");
356                 return error;
357                 //LCOV_EXCL_STOP
358         }
359
360         /* Check if the message is an error message or not in expected format
361          * and return error value */
362         dbus_message_iter_init(replymsg, &iter);
363         if (dbus_message_iter_get_arg_type(&iter) == DBUS_TYPE_INT32) {
364                 //LCOV_EXCL_START : system error
365                 dbus_message_iter_get_basic(&iter, &error);
366                 _E("DBUS_METHOD_CALL: call to resourced returned error message");
367                 dbus_message_unref(replymsg);
368                 return error;
369                 //LCOV_EXCL_STOP
370         } else if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY) {
371                 //LCOV_EXCL_START : system error
372                 _E("DBUS_METHOD_CALL: received dbus message is not in expected format");
373                 dbus_message_unref(replymsg);
374                 return RUNTIME_INFO_ERROR_REMOTE_IO;
375                 //LCOV_EXCL_STOP
376         }
377
378         /* Populate the entries of info array using the data received from resourced */
379         *usage = (process_cpu_usage_s *)malloc(size * sizeof(process_cpu_usage_s));
380         if (!(*usage)) {
381                 //LCOV_EXCL_START : system error
382                 _E("OUT_OF_MEMORY(0x%08x)", RUNTIME_INFO_ERROR_OUT_OF_MEMORY);
383                 dbus_message_unref(replymsg);
384                 return RUNTIME_INFO_ERROR_OUT_OF_MEMORY;
385                 //LCOV_EXCL_STOP
386         }
387
388         dbus_message_iter_recurse(&iter, &iter_array);
389         for (index = 0; index < size; ++index) {
390                 proc_usage = &(*usage)[index];
391
392                 dbus_message_iter_recurse(&iter_array, &iter_struct);
393
394                 dbus_message_iter_get_basic(&iter_struct, &value);
395                 proc_usage->utime = value;
396                 dbus_message_iter_next(&iter_struct);
397                 dbus_message_iter_get_basic(&iter_struct, &value);
398                 proc_usage->stime = value;
399
400                 dbus_message_iter_next(&iter_array);
401         }
402
403         dbus_message_unref(replymsg);
404         return RUNTIME_INFO_ERROR_NONE;
405 }
406
407 API int runtime_info_get_processor_count(int *num_core)
408 {
409         FILE *cpuinfo_fp;
410         int buf;
411         int result;
412
413         if (!num_core) {
414                 _E("INVALID_PARAMETER(0x%08x) : invalid output parameter",
415                                 RUNTIME_INFO_ERROR_INVALID_PARAMETER);
416                 return RUNTIME_INFO_ERROR_INVALID_PARAMETER;
417         }
418
419         cpuinfo_fp = fopen("/sys/devices/system/cpu/possible", "r");
420         if (cpuinfo_fp == NULL) {
421                 //LCOV_EXCL_START : system error
422                 _E("IO_ERROR(0x%08x) : failed to open file to read cpu information",
423                                 RUNTIME_INFO_ERROR_IO_ERROR);
424                 return RUNTIME_INFO_ERROR_IO_ERROR;
425                 //LCOV_EXCL_STOP
426         }
427
428         if (!fscanf(cpuinfo_fp, "%d-%d", &buf, &result)) {
429                 //LCOV_EXCL_START : system error
430                 _E("IO_ERROR(0x%08x) : there is no information in the system file",
431                                 RUNTIME_INFO_ERROR_IO_ERROR);
432                 fclose(cpuinfo_fp);
433                 return RUNTIME_INFO_ERROR_IO_ERROR;
434                 //LCOV_EXCL_STOP
435         }
436
437         *num_core = result + 1;
438
439         fclose(cpuinfo_fp);
440         return RUNTIME_INFO_ERROR_NONE;
441 }
442
443 API int runtime_info_get_processor_current_frequency(int core_idx, int *cpu_freq)
444 {
445         int num_core;
446
447         if (runtime_info_get_processor_count(&num_core)
448                         != RUNTIME_INFO_ERROR_NONE) {
449                 _E("runtime_info_get_processor_count is failed");
450                 return RUNTIME_INFO_ERROR_IO_ERROR;
451         }
452
453         if (core_idx < 0 || core_idx >= num_core) {
454                 _E("INVALID_PARAMETER(0x%08x) : invalid input parameter",
455                                 RUNTIME_INFO_ERROR_INVALID_PARAMETER);
456                 return RUNTIME_INFO_ERROR_INVALID_PARAMETER;
457         }
458
459         if (!cpu_freq) {
460                 _E("INVALID_PARAMETER(0x%08x) : invalid output parameter",
461                                 RUNTIME_INFO_ERROR_INVALID_PARAMETER);
462                 return RUNTIME_INFO_ERROR_INVALID_PARAMETER;
463         }
464
465         if (runtime_info_get_frequency_cpufreq(core_idx, "cur", cpu_freq)
466                         != RUNTIME_INFO_ERROR_NONE) {
467                 //LCOV_EXCL_START : system error
468                 _I("This system doesn't support cpufreq. Use cpuinfo instead.");
469
470                 switch (runtime_info_get_frequency_cpuinfo(core_idx, cpu_freq)) {
471                 case RUNTIME_INFO_ERROR_NONE:
472                         _I("Notice : it is max CPU frequency");
473                         break;
474                 case RUNTIME_INFO_ERROR_NOT_SUPPORTED:
475                         _E("This system doesn't support MHz information in the cpuinfo");
476                         return RUNTIME_INFO_ERROR_NOT_SUPPORTED;
477                 default:
478                         _E("Fail to get current CPU frequency");
479                         return RUNTIME_INFO_ERROR_IO_ERROR;
480                 };
481                 //LCOV_EXCL_STOP
482         }
483
484         return RUNTIME_INFO_ERROR_NONE;
485 }
486
487 API int runtime_info_get_processor_max_frequency(int core_idx, int *cpu_freq)
488 {
489         int num_core;
490
491         if (runtime_info_get_processor_count(&num_core)
492                         != RUNTIME_INFO_ERROR_NONE) {
493                 _E("runtime_info_get_processor_count is failed");
494                 return RUNTIME_INFO_ERROR_IO_ERROR;
495         }
496
497         if (core_idx < 0 || core_idx >= num_core) {
498                 _E("INVALID_PARAMETER(0x%08x) : invalid input parameter",
499                                 RUNTIME_INFO_ERROR_INVALID_PARAMETER);
500                 return RUNTIME_INFO_ERROR_INVALID_PARAMETER;
501         }
502
503         if (!cpu_freq) {
504                 _E("INVALID_PARAMETER(0x%08x) : invalid output parameter",
505                                 RUNTIME_INFO_ERROR_INVALID_PARAMETER);
506                 return RUNTIME_INFO_ERROR_INVALID_PARAMETER;
507         }
508
509         if (runtime_info_get_frequency_cpufreq(core_idx, "max", cpu_freq)
510                         != RUNTIME_INFO_ERROR_NONE) {
511                 //LCOV_EXCL_START : system error
512                 _I("This system doesn't support cpufreq. Use cpuinfo instead.");
513
514                 switch (runtime_info_get_frequency_cpuinfo(core_idx, cpu_freq)) {
515                 case RUNTIME_INFO_ERROR_NONE:
516                         break;
517                 case RUNTIME_INFO_ERROR_NOT_SUPPORTED:
518                         _E("This system doesn't support MHz information in the cpuinfo");
519                         return RUNTIME_INFO_ERROR_NOT_SUPPORTED;
520                 default:
521                         _E("Fail to get current CPU frequency");
522                         return RUNTIME_INFO_ERROR_IO_ERROR;
523                 };
524                 //LCOV_EXCL_STOP
525         }
526
527         return RUNTIME_INFO_ERROR_NONE;
528 }