Blame SOURCES/0012-irqbalance-ui-support-scroll-under-tui-mode-of-irqba.patch

120141
From db7dc03388a20ba2d873a81c3e14b933e2b09551 Mon Sep 17 00:00:00 2001
120141
From: Liu Chao <liuchao173@huawei.com>
120141
Date: Mon, 4 Jul 2022 16:25:14 +0800
120141
Subject: [PATCH 12/14] irqbalance-ui: support scroll under tui mode of
120141
 irqbalance-ui
120141
120141
support using Up, Down, PageUp, PageDown to scroll in view mode
120141
support using Up and Down in edit mode
120141
120141
Signed-off-by: Liu Chao <liuchao173@huawei.com>
120141
---
120141
 ui/helpers.c       |   2 +
120141
 ui/irqbalance-ui.c | 138 ++++++++++++++++++++++++---------
120141
 ui/irqbalance-ui.h |   2 +
120141
 ui/ui.c            | 187 ++++++++++++++++++++++++++++++++++++---------
120141
 ui/ui.h            |   3 +
120141
 5 files changed, 262 insertions(+), 70 deletions(-)
120141
120141
diff --git a/ui/helpers.c b/ui/helpers.c
120141
index 5d71275..0e9f76c 100644
120141
--- a/ui/helpers.c
120141
+++ b/ui/helpers.c
120141
@@ -89,6 +89,7 @@ gpointer copy_cpu_ban(gconstpointer src, gpointer data __attribute__((unused)))
120141
 	cpu_ban_t *new = malloc(sizeof(cpu_ban_t));
120141
 	new->number = old->number;
120141
 	new->is_banned = old->is_banned;
120141
+	new->is_changed = 0;
120141
 	return new;
120141
 }
120141
 
120141
@@ -100,6 +101,7 @@ gpointer copy_irq(gconstpointer src, gpointer data __attribute__((unused)))
120141
 	new->load = old->load;
120141
 	new->diff = old->diff;
120141
 	new->is_banned = old->is_banned;
120141
+	new->is_changed = 0;
120141
 	new->class = old->class;
120141
 	new->assigned_to = g_list_copy(old->assigned_to);
120141
 	return new;
120141
diff --git a/ui/irqbalance-ui.c b/ui/irqbalance-ui.c
120141
index 89ed94a..47b6c88 100644
120141
--- a/ui/irqbalance-ui.c
120141
+++ b/ui/irqbalance-ui.c
120141
@@ -371,48 +371,116 @@ gboolean rescan_tree(gpointer data __attribute__((unused)))
120141
 	free(irqbalance_data);
120141
 	return TRUE;
120141
 }
