|
| 1 | +import csv |
| 2 | +import os |
| 3 | + |
| 4 | + |
| 5 | +def start_pretty_print(): |
| 6 | + """ |
| 7 | + Pretty print the result of the demo by parsing the csv files of each cgroup, |
| 8 | + then proceed to calculate the average, maximum, and minimum consumption of each cgroup |
| 9 | + and print them in a table format |
| 10 | + """ |
| 11 | + data = [] |
| 12 | + result = [["Cgroup", "Average consumption", "Maximum consumption", "Minimum consumption"]] |
| 13 | + |
| 14 | + directory = './csv' |
| 15 | + |
| 16 | + print("The consumption are given in Watt, note that the precision depend on the value given in the configuration file (the base CPU frequence, the CPU TDP, ...) ") |
| 17 | + |
| 18 | + # Get all the csv power report in the csv directory |
| 19 | + for dirpath, dirnames, filenames in os.walk(directory): |
| 20 | + for filename in filenames: |
| 21 | + if filename.endswith('.csv'): |
| 22 | + file_path = os.path.join(dirpath, filename) |
| 23 | + |
| 24 | + with open(file_path, mode='r', newline='') as f: |
| 25 | + reader = csv.DictReader(f) |
| 26 | + for row in reader: |
| 27 | + data.append(row) |
| 28 | + |
| 29 | + cgroup_data = {} |
| 30 | + |
| 31 | + # We remove rapl, but it's still available in the csv files |
| 32 | + for row in data: |
| 33 | + target = row['target'] |
| 34 | + consumption = float(row['power']) |
| 35 | + |
| 36 | + if target not in cgroup_data and not target == 'rapl': |
| 37 | + cgroup_data[target] = [] |
| 38 | + |
| 39 | + if not target == 'rapl': |
| 40 | + cgroup_data[target].append(consumption) |
| 41 | + |
| 42 | + for target, consumptions in cgroup_data.items(): |
| 43 | + avg_consumption = sum(consumptions) / len(consumptions) |
| 44 | + max_consumption = max(consumptions) |
| 45 | + min_consumption = min(consumptions) |
| 46 | + |
| 47 | + result.append([target, f"{avg_consumption:.2f}", f"{max_consumption:.2f}", f"{min_consumption:.2f}"]) |
| 48 | + |
| 49 | + print(f"{'Cgroup':<20} {'Average consumption':<20} {'Maximum consumption':<20} {'Minimum consumption':<20}") |
| 50 | + print("=" * 80) |
| 51 | + |
| 52 | + for row in result[1:]: |
| 53 | + print(f"{row[0]:<20} {row[1]:<20} {row[2]:<20} {row[3]:<20}") |
| 54 | + |
| 55 | + |
| 56 | +if __name__ == '__main__': |
| 57 | + start_pretty_print() |
0 commit comments