-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathenergy.c
68 lines (59 loc) · 1.7 KB
/
energy.c
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
#include <stdio.h>
#include <raplcap.h>
int main()
{
raplcap handle;
// raplcap_zone zones;
int status;
uint32_t num_packages;
uint32_t num_die;
// Initialize the RAPL library
status = raplcap_init(&handle);
if (status != 0)
{
// An error has occured exit
fprintf(stderr, "Failed to initialize RAPL library");
// raplcap_destroy(&handle);
return 1;
}
// Get the number of available RAPL packages or processors
num_packages = raplcap_get_num_packages(&handle);
if (num_packages == 0)
{
// An error has occured
fprintf(stderr, "Failed to get the number of packages");
raplcap_destroy(&handle);
return 1;
}
// Get the number of die in a package
num_die = raplcap_get_num_die(&handle, 0);
if (num_die == 0)
{
// An error has occured
fprintf(stderr, "Failed to get the number of die");
raplcap_destroy(&handle);
return 1;
}
if (!raplcap_pd_is_zone_supported(&handle, 0, 0, RAPLCAP_ZONE_PACKAGE))
{
fprintf(stderr, "RAPL Package zone not supported\n");
raplcap_destroy(&handle);
return 1;
}
if (!raplcap_pd_is_zone_enabled(&handle, 0, 0, RAPLCAP_ZONE_PACKAGE))
{
fprintf(stderr, "RAPL Package zone not enabled\n");
raplcap_destroy(&handle);
return 1;
}
double energy_consumed = raplcap_pd_get_energy_counter(&handle, 0, 0, RAPLCAP_ZONE_PACKAGE);
if (energy_consumed < 0)
{
fprintf(stderr, "Failed to get energy consumption\n");
raplcap_destroy(&handle);
return 1;
}
printf("%f", energy_consumed);
raplcap_destroy(&handle);
return 0;
}