source: mainline/uspace/app/top/top.c@ 142084b2

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

add initial support for sorting the output of top

  • Property mode set to 100644
File size: 11.4 KB
RevLine 
[5c058d50]1/*
2 * Copyright (c) 2010 Stanislav Kozina
[dec16a2]3 * Copyright (c) 2010 Martin Decky
[5c058d50]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 top
31 * @brief Top utility.
32 * @{
33 */
34/**
35 * @file
36 */
37
38#include <stdio.h>
39#include <stdlib.h>
40#include <unistd.h>
[79edc36]41#include <task.h>
42#include <thread.h>
43#include <sys/time.h>
[638927a]44#include <arch/barrier.h>
[dec16a2]45#include <errno.h>
[172aad6]46#include <sort.h>
[5c058d50]47#include "screen.h"
48#include "input.h"
[79edc36]49#include "top.h"
50
[dec16a2]51#define NAME "top"
[79edc36]52
[dec16a2]53#define UPDATE_INTERVAL 1
54
55#define DAY 86400
56#define HOUR 3600
57#define MINUTE 60
[79edc36]58
[b3b7e14a]59op_mode_t op_mode = OP_TASKS;
[172aad6]60sort_mode_t sort_mode = SORT_TASK_CYCLES;
[b3b7e14a]61bool excs_all = false;
[bdfd3c97]62
[dec16a2]63static const char *read_data(data_t *target)
[79edc36]64{
[dec16a2]65 /* Initialize data */
66 target->load = NULL;
67 target->cpus = NULL;
68 target->cpus_perc = NULL;
69 target->tasks = NULL;
70 target->tasks_perc = NULL;
[172aad6]71 target->tasks_map = NULL;
[dec16a2]72 target->threads = NULL;
[be06914]73 target->exceptions = NULL;
74 target->exceptions_perc = NULL;
[dec16a2]75 target->physmem = NULL;
[172aad6]76 target->ucycles_diff = NULL;
77 target->kcycles_diff = NULL;
78 target->ecycles_diff = NULL;
79 target->ecount_diff = NULL;
[dec16a2]80
81 /* Get current time */
[79edc36]82 struct timeval time;
[dec16a2]83 if (gettimeofday(&time, NULL) != EOK)
84 return "Cannot get time of day";
85
[79edc36]86 target->hours = (time.tv_sec % DAY) / HOUR;
87 target->minutes = (time.tv_sec % HOUR) / MINUTE;
88 target->seconds = time.tv_sec % MINUTE;
[dec16a2]89
90 /* Get uptime */
91 sysarg_t uptime = stats_get_uptime();
92 target->udays = uptime / DAY;
93 target->uhours = (uptime % DAY) / HOUR;
94 target->uminutes = (uptime % HOUR) / MINUTE;
95 target->useconds = uptime % MINUTE;
96
97 /* Get load */
98 target->load = stats_get_load(&(target->load_count));
99 if (target->load == NULL)
100 return "Cannot get system load";
101
102 /* Get CPUs */
103 target->cpus = stats_get_cpus(&(target->cpus_count));
104 if (target->cpus == NULL)
105 return "Cannot get CPUs";
106
107 target->cpus_perc =
108 (perc_cpu_t *) calloc(target->cpus_count, sizeof(perc_cpu_t));
109 if (target->cpus_perc == NULL)
110 return "Not enough memory for CPU utilization";
111
112 /* Get tasks */
113 target->tasks = stats_get_tasks(&(target->tasks_count));
114 if (target->tasks == NULL)
115 return "Cannot get tasks";
116
117 target->tasks_perc =
118 (perc_task_t *) calloc(target->tasks_count, sizeof(perc_task_t));
119 if (target->tasks_perc == NULL)
120 return "Not enough memory for task utilization";
121
[172aad6]122 target->tasks_map =
123 (size_t *) calloc(target->tasks_count, sizeof(size_t));
124 if (target->tasks_map == NULL)
125 return "Not enough memory for task map";
126
[dec16a2]127 /* Get threads */
128 target->threads = stats_get_threads(&(target->threads_count));
129 if (target->threads == NULL)
130 return "Cannot get threads";
131
[be06914]132 /* Get Exceptions */
[8eec3c8]133 target->exceptions = stats_get_exceptions(&(target->exceptions_count));
134 if (target->exceptions == NULL)
135 return "Cannot get exceptions";
136
[be06914]137 target->exceptions_perc =
138 (perc_exc_t *) calloc(target->exceptions_count, sizeof(perc_exc_t));
139 if (target->exceptions_perc == NULL)
140 return "Not enough memory for exception utilization";
141
[dec16a2]142 /* Get physical memory */
143 target->physmem = stats_get_physmem();
144 if (target->physmem == NULL)
145 return "Cannot get physical memory";
146
[172aad6]147 target->ucycles_diff = calloc(target->tasks_count,
[be06914]148 sizeof(uint64_t));
[172aad6]149 if (target->ucycles_diff == NULL)
[dec16a2]150 return "Not enough memory for user utilization";
151
[172aad6]152 /* Allocate memory for computed values */
153 target->kcycles_diff = calloc(target->tasks_count,
[be06914]154 sizeof(uint64_t));
[172aad6]155 if (target->kcycles_diff == NULL)
[dec16a2]156 return "Not enough memory for kernel utilization";
157
[172aad6]158 target->ecycles_diff = calloc(target->exceptions_count,
[be06914]159 sizeof(uint64_t));
[172aad6]160 if (target->ecycles_diff == NULL)
[be06914]161 return "Not enough memory for exception cycles utilization";
162
[172aad6]163 target->ecount_diff = calloc(target->exceptions_count,
[be06914]164 sizeof(uint64_t));
[172aad6]165 if (target->ecount_diff == NULL)
[be06914]166 return "Not enough memory for exception count utilization";
167
[172aad6]168 return NULL;
169}
170
171/** Computes percentage differencies from old_data to new_data
172 *
173 * @param old_data Pointer to old data strucutre.
174 * @param new_data Pointer to actual data where percetages are stored.
175 *
176 */
177static void compute_percentages(data_t *old_data, data_t *new_data)
178{
[d0c82c5]179 /* For each CPU: Compute total cycles and divide it between
[dec16a2]180 user and kernel */
181
182 size_t i;
183 for (i = 0; i < new_data->cpus_count; i++) {
184 uint64_t idle =
[d0c82c5]185 new_data->cpus[i].idle_cycles - old_data->cpus[i].idle_cycles;
[dec16a2]186 uint64_t busy =
[d0c82c5]187 new_data->cpus[i].busy_cycles - old_data->cpus[i].busy_cycles;
[ee35ba0b]188 uint64_t sum = idle + busy;
[dec16a2]189
190 FRACTION_TO_FLOAT(new_data->cpus_perc[i].idle, idle * 100, sum);
191 FRACTION_TO_FLOAT(new_data->cpus_perc[i].busy, busy * 100, sum);
[ee35ba0b]192 }
[dec16a2]193
[452268a1]194 /* For all tasks compute sum and differencies of all cycles */
[dec16a2]195
[be06914]196 uint64_t virtmem_total = 0;
197 uint64_t ucycles_total = 0;
198 uint64_t kcycles_total = 0;
[dec16a2]199
200 for (i = 0; i < new_data->tasks_count; i++) {
201 /* Match task with the previous instance */
202
203 bool found = false;
204 size_t j;
205 for (j = 0; j < old_data->tasks_count; j++) {
206 if (new_data->tasks[i].task_id == old_data->tasks[j].task_id) {
207 found = true;
208 break;
209 }
210 }
211
212 if (!found) {
[452268a1]213 /* This is newly borned task, ignore it */
[172aad6]214 new_data->ucycles_diff[i] = 0;
215 new_data->kcycles_diff[i] = 0;
[452268a1]216 continue;
217 }
[dec16a2]218
[172aad6]219 new_data->ucycles_diff[i] =
[dec16a2]220 new_data->tasks[i].ucycles - old_data->tasks[j].ucycles;
[172aad6]221 new_data->kcycles_diff[i] =
[dec16a2]222 new_data->tasks[i].kcycles - old_data->tasks[j].kcycles;
223
224 virtmem_total += new_data->tasks[i].virtmem;
[172aad6]225 ucycles_total += new_data->ucycles_diff[i];
226 kcycles_total += new_data->kcycles_diff[i];
[452268a1]227 }
[dec16a2]228
[be06914]229 /* For each task compute percential change */
[dec16a2]230
231 for (i = 0; i < new_data->tasks_count; i++) {
232 FRACTION_TO_FLOAT(new_data->tasks_perc[i].virtmem,
233 new_data->tasks[i].virtmem * 100, virtmem_total);
234 FRACTION_TO_FLOAT(new_data->tasks_perc[i].ucycles,
[172aad6]235 new_data->ucycles_diff[i] * 100, ucycles_total);
[dec16a2]236 FRACTION_TO_FLOAT(new_data->tasks_perc[i].kcycles,
[172aad6]237 new_data->kcycles_diff[i] * 100, kcycles_total);
[452268a1]238 }
[dec16a2]239
[be06914]240 /* For all exceptions compute sum and differencies of cycles */
241
242 uint64_t ecycles_total = 0;
243 uint64_t ecount_total = 0;
244
245 for (i = 0; i < new_data->exceptions_count; i++) {
246 /*
247 * March exception with the previous instance.
248 * This is quite paranoid since exceptions do not
249 * usually disappear, but it does not hurt.
250 */
251
252 bool found = false;
253 size_t j;
254 for (j = 0; j < old_data->exceptions_count; j++) {
255 if (new_data->exceptions[i].id == old_data->exceptions[j].id) {
256 found = true;
257 break;
258 }
259 }
260
261 if (!found) {
262 /* This is a new exception, ignore it */
[172aad6]263 new_data->ecycles_diff[i] = 0;
264 new_data->ecount_diff[i] = 0;
[be06914]265 continue;
266 }
267
[172aad6]268 new_data->ecycles_diff[i] =
[be06914]269 new_data->exceptions[i].cycles - old_data->exceptions[j].cycles;
[172aad6]270 new_data->ecount_diff[i] =
[be06914]271 new_data->exceptions[i].count - old_data->exceptions[i].count;
272
[172aad6]273 ecycles_total += new_data->ecycles_diff[i];
274 ecount_total += new_data->ecount_diff[i];
[be06914]275 }
276
277 /* For each exception compute percential change */
278
279 for (i = 0; i < new_data->exceptions_count; i++) {
280 FRACTION_TO_FLOAT(new_data->exceptions_perc[i].cycles,
[172aad6]281 new_data->ecycles_diff[i] * 100, ecycles_total);
[be06914]282 FRACTION_TO_FLOAT(new_data->exceptions_perc[i].count,
[172aad6]283 new_data->ecount_diff[i] * 100, ecount_total);
[be06914]284 }
[172aad6]285}
286
287static int cmp_data(void *a, void *b, void *arg)
288{
289 size_t ia = *((size_t *) a);
290 size_t ib = *((size_t *) b);
291 data_t *data = (data_t *) arg;
[be06914]292
[172aad6]293 uint64_t acycles = data->ucycles_diff[ia] + data->kcycles_diff[ia];
294 uint64_t bcycles = data->ucycles_diff[ib] + data->kcycles_diff[ib];
[dec16a2]295
[172aad6]296 if (acycles > bcycles)
297 return -1;
[dec16a2]298
[172aad6]299 if (acycles < bcycles)
300 return 1;
301
302 return 0;
303}
304
305static void sort_data(data_t *data)
306{
307 size_t i;
308
309 for (i = 0; i < data->tasks_count; i++)
310 data->tasks_map[i] = i;
311
312 qsort((void *) data->tasks_map, data->tasks_count,
313 sizeof(size_t), cmp_data, (void *) data);
[ee35ba0b]314}
315
[8b2aba5]316static void free_data(data_t *target)
317{
[dec16a2]318 if (target->load != NULL)
319 free(target->load);
320
321 if (target->cpus != NULL)
322 free(target->cpus);
323
324 if (target->cpus_perc != NULL)
325 free(target->cpus_perc);
326
327 if (target->tasks != NULL)
328 free(target->tasks);
329
330 if (target->tasks_perc != NULL)
331 free(target->tasks_perc);
332
333 if (target->threads != NULL)
334 free(target->threads);
335
[8eec3c8]336 if (target->exceptions != NULL)
337 free(target->exceptions);
338
[be06914]339 if (target->exceptions_perc != NULL)
340 free(target->exceptions_perc);
341
[dec16a2]342 if (target->physmem != NULL)
343 free(target->physmem);
[172aad6]344
345 if (target->ucycles_diff != NULL)
346 free(target->ucycles_diff);
347
348 if (target->kcycles_diff != NULL)
349 free(target->kcycles_diff);
350
351 if (target->ecycles_diff != NULL)
352 free(target->ecycles_diff);
353
354 if (target->ecount_diff != NULL)
355 free(target->ecount_diff);
[8b2aba5]356}
357
[5c058d50]358int main(int argc, char *argv[])
359{
[dec16a2]360 data_t data;
361 data_t data_prev;
362 const char *ret = NULL;
363
[ee35ba0b]364 screen_init();
[79edc36]365 printf("Reading initial data...\n");
[dec16a2]366
367 if ((ret = read_data(&data_prev)) != NULL)
368 goto out;
369
[ee35ba0b]370 /* Compute some rubbish to have initialised values */
[172aad6]371 compute_percentages(&data_prev, &data_prev);
[dec16a2]372
373 /* And paint screen until death */
[5c058d50]374 while (true) {
[9e05055]375 int c = tgetchar(UPDATE_INTERVAL);
[5c058d50]376 if (c < 0) {
[dec16a2]377 if ((ret = read_data(&data)) != NULL) {
378 free_data(&data);
379 goto out;
380 }
381
[172aad6]382 compute_percentages(&data_prev, &data);
383 sort_data(&data);
[dec16a2]384 print_data(&data);
385 free_data(&data_prev);
386 data_prev = data;
387
[5c058d50]388 continue;
389 }
[dec16a2]390
[5c058d50]391 switch (c) {
[bdfd3c97]392 case 't':
[dec16a2]393 print_warning("Showing task statistics");
[b3b7e14a]394 op_mode = OP_TASKS;
395 break;
396 case 'i':
397 print_warning("Showing IPC statistics");
398 op_mode = OP_IPC;
[bdfd3c97]399 break;
[8eec3c8]400 case 'e':
401 print_warning("Showing exception statistics");
[b3b7e14a]402 op_mode = OP_EXCS;
403 break;
404 case 'h':
405 print_warning("Showing help");
406 op_mode = OP_HELP;
[8eec3c8]407 break;
[b3b7e14a]408 case 'q':
409 goto out;
410 case 'a':
411 if (op_mode == OP_EXCS) {
412 excs_all = !excs_all;
413 if (excs_all)
414 print_warning("Showing all exceptions");
415 else
416 print_warning("Showing only hot exceptions");
417 break;
418 }
[5c058d50]419 default:
[b3b7e14a]420 print_warning("Unknown command \"%c\", use \"h\" for help", c);
[5c058d50]421 break;
422 }
423 }
[dec16a2]424
425out:
426 screen_done();
427 free_data(&data_prev);
428
429 if (ret != NULL) {
430 fprintf(stderr, "%s: %s\n", NAME, ret);
431 return 1;
432 }
433
[5c058d50]434 return 0;
435}
436
437/** @}
438 */
Note: See TracBrowser for help on using the repository browser.