summaryrefslogtreecommitdiff
path: root/src/procfs.h
blob: 5cda830522a7bf248f5460a05dc5d92a1336198f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/*
 * Copyright (c) 2018 Samsung Electronics Co., Ltd.
 *
 * Licensed under the Flora License, Version 1.1 (the License);
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://floralicense.org/license/
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an AS IS BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef __PROCFS_H_
#define __PROCFS_H_

#include <stddef.h>
#include <stdbool.h>

/**
 * @brief System memory information.
 */
struct procfs_system_memory_usage_info {
	unsigned long total; /**< Total memory (KiB) */
	unsigned long used;  /**< Used memory (KiB) */
	unsigned long free;  /**< Free memory (KiB) */
	unsigned long cache; /**< Cache memory (KiB) */
	unsigned long swap;  /**< Swap memory (KiB) */
};

/**
 * @brief System cpu usage information.
 */
struct procfs_system_cpu_usage_info {
	unsigned long long user;    /** Time running un-niced user processes (clock ticks) */
	unsigned long long system;  /** Time running kernel process (clock ticks) */
	unsigned long long nice;    /** Time running niced user processes (clock ticks) */
	unsigned long long idle;    /** Time running in idle task user processes (clock ticks) */
	unsigned long long iowait;  /** Time waiting for I/O completion (clock ticks) */
	unsigned long long irq;     /** Time servicing interrupts (clock ticks) */
	unsigned long long softirq; /** Time servicing soft interrupts (clock ticks) */
};

/**
 * @brief System load average information.
 * @notice For more info refer to:
 * http://www.brendangregg.com/blog/2017-08-08/linux-load-averages.html
 * https://github.com/torvalds/linux/blob/master/kernel/sched/loadavg.c
 */
struct procfs_load_average_info {
	float one_min_avg;     /** One minute load average */
	float five_min_avg;    /** Five minutes load average */
	float fifteen_min_avg; /** Fifteen minutes load average */
};

/**
 * @brief Process memory information.
 */
struct procfs_process_memory_usage_info {
	unsigned long vsz;           /** Virtual memory size (KiB) */
	unsigned long rss;           /** Resident set size (KiB) */
	unsigned long pss;           /** Proportional set size (KiB) */
	unsigned long shared_clean;  /** Not modified and mapped by other processes (KiB) */
	unsigned long shared_dirty;  /** Modified and mapped by other processes (KiB) */
	unsigned long private_clean; /** Not modified and available only to that process (KiB) */
	unsigned long private_dirty; /** Modified and available only to that process (KiB) */
};

/**
 * @brief Process cpu usage information.
 */
struct procfs_process_cpu_usage_info {
	unsigned long long utime; /** Amount of time that this process has been scheduled in user mode (clock ticks) */
	unsigned long long stime; /** Amount of time that this process has been scheduled in kernel mode (clock ticks) */
};

/**
 * @brief Parses information from /proc/loadavg
 *
 * @param[out] avg structure to be filled.
 * @return: 0 on success, other value on error
 */
int procfs_read_system_load_average(struct procfs_load_average_info *avg);

/**
 * @brief Parses information from /proc/stat
 *
 * @param[out] avg structure to be filled.
 * @return: 0 on success, other value on error
 */
int procfs_read_system_cpu_usage(struct procfs_system_cpu_usage_info *usage);

/**
 * @brief Parses information from /proc/stat, returns system cpu usage per
 * logical cpu (core).
 *
 * @param[in] max_cpus maximum number of cpus to read stats from.
 * @param[out] usage array to be filled.
 * @return: on success - number of entries correctly written to usage array,
 *          value < 0 if an error occured.
 *
 * @remark the usage array must be have at least max_cpus lenght.
 */
int procfs_read_system_percpu_usage(int max_cpus, struct procfs_system_cpu_usage_info usage[]);

/**
 * @brief Parses information from /proc/meminfo
 *
 * @param[out] avg structure to be filled.
 * @return: 0 on success, other value on error
 */
int procfs_read_system_memory_usage(struct procfs_system_memory_usage_info *usage);

/**
 * @brief Parses information from /proc/{pid}/smaps
 *
 * @param[in] process pid
 * @param[out] avg structure to be filled.
 * @return: 0 on success, other value on error
 */
int procfs_read_process_memory_usage(int pid, struct procfs_process_memory_usage_info *usage);

/**
 * @brief Parses information from /proc/{pid}/stat
 *
 * @param[in] process pid
 * @param[out] avg structure to be filled.
 * @return: 0 on success, other value on error
 */
int procfs_read_process_cpu_usage(int pid, struct procfs_process_cpu_usage_info *usage);

/**
 * @brief Parses information from /proc/uptime
 *
 * @param[out] uptime Number of seconds after boot
 * @return: 0 on success, other value on error
 */
int procfs_read_uptime(unsigned long *uptime);

/**
 * @brief Parses information from /sys/devices/system/cpu/possible
 *
 * @param[out] cpu_count Number of logical cpus availabe in the system.
 * @return: 0 on success, other value on error
 */
int procfs_read_cpu_count(int *cpu_count);

/**
 * @brief Iterator over pids in /proc/ directory.
 */
typedef struct procfs_pid_iterator procfs_pid_iterator_t;

/**
 * @brief Move iterator to next entry.
 *
 * @param[in]: itearator
 *
 * @return returns true if there are more entries available, false otherwise
 */
bool procfs_pid_iterator_next(procfs_pid_iterator_t *iterator);

/**
 * @brief Gets current pid from iterator
 *
 * @param[in]: itearator
 *
 * @not User should always check validity of pid, since it not guaranteed that
 * returned pid is still valid after function returns.
 */
int procfs_pid_iterator_get_pid(procfs_pid_iterator_t *iterator);

/**
 * @brief Frees procfs_pid_iterator_t
 *
 * @param[in]: itearator
 */
void procfs_pid_iterator_free(procfs_pid_iterator_t *iterator);

/**
 * @brief Gets pid iterator
 *
 * @return new procfs_pid_iterator_t or NULL or error or no pids is available.
 *
 * @note the returned value should be released wiht procfs_pid_iterator_free
 */
procfs_pid_iterator_t *procfs_get_pid_iterator();

/**
 * @brief Reads cmdline file for given pid
 *
 * @param[in] pid
 * @param[out] cmdline pointer to newly allocated null-terminated string buffer or NULL
 * if cmdline file is empty.
 *
 * @return numer of bytes written in buffer or -1 on error.
 *
 * @note the returned cmdline should be released with @free
 * @note the cmdline parameters are separated using ' ' sign.
 */
int procfs_read_cmdline(int pid, char **cmdline);

/**
 * @brief Reads exe file for given pid
 *
 * @param[in] pid
 * @param[out] pointer to null-terminated string buffer
 *
 * @return 0 on success, -1 on failure
 *
 * @note the returned buffer should be released with @free
 */
int procfs_read_exe(int pid, char **buffer);

#endif