Add trace_analysis/src/trace_analysis/core.py

This commit is contained in:
Mika 2026-01-13 11:17:04 +00:00
commit 3ff2cc3c32

View file

@ -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
}