powerjoular_0.7.0_92d4a0eb/src/csv_power.adb

  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
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
--
--  Copyright (c) 2020-2023, Adel Noureddine, Université de Pau et des Pays de l'Adour.
--  All rights reserved. This program and the accompanying materials
--  are made available under the terms of the
--  GNU General Public License v3.0 only (GPL-3.0-only)
--  which accompanies this distribution, and is available at:
--  https://www.gnu.org/licenses/gpl-3.0.en.html
--
--  Author : Adel Noureddine
--

with Ada.Text_IO; use Ada.Text_IO;
with Ada.Calendar; use Ada.Calendar;
with Ada.Float_Text_IO; use Ada.Float_Text_IO;
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;

package body CSV_Power is

    procedure Save_To_CSV_File (Filename : String; Utilization : Float; Total_Power : Float; CPU_Power : Float; GPU_Power : Float; Overwrite_Data : 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, 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);
            Put (F, ",");
            Put (F, CPU_Power, Exp => 0, Fore => 0);
            Put (F, ",");
            Put (F, GPU_Power, Exp => 0, Fore => 0);
            New_Line (F);
        end Save_Data;
    begin
        if Overwrite_Data then
            -- Overwrite each line of data on the file
            Open (F, Out_File, Filename);
        else
            -- Append new data to the file
            Open (F, Append_File, Filename);
        end if;
        Save_Data (F);
        Close (F);
    exception
        when Name_Error =>
            -- If failed to open file (happens on first time if file doesn't exist), then create it
            Create (F, Out_File, Filename);
            Put_Line (F, "Date,CPU Utilization,Total Power,CPU Power,GPU Power");
            Save_Data (F);
            Close (F);
        when others =>
            raise PROGRAM_ERROR with "Error in accessing or creating the CSV file";
    end;

    procedure Save_PID_To_CSV_File (Filename : String; Utilization : Float; Power : Float; Overwrite_Data : 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, 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);
            New_Line (F);
        end Save_Data;
    begin
        if Overwrite_Data then
            -- Overwrite each line of data on the file
            Open (F, Out_File, Filename);
        else
            -- Append new data to the file
            Open (F, Append_File, Filename);
        end if;
        Save_Data (F);
        Close (F);
    exception
        when Name_Error =>
            -- If failed to open file (happens on first time if file doesn't exist), then create it
            Create (F, Out_File, Filename);
            Put_Line (F, "Date,CPU Utilization,CPU Power");
            Save_Data (F);
            Close (F);
        when others =>
            raise PROGRAM_ERROR with "Error in accessing or creating the CSV file";
    end;

    procedure Show_On_Terminal (Utilization : Float; Power : Float; Previous_Power : Float; CPU_Power : Float; GPU_Power : Float; GPU_Supported : Boolean) is
        Utilization_Percentage : Float;
        Power_Difference : Float;
    begin
        Utilization_Percentage := Utilization * 100.0;
        Put (CR);
        Put ("Total Power: ");
        Put (Power, Exp => 0, Fore => 0, Aft => 2);
        Put (" Watts ");
        Put ("(CPU: ");
        Put (CPU_Power, Exp => 0, Fore => 0, Aft => 2);
        Put (" W");

        if (GPU_Supported) then
            Put (", GPU: ");
            Put (GPU_Power, Exp => 0, Fore => 0, Aft => 2);
            Put (" W)" & HT);
        else
            Put (")" & HT);
        end if;

        Power_Difference := Power - Previous_Power;
        if (Power_Difference >= 0.0) then
            Put ("/\ ");
            Put (Power_Difference, Exp => 0, Fore => 0, Aft => 2);
            Put (" Watts");
        else
            Put ("\/ ");
            Put (Power_Difference, Exp => 0, Fore => 0, Aft => 2);
            Put (" Watts");
        end if;
    end;

    procedure Show_On_Terminal_PID (PID_Utilization : Float; PID_Power : Float; Utilization : Float; Power : Float; Is_PID : Boolean) is
        Utilization_Percentage : Float;
        PID_Utilization_Percentage : Float;
    begin
        Utilization_Percentage := Utilization * 100.0;
        PID_Utilization_Percentage := PID_Utilization * 100.0;
        Put (CR);
        if (Is_PID) then
            Put ("PID monitoring:" & HT & "CPU: ");
        else
            Put ("Application monitoring:" & HT & "CPU: ");
        end if;
        Put (PID_Utilization_Percentage, Exp => 0, Fore => 0, Aft => 2);
        Put (" % (");
        Put (Utilization_Percentage, Exp => 0, Fore => 0, Aft => 2);
        Put (" %)" & HT);
        Put (PID_Power, Exp => 0, Fore => 0, Aft => 2);
        Put (" Watts (");
        Put (Power, Exp => 0, Fore => 0, Aft => 2);
        Put (" Watts)");
    end;

end CSV_Power;