Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion include/nvtop/interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,6 @@ int interface_update_interval(const struct nvtop_interface *interface);

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

void print_snapshot(struct list_head *devices, bool use_fahrenheit_option);
void print_snapshot(struct list_head *devices, bool use_fahrenheit_option, int interval);

#endif // INTERFACE_H_
28 changes: 20 additions & 8 deletions src/interface.c
Original file line number Diff line number Diff line change
Expand Up @@ -2094,9 +2094,21 @@ bool show_information_messages(unsigned num_messages, const char **messages) {
return dontShowAgainOption;
}

void print_snapshot(struct list_head *devices, bool use_fahrenheit_option) {
void print_snapshot(struct list_head *devices, bool use_fahrenheit_option, int interval) {
gpuinfo_populate_static_infos(devices);
gpuinfo_refresh_dynamic_info(devices);
gpuinfo_refresh_processes(devices);
gpuinfo_utilisation_rate(devices);
gpuinfo_fix_dynamic_info_from_process_info(devices);

if (interval > 0) {
usleep(interval * 1000);
gpuinfo_refresh_dynamic_info(devices);
gpuinfo_refresh_processes(devices);
gpuinfo_utilisation_rate(devices);
gpuinfo_fix_dynamic_info_from_process_info(devices);
}

struct gpu_info *device;

printf("[\n");
Expand Down Expand Up @@ -2175,20 +2187,20 @@ void print_snapshot(struct list_head *devices, bool use_fahrenheit_option) {

// Memory Utilization
if (GPUINFO_DYNAMIC_FIELD_VALID(&device->dynamic_info, mem_util_rate))
printf("%s\"%s\": \"%u%%\"\n", indent_level_four, mem_util_field, device->dynamic_info.mem_util_rate);
printf("%s\"%s\": \"%u%%\",\n", indent_level_four, mem_util_field, device->dynamic_info.mem_util_rate);
else
printf("%s\"%s\": null\n", indent_level_four, mem_util_field);
printf("%s\"%s\": null,\n", indent_level_four, mem_util_field);
// Memory Total
if (GPUINFO_DYNAMIC_FIELD_VALID(&device->dynamic_info, total_memory))
printf("%s\"%s\": \"%llu\"\n", indent_level_four, mem_total_field, device->dynamic_info.total_memory);
printf("%s\"%s\": \"%llu\",\n", indent_level_four, mem_total_field, device->dynamic_info.total_memory);
else
printf("%s\"%s\": null\n", indent_level_four, mem_total_field);
printf("%s\"%s\": null,\n", indent_level_four, mem_total_field);
// Memory Used
if (GPUINFO_DYNAMIC_FIELD_VALID(&device->dynamic_info, used_memory))
printf("%s\"%s\": \"%llu\"\n", indent_level_four, mem_used_field, device->dynamic_info.used_memory);
printf("%s\"%s\": \"%llu\",\n", indent_level_four, mem_used_field, device->dynamic_info.used_memory);
else
printf("%s\"%s\": null\n", indent_level_four, mem_used_field);
// Memory Available
printf("%s\"%s\": null,\n", indent_level_four, mem_used_field);
// Memory Available (Notice: no comma at the end as it's the last field here)
if (GPUINFO_DYNAMIC_FIELD_VALID(&device->dynamic_info, free_memory))
printf("%s\"%s\": \"%llu\"\n", indent_level_four, mem_free_field, device->dynamic_info.free_memory);
else
Expand Down
27 changes: 24 additions & 3 deletions src/nvtop.c
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ static const char helpstring[] = "Available options:\n"
"(default 30s, negative = always on screen)\n"
" -h --help : Print help and exit\n"
" -s --snapshot : Output the current gpu stats without ncurses"
"(useful for scripting)\n";
"(useful for scripting)\n"
" -l --loop : Output the current gpu stats without ncurses in a loop\n";

static const char versionString[] = "nvtop version " NVTOP_VERSION_STRING;

Expand All @@ -92,10 +93,11 @@ static const struct option long_opts[] = {
{.name = "no-processes", .has_arg = no_argument, .flag = NULL, .val = 'P'},
{.name = "reverse-abs", .has_arg = no_argument, .flag = NULL, .val = 'r'},
{.name = "snapshot", .has_arg = no_argument, .flag = NULL, .val = 's'},
{.name = "loop", .has_arg = no_argument, .flag = NULL, .val = 'l'},
{0, 0, 0, 0},
};

static const char opts[] = "hvd:c:CfE:pPris";
static const char opts[] = "hvd:c:CfE:pPrisl";

int main(int argc, char **argv) {
(void)setlocale(LC_CTYPE, "");
Expand All @@ -111,6 +113,7 @@ int main(int argc, char **argv) {
bool encode_decode_timer_option_set = false;
bool show_gpu_info_bar = false;
bool show_snapshot = false;
bool loop_snapshot = false;
double encode_decode_hide_time = -1.;
char *custom_config_file_path = NULL;
while (true) {
Expand Down Expand Up @@ -174,6 +177,9 @@ int main(int argc, char **argv) {
case 's':
show_snapshot = true;
break;
case 'l':
loop_snapshot = true;
break;
case ':':
case '?':
switch (optopt) {
Expand Down Expand Up @@ -227,7 +233,22 @@ int main(int argc, char **argv) {
}

if (show_snapshot) {
print_snapshot(&monitoredGpus, use_fahrenheit_option);
int interval = 0;
if (update_interval_option_set)
interval = update_interval_option;
print_snapshot(&monitoredGpus, use_fahrenheit_option, interval);
gpuinfo_shutdown_info_extraction(&monitoredGpus);
return EXIT_SUCCESS;
}

if (loop_snapshot) {
int interval = 1000;
if (update_interval_option_set)
interval = update_interval_option;
while (!signal_exit) {
print_snapshot(&monitoredGpus, use_fahrenheit_option, 0);
usleep(interval * 1000);
}
gpuinfo_shutdown_info_extraction(&monitoredGpus);
return EXIT_SUCCESS;
}
Expand Down