[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>
|
---|
[dec16a2] | 44 | #include <errno.h>
|
---|
[172aad6] | 45 | #include <sort.h>
|
---|
[5c058d50] | 46 | #include "screen.h"
|
---|
[79edc36] | 47 | #include "top.h"
|
---|
| 48 |
|
---|
[dec16a2] | 49 | #define NAME "top"
|
---|
[79edc36] | 50 |
|
---|
[dec16a2] | 51 | #define UPDATE_INTERVAL 1
|
---|
| 52 |
|
---|
| 53 | #define DAY 86400
|
---|
| 54 | #define HOUR 3600
|
---|
| 55 | #define MINUTE 60
|
---|
[79edc36] | 56 |
|
---|
[f682f5a] | 57 | typedef enum {
|
---|
| 58 | OP_TASKS,
|
---|
| 59 | OP_IPC,
|
---|
| 60 | OP_EXCS,
|
---|
| 61 | } op_mode_t;
|
---|
| 62 |
|
---|
| 63 | static const column_t task_columns[] = {
|
---|
| 64 | {"taskid", 't', 8},
|
---|
| 65 | {"thrds", 'h', 7},
|
---|
| 66 | {"resident", 'r', 10},
|
---|
| 67 | {"%resi", 'R', 7},
|
---|
| 68 | {"virtual", 'v', 9},
|
---|
| 69 | {"%virt", 'V', 7},
|
---|
| 70 | {"%user", 'U', 7},
|
---|
| 71 | {"%kern", 'K', 7},
|
---|
| 72 | {"name", 'd', 0},
|
---|
| 73 | };
|
---|
| 74 |
|
---|
| 75 | enum {
|
---|
| 76 | TASK_COL_ID = 0,
|
---|
| 77 | TASK_COL_NUM_THREADS,
|
---|
| 78 | TASK_COL_RESIDENT,
|
---|
| 79 | TASK_COL_PERCENT_RESIDENT,
|
---|
| 80 | TASK_COL_VIRTUAL,
|
---|
| 81 | TASK_COL_PERCENT_VIRTUAL,
|
---|
| 82 | TASK_COL_PERCENT_USER,
|
---|
| 83 | TASK_COL_PERCENT_KERNEL,
|
---|
| 84 | TASK_COL_NAME,
|
---|
| 85 | TASK_NUM_COLUMNS,
|
---|
| 86 | };
|
---|
| 87 |
|
---|
| 88 | static const column_t ipc_columns[] = {
|
---|
| 89 | {"taskid", 't', 8},
|
---|
| 90 | {"cls snt", 'c', 9},
|
---|
| 91 | {"cls rcv", 'C', 9},
|
---|
| 92 | {"ans snt", 'a', 9},
|
---|
| 93 | {"ans rcv", 'A', 9},
|
---|
| 94 | {"forward", 'f', 9},
|
---|
| 95 | {"name", 'd', 0},
|
---|
| 96 | };
|
---|
| 97 |
|
---|
| 98 | enum {
|
---|
| 99 | IPC_COL_TASKID = 0,
|
---|
| 100 | IPC_COL_CLS_SNT,
|
---|
| 101 | IPC_COL_CLS_RCV,
|
---|
| 102 | IPC_COL_ANS_SNT,
|
---|
| 103 | IPC_COL_ANS_RCV,
|
---|
| 104 | IPC_COL_FORWARD,
|
---|
| 105 | IPC_COL_NAME,
|
---|
| 106 | IPC_NUM_COLUMNS,
|
---|
| 107 | };
|
---|
| 108 |
|
---|
| 109 | static const column_t exception_columns[] = {
|
---|
| 110 | {"exc", 'e', 8},
|
---|
| 111 | {"count", 'n', 10},
|
---|
| 112 | {"%count", 'N', 8},
|
---|
| 113 | {"cycles", 'c', 10},
|
---|
| 114 | {"%cycles", 'C', 9},
|
---|
| 115 | {"description", 'd', 0},
|
---|
| 116 | };
|
---|
| 117 |
|
---|
| 118 | enum {
|
---|
| 119 | EXCEPTION_COL_ID = 0,
|
---|
| 120 | EXCEPTION_COL_COUNT,
|
---|
| 121 | EXCEPTION_COL_PERCENT_COUNT,
|
---|
| 122 | EXCEPTION_COL_CYCLES,
|
---|
| 123 | EXCEPTION_COL_PERCENT_CYCLES,
|
---|
| 124 | EXCEPTION_COL_DESCRIPTION,
|
---|
| 125 | EXCEPTION_NUM_COLUMNS,
|
---|
| 126 | };
|
---|
[bdfd3c97] | 127 |
|
---|
[b8d6783] | 128 | screen_mode_t screen_mode = SCREEN_TABLE;
|
---|
| 129 | static op_mode_t op_mode = OP_TASKS;
|
---|
| 130 | static size_t sort_column = TASK_COL_PERCENT_USER;
|
---|
| 131 | static int sort_reverse = -1;
|
---|
| 132 | static bool excs_all = false;
|
---|
| 133 |
|
---|
[dec16a2] | 134 | static const char *read_data(data_t *target)
|
---|
[79edc36] | 135 | {
|
---|
[dec16a2] | 136 | /* Initialize data */
|
---|
| 137 | target->load = NULL;
|
---|
| 138 | target->cpus = NULL;
|
---|
| 139 | target->cpus_perc = NULL;
|
---|
| 140 | target->tasks = NULL;
|
---|
| 141 | target->tasks_perc = NULL;
|
---|
| 142 | target->threads = NULL;
|
---|
[be06914] | 143 | target->exceptions = NULL;
|
---|
| 144 | target->exceptions_perc = NULL;
|
---|
[dec16a2] | 145 | target->physmem = NULL;
|
---|
[172aad6] | 146 | target->ucycles_diff = NULL;
|
---|
| 147 | target->kcycles_diff = NULL;
|
---|
| 148 | target->ecycles_diff = NULL;
|
---|
| 149 | target->ecount_diff = NULL;
|
---|
[f682f5a] | 150 | target->table.name = NULL;
|
---|
| 151 | target->table.num_columns = 0;
|
---|
| 152 | target->table.columns = NULL;
|
---|
| 153 | target->table.num_fields = 0;
|
---|
| 154 | target->table.fields = NULL;
|
---|
[dec16a2] | 155 |
|
---|
| 156 | /* Get current time */
|
---|
[79edc36] | 157 | struct timeval time;
|
---|
[dec16a2] | 158 | if (gettimeofday(&time, NULL) != EOK)
|
---|
| 159 | return "Cannot get time of day";
|
---|
| 160 |
|
---|
[79edc36] | 161 | target->hours = (time.tv_sec % DAY) / HOUR;
|
---|
| 162 | target->minutes = (time.tv_sec % HOUR) / MINUTE;
|
---|
| 163 | target->seconds = time.tv_sec % MINUTE;
|
---|
[dec16a2] | 164 |
|
---|
| 165 | /* Get uptime */
|
---|
| 166 | sysarg_t uptime = stats_get_uptime();
|
---|
| 167 | target->udays = uptime / DAY;
|
---|
| 168 | target->uhours = (uptime % DAY) / HOUR;
|
---|
| 169 | target->uminutes = (uptime % HOUR) / MINUTE;
|
---|
| 170 | target->useconds = uptime % MINUTE;
|
---|
| 171 |
|
---|
| 172 | /* Get load */
|
---|
| 173 | target->load = stats_get_load(&(target->load_count));
|
---|
| 174 | if (target->load == NULL)
|
---|
| 175 | return "Cannot get system load";
|
---|
| 176 |
|
---|
| 177 | /* Get CPUs */
|
---|
| 178 | target->cpus = stats_get_cpus(&(target->cpus_count));
|
---|
| 179 | if (target->cpus == NULL)
|
---|
| 180 | return "Cannot get CPUs";
|
---|
| 181 |
|
---|
| 182 | target->cpus_perc =
|
---|
| 183 | (perc_cpu_t *) calloc(target->cpus_count, sizeof(perc_cpu_t));
|
---|
| 184 | if (target->cpus_perc == NULL)
|
---|
| 185 | return "Not enough memory for CPU utilization";
|
---|
| 186 |
|
---|
| 187 | /* Get tasks */
|
---|
| 188 | target->tasks = stats_get_tasks(&(target->tasks_count));
|
---|
| 189 | if (target->tasks == NULL)
|
---|
| 190 | return "Cannot get tasks";
|
---|
| 191 |
|
---|
| 192 | target->tasks_perc =
|
---|
| 193 | (perc_task_t *) calloc(target->tasks_count, sizeof(perc_task_t));
|
---|
| 194 | if (target->tasks_perc == NULL)
|
---|
| 195 | return "Not enough memory for task utilization";
|
---|
| 196 |
|
---|
| 197 | /* Get threads */
|
---|
| 198 | target->threads = stats_get_threads(&(target->threads_count));
|
---|
| 199 | if (target->threads == NULL)
|
---|
| 200 | return "Cannot get threads";
|
---|
| 201 |
|
---|
[be06914] | 202 | /* Get Exceptions */
|
---|
[8eec3c8] | 203 | target->exceptions = stats_get_exceptions(&(target->exceptions_count));
|
---|
| 204 | if (target->exceptions == NULL)
|
---|
| 205 | return "Cannot get exceptions";
|
---|
| 206 |
|
---|
[be06914] | 207 | target->exceptions_perc =
|
---|
| 208 | (perc_exc_t *) calloc(target->exceptions_count, sizeof(perc_exc_t));
|
---|
| 209 | if (target->exceptions_perc == NULL)
|
---|
| 210 | return "Not enough memory for exception utilization";
|
---|
| 211 |
|
---|
[dec16a2] | 212 | /* Get physical memory */
|
---|
| 213 | target->physmem = stats_get_physmem();
|
---|
| 214 | if (target->physmem == NULL)
|
---|
| 215 | return "Cannot get physical memory";
|
---|
| 216 |
|
---|
[172aad6] | 217 | target->ucycles_diff = calloc(target->tasks_count,
|
---|
[be06914] | 218 | sizeof(uint64_t));
|
---|
[172aad6] | 219 | if (target->ucycles_diff == NULL)
|
---|
[dec16a2] | 220 | return "Not enough memory for user utilization";
|
---|
| 221 |
|
---|
[172aad6] | 222 | /* Allocate memory for computed values */
|
---|
| 223 | target->kcycles_diff = calloc(target->tasks_count,
|
---|
[be06914] | 224 | sizeof(uint64_t));
|
---|
[172aad6] | 225 | if (target->kcycles_diff == NULL)
|
---|
[dec16a2] | 226 | return "Not enough memory for kernel utilization";
|
---|
| 227 |
|
---|
[172aad6] | 228 | target->ecycles_diff = calloc(target->exceptions_count,
|
---|
[be06914] | 229 | sizeof(uint64_t));
|
---|
[172aad6] | 230 | if (target->ecycles_diff == NULL)
|
---|
[be06914] | 231 | return "Not enough memory for exception cycles utilization";
|
---|
| 232 |
|
---|
[172aad6] | 233 | target->ecount_diff = calloc(target->exceptions_count,
|
---|
[be06914] | 234 | sizeof(uint64_t));
|
---|
[172aad6] | 235 | if (target->ecount_diff == NULL)
|
---|
[be06914] | 236 | return "Not enough memory for exception count utilization";
|
---|
| 237 |
|
---|
[172aad6] | 238 | return NULL;
|
---|
| 239 | }
|
---|
| 240 |
|
---|
| 241 | /** Computes percentage differencies from old_data to new_data
|
---|
| 242 | *
|
---|
| 243 | * @param old_data Pointer to old data strucutre.
|
---|
| 244 | * @param new_data Pointer to actual data where percetages are stored.
|
---|
| 245 | *
|
---|
| 246 | */
|
---|
| 247 | static void compute_percentages(data_t *old_data, data_t *new_data)
|
---|
| 248 | {
|
---|
[d0c82c5] | 249 | /* For each CPU: Compute total cycles and divide it between
|
---|
[dec16a2] | 250 | user and kernel */
|
---|
| 251 |
|
---|
| 252 | size_t i;
|
---|
| 253 | for (i = 0; i < new_data->cpus_count; i++) {
|
---|
| 254 | uint64_t idle =
|
---|
[d0c82c5] | 255 | new_data->cpus[i].idle_cycles - old_data->cpus[i].idle_cycles;
|
---|
[dec16a2] | 256 | uint64_t busy =
|
---|
[d0c82c5] | 257 | new_data->cpus[i].busy_cycles - old_data->cpus[i].busy_cycles;
|
---|
[ee35ba0b] | 258 | uint64_t sum = idle + busy;
|
---|
[dec16a2] | 259 |
|
---|
| 260 | FRACTION_TO_FLOAT(new_data->cpus_perc[i].idle, idle * 100, sum);
|
---|
| 261 | FRACTION_TO_FLOAT(new_data->cpus_perc[i].busy, busy * 100, sum);
|
---|
[ee35ba0b] | 262 | }
|
---|
[dec16a2] | 263 |
|
---|
[452268a1] | 264 | /* For all tasks compute sum and differencies of all cycles */
|
---|
[dec16a2] | 265 |
|
---|
[be06914] | 266 | uint64_t virtmem_total = 0;
|
---|
[a0ce870] | 267 | uint64_t resmem_total = 0;
|
---|
[be06914] | 268 | uint64_t ucycles_total = 0;
|
---|
| 269 | uint64_t kcycles_total = 0;
|
---|
[dec16a2] | 270 |
|
---|
| 271 | for (i = 0; i < new_data->tasks_count; i++) {
|
---|
| 272 | /* Match task with the previous instance */
|
---|
| 273 |
|
---|
| 274 | bool found = false;
|
---|
| 275 | size_t j;
|
---|
| 276 | for (j = 0; j < old_data->tasks_count; j++) {
|
---|
| 277 | if (new_data->tasks[i].task_id == old_data->tasks[j].task_id) {
|
---|
| 278 | found = true;
|
---|
| 279 | break;
|
---|
| 280 | }
|
---|
| 281 | }
|
---|
| 282 |
|
---|
| 283 | if (!found) {
|
---|
[452268a1] | 284 | /* This is newly borned task, ignore it */
|
---|
[172aad6] | 285 | new_data->ucycles_diff[i] = 0;
|
---|
| 286 | new_data->kcycles_diff[i] = 0;
|
---|
[452268a1] | 287 | continue;
|
---|
| 288 | }
|
---|
[dec16a2] | 289 |
|
---|
[172aad6] | 290 | new_data->ucycles_diff[i] =
|
---|
[dec16a2] | 291 | new_data->tasks[i].ucycles - old_data->tasks[j].ucycles;
|
---|
[172aad6] | 292 | new_data->kcycles_diff[i] =
|
---|
[dec16a2] | 293 | new_data->tasks[i].kcycles - old_data->tasks[j].kcycles;
|
---|
| 294 |
|
---|
| 295 | virtmem_total += new_data->tasks[i].virtmem;
|
---|
[a0ce870] | 296 | resmem_total += new_data->tasks[i].resmem;
|
---|
[172aad6] | 297 | ucycles_total += new_data->ucycles_diff[i];
|
---|
| 298 | kcycles_total += new_data->kcycles_diff[i];
|
---|
[452268a1] | 299 | }
|
---|
[dec16a2] | 300 |
|
---|
[be06914] | 301 | /* For each task compute percential change */
|
---|
[dec16a2] | 302 |
|
---|
| 303 | for (i = 0; i < new_data->tasks_count; i++) {
|
---|
| 304 | FRACTION_TO_FLOAT(new_data->tasks_perc[i].virtmem,
|
---|
| 305 | new_data->tasks[i].virtmem * 100, virtmem_total);
|
---|
[a0ce870] | 306 | FRACTION_TO_FLOAT(new_data->tasks_perc[i].resmem,
|
---|
| 307 | new_data->tasks[i].resmem * 100, resmem_total);
|
---|
[dec16a2] | 308 | FRACTION_TO_FLOAT(new_data->tasks_perc[i].ucycles,
|
---|
[172aad6] | 309 | new_data->ucycles_diff[i] * 100, ucycles_total);
|
---|
[dec16a2] | 310 | FRACTION_TO_FLOAT(new_data->tasks_perc[i].kcycles,
|
---|
[172aad6] | 311 | new_data->kcycles_diff[i] * 100, kcycles_total);
|
---|
[452268a1] | 312 | }
|
---|
[dec16a2] | 313 |
|
---|
[be06914] | 314 | /* For all exceptions compute sum and differencies of cycles */
|
---|
| 315 |
|
---|
| 316 | uint64_t ecycles_total = 0;
|
---|
| 317 | uint64_t ecount_total = 0;
|
---|
| 318 |
|
---|
| 319 | for (i = 0; i < new_data->exceptions_count; i++) {
|
---|
| 320 | /*
|
---|
| 321 | * March exception with the previous instance.
|
---|
| 322 | * This is quite paranoid since exceptions do not
|
---|
| 323 | * usually disappear, but it does not hurt.
|
---|
| 324 | */
|
---|
| 325 |
|
---|
| 326 | bool found = false;
|
---|
| 327 | size_t j;
|
---|
| 328 | for (j = 0; j < old_data->exceptions_count; j++) {
|
---|
| 329 | if (new_data->exceptions[i].id == old_data->exceptions[j].id) {
|
---|
| 330 | found = true;
|
---|
| 331 | break;
|
---|
| 332 | }
|
---|
| 333 | }
|
---|
| 334 |
|
---|
| 335 | if (!found) {
|
---|
| 336 | /* This is a new exception, ignore it */
|
---|
[172aad6] | 337 | new_data->ecycles_diff[i] = 0;
|
---|
| 338 | new_data->ecount_diff[i] = 0;
|
---|
[be06914] | 339 | continue;
|
---|
| 340 | }
|
---|
| 341 |
|
---|
[172aad6] | 342 | new_data->ecycles_diff[i] =
|
---|
[be06914] | 343 | new_data->exceptions[i].cycles - old_data->exceptions[j].cycles;
|
---|
[172aad6] | 344 | new_data->ecount_diff[i] =
|
---|
[be06914] | 345 | new_data->exceptions[i].count - old_data->exceptions[i].count;
|
---|
| 346 |
|
---|
[172aad6] | 347 | ecycles_total += new_data->ecycles_diff[i];
|
---|
| 348 | ecount_total += new_data->ecount_diff[i];
|
---|
[be06914] | 349 | }
|
---|
| 350 |
|
---|
| 351 | /* For each exception compute percential change */
|
---|
| 352 |
|
---|
| 353 | for (i = 0; i < new_data->exceptions_count; i++) {
|
---|
| 354 | FRACTION_TO_FLOAT(new_data->exceptions_perc[i].cycles,
|
---|
[172aad6] | 355 | new_data->ecycles_diff[i] * 100, ecycles_total);
|
---|
[be06914] | 356 | FRACTION_TO_FLOAT(new_data->exceptions_perc[i].count,
|
---|
[172aad6] | 357 | new_data->ecount_diff[i] * 100, ecount_total);
|
---|
[be06914] | 358 | }
|
---|
[172aad6] | 359 | }
|
---|
| 360 |
|
---|
| 361 | static int cmp_data(void *a, void *b, void *arg)
|
---|
| 362 | {
|
---|
[b8d6783] | 363 | field_t *fa = (field_t *)a + sort_column;
|
---|
| 364 | field_t *fb = (field_t *)b + sort_column;
|
---|
| 365 |
|
---|
| 366 | if (fa->type > fb->type)
|
---|
| 367 | return 1 * sort_reverse;
|
---|
| 368 |
|
---|
| 369 | if (fa->type < fb->type)
|
---|
| 370 | return -1 * sort_reverse;
|
---|
| 371 |
|
---|
| 372 | switch (fa->type) {
|
---|
[d76a329] | 373 | case FIELD_EMPTY:
|
---|
| 374 | return 0;
|
---|
| 375 | case FIELD_UINT_SUFFIX_BIN: /* fallthrough */
|
---|
| 376 | case FIELD_UINT_SUFFIX_DEC: /* fallthrough */
|
---|
| 377 | case FIELD_UINT:
|
---|
| 378 | if (fa->uint > fb->uint)
|
---|
| 379 | return 1 * sort_reverse;
|
---|
| 380 | if (fa->uint < fb->uint)
|
---|
| 381 | return -1 * sort_reverse;
|
---|
| 382 | return 0;
|
---|
| 383 | case FIELD_PERCENT:
|
---|
| 384 | if (fa->fixed.upper * fb->fixed.lower
|
---|
| 385 | > fb->fixed.upper * fa->fixed.lower)
|
---|
| 386 | return 1 * sort_reverse;
|
---|
| 387 | if (fa->fixed.upper * fb->fixed.lower
|
---|
| 388 | < fb->fixed.upper * fa->fixed.lower)
|
---|
| 389 | return -1 * sort_reverse;
|
---|
| 390 | return 0;
|
---|
| 391 | case FIELD_STRING:
|
---|
| 392 | return str_cmp(fa->string, fb->string) * sort_reverse;
|
---|
[b8d6783] | 393 | }
|
---|
| 394 |
|
---|
[172aad6] | 395 | return 0;
|
---|
| 396 | }
|
---|
| 397 |
|
---|
[b8d6783] | 398 | static void sort_table(table_t *table)
|
---|
[172aad6] | 399 | {
|
---|
[b8d6783] | 400 | if (sort_column >= table->num_columns)
|
---|
| 401 | sort_column = 0;
|
---|
| 402 | /* stable sort is probably best, so we use gsort */
|
---|
| 403 | gsort((void *) table->fields, table->num_fields / table->num_columns,
|
---|
| 404 | sizeof(field_t) * table->num_columns, cmp_data, NULL);
|
---|
[ee35ba0b] | 405 | }
|
---|
| 406 |
|
---|
[f682f5a] | 407 | static const char *fill_task_table(data_t *data)
|
---|
| 408 | {
|
---|
| 409 | data->table.name = "Tasks";
|
---|
| 410 | data->table.num_columns = TASK_NUM_COLUMNS;
|
---|
| 411 | data->table.columns = task_columns;
|
---|
| 412 | data->table.num_fields = data->tasks_count * TASK_NUM_COLUMNS;
|
---|
| 413 | data->table.fields = calloc(data->table.num_fields,
|
---|
| 414 | sizeof(field_t));
|
---|
| 415 | if (data->table.fields == NULL)
|
---|
| 416 | return "Not enough memory for table fields";
|
---|
| 417 |
|
---|
| 418 | field_t *field = data->table.fields;
|
---|
| 419 | for (size_t i = 0; i < data->tasks_count; i++) {
|
---|
[b8d6783] | 420 | stats_task_t *task = &data->tasks[i];
|
---|
| 421 | perc_task_t *perc = &data->tasks_perc[i];
|
---|
[f682f5a] | 422 | field[TASK_COL_ID].type = FIELD_UINT;
|
---|
| 423 | field[TASK_COL_ID].uint = task->task_id;
|
---|
| 424 | field[TASK_COL_NUM_THREADS].type = FIELD_UINT;
|
---|
| 425 | field[TASK_COL_NUM_THREADS].uint = task->threads;
|
---|
| 426 | field[TASK_COL_RESIDENT].type = FIELD_UINT_SUFFIX_BIN;
|
---|
| 427 | field[TASK_COL_RESIDENT].uint = task->resmem;
|
---|
| 428 | field[TASK_COL_PERCENT_RESIDENT].type = FIELD_PERCENT;
|
---|
| 429 | field[TASK_COL_PERCENT_RESIDENT].fixed = perc->resmem;
|
---|
| 430 | field[TASK_COL_VIRTUAL].type = FIELD_UINT_SUFFIX_BIN;
|
---|
| 431 | field[TASK_COL_VIRTUAL].uint = task->virtmem;
|
---|
| 432 | field[TASK_COL_PERCENT_VIRTUAL].type = FIELD_PERCENT;
|
---|
| 433 | field[TASK_COL_PERCENT_VIRTUAL].fixed = perc->virtmem;
|
---|
| 434 | field[TASK_COL_PERCENT_USER].type = FIELD_PERCENT;
|
---|
| 435 | field[TASK_COL_PERCENT_USER].fixed = perc->ucycles;
|
---|
| 436 | field[TASK_COL_PERCENT_KERNEL].type = FIELD_PERCENT;
|
---|
| 437 | field[TASK_COL_PERCENT_KERNEL].fixed = perc->kcycles;
|
---|
| 438 | field[TASK_COL_NAME].type = FIELD_STRING;
|
---|
| 439 | field[TASK_COL_NAME].string = task->name;
|
---|
| 440 | field += TASK_NUM_COLUMNS;
|
---|
| 441 | }
|
---|
| 442 |
|
---|
| 443 | return NULL;
|
---|
| 444 | }
|
---|
| 445 |
|
---|
| 446 | static const char *fill_ipc_table(data_t *data)
|
---|
| 447 | {
|
---|
| 448 | data->table.name = "IPC";
|
---|
| 449 | data->table.num_columns = IPC_NUM_COLUMNS;
|
---|
| 450 | data->table.columns = ipc_columns;
|
---|
| 451 | data->table.num_fields = data->tasks_count * IPC_NUM_COLUMNS;
|
---|
| 452 | data->table.fields = calloc(data->table.num_fields,
|
---|
| 453 | sizeof(field_t));
|
---|
| 454 | if (data->table.fields == NULL)
|
---|
| 455 | return "Not enough memory for table fields";
|
---|
| 456 |
|
---|
| 457 | field_t *field = data->table.fields;
|
---|
| 458 | for (size_t i = 0; i < data->tasks_count; i++) {
|
---|
| 459 | field[IPC_COL_TASKID].type = FIELD_UINT;
|
---|
| 460 | field[IPC_COL_TASKID].uint = data->tasks[i].task_id;
|
---|
| 461 | field[IPC_COL_CLS_SNT].type = FIELD_UINT_SUFFIX_DEC;
|
---|
| 462 | field[IPC_COL_CLS_SNT].uint = data->tasks[i].ipc_info.call_sent;
|
---|
| 463 | field[IPC_COL_CLS_RCV].type = FIELD_UINT_SUFFIX_DEC;
|
---|
| 464 | field[IPC_COL_CLS_RCV].uint = data->tasks[i].ipc_info.call_received;
|
---|
| 465 | field[IPC_COL_ANS_SNT].type = FIELD_UINT_SUFFIX_DEC;
|
---|
| 466 | field[IPC_COL_ANS_SNT].uint = data->tasks[i].ipc_info.answer_sent;
|
---|
| 467 | field[IPC_COL_ANS_RCV].type = FIELD_UINT_SUFFIX_DEC;
|
---|
| 468 | field[IPC_COL_ANS_RCV].uint = data->tasks[i].ipc_info.answer_received;
|
---|
| 469 | field[IPC_COL_FORWARD].type = FIELD_UINT_SUFFIX_DEC;
|
---|
| 470 | field[IPC_COL_FORWARD].uint = data->tasks[i].ipc_info.forwarded;
|
---|
| 471 | field[IPC_COL_NAME].type = FIELD_STRING;
|
---|
| 472 | field[IPC_COL_NAME].string = data->tasks[i].name;
|
---|
| 473 | field += IPC_NUM_COLUMNS;
|
---|
| 474 | }
|
---|
| 475 |
|
---|
| 476 | return NULL;
|
---|
| 477 | }
|
---|
| 478 |
|
---|
| 479 | static const char *fill_exception_table(data_t *data)
|
---|
| 480 | {
|
---|
| 481 | data->table.name = "Exceptions";
|
---|
| 482 | data->table.num_columns = EXCEPTION_NUM_COLUMNS;
|
---|
| 483 | data->table.columns = exception_columns;
|
---|
| 484 | data->table.num_fields = data->exceptions_count *
|
---|
| 485 | EXCEPTION_NUM_COLUMNS;
|
---|
| 486 | data->table.fields = calloc(data->table.num_fields, sizeof(field_t));
|
---|
| 487 | if (data->table.fields == NULL)
|
---|
| 488 | return "Not enough memory for table fields";
|
---|
| 489 |
|
---|
| 490 | field_t *field = data->table.fields;
|
---|
| 491 | for (size_t i = 0; i < data->exceptions_count; i++) {
|
---|
| 492 | if (!excs_all && !data->exceptions[i].hot)
|
---|
| 493 | continue;
|
---|
| 494 | field[EXCEPTION_COL_ID].type = FIELD_UINT;
|
---|
| 495 | field[EXCEPTION_COL_ID].uint = data->exceptions[i].id;
|
---|
| 496 | field[EXCEPTION_COL_COUNT].type = FIELD_UINT_SUFFIX_DEC;
|
---|
| 497 | field[EXCEPTION_COL_COUNT].uint = data->exceptions[i].count;
|
---|
| 498 | field[EXCEPTION_COL_PERCENT_COUNT].type = FIELD_PERCENT;
|
---|
| 499 | field[EXCEPTION_COL_PERCENT_COUNT].fixed = data->exceptions_perc[i].count;
|
---|
| 500 | field[EXCEPTION_COL_CYCLES].type = FIELD_UINT_SUFFIX_DEC;
|
---|
| 501 | field[EXCEPTION_COL_CYCLES].uint = data->exceptions[i].cycles;
|
---|
| 502 | field[EXCEPTION_COL_PERCENT_CYCLES].type = FIELD_PERCENT;
|
---|
| 503 | field[EXCEPTION_COL_PERCENT_CYCLES].fixed = data->exceptions_perc[i].cycles;
|
---|
| 504 | field[EXCEPTION_COL_DESCRIPTION].type = FIELD_STRING;
|
---|
| 505 | field[EXCEPTION_COL_DESCRIPTION].string = data->exceptions[i].desc;
|
---|
| 506 | field += EXCEPTION_NUM_COLUMNS;
|
---|
| 507 | }
|
---|
| 508 |
|
---|
| 509 | /* in case any cold exceptions were ignored */
|
---|
| 510 | data->table.num_fields = field - data->table.fields;
|
---|
| 511 |
|
---|
| 512 | return NULL;
|
---|
| 513 | }
|
---|
| 514 |
|
---|
| 515 | static const char *fill_table(data_t *data)
|
---|
| 516 | {
|
---|
| 517 | if (data->table.fields != NULL) {
|
---|
| 518 | free(data->table.fields);
|
---|
| 519 | data->table.fields = NULL;
|
---|
| 520 | }
|
---|
| 521 |
|
---|
| 522 | switch (op_mode) {
|
---|
[d76a329] | 523 | case OP_TASKS:
|
---|
| 524 | return fill_task_table(data);
|
---|
| 525 | case OP_IPC:
|
---|
| 526 | return fill_ipc_table(data);
|
---|
| 527 | case OP_EXCS:
|
---|
| 528 | return fill_exception_table(data);
|
---|
[f682f5a] | 529 | }
|
---|
| 530 | return NULL;
|
---|
| 531 | }
|
---|
| 532 |
|
---|
[8b2aba5] | 533 | static void free_data(data_t *target)
|
---|
| 534 | {
|
---|
[dec16a2] | 535 | if (target->load != NULL)
|
---|
| 536 | free(target->load);
|
---|
| 537 |
|
---|
| 538 | if (target->cpus != NULL)
|
---|
| 539 | free(target->cpus);
|
---|
| 540 |
|
---|
| 541 | if (target->cpus_perc != NULL)
|
---|
| 542 | free(target->cpus_perc);
|
---|
| 543 |
|
---|
| 544 | if (target->tasks != NULL)
|
---|
| 545 | free(target->tasks);
|
---|
| 546 |
|
---|
| 547 | if (target->tasks_perc != NULL)
|
---|
| 548 | free(target->tasks_perc);
|
---|
| 549 |
|
---|
| 550 | if (target->threads != NULL)
|
---|
| 551 | free(target->threads);
|
---|
| 552 |
|
---|
[8eec3c8] | 553 | if (target->exceptions != NULL)
|
---|
| 554 | free(target->exceptions);
|
---|
| 555 |
|
---|
[be06914] | 556 | if (target->exceptions_perc != NULL)
|
---|
| 557 | free(target->exceptions_perc);
|
---|
| 558 |
|
---|
[dec16a2] | 559 | if (target->physmem != NULL)
|
---|
| 560 | free(target->physmem);
|
---|
[172aad6] | 561 |
|
---|
| 562 | if (target->ucycles_diff != NULL)
|
---|
| 563 | free(target->ucycles_diff);
|
---|
| 564 |
|
---|
| 565 | if (target->kcycles_diff != NULL)
|
---|
| 566 | free(target->kcycles_diff);
|
---|
| 567 |
|
---|
| 568 | if (target->ecycles_diff != NULL)
|
---|
| 569 | free(target->ecycles_diff);
|
---|
| 570 |
|
---|
| 571 | if (target->ecount_diff != NULL)
|
---|
| 572 | free(target->ecount_diff);
|
---|
[f682f5a] | 573 |
|
---|
| 574 | if (target->table.fields != NULL)
|
---|
| 575 | free(target->table.fields);
|
---|
[8b2aba5] | 576 | }
|
---|
| 577 |
|
---|
[5c058d50] | 578 | int main(int argc, char *argv[])
|
---|
| 579 | {
|
---|
[dec16a2] | 580 | data_t data;
|
---|
| 581 | data_t data_prev;
|
---|
| 582 | const char *ret = NULL;
|
---|
| 583 |
|
---|
[ee35ba0b] | 584 | screen_init();
|
---|
[79edc36] | 585 | printf("Reading initial data...\n");
|
---|
[dec16a2] | 586 |
|
---|
[d517c5b] | 587 | if ((ret = read_data(&data)) != NULL)
|
---|
[dec16a2] | 588 | goto out;
|
---|
| 589 |
|
---|
[ee35ba0b] | 590 | /* Compute some rubbish to have initialised values */
|
---|
[d517c5b] | 591 | compute_percentages(&data, &data);
|
---|
[dec16a2] | 592 |
|
---|
| 593 | /* And paint screen until death */
|
---|
[5c058d50] | 594 | while (true) {
|
---|
[9e05055] | 595 | int c = tgetchar(UPDATE_INTERVAL);
|
---|
[d517c5b] | 596 |
|
---|
[b8d6783] | 597 | if (c < 0) { /* timeout */
|
---|
| 598 | data_prev = data;
|
---|
| 599 | if ((ret = read_data(&data)) != NULL) {
|
---|
[d517c5b] | 600 | free_data(&data_prev);
|
---|
[b8d6783] | 601 | goto out;
|
---|
| 602 | }
|
---|
| 603 |
|
---|
| 604 | compute_percentages(&data_prev, &data);
|
---|
| 605 | free_data(&data_prev);
|
---|
| 606 |
|
---|
| 607 | c = -1;
|
---|
| 608 | }
|
---|
| 609 |
|
---|
| 610 | if (screen_mode == SCREEN_HELP && c >= 0) {
|
---|
| 611 | if (c == 'h' || c == '?')
|
---|
| 612 | c = -1;
|
---|
| 613 | /* go back to table and handle the key */
|
---|
| 614 | screen_mode = SCREEN_TABLE;
|
---|
| 615 | }
|
---|
| 616 |
|
---|
| 617 | if (screen_mode == SCREEN_SORT && c >= 0) {
|
---|
| 618 | for (size_t i = 0; i < data.table.num_columns; i++) {
|
---|
| 619 | if (data.table.columns[i].key == c) {
|
---|
| 620 | sort_column = i;
|
---|
| 621 | screen_mode = SCREEN_TABLE;
|
---|
| 622 | }
|
---|
| 623 | }
|
---|
| 624 |
|
---|
| 625 | c = -1;
|
---|
| 626 | }
|
---|
| 627 |
|
---|
| 628 | switch (c) {
|
---|
[d76a329] | 629 | case -1: /* do nothing */
|
---|
| 630 | break;
|
---|
| 631 | case 't':
|
---|
| 632 | op_mode = OP_TASKS;
|
---|
| 633 | break;
|
---|
| 634 | case 'i':
|
---|
| 635 | op_mode = OP_IPC;
|
---|
| 636 | break;
|
---|
| 637 | case 'e':
|
---|
| 638 | op_mode = OP_EXCS;
|
---|
| 639 | break;
|
---|
| 640 | case 's':
|
---|
| 641 | screen_mode = SCREEN_SORT;
|
---|
| 642 | break;
|
---|
| 643 | case 'r':
|
---|
| 644 | sort_reverse = -sort_reverse;
|
---|
| 645 | break;
|
---|
| 646 | case 'h':
|
---|
| 647 | case '?':
|
---|
| 648 | screen_mode = SCREEN_HELP;
|
---|
| 649 | break;
|
---|
| 650 | case 'q':
|
---|
| 651 | goto out;
|
---|
| 652 | case 'a':
|
---|
| 653 | if (op_mode == OP_EXCS) {
|
---|
| 654 | excs_all = !excs_all;
|
---|
| 655 | if (excs_all)
|
---|
| 656 | show_warning("Showing all exceptions");
|
---|
| 657 | else
|
---|
| 658 | show_warning("Showing only hot exceptions");
|
---|
[8eec3c8] | 659 | break;
|
---|
[d76a329] | 660 | }
|
---|
| 661 | /* fallthrough */
|
---|
| 662 | default:
|
---|
| 663 | show_warning("Unknown command \"%c\", use \"h\" for help", c);
|
---|
| 664 | continue; /* don't redraw */
|
---|
[5c058d50] | 665 | }
|
---|
[d517c5b] | 666 |
|
---|
[f682f5a] | 667 | if ((ret = fill_table(&data)) != NULL) {
|
---|
| 668 | goto out;
|
---|
| 669 | }
|
---|
[b8d6783] | 670 | sort_table(&data.table);
|
---|
[d517c5b] | 671 | print_data(&data);
|
---|
[5c058d50] | 672 | }
|
---|
[dec16a2] | 673 |
|
---|
| 674 | out:
|
---|
| 675 | screen_done();
|
---|
[d517c5b] | 676 | free_data(&data);
|
---|
[dec16a2] | 677 |
|
---|
| 678 | if (ret != NULL) {
|
---|
| 679 | fprintf(stderr, "%s: %s\n", NAME, ret);
|
---|
| 680 | return 1;
|
---|
| 681 | }
|
---|
| 682 |
|
---|
[5c058d50] | 683 | return 0;
|
---|
| 684 | }
|
---|
| 685 |
|
---|
| 686 | /** @}
|
---|
| 687 | */
|
---|