Skip to content

Commit 40bb10b

Browse files
authored
Merge pull request #454 from yinghao-w/fix-no-processes-snapshot-loop
Fix: make --no-processes work with --snapshot and --loop
2 parents 53811bb + ec0c7b6 commit 40bb10b

3 files changed

Lines changed: 116 additions & 111 deletions

File tree

include/nvtop/interface.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ int interface_update_interval(const struct nvtop_interface *interface);
5555

5656
bool show_information_messages(unsigned num_messages, const char **messages);
5757

58-
void print_snapshot(struct list_head *devices, bool use_fahrenheit_option);
58+
void print_snapshot(struct list_head *devices, bool use_fahrenheit_option, bool hide_processes_option);
5959

6060
void apply_plot_colors(const unsigned char plot_color_idx[MAX_LINES_PER_PLOT]);
6161

src/interface.c

Lines changed: 114 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -2138,7 +2138,7 @@ bool show_information_messages(unsigned num_messages, const char **messages) {
21382138
return dontShowAgainOption;
21392139
}
21402140

2141-
void print_snapshot(struct list_head *devices, bool use_fahrenheit_option) {
2141+
void print_snapshot(struct list_head *devices, bool use_fahrenheit_option, bool hide_processes_option) {
21422142
struct gpu_info *device;
21432143

21442144
printf("[\n");
@@ -2254,129 +2254,134 @@ void print_snapshot(struct list_head *devices, bool use_fahrenheit_option) {
22542254
printf("%s\"%s\": null,\n", indent_level_four, mem_used_field);
22552255
// Memory Available
22562256
if (GPUINFO_DYNAMIC_FIELD_VALID(&device->dynamic_info, free_memory))
2257-
printf("%s\"%s\": \"%llu\",\n", indent_level_four, mem_free_field, device->dynamic_info.free_memory);
2257+
printf("%s\"%s\": \"%llu\"", indent_level_four, mem_free_field, device->dynamic_info.free_memory);
22582258
else
2259-
printf("%s\"%s\": null,\n", indent_level_four, mem_free_field);
2259+
printf("%s\"%s\": null", indent_level_four, mem_free_field);
22602260

22612261
// Processes
2262-
printf("%s\"processes\" : [\n", indent_level_four);
2263-
for (unsigned i = 0; i < device->processes_count; ++i) {
2264-
struct gpu_process *proc = &device->processes[i];
2265-
printf("%s{\n", indent_level_six);
2266-
2267-
// PID
2268-
printf("%s\"pid\": \"%d\",\n", indent_level_eight, proc->pid);
2269-
2270-
if (GPUINFO_PROCESS_FIELD_VALID(proc, cmdline) && proc->cmdline) {
2271-
printf("%s\"cmdline\": \"", indent_level_eight);
2272-
for (char *li = proc->cmdline; *li != '\0'; li++) {
2273-
// We need to escape some characters for for json strings
2274-
if (*li == '\n') {
2275-
printf("\\n");
2276-
continue;
2277-
} else if (*li == '\b') {
2278-
printf("\\b");
2279-
continue;
2280-
} else if (*li == '\f') {
2281-
printf("\\f");
2282-
continue;
2283-
} else if (*li == '\r') {
2284-
printf("\\r");
2285-
continue;
2286-
} else if (*li == '\t') {
2287-
printf("\\t");
2288-
continue;
2262+
if (hide_processes_option) {
2263+
// (Notice: no comma at the end as it's the last field here)
2264+
printf("\n");
2265+
} else {
2266+
printf(",\n%s\"processes\" : [\n", indent_level_four);
2267+
for (unsigned i = 0; i < device->processes_count; ++i) {
2268+
struct gpu_process *proc = &device->processes[i];
2269+
printf("%s{\n", indent_level_six);
2270+
2271+
// PID
2272+
printf("%s\"pid\": \"%d\",\n", indent_level_eight, proc->pid);
2273+
2274+
if (GPUINFO_PROCESS_FIELD_VALID(proc, cmdline) && proc->cmdline) {
2275+
printf("%s\"cmdline\": \"", indent_level_eight);
2276+
for (char *li = proc->cmdline; *li != '\0'; li++) {
2277+
// We need to escape some characters for for json strings
2278+
if (*li == '\n') {
2279+
printf("\\n");
2280+
continue;
2281+
} else if (*li == '\b') {
2282+
printf("\\b");
2283+
continue;
2284+
} else if (*li == '\f') {
2285+
printf("\\f");
2286+
continue;
2287+
} else if (*li == '\r') {
2288+
printf("\\r");
2289+
continue;
2290+
} else if (*li == '\t') {
2291+
printf("\\t");
2292+
continue;
2293+
}
2294+
// escaping backslash and quotes
2295+
if (*li == '\\' || *li == '"')
2296+
printf("\\");
2297+
printf("%c", *li);
22892298
}
2290-
// escaping backslash and quotes
2291-
if (*li == '\\' || *li == '"')
2292-
printf("\\");
2293-
printf("%c", *li);
2299+
printf("\",\n");
2300+
} else {
2301+
printf("%s\"cmdline\": null,\n", indent_level_eight);
22942302
}
2295-
printf("\",\n");
2296-
} else {
2297-
printf("%s\"cmdline\": null,\n", indent_level_eight);
2298-
}
22992303

2300-
printf("%s\"kind\": ", indent_level_eight);
2301-
if (proc->type != gpu_process_unknown) {
2302-
printf("\"");
2303-
switch (proc->type) {
2304-
case gpu_process_graphical:
2305-
printf("graphic");
2306-
break;
2307-
case gpu_process_compute:
2308-
printf("compute");
2309-
break;
2310-
case gpu_process_graphical_compute:
2311-
printf("graphic & compute");
2312-
break;
2313-
default:
2314-
printf("N/A");
2315-
break;
2304+
printf("%s\"kind\": ", indent_level_eight);
2305+
if (proc->type != gpu_process_unknown) {
2306+
printf("\"");
2307+
switch (proc->type) {
2308+
case gpu_process_graphical:
2309+
printf("graphic");
2310+
break;
2311+
case gpu_process_compute:
2312+
printf("compute");
2313+
break;
2314+
case gpu_process_graphical_compute:
2315+
printf("graphic & compute");
2316+
break;
2317+
default:
2318+
printf("N/A");
2319+
break;
2320+
}
2321+
printf("\"");
2322+
} else {
2323+
printf("null");
23162324
}
2317-
printf("\"");
2318-
} else {
2319-
printf("null");
2320-
}
2321-
printf(",\n");
2322-
2323-
// GPU memory usage
2324-
printf("%s\"user\": ", indent_level_eight);
2325-
if (GPUINFO_PROCESS_FIELD_VALID(proc, user_name))
2326-
printf("\"%s\",\n", proc->user_name);
2327-
else
2328-
printf("null,\n");
2329-
2330-
// GPU usage
2331-
printf("%s\"gpu_usage\": ", indent_level_eight);
2332-
if (GPUINFO_PROCESS_FIELD_VALID(proc, gpu_usage))
2333-
printf("\"%u%%\",\n", proc->gpu_usage);
2334-
else
2335-
printf("null,\n");
2325+
printf(",\n");
23362326

2337-
// GPU memory usage
2338-
printf("%s\"gpu_mem_bytes_alloc\": ", indent_level_eight);
2339-
if (GPUINFO_PROCESS_FIELD_VALID(proc, gpu_memory_usage))
2340-
printf("\"%llu\",\n", proc->gpu_memory_usage);
2341-
else
2342-
printf("null,\n");
2343-
2344-
// GPU memory usage
2345-
printf("%s\"gpu_mem_usage\": ", indent_level_eight);
2346-
if (GPUINFO_PROCESS_FIELD_VALID(proc, gpu_memory_percentage))
2347-
printf("\"%u%%\",\n", proc->gpu_memory_percentage);
2348-
else
2349-
printf("null,\n");
2327+
// GPU memory usage
2328+
printf("%s\"user\": ", indent_level_eight);
2329+
if (GPUINFO_PROCESS_FIELD_VALID(proc, user_name))
2330+
printf("\"%s\",\n", proc->user_name);
2331+
else
2332+
printf("null,\n");
23502333

2351-
// Encode usage
2352-
if (device->static_info.encode_decode_shared) {
2353-
// (Notice: no comma at the end as it's the last field here)
2354-
printf("%s\"encode_decode\": ", indent_level_eight);
2355-
if (GPUINFO_PROCESS_FIELD_VALID(proc, decode_usage))
2356-
printf("\"%u%%\"\n", proc->decode_usage);
2334+
// GPU usage
2335+
printf("%s\"gpu_usage\": ", indent_level_eight);
2336+
if (GPUINFO_PROCESS_FIELD_VALID(proc, gpu_usage))
2337+
printf("\"%u%%\",\n", proc->gpu_usage);
23572338
else
2358-
printf("null\n");
2359-
} else {
2360-
printf("%s\"encode\": ", indent_level_eight);
2361-
if (GPUINFO_PROCESS_FIELD_VALID(proc, encode_usage))
2362-
printf("\"%u%%\",\n", proc->encode_usage);
2339+
printf("null,\n");
2340+
2341+
// GPU memory usage
2342+
printf("%s\"gpu_mem_bytes_alloc\": ", indent_level_eight);
2343+
if (GPUINFO_PROCESS_FIELD_VALID(proc, gpu_memory_usage))
2344+
printf("\"%llu\",\n", proc->gpu_memory_usage);
23632345
else
23642346
printf("null,\n");
2365-
// (Notice: no comma at the end as it's the last field here)
2366-
printf("%s\"decode\": ", indent_level_eight);
2367-
if (GPUINFO_PROCESS_FIELD_VALID(proc, decode_usage))
2368-
printf("\"%u%%\"\n", proc->decode_usage);
2347+
2348+
// GPU memory usage
2349+
printf("%s\"gpu_mem_usage\": ", indent_level_eight);
2350+
if (GPUINFO_PROCESS_FIELD_VALID(proc, gpu_memory_percentage))
2351+
printf("\"%u%%\",\n", proc->gpu_memory_percentage);
23692352
else
2370-
printf("null\n");
2371-
}
2353+
printf("null,\n");
23722354

2373-
printf("%s}", indent_level_six);
2374-
if (i != device->processes_count - 1)
2375-
printf(",");
2376-
printf("\n");
2355+
// Encode usage
2356+
if (device->static_info.encode_decode_shared) {
2357+
// (Notice: no comma at the end as it's the last field here)
2358+
printf("%s\"encode_decode\": ", indent_level_eight);
2359+
if (GPUINFO_PROCESS_FIELD_VALID(proc, decode_usage))
2360+
printf("\"%u%%\"\n", proc->decode_usage);
2361+
else
2362+
printf("null\n");
2363+
} else {
2364+
printf("%s\"encode\": ", indent_level_eight);
2365+
if (GPUINFO_PROCESS_FIELD_VALID(proc, encode_usage))
2366+
printf("\"%u%%\",\n", proc->encode_usage);
2367+
else
2368+
printf("null,\n");
2369+
// (Notice: no comma at the end as it's the last field here)
2370+
printf("%s\"decode\": ", indent_level_eight);
2371+
if (GPUINFO_PROCESS_FIELD_VALID(proc, decode_usage))
2372+
printf("\"%u%%\"\n", proc->decode_usage);
2373+
else
2374+
printf("null\n");
2375+
}
2376+
2377+
printf("%s}", indent_level_six);
2378+
if (i != device->processes_count - 1)
2379+
printf(",");
2380+
printf("\n");
2381+
}
2382+
// (Notice: no comma at the end as it's the last field here)
2383+
printf("%s]\n", indent_level_four);
23772384
}
2378-
// (Notice: no comma at the end as it's the last field here)
2379-
printf("%s]\n", indent_level_four);
23802385

23812386
if (device->list.next == devices)
23822387
printf("%s}\n", indent_level_two);

src/nvtop.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ int main(int argc, char **argv) {
258258
gpuinfo_refresh_processes(&monitoredGpus);
259259
gpuinfo_utilisation_rate(&monitoredGpus);
260260
gpuinfo_fix_dynamic_info_from_process_info(&monitoredGpus);
261-
print_snapshot(&monitoredGpus, use_fahrenheit_option);
261+
print_snapshot(&monitoredGpus, use_fahrenheit_option, hide_processes_option);
262262
} while (loop_snapshot && !signal_exit);
263263

264264
gpuinfo_shutdown_info_extraction(&monitoredGpus);

0 commit comments

Comments
 (0)