Clockworker
Clockworker, loosely inspired by Seastar, is a single-threaded async executor with fair scheduling across multiple queues. Clockworker is agnostic to the underlying async runtime and can sit on top of any runtime like Tokio, Monoio, or Smol.
⚠️ Early/Alpha Release: This project is in early development. APIs may change in breaking ways between versions. Use at your own risk.
What is Clockworker for?
Single-threaded async runtimes are a great fit for a class of problems. Several such runtimes exist in the Rust ecosystem—Tokio, Monoio, Glommio, etc. But almost none of these (with the exception of Glommio) provide the ability to run multiple configurable work queues with different priorities. This becomes important for many real-world single-threaded systems, at minimum to separate foreground and background work. Clockworker aims to solve this problem.
It does so via work queues with configurable time-shares onto which tasks can be spawned. Clockworker uses an EEVDF-inspired scheduler: it chooses the queue to poll based on its fair time share (inspired by Linux CFS/EEVDF), and then executes tasks from that queue in FIFO order.
Note that Clockworker itself is just an executor loop, not a full async runtime, and is designed to sit on top of any other runtime.
Features
- EEVDF-based queue scheduling: Fair CPU time distribution between queues using virtual runtime (inspired by Linux CFS/EEVDF)
- Premption: If a queue becomes runnable while the timeslice of another lower priority queue is being executed, the timeslice is preempted at the next poll
- FIFO task ordering: Tasks within each queue execute in FIFO order
- Task cancellation: Abort running tasks via
JoinHandle::abort() - Panic handling: Configurable panic behavior (propagate or catch as
JoinError::Panic) - Statistics: Built-in metrics for monitoring executor and queue performance
Quick Start
Add to your Cargo.toml:
[]
= "0.2.0"
Examples
Basic Usage
The simplest example - spawn a task and wait for it:
use ExecutorBuilder;
use LocalSet;
async
Multiple Queues with Different Weights
Allocate CPU time proportionally between queues:
use ;
use LocalSet;
use Arc;
use ;
async
Task Cancellation
Cancel tasks using JoinHandle::abort():
use ExecutorBuilder;
use LocalSet;
use ;
async
Panic Handling
By default, the executor also panics when any of the tasks panic (same behavior as Tokio's single-threaded runtime). However, this can be configured:
use ;
use LocalSet;
async
Architecture
Clockworker uses a two-level scheduling approach based on EEVDF:
- Queue-level scheduling (EEVDF): Fairly distributes CPU time between queues based on their weights using virtual runtime
- Task-level scheduling (FIFO): Within each queue, tasks execute in FIFO order
This design allows you to:
- Allocate CPU resources between different workload classes (via queue weights)
- Ensure predictable task ordering within each queue
Benchmarks
Setup
Foreground tasks are generated at RPS of 1K/sec, each doing 1-3 yields - at each yield point, the task does variable (100-500us) of cpu work and a variable (100-500us) of sleep. In addition, some background tasks (0 or 8) are also generated that do similar amount of cpu work and sleep. Background tasks, when present, have enough CPU work in aggregate to saturate the CPU.
The workload runs on a single thread pinned to a core while the work generation happens on another thread/core. Two latencies are measured: a) time from task generation to task starting (i.e. queue delay) b) end to end time from task generation to task finishing (i.e. total latency). In addition, the amount of background work done is also measured.
The benchmark compares five options:
- Tokio single threaded runtime with only foreground tasks (baseline)
- Tokio single threaded runtime with both foreground & background tasks
- Clockworker executor on top of tokio single threaded runtime with only foreground tasks
- Same as #3 above but with both foreground/background tasks.
- Two separate single threaded tokio runtimes - one for background and one for foreground (no coordination between them)
Results
| Metric | Tokio (fg only) | CW (fg+bg) | CW (fg only) | Tokio (fg+bg) | Two-RT/OS |
|---|---|---|---|---|---|
| p50 queue delay | 0.40ms | 0.41ms (+2%) | 0.38ms (-5%) | 8.33ms (+1982%) | 0.49ms (+22%) |
| p90 queue delay | 1.23ms | 1.18ms (-4%) | 1.20ms (-2%) | 12.05ms (+880%) | 1.39ms (+13%) |
| p99 queue delay | 2.19ms | 2.18ms (-0%) | 2.18ms (-0%) | 15.89ms (+626%) | 2.52ms (+15%) |
| p50 total latency | 2.44ms | 2.37ms (-3%) | 2.46ms (+1%) | 14.78ms (+506%) | 2.56ms (+5%) |
| p90 total latency | 4.65ms | 4.53ms (-3%) | 4.71ms (+1%) | 24.23ms (+421%) | 4.83ms (+4%) |
| p99 total latency | 6.16ms | 6.11ms (-1%) | 6.30ms (+2%) | 31.60ms (+413%) | 6.63ms (+8%) |
| BG throughput | 0 | 1299 | 0 | 1337 | 1299 |
Interpretation
- Clockworker adds negligible overhead and with foreground only tasks, is competitive with singel threaded tokio runtime.
- With Tokio runtime, foreground latency degrades 4-5x with background tasks.
- With Clockworker executor, background tasks don't impact the foreground latency at all - and the performance is competitive with tokio foreground only
- Adding two-runtimes is also competitive from latency POV
- All three options with background tasks are comparable in terms of the volume of the background work done.
Clockworker vs Multiple Runtimes
While running multiple runtimes, one for each priority queue, is competitive with Clockworker as shown in above benchmarks, there are a few other benefits of using clockworker:
- Less complexity - it's some additional work to setup & maintain multiple runtimes (e.g. graceful shutdowns, sharing stats etc.)
- Clockworkes works with !Send and !Sync futures - no need for Arc/Mutex etc. This both improves ergonomics and may also show up as overhead depending on the application (not tested in the benchmarks)
- It's non-trivial to setup multiple runtimes in platform agnostic way (e.g. CPU affinity works differently on Mac vs Linux)
Requirements
- Rust 1.70+
- Works with any async runtime (tokio, smol, monoio, etc.) via
LocalSetor similar
License
Licensed under the Apache License, Version 2.0. See LICENSE for details.