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 "err-check.h"
23 int stats_get_system_cpu_usage_average(struct stats_system *previous, struct stats_system *current, float *usage)
25 struct stats_system diff;
27 diff.busy_ticks = current->busy_ticks > previous->busy_ticks ? current->busy_ticks - previous->busy_ticks : 0;
29 diff.total_ticks = current->total_ticks > previous->total_ticks ? current->total_ticks - previous->total_ticks : 0;
31 if (diff.total_ticks == 0) {
34 *usage = (float)diff.busy_ticks / diff.total_ticks;
40 int stats_update_system_stats(struct stats_system *sys)
42 ON_NULL_RETURN_VAL(sys, -1);
44 struct procfs_system_cpu_usage_info info;
46 if (procfs_read_system_cpu_usage(&info) != 0) {
50 sys->busy_ticks = info.user + info.system + info.nice + info.irq + info.softirq;
51 sys->total_ticks = info.user + info.system + info.nice + info.irq + info.softirq + info.idle + info.iowait;
56 int stats_get_system_memory_usage(float *usage)
58 ON_NULL_RETURN_VAL(usage, -1);
60 struct procfs_system_memory_usage_info info;
62 if (procfs_read_system_memory_usage(&info) != 0) {
63 ERR("procfs_read_system_memory_usage failed.");
67 *usage = (float)info.used / info.total;
72 int stats_update_process_stats(int pid, struct stats_process *stats)
74 ON_TRUE_RETURN_VAL(pid < 0, -1);
75 ON_NULL_RETURN_VAL(stats, -1);
77 struct procfs_process_cpu_usage_info proc_info;
78 struct procfs_system_cpu_usage_info sys_info;
80 if (procfs_read_process_cpu_usage(pid, &proc_info) != 0) {
81 ERR("procfs_read_process_cpu_usage failed.");
85 if (procfs_read_system_cpu_usage(&sys_info) != 0) {
86 ERR("procfs_read_system_cpu_usage failed.");
90 stats->process_ticks = proc_info.stime + proc_info.utime;
91 stats->system_ticks = sys_info.user + sys_info.system + sys_info.nice + sys_info.idle;
97 int stats_get_process_cpu_usage_average(struct stats_process *previous, struct stats_process *current, float *usage)
99 struct stats_process diff;
101 diff.process_ticks = current->process_ticks > previous->process_ticks ? current->process_ticks - previous->process_ticks: 0;
102 diff.system_ticks = current->system_ticks > previous->system_ticks ? current->system_ticks - previous->system_ticks : 0;
104 if (diff.system_ticks == 0)
107 *usage = (float)diff.process_ticks / diff.system_ticks * ncpus;
112 int stats_get_process_memory_usage(int pid, float *usage)
114 ON_TRUE_RETURN_VAL(pid < 0, -1);
115 ON_NULL_RETURN_VAL(usage, -1);
117 struct procfs_process_memory_usage_info mem_info;
118 struct procfs_system_memory_usage_info sys_info;
120 if (procfs_read_process_memory_usage(pid, &mem_info) != 0) {
121 ERR("procfs_read_process_memory_usage failed.");
125 if (procfs_read_system_memory_usage(&sys_info) != 0) {
126 ERR("procfs_read_system_memory_usage failed.");
130 *usage = sys_info.total > 0 ? (float)mem_info.rss / sys_info.total : 0;
135 int stats_get_load_averages(float *a1, float *a5, float *a15)
137 struct procfs_load_average_info info;
139 if (procfs_read_system_load_average(&info) != 0) {
140 ERR("procfs_read_system_load_average failed.");
144 if (a1) *a1 = info.one_min_avg;
145 if (a5) *a5 = info.five_min_avg;
146 if (a15) *a15 = info.fifteen_min_avg;
153 if (procfs_read_cpu_count(&ncpus) != 0) {
154 ERR("procfs_read_cpu_count failed.");