-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprometheus_convergence.py
More file actions
398 lines (335 loc) · 14.2 KB
/
prometheus_convergence.py
File metadata and controls
398 lines (335 loc) · 14.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
# -*- coding: utf-8 -*-
# prometheus_convergence.py
"""
Prometheus-backed convergence policy for worker-pattern adaptation.
This module evaluates per-core pressure using observable runtime metrics
instead of indirect OS-level CPU measurements.
Primary indicators:
1. Queue wait time
2. Worker utilization
3. Queue depth
4. Task duration trend
These signals are used to recommend per-core worker patterns under
overloaded, balanced, or underutilized conditions.
"""
import time
from typing import Dict, List, Optional
from enum import Enum
from dataclasses import dataclass
from .threading_metrics import get_metrics
from prometheus_client import generate_latest
class WorkerPattern(Enum):
"""Per-core worker allocation patterns used by convergence."""
HEAVY = 2 # 2 workers per core (high contention)
MEDIUM = 3 # 3 workers per core (balanced, default)
LIGHT = 4 # 4 workers per core (underutilized)
@dataclass
class CorePressure:
"""Observed pressure summary and recommendation for one core."""
core_id: int
queue_depth: int
worker_utilization: float # 0-100%
queue_wait_p95: Optional[float] # seconds
avg_task_duration: Optional[float] # seconds
pressure_level: str # 'overloaded', 'balanced', 'underutilized'
recommended_pattern: WorkerPattern
class PrometheusConvergenceEngine:
"""Convergence engine driven by observable Prometheus metrics.
Assesses per-core pressure from queue, utilization, and duration signals,
then recommends worker-pattern adjustments without relying on OS CPU
percentage measurements.
"""
def __init__(
self,
topology,
queue_wait_threshold: float = 1.0, # p95 > 1s = overloaded
utilization_high: float = 85.0, # >80% = saturated
utilization_low: float = 35.0, # <40% = underutilized
queue_depth_factor: int = 5, # queue > workers*5 = overloaded
verbose: bool = True # Print [CONVERGENCE] messages
):
"""
Initialize the convergence engine and default per-core patterns.
Args:
topology: CPU topology provider exposing physical core count.
queue_wait_threshold: p95 queue wait above this value signals overload.
utilization_high: Utilization threshold indicating saturation.
utilization_low: Utilization threshold indicating underutilization.
queue_depth_factor: Queue depth multiplier used in overload checks.
verbose: Whether convergence decisions should be printed.
"""
self.topology = topology
self.queue_wait_threshold = queue_wait_threshold
self.utilization_high = utilization_high
self.utilization_low = utilization_low
self.queue_depth_factor = queue_depth_factor
self.verbose = verbose
self.metrics = get_metrics()
# Track current patterns
self.core_patterns: Dict[int, WorkerPattern] = {}
for core_id in range(topology.physical_cores):
self.core_patterns[core_id] = WorkerPattern.LIGHT
# Track convergence history
self.convergence_history: List[Dict] = []
def analyze_cores(self, worker_pool) -> List[CorePressure]:
"""Analyze all physical cores using current Prometheus metric output."""
pressures = []
# Get current Prometheus data
registry = self.metrics.get_registry()
prom_data = generate_latest(registry).decode('utf-8')
# Parse metrics for each core
for core_id in range(self.topology.physical_cores):
pressure = self._analyze_single_core(core_id, prom_data, worker_pool)
pressures.append(pressure)
return pressures
def _analyze_single_core(
self,
core_id: int,
prom_data: str,
worker_pool
) -> CorePressure:
"""Build one core-pressure summary from current Prometheus metrics."""
# Get queue depth (current gauge value)
queue_depth = self._extract_gauge(
prom_data,
'threading_queue_depth',
{'core_id': str(core_id)}
)
# Get worker utilization
utilization = self._extract_gauge(
prom_data,
'threading_worker_utilization_percent',
{'core_id': str(core_id)}
)
# Calculate queue wait p95 from histogram
queue_wait_p95 = self._calculate_histogram_percentile(
prom_data,
'threading_queue_wait_seconds',
{'core_id': str(core_id)},
0.95
)
# Calculate average task duration
avg_duration = self._calculate_histogram_average(
prom_data,
'threading_task_duration_seconds',
{'core_id': str(core_id)}
)
# Assess pressure level
pressure_level, recommended = self._assess_pressure(
core_id,
queue_depth,
utilization,
queue_wait_p95,
worker_pool
)
return CorePressure(
core_id=core_id,
queue_depth=int(queue_depth) if queue_depth else 0,
worker_utilization=utilization if utilization else 0.0,
queue_wait_p95=queue_wait_p95,
avg_task_duration=avg_duration,
pressure_level=pressure_level,
recommended_pattern=recommended
)
def _assess_pressure(
self,
core_id: int,
queue_depth: Optional[float],
utilization: Optional[float],
queue_wait_p95: Optional[float],
worker_pool
) -> tuple[str, WorkerPattern]:
"""Classify core pressure and return the recommended worker pattern."""
workers_per_core = worker_pool.workers_per_core if worker_pool else 4
# Convert None to 0 for comparisons
queue_depth = queue_depth or 0
utilization = utilization or 0
queue_wait_p95 = queue_wait_p95 or 0
# RULE 1: High queue wait time = OVERLOADED
if queue_wait_p95 > self.queue_wait_threshold:
if self.verbose:
print(f"[CONVERGENCE] Core {core_id}: Queue wait p95 = {queue_wait_p95:.2f}s > {self.queue_wait_threshold}s")
return 'overloaded', WorkerPattern.HEAVY
# RULE 2: Deep queue = OVERLOADED
if queue_depth > workers_per_core * self.queue_depth_factor:
if self.verbose:
print(f"[CONVERGENCE] Core {core_id}: Queue depth = {queue_depth} > {workers_per_core * self.queue_depth_factor}")
return 'overloaded', WorkerPattern.HEAVY
# RULE 3: High utilization + any queue = OVERLOADED
if utilization > self.utilization_high and queue_depth > 0:
if self.verbose:
print(f"[CONVERGENCE] Core {core_id}: Utilization = {utilization:.1f}% > {self.utilization_high}% with queue")
return 'overloaded', WorkerPattern.HEAVY
# RULE 4: Low utilization = UNDERUTILIZED
if utilization < self.utilization_low:
if self.verbose:print(f"[CONVERGENCE] Core {core_id}: Utilization = {utilization:.1f}% < {self.utilization_low}%")
return 'underutilized', WorkerPattern.LIGHT
# RULE 5: Everything else = BALANCED
return 'balanced', WorkerPattern.MEDIUM
def recommend_adjustments(
self,
core_pressures: List[CorePressure]
) -> Dict[int, WorkerPattern]:
"""Return only the per-core pattern changes that differ from current state."""
adjustments = {}
for pressure in core_pressures:
current = self.core_patterns[pressure.core_id]
recommended = pressure.recommended_pattern
if current != recommended:
adjustments[pressure.core_id] = recommended
# Log the reason (ALL prints wrapped!)
if self.verbose:
print(f"[CONVERGENCE] Core {pressure.core_id} adjustment:")
print(f" Current: {current.name} ({current.value} workers)")
print(f" Recommended: {recommended.name} ({recommended.value} workers)")
print(f" Reason: {pressure.pressure_level}")
print(f" Queue depth: {pressure.queue_depth}")
print(f" Utilization: {pressure.worker_utilization:.1f}%")
if pressure.queue_wait_p95:
print(f" Queue wait p95: {pressure.queue_wait_p95:.2f}s")
return adjustments
def apply_pattern(
self,
core_id: int,
pattern: WorkerPattern,
worker_pool=None
):
"""Apply a recommended pattern, record it in metrics, and log history.
If the provided worker pool supports dynamic per-core patterns, the
change is applied to the live pool as well.
"""
old_pattern = self.core_patterns[core_id]
self.core_patterns[core_id] = pattern
# Record in Prometheus
self.metrics.update_pattern(core_id, pattern.value)
if old_pattern != pattern:
if worker_pool and hasattr(worker_pool, 'set_core_pattern'):
try:
worker_pool.set_pattern(core_id, pattern.value)
if self.verbose:
print(f"[CONVERGENCE] Applied pattern to worker pool")
except Exception as e:
if self.verbose:
print(f"[CONVERGENCE] Warning: Could not apply pattern to pool: {e}")
self.metrics.record_convergence_change(
core_id,
old_pattern.value,
pattern.value
)
self.convergence_history.append({
'timestamp': time.time(),
'core_id': core_id,
'from_pattern': old_pattern.name,
'to_pattern': pattern.name
})
if self.verbose:
print(
f"[CONVERGENCE] Core {core_id}: "
f"{old_pattern.name} ({old_pattern.value} workers) -> "
f"{pattern.name} ({pattern.value} workers)"
)
def get_convergence_status(self) -> dict:
"""Get a current convergence state."""
distribution = {
'heavy': sum(1 for p in self.core_patterns.values() if p == WorkerPattern.HEAVY),
'medium': sum(1 for p in self.core_patterns.values() if p == WorkerPattern.MEDIUM),
'light': sum(1 for p in self.core_patterns.values() if p == WorkerPattern.LIGHT)
}
return {
'core_patterns': {
core_id: pattern.name
for core_id, pattern in self.core_patterns.items()
},
'pattern_distribution': distribution,
'total_changes': len(self.convergence_history),
'recent_changes': self.convergence_history[-5:] if self.convergence_history else []
}
# Utility methods for parsing Prometheus data
@staticmethod
def _extract_gauge(
prom_data: str,
metric_name: str,
labels: Dict[str, str]
) -> Optional[float]:
"""Extract gauge value from Prometheus data."""
label_str = ','.join(f'{k}="{v}"' for k, v in labels.items())
search = f'{metric_name}{{{label_str}}}'
for line in prom_data.split('\n'):
if search in line and not line.startswith('#'):
parts = line.split()
if len(parts) >= 2:
try:
return float(parts[-1])
except ValueError:
pass
return None
@staticmethod
def _calculate_histogram_percentile(
prom_data: str,
metric_name: str,
labels: Dict[str, str],
percentile: float
) -> Optional[float]:
"""
Calculate percentile from histogram buckets.
Simplified calculation - would use proper quantile estimation.
For now, returns the bucket le value where count >= target.
"""
# Build label string
base_labels = ','.join(f'{k}="{v}"' for k, v in labels.items())
# Parse buckets
buckets = []
for line in prom_data.split('\n'):
if f'{metric_name}_bucket' in line and base_labels in line:
# Extract le value and count
if 'le=' in line:
try:
le_val = line.split('le="')[1].split('"')[0]
if le_val == '+Inf':
continue
count = float(line.split()[-1])
buckets.append((float(le_val), count))
except:
continue
if not buckets:
return None
# Sort by le value
buckets.sort(key=lambda x: x[0])
# Find total count
total = buckets[-1][1] if buckets else 0
if total == 0:
return None
# Find bucket containing percentile
target = total * percentile
for le, count in buckets:
if count >= target:
return le
return buckets[-1][0] if buckets else None
@staticmethod
def _calculate_histogram_average(
prom_data: str,
metric_name: str,
labels: Dict[str, str]
) -> Optional[float]:
"""Calculate average from histogram sum and count."""
label_str = ','.join(f'{k}="{v}"' for k, v in labels.items())
sum_val = None
count_val = None
for line in prom_data.split('\n'):
if f'{metric_name}_sum{{{label_str}}}' in line:
parts = line.split()
if len(parts) >= 2:
try:
sum_val = float(parts[-1])
except ValueError:
pass
elif f'{metric_name}_count{{{label_str}}}' in line:
parts = line.split()
if len(parts) >= 2:
try:
count_val = float(parts[-1])
except ValueError:
pass
if sum_val is not None and count_val and count_val > 0:
return sum_val / count_val
return None