power_streaming
zeus.monitor.power_streaming
Stream GPU and CPU power readings from zeusd instances via SSE.
This module provides PowerStreamingClient, a thread-based SSE client
that connects to one or more zeusd endpoints (TCP or Unix domain socket)
and provides the latest GPU and CPU power readings in a thread-safe manner.
from zeus.utils.zeusd import ZeusdConfig
from zeus.monitor.power_streaming import PowerStreamingClient
client = PowerStreamingClient(
servers=[
ZeusdConfig.tcp("node1", 4938, gpu_indices=[0, 1, 2, 3]),
ZeusdConfig.tcp("node2", 4938),
],
)
# Snapshot (latest readings at this instant):
readings = client.get_power() # {"node1:4938": PowerReadings(...)}
# Blocking iterator (yields on every SSE update):
for readings in client:
print(readings)
# Async iterator:
async for readings in client:
print(readings)
client.stop()
CpuPowerReading
dataclass
Power reading for a single CPU package.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cpu_w
|
float
|
CPU package power in watts. |
0.0
|
dram_w
|
float | None
|
DRAM power in watts, or None if not available. |
None
|
Source code in zeus/monitor/power_streaming.py
51 52 53 54 55 56 57 58 59 60 61 | |
PowerReadings
dataclass
Power readings from a single zeusd endpoint.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
timestamp_s
|
float
|
Unix timestamp (seconds) of the reading. |
0.0
|
gpu_power_w
|
dict[int, float]
|
Per-GPU power in watts, keyed by GPU index. |
dict()
|
cpu_power_w
|
dict[int, CpuPowerReading]
|
Per-CPU power readings, keyed by CPU index. |
dict()
|
Source code in zeus/monitor/power_streaming.py
64 65 66 67 68 69 70 71 72 73 74 75 76 | |
PowerStreamingClient
Connect to multiple zeusd instances and stream GPU/CPU power readings.
One background thread per device type per endpoint maintains an SSE
connection to the zeusd streaming endpoints. The latest power readings
are stored in a thread-safe dict, accessible via get_power().
The client supports three access patterns:
- Snapshot: Call
get_power()to retrieve the latest readings at any time. - Blocking iterator: Use
for readings in clientto block and yield a snapshot each time new SSE data arrives. Iteration stops whenstop()is called. - Async iterator: Use
async for readings in clientfor the same behavior without blocking the event loop.
client = PowerStreamingClient(servers=[...])
# Snapshot
readings = client.get_power()
# Blocking iterator
for readings in client:
print(readings)
# Async iterator
async for readings in client:
print(readings)
client.stop()
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
servers
|
Sequence[ZeusdConfig]
|
List of |
required |
reconnect_delay_s
|
float
|
Seconds to wait before reconnecting after a disconnect. |
1.0
|
Source code in zeus/monitor/power_streaming.py
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 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 | |
__init__
__init__(servers, reconnect_delay_s=1.0)
Source code in zeus/monitor/power_streaming.py
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 | |
stop
stop()
Stop all background connections and wake any blocked iterators.
Source code in zeus/monitor/power_streaming.py
175 176 177 178 179 180 181 182 183 | |
get_power
get_power()
Get the latest power readings from all endpoints.
Returns:
| Type | Description |
|---|---|
dict[str, PowerReadings]
|
Mapping of endpoint identifier to |
dict[str, PowerReadings]
|
timestamp and per-GPU/CPU power in watts. |
Source code in zeus/monitor/power_streaming.py
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 | |
__iter__
__iter__()
Yield power reading snapshots as they arrive from SSE streams.
Blocks until new readings are available, then yields a snapshot
(same format as get_power()). Iteration stops when stop() is
called.
client = PowerStreamingClient(servers=[...])
for readings in client:
for endpoint, pr in readings.items():
print(f"{endpoint}: {pr.gpu_power_w}")
Source code in zeus/monitor/power_streaming.py
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 | |
__aiter__
async
__aiter__()
Async version of __iter__.
Yields power reading snapshots without blocking the event loop.
Iteration stops when stop() is called.
client = PowerStreamingClient(servers=[...])
async for readings in client:
for endpoint, pr in readings.items():
print(f"{endpoint}: {pr.gpu_power_w}")
Source code in zeus/monitor/power_streaming.py
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 | |
_wait_for_update
_wait_for_update()
Block until readings are updated or timeout (1 s).
Used by __aiter__ to avoid blocking the async event loop.
Source code in zeus/monitor/power_streaming.py
249 250 251 252 253 254 255 | |
_init_server
_init_server(server)
Initialize connection to a server and decide what to stream.
Creates a ZeusdClient for the server (handling discovery and auth),
validates requested indices, and checks scope permissions.
Returns:
| Type | Description |
|---|---|
tuple[bool, bool]
|
A |
Raises:
| Type | Description |
|---|---|
ZeusdConnectionError
|
If the server is not reachable. |
ValueError
|
If explicitly requested indices are not available. |
ZeusdCapabilityError
|
If explicitly requested streaming requires a scope the token doesn't have. |
Source code in zeus/monitor/power_streaming.py
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 | |
_resolve_streaming
staticmethod
_resolve_streaming(user_indices, available_ids, has_permission, scope_name, device_type, endpoint)
Decide whether to stream a device type.
Semantics:
- user_indices is None: stream all available, silently skip if
none exist or if the token lacks the scope.
- user_indices == []: explicitly opt out, never stream.
- user_indices is a non-empty list: require all IDs to exist
and the scope to be granted; raise on mismatch.
Returns:
| Type | Description |
|---|---|
bool
|
True if streaming should be started for this device type. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If explicit indices are not a subset of available. |
ZeusdCapabilityError
|
If explicit indices are given but the token lacks the required scope. |
Source code in zeus/monitor/power_streaming.py
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 | |
_estimate_clock_offset
_estimate_clock_offset(endpoint, num_samples=5)
Estimate the clock offset between this client and the daemon.
Performs num_samples round-trips to GET /time on the daemon,
computes client_midpoint - daemon_time for each, and returns the
median offset in seconds. A positive offset means the daemon clock
is behind the client clock.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
endpoint
|
str
|
The endpoint identifier. |
required |
num_samples
|
int
|
Number of round-trips for robustness. |
5
|
Returns:
| Type | Description |
|---|---|
float
|
Estimated clock offset in seconds. Add this to daemon |
float
|
timestamps to align them with client time. |
Source code in zeus/monitor/power_streaming.py
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 399 400 | |
_gpu_stream_loop
_gpu_stream_loop(server, endpoint)
Background thread: stream GPU power from a single server.
Source code in zeus/monitor/power_streaming.py
402 403 404 405 406 407 408 409 410 411 | |
_cpu_stream_loop
_cpu_stream_loop(server, endpoint)
Background thread: stream CPU power from a single server.
Source code in zeus/monitor/power_streaming.py
413 414 415 416 417 418 419 420 421 422 | |
_stream_loop
_stream_loop(url, endpoint, process_fn, label)
Shared reconnect loop for SSE streams.
Source code in zeus/monitor/power_streaming.py
424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 | |
_connect_and_stream
_connect_and_stream(url, endpoint, process_fn)
Open an SSE connection and process events until disconnected.
Source code in zeus/monitor/power_streaming.py
466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 | |
_process_gpu_event
_process_gpu_event(event_text, endpoint)
Parse a GPU SSE event and update readings.
Source code in zeus/monitor/power_streaming.py
487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 | |
_process_cpu_event
_process_cpu_event(event_text, endpoint)
Parse a CPU SSE event and update readings.
Expected JSON format: {"timestamp_ms": N, "power_mw": {"0": {"cpu_mw": N, "dram_mw": N|null}}}.
Source code in zeus/monitor/power_streaming.py
518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 | |