Skip to content

Commit

Permalink
Energy APIs: Print and JSON (#530)
Browse files Browse the repository at this point in the history
Expands support for variorum_print_energy other than AMD and adds two new APIs -- variorum_print_verbose_energy, variorum_get_energy_json on Intel and IBM platforms.
---------

Co-authored-by: Tapasya Patki <[email protected]>
Co-authored-by: Aniruddha Marathe <[email protected]>
Co-authored-by: Stephanie Brink <[email protected]>
  • Loading branch information
3 people authored Mar 25, 2024
1 parent c47e6af commit 790446f
Show file tree
Hide file tree
Showing 36 changed files with 1,244 additions and 20 deletions.
2 changes: 2 additions & 0 deletions host-configs/[email protected]
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,5 @@ set(VARIORUM_WITH_NVIDIA_GPU OFF CACHE BOOL "")
set(VARIORUM_DEBUG OFF CACHE BOOL "")

set(BUILD_TESTS ON CACHE BOOL "")

set(CMAKE_SHARED_LINKER_FLAGS "-lpthread" CACHE PATH "")
2 changes: 2 additions & 0 deletions src/examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ set(BASIC_EXAMPLES
variorum-cap-socket-power-limit-example
variorum-disable-turbo-example
variorum-enable-turbo-example
variorum-get-energy-json-example
variorum-get-frequency-json-example
variorum-get-node-power-domain-info-json-example
variorum-get-power-json-example
Expand All @@ -39,6 +40,7 @@ set(BASIC_EXAMPLES
variorum-print-topology-example
variorum-print-turbo-example
variorum-print-verbose-counters-example
variorum-print-verbose-energy-example
variorum-print-verbose-frequency-example
variorum-print-verbose-gpu-utilization-example
variorum-print-verbose-power-example
Expand Down
89 changes: 89 additions & 0 deletions src/examples/variorum-get-energy-json-example.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
// Copyright 2019-2023 Lawrence Livermore National Security, LLC and other
// Variorum Project Developers. See the top-level LICENSE file for details.
//
// SPDX-License-Identifier: MIT

#include <getopt.h>
#include <stdio.h>
#include <stdlib.h>

#include <variorum.h>
#include <variorum_topology.h>

#ifdef SECOND_RUN
static inline double do_work(int input)
{
int i;
double result = (double)input;

for (i = 0; i < 100000; i++)
{
result += i * result;
}

return result;
}
#endif

int main(int argc, char **argv)
{
int ret;
char *s = NULL;
#ifdef SECOND_RUN
int i;
int size = 1E4;
volatile double x = 0.0;
#endif

const char *usage = "Usage: %s [-h] [-v]\n";
int opt;
while ((opt = getopt(argc, argv, "hv")) != -1)
{
switch (opt)
{
case 'h':
printf(usage, argv[0]);
return 0;
case 'v':
printf("%s\n", variorum_get_current_version());
return 0;
default:
fprintf(stderr, usage, argv[0]);
return -1;
}
}

ret = variorum_get_energy_json(&s);
if (ret != 0)
{
printf("First run: JSON get energy failed!\n");
free(s);
exit(-1);
}

/* Print the entire JSON object */
puts(s);

#ifdef SECOND_RUN
for (i = 0; i < size; i++)
{
x += do_work(i);
}
printf("Final result: %f\n", x);
ret = variorum_get_energy_json(&s);
if (ret != 0)
{
printf("Second run: JSON get energy failed!\n");
free(s);
exit(-1);
}

/* Print the entire JSON object */
puts(s);
#endif

/* Deallocate the string */
free(s);

return ret;
}
16 changes: 14 additions & 2 deletions src/examples/variorum-print-energy-example.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#include <getopt.h>
#include <stdio.h>
#include <unistd.h>

#include <variorum.h>

Expand All @@ -30,10 +31,21 @@ int main(int argc, char **argv)
}
}

// First call to variorum_print_energy should print zero.
ret = variorum_print_energy();
if (ret != 0)
{
printf("Print core and Socket energy failed!\n");
printf("Print core and socket energy failed!\n");
return ret;
}

sleep(2);

// Second call to variorum_print_energy should print a valid value.
ret = variorum_print_energy();
if (ret != 0)
{
printf("Print core and socket energy failed!\n");
return ret;
}
return ret;
}
49 changes: 49 additions & 0 deletions src/examples/variorum-print-verbose-energy-example.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Copyright 2019-2023 Lawrence Livermore National Security, LLC and other
// Variorum Project Developers. See the top-level LICENSE file for details.
//
// SPDX-License-Identifier: MIT

#include <getopt.h>
#include <stdio.h>
#include <unistd.h>

#include <variorum.h>

int main(int argc, char **argv)
{
int ret;

const char *usage = "Usage: %s [-h] [-v]\n";
int opt;
while ((opt = getopt(argc, argv, "hv")) != -1)
{
switch (opt)
{
case 'h':
printf(usage, argv[0]);
return 0;
case 'v':
printf("%s\n", variorum_get_current_version());
return 0;
default:
fprintf(stderr, usage, argv[0]);
return -1;
}
}

ret = variorum_print_verbose_energy();
if (ret != 0)
{
printf("Print core and socket energy failed!\n");
return ret;
}

sleep(2);

ret = variorum_print_verbose_energy();
if (ret != 0)
{
printf("Print core and socket energy failed!\n");
return ret;
}
}
5 changes: 3 additions & 2 deletions src/variorum/AMD/epyc.c
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,8 @@ static struct EPYC_19h_offsets msrs =
.msr_pkg_energy_stat = 0xC001029B
};

int amd_cpu_epyc_print_energy()

int amd_cpu_epyc_print_energy(int long_ver)
{
char *val = getenv("VARIORUM_LOG");
if (val != NULL && atoi(val) == 1)
Expand All @@ -376,7 +377,7 @@ int amd_cpu_epyc_print_energy()
}

int ret;
if (!esmi_init())
if (!esmi_init() && long_ver == 0)
{
int i;
uint64_t energy;
Expand Down
Loading

0 comments on commit 790446f

Please sign in to comment.