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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since a721f6a was dec16a2, checked in by Martin Decky <martin@…>, 15 years ago
  • sysinfo items "system.tasks" and "system.threads" now return complete statistics of all tasks and threads (statistics of individual tasks and threads can be still acquited from "system.tasks.#" and "system.threads.#")
  • update user space functions accordingly
  • cleanup top — it is fully functional again
  • Property mode set to 100644
File size: 6.1 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 system load
187 *
188 * @param count Number of load records returned.
189 *
190 * @return Array of load records (load_t).
191 * If non-NULL then it should be eventually freed
192 * by free().
193 *
194 */
195load_t *stats_get_load(size_t *count)
196{
197 size_t size = 0;
198 load_t *load =
199 (load_t *) sysinfo_get_data("system.load", &size);
200
201 assert((size % sizeof(load_t)) == 0);
202
203 *count = size / sizeof(load_t);
204 return load;
205}
206
207/** Get system uptime
208 *
209 * @return System uptime (in seconds).
210 *
211 */
212sysarg_t stats_get_uptime(void)
213{
214 sysarg_t uptime;
215 if (sysinfo_get_value("system.uptime", &uptime) != EOK)
216 uptime = 0;
217
218 return uptime;
219}
220
221/** Print load fixed-point value
222 *
223 * Print the load record fixed-point value in decimal
224 * representation on stdout.
225 *
226 * @param upper Load record.
227 * @param dec_length Number of decimal digits to print.
228 *
229 */
230void stats_print_load_fragment(load_t upper, unsigned int dec_length)
231{
232 /* Magic value from BSD */
233 load_t lower = 65536;
234
235 /* Print the whole part */
236 printf("%u.", upper / lower);
237
238 load_t rest = (upper % lower) * 10;
239
240 unsigned int i;
241 for (i = 0; i < dec_length; i++) {
242 printf("%u", rest / lower);
243 rest = (rest % lower) * 10;
244 }
245}
246
247const char *thread_get_state(state_t state)
248{
249 if (state <= Lingering)
250 return thread_states[state];
251
252 return thread_states[Invalid];
253}
254
255/** @}
256 */
Note: See TracBrowser for help on using the repository browser.