source: mainline/uspace/app/stats/stats.c@ 21f1543

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 21f1543 was 1ab8539, checked in by Martin Decky <martin@…>, 11 years ago

remove system.uptime sysinfo entry since it is redundant
cleanup the time handling routines

  • still more cleanup is needed here, the code is a wild combination of quasi-POSIX API (with all the brain-dead types, etc.) and HelenOS API
  • Property mode set to 100644
File size: 7.6 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 stats
31 * @brief Print system statistics.
32 * @{
33 */
34/**
35 * @file
36 */
37
38#include <stdio.h>
39#include <task.h>
40#include <thread.h>
41#include <stats.h>
42#include <errno.h>
43#include <stdlib.h>
44#include <malloc.h>
45#include <inttypes.h>
46#include <stdbool.h>
47#include <str.h>
48#include <arg_parse.h>
49
50#define NAME "stats"
51
52#define DAY 86400
53#define HOUR 3600
54#define MINUTE 60
55
56static void list_tasks(void)
57{
58 size_t count;
59 stats_task_t *stats_tasks = stats_get_tasks(&count);
60
61 if (stats_tasks == NULL) {
62 fprintf(stderr, "%s: Unable to get tasks\n", NAME);
63 return;
64 }
65
66 printf("[taskid] [thrds] [resident] [virtual] [ucycles]"
67 " [kcycles] [name\n");
68
69 size_t i;
70 for (i = 0; i < count; i++) {
71 uint64_t resmem;
72 uint64_t virtmem;
73 uint64_t ucycles;
74 uint64_t kcycles;
75 const char *resmem_suffix;
76 const char *virtmem_suffix;
77 char usuffix;
78 char ksuffix;
79
80 bin_order_suffix(stats_tasks[i].resmem, &resmem, &resmem_suffix, true);
81 bin_order_suffix(stats_tasks[i].virtmem, &virtmem, &virtmem_suffix, true);
82 order_suffix(stats_tasks[i].ucycles, &ucycles, &usuffix);
83 order_suffix(stats_tasks[i].kcycles, &kcycles, &ksuffix);
84
85 printf("%-8" PRIu64 " %7zu %7" PRIu64 "%s %6" PRIu64 "%s"
86 " %8" PRIu64 "%c %8" PRIu64 "%c %s\n",
87 stats_tasks[i].task_id, stats_tasks[i].threads,
88 resmem, resmem_suffix, virtmem, virtmem_suffix,
89 ucycles, usuffix, kcycles, ksuffix, stats_tasks[i].name);
90 }
91
92 free(stats_tasks);
93}
94
95static void list_threads(task_id_t task_id, bool all)
96{
97 size_t count;
98 stats_thread_t *stats_threads = stats_get_threads(&count);
99
100 if (stats_threads == NULL) {
101 fprintf(stderr, "%s: Unable to get threads\n", NAME);
102 return;
103 }
104
105 printf("[taskid] [threadid] [state ] [prio] [cpu ] [ucycles] [kcycles]\n");
106
107 size_t i;
108 for (i = 0; i < count; i++) {
109 if ((all) || (stats_threads[i].task_id == task_id)) {
110 uint64_t ucycles, kcycles;
111 char usuffix, ksuffix;
112
113 order_suffix(stats_threads[i].ucycles, &ucycles, &usuffix);
114 order_suffix(stats_threads[i].kcycles, &kcycles, &ksuffix);
115
116 printf("%-8" PRIu64 " %-10" PRIu64 " %-8s %6d ",
117 stats_threads[i].task_id, stats_threads[i].thread_id,
118 thread_get_state(stats_threads[i].state),
119 stats_threads[i].priority);
120
121 if (stats_threads[i].on_cpu)
122 printf("%6u ", stats_threads[i].cpu);
123 else
124 printf("(none) ");
125
126 printf("%8" PRIu64"%c %8" PRIu64"%c\n",
127 ucycles, usuffix, kcycles, ksuffix);
128 }
129 }
130
131 free(stats_threads);
132}
133
134static void list_cpus(void)
135{
136 size_t count;
137 stats_cpu_t *cpus = stats_get_cpus(&count);
138
139 if (cpus == NULL) {
140 fprintf(stderr, "%s: Unable to get CPU statistics\n", NAME);
141 return;
142 }
143
144 printf("[id] [MHz ] [busy cycles] [idle cycles]\n");
145
146 size_t i;
147 for (i = 0; i < count; i++) {
148 printf("%-4u ", cpus[i].id);
149 if (cpus[i].active) {
150 uint64_t bcycles, icycles;
151 char bsuffix, isuffix;
152
153 order_suffix(cpus[i].busy_cycles, &bcycles, &bsuffix);
154 order_suffix(cpus[i].idle_cycles, &icycles, &isuffix);
155
156 printf("%10" PRIu16 " %12" PRIu64 "%c %12" PRIu64 "%c\n",
157 cpus[i].frequency_mhz, bcycles, bsuffix,
158 icycles, isuffix);
159 } else
160 printf("inactive\n");
161 }
162
163 free(cpus);
164}
165
166static void print_load(void)
167{
168 size_t count;
169 load_t *load = stats_get_load(&count);
170
171 if (load == NULL) {
172 fprintf(stderr, "%s: Unable to get load\n", NAME);
173 return;
174 }
175
176 printf("%s: Load average: ", NAME);
177
178 size_t i;
179 for (i = 0; i < count; i++) {
180 if (i > 0)
181 printf(" ");
182
183 stats_print_load_fragment(load[i], 2);
184 }
185
186 printf("\n");
187
188 free(load);
189}
190
191static void print_uptime(void)
192{
193 struct timeval uptime;
194 getuptime(&uptime);
195
196 printf("%s: Up %ld days, %ld hours, %ld minutes, %ld seconds\n",
197 NAME, uptime.tv_sec / DAY, (uptime.tv_sec % DAY) / HOUR,
198 (uptime.tv_sec % HOUR) / MINUTE, uptime.tv_sec % MINUTE);
199}
200
201static void usage(const char *name)
202{
203 printf(
204 "Usage: %s [-t task_id] [-a] [-c] [-l] [-u]\n" \
205 "\n" \
206 "Options:\n" \
207 "\t-t task_id\n" \
208 "\t--task=task_id\n" \
209 "\t\tList threads of the given task\n" \
210 "\n" \
211 "\t-a\n" \
212 "\t--all\n" \
213 "\t\tList all threads\n" \
214 "\n" \
215 "\t-c\n" \
216 "\t--cpus\n" \
217 "\t\tList CPUs\n" \
218 "\n" \
219 "\t-l\n" \
220 "\t--load\n" \
221 "\t\tPrint system load\n" \
222 "\n" \
223 "\t-u\n" \
224 "\t--uptime\n" \
225 "\t\tPrint system uptime\n" \
226 "\n" \
227 "\t-h\n" \
228 "\t--help\n" \
229 "\t\tPrint this usage information\n"
230 "\n" \
231 "Without any options all tasks are listed\n",
232 name
233 );
234}
235
236int main(int argc, char *argv[])
237{
238 bool toggle_tasks = true;
239 bool toggle_threads = false;
240 bool toggle_all = false;
241 bool toggle_cpus = false;
242 bool toggle_load = false;
243 bool toggle_uptime = false;
244
245 task_id_t task_id = 0;
246
247 int i;
248 for (i = 1; i < argc; i++) {
249 int off;
250
251 /* Usage */
252 if ((off = arg_parse_short_long(argv[i], "-h", "--help")) != -1) {
253 usage(argv[0]);
254 return 0;
255 }
256
257 /* All threads */
258 if ((off = arg_parse_short_long(argv[i], "-a", "--all")) != -1) {
259 toggle_tasks = false;
260 toggle_threads = true;
261 toggle_all = true;
262 continue;
263 }
264
265 /* CPUs */
266 if ((off = arg_parse_short_long(argv[i], "-c", "--cpus")) != -1) {
267 toggle_tasks = false;
268 toggle_cpus = true;
269 continue;
270 }
271
272 /* Threads */
273 if ((off = arg_parse_short_long(argv[i], "-t", "--task=")) != -1) {
274 // TODO: Support for 64b range
275 int tmp;
276 int ret = arg_parse_int(argc, argv, &i, &tmp, off);
277 if (ret != EOK) {
278 printf("%s: Malformed task_id '%s'\n", NAME, argv[i]);
279 return -1;
280 }
281
282 task_id = tmp;
283
284 toggle_tasks = false;
285 toggle_threads = true;
286 continue;
287 }
288
289 /* Load */
290 if ((off = arg_parse_short_long(argv[i], "-l", "--load")) != -1) {
291 toggle_tasks = false;
292 toggle_load = true;
293 continue;
294 }
295
296 /* Uptime */
297 if ((off = arg_parse_short_long(argv[i], "-u", "--uptime")) != -1) {
298 toggle_tasks = false;
299 toggle_uptime = true;
300 continue;
301 }
302 }
303
304 if (toggle_tasks)
305 list_tasks();
306
307 if (toggle_threads)
308 list_threads(task_id, toggle_all);
309
310 if (toggle_cpus)
311 list_cpus();
312
313 if (toggle_load)
314 print_load();
315
316 if (toggle_uptime)
317 print_uptime();
318
319 return 0;
320}
321
322/** @}
323 */
Note: See TracBrowser for help on using the repository browser.