| 1 |
|
| 2 |
#include <inttypes.h> |
| 3 |
#include <string.h> |
| 4 |
#include "ui.h" |
| 5 |
|
| 6 |
int offset; |
| 7 |
int max_offset; |
| 8 |
|
| 9 |
GList *all_cpus = NULL; |
| 10 |
GList *all_irqs = NULL; |
| 11 |
|
| 12 |
static char **irq_name; |
| 13 |
|
| 14 |
char *IRQ_CLASS_TO_STR[] = { |
| 15 |
"Other", |
| 16 |
"Legacy", |
| 17 |
"SCSI", |
| 18 |
"Video", |
| 19 |
"Ethernet", |
| 20 |
"Gigabit Ethernet", |
| 21 |
"10-Gigabit Ethernet", |
| 22 |
"Virt Event"}; |
| 23 |
|
| 24 |
void show_frame() |
| 25 |
{ |
| 26 |
int i; |
| 27 |
attrset(COLOR_PAIR(4)); |
| 28 |
char top[COLS]; |
| 29 |
top[0] = '\0'; |
| 30 |
while(strlen(top) != (size_t)COLS - 1) { |
| 31 |
snprintf(top + strlen(top), COLS - strlen(top), " "); |
| 32 |
} |
| 33 |
mvprintw(0, 0, "%s", top); |
| 34 |
for(i = 0; i < LINES; i++) { |
| 35 |
mvprintw(i, 0, " "); |
| 36 |
mvprintw(i, COLS - 1, " "); |
| 37 |
} |
| 38 |
} |
| 39 |
|
| 40 |
void show_footer() |
| 41 |
{ |
| 42 |
char footer[COLS]; |
| 43 |
snprintf(footer, COLS - 1, |
| 44 |
" q (QUIT) F3 (TREE) F4 (SETTINGS) F5 (SETUP IRQS)"); |
| 45 |
while(strlen(footer) != (size_t)COLS - 1) { |
| 46 |
snprintf(footer + strlen(footer), COLS - strlen(footer), " "); |
| 47 |
} |
| 48 |
attrset(COLOR_PAIR(4) | A_BOLD); |
| 49 |
mvprintw(LINES - 1, 0, "%s", footer); |
| 50 |
} |
| 51 |
|
| 52 |
char * check_control_in_sleep_input(int max_len, int column_offest, int line_offset) |
| 53 |
{ |
| 54 |
char *input_to = malloc(max_len * sizeof(char)); |
| 55 |
int iteration = 0; |
| 56 |
while(iteration < max_len) { |
| 57 |
int new = getch(); |
| 58 |
switch(new) { |
| 59 |
case ERR: |
| 60 |
/* No input is ready for nonblocking getch() call */ |
| 61 |
break; |
| 62 |
case '\r': |
| 63 |
case '\n': |
| 64 |
input_to[iteration] = '\0'; |
| 65 |
return input_to; |
| 66 |
case 'q': |
| 67 |
close_window(0); |
| 68 |
break; |
| 69 |
case KEY_BACKSPACE: |
| 70 |
if(iteration > 0) { |
| 71 |
attrset(COLOR_PAIR(5)); |
| 72 |
iteration--; |
| 73 |
mvaddch(line_offset, column_offest + iteration, ' '); |
| 74 |
} |
| 75 |
move(line_offset, column_offest + iteration); |
| 76 |
attrset(COLOR_PAIR(5) | A_REVERSE | A_BOLD); |
| 77 |
break; |
| 78 |
case 27: |
| 79 |
free(input_to); |
| 80 |
return NULL; |
| 81 |
default: |
| 82 |
input_to[iteration] = new; |
| 83 |
iteration++; |
| 84 |
break; |
| 85 |
} |
| 86 |
} |
| 87 |
return input_to; |
| 88 |
} |
| 89 |
|
| 90 |
int get_valid_sleep_input(int column_offest) |
| 91 |
{ |
| 92 |
uint64_t new_sleep = setup.sleep; |
| 93 |
while(1) { |
| 94 |
attrset(COLOR_PAIR(5)); |
| 95 |
mvprintw(2, column_offest, " "); |
| 96 |
attrset(COLOR_PAIR(5) | A_REVERSE | A_BOLD); |
| 97 |
refresh(); |
| 98 |
move(2, column_offest); |
| 99 |
curs_set(1); |
| 100 |
char *input = check_control_in_sleep_input(20, column_offest, 3); |
| 101 |
if(input == NULL) { |
| 102 |
curs_set(0); |
| 103 |
attrset(COLOR_PAIR(1)); |
| 104 |
mvprintw(2, column_offest, "%lu ", new_sleep); |
| 105 |
move(LINES, COLS); |
| 106 |
break; |
| 107 |
} |
| 108 |
attrset(COLOR_PAIR(1)); |
| 109 |
mvprintw(LINES - 2, 1, " "); |
| 110 |
curs_set(0); |
| 111 |
refresh(); |
| 112 |
char *error; |
| 113 |
new_sleep = strtol(input, &error, 10); |
| 114 |
if((*error == '\0') && (new_sleep >= 1)) { |
| 115 |
break; |
| 116 |
} else { |
| 117 |
new_sleep = setup.sleep; |
| 118 |
attrset(COLOR_PAIR(4) | A_BOLD); |
| 119 |
mvprintw(LINES - 2, 1, |
| 120 |
"Invalid input: %s ", |
| 121 |
input); |
| 122 |
refresh(); |
| 123 |
} |
| 124 |
free(input); |
| 125 |
} |
| 126 |
|
| 127 |
attrset(COLOR_PAIR(1)); |
| 128 |
mvprintw(2, column_offest, "%lu ", new_sleep); |
| 129 |
|
| 130 |
return new_sleep; |
| 131 |
} |
| 132 |
|
| 133 |
void get_banned_cpu(int *cpu, void *data __attribute__((unused))) |
| 134 |
{ |
| 135 |
cpu_ban_t *new = malloc(sizeof(cpu_ban_t)); |
| 136 |
new->number = *cpu; |
| 137 |
new->is_banned = 1; |
| 138 |
all_cpus = g_list_append(all_cpus, new); |
| 139 |
} |
| 140 |
|
| 141 |
void print_tmp_cpu_line(cpu_ban_t *cpu, void *data __attribute__((unused))) |
| 142 |
{ |
| 143 |
int line = max_offset - offset + 6; |
| 144 |
if (max_offset >= offset && line < LINES - 3) { |
| 145 |
if (cpu->is_changed) |
| 146 |
attrset(COLOR_PAIR(3)); |
| 147 |
else if(cpu->is_banned) |
| 148 |
attrset(COLOR_PAIR(10)); |
| 149 |
else |
| 150 |
attrset(COLOR_PAIR(9)); |
| 151 |
mvprintw(line, 3, "CPU %d ", cpu->number); |
| 152 |
mvprintw(line, 19, "%s", cpu->is_banned ? |
| 153 |
"YES " : |
| 154 |
"NO "); |
| 155 |
} |
| 156 |
max_offset++; |
| 157 |
} |
| 158 |
|
| 159 |
void print_cpu_line(cpu_ban_t *cpu, void *data __attribute__((unused))) |
| 160 |
{ |
| 161 |
int line = max_offset - offset + 6; |
| 162 |
if (max_offset >= offset && line < LINES - 2) { |
| 163 |
if(cpu->is_banned) |
| 164 |
attrset(COLOR_PAIR(10)); |
| 165 |
else |
| 166 |
attrset(COLOR_PAIR(9)); |
| 167 |
mvprintw(line, 3, "CPU %d ", cpu->number); |
| 168 |
mvprintw(line, 19, "%s", cpu->is_banned ? |
| 169 |
"YES " : |
| 170 |
"NO "); |
| 171 |
} |
| 172 |
max_offset++; |
| 173 |
} |
| 174 |
|
| 175 |
void print_all_cpus() |
| 176 |
{ |
| 177 |
max_offset = 0; |
| 178 |
if(all_cpus == NULL) { |
| 179 |
for_each_node(tree, get_cpu, NULL); |
| 180 |
for_each_int(setup.banned_cpus, get_banned_cpu, NULL); |
| 181 |
all_cpus = g_list_sort(all_cpus, sort_all_cpus); |
| 182 |
} |
| 183 |
attrset(COLOR_PAIR(2)); |
| 184 |
mvprintw(4, 3, "NUMBER IS BANNED"); |
| 185 |
for_each_cpu(all_cpus, print_cpu_line, NULL); |
| 186 |
max_offset -= LINES - 8; |
| 187 |
if (max_offset < 0) |
| 188 |
max_offset = 0; |
| 189 |
} |
| 190 |
|
| 191 |
void add_banned_cpu(int *banned_cpu, void *data) |
| 192 |
{ |
| 193 |
snprintf(data + strlen(data), 1024 - strlen(data), "%d, ", *banned_cpu); |
| 194 |
} |
| 195 |
|
| 196 |
void display_banned_cpus() |
| 197 |
{ |
| 198 |
char banned_cpus[1024] = "Banned CPU numbers: \0"; |
| 199 |
if(g_list_length(setup.banned_cpus) > 0) { |
| 200 |
for_each_int(setup.banned_cpus, add_banned_cpu, banned_cpus); |
| 201 |
snprintf(banned_cpus + strlen(banned_cpus) - 2, |
| 202 |
1024 - strlen(banned_cpus), "\n"); |
| 203 |
} else { |
| 204 |
snprintf(banned_cpus + strlen(banned_cpus), |
| 205 |
1024 - strlen(banned_cpus), "None\n"); |
| 206 |
} |
| 207 |
attrset(COLOR_PAIR(0)); |
| 208 |
mvprintw(2, 5, "%s\n", banned_cpus); |
| 209 |
} |
| 210 |
|
| 211 |
int toggle_cpu(GList *cpu_list, int cpu_number) |
| 212 |
{ |
| 213 |
GList *entry = g_list_first(cpu_list); |
| 214 |
cpu_ban_t *entry_data = (cpu_ban_t *)(entry->data); |
| 215 |
while(entry_data->number != cpu_number) { |
| 216 |
entry = g_list_next(entry); |
| 217 |
entry_data = (cpu_ban_t *)(entry->data); |
| 218 |
} |
| 219 |
if(((cpu_ban_t *)(entry->data))->is_banned) { |
| 220 |
((cpu_ban_t *)(entry->data))->is_banned = 0; |
| 221 |
} else { |
| 222 |
((cpu_ban_t *)(entry->data))->is_banned = 1; |
| 223 |
} |
| 224 |
((cpu_ban_t *)(entry->data))->is_changed = 1; |
| 225 |
return ((cpu_ban_t *)(entry->data))->is_banned; |
| 226 |
} |
| 227 |
|
| 228 |
void get_new_cpu_ban_values(cpu_ban_t *cpu, void *data) |
| 229 |
{ |
| 230 |
char *mask_data = (char *)data; |
| 231 |
if(cpu->is_banned) { |
| 232 |
snprintf(mask_data + strlen(mask_data), 1024 - strlen(mask_data), |
| 233 |
"%d,", cpu->number); |
| 234 |
} |
| 235 |
} |
| 236 |
|
| 237 |
void get_cpu(cpu_node_t *node, void *data __attribute__((unused))) |
| 238 |
{ |
| 239 |
if(node->type == OBJ_TYPE_CPU) { |
| 240 |
cpu_ban_t *new = malloc(sizeof(cpu_ban_t)); |
| 241 |
new->number = node->number; |
| 242 |
new->is_banned = 0; |
| 243 |
all_cpus = g_list_append(all_cpus, new); |
| 244 |
} |
| 245 |
if(g_list_length(node->children) > 0) { |
| 246 |
for_each_node(node->children, get_cpu, NULL); |
| 247 |
} |
| 248 |
} |
| 249 |
|
| 250 |
void handle_cpu_banning() |
| 251 |
{ |
| 252 |
GList *tmp = g_list_copy_deep(all_cpus, copy_cpu_ban, NULL); |
| 253 |
attrset(COLOR_PAIR(5)); |
| 254 |
mvprintw(LINES - 3, 1, "Move up and down the list, toggle ban with Enter."); |
| 255 |
mvprintw(LINES - 2, 1, |
| 256 |
"Press ESC for discarding and <S> for saving the values."); |
| 257 |
move(6, 19); |
| 258 |
curs_set(1); |
| 259 |
refresh(); |
| 260 |
size_t position = 6; |
| 261 |
char processing = 1; |
| 262 |
while(processing) { |
| 263 |
int direction = getch(); |
| 264 |
switch(direction) { |
| 265 |
case KEY_UP: |
| 266 |
if(position > 6) { |
| 267 |
position--; |
| 268 |
move(position, 19); |
| 269 |
} else if (offset > 0) { |
| 270 |
offset--; |
| 271 |
max_offset = 0; |
| 272 |
for_each_cpu(tmp, print_tmp_cpu_line, NULL); |
| 273 |
max_offset -= LINES - 9; |
| 274 |
if (max_offset < 0) |
| 275 |
max_offset = 0; |
| 276 |
move(position, 19); |
| 277 |
} |
| 278 |
break; |
| 279 |
case KEY_DOWN: |
| 280 |
if(position < (size_t)(LINES - 4)) { |
| 281 |
if (position <= g_list_length(all_cpus) + 4 - offset) { |
| 282 |
position++; |
| 283 |
move(position, 19); |
| 284 |
} |
| 285 |
} else if (offset < max_offset) { |
| 286 |
offset++; |
| 287 |
max_offset = 0; |
| 288 |
for_each_cpu(tmp, print_tmp_cpu_line, NULL); |
| 289 |
max_offset -= LINES - 9; |
| 290 |
if (max_offset < 0) |
| 291 |
max_offset = 0; |
| 292 |
move(position, 19); |
| 293 |
} |
| 294 |
break; |
| 295 |
case '\n': |
| 296 |
case '\r': { |
| 297 |
attrset(COLOR_PAIR(3)); |
| 298 |
int banned = toggle_cpu(tmp, position + offset - 6); |
| 299 |
mvprintw(position, 3, "CPU %d ", position + offset - 6); |
| 300 |
if(banned) { |
| 301 |
mvprintw(position, 19, "YES"); |
| 302 |
} else { |
| 303 |
mvprintw(position, 19, "NO "); |
| 304 |
} |
| 305 |
move(position, 19); |
| 306 |
refresh(); |
| 307 |
break; |
| 308 |
} |
| 309 |
case 27: |
| 310 |
processing = 0; |
| 311 |
curs_set(0); |
| 312 |
g_list_free(tmp); |
| 313 |
print_all_cpus(); |
| 314 |
attrset(COLOR_PAIR(0)); |
| 315 |
mvprintw(LINES - 3, 1, " \ |
| 316 |
"); |
| 317 |
attrset(COLOR_PAIR(5)); |
| 318 |
mvprintw(LINES - 2, 1, |
| 319 |
"Press <S> for changing sleep setup, <C> for CPU ban setup. "); |
| 320 |
show_frame(); |
| 321 |
show_footer(); |
| 322 |
refresh(); |
| 323 |
break; |
| 324 |
case 's': |
| 325 |
processing = 0; |
| 326 |
g_list_free(all_cpus); |
| 327 |
all_cpus = tmp; |
| 328 |
curs_set(0); |
| 329 |
print_all_cpus(); |
| 330 |
attrset(COLOR_PAIR(0)); |
| 331 |
mvprintw(LINES - 3, 1, " \ |
| 332 |
"); |
| 333 |
attrset(COLOR_PAIR(5)); |
| 334 |
mvprintw(LINES - 2, 1, |
| 335 |
"Press <S> for changing sleep setup, <C> for CPU ban setup. "); |
| 336 |
show_frame(); |
| 337 |
show_footer(); |
| 338 |
refresh(); |
| 339 |
char settings_string[1024] = "settings cpus \0"; |
| 340 |
for_each_cpu(all_cpus, get_new_cpu_ban_values, settings_string); |
| 341 |
if(!strcmp("settings cpus \0", settings_string)) { |
| 342 |
strncpy(settings_string + strlen(settings_string), |
| 343 |
"NULL", 1024 - strlen(settings_string)); |
| 344 |
} |
| 345 |
send_settings(settings_string); |
| 346 |
break; |
| 347 |
case 'q': |
| 348 |
processing = 0; |
| 349 |
close_window(0); |
| 350 |
break; |
| 351 |
default: |
| 352 |
break; |
| 353 |
} |
| 354 |
} |
| 355 |
} |
| 356 |
|
| 357 |
static int rbot, rtop; |
| 358 |
|
| 359 |
static inline void bsnl_emit(char *buf, int buflen) |
| 360 |
{ |
| 361 |
int len = strlen(buf); |
| 362 |
if (len > 0) { |
| 363 |
snprintf(buf + len, buflen - len, ","); |
| 364 |
len++; |
| 365 |
} |
| 366 |
if (rbot == rtop) |
| 367 |
snprintf(buf + len, buflen - len, "%d", rbot); |
| 368 |
else |
| 369 |
snprintf(buf + len, buflen - len, "%d-%d", rbot, rtop); |
| 370 |
} |
| 371 |
|
| 372 |
void copy_assigned_obj(int *number, void *data) |
| 373 |
{ |
| 374 |
if (rtop == -1) { |
| 375 |
rbot = rtop = *number; |
| 376 |
return; |
| 377 |
} |
| 378 |
if (*number > rtop + 1) { |
| 379 |
bsnl_emit(data, 128); |
| 380 |
rbot = *number; |
| 381 |
} |
| 382 |
rtop = *number; |
| 383 |
} |
| 384 |
|
| 385 |
void print_assigned_objects_string(irq_t *irq, int *line_offset) |
| 386 |
{ |
| 387 |
if(irq->is_banned) { |
| 388 |
return; |
| 389 |
} |
| 390 |
char assigned_to[128] = "\0"; |
| 391 |
rtop = -1; |
| 392 |
for_each_int(irq->assigned_to, copy_assigned_obj, assigned_to); |
| 393 |
bsnl_emit(assigned_to, 128); |
| 394 |
mvprintw(*line_offset, 68, "%s ", assigned_to); |
| 395 |
} |
| 396 |
|
| 397 |
void get_irq_name(int end) |
| 398 |
{ |
| 399 |
int i, cpunr, len; |
| 400 |
FILE *output; |
| 401 |
char *cmd; |
| 402 |
char buffer[128]; |
| 403 |
|
| 404 |
if (irq_name == NULL) { |
| 405 |
irq_name = malloc(sizeof(char *) * LINES); |
| 406 |
for (i = 4; i < LINES; i++) { |
| 407 |
irq_name[i] = malloc(sizeof(char) * 50); |
| 408 |
memset(irq_name[i], 0, sizeof(char) * 50); |
| 409 |
} |
| 410 |
} |
| 411 |
|
| 412 |
output = popen("cat /proc/interrupts | head -1 | awk '{print NF}'", "r"); |
| 413 |
if (!output) |
| 414 |
return; |
| 415 |
fscanf(output, "%d", &cpunr); |
| 416 |
pclose(output); |
| 417 |
|
| 418 |
len = snprintf(NULL, 0, "cat /proc/interrupts | awk '{for (i=%d;i<=NF;i++)printf(\"%%s \", $i);print \"\"}' | cut -c-49", cpunr + 2); |
| 419 |
cmd = alloca(sizeof(char) * (len + 1)); |
| 420 |
snprintf(cmd, len + 1, "cat /proc/interrupts | awk '{for (i=%d;i<=NF;i++)printf(\"%%s \", $i);print \"\"}' | cut -c-49", cpunr + 2); |
| 421 |
output = popen(cmd, "r"); |
| 422 |
for (i = 0; i <= offset; i++) |
| 423 |
fgets(buffer, 50, output); |
| 424 |
for (i = 4; i < end; i++) |
| 425 |
fgets(irq_name[i], 50, output); |
| 426 |
pclose(output); |
| 427 |
} |
| 428 |
|
| 429 |
void print_tmp_irq_line(irq_t *irq, void *data __attribute__((unused))) |
| 430 |
{ |
| 431 |
int line = max_offset - offset + 4; |
| 432 |
max_offset++; |
| 433 |
if (line < 4 || line >= LINES - 3) |
| 434 |
return; |
| 435 |
switch(irq->class) { |
| 436 |
case(IRQ_OTHER): |
| 437 |
attrset(COLOR_PAIR(1)); |
| 438 |
break; |
| 439 |
case(IRQ_LEGACY): |
| 440 |
attrset(COLOR_PAIR(2)); |
| 441 |
break; |
| 442 |
case(IRQ_SCSI): |
| 443 |
attrset(COLOR_PAIR(3)); |
| 444 |
break; |
| 445 |
case(IRQ_VIDEO): |
| 446 |
attrset(COLOR_PAIR(8)); |
| 447 |
break; |
| 448 |
case(IRQ_ETH): |
| 449 |
case(IRQ_GBETH): |
| 450 |
case(IRQ_10GBETH): |
| 451 |
attrset(COLOR_PAIR(9)); |
| 452 |
break; |
| 453 |
case(IRQ_VIRT_EVENT): |
| 454 |
attrset(COLOR_PAIR(10)); |
| 455 |
break; |
| 456 |
default: |
| 457 |
attrset(COLOR_PAIR(0)); |
| 458 |
break; |
| 459 |
} |
| 460 |
mvprintw(line, 3, "IRQ %d ", irq->vector); |
| 461 |
mvprintw(line, 19, "%s", irq->is_banned ? "YES" : "NO "); |
| 462 |
mvprintw(line, 36, "%s ", |
| 463 |
irq->class < 0 ? "Unknown" : IRQ_CLASS_TO_STR[irq->class]); |
| 464 |
print_assigned_objects_string(irq, &line); |
| 465 |
mvprintw(line, 120, "%s", irq_name[line]); |
| 466 |
} |
| 467 |
|
| 468 |
void print_irq_line(irq_t *irq, void *data __attribute__((unused))) |
| 469 |
{ |
| 470 |
int line = max_offset - offset + 4; |
| 471 |
max_offset++; |
| 472 |
if (line < 4 || line >= LINES - 2) |
| 473 |
return; |
| 474 |
switch(irq->class) { |
| 475 |
case(IRQ_OTHER): |
| 476 |
attrset(COLOR_PAIR(1)); |
| 477 |
break; |
| 478 |
case(IRQ_LEGACY): |
| 479 |
attrset(COLOR_PAIR(2)); |
| 480 |
break; |
| 481 |
case(IRQ_SCSI): |
| 482 |
attrset(COLOR_PAIR(3)); |
| 483 |
break; |
| 484 |
case(IRQ_VIDEO): |
| 485 |
attrset(COLOR_PAIR(8)); |
| 486 |
break; |
| 487 |
case(IRQ_ETH): |
| 488 |
case(IRQ_GBETH): |
| 489 |
case(IRQ_10GBETH): |
| 490 |
attrset(COLOR_PAIR(9)); |
| 491 |
break; |
| 492 |
case(IRQ_VIRT_EVENT): |
| 493 |
attrset(COLOR_PAIR(10)); |
| 494 |
break; |
| 495 |
default: |
| 496 |
attrset(COLOR_PAIR(0)); |
| 497 |
break; |
| 498 |
} |
| 499 |
mvprintw(line, 3, "IRQ %d", irq->vector); |
| 500 |
mvprintw(line, 19, "%s", irq->is_banned ? "YES" : "NO "); |
| 501 |
mvprintw(line, 36, "%s ", |
| 502 |
irq->class < 0 ? "Unknown" : IRQ_CLASS_TO_STR[irq->class]); |
| 503 |
print_assigned_objects_string(irq, &line); |
| 504 |
mvprintw(line, 120, "%s", irq_name[line]); |
| 505 |
} |
| 506 |
|
| 507 |
void print_all_irqs() |
| 508 |
{ |
| 509 |
max_offset = 0; |
| 510 |
attrset(COLOR_PAIR(0)); |
| 511 |
mvprintw(2, 3, |
| 512 |
"NUMBER IS BANNED CLASS \ |
| 513 |
ASSIGNED TO CPUS IRQ NAME"); |
| 514 |
get_irq_name(LINES - 2); |
| 515 |
for_each_irq(all_irqs, print_irq_line, NULL); |
| 516 |
max_offset -= LINES - 6; |
| 517 |
if (max_offset < 0) |
| 518 |
max_offset = 0; |
| 519 |
} |
| 520 |
|
| 521 |
int toggle_irq(GList *irq_list, int position) |
| 522 |
{ |
| 523 |
GList *entry = g_list_first(irq_list); |
| 524 |
int irq_node = 0; |
| 525 |
while(irq_node != position) { |
| 526 |
entry = g_list_next(entry); |
| 527 |
irq_node++; |
| 528 |
} |
| 529 |
if(((irq_t *)(entry->data))->is_banned) { |
| 530 |
((irq_t *)(entry->data))->is_banned = 0; |
| 531 |
} else { |
| 532 |
((irq_t *)(entry->data))->is_banned = 1; |
| 533 |
} |
| 534 |
((irq_t *)(entry->data))->is_changed = 1; |
| 535 |
return ((irq_t *)(entry->data))->is_banned; |
| 536 |
} |
| 537 |
|
| 538 |
void get_new_irq_ban_values(irq_t *irq, void *data) |
| 539 |
{ |
| 540 |
char *ban_list = (char *)data; |
| 541 |
if(irq->is_banned) { |
| 542 |
snprintf(ban_list + strlen(ban_list), 1024 - strlen(ban_list), |
| 543 |
" %d", irq->vector); |
| 544 |
} |
| 545 |
} |
| 546 |
|
| 547 |
void copy_irqs_from_nodes(cpu_node_t *node, void *data __attribute__((unused))) |
| 548 |
{ |
| 549 |
if(g_list_length(node->irqs) > 0) { |
| 550 |
GList *new = g_list_copy_deep(node->irqs, copy_irq, NULL); |
| 551 |
all_irqs = g_list_concat(all_irqs, new); |
| 552 |
} |
| 553 |
if(g_list_length(node->children) > 0) { |
| 554 |
for_each_node(node->children, copy_irqs_from_nodes, all_irqs); |
| 555 |
} |
| 556 |
} |
| 557 |
|
| 558 |
void get_all_irqs() |
| 559 |
{ |
| 560 |
all_irqs = g_list_copy_deep(setup.banned_irqs, copy_irq, NULL); |
| 561 |
for_each_node(tree, copy_irqs_from_nodes, NULL); |
| 562 |
} |
| 563 |
|
| 564 |
void handle_irq_banning() |
| 565 |
{ |
| 566 |
GList *tmp = g_list_copy_deep(all_irqs, copy_irq, NULL); |
| 567 |
attrset(COLOR_PAIR(5)); |
| 568 |
mvprintw(LINES - 3, 1, "Move up and down the list, toggle ban with Enter."); |
| 569 |
mvprintw(LINES - 2, 1, "Press ESC for discarding and <S> for saving the values."); |
| 570 |
move(4, 19); |
| 571 |
curs_set(1); |
| 572 |
refresh(); |
| 573 |
size_t position = 4; |
| 574 |
char processing = 1; |
| 575 |
while(processing) { |
| 576 |
int direction = getch(); |
| 577 |
switch(direction) { |
| 578 |
case KEY_UP: |
| 579 |
if(position > 4) { |
| 580 |
position--; |
| 581 |
move(position, 19); |
| 582 |
} else if (offset > 0) { |
| 583 |
offset--; |
| 584 |
max_offset = 0; |
| 585 |
get_irq_name(LINES - 3); |
| 586 |
for_each_irq(tmp, print_tmp_irq_line, NULL); |
| 587 |
max_offset -= LINES - 7; |
| 588 |
if (max_offset < 0) |
| 589 |
max_offset = 0; |
| 590 |
move(position, 19); |
| 591 |
} |
| 592 |
break; |
| 593 |
case KEY_DOWN: |
| 594 |
if (position < (size_t)(LINES - 4)) { |
| 595 |
if(position < g_list_length(all_irqs) + 3) { |
| 596 |
position++; |
| 597 |
move(position, 19); |
| 598 |
} |
| 599 |
} else if (offset < max_offset) { |
| 600 |
offset++; |
| 601 |
max_offset = 0; |
| 602 |
get_irq_name(LINES - 3); |
| 603 |
for_each_irq(tmp, print_tmp_irq_line, NULL); |
| 604 |
max_offset -= LINES - 7; |
| 605 |
if (max_offset < 0) |
| 606 |
max_offset = 0; |
| 607 |
move(position, 19); |
| 608 |
} |
| 609 |
break; |
| 610 |
case '\n': |
| 611 |
case '\r': { |
| 612 |
attrset(COLOR_PAIR(3)); |
| 613 |
int banned = toggle_irq(tmp, position + offset - 4); |
| 614 |
if(banned) { |
| 615 |
mvprintw(position, 19, "YES"); |
| 616 |
} else { |
| 617 |
mvprintw(position, 19, "NO "); |
| 618 |
} |
| 619 |
move(position, 19); |
| 620 |
refresh(); |
| 621 |
break; |
| 622 |
} |
| 623 |
case 27: |
| 624 |
processing = 0; |
| 625 |
curs_set(0); |
| 626 |
/* Forget the changes */ |
| 627 |
g_list_free(tmp); |
| 628 |
print_all_irqs(); |
| 629 |
attrset(COLOR_PAIR(0)); |
| 630 |
mvprintw(LINES - 3, 1, " \ |
| 631 |
"); |
| 632 |
attrset(COLOR_PAIR(5)); |
| 633 |
mvprintw(LINES - 2, 1, "Press <I> for setting up IRQ banning.\ |
| 634 |
"); |
| 635 |
show_frame(); |
| 636 |
show_footer(); |
| 637 |
refresh(); |
| 638 |
break; |
| 639 |
case 's': |
| 640 |
processing = 0; |
| 641 |
g_list_free(all_irqs); |
| 642 |
all_irqs = tmp; |
| 643 |
curs_set(0); |
| 644 |
print_all_irqs(); |
| 645 |
attrset(COLOR_PAIR(0)); |
| 646 |
mvprintw(LINES - 3, 1, " \ |
| 647 |
"); |
| 648 |
attrset(COLOR_PAIR(5)); |
| 649 |
mvprintw(LINES - 2, 1, "Press <I> for setting up IRQ banning.\ |
| 650 |
"); |
| 651 |
attrset(COLOR_PAIR(3)); |
| 652 |
show_frame(); |
| 653 |
show_footer(); |
| 654 |
refresh(); |
| 655 |
char settings_string[1024] = BAN_IRQS; |
| 656 |
for_each_irq(all_irqs, get_new_irq_ban_values, settings_string); |
| 657 |
if(!strcmp(BAN_IRQS, settings_string)) { |
| 658 |
strncpy(settings_string + strlen(settings_string), |
| 659 |
" NONE", 1024 - strlen(settings_string)); |
| 660 |
} |
| 661 |
send_settings(settings_string); |
| 662 |
break; |
| 663 |
case 'q': |
| 664 |
processing = 0; |
| 665 |
close_window(0); |
| 666 |
break; |
| 667 |
default: |
| 668 |
break; |
| 669 |
} |
| 670 |
} |
| 671 |
} |
| 672 |
|
| 673 |
void handle_sleep_setting() |
| 674 |
{ |
| 675 |
char info[128] = "Current sleep interval between rebalancing: \0"; |
| 676 |
uint8_t sleep_input_offset = strlen(info) + 3; |
| 677 |
mvprintw(LINES - 1, 1, "Press ESC for discarding your input.\ |
| 678 |
"); |
| 679 |
attrset(COLOR_PAIR(0)); |
| 680 |
mvprintw(LINES - 2, 1, " \ |
| 681 |
"); |
| 682 |
uint64_t new_sleep = get_valid_sleep_input(sleep_input_offset); |
| 683 |
if(new_sleep != setup.sleep) { |
| 684 |
setup.sleep = new_sleep; |
| 685 |
char settings_data[128]; |
| 686 |
snprintf(settings_data, 128, "%s %" PRIu64, SET_SLEEP, new_sleep); |
| 687 |
send_settings(settings_data); |
| 688 |
} |
| 689 |
attrset(COLOR_PAIR(5)); |
| 690 |
mvprintw(LINES - 2, 1, "Press <S> for changing sleep setup, <C> for CPU ban setup. "); |
| 691 |
show_frame(); |
| 692 |
show_footer(); |
| 693 |
refresh(); |
| 694 |
} |
| 695 |
|
| 696 |
void init() |
| 697 |
{ |
| 698 |
signal(SIGINT, close_window); |
| 699 |
initscr(); |
| 700 |
keypad(stdscr, TRUE); |
| 701 |
curs_set(0); |
| 702 |
nonl(); |
| 703 |
cbreak(); |
| 704 |
nodelay(stdscr, TRUE); |
| 705 |
echo(); |
| 706 |
if(has_colors()) { |
| 707 |
start_color(); |
| 708 |
use_default_colors(); |
| 709 |
init_pair(1, COLOR_RED, -1); |
| 710 |
init_pair(2, COLOR_YELLOW, -1); |
| 711 |
init_pair(3, COLOR_GREEN, -1); |
| 712 |
init_pair(4, COLOR_WHITE, COLOR_BLUE); |
| 713 |
init_pair(5, -1, COLOR_RED); |
| 714 |
/* Pair 6 is unused */ |
| 715 |
/* Pair 7 is unused */ |
| 716 |
init_pair(8, COLOR_BLUE, -1); |
| 717 |
init_pair(9, COLOR_CYAN, -1); |
| 718 |
init_pair(10, COLOR_MAGENTA, -1); |
| 719 |
} |
| 720 |
|
| 721 |
offset = 0; |
| 722 |
display_tree(); |
| 723 |
} |
| 724 |
|
| 725 |
void close_window(int sig __attribute__((unused))) |
| 726 |
{ |
| 727 |
g_list_free(all_cpus); |
| 728 |
g_list_free(setup.banned_irqs); |
| 729 |
g_list_free(setup.banned_cpus); |
| 730 |
g_list_free_full(tree, free); |
| 731 |
endwin(); |
| 732 |
exit(EXIT_SUCCESS); |
| 733 |
} |
| 734 |
|
| 735 |
void settings() |
| 736 |
{ |
| 737 |
clear(); |
| 738 |
char *setup_data = get_data(SETUP); |
| 739 |
parse_setup(setup_data); |
| 740 |
|
| 741 |
char info[128] = "Current sleep interval between rebalancing: \0"; |
| 742 |
snprintf(info + strlen(info), 128 - strlen(info), "%" PRIu64 "\n", setup.sleep); |
| 743 |
attrset(COLOR_PAIR(1)); |
| 744 |
mvprintw(2, 3, "%s", info); |
| 745 |
print_all_cpus(); |
| 746 |
attrset(COLOR_PAIR(5)); |
| 747 |
mvprintw(LINES - 2, 1, "Press <S> for changing sleep setup, <C> for CPU ban setup. "); |
| 748 |
show_frame(); |
| 749 |
show_footer(); |
| 750 |
refresh(); |
| 751 |
free(setup_data); |
| 752 |
} |
| 753 |
|
| 754 |
void setup_irqs() |
| 755 |
{ |
| 756 |
clear(); |
| 757 |
get_all_irqs(); |
| 758 |
all_irqs = g_list_sort(all_irqs, sort_all_irqs); |
| 759 |
print_all_irqs(); |
| 760 |
attrset(COLOR_PAIR(5)); |
| 761 |
mvprintw(LINES - 2, 1, "Press <I> for setting up IRQ banning."); |
| 762 |
show_frame(); |
| 763 |
show_footer(); |
| 764 |
refresh(); |
| 765 |
} |
| 766 |
|
| 767 |
void display_tree_node_irqs(irq_t *irq, void *data) |
| 768 |
{ |
| 769 |
char indent[32] = " \0"; |
| 770 |
if (max_offset >= offset && max_offset - offset < LINES - 5) { |
| 771 |
snprintf(indent + strlen(indent), 32 - strlen(indent), "%s", (char *)data); |
| 772 |
attrset(COLOR_PAIR(3)); |
| 773 |
printw("%sIRQ %u, IRQs since last rebalance %lu\n", |
| 774 |
indent, irq->vector, irq->diff); |
| 775 |
} |
| 776 |
max_offset++; |
| 777 |
} |
| 778 |
|
| 779 |
void display_tree_node(cpu_node_t *node, void *data) |
| 780 |
{ |
| 781 |
int i; |
| 782 |
const char *node_type_to_str[] = { |
| 783 |
"CPU\0", |
| 784 |
"CACHE DOMAIN\0", |
| 785 |
"CPU PACKAGE\0", |
| 786 |
"NUMA NODE\0"}; |
| 787 |
|
| 788 |
char *spaces = " \0"; |
| 789 |
char indent[32] = "\0"; |
| 790 |
char *asciitree = " `--\0"; |
| 791 |
for(i = node->type; i <= OBJ_TYPE_NODE; i++) { |
| 792 |
snprintf(indent + strlen(indent), 32 - strlen(indent), "%s", spaces); |
| 793 |
if(i != OBJ_TYPE_NODE) { |
| 794 |
snprintf(indent + strlen(indent), 32 - strlen(indent), " "); |
| 795 |
} |
| 796 |
} |
| 797 |
snprintf(indent + strlen(indent), 32 - strlen(indent), "%s", asciitree); |
| 798 |
char copy_to[1024]; |
| 799 |
char *numa_available = "\0"; |
| 800 |
if((node->type == OBJ_TYPE_NODE) && (node->number == -1)) { |
| 801 |
numa_available = " (This machine is not NUMA-capable)"; |
| 802 |
} |
| 803 |
snprintf(copy_to, 1024, "%s%s, number %d%s, CPU mask %s\n", |
| 804 |
indent, node_type_to_str[node->type], node->number, numa_available, |
| 805 |
node->cpu_mask); |
| 806 |
switch(node->type) { |
| 807 |
case(OBJ_TYPE_CPU): |
| 808 |
attrset(COLOR_PAIR(1)); |
| 809 |
break; |
| 810 |
case(OBJ_TYPE_CACHE): |
| 811 |
attrset(COLOR_PAIR(2)); |
| 812 |
break; |
| 813 |
case(OBJ_TYPE_PACKAGE): |
| 814 |
attrset(COLOR_PAIR(8)); |
| 815 |
break; |
| 816 |
case(OBJ_TYPE_NODE): |
| 817 |
attrset(COLOR_PAIR(9)); |
| 818 |
break; |
| 819 |
default: |
| 820 |
break; |
| 821 |
} |
| 822 |
if (max_offset >= offset) |
| 823 |
printw("%s", copy_to); |
| 824 |
max_offset++; |
| 825 |
if(g_list_length(node->irqs) > 0) { |
| 826 |
for_each_irq(node->irqs, display_tree_node_irqs, indent); |
| 827 |
} |
| 828 |
if(g_list_length(node->children)) { |
| 829 |
for_each_node(node->children, display_tree_node, data); |
| 830 |
} |
| 831 |
} |
| 832 |
|
| 833 |
void display_tree() |
| 834 |
{ |
| 835 |
clear(); |
| 836 |
char *setup_data = get_data(SETUP); |
| 837 |
parse_setup(setup_data); |
| 838 |
char *irqbalance_data = get_data(STATS); |
| 839 |
parse_into_tree(irqbalance_data); |
| 840 |
display_banned_cpus(); |
| 841 |
max_offset = 0; |
| 842 |
for_each_node(tree, display_tree_node, NULL); |
| 843 |
max_offset -= LINES - 5; |
| 844 |
if (max_offset < 0) |
| 845 |
max_offset = 0; |
| 846 |
show_frame(); |
| 847 |
show_footer(); |
| 848 |
refresh(); |
| 849 |
free(setup_data); |
| 850 |
free(irqbalance_data); |
| 851 |
} |