source: mainline/uspace/lib/c/generic/stats.c@ b83c5e4

ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since b83c5e4 was a6302ae, checked in by Martin Decky <martin@…>, 6 years ago

add basic support for IPC statistics

Dumping of phone connections is supported right now.

  • Property mode set to 100644
File size: 7.4 KB
Line 
1/*
2 * Copyright (c) 2010 Stanislav Kozina
3 * Copyright (c) 2010 Martin Decky
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * - Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * - Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * - The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30/** @addtogroup libc
31 * @{
32 */
33/** @file
34 */
35
36#include <stats.h>
37#include <sysinfo.h>
38#include <errno.h>
39#include <stdio.h>
40#include <inttypes.h>
41#include <stdlib.h>
42
43#define SYSINFO_STATS_MAX_PATH 64
44
45/** Thread states
46 *
47 */
48static const char *thread_states[] = {
49 "Invalid",
50 "Running",
51 "Sleeping",
52 "Ready",
53 "Entering",
54 "Exiting",
55 "Lingering"
56};
57
58/** Get CPUs statistics
59 *
60 * @param count Number of records returned.
61 *
62 * @return Array of stats_cpu_t structures.
63 * If non-NULL then it should be eventually freed
64 * by free().
65 *
66 */
67stats_cpu_t *stats_get_cpus(size_t *count)
68{
69 size_t size = 0;
70 stats_cpu_t *stats_cpus =
71 (stats_cpu_t *) sysinfo_get_data("system.cpus", &size);
72
73 if ((size % sizeof(stats_cpu_t)) != 0) {
74 if (stats_cpus != NULL)
75 free(stats_cpus);
76 *count = 0;
77 return NULL;
78 }
79
80 *count = size / sizeof(stats_cpu_t);
81 return stats_cpus;
82}
83
84/** Get physical memory statistics
85 *
86 *
87 * @return Pointer to the stats_physmem_t structure.
88 * If non-NULL then it should be eventually freed
89 * by free().
90 *
91 */
92stats_physmem_t *stats_get_physmem(void)
93{
94 size_t size = 0;
95 stats_physmem_t *stats_physmem =
96 (stats_physmem_t *) sysinfo_get_data("system.physmem", &size);
97
98 if (size != sizeof(stats_physmem_t)) {
99 if (stats_physmem != NULL)
100 free(stats_physmem);
101 return NULL;
102 }
103
104 return stats_physmem;
105}
106
107/** Get task statistics
108 *
109 * @param count Number of records returned.
110 *
111 * @return Array of stats_task_t structures.
112 * If non-NULL then it should be eventually freed
113 * by free().
114 *
115 */
116stats_task_t *stats_get_tasks(size_t *count)
117{
118 size_t size = 0;
119 stats_task_t *stats_tasks =
120 (stats_task_t *) sysinfo_get_data("system.tasks", &size);
121
122 if ((size % sizeof(stats_task_t)) != 0) {
123 if (stats_tasks != NULL)
124 free(stats_tasks);
125 *count = 0;
126 return NULL;
127 }
128
129 *count = size / sizeof(stats_task_t);
130 return stats_tasks;
131}
132
133/** Get single task statistics
134 *
135 * @param task_id Task ID we are interested in.
136 *
137 * @return Pointer to the stats_task_t structure.
138 * If non-NULL then it should be eventually freed
139 * by free().
140 *
141 */
142stats_task_t *stats_get_task(task_id_t task_id)
143{
144 char name[SYSINFO_STATS_MAX_PATH];
145 snprintf(name, SYSINFO_STATS_MAX_PATH, "system.tasks.%" PRIu64, task_id);
146
147 size_t size = 0;
148 stats_task_t *stats_task =
149 (stats_task_t *) sysinfo_get_data(name, &size);
150
151 if (size != sizeof(stats_task_t)) {
152 if (stats_task != NULL)
153 free(stats_task);
154 return NULL;
155 }
156
157 return stats_task;
158}
159
160/** Get thread statistics.
161 *
162 * @param count Number of records returned.
163 *
164 * @return Array of stats_thread_t structures.
165 * If non-NULL then it should be eventually freed
166 * by free().
167 *
168 */
169stats_thread_t *stats_get_threads(size_t *count)
170{
171 size_t size = 0;
172 stats_thread_t *stats_threads =
173 (stats_thread_t *) sysinfo_get_data("system.threads", &size);
174
175 if ((size % sizeof(stats_thread_t)) != 0) {
176 if (stats_threads != NULL)
177 free(stats_threads);
178 *count = 0;
179 return NULL;
180 }
181
182 *count = size / sizeof(stats_thread_t);
183 return stats_threads;
184}
185
186/** Get IPC connections statistics.
187 *
188 * @param count Number of records returned.
189 *
190 * @return Array of stats_ipcc_t structures.
191 * If non-NULL then it should be eventually freed
192 * by free().
193 *
194 */
195stats_ipcc_t *stats_get_ipccs(size_t *count)
196{
197 size_t size = 0;
198 stats_ipcc_t *stats_ipccs =
199 (stats_ipcc_t *) sysinfo_get_data("system.ipccs", &size);
200
201 if ((size % sizeof(stats_ipcc_t)) != 0) {
202 if (stats_ipccs != NULL)
203 free(stats_ipccs);
204 *count = 0;
205 return NULL;
206 }
207
208 *count = size / sizeof(stats_ipcc_t);
209 return stats_ipccs;
210}
211
212/** Get exception statistics.
213 *
214 * @param count Number of records returned.
215 *
216 * @return Array of stats_exc_t structures.
217 * If non-NULL then it should be eventually freed
218 * by free().
219 *
220 */
221stats_exc_t *stats_get_exceptions(size_t *count)
222{
223 size_t size = 0;
224 stats_exc_t *stats_exceptions =
225 (stats_exc_t *) sysinfo_get_data("system.exceptions", &size);
226
227 if ((size % sizeof(stats_exc_t)) != 0) {
228 if (stats_exceptions != NULL)
229 free(stats_exceptions);
230 *count = 0;
231 return NULL;
232 }
233
234 *count = size / sizeof(stats_exc_t);
235 return stats_exceptions;
236}
237
238/** Get single exception statistics
239 *
240 * @param excn Exception number we are interested in.
241 *
242 * @return Pointer to the stats_exc_t structure.
243 * If non-NULL then it should be eventually freed
244 * by free().
245 *
246 */
247stats_exc_t *stats_get_exception(unsigned int excn)
248{
249 char name[SYSINFO_STATS_MAX_PATH];
250 snprintf(name, SYSINFO_STATS_MAX_PATH, "system.exceptions.%u", excn);
251
252 size_t size = 0;
253 stats_exc_t *stats_exception =
254 (stats_exc_t *) sysinfo_get_data(name, &size);
255
256 if (size != sizeof(stats_exc_t)) {
257 if (stats_exception != NULL)
258 free(stats_exception);
259 return NULL;
260 }
261
262 return stats_exception;
263}
264
265/** Get system load
266 *
267 * @param count Number of load records returned.
268 *
269 * @return Array of load records (load_t).
270 * If non-NULL then it should be eventually freed
271 * by free().
272 *
273 */
274load_t *stats_get_load(size_t *count)
275{
276 size_t size = 0;
277 load_t *load =
278 (load_t *) sysinfo_get_data("system.load", &size);
279
280 if ((size % sizeof(load_t)) != 0) {
281 if (load != NULL)
282 free(load);
283 *count = 0;
284 return NULL;
285 }
286
287 *count = size / sizeof(load_t);
288 return load;
289}
290
291/** Print load fixed-point value
292 *
293 * Print the load record fixed-point value in decimal
294 * representation on stdout.
295 *
296 * @param upper Load record.
297 * @param dec_length Number of decimal digits to print.
298 *
299 */
300void stats_print_load_fragment(load_t upper, unsigned int dec_length)
301{
302 /* Print the whole part */
303 printf("%u.", upper / LOAD_UNIT);
304
305 load_t rest = (upper % LOAD_UNIT) * 10;
306
307 unsigned int i;
308 for (i = 0; i < dec_length; i++) {
309 printf("%u", rest / LOAD_UNIT);
310 rest = (rest % LOAD_UNIT) * 10;
311 }
312}
313
314const char *thread_get_state(state_t state)
315{
316 if (state <= Lingering)
317 return thread_states[state];
318
319 return thread_states[Invalid];
320}
321
322/** @}
323 */
Note: See TracBrowser for help on using the repository browser.