Skip to content

Commit 365dbe2

Browse files
Merge pull request #92 from joular/84-use-logging-instead-of-prints
84 use logging instead of prints
2 parents c9358cb + eb6d3af commit 365dbe2

14 files changed

+368
-160
lines changed

src/cpu_cycles.adb

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,15 @@ with Ada.Text_IO; use Ada.Text_IO;
1313
with GNAT.String_Split; use GNAT;
1414
with GNAT.OS_Lib; use GNAT.OS_Lib;
1515

16+
with Logger; use Logger;
17+
1618
package body CPU_Cycles is
1719

1820
procedure Calculate_CPU_Cycles (CPU_Data : in out CPU_Cycles_Data) is
1921
F : File_Type; -- File handle
2022
File_Name : constant String := "/proc/stat"; -- Filename to read
2123
Subs : String_Split.Slice_Set; -- Used to slice the read data from stat file
22-
Seps : constant String := " "; -- Seperator (space) for slicing string
24+
Seps : constant String := " "; -- Separator (space) for slicing string
2325
begin
2426
Open (F, In_File, File_Name);
2527
String_Split.Create (S => Subs, -- Store sliced data in Subs
@@ -34,11 +36,13 @@ package body CPU_Cycles is
3436
CPU_Data.cnice := Long_Integer'Value (String_Split.Slice (Subs, 3)); -- Index 2 in file. Slice function starts index at 1, so it is 3
3537
CPU_Data.csystem := Long_Integer'Value (String_Split.Slice (Subs, 4)); -- Index 3 in file. Slice function starts index at 1, so it is 4
3638
CPU_Data.cidle := Long_Integer'Value (String_Split.Slice (Subs, 5)); -- Index 4 in file. Slice function starts index at 1, so it is 5
37-
CPU_Data.cbusy := CPU_Data.cuser + CPU_Data.cnice + CPU_Data.csystem; --- cbusy time
39+
40+
CPU_Data.cbusy := CPU_Data.cuser + CPU_Data.cnice + CPU_Data.csystem; -- cbusy time
3841
CPU_Data.ctotal := CPU_Data.cuser + CPU_Data.cnice + CPU_Data.csystem + CPU_Data.cidle; -- total time
42+
3943
exception
4044
when others =>
41-
Put_Line (Standard_Error, "Error reading " & File_Name & " file");
45+
Logger.Log(Error, "Error reading " & File_Name & " file");
4246
OS_Exit (0);
4347
end;
4448

src/cpu_stat_app.adb

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,21 @@ with GNAT.OS_Lib; use GNAT.OS_Lib;
1616
with GNAT.Expect; use GNAT.Expect;
1717
with GNAT.String_Split; use GNAT;
1818

19+
with Logger; use Logger;
20+
1921
package body CPU_STAT_App is
2022

2123
-- Calculate PID CPU time
2224
function Get_PID_Time (PID_Number : Integer) return Long_Integer is
2325
F : File_Type; -- File handle
2426
File_Name : constant String := "/proc/" & Trim(Integer'Image(PID_Number), Ada.Strings.Left) & "/stat"; -- File name /proc/pid/stat
2527
Subs : String_Split.Slice_Set; -- Used to slice the read data from stat file
26-
Seps : constant String := " "; -- Seperator (space) for slicing string
28+
Seps : constant String := " "; -- Separator (space) for slicing string
2729
Utime : Long_Integer; -- User time
2830
Stime : Long_Integer; -- System time
2931
begin
32+
Log(Info, "Reading CPU time for PID: " & Integer'Image(PID_Number));
33+
3034
Open (F, In_File, File_Name);
3135
String_Split.Create (S => Subs, -- Store sliced data in Subs
3236
From => Get_Line (F), -- Read data to slice. We only need the first line of the stat file
@@ -43,38 +47,54 @@ package body CPU_STAT_App is
4347
-- fscanf(fp, "%*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %lu %lu", &cpu_process_data->utime, &cpu_process_data->stime);
4448
Utime := Long_Integer'Value (String_Split.Slice (Subs, 14)); -- Index 13 in file. Slice function starts index at 1, so it is 14
4549
Stime := Long_Integer'Value (String_Split.Slice (Subs, 15)); -- Index 14 in file. Slice function starts index at 1, so it is 15
50+
51+
Log(Debug, "PID " & Integer'Image(PID_Number) &
52+
" has Utime=" & Long_Integer'Image(Utime) &
53+
" Stime=" & Long_Integer'Image(Stime) &
54+
" Total=" & Long_Integer'Image(Utime + Stime));
55+
4656
return Utime + Stime; -- Total time
4757
exception
4858
when others =>
59+
Log(Error, "Can't access stat file for PID: " & Integer'Image(PID_Number));
4960
return 0; -- Return 0 if PID doesn't exist or its file can't be accessed
5061
end;
5162

63+
-- Calculate total time of the application from all its PIDs
5264
procedure Calculate_App_Time (App_Data : in out CPU_STAT_App_Data; Is_Before : in Boolean) is
5365
Total_Time : Long_Integer := 0; -- Total time for app (all PIDs)
5466
PID_Number : Integer;
5567
begin
68+
Log(Info, "Calculating total CPU time for application: " & To_String(App_Data.App_Name));
69+
5670
for I in App_Data.PID_Array'Range loop
5771
PID_Number := App_Data.PID_Array (I);
5872
Total_Time := Total_Time + Get_PID_Time (PID_Number);
5973
end loop;
6074

6175
if (Is_Before) then
6276
App_Data.Before_Time := Total_Time; -- Total time
77+
Log(Info, "'Before' total time stored: " & Long_Integer'Image(App_Data.Before_Time));
6378
else
6479
App_Data.After_Time := Total_Time; -- Total time
6580
App_Data.Monitored_Time := App_Data.After_Time - App_Data.Before_Time;
81+
Log(Info, "'After' total time stored: " & Long_Integer'Image(App_Data.After_Time));
82+
Log(Info, "Monitored time difference: " & Long_Integer'Image(App_Data.Monitored_Time));
6683
end if;
6784
end;
6885

86+
-- Update the PID array of the application by calling 'pidof'
6987
procedure Update_PID_Array (App_Data : in out CPU_STAT_App_Data) is
7088
Command : String := "pidof " & To_String (App_Data.App_Name);
7189
Args : Argument_List_Access;
7290
Status : aliased Integer;
7391
Subs : String_Split.Slice_Set; -- Used to slice the read data from PID list
74-
Seps : constant String := " "; -- Seperator (space) for slicing string
92+
Seps : constant String := " "; -- Separator (space) for slicing string
7593
Slice_number_count : String_Split.Slice_Number;
7694
Loop_I : Integer;
7795
begin
96+
Log(Info, "Updating PID array for application: " & To_String(App_Data.App_Name));
97+
7898
Args := Argument_String_To_List (Command);
7999
declare
80100
Response : String :=
@@ -99,7 +119,7 @@ package body CPU_STAT_App is
99119
end;
100120
exception
101121
when others =>
102-
Put_Line (Standard_Error, "Can't find any PID of application: " & To_String (App_Data.App_Name));
122+
Log(Error, "Can't find any PID of application: " & To_String (App_Data.App_Name));
103123
OS_Exit (0);
104124
end;
105125

src/cpu_stat_pid.adb

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,21 @@ with GNAT.String_Split; use GNAT;
1414
with Ada.Strings.Fixed; use Ada.Strings.Fixed;
1515
with GNAT.OS_Lib; use GNAT.OS_Lib;
1616

17+
with Logger; use Logger;
18+
1719
package body CPU_STAT_PID is
1820

21+
-- Calculate CPU time for a single PID
1922
procedure Calculate_PID_Time (PID_Data : in out CPU_STAT_PID_Data; Is_Before : in Boolean) is
2023
F : File_Type; -- File handle
2124
File_Name : constant String := "/proc/" & Trim(Integer'Image(PID_Data.PID_Number), Ada.Strings.Left) & "/stat"; -- File name /proc/pid/stat
2225
Subs : String_Split.Slice_Set; -- Used to slice the read data from stat file
23-
Seps : constant String := " "; -- Seperator (space) for slicing string
26+
Seps : constant String := " "; -- Separator (space) for slicing string
2427
Utime : Long_Integer; -- User time
2528
Stime : Long_Integer; -- System time
2629
begin
30+
Logger.Log(Info, "Calculating CPU time for PID: " & Integer'Image(PID_Data.PID_Number));
31+
2732
Open (F, In_File, File_Name);
2833
String_Split.Create (S => Subs, -- Store sliced data in Subs
2934
From => Get_Line (F), -- Read data to slice. We only need the first line of the stat file
@@ -40,6 +45,7 @@ package body CPU_STAT_PID is
4045
-- fscanf(fp, "%*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %lu %lu", &cpu_process_data->utime, &cpu_process_data->stime);
4146
Utime := Long_Integer'Value (String_Split.Slice (Subs, 14)); -- Index 13 in file. Slice function starts index at 1, so it is 14
4247
Stime := Long_Integer'Value (String_Split.Slice (Subs, 15)); -- Index 14 in file. Slice function starts index at 1, so it is 15
48+
4349
if (Is_Before) then
4450
PID_Data.Before_Time := Utime + Stime; -- Total time
4551
else
@@ -48,7 +54,7 @@ package body CPU_STAT_PID is
4854
end if;
4955
exception
5056
when others =>
51-
Put_Line (Standard_Error, "Error reading " & File_Name & " file");
57+
Logger.Log(Error, "Error reading " & File_Name & " file");
5258
OS_Exit (0);
5359
end;
5460

src/cpu_stat_tid.adb

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ with GNAT.Expect; use GNAT.Expect;
1818
with Ada.Exceptions; use Ada.Exceptions;
1919

2020
with CPU_STAT_PID; use CPU_STAT_PID;
21+
with Logger; use Logger;
2122

2223
package body CPU_STAT_TID is
2324

@@ -31,6 +32,7 @@ package body CPU_STAT_TID is
3132
Stime : Long_Integer;
3233
Sum_Time : Long_Integer;
3334
begin
35+
Logger.Log(Debug, "Opening stat file for TID " & Integer'Image(TID) & " of PID " & Integer'Image(PID));
3436
Open (F, In_File, File_Name);
3537
String_Split.Create (
3638
S => Subs,
@@ -43,20 +45,25 @@ package body CPU_STAT_TID is
4345
Utime := Long_Integer'Value (String_Split.Slice (Subs, 14));
4446
Stime := Long_Integer'Value (String_Split.Slice (Subs, 15));
4547
Sum_Time := Utime + Stime;
48+
49+
Logger.Log(Debug, "TID " & Integer'Image(TID) & " of PID " & Integer'Image(PID) &
50+
" Utime=" & Long_Integer'Image(Utime) &
51+
" Stime=" & Long_Integer'Image(Stime) &
52+
" Sum=" & Long_Integer'Image(Sum_Time));
53+
4654
return Sum_Time;
4755
exception
4856
when NAME_ERROR | STATUS_ERROR =>
49-
Put_Line (Standard_Error, "Error opening or reading the file: " & File_Name);
57+
Logger.Log(Error, "Error opening or reading the file: " & File_Name);
5058
return 0;
5159
when DATA_ERROR | NUMERIC_ERROR =>
52-
Put_Line (Standard_Error, "Error converting data from the file: " & File_Name);
60+
Logger.Log(Error, "Error converting data from the file: " & File_Name);
5361
return 0;
5462
when others =>
55-
Put_Line (Standard_Error, "Unknown error processing the file: " & File_Name);
63+
Logger.Log(Error, "Unknown error processing the file: " & File_Name);
5664
return 0;
5765
end Get_TID_Time;
5866

59-
6067
-- Calculate PID Time using TID instead of PID directly
6168
procedure Calculate_PID_Time_TID (PID_Data : in out CPU_STAT_PID_Data; Is_Before : in Boolean) is
6269
Task_Directory : constant String := "/proc/" & Trim(Integer'Image(PID_Data.PID_Number), Ada.Strings.Left) & "/task";
@@ -73,6 +80,8 @@ package body CPU_STAT_TID is
7380
type TID_Array_Int is array (1..100) of Integer;
7481
TID_Array : TID_Array_Int; -- Array of all TIDs of the application
7582
begin
83+
Logger.Log(Info, "Calculating CPU time for PID: " & Integer'Image(PID_Data.PID_Number));
84+
7685
Args := Argument_String_To_List (Command);
7786
TID_Array := (others => -1);
7887
declare
@@ -86,7 +95,7 @@ package body CPU_STAT_TID is
8695
Free (Args);
8796
String_Split.Create (S => Subs, -- Store sliced data in Subs
8897
From => Response, -- Read data to slice
89-
Separators => Seps, -- Separator (here space)
98+
Separators => Seps, -- Separator (here newline)
9099
Mode => String_Split.Multiple);
91100
Slice_number_count := String_Split.Slice_Count (Subs);
92101

@@ -95,33 +104,38 @@ package body CPU_STAT_TID is
95104
TID_Array(Loop_I) := Integer'Value (String_Split.Slice (Subs, I));
96105
TID_Counter := TID_Counter + 1;
97106
end loop;
107+
Logger.Log(Debug, "Found " & Integer'Image(TID_Counter) & " TIDs for PID " & Integer'Image(PID_Data.PID_Number));
98108
end;
99109

100110
for I in 1 .. TID_Counter loop
101111
if TID_Array(I) /= -1 then
102112
TID_Number := TID_Array (I);
113+
Logger.Log(Debug, "Processing TID: " & Integer'Image(TID_Number));
103114
TID_Total_Time := TID_Total_Time + Get_TID_Time (PID_Data.PID_Number, TID_Number);
104115
end if;
105116
end loop;
117+
106118
if (Is_Before) then
107119
PID_Data.Before_Time := TID_Total_Time;
120+
Logger.Log(Info, "Stored 'Before' time: " & Long_Integer'Image(PID_Data.Before_Time));
108121
else
109122
PID_Data.After_Time := TID_Total_Time;
110123
PID_Data.Monitored_Time := PID_Data.After_Time - PID_Data.Before_Time;
124+
Logger.Log(Info, "Stored 'After' time: " & Long_Integer'Image(PID_Data.After_Time));
125+
Logger.Log(Info, "Monitored time difference: " & Long_Integer'Image(PID_Data.Monitored_Time));
111126
end if;
112127
exception
113128
when NAME_ERROR | STATUS_ERROR =>
114-
Put_Line (Standard_Error, "Error dealing with files in /proc/" & Trim(Integer'Image(PID_Data.PID_Number), Ada.Strings.Left) & "/task directory");
129+
Logger.Log(Error, "Error dealing with files in " & Task_Directory);
115130
OS_Exit (0);
116131
when DATA_ERROR =>
117-
Put_Line (Standard_Error, "Error related to data formatting or I/O");
132+
Logger.Log(Error, "Error related to data formatting or I/O");
118133
OS_Exit (0);
119134
when E : NUMERIC_ERROR =>
120-
Put_Line (Standard_Error, "Arithmetic error encountered");
121-
Put_Line (Exception_Message (E));
135+
Logger.Log(Error, "Arithmetic error encountered: " & Exception_Message (E));
122136
OS_Exit (0);
123137
when others =>
124-
Put_Line (Standard_Error, "Unknown error processing /proc/" & Trim(Integer'Image(PID_Data.PID_Number), Ada.Strings.Left) & "/task directory");
138+
Logger.Log(Error, "Unknown error processing " & Task_Directory);
125139
OS_Exit (0);
126140
end Calculate_PID_Time_TID;
127141

0 commit comments

Comments
 (0)