-
Notifications
You must be signed in to change notification settings - Fork 32
RE-Implemented NVIDIA Energy capture via C #1167
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
3b83ff3
RE-Implemented NVIDIA Energy capture via C
ArneTR 6a61ec7
Name change from NVIDIA SMI to NVML [skip ci]
ArneTR 98ef46a
Makefile cleanup
ArneTR dc25a09
Adding NVIDIA Headers download to install
ArneTR d10051d
Directory rename
ArneTR 565f414
Changed resolution to sampling_rate [skip ci]
ArneTR a3a4776
Merge branch 'main' into nvidia-energy-c [skip ci]
ArneTR d09f59b
Merge branch 'main' into nvidia-energy-c
ArneTR cd601fb
Fixing installer --nvidia-gpu optione
ArneTR 3d0a8c1
Installing libs for fedora for --nvidia-gpu
ArneTR 9792307
Including fedora libs. Should not harm Ubuntu target
ArneTR 7d9ac87
Nvidia lm was duplicate; nvmlShutdown added when checking card [skip ci]
ArneTR File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
CFLAGS = -O3 -Wall -Werror -lc -I../../../../../../lib/c -lnvidia-ml | ||
|
||
metric-provider-binary: source.c | ||
gcc ../../../../../../lib/c/gmt-lib.o $< $(CFLAGS) -o $@ |
19 changes: 0 additions & 19 deletions
19
metric_providers/gpu/energy/nvidia/smi/component/metric-provider-nvidia-smi-wrapper.sh
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
152 changes: 152 additions & 0 deletions
152
metric_providers/gpu/energy/nvidia/smi/component/source.c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <errno.h> | ||
#include <unistd.h> | ||
#include <sys/time.h> | ||
#include <time.h> | ||
#include <string.h> // for strtok | ||
#include <getopt.h> | ||
#include <limits.h> | ||
#include <stdbool.h> | ||
#include <nvml.h> | ||
#include "gmt-lib.h" | ||
|
||
|
||
// All variables are made static, because we believe that this will | ||
// keep them local in scope to the file and not make them persist in state | ||
// between Threads. | ||
// in any case, none of these variables should change between threads | ||
static unsigned int msleep_time=1000; | ||
static struct timespec offset; | ||
|
||
static void output_stats() { | ||
struct timeval now; | ||
nvmlReturn_t result; | ||
unsigned int device_count; | ||
nvmlDevice_t device; | ||
char name[NVML_DEVICE_NAME_BUFFER_SIZE]; | ||
// nvmlUtilization_t utilization; | ||
// nvmlMemory_t memory; | ||
unsigned int power_usage; | ||
// unsigned int power_limit; | ||
|
||
result = nvmlInit(); | ||
if (result != NVML_SUCCESS) { | ||
fprintf(stderr, "Failed to initialize NVML: %s\n", nvmlErrorString(result)); | ||
exit(1); | ||
} | ||
|
||
result = nvmlDeviceGetCount(&device_count); | ||
if (result != NVML_SUCCESS) { | ||
fprintf(stderr, "Failed to get device count: %s\n", nvmlErrorString(result)); | ||
nvmlShutdown(); | ||
exit(1); | ||
} | ||
|
||
while (1) { | ||
get_adjusted_time(&now, &offset); | ||
|
||
for (unsigned int i = 0; i < device_count; i++) { | ||
|
||
nvmlDeviceGetHandleByIndex(i, &device); | ||
nvmlDeviceGetName(device, name, sizeof(name)); | ||
ArneTR marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// printf("GPU %u: %s\n", i, name); | ||
|
||
// nvmlDeviceGetUtilizationRates(device, &utilization); | ||
// printf(" Utilization: %u%%\n", utilization.gpu); | ||
|
||
// nvmlDeviceGetMemoryInfo(device, &memory); | ||
// printf(" Memory: %llu MiB / %llu MiB\n", memory.used / 1024 / 1024, memory.total / 1024 / 1024); | ||
|
||
// nvmlDeviceGetEnforcedPowerLimit(device, &power_limit); // mW | ||
|
||
nvmlDeviceGetPowerUsage(device, &power_usage); // mW | ||
printf("%ld%06ld %u \"%s-%u\"\n", now.tv_sec, now.tv_usec, power_usage, name, i); | ||
|
||
} | ||
usleep(msleep_time*1000); | ||
} | ||
|
||
|
||
} | ||
|
||
|
||
static int check_system() { | ||
nvmlReturn_t result; | ||
nvmlDevice_t device; | ||
unsigned int power_usage; | ||
unsigned int device_count; | ||
|
||
result = nvmlInit(); | ||
if (result != NVML_SUCCESS) { | ||
fprintf(stderr, "Failed to initialize NVML: %s\n", nvmlErrorString(result)); | ||
return 1; | ||
} | ||
|
||
result = nvmlDeviceGetCount(&device_count); | ||
if (result != NVML_SUCCESS) { | ||
fprintf(stderr, "Failed to get device count: %s\n", nvmlErrorString(result)); | ||
nvmlShutdown(); | ||
exit(1); | ||
} | ||
|
||
if (device_count <= 0) { | ||
fprintf(stderr, "No NVIDIA cards found\n"); | ||
nvmlShutdown(); | ||
exit(1); | ||
|
||
} | ||
|
||
nvmlDeviceGetHandleByIndex(0, &device); | ||
nvmlDeviceGetPowerUsage(device, &power_usage); | ||
|
||
return 0; | ||
} | ||
|
||
int main(int argc, char **argv) { | ||
|
||
int c; | ||
bool check_system_flag = false; | ||
|
||
static struct option long_options[] = | ||
{ | ||
{"help", no_argument, NULL, 'h'}, | ||
{"interval", no_argument, NULL, 'i'}, | ||
ArneTR marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{"check", no_argument, NULL, 'c'}, | ||
{NULL, 0, NULL, 0} | ||
}; | ||
|
||
while ((c = getopt_long(argc, argv, "i:hc", long_options, NULL)) != -1) { | ||
switch (c) { | ||
case 'h': | ||
printf("Usage: %s [-i msleep_time] [-h]\n\n",argv[0]); | ||
printf("\t-h : displays this help\n"); | ||
printf("\t-i : specifies the milliseconds sleep time that will be slept between measurements\n"); | ||
printf("\t-c : check system and exit\n"); | ||
printf("\n"); | ||
|
||
exit(0); | ||
case 'i': | ||
msleep_time = parse_int(optarg); | ||
break; | ||
case 'c': | ||
check_system_flag = true; | ||
break; | ||
default: | ||
fprintf(stderr,"Unknown option %c\n",c); | ||
exit(-1); | ||
} | ||
} | ||
|
||
if(check_system_flag){ | ||
exit(check_system()); | ||
} | ||
|
||
get_time_offset(&offset); | ||
|
||
output_stats(); | ||
|
||
nvmlShutdown(); | ||
|
||
return 0; | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.