Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add some ruff checks #68

Merged
merged 4 commits into from
Jan 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 3 additions & 10 deletions csv/convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,16 +154,9 @@ def result_key(r):

def mean_power_package(result) -> float:
mean_power = 0
result_package_mean = (
result.get("monitoring", {}).get("package", {}).get("mean", {})
)
if (
result_package_mean.get("unit") == "Watts"
and len(result_package_mean.get("events", [])) > 0
):
mean_power = sum(result_package_mean["events"]) / len(
result_package_mean["events"]
)
result_package_mean = result.get("monitoring", {}).get("package", {}).get("mean", {})
if result_package_mean.get("unit") == "Watts" and len(result_package_mean.get("events", [])) > 0:
mean_power = sum(result_package_mean["events"]) / len(result_package_mean["events"])
return mean_power


Expand Down
20 changes: 7 additions & 13 deletions graph/chassis.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import numpy as np

from graph.graph import Graph
from hwbench.bench.monitoring_structs import Metrics, PowerCategories, PowerContext

Expand All @@ -15,10 +16,7 @@ def graph_chassis(args, bench_name, output_dir) -> int:
bench = args.traces[0].bench(bench_name)
base_outfile = f"{bench_name} {bench.workers()}x{bench.engine()}_{bench.engine_module()}_{bench.engine_module_parameter()}_chassis"
y_label = "Watts"
title = (
f'{args.title}\n\nChassis power consumption during "{bench_name}" benchmark\n'
f"\n{bench.title()}"
)
title = f"{args.title}\n\nChassis power consumption during {bench_name} benchmark\n\n{bench.title()}"

def get_marker(category: PowerCategories) -> str:
if category == PowerCategories.SERVER:
Expand All @@ -38,9 +36,7 @@ def get_marker(category: PowerCategories) -> str:
# Collect all components mean value
for component in PowerCategories.list():
# Not all components are available on every system
if component not in bench.get_component(
Metrics.POWER_CONSUMPTION, PowerContext.BMC
):
if component not in bench.get_component(Metrics.POWER_CONSUMPTION, PowerContext.BMC):
continue

if str(component) not in sum_serie:
Expand Down Expand Up @@ -83,9 +79,9 @@ def get_marker(category: PowerCategories) -> str:
else:
# These are shared metrics on the chassis, so picking one from the first bench
# should be enough to get the chassis metric, no need to iterate on traces.
value = bench.get_single_metric(
Metrics.POWER_CONSUMPTION, PowerContext.BMC, component
).get_mean()[sample]
value = bench.get_single_metric(Metrics.POWER_CONSUMPTION, PowerContext.BMC, component).get_mean()[
sample
]
sum_serie[str(component)].append(value)
sum_serie_in_chassis[str(component)].append(value)

Expand Down Expand Up @@ -131,9 +127,7 @@ def get_marker(category: PowerCategories) -> str:
curve_label = str(component)
if component in [PowerCategories.SERVER, PowerCategories.SERVERINCHASSIS]:
curve_label = f"sum of {str(component)}"
graph.get_ax().plot(
x_serie, y_serie, "", label=curve_label, marker=get_marker(component)
)
graph.get_ax().plot(x_serie, y_serie, "", label=curve_label, marker=get_marker(component))

for trace in args.traces:
y_serie = np.array(serie[trace.get_name()])[order]
Expand Down
2 changes: 1 addition & 1 deletion graph/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@

def fatal(reason):
"""Print the error and exit 1."""
sys.stderr.write("Fatal: {}\n".format(reason))
sys.stderr.write(f"Fatal: {reason}\n")
sys.stderr.flush()
sys.exit(1)
41 changes: 13 additions & 28 deletions graph/graph.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
from typing import Optional
from __future__ import annotations

import matplotlib
from matplotlib.pylab import Axes
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.ticker import FuncFormatter, AutoMinorLocator, MultipleLocator
from matplotlib.pylab import Axes
from matplotlib.ticker import AutoMinorLocator, FuncFormatter, MultipleLocator

