Skip to content

Commit 7f63b2e

Browse files
committed
docs(getting-started): Merged pretty print with start.py
1 parent a5212f1 commit 7f63b2e

File tree

4 files changed

+91
-131
lines changed

4 files changed

+91
-131
lines changed

docs/script/getting_started/docker-compose.yaml

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,6 @@ services:
1111
profiles:
1212
- mongodb
1313

14-
# CSV (Default is a dummy image, can be used to add treatments on CSV files)
15-
csv:
16-
container_name: csv
17-
image: ${CSV_IMAGE}
18-
volumes:
19-
- ${PWD}/csv:/tmp/csv
20-
profiles:
21-
- csv
22-
2314

2415
############################################
2516
# POWERAPI #
@@ -51,6 +42,7 @@ services:
5142

5243
# PowerAPI Formula
5344
formula:
45+
user: $UID:$GUID
5446
container_name: formula
5547
image: ${FORMULA_IMAGE}
5648
command:

docs/script/getting_started/pretty_print.py

Lines changed: 0 additions & 112 deletions
This file was deleted.

docs/script/getting_started/sensor/hwpc-mongodb.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
{
22
"name": "sensor",
33
"verbose": true,
4-
"frequency": 500,
4+
"frequency": 1000,
5+
"cgroup_basepath": "/sys/fs/cgroup/",
56
"output": {
67
"type": "mongodb",
7-
"uri": "mongodb://127.0.0.1",
8+
"uri": "mongodb://mongodb:27017",
89
"database": "db_sensor",
9-
"collection": "report_0"
10+
"collection": "prep"
1011
},
1112
"system": {
1213
"rapl": {
@@ -32,6 +33,5 @@
3233
"INSTRUCTIONS_RETIRED"
3334
]
3435
}
35-
},
36-
"cgroup_basepath": "/sys/fs/cgroup/"
36+
}
3737
}

docs/script/getting_started/start.py

Lines changed: 85 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,10 @@ def signal_handler(sig, frame):
5252

5353

5454
def 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):
6265
def 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

6871
def 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("\nFor more precise evaluation, consult the PowerAPI documentation to adjust configurations.\n")
156+
78157
def find_cpu(data):
79158
"""
80159
Find the cpu in the list of compatible cpu
@@ -211,7 +290,8 @@ def start_demo():
211290

212291
print("\nStarting 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("\nThe 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

240320
if __name__ == '__main__':
241321
start_demo()

0 commit comments

Comments
 (0)