stats: rename to sys-stats + refactor
[apps/native/ttsd-worker-task.git] / src / sys-stats.c
1 /*
2  * Copyright (c) 2018 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Flora License, Version 1.1 (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://floralicense.org/license/
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 <unistd.h>
18
19 #include "sys-stats.h"
20 #include "procfs.h"
21 #include "err-check.h"
22 #include "clock.h"
23
24 int sys_stats_get_cpu_usage_percentage(struct sys_stats *stats, float *usage)
25 {
26         ON_NULL_RETURN_VAL(stats, -1);
27         ON_NULL_RETURN_VAL(usage, -1);
28
29         if (stats->total_ticks_delta == 0) {
30                 *usage = 0;
31         } else {
32                 *usage = (float)stats->busy_ticks_delta / stats->total_ticks_delta;
33         }
34
35         return 0;
36 }
37
38 int sys_stats_update(struct sys_stats *sys)
39 {
40         ON_NULL_RETURN_VAL(sys, -1);
41
42         struct procfs_stat cpu_info;
43         struct procfs_meminfo mem_info;
44         unsigned long long ticks;
45
46         if (procfs_read_stat(&cpu_info) != 0) {
47                 ERR("procfs_read_stat failed.");
48                 return -1;
49         }
50
51         if (procfs_read_meminfo(&mem_info) != 0) {
52                 ERR("procfs_read_meminfo failed.");
53                 return -1;
54         }
55
56         ticks = cpu_info.user + cpu_info.system + cpu_info.nice + cpu_info.irq + cpu_info.softirq;
57         sys->busy_ticks_delta = ticks > sys->busy_ticks ? ticks - sys->busy_ticks : 0;
58         sys->busy_ticks = ticks;
59
60         ticks = cpu_info.user + cpu_info.system + cpu_info.nice + cpu_info.irq +
61                 cpu_info.softirq + cpu_info.idle + cpu_info.iowait;
62         sys->total_ticks_delta = ticks > sys->total_ticks ? ticks - sys->total_ticks : 0;
63         sys->total_ticks = ticks;
64
65         sys->memory_used = mem_info.used;
66         sys->total_memory = mem_info.total;
67
68         return 0;
69 }
70
71 int sys_stats_get_memory_usage_percentage(struct sys_stats *sys, float *usage)
72 {
73         ON_NULL_RETURN_VAL(sys, -1);
74         ON_NULL_RETURN_VAL(usage, -1);
75
76         *usage = (float)sys->memory_used / sys->total_memory;
77
78         return 0;
79 }
80
81 int sys_stats_get_load_averages(float *a1, float *a5, float *a15)
82 {
83         struct procfs_loadavg info;
84
85         if (procfs_read_loadavg(&info) != 0) {
86                 ERR("procfs_read_loadavg failed.");
87                 return -1;
88         }
89
90         if (a1) *a1 = info.one_min_avg;
91         if (a5) *a5 = info.five_min_avg;
92         if (a15) *a15 = info.fifteen_min_avg;
93
94         return 0;
95 }