|
31 | 31 | import os |
32 | 32 |
|
33 | 33 |
|
34 | | -def start_pretty_print(): |
| 34 | +def load_data(directory='./csv'): |
35 | 35 | """ |
36 | | - Pretty print the result of the demo by parsing the csv files, |
37 | | - then proceed to calculate the average, maximum, and minimum consumption |
38 | | - and print them in a table format |
| 36 | + Load CSV files from the specified directory and return the data as a list of dictionaries. |
39 | 37 | """ |
40 | 38 | data = [] |
41 | | - result = [["Cgroup", |
42 | | - "Average consumption", |
43 | | - "Maximum consumption", |
44 | | - "Minimum consumption"]] |
45 | | - |
46 | | - print("\nThe consumptions are given in Watt, " |
47 | | - "note that the precision depend on the value given in the " |
48 | | - "configuration file (the base CPU frequency, the CPU TDP, ...) \n") |
49 | | - |
50 | | - # Get all the csv power report in the csv directory |
51 | | - for root, _, files in os.walk('./csv'): |
| 39 | + for root, _, files in os.walk(directory): |
52 | 40 | for filename in files: |
53 | 41 | if filename.endswith('.csv'): |
54 | 42 | file_path = os.path.join(root, filename) |
55 | | - |
56 | 43 | with open(file_path, mode='r', newline='', encoding='UTF-8') as f: |
57 | | - for row in csv.DictReader(f): |
58 | | - data.append(row) |
| 44 | + data.extend(csv.DictReader(f)) |
| 45 | + return data |
| 46 | + |
59 | 47 |
|
60 | | - cgroup_data = {} |
61 | | - total = [0, 0, 0] |
62 | | - # We remove rapl, but it's still available in the csv files |
| 48 | +def calculate_statistics(data, scope): |
| 49 | + """ |
| 50 | + Calculate average, maximum, and minimum consumption for the given scope (cpu or dram). |
| 51 | + """ |
| 52 | + stats = {} |
63 | 53 | for row in data: |
64 | | - target = row['target'] |
65 | | - consumption = float(row['power']) |
| 54 | + if row['scope'] == scope and row['target'] != 'rapl': |
| 55 | + target = row['target'] |
| 56 | + consumption = float(row['power']) |
| 57 | + stats.setdefault(target, []).append(consumption) |
66 | 58 |
|
67 | | - if target not in cgroup_data and not target == 'rapl': |
68 | | - cgroup_data[target] = [] |
| 59 | + return { |
| 60 | + target: { |
| 61 | + 'avg': sum(consumptions) / len(consumptions), |
| 62 | + 'max': max(consumptions), |
| 63 | + 'min': min(consumptions) |
| 64 | + } for target, consumptions in stats.items() |
| 65 | + } |
69 | 66 |
|
70 | | - if not target == 'rapl': |
71 | | - cgroup_data[target].append(consumption) |
72 | 67 |
|
73 | | - for target, consumptions in cgroup_data.items(): |
74 | | - avg_consumption = sum(consumptions) / len(consumptions) |
75 | | - max_consumption = max(consumptions) |
76 | | - min_consumption = min(consumptions) |
| 68 | +def print_statistics(stats, title): |
| 69 | + """ |
| 70 | + Print statistics (average, max, min) for the given data in a formatted table. |
| 71 | + """ |
| 72 | + if not stats: |
| 73 | + return |
| 74 | + |
| 75 | + print(f"\n{title}\n") |
| 76 | + print(f"{'Target':<20} {'Average consumption':<20} {'Maximum consumption':<20} {'Minimum consumption':<20}") |
| 77 | + print("=" * 80) |
77 | 78 |
|
78 | | - if not target == 'global': |
79 | | - result.append([target, |
80 | | - f"{avg_consumption:.2f}", |
81 | | - f"{max_consumption:.2f}", |
82 | | - f"{min_consumption:.2f}"]) |
| 79 | + total = {'avg': 0, 'max': 0, 'min': 0} |
| 80 | + for target, values in stats.items(): |
| 81 | + if target != 'global': |
| 82 | + print(f"{target:<20} {values['avg']:<20.2f} {values['max']:<20.2f} {values['min']:<20.2f}") |
83 | 83 | else: |
84 | | - total = [avg_consumption, max_consumption, min_consumption] |
| 84 | + total = values |
85 | 85 |
|
86 | | - print(f"{'Target':<20} " |
87 | | - f"{'Average consumption':<20} " |
88 | | - f"{'Maximum consumption':<20} " |
89 | | - f"{'Minimum consumption':<20}") |
90 | | - print("=" * 80) |
| 86 | + print("-" * 80) |
| 87 | + print(f"{'Global':<20} {total['avg']:<20.2f} {total['max']:<20.2f} {total['min']:<20.2f}") |
91 | 88 |
|
92 | | - for row in result[1:]: |
93 | | - print(f"{row[0]:<20} {row[1]:<20} {row[2]:<20} {row[3]:<20}") |
94 | 89 |
|
95 | | - print("=" * 80) |
96 | | - print(f"{'Global':<20} " |
97 | | - f"{total[0]:<20.2f} " |
98 | | - f"{total[1]:<20.2f} " |
99 | | - f"{total[2]:<20.2f}") |
100 | | - |
101 | | - print("\nIf you want to get a more precise evaluation, " |
102 | | - "we encourage you to read the documentation of PowerAPI " |
103 | | - "and adapt the sensor/formula configuration file in consequence \n") |
| 90 | +def start_pretty_print(): |
| 91 | + """ |
| 92 | + Pretty print the CPU and DRAM power consumption statistics from CSV files. |
| 93 | + """ |
| 94 | + print("The consumptions are given in Watt, note that the precision depends on the configuration file\n") |
| 95 | + |
| 96 | + data = load_data() |
| 97 | + |
| 98 | + # Calculate and print CPU statistics |
| 99 | + cpu_stats = calculate_statistics(data, 'cpu') |
| 100 | + print_statistics(cpu_stats, "CPU Consumption Statistics :") |
| 101 | + |
| 102 | + # Calculate and print DRAM statistics |
| 103 | + dram_stats = calculate_statistics(data, 'dram') |
| 104 | + print_statistics(dram_stats, "DRAM Consumption Statistics :") |
| 105 | + |
| 106 | + # Could add the GPU statistics here |
| 107 | + |
| 108 | + print("\nFor more precise evaluation, consult the PowerAPI documentation to adjust configurations.\n") |
104 | 109 |
|
105 | 110 |
|
106 | 111 | if __name__ == '__main__': |
|
0 commit comments