Skip to content
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

Timestamps with ms #75

Merged
merged 24 commits into from
Feb 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
acc85df
Add Citation file
adelnoureddine Feb 21, 2023
ec074f9
Add paper citation
adelnoureddine Feb 21, 2023
c596c30
Merge pull request #18 from joular/develop
adelnoureddine Mar 10, 2023
03a1c7f
Merge pull request #19 from joular/develop
adelnoureddine Mar 13, 2023
701e6d9
Merge pull request #20 from joular/develop
adelnoureddine Mar 15, 2023
bbc6b9c
Update README.md
adelnoureddine Apr 13, 2023
18acfa6
Merge pull request #22 from joular/develop
adelnoureddine Jun 18, 2023
e2b8a8e
Merge pull request #28 from joular/develop
adelnoureddine Sep 13, 2023
92d4a0e
Merge pull request #35 from joular/develop
adelnoureddine Oct 20, 2023
7b30c9e
Merge pull request #40 from joular/develop
adelnoureddine Jan 30, 2024
509d181
Merge pull request #44 from joular/develop
adelnoureddine Feb 20, 2024
2308f2e
Merge pull request #48 from joular/develop
adelnoureddine Apr 2, 2024
46db963
Merge pull request #49 from joular/develop
adelnoureddine Apr 2, 2024
634c5cf
Merge pull request #55 from joular/develop
adelnoureddine Jun 26, 2024
579fe89
Merge pull request #57 from joular/develop
adelnoureddine Jul 4, 2024
1cb2359
Merge pull request #59 from joular/develop
adelnoureddine Jul 8, 2024
9a5a006
Merge pull request #60 from joular/develop
adelnoureddine Jul 8, 2024
4a791cd
Merge pull request #62 from joular/develop
adelnoureddine Jul 11, 2024
cbd80a9
Merge pull request #70 from joular/develop
adelnoureddine Nov 19, 2024
b6ed98f
added timestamps with ms to the output csv
afonsoCarreira1 Dec 13, 2024
f8f52c5
Merge remote-tracking branch 'upstream/develop' into timestamps-with-ms
afonsoCarreira1 Jan 31, 2025
5d95052
updated pr to meet requirements
afonsoCarreira1 Jan 31, 2025
ad2dbd0
Styling and typos
adelnoureddine Feb 4, 2025
984478e
Styling and typos
adelnoureddine Feb 4, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ The following options are available:
- ```-m```: specify a filename for the power consumption of the virtual machine
- ```-s```: specify the format of the VM power, either ```powerjoular``` format (generated with the ```-o``` option: 3 columns csv file with the 3rd containing the power consumption the VM), or ```watts``` format (1 column containing just the power consumption of the VM)
- ```-k```: use TIDs to calculate PID stats instead of PID stat directly (Experimental feature)
- ```-c```: save timestamps in milliseconds (instead of just seconds) in the written CSV files

You can mix options, i.e., ```powerjoular -tp 144``` will monitor PID 144 and will print to the terminal.

Expand Down
64 changes: 60 additions & 4 deletions src/csv_power.adb
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,72 @@ with Ada.Calendar.Formatting; use Ada.Calendar.Formatting;
with Ada.Calendar.Time_Zones; use Ada.Calendar.Time_Zones;
with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;
with Ada.Characters.Latin_1; use Ada.Characters.Latin_1;
with Ada.Strings.Fixed; use Ada.Strings.Fixed;

package body CSV_Power is

procedure Save_To_CSV_File (Filename : String; Utilization : Long_Float; Total_Power : Long_Float; CPU_Power : Long_Float; GPU_Power : Long_Float; Overwrite_Data : Boolean) is
procedure Get_Timestamp (F : in File_Type; Save_Ms : Boolean) is
Current_Time : Time := Clock;
Year : Year_Number;
Month : Month_Number;
Day : Day_Number;
Seconds : Duration;
Hours, Minutes, Secs, Msecs : Integer;
Total_Seconds : Integer;
Now : Time := Clock; -- Current UTC time

begin
if not Save_Ms then
Put (F, Image (Date => Now, Time_Zone => UTC_Time_Offset) & ","); -- Get time based on current timezone
return;
end if;

