1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430
|
/*
* hyptop - Show hypervisor performance data on System z
*
* Table module: Provide line mode and curses base table
*
* Copyright IBM Corp. 2010, 2017
*
* s390-tools is free software; you can redistribute it and/or modify
* it under the terms of the MIT license. See LICENSE for details.
*/
#ifndef TABLE_H
#define TABLE_H
#include <assert.h>
#include <string.h>
#include "lib/util_list.h"
#include "helper.h"
#define TABLE_STR_MAX 64
#define TABLE_HEADING_SIZE 20
struct table_col;
struct table_entry;
/*
* Table Column Unit
*/
struct table_col_unit {
int (*fn)(struct table_col*, struct table_entry *);
const char *str;
char *desc;
char hotkey;
};
/* Predefined units */
extern struct table_col_unit table_col_unit_str;
extern struct table_col_unit table_col_unit_cnt;
extern struct table_col_unit table_col_unit_kib;
extern struct table_col_unit table_col_unit_mib;
extern struct table_col_unit table_col_unit_gib;
extern struct table_col_unit table_col_unit_us;
extern struct table_col_unit table_col_unit_ms;
extern struct table_col_unit table_col_unit_s;
extern struct table_col_unit table_col_unit_hm;
extern struct table_col_unit table_col_unit_dhm;
extern struct table_col_unit table_col_unit_perc;
extern struct table_col_unit table_col_unit_vis;
/* Predefined families */
extern struct table_col_unit *table_col_unit_fam_str[];
extern struct table_col_unit *table_col_unit_fam_cnt[];
extern struct table_col_unit *table_col_unit_fam_mem[];
extern struct table_col_unit *table_col_unit_fam_time[];
extern struct table_col_unit *table_col_unit_fam_time_diff[];
extern struct table_col_unit *table_col_unit_fam_vis[];
/*
* Table Column Type
*/
enum table_col_type {
TABLE_COL_TYPE_U64,
TABLE_COL_TYPE_S64,
TABLE_COL_TYPE_STR,
};
/*
* Table Column Alignment
*/
enum table_col_align {
TABLE_COL_ALIGN_LEFT,
TABLE_COL_ALIGN_RIGHT,
};
/*
* Table Column Aggregation
*/
enum table_col_agg {
TABLE_COL_AGG_SUM,
TABLE_COL_AGG_MAX,
TABLE_COL_AGG_NONE,
};
static inline const char *table_col_agg_str(enum table_col_agg agg)
{
switch (agg) {
case TABLE_COL_AGG_SUM:
return "sum";
case TABLE_COL_AGG_MAX:
return "max";
case TABLE_COL_AGG_NONE:
return "none";
}
return NULL;
}
/*
* Table Column
*/
struct table_col_priv {
unsigned int max_width;
int col_nr;
int enabled;
char head_first[TABLE_HEADING_SIZE];
char head_char[2];
char head_last[TABLE_HEADING_SIZE];
int rsort;
};
/*
* Table Column Specification
*/
struct table_col_spec {
char hotkey;
char *unit_str;
};
/*
* Table Column
*/
struct table_col {
enum table_col_type type;
struct table_col_unit *unit;
struct table_col_unit **unit_fam;
enum table_col_align align;
enum table_col_agg agg;
char hotkey;
char head[TABLE_HEADING_SIZE];
struct table_col_priv *p;
};
static inline int table_col_enabled(struct table_col *col)
{
return col->p->enabled;
}
/*
* Table Column Constructor Macros
*/
#define TABLE_COL_STR(l, h) \
{ \
.type = TABLE_COL_TYPE_STR, \
.unit = &table_col_unit_str, \
.unit_fam = table_col_unit_fam_str, \
.align = TABLE_COL_ALIGN_RIGHT, \
.agg = TABLE_COL_AGG_NONE, \
.hotkey = l, \
.head = h, \
}
#define TABLE_COL_STR_LEFT(l, h) \
{ \
.type = TABLE_COL_TYPE_STR, \
.unit = &table_col_unit_str, \
.unit_fam = table_col_unit_fam_str, \
.align = TABLE_COL_ALIGN_LEFT, \
.agg = TABLE_COL_AGG_NONE, \
.hotkey = l, \
.head = h, \
}
#define TABLE_COL_CNT_SUM(l, h) \
{ \
.type = TABLE_COL_TYPE_U64, \
.unit = &table_col_unit_cnt, \
.unit_fam = table_col_unit_fam_cnt, \
.align = TABLE_COL_ALIGN_RIGHT, \
.agg = TABLE_COL_AGG_SUM, \
.hotkey = l, \
.head = h, \
}
#define TABLE_COL_CNT_NONE(l, h) \
{ \
.type = TABLE_COL_TYPE_U64, \
.unit = &table_col_unit_cnt, \
.unit_fam = table_col_unit_fam_cnt, \
.align = TABLE_COL_ALIGN_RIGHT, \
.agg = TABLE_COL_AGG_NONE, \
.hotkey = l, \
.head = h, \
}
#define TABLE_COL_CNT_MAX(l, h) \
{ \
.type = TABLE_COL_TYPE_U64, \
.unit = &table_col_unit_cnt, \
.unit_fam = table_col_unit_fam_cnt, \
.align = TABLE_COL_ALIGN_RIGHT, \
.agg = TABLE_COL_AGG_MAX, \
.hotkey = l, \
.head = h, \
}
#define TABLE_COL_MEM_SUM(f, l, h) \
{ \
.type = TABLE_COL_TYPE_U64, \
.unit = &f, \
.unit_fam = table_col_unit_fam_mem, \
.align = TABLE_COL_ALIGN_RIGHT, \
.agg = TABLE_COL_AGG_SUM, \
.hotkey = l, \
.head = h, \
}
#define TABLE_COL_TIME_SUM(f, l, h) \
{ \
.type = TABLE_COL_TYPE_U64, \
.unit = &f, \
.unit_fam = table_col_unit_fam_time, \
.align = TABLE_COL_ALIGN_RIGHT, \
.agg = TABLE_COL_AGG_SUM, \
.hotkey = l, \
.head = h, \
}
#define TABLE_COL_TIME_DIFF_SUM(f, l, h) \
{ \
.type = TABLE_COL_TYPE_U64, \
.unit = &f, \
.unit_fam = table_col_unit_fam_time_diff, \
.align = TABLE_COL_ALIGN_RIGHT, \
.agg = TABLE_COL_AGG_SUM, \
.hotkey = l, \
.head = h, \
}
#define TABLE_COL_STIME_SUM(f, l, h) \
{ \
.type = TABLE_COL_TYPE_S64, \
.unit = &f, \
.unit_fam = table_col_unit_fam_time, \
.align = TABLE_COL_ALIGN_RIGHT, \
.agg = TABLE_COL_AGG_SUM, \
.hotkey = l, \
.head = h, \
}
#define TABLE_COL_STIME_DIFF_SUM(f, l, h) \
{ \
.type = TABLE_COL_TYPE_S64, \
.unit = &f, \
.unit_fam = table_col_unit_fam_time_diff, \
.align = TABLE_COL_ALIGN_RIGHT, \
.agg = TABLE_COL_AGG_SUM, \
.hotkey = l, \
.head = h, \
}
#define TABLE_COL_TIME_MAX(f, l, h) \
{ \
.type = TABLE_COL_TYPE_U64, \
.unit = &f, \
.unit_fam = table_col_unit_fam_time, \
.align = TABLE_COL_ALIGN_RIGHT, \
.agg = TABLE_COL_AGG_MAX, \
.hotkey = l, \
.head = h, \
}
/*
* Set reverse sort property for column
*/
static inline void table_col_rsort(struct table_col *col)
{
col->p->rsort = 1;
}
/*
* Column member access macros
*/
#define table_col_hotkey(col) ((col)->hotkey)
#define table_col_head(col) ((col)->head)
#define table_col_unit_str(col) ((col)->unit->str)
/*
* Table Entry
*/
struct table_entry {
union {
struct {
u64 v1;
u64 v2;
} u64;
struct {
s64 v1;
s64 v2;
} s64;
} d;
int set;
char str[TABLE_STR_MAX];
};
/*
* Table Row
*/
struct table_row {
struct util_list_node list;
struct table_entry *entries;
int marked;
};
/*
* Table Mark Key
*/
struct table_mark_key {
struct util_list_node list;
char str[TABLE_STR_MAX];
};
/*
* Table
*/
struct table {
struct util_list row_list;
int col_cnt;
struct table_col **col_vec;
struct table_col *col_selected;
struct table_row *row_last;
int row_cnt;
int row_cnt_marked;
int row_cnt_extra;
int row_nr_begin;
int row_nr_select;
int ready;
struct util_list mark_key_list;
int attr_sorted_table;
int attr_first_bold;
int attr_with_units;
int mode_sort_inverse;
int mode_select;
int mode_hide_unmarked;
};
/*
* Return if we are in select mode
*/
static inline int table_mode_select(struct table *t)
{
return t->mode_select;
}
/*
* Table croll units
*/
enum table_scroll_unit {
TABLE_SCROLL_LINE,
TABLE_SCROLL_PAGE,
TABLE_SCROLL_LAST,
};
/*
* Prototypes
*/
extern struct table *table_new(int extra_rows, int sorted, int first_bold,
int with_units);
extern void table_reset(struct table *t);
extern void table_rebuild(struct table *t);
extern void table_finish(struct table *t);
extern void table_print(struct table *t);
extern void table_process_input(struct table *t, int c);
extern void table_col_unit_next(struct table *t, char hotkey);
extern void table_col_unit_prev(struct table *t, char hotkey);
extern int table_col_unit_set(struct table *t, char hotkey, const char *unit);
extern void table_col_add(struct table *t, struct table_col *col);
extern int table_col_select(struct table *t, char hotkey);
extern void table_col_select_next(struct table *t);
extern void table_col_select_prev(struct table *t);
extern void table_col_enable_toggle(struct table *t, char hotkey);
extern void table_row_del_all(struct table *t);
extern void table_row_add(struct table *t, struct table_row *row);
extern void table_row_mark(struct table *t, struct table_row *row);
extern void table_row_mark_del_all(struct table *t);
extern void table_row_mark_toggle(struct table *t, struct table_row *row);
extern void table_row_mark_toggle_by_key(struct table *t, const char *mark_key);
extern void table_row_select_down(struct table *t, enum table_scroll_unit unit);
extern void table_row_select_up(struct table *t, enum table_scroll_unit unit);
extern void table_row_select_key_get(struct table *t, char str[TABLE_STR_MAX]);
extern struct table_row *table_row_alloc(struct table *t);
extern void table_scroll_down(struct table *t, enum table_scroll_unit unit);
extern void table_scroll_up(struct table *t, enum table_scroll_unit unit);
/*
* Entry add functions
*/
static inline void table_row_entry_u64_add(struct table_row *table_row,
struct table_col *table_col,
u64 value)
{
table_row->entries[table_col->p->col_nr].d.u64.v1 = value;
table_row->entries[table_col->p->col_nr].set = 1;
}
static inline void table_row_entry_s64_add(struct table_row *table_row,
struct table_col *table_col,
s64 value)
{
table_row->entries[table_col->p->col_nr].d.s64.v1 = value;
table_row->entries[table_col->p->col_nr].set = 1;
}
static inline void table_row_entry_u64_add_pair(struct table_row *table_row,
struct table_col *table_col,
u64 value1, u64 value2)
{
table_row->entries[table_col->p->col_nr].d.u64.v1 = value1;
table_row->entries[table_col->p->col_nr].d.u64.v2 = value2;
table_row->entries[table_col->p->col_nr].set = 1;
}
static inline void table_row_entry_str_add(struct table_row *table_row,
struct table_col *table_col,
const char *str)
{
assert(strlen(str) < TABLE_STR_MAX);
strcpy(table_row->entries[table_col->p->col_nr].str, str);
table_row->entries[table_col->p->col_nr].set = 1;
}
/*
* Interate over all mark keys
*/
#define table_iterate_mark_keys(t, key) \
util_list_iterate(&t->mark_key_list, key)
#endif /* TABLE_H */
|