source: mainline/uspace/lib/c/generic/stats.c@ 7907cf9

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 7907cf9 was 8eec3c8, checked in by Martin Decky <martin@…>, 15 years ago

merge basic exception accounting (this is the last piece missing from the original "measure" branch by Stanislav Kozina)

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