From 3ff2cc3c326dabd6ff5acb8ee1b1b84a2e3b6cc0 Mon Sep 17 00:00:00 2001 From: Mika Date: Tue, 13 Jan 2026 11:17:04 +0000 Subject: [PATCH] Add trace_analysis/src/trace_analysis/core.py --- trace_analysis/src/trace_analysis/core.py | 75 +++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 trace_analysis/src/trace_analysis/core.py diff --git a/trace_analysis/src/trace_analysis/core.py b/trace_analysis/src/trace_analysis/core.py new file mode 100644 index 0000000..885cfe6 --- /dev/null +++ b/trace_analysis/src/trace_analysis/core.py @@ -0,0 +1,75 @@ +import statistics +from typing import Dict, Any, List + + +def analyze_trace(trace_data: Dict[str, Any]) -> Dict[str, Any]: + """Analysiert Trace-Daten auf CPU-Migrationen, Reorder-Score und potenzielle Anomalien. + + Args: + trace_data (dict): Enthält Trace-Events, die jeweils timestamp, cpu_path, reorder_score + und mixed_snapshot_signature_count umfassen. + + Returns: + dict: Analyseergebnis mit Statistiken und Identifikation möglicher Anomalien. + """ + events: List[Dict[str, Any]] = trace_data.get("events", []) + + if not events: + return { + "cpu_switch_count": 0, + "reorder_score": {"mean": None, "stdev": None, "max": None}, + "mixed_snapshot_sum": 0, + "anomalies": [] + } + + cpu_switch_count = 0 + reorder_scores: List[float] = [] + mixed_snapshot_sum = 0 + anomalies: List[Dict[str, Any]] = [] + + last_cpu = None + + for ev in events: + cpu = ev.get("cpu_path") + reorder = ev.get("reorder_score", 0.0) + mixed_sig = ev.get("mixed_snapshot_signature_count", 0) + + if last_cpu is not None and cpu != last_cpu: + cpu_switch_count += 1 + last_cpu = cpu + + if isinstance(reorder, (int, float)): + reorder_scores.append(reorder) + if reorder > 0.8: # heuristische Anomalieschwelle + anomalies.append({ + "timestamp": ev.get("timestamp"), + "issue": "High reorder score", + "score": reorder + }) + + if isinstance(mixed_sig, int): + mixed_snapshot_sum += mixed_sig + if mixed_sig > 5: + anomalies.append({ + "timestamp": ev.get("timestamp"), + "issue": "High mixed snapshot signature count", + "count": mixed_sig + }) + + if reorder_scores: + mean_r = statistics.mean(reorder_scores) + stdev_r = statistics.stdev(reorder_scores) if len(reorder_scores) > 1 else 0.0 + max_r = max(reorder_scores) + else: + mean_r = stdev_r = max_r = None + + return { + "cpu_switch_count": cpu_switch_count, + "reorder_score": { + "mean": mean_r, + "stdev": stdev_r, + "max": max_r + }, + "mixed_snapshot_sum": mixed_snapshot_sum, + "anomalies": anomalies + }