report-json-serializer: app and top serializers
[apps/native/ttsd-worker-task.git] / src / procfs.h
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 #ifndef __PROCFS_H_
18 #define __PROCFS_H_
19
20 #include <stddef.h>
21 #include <stdbool.h>
22
23 /**
24  * @brief System memory information.
25  */
26 struct procfs_system_memory_usage_info {
27         unsigned long total; /**< Total memory (KiB) */
28         unsigned long used;  /**< Used memory (KiB) */
29         unsigned long free;  /**< Free memory (KiB) */
30         unsigned long cache; /**< Cache memory (KiB) */
31         unsigned long swap;  /**< Swap memory (KiB) */
32 };
33
34 /**
35  * @brief System cpu usage information.
36  */
37 struct procfs_system_cpu_usage_info {
38         unsigned long long user;    /** Time running un-niced user processes (clock ticks) */
39         unsigned long long system;  /** Time running kernel process (clock ticks) */
40         unsigned long long nice;    /** Time running niced user processes (clock ticks) */
41         unsigned long long idle;    /** Time running in idle task user processes (clock ticks) */
42         unsigned long long iowait;  /** Time waiting for I/O completion (clock ticks) */
43         unsigned long long irq;     /** Time servicing interrupts (clock ticks) */
44         unsigned long long softirq; /** Time servicing soft interrupts (clock ticks) */
45 };
46
47 /**
48  * @brief System load average information.
49  * @notice For more info refer to:
50  * http://www.brendangregg.com/blog/2017-08-08/linux-load-averages.html
51  * https://github.com/torvalds/linux/blob/master/kernel/sched/loadavg.c
52  */
53 struct procfs_load_average_info {
54         float one_min_avg;     /** One minute load average */
55         float five_min_avg;    /** Five minutes load average */
56         float fifteen_min_avg; /** Fifteen minutes load average */
57 };
58
59 /**
60  * @brief Process memory information.
61  */
62 struct procfs_process_memory_usage_info {
63         unsigned long vsz;           /** Virtual memory size (KiB) */
64         unsigned long rss;           /** Resident set size (KiB) */
65         unsigned long pss;           /** Proportional set size (KiB) */
66         unsigned long shared_clean;  /** Not modified and mapped by other processes (KiB) */
67         unsigned long shared_dirty;  /** Modified and mapped by other processes (KiB) */
68         unsigned long private_clean; /** Not modified and available only to that process (KiB) */
69         unsigned long private_dirty; /** Modified and available only to that process (KiB) */
70 };
71
72 /**
73  * @brief Process cpu usage information.
74  */
75 struct procfs_process_cpu_usage_info {
76         unsigned long long utime; /** Amount of time that this process has been scheduled in user mode (clock ticks) */
77         unsigned long long stime; /** Amount of time that this process has been scheduled in kernel mode (clock ticks) */
78 };
79
80 /**
81  * @brief Parses information from /proc/loadavg
82  *
83  * @param[out] avg structure to be filled.
84  * @return: 0 on success, other value on error
85  */
86 int procfs_read_system_load_average(struct procfs_load_average_info *avg);
87
88 /**
89  * @brief Parses information from /proc/stat
90  *
91  * @param[out] avg structure to be filled.
92  * @return: 0 on success, other value on error
93  */
94 int procfs_read_system_cpu_usage(struct procfs_system_cpu_usage_info *usage);
95
96 /**
97  * @brief Parses information from /proc/stat, returns system cpu usage per
98  * logical cpu (core).
99  *
100  * @param[in] max_cpus maximum number of cpus to read stats from.
101  * @param[out] usage array to be filled.
102  * @return: on success - number of entries correctly written to usage array,
103  *          value < 0 if an error occured.
104  *
105  * @remark the usage array must be have at least max_cpus lenght.
106  */
107 int procfs_read_system_percpu_usage(int max_cpus, struct procfs_system_cpu_usage_info usage[]);
108
109 /**
110  * @brief Parses information from /proc/meminfo
111  *
112  * @param[out] avg structure to be filled.
113  * @return: 0 on success, other value on error
114  */
115 int procfs_read_system_memory_usage(struct procfs_system_memory_usage_info *usage);
116
117 /**
118  * @brief Parses information from /proc/{pid}/smaps
119  *
120  * @param[in] process pid
121  * @param[out] avg structure to be filled.
122  * @return: 0 on success, other value on error
123  */
124 int procfs_read_process_memory_usage(int pid, struct procfs_process_memory_usage_info *usage);
125
126 /**
127  * @brief Parses information from /proc/{pid}/stat
128  *
129  * @param[in] process pid
130  * @param[out] avg structure to be filled.
131  * @return: 0 on success, other value on error
132  */
133 int procfs_read_process_cpu_usage(int pid, struct procfs_process_cpu_usage_info *usage);
134
135 /**
136  * @brief Parses information from /proc/uptime
137  *
138  * @param[out] uptime Number of seconds after boot
139  * @return: 0 on success, other value on error
140  */
141 int procfs_read_uptime(unsigned long *uptime);
142
143 /**
144  * @brief Parses information from /sys/devices/system/cpu/possible
145  *
146  * @param[out] cpu_count Number of logical cpus availabe in the system.
147  * @return: 0 on success, other value on error
148  */
149 int procfs_read_cpu_count(int *cpu_count);
150
151 /**
152  * @brief Iterator over pids in /proc/ directory.
153  */
154 typedef struct procfs_pid_iterator procfs_pid_iterator_t;
155
156 /**
157  * @brief Move iterator to next entry.
158  *
159  * @param[in]: itearator
160  *
161  * @return returns true if there are more entries available, false otherwise
162  */
163 bool procfs_pid_iterator_next(procfs_pid_iterator_t *iterator);
164
165 /**
166  * @brief Gets current pid from iterator
167  *
168  * @param[in]: itearator
169  *
170  * @not User should always check validity of pid, since it not guaranteed that
171  * returned pid is still valid after function returns.
172  */
173 int procfs_pid_iterator_get_pid(procfs_pid_iterator_t *iterator);
174
175 /**
176  * @brief Frees procfs_pid_iterator_t
177  *
178  * @param[in]: itearator
179  */
180 void procfs_pid_iterator_free(procfs_pid_iterator_t *iterator);
181
182 /**
183  * @brief Gets pid iterator
184  *
185  * @return new procfs_pid_iterator_t or NULL or error or no pids is available.
186  *
187  * @note the returned value should be released wiht procfs_pid_iterator_free
188  */
189 procfs_pid_iterator_t *procfs_get_pid_iterator();
190
191 #endif