| 1 | /* |
| 2 | * SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | * |
| 4 | * SPDX-FileCopyrightText: Copyright (C) 2020 Jacob Barkdull |
| 5 | */ |
| 6 | |
| 7 | #include "config.h" |
| 8 | |
| 9 | #include <glib/gi18n.h> |
| 10 | |
| 11 | #include <sys/types.h> |
| 12 | #include <unistd.h> |
| 13 | #include <glibtop/procaffinity.h> |
| 14 | |
| 15 | #include "application.h" |
| 16 | #include "gsm-actions.h" |
| 17 | #include "procinfo.h" |
| 18 | #include "proctable.h" |
| 19 | |
| 20 | #include "setaffinity.h" |
| 21 | |
| 22 | namespace |
| 23 | { |
| 24 | class SetAffinityData |
| 25 | { |
| 26 | public: |
| 27 | GtkWidget *dialog; |
| 28 | pid_t pid; |
| 29 | GtkWidget **buttons; |
| 30 | GtkWidget *all_threads_row; |
| 31 | guint32 cpu_count; |
| 32 | gboolean toggle_single_blocked; |
| 33 | gboolean toggle_all_blocked; |
| 34 | }; |
| 35 | } |
| 36 | |
| 37 | static gboolean |
| 38 | all_toggled (SetAffinityData *affinity) |
| 39 | { |
| 40 | guint32 i; |
| 41 | |
| 42 | /* Check if any CPU's aren't set for this process */ |
| 43 | for (i = 1; i <= affinity->cpu_count; i++) |
| 44 | /* If so, return FALSE */ |
| 45 | if (!adw_switch_row_get_active (ADW_SWITCH_ROW (affinity->buttons[i]))) |
| 46 | return FALSE; |
| 47 | |
| 48 | return TRUE; |
| 49 | } |
| 50 | |
| 51 | static void |
| 52 | affinity_toggler_single (GObject *self, |
| 53 | GParamSpec *, |
| 54 | gpointer data) |
| 55 | { |
| 56 | SetAffinityData *affinity = static_cast<SetAffinityData *>(data); |
| 57 | gboolean toggle_all_state = FALSE; |
| 58 | |
| 59 | /* Return void if toggle single is blocked */ |
| 60 | if (affinity->toggle_single_blocked == TRUE) |
| 61 | return; |
| 62 | |
| 63 | /* Set toggle all state based on whether all are toggled */ |
| 64 | if (adw_switch_row_get_active (ADW_SWITCH_ROW (self))) |
| 65 | toggle_all_state = all_toggled (affinity); |
| 66 | |
| 67 | /* Block toggle all signal */ |
| 68 | affinity->toggle_all_blocked = TRUE; |
| 69 | |
| 70 | /* Set toggle all check box state */ |
| 71 | adw_switch_row_set_active (ADW_SWITCH_ROW (affinity->buttons[0]), |
| 72 | toggle_all_state); |
| 73 | |
| 74 | /* Unblock toggle all signal */ |
| 75 | affinity->toggle_all_blocked = FALSE; |
| 76 | } |
| 77 | |
| 78 | static void |
| 79 | affinity_toggle_all (GObject *self, |
| 80 | GParamSpec *, |
| 81 | gpointer data) |
| 82 | { |
| 83 | SetAffinityData *affinity = static_cast<SetAffinityData *>(data); |
| 84 | |
| 85 | guint32 i; |
| 86 | gboolean state; |
| 87 | |
| 88 | /* Return void if toggle all is blocked */ |
| 89 | if (affinity->toggle_all_blocked == TRUE) |
| 90 | return; |
| 91 | |
| 92 | /* Set individual CPU toggles based on toggle all state */ |
| 93 | state = adw_switch_row_get_active (ADW_SWITCH_ROW (self)); |
| 94 | |
| 95 | /* Block toggle single signal */ |
| 96 | affinity->toggle_single_blocked = TRUE; |
| 97 | |
| 98 | /* Set all CPU check boxes to specified state */ |
| 99 | for (i = 1; i <= affinity->cpu_count; i++) |
| 100 | adw_switch_row_set_active ( |
| 101 | ADW_SWITCH_ROW (affinity->buttons[i]), |
| 102 | state); |
| 103 | |
| 104 | /* Unblock toggle single signal */ |
| 105 | affinity->toggle_single_blocked = FALSE; |
| 106 | } |
| 107 | |
| 108 | |
| 109 | static void |
| 110 | set_affinity_error (GError *error) |
| 111 | { |
| 112 | AdwDialog *dialog; |
| 113 | |
| 114 | /* Create error alert dialog */ |
| 115 | dialog = adw_alert_dialog_new (_("GNU CPU Affinity error"), |
| 116 | NULL); |
| 117 | |
| 118 | if (error) { |
| 119 | adw_alert_dialog_set_body (ADW_ALERT_DIALOG (dialog), error->message); |
| 120 | } else { |
| 121 | adw_alert_dialog_format_body (ADW_ALERT_DIALOG (dialog), "%s", g_strerror (errno)); |
| 122 | } |
| 123 | |
| 124 | adw_alert_dialog_add_response (ADW_ALERT_DIALOG (dialog), "close", _("_Close")); |
| 125 | |
| 126 | /* Show the dialog */ |
| 127 | adw_dialog_present (dialog, GTK_WIDGET (GsmApplication::get ().main_window)); |
| 128 | } |
| 129 | |
| 130 | |
| 131 | static void |
| 132 | did_affinity (GObject *source, GAsyncResult *result, gpointer user_data) |
| 133 | { |
| 134 | g_autoptr (GError) error = NULL; |
| 135 | g_autoptr (GtkWidget) dialog = GTK_WIDGET (user_data); |
| 136 | |
| 137 | gsm_actions_set_affinity_finish (GSM_ACTIONS (source), result, &error); |
| 138 | |
| 139 | if (error) { |
| 140 | set_affinity_error (error); |
| 141 | } |
| 142 | |
| 143 | gtk_window_destroy (GTK_WINDOW (dialog)); |
| 144 | } |
| 145 | |
| 146 | |
| 147 | static void |
| 148 | set_affinity (GtkCheckButton*, |
| 149 | gpointer data) |
| 150 | { |
| 151 | g_autoptr (GError) error = NULL; |
| 152 | SetAffinityData *affinity = static_cast<SetAffinityData *>(data); |
| 153 | glibtop_proc_affinity get_affinity; |
| 154 | g_autoptr (GArray) cpuset = NULL; |
| 155 | g_autofree uint16_t *cpus = NULL; |
| 156 | gboolean all_threads; |
| 157 | g_autoptr (GsmActions) actions = |
| 158 | GSM_ACTIONS (g_object_new (GSM_TYPE_ACTIONS, NULL)); |
| 159 | |
| 160 | /* Check whether we can get process's current affinity */ |
| 161 | cpus = glibtop_get_proc_affinity (&get_affinity, affinity->pid); |
| 162 | if (cpus != NULL) { |
| 163 | /* If so, create array for CPU numbers */ |
| 164 | cpuset = g_array_new (FALSE, FALSE, sizeof (uint16_t)); |
| 165 | |
| 166 | /* Run through all CPUs set for this process */ |
| 167 | for (size_t i = 0; i < affinity->cpu_count; i++) { |
| 168 | /* Check if CPU check box button is active */ |
| 169 | if (adw_switch_row_get_active (ADW_SWITCH_ROW (affinity->buttons[i + 1]))) { |
| 170 | /* If so, get its CPU number as 16bit integer */ |
| 171 | uint16_t n = i; |
| 172 | |
| 173 | /* Add its CPU for process affinity */ |
| 174 | g_array_append_val (cpuset, n); |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | all_threads = adw_switch_row_get_active (ADW_SWITCH_ROW (affinity->all_threads_row)); |
| 179 | |
| 180 | gsm_actions_set_affinity (actions, |
| 181 | affinity->pid, |
| 182 | cpuset->len, |
| 183 | (uint16_t *) cpuset->data, |
| 184 | all_threads, |
| 185 | NULL, |
| 186 | did_affinity, |
| 187 | g_object_ref (affinity->dialog)); |
| 188 | } else { |
| 189 | /* If not, show error message dialog */ |
| 190 | set_affinity_error (NULL); |
| 191 | /* Destroy dialog window */ |
| 192 | gtk_window_destroy (GTK_WINDOW (affinity->dialog)); |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | |
| 197 | static void |
| 198 | create_single_set_affinity_dialog (GtkTreeModel *model, |
| 199 | GtkTreePath*, |
| 200 | GtkTreeIter *iter, |
| 201 | gpointer data) |
| 202 | { |
| 203 | GsmApplication *app = static_cast<GsmApplication *>(data); |
| 204 | |
| 205 | ProcInfo *info; |
| 206 | SetAffinityData *affinity_data; |
| 207 | AdwWindowTitle *window_title; |
| 208 | GtkWidget *cancel_button; |
| 209 | GtkWidget *apply_button; |
| 210 | GtkListBox *cpulist_box; |
| 211 | |
| 212 | guint16 *affinity_cpus; |
| 213 | guint16 affinity_cpu; |
| 214 | glibtop_proc_affinity affinity; |
| 215 | guint32 affinity_i; |
| 216 | gint button_n; |
| 217 | gchar *button_text; |
| 218 | |
| 219 | /* Get selected process information */ |
| 220 | gtk_tree_model_get (model, iter, COL_POINTER, &info, -1); |
| 221 | |
| 222 | /* Return void if process information comes back not true */ |
| 223 | if (!info) |
| 224 | return; |
| 225 | |
| 226 | /* Create affinity data object */ |
| 227 | affinity_data = new SetAffinityData (); |
| 228 | |
| 229 | /* Set initial check box array */ |
| 230 | affinity_data->buttons = g_new (GtkWidget *, app->config.num_cpus); |
| 231 | |
| 232 | GtkBuilder *builder = gtk_builder_new (); |
| 233 | GError *err = NULL; |
| 234 | |
| 235 | gtk_builder_add_from_resource (builder, "/org/gnome/gnome-system-monitor/data/setaffinity.ui", &err); |
| 236 | if (err != NULL) |
| 237 | g_error ("%s", err->message); |
| 238 | |
| 239 | affinity_data->dialog = GTK_WIDGET (gtk_builder_get_object (builder, "setaffinity_dialog")); |
| 240 | window_title = ADW_WINDOW_TITLE (gtk_builder_get_object (builder, "window_title")); |
| 241 | cpulist_box = GTK_LIST_BOX (gtk_builder_get_object (builder, "cpulist_box")); |
| 242 | affinity_data->buttons[0] = GTK_WIDGET (gtk_builder_get_object (builder, "allcpus_row")); |
| 243 | cancel_button = GTK_WIDGET (gtk_builder_get_object (builder, "cancel_button")); |
| 244 | apply_button = GTK_WIDGET (gtk_builder_get_object (builder, "apply_button")); |
| 245 | affinity_data->all_threads_row = GTK_WIDGET (gtk_builder_get_object (builder, "all_threads_row")); |
| 246 | |
| 247 | // Translators: process name and id |
| 248 | char *subtitle = g_strdup_printf (_("%s (PID %u)"), info->name.c_str (), info->pid); |
| 249 | adw_window_title_set_subtitle (window_title, subtitle); |
| 250 | g_free (subtitle); |
| 251 | |
| 252 | /* Add selected process pid to affinity data */ |
| 253 | affinity_data->pid = info->pid; |
| 254 | |
| 255 | /* Add CPU count to affinity data */ |
| 256 | affinity_data->cpu_count = app->config.num_cpus; |
| 257 | |
| 258 | /* Set default toggle signal block states */ |
| 259 | affinity_data->toggle_single_blocked = FALSE; |
| 260 | affinity_data->toggle_all_blocked = FALSE; |
| 261 | |
| 262 | /* Get process's current affinity */ |
| 263 | affinity_cpus = glibtop_get_proc_affinity (&affinity, info->pid); |
| 264 | |
| 265 | /* Set toggle all check box based on whether all CPUs are set for this process */ |
| 266 | adw_switch_row_set_active ( |
| 267 | ADW_SWITCH_ROW (affinity_data->buttons[0]), |
| 268 | affinity.all); |
| 269 | |
| 270 | /* Run through all CPU buttons */ |
| 271 | for (button_n = 1; button_n < app->config.num_cpus + 1; button_n++) |
| 272 | { |
| 273 | /* Set check box label value to CPU [1..2048] */ |
| 274 | button_text = g_strdup_printf (_("CPU%d"), button_n); |
| 275 | |
| 276 | /* Create check box button for current CPU */ |
| 277 | affinity_data->buttons[button_n] = adw_switch_row_new (); |
| 278 | adw_preferences_row_set_title (ADW_PREFERENCES_ROW (affinity_data->buttons[button_n]), button_text); |
| 279 | |
| 280 | /* Add check box to CPU list */ |
| 281 | gtk_list_box_insert (cpulist_box, affinity_data->buttons[button_n], button_n); |
| 282 | |
| 283 | /* Connect check box to toggler function */ |
| 284 | g_signal_connect (affinity_data->buttons[button_n], |
| 285 | "notify::active", |
| 286 | G_CALLBACK (affinity_toggler_single), |
| 287 | affinity_data); |
| 288 | |
| 289 | /* Free check box label value gchar */ |
| 290 | g_free (button_text); |
| 291 | } |
| 292 | |
| 293 | /* Run through all CPUs set for this process */ |
| 294 | for (affinity_i = 0; affinity_i < affinity.number; affinity_i++) |
| 295 | { |
| 296 | /* Get CPU button index */ |
| 297 | affinity_cpu = affinity_cpus[affinity_i] + 1; |
| 298 | |
| 299 | /* Set CPU check box active */ |
| 300 | adw_switch_row_set_active ( |
| 301 | ADW_SWITCH_ROW (affinity_data->buttons[affinity_cpu]), |
| 302 | TRUE); |
| 303 | } |
| 304 | |
| 305 | /* Swap click signal on "Cancel" button */ |
| 306 | g_signal_connect_swapped (cancel_button, |
| 307 | "clicked", |
| 308 | G_CALLBACK (gtk_window_destroy), |
| 309 | affinity_data->dialog); |
| 310 | |
| 311 | /* Connect click signal on "Apply" button */ |
| 312 | g_signal_connect (apply_button, |
| 313 | "clicked", |
| 314 | G_CALLBACK (set_affinity), |
| 315 | affinity_data); |
| 316 | |
| 317 | /* Connect toggle all check box to (de)select all function */ |
| 318 | g_signal_connect (affinity_data->buttons[0], |
| 319 | "notify::active", |
| 320 | G_CALLBACK (affinity_toggle_all), |
| 321 | affinity_data); |
| 322 | |
| 323 | /* Show dialog window */ |
| 324 | gtk_window_present (GTK_WINDOW (affinity_data->dialog)); |
| 325 | |
| 326 | g_object_unref (G_OBJECT (builder)); |
| 327 | |
| 328 | g_free (affinity_cpus); |
| 329 | } |
| 330 | |
| 331 | void |
| 332 | create_set_affinity_dialog (GsmApplication *app) |
| 333 | { |
| 334 | /* Create a dialog window for each selected process */ |
| 335 | gtk_tree_selection_selected_foreach (app->selection, |
| 336 | create_single_set_affinity_dialog, |
| 337 | app); |
| 338 | } |