2 * Copyright (c) 2018 Samsung Electronics Co., Ltd.
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
8 * http://floralicense.org/license/
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.
19 #include "sys-stats.h"
21 #include "err-check.h"
24 int sys_stats_get_cpu_usage_percentage(struct sys_stats *stats, float *usage)
26 ON_NULL_RETURN_VAL(stats, -1);
27 ON_NULL_RETURN_VAL(usage, -1);
29 if (stats->total_ticks_delta == 0) {
32 *usage = (float)stats->busy_ticks_delta / stats->total_ticks_delta;
38 int sys_stats_update(struct sys_stats *sys)
40 ON_NULL_RETURN_VAL(sys, -1);
42 struct procfs_stat cpu_info;
43 struct procfs_meminfo mem_info;
44 unsigned long long ticks;
46 if (procfs_read_stat(&cpu_info) != 0) {
47 ERR("procfs_read_stat failed.");
51 if (procfs_read_meminfo(&mem_info) != 0) {
52 ERR("procfs_read_meminfo failed.");
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;
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;
65 sys->memory_used = mem_info.used;
66 sys->total_memory = mem_info.total;
71 int sys_stats_get_memory_usage_percentage(struct sys_stats *sys, float *usage)
73 ON_NULL_RETURN_VAL(sys, -1);
74 ON_NULL_RETURN_VAL(usage, -1);
76 *usage = (float)sys->memory_used / sys->total_memory;
81 int sys_stats_get_load_averages(float *a1, float *a5, float *a15)
83 struct procfs_loadavg info;
85 if (procfs_read_loadavg(&info) != 0) {
86 ERR("procfs_read_loadavg failed.");
90 if (a1) *a1 = info.one_min_avg;
91 if (a5) *a5 = info.five_min_avg;
92 if (a15) *a15 = info.fifteen_min_avg;