from graph.common import fatal
from graph.trace import Bench
from hwbench.bench.monitoring_structs import Metrics, MonitorMetric
Expand Down Expand Up @@ -34,7 +36,7 @@ def __init__(
square=False,
show_source_file=None,
) -> None:
self.ax2: Optional[Axes] = None
self.ax2: Axes | None = None
self.args = args
self.fig, self.ax = plt.subplots()
self.dpi = 100
Expand Down Expand Up @@ -116,9 +118,7 @@ def prepare_axes(
self.ax2.set_ylim(None, ymin=0, ymax=self.y2_max, emit=True, auto=True)
self.ax2.yaxis.set_major_formatter(FuncFormatter(self.human_format))
self.fig.tight_layout() # otherwise the right y-label is slightly clipped
self.ax2.yaxis.set_major_locator(
matplotlib.ticker.LinearLocator(len(self.ax.get_yticks()) - 2)
)
self.ax2.yaxis.set_major_locator(matplotlib.ticker.LinearLocator(len(self.ax.get_yticks()) - 2))
else:
# Bar graphs do not need legend, let the caller disable it
if legend:
Expand Down Expand Up @@ -222,21 +222,15 @@ def generic_graph(

samples_count = bench.get_samples_count()
unit = bench.get_metric_unit(component_type)
title = (
f'{item_title} during "{bench.get_bench_name()}" benchmark job\n'
f"{args.title}\n"
f"\n Stressor: "
)
title = f'{item_title} during "{bench.get_bench_name()}" benchmark job\n' f"{args.title}\n" f"\n Stressor: "
title += f"{bench.workers()} x {bench.get_title_engine_name()} for {bench.duration()} seconds"
title += f"\n{bench.get_system_title()}"
graph = Graph(
args,
title,
"Time [seconds]",
unit,
output_dir.joinpath(
f"{trace.get_name()}/{bench.get_bench_name()}/{str(component_type)}"
),
output_dir.joinpath(f"{trace.get_name()}/{bench.get_bench_name()}/{str(component_type)}"),
outfile,
show_source_file=trace,
)
Expand Down Expand Up @@ -278,16 +272,12 @@ def generic_graph(

# Let's pick the last known value
if args.ignore_missing_datapoint == "last":
data_serie[component.get_full_name()].append(
component.get_mean()[-1]
)
data_serie[component.get_full_name()].append(component.get_mean()[-1])
else:
# Replace it by a zero
data_serie[component.get_full_name()].append(0)
else:
data_serie[component.get_full_name()].append(
component.get_mean()[sample]
)
data_serie[component.get_full_name()].append(component.get_mean()[sample])

if second_axis:
for _, entry in bench.get_monitoring_metric(second_axis).items():
Expand Down Expand Up @@ -376,10 +366,7 @@ def yerr_graph(
)
data_serie[MEAN].append(mean_value)

title = (
f'{prefix}{component.get_name()} during "{bench.get_bench_name()}" benchmark job\n'
f"\n Stressor: "
)
title = f'{prefix}{component.get_name()} during "{bench.get_bench_name()}" benchmark job\n' f"\n Stressor: "
title += f"{bench.workers()} x {bench.get_title_engine_name()} for {bench.duration()} seconds"
title += f"\n{bench.get_system_title()}"

Expand All @@ -388,9 +375,7 @@ def yerr_graph(
title,
"Time [seconds]",
unit,
output_dir.joinpath(
f"{trace.get_name()}/{bench.get_bench_name()}/{str(component_type)}"
),
output_dir.joinpath(f"{trace.get_name()}/{bench.get_bench_name()}/{str(component_type)}"),
f"{prefix}{component.get_name()}",
show_source_file=trace,
)
Expand Down
74 changes: 20 additions & 54 deletions graph/hwgraph.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@
from typing import Any # noqa: F401

try:
from graph.chassis import graph_chassis
from graph.common import fatal
from graph.graph import init_matplotlib, generic_graph, yerr_graph
from graph.graph import generic_graph, init_matplotlib, yerr_graph
from graph.individual import individual_graph
from graph.scaling import scaling_graph
from graph.chassis import graph_chassis
from graph.trace import Trace
from hwbench.bench.monitoring_structs import (
FanContext,
Metrics,
PowerCategories,
PowerContext,
Metrics,
)
except ImportError as exc:
print(exc)
Expand All @@ -29,13 +29,9 @@
def valid_trace_file(trace_arg: str) -> Trace:
"""Custom argparse type to decode and validate the trace files"""

match = re.search(
r"(?P<filename>.*):(?P<logical_name>.*):(?P<power_metric>.*)", trace_arg
)
match = re.search(r"(?P<filename>.*):(?P<logical_name>.*):(?P<power_metric>.*)", trace_arg)
if not match:
raise argparse.ArgumentTypeError(
f"{trace_arg} does not match 'filename:logical_name:power_metric' syntax"
)
raise argparse.ArgumentTypeError(f"{trace_arg} does not match 'filename:logical_name:power_metric' syntax")

try:
trace = Trace(
Expand Down Expand Up @@ -83,13 +79,9 @@ def compare_traces(args) -> None:
if args.traces[0].get_original_config() != trace.get_original_config():
# If a trace is not having the same configuration file,
# It's impossible to compare & graph the results.
fatal(
f"{trace.filename} is not having the same configuration file as previous traces"
)
fatal(f"{trace.filename} is not having the same configuration file as previous traces")
if trace.get_name() in names:
fatal(
f"{trace.filename} is using '{trace.get_name()}' as logical_name while it's already in use"
)
fatal(f"{trace.filename} is using '{trace.get_name()}' as logical_name while it's already in use")
else:
names.append(trace.get_name())

Expand All @@ -103,9 +95,7 @@ def graph_monitoring_metrics(args, trace: Trace, bench_name: str, output_dir) ->
for metric in metrics:
# If a metric has no measure, let's ignore it
if len(metrics[metric].get_samples()) == 0:
print(
f"{bench_name}: No samples found in {metric_name}.{metric}, ignoring metric."
)
print(f"{bench_name}: No samples found in {metric_name}.{metric}, ignoring metric.")
else:
rendered_graphs += yerr_graph(
args,
Expand All @@ -127,9 +117,7 @@ def graph_fans(args, trace: Trace, bench_name: str, output_dir) -> int:
print(f"{bench_name}: no fans")
return rendered_graphs
for second_axis in [Metrics.THERMAL, Metrics.POWER_CONSUMPTION]:
rendered_graphs += generic_graph(
args, output_dir, bench, Metrics.FANS, "Fans speed", second_axis
)
rendered_graphs += generic_graph(args, output_dir, bench, Metrics.FANS, "Fans speed", second_axis)

for fan in fans:
rendered_graphs += yerr_graph(args, output_dir, bench, Metrics.FANS, fans[fan])
Expand Down Expand Up @@ -185,9 +173,7 @@ def graph_pdu(args, trace: Trace, bench_name: str, output_dir) -> int:

def graph_thermal(args, trace: Trace, bench_name: str, output_dir) -> int:
rendered_graphs = 0
rendered_graphs += generic_graph(
args, output_dir, trace.bench(bench_name), Metrics.THERMAL, str(Metrics.THERMAL)
)
rendered_graphs += generic_graph(args, output_dir, trace.bench(bench_name), Metrics.THERMAL, str(Metrics.THERMAL))
return rendered_graphs


Expand All @@ -203,9 +189,7 @@ def graph_environment(args, output_dir) -> int:
all_chassis = [t.get_chassis_serial() == chassis for t in args.traces]
# if all traces are from the same chassis, let's enable the same_chassis feature
if all_chassis.count(True) == len(args.traces) and len(args.traces) > 1:
print(
f"environment: All traces are from the same chassis ({chassis}), enabling --same-chassis feature"
)
print(f"environment: All traces are from the same chassis ({chassis}), enabling --same-chassis feature")
args.same_chassis = True

if args.same_chassis:
Expand All @@ -230,9 +214,7 @@ def valid_traces(args):
except KeyError:
return f"environment: missing '{metric}' monitoric metric in {trace.get_filename()}, disabling same-enclosure print"
else:
return (
"environment: server are not unique, disabling same-chassis print"
)
return "environment: server are not unique, disabling same-chassis print"
return ""

error_message = valid_traces(args)
Expand All @@ -245,13 +227,9 @@ def valid_traces(args):
for trace in args.traces:
output_dir.joinpath(f"{trace.get_name()}").mkdir(parents=True, exist_ok=True)
benches = trace.bench_list()
print(
f"environment: rendering {len(benches)} jobs from {trace.get_filename()} ({trace.get_name()})"
)
print(f"environment: rendering {len(benches)} jobs from {trace.get_filename()} ({trace.get_name()})")
for bench_name in sorted(benches):
rendered_graphs += graph_monitoring_metrics(
args, trace, bench_name, output_dir
)
rendered_graphs += graph_monitoring_metrics(args, trace, bench_name, output_dir)
rendered_graphs += graph_fans(args, trace, bench_name, output_dir)
rendered_graphs += graph_cpu(args, trace, bench_name, output_dir)
rendered_graphs += graph_pdu(args, trace, bench_name, output_dir)
Expand Down Expand Up @@ -300,9 +278,7 @@ def main():
)
subparsers = parser.add_subparsers(help="hwgraph sub-commands")

parser_graph = subparsers.add_parser(
"graph", help="Generate graphs from trace files"
)
parser_graph = subparsers.add_parser("graph", help="Generate graphs from trace files")
parser_graph.add_argument(
"--traces",
type=valid_trace_file,
Expand All @@ -318,15 +294,9 @@ def main():
""",
required=True,
)
parser_graph.add_argument(
"--no-env", help="Disable environmental graphs", action="store_false"
)
parser_graph.add_argument(
"--no-scaling", help="Disable scaling graphs", action="store_false"
)
parser_graph.add_argument(
"--no-individual", help="Disable individual graphs", action="store_false"
)
parser_graph.add_argument("--no-env", help="Disable environmental graphs", action="store_false")
parser_graph.add_argument("--no-scaling", help="Disable scaling graphs", action="store_false")
parser_graph.add_argument("--no-individual", help="Disable individual graphs", action="store_false")
parser_graph.add_argument("--title", help="Title of the graph")
parser_graph.add_argument("--dpi", help="Graph dpi", type=int, default="72")
parser_graph.add_argument("--width", help="Graph width", type=int, default="1920")
Expand All @@ -344,9 +314,7 @@ def main():
choices=["pgf", "svg", "agg", "cairo"],
default="cairo",
)
parser_graph.add_argument(
"--outdir", help="Name of the output directory", required=True
)
parser_graph.add_argument("--outdir", help="Name of the output directory", required=True)
parser_graph.add_argument(
"--same-chassis",
help="All traces are from the same chassis",
Expand All @@ -366,9 +334,7 @@ def main():
)
parser_graph.set_defaults(func=render_traces)

parser_list = subparsers.add_parser(
"list", help="list monitoring metrics from a trace file"
)
parser_list = subparsers.add_parser("list", help="list monitoring metrics from a trace file")
parser_list.add_argument(
"--trace",
type=str,
Expand Down
Loading
Loading