source: mainline/uspace/app/stats/stats.c@ 54618da

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 54618da was 62c4297, checked in by Jiří Zárevúcky <jiri.zarevucky@…>, 7 years ago

Remove some unnecessary includes.

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