Skip to content

Commit

Permalink
Merge pull request #46 from gcorrall/rapl_wraparound
Browse files Browse the repository at this point in the history
Handle when the energy counter (energy_uj) wraps around (reaches max_…
  • Loading branch information
adelnoureddine authored Feb 23, 2024
2 parents f548421 + 563a8c9 commit 849fa01
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 6 deletions.
22 changes: 22 additions & 0 deletions src/intel_rapl_sysfs.adb
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,26 @@ package body Intel_RAPL_sysfs is
--Put_Line ("Error reading file " & Package_Name & " for Intel RAPL.");
end;

procedure Get_Max_Energy_Range (RAPL_Data : in out Intel_RAPL_Data; Package_Name : in String) is
F_Name : File_Type; -- File handle
Folder_Name : constant String := "/sys/class/powercap/intel-rapl/"; -- Folder prefix for file to read
begin
if (Package_Name = "psys") then
Open (F_Name, In_File, Folder_Name & "intel-rapl:1/max_energy_range_uj");
RAPL_Data.psys_max_energy_range := Long_Float'Value (Get_Line (F_Name)) / 1000000.0;
Close (F_Name);
elsif (Package_Name = "pkg") then
Open (F_Name, In_File, Folder_Name & "intel-rapl:0/max_energy_range_uj");
RAPL_Data.pkg_max_energy_range := Long_Float'Value (Get_Line (F_Name)) / 1000000.0;
Close (F_Name);
elsif (Package_Name = "dram") then
Open (F_Name, In_File, Folder_Name & "intel-rapl:0/intel-rapl:0:2/max_energy_range_uj");
RAPL_Data.dram_max_energy_range := Long_Float'Value (Get_Line (F_Name)) / 1000000.0;
Close (F_Name);
end if;
exception
when others =>
return;
end;

end Intel_RAPL_sysfs;
6 changes: 6 additions & 0 deletions src/intel_rapl_sysfs.ads
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ package Intel_RAPL_sysfs is
psys_supported : Boolean := False; -- if system supports psys
pkg_supported : Boolean := False; -- if system supports pkg 0
dram_supported : Boolean := False; -- if system support dram 0

-- Data to store max energy ranges
pkg_max_energy_range : Long_Float := 0.0; -- pkg max_energy_range_uj
psys_max_energy_range : Long_Float := 0.0; -- psys max_energy_range_uj
dram_max_energy_range : Long_Float := 0.0; -- dram max_energy_range_uj
end record;

-- Calculate total energy consumption from Linux powercap sysfs
Expand All @@ -38,4 +43,5 @@ package Intel_RAPL_sysfs is
-- So far, only package 0 is supported (and dram in package 0)
procedure Check_Supported_Packages (RAPL_Data : in out Intel_RAPL_Data; Package_Name : in String);

procedure Get_Max_Energy_Range (RAPL_Data : in out Intel_RAPL_Data; Package_Name : in String);
end Intel_RAPL_sysfs;
39 changes: 33 additions & 6 deletions src/powerjoular.adb
Original file line number Diff line number Diff line change
Expand Up @@ -167,18 +167,28 @@ begin
if Check_Intel_Supported_System (Platform_Name) then
-- For Intel RAPL, check and populate supported packages first
Check_Supported_Packages (RAPL_Before, "psys");
if RAPL_Before.psys_supported and Show_Debug then
Put_Line (Ada.Characters.Latin_1.HT & "Intel RAPL psys: " & Boolean'Image (RAPL_Before.Psys_Supported));

if RAPL_Before.psys_supported then
Get_Max_Energy_Range (RAPL_Before, "psys");
if Show_Debug then
Put_Line (Ada.Characters.Latin_1.HT & "Intel RAPL psys: " & Boolean'Image (RAPL_Before.Psys_Supported));
end if;
end if;

if (not RAPL_Before.psys_supported) then -- Only check for pkg and dram if psys is not supported
Check_Supported_Packages (RAPL_Before, "pkg");
Check_Supported_Packages (RAPL_Before, "dram");
if RAPL_Before.Pkg_Supported and Show_Debug then
Put_Line (Ada.Characters.Latin_1.HT & "Intel RAPL pkg: " & Boolean'Image (RAPL_Before.pkg_supported));
if RAPL_Before.Pkg_Supported then
Get_Max_Energy_Range (RAPL_Before, "pkg");
if Show_Debug then
Put_Line (Ada.Characters.Latin_1.HT & "Intel RAPL pkg: " & Boolean'Image (RAPL_Before.pkg_supported));
end if;
end if;
if RAPL_Before.Dram_Supported and Show_Debug then
Put_Line (Ada.Characters.Latin_1.HT & "Intel RAPL dram: " & Boolean'Image (RAPL_Before.Dram_Supported));
if RAPL_Before.Dram_Supported then
Get_Max_Energy_Range (RAPL_Before, "dram");
if Show_Debug then
Put_Line (Ada.Characters.Latin_1.HT & "Intel RAPL dram: " & Boolean'Image (RAPL_Before.Dram_Supported));
end if;
end if;
end if;
RAPL_After := RAPL_Before; -- Populate the "after" data type with same checking as the "before" (insteaf of wasting redundant calls to procedure)
Expand Down Expand Up @@ -257,6 +267,23 @@ begin
if Check_Intel_Supported_System (Platform_Name) then
-- Calculate Intel RAPL energy consumption
RAPL_Energy := RAPL_After.total_energy - RAPL_Before.total_energy;

if RAPL_Before.total_energy > RAPL_After.total_energy then
-- energy has wrapped
if RAPL_Before.psys_supported then
RAPL_Energy := RAPL_Energy + RAPL_Before.psys_max_energy_range;
elsif RAPL_Before.Pkg_Supported then
RAPL_Energy := RAPL_Energy + RAPL_Before.pkg_max_energy_range;
end if;
end if;

if RAPL_Before.Pkg_Supported and RAPL_Before.Dram_supported then
if RAPL_Before.dram > RAPL_After.dram then
-- dram has wrapped
RAPL_Energy := RAPL_Energy + RAPL_Before.dram_max_energy_range;
end if;
end if;

CPU_Power := RAPL_Energy;
Total_Power := CPU_Power;
end if;
Expand Down

0 comments on commit 849fa01

Please sign in to comment.