summaryrefslogtreecommitdiffstats
path: root/src/interface_options.c
blob: c265d1dafe77af99450330edd652600e59ad2671 (plain)
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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
/*
 *
 * Copyright (C) 2021 Maxime Schmitt <maxime.schmitt91@gmail.com>
 *
 * This file is part of Nvtop.
 *
 * Nvtop is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Nvtop is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with nvtop.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#include "nvtop/interface_options.h"
#include "ini.h"
#include "nvtop/extract_processinfo_fdinfo.h"
#include "nvtop/interface_common.h"

#include <assert.h>
#include <errno.h>
#include <libgen.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>

char config_file_path[PATH_MAX];
const char config_file_location[] = "nvtop/interface.ini";
const char config_conf_path[] = ".config";

static const char *default_config_path(void) {
  char *xdg_config_dir = getenv("XDG_CONFIG_HOME");
  size_t conf_path_length = 0;
  if (!xdg_config_dir) {
    // XDG config dir not set, default to $HOME/.config
    xdg_config_dir = getenv("HOME");
    if (!xdg_config_dir)
            return NULL;
    conf_path_length = sizeof(config_conf_path);
  }
  size_t xdg_path_length = strlen(xdg_config_dir);
  if (xdg_path_length < PATH_MAX - conf_path_length - sizeof(config_file_location)) {
    strcpy(config_file_path, xdg_config_dir);
    config_file_path[xdg_path_length] = '/';
    if (conf_path_length) {
      strcpy(config_file_path + xdg_path_length + 1, config_conf_path);
      config_file_path[xdg_path_length + conf_path_length] = '/';
    }
    strcpy(config_file_path + xdg_path_length + 1 + conf_path_length, config_file_location);
    return config_file_path;
  } else {
    return NULL;
  }
}

unsigned interface_check_and_fix_monitored_gpus(unsigned num_devices, struct list_head *monitoredGpu,
                                                struct list_head *nonMonitoredGpu, nvtop_interface_option *options) {
  // The array in options->gpu_specifi_opts is kept in sync with the lists
  unsigned idx = 0;
  struct gpu_info *device, *list_tmp;
  list_for_each_entry_safe(device, list_tmp, monitoredGpu, list) {
    assert(idx <= num_devices);
    if (options->gpu_specific_opts[idx].doNotMonitor) {
      list_move_tail(&device->list, nonMonitoredGpu);
      nvtop_interface_gpu_opts saveInfo = options->gpu_specific_opts[idx];
      memmove(&options->gpu_specific_opts[idx], &options->gpu_specific_opts[idx + 1],
              (num_devices - idx - 1) * sizeof(*options->gpu_specific_opts));
      options->gpu_specific_opts[num_devices - 1] = saveInfo;
    } else {
      idx++;
    }
  }
  unsigned numMonitored = idx;
  list_for_each_entry_safe(device, list_tmp, nonMonitoredGpu, list) {
    assert(idx <= num_devices);
    if (!options->gpu_specific_opts[idx].doNotMonitor) {
      list_move_tail(&device->list, monitoredGpu);
      nvtop_interface_gpu_opts saveInfo = options->gpu_specific_opts[idx];
      for (unsigned nonMonPred = idx; nonMonPred > numMonitored; --nonMonPred) {
        options->gpu_specific_opts[nonMonPred] = options->gpu_specific_opts[nonMonPred - 1];
      }
      options->gpu_specific_opts[numMonitored] = saveInfo;
      numMonitored++;
    }
    idx++;
  }
  assert(idx == num_devices);
  // We keep at least one monitored gpu at all times
  if (num_devices > 0 && numMonitored == 0) {
    assert(list_empty(monitoredGpu));
    list_move(&list_first_entry(nonMonitoredGpu, struct gpu_info, list)->list, monitoredGpu);
    options->gpu_specific_opts[0].doNotMonitor = false;
    numMonitored++;
  }
  list_for_each_entry(device, monitoredGpu, list) { processinfo_enable_disable_callback_for(device, true); }
  list_for_each_entry(device, nonMonitoredGpu, list) { processinfo_enable_disable_callback_for(device, false); }
  return numMonitored;
}

void alloc_interface_options_internals(char *config_location, unsigned num_devices, struct list_head *devices,
                                       nvtop_interface_option *options) {
  options->gpu_specific_opts = calloc(num_devices, sizeof(*options->gpu_specific_opts));
  if (!options->gpu_specific_opts) {
    perror("Cannot allocate memory: ");
    exit(EXIT_FAILURE);
  }
  unsigned idx = 0;
  struct gpu_info *device;
  list_for_each_entry(device, devices, list) { options->gpu_specific_opts[idx++].linkedGpu = device; }
  options->plot_left_to_right = false;
  options->use_color = true;
  options->encode_decode_hiding_timer = 30.;
  options->temperature_in_fahrenheit = false;
  options->config_file_location = NULL;
  options->sort_processes_by = process_memory;
  options->sort_descending_order = true;
  options->update_interval = 1000;
  options->process_fields_displayed = 0;
  options->has_monitored_set_changed = false;
  options->show_startup_messages = true;
  options->filter_nvtop_pid = true;
  options->has_gpu_info_bar = false;
  if (config_location) {
    options->config_file_location = malloc(strlen(config_location) + 1);
    if (!options->config_file_location) {
      perror("Cannot allocate memory: ");
      exit(EXIT_FAILURE);
    }
    strcpy(options->config_file_location, config_location);
  } else {
    const char *default_path = default_config_path();
    if (default_path) {
      options->config_file_location = malloc(strlen(default_path) + 1);
      if (!options->config_file_location) {
	perror("Cannot allocate memory: ");
	exit(EXIT_FAILURE);
      }
      strcpy(options->config_file_location, default_path);
    }
  }
}

struct nvtop_option_ini_data {
  unsigned num_devices;
  unsigned selectedGpu;
  nvtop_interface_option *options;
};

static const char do_not_modify_notice[] = "; Please do not edit this file.\n"
                                           "; The file is automatically generated and modified by nvtop by pressing "
                                           "F12.\n"
                                           "; If you wish to modify an option, use nvtop's setup window (F2) and "
                                           "follow "
                                           "up by saving the preference (F12).\n";

static const char general_section[] = "GeneralOption";
static const char general_value_use_color[] = "UseColor";
static const char general_value_update_interval[] = "UpdateInterval";
static const char general_show_messages[] = "ShowInfoMessages";

static const char header_section[] = "HeaderOption";
static const char header_value_use_fahrenheit[] = "UseFahrenheit";
static const char header_value_encode_decode_timer[] = "EncodeHideTimer";
static const char header_value_gpu_info_bar[] = "GPUInfoBar";

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] = {
    "pId", "user", "gpuId", "type", "gpuRate", "encRate", "decRate", "memory", "cpuUsage", "cpuMem", "cmdline", "none"};
static const char process_value_sort_order[] =