120141
-
120141
-gboolean key_loop(gpointer data __attribute__((unused)))
120141
-{
120141
-	int c = getch();
120141
-	switch(c) {
120141
-	case 'q':
120141
-		close_window(0);
120141
-		break;
120141
-	case KEY_F(3):
120141
-		if (state == STATE_SETTINGS || state == STATE_SETUP_IRQS) {
120141
-			state = STATE_TREE;
120141
-			display_tree();
120141
-		}
120141
+void scroll_window() {
120141
+	switch(state) {
120141
+	case STATE_TREE:
120141
+		display_tree();
120141
 		break;
120141
-	case KEY_F(4):
120141
-		if (state == STATE_TREE || state == STATE_SETUP_IRQS) {
120141
-			state = STATE_SETTINGS;
120141
-			settings();
120141
-		}
120141
+	case STATE_SETTINGS:
120141
 		settings();
120141
 		break;
120141
-	case KEY_F(5):
120141
-		if (state == STATE_TREE || state == STATE_SETTINGS) {
120141
-			state = STATE_SETUP_IRQS;
120141
-			setup_irqs();
120141
-		}
120141
-		break;
120141
-	case 'c':
120141
-		if (state == STATE_SETTINGS)
120141
-			handle_cpu_banning();
120141
-		break;
120141
-	case 'i':
120141
-		if (state == STATE_SETUP_IRQS)
120141
-			handle_irq_banning();
120141
-		break;
120141
-	case 's':
120141
-		if (state == STATE_SETTINGS)
120141
-			handle_sleep_setting();
120141
+	case STATE_SETUP_IRQS:
120141
+		setup_irqs();
120141
 		break;
120141
 	default:
120141
 		break;
120141
 	}
120141
+}
120141
+
120141
+gboolean key_loop(gpointer data __attribute__((unused)))
120141
+{
120141
+	while(1) {
120141
+		int c = getch();
120141
+		switch(c) {
120141
+		case 'q':
120141
+			close_window(0);
120141
+			break;
120141
+		case KEY_UP:
120141
+			if (offset > 0) {
120141
+				offset--;
120141
+				scroll_window();
120141
+			}
120141
+			break;
120141
+		case KEY_DOWN:
120141
+			if (offset < max_offset) {
120141
+				offset++;
120141
+				scroll_window();
120141
+			}
120141
+			break;
120141
+		case KEY_NPAGE:
120141
+			switch (state) {
120141
+			case STATE_TREE:
120141
+				offset += LINES - 5;
120141
+				break;
120141
+			case STATE_SETTINGS:
120141
+				offset += LINES - 8;
120141
+				break;
120141
+			case STATE_SETUP_IRQS:
120141
+				offset += LINES - 6;
120141
+				break;
120141
+			default:
120141
+				break;
120141
+			}
120141
+			if (offset > max_offset)
120141
+				offset = max_offset;
120141
+			scroll_window();
120141
+			break;
120141
+		case KEY_PPAGE:
120141
+			switch (state) {
120141
+			case STATE_TREE:
120141
+				offset -= LINES - 5;
120141
+				break;
120141
+			case STATE_SETTINGS:
120141
+				offset -= LINES - 8;
120141
+				break;
120141
+			case STATE_SETUP_IRQS:
120141
+				offset -= LINES - 6;
120141
+				break;
120141
+			default:
120141
+				break;
120141
+			}
120141
+			if (offset < 0)
120141
+				offset = 0;
120141
+			scroll_window();
120141
+			break;
120141
+		case KEY_F(3):
120141
+			if (state == STATE_SETTINGS || state == STATE_SETUP_IRQS) {
120141
+				state = STATE_TREE;
120141
+				offset = 0;
120141
+				display_tree();
120141
+			}
120141
+			break;
120141
+		case KEY_F(4):
120141
+			if (state == STATE_TREE || state == STATE_SETUP_IRQS) {
120141
+				state = STATE_SETTINGS;
120141
+				offset = 0;
120141
+				settings();
120141
+			}
120141
+			settings();
120141
+			break;
120141
+		case KEY_F(5):
120141
+			if (state == STATE_TREE || state == STATE_SETTINGS) {
120141
+				state = STATE_SETUP_IRQS;
120141
+				offset = 0;
120141
+				setup_irqs();
120141
+			}
120141
+			break;
120141
+		case 'c':
120141
+			if (state == STATE_SETTINGS)
120141
+				handle_cpu_banning();
120141
+			break;
120141
+		case 'i':
120141
+			if (state == STATE_SETUP_IRQS)
120141
+				handle_irq_banning();
120141
+			break;
120141
+		case 's':
120141
+			if (state == STATE_SETTINGS)
120141
+				handle_sleep_setting();
120141
+			break;
120141
+		default:
120141
+			break;
120141
+		}
120141
+	}
120141
 	return TRUE;
120141
 }
120141
 
120141
diff --git a/ui/irqbalance-ui.h b/ui/irqbalance-ui.h
120141
index fba7e7c..dc24083 100644
120141
--- a/ui/irqbalance-ui.h
120141
+++ b/ui/irqbalance-ui.h
120141
@@ -41,6 +41,7 @@ typedef struct irq {
120141
 	uint64_t load;
120141
 	uint64_t diff;
120141
 	char is_banned;
120141
+	char is_changed;
120141
 	GList *assigned_to;
120141
 	int class;
120141
 } irq_t;
120141
@@ -60,6 +61,7 @@ typedef struct cpu_node {
120141
 typedef struct cpu_ban {
120141
 	int number;
120141
 	char is_banned;
120141
+	char is_changed;
120141
 } cpu_ban_t;
120141
 
120141
 typedef struct setup {
120141
diff --git a/ui/ui.c b/ui/ui.c
120141
index 6b1c689..2dad442 100644
120141
--- a/ui/ui.c
120141
+++ b/ui/ui.c
120141
@@ -3,6 +3,8 @@
120141
 #include <string.h>
120141
 #include "ui.h"
120141
 
120141
+int offset;
120141
+int max_offset;
120141
 
120141
 GList *all_cpus = NULL;
120141
 GList *all_irqs = NULL;
120141
@@ -134,32 +136,54 @@ void get_banned_cpu(int *cpu, void *data __attribute__((unused)))
120141
 	all_cpus = g_list_append(all_cpus, new);
120141
 }
120141
 
120141
-void print_cpu_line(cpu_ban_t *cpu, void *data)
120141
+void print_tmp_cpu_line(cpu_ban_t *cpu, void *data __attribute__((unused)))
120141
 {
120141
-	int *line_offset = data;
120141
-	if(cpu->is_banned) {
120141
-		attrset(COLOR_PAIR(10));
120141
-	} else {
120141
-		attrset(COLOR_PAIR(9));
120141
+	int line = max_offset - offset + 6;
120141
+	if (max_offset >= offset && line < LINES - 3) {
120141
+		if (cpu->is_changed)
120141
+			attrset(COLOR_PAIR(3));
120141
+		else if(cpu->is_banned)
120141
+			attrset(COLOR_PAIR(10));
120141
+		else
120141
+			attrset(COLOR_PAIR(9));
120141
+		mvprintw(line, 3, "CPU %d     ", cpu->number);
120141
+		mvprintw(line, 19, "%s", cpu->is_banned ?
120141
+				"YES	" :
120141
+				"NO	 ");
120141
 	}
120141
-	mvprintw(*line_offset, 3, "CPU %d", cpu->number);
120141
-	mvprintw(*line_offset, 19, "%s", cpu->is_banned ?
120141
-			"YES	" :
120141
-			"NO	 ");
120141
-	(*line_offset)++;
120141
+	max_offset++;
120141
+}
120141
+
120141
+void print_cpu_line(cpu_ban_t *cpu, void *data __attribute__((unused)))
120141
+{
120141
+	int line = max_offset - offset + 6;
120141
+	if (max_offset >= offset && line < LINES - 2) {
120141
+		if(cpu->is_banned)
120141
+			attrset(COLOR_PAIR(10));
120141
+		else
120141
+			attrset(COLOR_PAIR(9));
120141
+		mvprintw(line, 3, "CPU %d     ", cpu->number);
120141
+		mvprintw(line, 19, "%s", cpu->is_banned ?
120141
+				"YES	" :
120141
+				"NO	 ");
120141
+	}
120141
+	max_offset++;
120141
 }
120141
 
120141
 void print_all_cpus()
120141
 {
120141
+	max_offset = 0;
120141
 	if(all_cpus == NULL) {
120141
 		for_each_node(tree, get_cpu, NULL);
120141
 		for_each_int(setup.banned_cpus, get_banned_cpu, NULL);
120141
 		all_cpus = g_list_sort(all_cpus, sort_all_cpus);
120141
 	}
120141
-	int line = 6;
120141
 	attrset(COLOR_PAIR(2));
120141
 	mvprintw(4, 3, "NUMBER          IS BANNED");
120141
-	for_each_cpu(all_cpus, print_cpu_line, &line);
120141
+	for_each_cpu(all_cpus, print_cpu_line, NULL);
120141
+	max_offset -= LINES - 8;
120141
+	if (max_offset < 0)
120141
+		max_offset = 0;
120141
 }
120141
 
120141
 void add_banned_cpu(int *banned_cpu, void *data)
120141
@@ -195,6 +219,7 @@ int toggle_cpu(GList *cpu_list, int cpu_number)
120141
 	} else {
120141
 		((cpu_ban_t *)(entry->data))->is_banned = 1;
120141
 	}
120141
+	((cpu_ban_t *)(entry->data))->is_changed = 1;
120141
 	return ((cpu_ban_t *)(entry->data))->is_banned;
120141
 }
120141
 
120141
@@ -239,18 +264,37 @@ void handle_cpu_banning()
120141
 			if(position > 6) {
120141
 				position--;
120141
 				move(position, 19);
120141
+			} else if (offset > 0) {
120141
+				offset--;
120141
+				max_offset = 0;
120141
+				for_each_cpu(tmp, print_tmp_cpu_line, NULL);
120141
+				max_offset -= LINES - 9;
120141
+				if (max_offset < 0)
120141
+					max_offset = 0;
120141
+				move(position, 19);
120141
 			}
120141
 			break;
120141
 		case KEY_DOWN:
120141
-			if(position <= g_list_length(all_cpus) + 4) {
120141
-				position++;
120141
+			if(position < (size_t)(LINES - 4)) {
120141
+				if (position <= g_list_length(all_cpus) + 4 - offset) {
120141
+					position++;
120141
+					move(position, 19);
120141
+				}
120141
+			} else if (offset < max_offset) {
120141
+				offset++;
120141
+				max_offset = 0;
120141
+				for_each_cpu(tmp, print_tmp_cpu_line, NULL);
120141
+				max_offset -= LINES - 9;
120141
+				if (max_offset < 0)
120141
+					max_offset = 0;
120141
 				move(position, 19);
120141
 			}
120141
 			break;
120141
 		case '\n':
120141
 		case '\r': {
120141
 			attrset(COLOR_PAIR(3));
120141
-			int banned = toggle_cpu(tmp, position - 6);
120141
+			int banned = toggle_cpu(tmp, position + offset - 6);
120141
+			mvprintw(position, 3, "CPU %d     ", position + offset - 6);
120141
 			if(banned) {
120141
 				mvprintw(position, 19, "YES");
120141
 			} else {
120141
@@ -263,8 +307,7 @@ void handle_cpu_banning()
120141
 		case 27:
120141
 			processing = 0;
120141
 			curs_set(0);
120141
-			/* Forget the changes */
120141
-			tmp = g_list_copy_deep(all_cpus, copy_cpu_ban, NULL);
120141
+			g_list_free(tmp);
120141
 			print_all_cpus();
120141
 			attrset(COLOR_PAIR(0));
120141
 			mvprintw(LINES - 3, 1, "			\
120141
@@ -278,6 +321,7 @@ void handle_cpu_banning()
120141
 			break;
120141
 		case 's':
120141
 			processing = 0;
120141
+			g_list_free(all_cpus);
120141
 			all_cpus = tmp;
120141
 			curs_set(0);
120141
 			print_all_cpus();
120141
@@ -324,9 +368,12 @@ void print_assigned_objects_string(irq_t *irq, int *line_offset)
120141
 	mvprintw(*line_offset, 68, "%s", assigned_to);
120141
 }
120141
 
120141
-void print_irq_line(irq_t *irq, void *data)
120141
+void print_tmp_irq_line(irq_t *irq, void *data __attribute__((unused)))
120141
 {
120141
-	int *line_offset = data;
120141
+	int line = max_offset - offset + 4;
120141
+	max_offset++;
120141
+	if (line < 4 || line >= LINES - 3)
120141
+		return;
120141
 	switch(irq->class) {
120141
 	case(IRQ_OTHER):
120141
 		attrset(COLOR_PAIR(1));
120141
@@ -352,23 +399,62 @@ void print_irq_line(irq_t *irq, void *data)
120141
 		attrset(COLOR_PAIR(0));
120141
 		break;
120141
 	}
120141
-	mvprintw(*line_offset, 3, "IRQ %d", irq->vector);
120141
-	mvprintw(*line_offset, 19, "%s", irq->is_banned ? "YES" : "NO ");
120141
-	mvprintw(*line_offset, 36, "%s",
120141
+	mvprintw(line, 3, "IRQ %d      ", irq->vector);
120141
+	mvprintw(line, 19, "%s", irq->is_banned ? "YES" : "NO ");
120141
+	mvprintw(line, 36, "%s               ",
120141
 			 irq->class < 0 ? "Unknown" : IRQ_CLASS_TO_STR[irq->class]);
120141
-	print_assigned_objects_string(irq, line_offset);
120141
-	(*line_offset)++;
120141
+	print_assigned_objects_string(irq, &line);
120141
+}
120141
 
120141
+void print_irq_line(irq_t *irq, void *data __attribute__((unused)))
120141
+{
120141
+	int line = max_offset - offset + 4;
120141
+	max_offset++;
120141
+	if (line < 4 || line >= LINES - 2)
120141
+		return;
120141
+	switch(irq->class) {
120141
+	case(IRQ_OTHER):
120141
+		attrset(COLOR_PAIR(1));
120141
+		break;
120141
+	case(IRQ_LEGACY):
120141
+		attrset(COLOR_PAIR(2));
120141
+		break;
120141
+	case(IRQ_SCSI):
120141
+		attrset(COLOR_PAIR(3));
120141
+		break;
120141
+	case(IRQ_VIDEO):
120141
+		attrset(COLOR_PAIR(8));
120141
+		break;
120141
+	case(IRQ_ETH):
120141
+	case(IRQ_GBETH):
120141
+	case(IRQ_10GBETH):
120141
+		attrset(COLOR_PAIR(9));
120141
+		break;
120141
+	case(IRQ_VIRT_EVENT):
120141
+		attrset(COLOR_PAIR(10));
120141
+		break;
120141
+	default:
120141
+		attrset(COLOR_PAIR(0));
120141
+		break;
120141
+	}
120141
+	mvprintw(line, 3, "IRQ %d", irq->vector);
120141
+	mvprintw(line, 19, "%s", irq->is_banned ? "YES" : "NO ");
120141
+	mvprintw(line, 36, "%s               ",
120141
+			 irq->class < 0 ? "Unknown" : IRQ_CLASS_TO_STR[irq->class]);
120141
+	print_assigned_objects_string(irq, &line);
120141
 }
120141
 
120141
 void print_all_irqs()
120141
 {
120141
-	int line = 4;
120141
+	max_offset = 0;
120141
 	attrset(COLOR_PAIR(0));
120141
 	mvprintw(2, 3,
120141
 			"NUMBER          IS BANNED        CLASS      \
120141
 			    ASSIGNED TO CPUS");
120141
-	for_each_irq(all_irqs, print_irq_line, &line);
120141
+	for_each_irq(all_irqs, print_irq_line, NULL);
120141
+	max_offset -= LINES - 6;
120141
+	if (max_offset < 0)
120141
+		max_offset = 0;
120141
 }
120141
 
120141
 int toggle_irq(GList *irq_list, int position)
120141
@@ -384,6 +470,7 @@ int toggle_irq(GList *irq_list, int position)
120141
 	} else {
120141
 		((irq_t *)(entry->data))->is_banned = 1;
120141
 	}
120141
+	((irq_t *)(entry->data))->is_changed = 1;
120141
 	return ((irq_t *)(entry->data))->is_banned;
120141
 }
120141
 
120141
@@ -431,18 +518,36 @@ void handle_irq_banning()
120141
 			if(position > 4) {
120141
 				position--;
120141
 				move(position, 19);
120141
+			} else if (offset > 0) {
120141
+				offset--;
120141
+				max_offset = 0;
120141
+				for_each_irq(tmp, print_tmp_irq_line, NULL);
120141
+				max_offset -= LINES - 7;
120141
+				if (max_offset < 0)
120141
+					max_offset = 0;
120141
+				move(position, 19);
120141
 			}
120141
 			break;
120141
 		case KEY_DOWN:
120141
-			if(position < g_list_length(all_irqs) + 3) {
120141
-				position++;
120141
+			if (position < (size_t)(LINES  - 4)) {
120141
+				if(position < g_list_length(all_irqs) + 3) {
120141
+					position++;
120141
+					move(position, 19);
120141
+				}
120141
+			} else if (offset < max_offset) {
120141
+				offset++;
120141
+				max_offset = 0;
120141
+				for_each_irq(tmp, print_tmp_irq_line, NULL);
120141
+				max_offset -= LINES - 7;
120141
+				if (max_offset < 0)
120141
+					max_offset = 0;
120141
 				move(position, 19);
120141
 			}
120141
 			break;
120141
 		case '\n':
120141
 		case '\r': {
120141
 			attrset(COLOR_PAIR(3));
120141
-			int banned = toggle_irq(tmp, position - 4);
120141
+			int banned = toggle_irq(tmp, position + offset - 4);
120141
 			if(banned) {
120141
 				mvprintw(position, 19, "YES");
120141
 			} else {
120141
@@ -456,7 +561,7 @@ void handle_irq_banning()
120141
 			processing = 0;
120141
 			curs_set(0);
120141
 			/* Forget the changes */
120141
-			tmp = g_list_copy_deep(all_irqs, copy_irq, NULL);
120141
+			g_list_free(tmp);
120141
 			print_all_irqs();
120141
 			attrset(COLOR_PAIR(0));
120141
 			mvprintw(LINES - 3, 1, "			\
120141
@@ -470,6 +575,7 @@ void handle_irq_banning()
120141
 			break;
120141
 		case 's':
120141
 			processing = 0;
120141
+			g_list_free(all_irqs);
120141
 			all_irqs = tmp;
120141
 			curs_set(0);
120141
 			print_all_irqs();
120141
@@ -548,11 +654,13 @@ void init()
120141
 		init_pair(10, COLOR_MAGENTA, COLOR_BLACK);
120141
 	}
120141
 
120141
+	offset = 0;
120141
 	display_tree();
120141
 }
120141
 
120141
 void close_window(int sig __attribute__((unused)))
120141
 {
120141
+	g_list_free(all_cpus);
120141
 	g_list_free(setup.banned_irqs);
120141
 	g_list_free(setup.banned_cpus);
120141
 	g_list_free_full(tree, free);
120141
@@ -595,10 +703,13 @@ void setup_irqs()
120141
 void display_tree_node_irqs(irq_t *irq, void *data)
120141
 {
120141
 	char indent[32] = "	   \0";
120141
-	snprintf(indent + strlen(indent), 32 - strlen(indent), "%s", (char *)data);
120141
-	attrset(COLOR_PAIR(3));
120141
-	printw("%sIRQ %u, IRQs since last rebalance %lu\n",
120141
+	if (max_offset >= offset && max_offset - offset < LINES - 5) {
120141
+		snprintf(indent + strlen(indent), 32 - strlen(indent), "%s", (char *)data);
120141
+		attrset(COLOR_PAIR(3));
120141
+		printw("%sIRQ %u, IRQs since last rebalance %lu\n",
120141
 			indent, irq->vector, irq->diff);
120141
+	}
120141
+	max_offset++;
120141
 }
120141
 
120141
 void display_tree_node(cpu_node_t *node, void *data)
120141
@@ -644,7 +755,9 @@ void display_tree_node(cpu_node_t *node, void *data)
120141
 	default:
120141
 		break;
120141
 	}
120141
-	printw("%s", copy_to);
120141
+	if (max_offset >= offset)
120141
+		printw("%s", copy_to);
120141
+	max_offset++;
120141
 	if(g_list_length(node->irqs) > 0) {
120141
 		for_each_irq(node->irqs, display_tree_node_irqs, indent);
120141
 	}
120141
@@ -661,7 +774,11 @@ void display_tree()
120141
 	char *irqbalance_data = get_data(STATS);
120141
 	parse_into_tree(irqbalance_data);
120141
 	display_banned_cpus();
120141
+	max_offset = 0;
120141
 	for_each_node(tree, display_tree_node, NULL);
120141
+	max_offset -= LINES - 5;
120141
+	if (max_offset < 0)
120141
+		max_offset = 0;
120141
 	show_frame();
120141
 	show_footer();
120141
 	refresh();
120141
diff --git a/ui/ui.h b/ui/ui.h
120141
index ca2a3a6..da5b4b9 100644
120141
--- a/ui/ui.h
120141
+++ b/ui/ui.h
120141
@@ -14,6 +14,9 @@
120141
 extern GList *tree;
120141
 extern setup_t setup;
120141
 
120141
+extern int offset;
120141
+extern int max_offset;
120141
+
120141
 void show_frame();
120141
 void show_footer();
120141
 
120141
-- 
120141
2.33.1
120141