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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since d4d74dc was d4d74dc, checked in by Vojtech Horky <vojtechhorky@…>, 13 years ago

Less includes in library headers

There is no need for errno.h to include fibril.h.
Similarly, tinput.h does not need list.h or async.h.

Unfortunately, many programs depended on the fact that including
errno.h would (recursively) include unistd.h and NULL would be
defined. Most of the fixes remedy this problem.

  • Property mode set to 100644
File size: 7.8 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 <malloc.h>
42#include <unistd.h>
43
44#define SYSINFO_STATS_MAX_PATH 64
45
46/** Thread states
47 *
48 */
49static const char *thread_states[] = {
50 "Invalid",
51 "Running",
52 "Sleeping",
53 "Ready",
54 "Entering",
55 "Exiting",
56 "Lingering"
57};
58
59/** Get CPUs statistics
60 *
61 * @param count Number of records returned.
62 *
63 * @return Array of stats_cpu_t structures.
64 * If non-NULL then it should be eventually freed
65 * by free().
66 *
67 */
68stats_cpu_t *stats_get_cpus(size_t *count)
69{
70 size_t size = 0;
71 stats_cpu_t *stats_cpus =
72 (stats_cpu_t *) sysinfo_get_data("system.cpus", &size);
73
74 if ((size % sizeof(stats_cpu_t)) != 0) {
75 if (stats_cpus != NULL)
76 free(stats_cpus);
77 *count = 0;
78 return NULL;
79 }
80
81 *count = size / sizeof(stats_cpu_t);
82 return stats_cpus;
83}
84
85/** Get physical memory statistics
86 *
87 *
88 * @return Pointer to the stats_physmem_t structure.
89 * If non-NULL then it should be eventually freed
90 * by free().
91 *
92 */
93stats_physmem_t *stats_get_physmem(void)
94{
95 size_t size = 0;
96 stats_physmem_t *stats_physmem =
97 (stats_physmem_t *) sysinfo_get_data("system.physmem", &size);
98
99 if (size != sizeof(stats_physmem_t)) {
100 if (stats_physmem != NULL)
101 free(stats_physmem);
102 return NULL;
103 }
104
105 return stats_physmem;
106}
107
108/** Get task statistics
109 *
110 * @param count Number of records returned.
111 *
112 * @return Array of stats_task_t structures.
113 * If non-NULL then it should be eventually freed
114 * by free().
115 *
116 */
117stats_task_t *stats_get_tasks(size_t *count)
118{
119 size_t size = 0;
120 stats_task_t *stats_tasks =
121 (stats_task_t *) sysinfo_get_data("system.tasks", &size);
122
123 if ((size % sizeof(stats_task_t)) != 0) {
124 if (stats_tasks != NULL)
125 free(stats_tasks);
126 *count = 0;
127 return NULL;
128 }
129
130 *count = size / sizeof(stats_task_t);
131 return stats_tasks;
132}
133
134/** Get single task statistics
135 *
136 * @param task_id Task ID we are interested in.
137 *
138 * @return Pointer to the stats_task_t structure.
139 * If non-NULL then it should be eventually freed
140 * by free().
141 *
142 */
143stats_task_t *stats_get_task(task_id_t task_id)
144{
145 char name[SYSINFO_STATS_MAX_PATH];
146 snprintf(name, SYSINFO_STATS_MAX_PATH, "system.tasks.%" PRIu64, task_id);
147
148 size_t size = 0;
149 stats_task_t *stats_task =
150 (stats_task_t *) sysinfo_get_data(name, &size);
151
152 if (size != sizeof(stats_task_t)) {
153 if (stats_task != NULL)
154 free(stats_task);
155 return NULL;
156 }
157
158 return stats_task;
159}
160
161/** Get thread statistics.
162 *
163 * @param count Number of records returned.
164 *
165 * @return Array of stats_thread_t structures.
166 * If non-NULL then it should be eventually freed
167 * by free().
168 *
169 */
170stats_thread_t *stats_get_threads(size_t *count)
171{
172 size_t size = 0;
173 stats_thread_t *stats_threads =
174 (stats_thread_t *) sysinfo_get_data("system.threads", &size);
175
176 if ((size % sizeof(stats_thread_t)) != 0) {
177 if (stats_threads != NULL)
178 free(stats_threads);
179 *count = 0;
180 return NULL;
181 }
182
183 *count = size / sizeof(stats_thread_t);
184 return stats_threads;
185}
186
187/** Get single thread statistics
188 *
189 * @param thread_id Thread ID we are interested in.
190 *
191 * @return Pointer to the stats_thread_t structure.
192 * If non-NULL then it should be eventually freed
193 * by free().
194 *
195 */
196stats_thread_t *stats_get_thread(thread_id_t thread_id)
197{
198 char name[SYSINFO_STATS_MAX_PATH];
199 snprintf(name, SYSINFO_STATS_MAX_PATH, "system.threads.%" PRIu64, thread_id);
200
201 size_t size = 0;
202 stats_thread_t *stats_thread =
203 (stats_thread_t *) sysinfo_get_data(name, &size);
204
205 if (size != sizeof(stats_thread_t)) {
206 if (stats_thread != NULL)
207 free(stats_thread);
208 return NULL;
209 }
210
211 return stats_thread;
212}
213
214/** Get exception statistics.
215 *
216 * @param count Number of records returned.
217 *
218 * @return Array of stats_exc_t structures.
219 * If non-NULL then it should be eventually freed
220 * by free().
221 *
222 */
223stats_exc_t *stats_get_exceptions(size_t *count)
224{
225 size_t size = 0;
226 stats_exc_t *stats_exceptions =
227 (stats_exc_t *) sysinfo_get_data("system.exceptions", &size);
228
229 if ((size % sizeof(stats_exc_t)) != 0) {
230 if (stats_exceptions != NULL)
231 free(stats_exceptions);
232 *count = 0;
233 return NULL;
234 }
235
236 *count = size / sizeof(stats_exc_t);
237 return stats_exceptions;
238}
239
240/** Get single exception statistics
241 *
242 * @param excn Exception number we are interested in.
243 *
244 * @return Pointer to the stats_exc_t structure.
245 * If non-NULL then it should be eventually freed
246 * by free().
247 *
248 */
249stats_exc_t *stats_get_exception(unsigned int excn)
250{
251 char name[SYSINFO_STATS_MAX_PATH];
252 snprintf(name, SYSINFO_STATS_MAX_PATH, "system.exceptions.%u", excn);
253
254 size_t size = 0;
255 stats_exc_t *stats_exception =
256 (stats_exc_t *) sysinfo_get_data(name, &size);
257
258 if (size != sizeof(stats_exc_t)) {
259 if (stats_exception != NULL)
260 free(stats_exception);
261 return NULL;
262 }
263
264 return stats_exception;
265}
266
267/** Get system load
268 *
269 * @param count Number of load records returned.
270 *
271 * @return Array of load records (load_t).
272 * If non-NULL then it should be eventually freed
273 * by free().
274 *
275 */
276load_t *stats_get_load(size_t *count)
277{
278 size_t size = 0;
279 load_t *load =
280 (load_t *) sysinfo_get_data("system.load", &size);
281
282 if ((size % sizeof(load_t)) != 0) {
283 if (load != NULL)
284 free(load);
285 *count = 0;
286 return NULL;
287 }
288
289 *count = size / sizeof(load_t);
290 return load;
291}
292
293/** Get system uptime
294 *
295 * @return System uptime (in seconds).
296 *
297 */
298sysarg_t stats_get_uptime(void)
299{
300 sysarg_t uptime;
301 if (sysinfo_get_value("system.uptime", &uptime) != EOK)
302 uptime = 0;
303
304 return uptime;
305}
306
307/** Print load fixed-point value
308 *
309 * Print the load record fixed-point value in decimal
310 * representation on stdout.
311 *
312 * @param upper Load record.
313 * @param dec_length Number of decimal digits to print.
314 *
315 */
316void stats_print_load_fragment(load_t upper, unsigned int dec_length)
317{
318 /* Magic value from BSD */
319 load_t lower = 65536;
320
321 /* Print the whole part */
322 printf("%u.", upper / lower);
323
324 load_t rest = (upper % lower) * 10;
325
326 unsigned int i;
327 for (i = 0; i < dec_length; i++) {
328 printf("%u", rest / lower);
329 rest = (rest % lower) * 10;
330 }
331}
332
333const char *thread_get_state(state_t state)
334{
335 if (state <= Lingering)
336 return thread_states[state];
337
338 return thread_states[Invalid];
339}
340
341/** @}
342 */
Note: See TracBrowser for help on using the repository browser.