From 64676909ec7b1c1004af50a7c9f4639513923308 Mon Sep 17 00:00:00 2001
From: Georges Da Costa <dacosta@irit.fr>
Date: Sun, 2 Jun 2024 11:57:45 +0200
Subject: [PATCH] Corrects the overflow bug of Intel RAPL

---
 src/rapl.c | 6 ++++--
 src/util.c | 6 ++++++
 src/util.h | 7 +------
 3 files changed, 11 insertions(+), 8 deletions(-)

diff --git a/src/rapl.c b/src/rapl.c
index a93147a..267d037 100644
--- a/src/rapl.c
+++ b/src/rapl.c
@@ -118,7 +118,7 @@ int add_rapl_source_from_str(IntelRapl *rapl, const char*name_base, const int i)
   append(tmp, i, MAX_HEADER); // tmp contains the name with its index. ex: dram0
 
   snprintf(buffer, BUFFER_SIZE, name_base, i);
-  strcat(buffer, "name");
+  strcat(buffer, "max_energy_range_uj");
   uint64_t modulo = strtoull(get_rapl_string(buffer), NULL, 10);
 
   snprintf(buffer, BUFFER_SIZE, name_base, i);
@@ -180,7 +180,9 @@ unsigned int get_rapl(uint64_t *results, void *ptr)
 
     
     for (unsigned int i = 0; i < state->nb; i++) {
-        results[i] = modulo_substraction(state->tmp_values[i], state->values[i]);
+        results[i] = modulo_substraction_bound(state->tmp_values[i],
+					       state->values[i],
+					       state->modulo[i]);
     }
 
     memcpy(state->values, state->tmp_values, sizeof(uint64_t)*state->nb);
diff --git a/src/util.c b/src/util.c
index 5e835a7..e5d05f1 100644
--- a/src/util.c
+++ b/src/util.c
@@ -26,3 +26,9 @@ uint64_t modulo_substraction(const uint64_t lhs, const uint64_t rhs)
     return lhs >= rhs ? (lhs - rhs)
            : (UINT64_MAX - rhs + 1) + lhs;
 }
+
+uint64_t modulo_substraction_bound(const uint64_t lhs, const uint64_t rhs, const uint64_t modulo)
+{
+    return lhs >= rhs ? (lhs - rhs)
+           : (modulo - rhs + 1) + lhs;
+}
diff --git a/src/util.h b/src/util.h
index 0015a98..5b0ed8c 100644
--- a/src/util.h
+++ b/src/util.h
@@ -23,12 +23,6 @@
 
 #include <stdint.h>
 
-#define CASSERT(predicate, file) _impl_CASSERT_LINE(predicate,__LINE__,file)
-
-#define _impl_PASTE(a,b) a##b
-#define _impl_CASSERT_LINE(predicate, line, file) \
-    typedef char _impl_PASTE(assertion_failed_##file##_,line)[2*!!(predicate)-1];
-
 #define UNUSED(expr) do { (void)(expr); } while (0)
 #define PANIC(code, fmt, ...)                \
     do {                                     \
@@ -49,5 +43,6 @@
  * @return uint64_t
  */
 uint64_t modulo_substraction(const uint64_t lhs, const uint64_t rhs);
+uint64_t modulo_substraction_bound(const uint64_t lhs, const uint64_t rhs, const uint64_t modulo);
 
 #endif
-- 
GitLab