source: mainline/uspace/app/tasks/tasks.c@ e535eeb

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

rename order() to order_suffix(), make it a generic libc string function in user space

  • Property mode set to 100644
File size: 6.9 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 tasks
31 * @brief Task lister.
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 <bool.h>
47#include <str.h>
48#include <arg_parse.h>
49
50#define NAME "tasks"
51
52#define TASK_COUNT 10
53#define THREAD_COUNT 50
54
55#define PRINT_LOAD1(x) ((x) >> 11)
56#define PRINT_LOAD2(x) (((x) & 0x7ff) / 2)
57
58static void list_tasks(void)
59{
60 size_t count;
61 task_id_t *ids =
62 (task_id_t *) stats_get_tasks(&count);
63
64 if (ids == NULL) {
65 fprintf(stderr, "%s: Unable to get tasks\n", NAME);
66 return;
67 }
68
69 printf(" ID Threads Mem uCycles kCycles Name\n");
70
71 size_t i;
72 for (i = 0; i < count; i++) {
73 stats_task_t *stats_task = stats_get_task(ids[i]);
74 if (stats_task != NULL) {
75 uint64_t virtmem, ucycles, kcycles;
76 char vmsuffix, usuffix, ksuffix;
77
78 order_suffix(stats_task->virtmem, &virtmem, &vmsuffix);
79 order_suffix(stats_task->ucycles, &ucycles, &usuffix);
80 order_suffix(stats_task->kcycles, &kcycles, &ksuffix);
81
82 printf("%8" PRIu64 "%8u %8" PRIu64"%c %12"
83 PRIu64 "%c %12" PRIu64 "%c %s\n", ids[i], stats_task->threads,
84 virtmem, vmsuffix, ucycles, usuffix, kcycles, ksuffix,
85 stats_task->name);
86
87 free(stats_task);
88 } else
89 printf("%8" PRIu64 "\n", ids[i]);
90 }
91
92 free(ids);
93}
94
95static void list_threads(task_id_t task_id, bool all)
96{
97 size_t count;
98 thread_id_t *ids =
99 (thread_id_t *) stats_get_threads(&count);
100
101 if (ids == NULL) {
102 fprintf(stderr, "%s: Unable to get threads\n", NAME);
103 return;
104 }
105
106 printf(" ID State CPU Prio [k]uCycles [k]kcycles Cycle fault\n");
107 size_t i;
108 for (i = 0; i < count; i++) {
109 stats_thread_t *stats_thread = stats_get_thread(ids[i]);
110 if (stats_thread != NULL) {
111 if ((all) || (stats_thread->task_id == task_id)) {
112 uint64_t ucycles, kcycles;
113 char usuffix, ksuffix;
114
115 order_suffix(stats_thread->ucycles, &ucycles, &usuffix);
116 order_suffix(stats_thread->kcycles, &kcycles, &ksuffix);
117
118 if (stats_thread->on_cpu) {
119 printf("%8" PRIu64 " %-8s %4u %6d %12"
120 PRIu64"%c %12" PRIu64"%c\n", ids[i],
121 thread_get_state(stats_thread->state),
122 stats_thread->cpu, stats_thread->priority,
123 ucycles, usuffix, kcycles, ksuffix);
124 } else {
125 printf("%8" PRIu64 " %-8s ---- %6d %12"
126 PRIu64"%c %12" PRIu64"%c\n", ids[i],
127 thread_get_state(stats_thread->state),
128 stats_thread->priority,
129 ucycles, usuffix, kcycles, ksuffix);
130 }
131 }
132
133 free(stats_thread);
134 } else if (all)
135 printf("%8" PRIu64 "\n", ids[i]);
136 }
137
138 free(ids);
139}
140
141static void print_load(void)
142{
143 size_t count;
144 load_t *load = stats_get_load(&count);
145
146 if (load == NULL) {
147 fprintf(stderr, "%s: Unable to get load\n", NAME);
148 return;
149 }
150
151 printf("%s: Load avarage: ", NAME);
152
153 size_t i;
154 for (i = 0; i < count; i++) {
155 if (i > 0)
156 printf(" ");
157
158 stats_print_load_fragment(load[i], 2);
159 }
160
161 printf("\n");
162
163 free(load);
164}
165
166static void list_cpus(void)
167{
168 size_t count;
169 stats_cpu_t *cpus = stats_get_cpus(&count);
170
171 if (cpus == NULL) {
172 fprintf(stderr, "%s: Unable to get CPU statistics\n", NAME);
173 return;
174 }
175
176 printf("%s: %u CPU(s) detected\n", NAME, count);
177
178 size_t i;
179 for (i = 0; i < count; i++) {
180 printf("cpu%u: %" PRIu16 " MHz, busy ticks: "
181 "%" PRIu64 ", idle ticks: %" PRIu64 "\n",
182 cpus[i].id, cpus[i].frequency_mhz, cpus[i].busy_ticks,
183 cpus[i].idle_ticks);
184 }
185
186 free(cpus);
187}
188
189static void usage()
190{
191 printf(
192 "Usage: tasks [-t task_id] [-a] [-l] [-c]\n" \
193 "\n" \
194 "Options:\n" \
195 "\t-t task_id\n" \
196 "\t--task=task_id\n" \
197 "\t\tList threads of the given task\n" \
198 "\n" \
199 "\t-a\n" \
200 "\t--all\n" \
201 "\t\tList all threads\n" \
202 "\n" \
203 "\t-l\n" \
204 "\t--load\n" \
205 "\t\tPrint system load\n" \
206 "\n" \
207 "\t-c\n" \
208 "\t--cpu\n" \
209 "\t\tList CPUs\n" \
210 "\n" \
211 "\t-h\n" \
212 "\t--help\n" \
213 "\t\tPrint this usage information\n"
214 "\n" \
215 "Without any options all tasks are listed\n"
216 );
217}
218
219int main(int argc, char *argv[])
220{
221 bool toggle_tasks = true;
222 bool toggle_threads = false;
223 bool toggle_all = false;
224 bool toggle_load = false;
225 bool toggle_cpus = false;
226
227 task_id_t task_id = 0;
228
229 int i;
230 for (i = 1; i < argc; i++) {
231 int off;
232
233 /* Usage */
234 if ((off = arg_parse_short_long(argv[i], "-h", "--help")) != -1) {
235 usage();
236 return 0;
237 }
238
239 /* All threads */
240 if ((off = arg_parse_short_long(argv[i], "-a", "--all")) != -1) {
241 toggle_tasks = false;
242 toggle_threads = true;
243 toggle_all = true;
244 continue;
245 }
246
247 /* Load */
248 if ((off = arg_parse_short_long(argv[i], "-l", "--load")) != -1) {
249 toggle_tasks = false;
250 toggle_load = true;
251 continue;
252 }
253
254 /* CPUs */
255 if ((off = arg_parse_short_long(argv[i], "-c", "--cpus")) != -1) {
256 toggle_tasks = false;
257 toggle_cpus = true;
258 continue;
259 }
260
261 /* Threads */
262 if ((off = arg_parse_short_long(argv[i], "-t", "--task=")) != -1) {
263 // TODO: Support for 64b range
264 int tmp;
265 int ret = arg_parse_int(argc, argv, &i, &tmp, off);
266 if (ret != EOK) {
267 printf("%s: Malformed task_id '%s'\n", NAME, argv[i]);
268 return -1;
269 }
270
271 task_id = tmp;
272
273 toggle_tasks = false;
274 toggle_threads = true;
275 continue;
276 }
277 }
278
279 if (toggle_tasks)
280 list_tasks();
281
282 if (toggle_threads)
283 list_threads(task_id, toggle_all);
284
285 if (toggle_load)
286 print_load();
287
288 if (toggle_cpus)
289 list_cpus();
290
291 return 0;
292}
293
294/** @}
295 */
Note: See TracBrowser for help on using the repository browser.