@@ -52,7 +52,10 @@ def signal_handler(sig, frame):
5252
5353
5454def docker_start (time ):
55- os .system ("docker-compose up -d" )
55+ id1 = os .popen ("id -u" ).read ()
56+ id2 = os .popen ("id -g" ).read ()
57+ print ("UID=" + id1 [:- 1 ] + " GUID=" + id2 [:- 1 ] + " docker compose up -d" )
58+ os .system ("UID=" + id1 [:- 1 ] + " GUID=" + id2 [:- 1 ] + " docker compose up -d" )
5659 os .system ("docker compose logs sensor -f &" )
5760 os .system ("docker compose logs formula -f &" )
5861 os .system ("sleep " + str (time ))
@@ -62,7 +65,7 @@ def docker_start(time):
6265def docker_stop ():
6366 os .system ("set -ueo pipefail" )
6467 os .system ("set +x" )
65- os .system ("docker- compose down" )
68+ os .system ("docker compose down" )
6669
6770
6871def load_data ():
@@ -75,6 +78,82 @@ def load_data():
7578 return data
7679
7780
81+ def load_result (directory = './csv' ):
82+ """
83+ Load CSV files from the specified directory and return the data as a list of dictionaries.
84+ """
85+ data = []
86+ for root , _ , files in os .walk (directory ):
87+ for filename in files :
88+ if filename .endswith ('.csv' ):
89+ file_path = os .path .join (root , filename )
90+ with open (file_path , mode = 'r' , newline = '' , encoding = 'UTF-8' ) as f :
91+ data .extend (csv .DictReader (f ))
92+ return data
93+
94+
95+ def calculate_statistics (data , scope ):
96+ """
97+ Calculate average, maximum, and minimum consumption for the given scope (cpu or dram).
98+ """
99+ stats = {}
100+ for row in data :
101+ if row ['scope' ] == scope and row ['target' ] != 'rapl' :
102+ target = row ['target' ]
103+ consumption = float (row ['power' ])
104+ stats .setdefault (target , []).append (consumption )
105+
106+ return {
107+ target : {
108+ 'avg' : sum (consumptions ) / len (consumptions ),
109+ 'max' : max (consumptions ),
110+ 'min' : min (consumptions )
111+ } for target , consumptions in stats .items ()
112+ }
113+
114+
115+ def print_statistics (stats , title ):
116+ """
117+ Print statistics (average, max, min) for the given data in a formatted table.
118+ """
119+ if not stats :
120+ return
121+
122+ print (f"\n { title } \n " )
123+ print (f"{ 'Target' :<20} { 'Average consumption' :<20} { 'Maximum consumption' :<20} { 'Minimum consumption' :<20} " )
124+ print ("=" * 80 )
125+
126+ total = {'avg' : 0 , 'max' : 0 , 'min' : 0 }
127+ for target , values in stats .items ():
128+ if target != 'global' :
129+ print (f"{ target :<20} { values ['avg' ]:<20.2f} { values ['max' ]:<20.2f} { values ['min' ]:<20.2f} " )
130+ else :
131+ total = values
132+
133+ print ("-" * 80 )
134+ print (f"{ 'Global' :<20} { total ['avg' ]:<20.2f} { total ['max' ]:<20.2f} { total ['min' ]:<20.2f} " )
135+
136+
137+ def start_pretty_print ():
138+ """
139+ Pretty print the CPU and DRAM power consumption statistics from CSV files.
140+ """
141+ print ("The consumptions are given in Watt, note that the precision depends on the configuration file\n " )
142+
143+ data = load_result ()
144+
145+ # Calculate and print CPU statistics
146+ cpu_stats = calculate_statistics (data , 'cpu' )
147+ print_statistics (cpu_stats , "CPU Consumption Statistics :" )
148+
149+ # Calculate and print DRAM statistics
150+ dram_stats = calculate_statistics (data , 'dram' )
151+ print_statistics (dram_stats , "DRAM Consumption Statistics :" )
152+
153+ # Could add the GPU statistics here
154+
155+ print ("\n For more precise evaluation, consult the PowerAPI documentation to adjust configurations.\n " )
156+
78157def find_cpu (data ):
79158 """
80159 Find the cpu in the list of compatible cpu
@@ -211,7 +290,8 @@ def start_demo():
211290
212291 print ("\n Starting the demo..." )
213292 print ("-" * 80 )
214- print ("The demo will run for " + val + "\n " )
293+ print ("The demo will run for " + str (val ) + " seconds\n " )
294+ print ("If you wish to stop it, Ctrl-C will do so and stop the docker compose stack\n " )
215295
216296 docker_start (val )
217297
@@ -233,9 +313,9 @@ def start_demo():
233313 else :
234314 print ("\n The demo has ended, "
235315 "you can see the result under the /csv directory"
236- " or use 'python3 pretty_print.py' "
237- "to get a quick summary of the result in the terminal\n " )
316+ " here is a quick summary\n " )
238317
318+ start_pretty_print ()
239319
240320if __name__ == '__main__' :
241321 start_demo ()
0 commit comments