From da3aec36d9d088e089399bc91609dda50de83091 Mon Sep 17 00:00:00 2001 From: Maxime Schmitt Date: Fri, 21 Oct 2022 21:02:48 +0200 Subject: [PATCH] Option to hide nvtop from the process list --- include/nvtop/interface_options.h | 1 + src/interface.c | 17 +++++++++++++++++ src/interface_options.c | 11 +++++++++++ src/interface_setup_win.c | 16 +++++++++++++--- 4 files changed, 42 insertions(+), 3 deletions(-) diff --git a/include/nvtop/interface_options.h b/include/nvtop/interface_options.h index c6f328c..bcbb533 100644 --- a/include/nvtop/interface_options.h +++ b/include/nvtop/interface_options.h @@ -48,6 +48,7 @@ typedef struct nvtop_interface_option_struct { process_field_displayed process_fields_displayed; // Which columns of the // process list are displayed bool show_startup_messages; // True to show the startup messages + bool filter_nvtop_pid; // Do not show nvtop pid in the processes list bool has_monitored_set_changed; // True if the set of monitored gpu was modified through the interface } nvtop_interface_option; diff --git a/src/interface.c b/src/interface.c index 427e675..5ebba1e 100644 --- a/src/interface.c +++ b/src/interface.c @@ -326,6 +326,8 @@ static unsigned device_length(void) { sizeof_device_field[device_fan_speed] + sizeof_device_field[device_power] + 4); } +static pid_t nvtop_pid; + static void initialize_all_windows(struct nvtop_interface *dwin) { int rows, cols; getmaxyx(stdscr, rows, cols); @@ -355,6 +357,7 @@ static void initialize_all_windows(struct nvtop_interface *dwin) { dwin->shortcut_window = newwin(1, cols, rows - 1, 0); alloc_setup_window(&setup_position, &dwin->setup_win); + nvtop_pid = getpid(); } static void delete_all_windows(struct nvtop_interface *dwin) { @@ -1043,6 +1046,19 @@ static void sort_process(all_processes all_procs, enum process_field criterion, qsort(all_procs.processes, all_procs.processes_count, sizeof(*all_procs.processes), sort_fun); } +static void filter_out_nvtop_pid(all_processes *all_procs, struct nvtop_interface *interface) { + if (interface->options.filter_nvtop_pid) { + for (unsigned procId = 0; procId < all_procs->processes_count; ++procId) { + if (all_procs->processes[procId].process->pid == nvtop_pid) { + memmove(&all_procs->processes[procId], &all_procs->processes[procId + 1], + all_procs->processes_count - procId - 1); + all_procs->processes_count = all_procs->processes_count - 1; + break; + } + } + } +} + static const char *columnName[process_field_count] = { "PID", "USER", "DEV", "TYPE", "GPU", "ENC", "DEC", "GPU MEM", "CPU", "HOST MEM", "Command", }; @@ -1280,6 +1296,7 @@ static void draw_processes(struct list_head *devices, struct nvtop_interface *in update_process_option_win(interface); all_processes all_procs = all_processes_array(devices); + filter_out_nvtop_pid(&all_procs, interface); sort_process(all_procs, interface->options.sort_processes_by, !interface->options.sort_descending_order); if (all_procs.processes_count > 0) { diff --git a/src/interface_options.c b/src/interface_options.c index 37c971d..40a6216 100644 --- a/src/interface_options.c +++ b/src/interface_options.c @@ -124,6 +124,7 @@ void alloc_interface_options_internals(char *config_location, unsigned num_devic options->process_fields_displayed = 0; options->has_monitored_set_changed = false; options->show_startup_messages = true; + options->filter_nvtop_pid = true; if (config_location) { options->config_file_location = malloc(strlen(config_location) + 1); if (!options->config_file_location) { @@ -168,6 +169,7 @@ static const char chart_section[] = "ChartOption"; static const char chart_value_reverse[] = "ReverseChart"; static const char process_list_section[] = "ProcessListOption"; +static const char process_hide_nvtop_process[] = "HideNvtopProcess"; static const char process_value_sortby[] = "SortBy"; static const char process_value_display_field[] = "DisplayField"; static const char *process_sortby_vals[process_field_count + 1] = { @@ -239,6 +241,14 @@ static int nvtop_option_ini_handler(void *user, const char *section, const char } // Process List Options if (strcmp(section, process_list_section) == 0) { + if (strcmp(name, process_hide_nvtop_process) == 0) { + if (strcmp(value, "true") == 0) { + ini_data->options->filter_nvtop_pid = true; + } + if (strcmp(value, "false") == 0) { + ini_data->options->filter_nvtop_pid = false; + } + } if (strcmp(name, process_value_sortby) == 0) { for (enum process_field i = process_pid; i < process_field_count; ++i) { if (strcmp(value, process_sortby_vals[i]) == 0) { @@ -370,6 +380,7 @@ bool save_interface_options_to_config_file(unsigned total_dev_count, const nvtop // Process Options fprintf(config_file, "\n[%s]\n", process_list_section); + fprintf(config_file, "%s = %s\n", process_hide_nvtop_process, boolean_string(options->filter_nvtop_pid)); fprintf(config_file, "%s = %s\n", process_value_sort_order, options->sort_descending_order ? process_sort_descending : process_sort_ascending); fprintf(config_file, "%s = %s\n", process_value_sortby, process_sortby_vals[options->sort_processes_by]); diff --git a/src/interface_setup_win.c b/src/interface_setup_win.c index ec4bebc..c18aa2e 100644 --- a/src/interface_setup_win.c +++ b/src/interface_setup_win.c @@ -78,14 +78,15 @@ static const char *setup_chart_gpu_value_descriptions[plot_information_count] = // Process List Options enum setup_proc_list_options { + setup_proc_list_hide_nvtop_process, setup_proc_list_sort_ascending, setup_proc_list_sort_by, setup_proc_list_display, setup_proc_list_options_count }; -static const char *setup_proc_list_option_description[setup_proc_list_options_count] = {"Sort Ascending", "Sort by", - "Field Displayed"}; +static const char *setup_proc_list_option_description[setup_proc_list_options_count] = { + "Hide nvtop process", "Sort Ascending", "Sort by", "Field Displayed"}; static const char *setup_proc_list_value_descriptions[process_field_count] = { "Process Id", "User name", "Device Id", "Workload type", "GPU usage", "Encoder usage", @@ -444,7 +445,14 @@ static void draw_setup_window_proc_list(struct nvtop_interface *interface) { mvwchgat(option_list_win, 0, cur_col, maxcols - cur_col, A_STANDOUT, green_color, NULL); // Sort Ascending - enum option_state option_state = !interface->options.sort_descending_order; + enum option_state option_state = interface->options.filter_nvtop_pid; + mvwprintw(option_list_win, setup_proc_list_hide_nvtop_process + 1, 0, "[%c] %s", option_state_char(option_state), + setup_proc_list_option_description[setup_proc_list_hide_nvtop_process]); + if (interface->setup_win.indentation_level == 1 && + interface->setup_win.options_selected[0] == setup_proc_list_hide_nvtop_process) { + mvwchgat(option_list_win, setup_proc_list_hide_nvtop_process + 1, 0, 3, A_STANDOUT, cyan_color, NULL); + } + option_state = !interface->options.sort_descending_order; mvwprintw(option_list_win, setup_proc_list_sort_ascending + 1, 0, "[%c] %s", option_state_char(option_state), setup_proc_list_option_description[setup_proc_list_sort_ascending]); if (interface->setup_win.indentation_level == 1 && @@ -764,6 +772,8 @@ void handle_setup_win_keypress(int keyId, struct nvtop_interface *interface) { if (interface->setup_win.indentation_level == 1) { if (interface->setup_win.options_selected[0] == setup_proc_list_sort_ascending) { interface->options.sort_descending_order = !interface->options.sort_descending_order; + } else if (interface->setup_win.options_selected[0] == setup_proc_list_hide_nvtop_process) { + interface->options.filter_nvtop_pid = !interface->options.filter_nvtop_pid; } else if (interface->setup_win.options_selected[0] == setup_proc_list_sort_by) { handle_setup_win_keypress(KEY_RIGHT, interface); }