Split(Current_Time, Year, Month, Day, Seconds);

Total_Seconds := Integer(Seconds);
Hours := Total_Seconds / 3600;
Minutes := (Total_Seconds mod 3600) / 60;
Secs := Total_Seconds mod 60;
Msecs := Integer((Seconds - Duration(Total_Seconds)) * 1000.0);

if Msecs < 0 then
Msecs := 1000 + Msecs;
Secs := Secs -1;
end if;

if Secs < 0 then
Secs := 59;
Minutes := Minutes - 1;

if Minutes < 0 then
Minutes := 59;
Hours := Hours - 1;

if Hours < 0 then
Hours := 23;
end if;
end if;
end if;

Put(F,Trim(Year'Image & "-" , Ada.Strings.Left) &
Trim(Month'Image & "-" , Ada.Strings.Left) &
Trim(Day'Image & " " , Ada.Strings.Left) &
Trim(Hours'Image & ":" , Ada.Strings.Left) &
Trim(Minutes'Image & ":" , Ada.Strings.Left) &
Trim(Secs'Image & "." , Ada.Strings.Left) &
Trim(Msecs'Image & "," , Ada.Strings.Left));
end Get_Timestamp;


procedure Save_To_CSV_File (Filename : String; Utilization : Long_Float; Total_Power : Long_Float; CPU_Power : Long_Float; GPU_Power : Long_Float; Overwrite_Data : Boolean; Save_Ms : Boolean) is
F : File_Type; -- File handle
Now : Time := Clock; -- Current UTC time

-- Procedure to save data to file
procedure Save_Data (F : File_Type) is
begin
Put (F, Image (Date => Now, Time_Zone => UTC_Time_Offset) & ","); -- Get time based on current timezone with UTC offset
-- Put (F, Image (Date => Now, Time_Zone => UTC_Time_Offset) & ","); -- Get time based on current timezone with UTC offset
Get_Timestamp(F, Save_Ms);
Put (F, Utilization, Exp => 0, Fore => 0); -- Exp = 0 to not show in scientific notation. Fore = 0 to show all digits
Put (F, ",");
Put (F, Total_Power, Exp => 0, Fore => 0);
Expand Down Expand Up @@ -57,14 +112,15 @@ package body CSV_Power is
raise PROGRAM_ERROR with "Error in accessing or creating the CSV file";
end;

procedure Save_PID_To_CSV_File (Filename : String; Utilization : Long_Float; Power : Long_Float; Overwrite_Data : Boolean) is
procedure Save_PID_To_CSV_File (Filename : String; Utilization : Long_Float; Power : Long_Float; Overwrite_Data : Boolean; Save_Ms : Boolean) is
F : File_Type; -- File handle
Now : Time := Clock; -- Current UTC time

-- Procedure to save data to file
procedure Save_Data (F : File_Type) is
begin
Put (F, Image (Date => Now, Time_Zone => UTC_Time_Offset) & ","); -- Get time based on current timezone with UTC offset
-- Put (F, Image (Date => Now, Time_Zone => UTC_Time_Offset) & ","); -- Get time based on current timezone with UTC offset
Get_Timestamp(F, Save_Ms);
Put (F, Utilization, Exp => 0, Fore => 0); -- Exp = 0 to not show in scientific notation. Fore = 0 to show all digits
Put (F, ",");
Put (F, Power, Exp => 0, Fore => 0);
Expand Down
4 changes: 2 additions & 2 deletions src/csv_power.ads
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
package CSV_Power is

-- Save CPU utilization and power conusmption to CSV file
procedure Save_To_CSV_File (Filename : String; Utilization : Long_Float; Total_Power : Long_Float; CPU_Power : Long_Float; GPU_Power : Long_Float; Overwrite_Data : Boolean);
procedure Save_To_CSV_File (Filename : String; Utilization : Long_Float; Total_Power : Long_Float; CPU_Power : Long_Float; GPU_Power : Long_Float; Overwrite_Data : Boolean; Save_Ms : Boolean);

-- Save PID's CPU utilization and power conusmption to CSV file
procedure Save_PID_To_CSV_File (Filename : String; Utilization : Long_Float; Power : Long_Float; Overwrite_Data : Boolean);
procedure Save_PID_To_CSV_File (Filename : String; Utilization : Long_Float; Power : Long_Float; Overwrite_Data : Boolean; Save_Ms : Boolean);

-- Print CPU utilization, CPU, GPU and total power conusmption on the terminal
procedure Show_On_Terminal (Utilization : Long_Float; Power : Long_Float; Previous_Power : Long_Float; CPU_Power : Long_Float; GPU_Power : Long_Float; GPU_Supported : Boolean);
Expand Down
1 change: 1 addition & 0 deletions src/help_info.adb
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ package body Help_Info is
Put_Line (HT & "-s: specify the format of the VM power, either powerjoular format (generated with the -o option: 3 columns csv file with the 3rd containing the power consumption the VM), or watts format (1 column containing just the power consumption of the VM)");
Put_Line ("You can mix options, i.e., powerjoular -tp 144 --> monitor PID 144 and will print to the terminal");
Put_Line (HT & "-k: use TIDs to calculate PID stats instead of PID stat directly (Experimental feature)");
Put_Line (HT & "-c: save timestamps in milliseconds (instead of just seconds) in the written CSV files");
Put_Line ("--------------------------");
Put_Line (ESC & "[93m" & "Daemons/Systemd service:" & ESC & "[0m");
Put_Line ("When installing the tool, a systemd service can also be installed. The service runs PowerJoular using the -o option and saves power data to /tmp/powerjoular-service.csv");
Expand Down
11 changes: 7 additions & 4 deletions src/powerjoular.adb
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ procedure Powerjoular is
Monitor_App : Boolean := False; -- Monitor a specific application by its name
Overwrite_Data : Boolean := false; -- Overwrite data instead of append on file
TID_PID : Boolean := false; -- Use TIDs to calculate PID stats instead of PID directly (Experimental feature)
Save_Ms : Boolean := false; -- Save timestamps with milliseconds in CSV

-- Procedure to capture Ctrl+C to show total energy on exit
procedure CtrlCHandler is
Expand All @@ -117,7 +118,7 @@ procedure Powerjoular is
begin
-- Loop over command line options
loop
case Getopt ("h v t d f: p: a: o: u l m: s: k") is
case Getopt ("h v t d f: p: a: o: u l m: s: k c") is
when 'h' => -- Show help
Show_Help;
OS_Exit (0);
Expand Down Expand Up @@ -153,6 +154,8 @@ procedure Powerjoular is
Monitor_VM := True;
when 'k' => -- Use TIDs to calculate PID stats instead of PID stat directly (Experimental feature)
TID_PID := True;
when 'c' => -- Save timestamps with milliseconds in CSV
Save_Ms := True;
when others =>
exit;
end case;
Expand Down Expand Up @@ -356,7 +359,7 @@ begin

-- Save CPU power data to CSV file of monitored PID
if Print_File then
Save_PID_To_CSV_File (To_String (PID_Or_App_CSV_Filename), PID_CPU_Utilization, PID_CPU_Power, Overwrite_Data);
Save_PID_To_CSV_File (To_String (PID_Or_App_CSV_Filename), PID_CPU_Utilization, PID_CPU_Power, Overwrite_Data, Save_Ms);
end if;
end if;

Expand All @@ -373,7 +376,7 @@ begin

-- Save CPU power data to CSV file of monitored PID
if Print_File then
Save_PID_To_CSV_File (To_String (PID_Or_App_CSV_Filename), App_CPU_Utilization, App_CPU_Power, Overwrite_Data);
Save_PID_To_CSV_File (To_String (PID_Or_App_CSV_Filename), App_CPU_Utilization, App_CPU_Power, Overwrite_Data, Save_Ms);
end if;
end if;

Expand All @@ -394,7 +397,7 @@ begin

-- Save total power data to CSV file
if Print_File then
Save_To_CSV_File (To_String (CSV_Filename), CPU_Utilization, Total_Power, CPU_Power, GPU_Power, Overwrite_Data);
Save_To_CSV_File (To_String (CSV_Filename), CPU_Utilization, Total_Power, CPU_Power, GPU_Power, Overwrite_Data, Save_Ms);
end if;
end loop;
end Powerjoular;