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