libxtc 0.4.0
Async concurrency for C: Tokio + Seastar + BEAM, in one library
Loading...
Searching...
No Matches
xtc_iosched.h
1/*-
2 * Copyright (c) 2026, The XTC Project
3 * Use of this source code is governed by the ISC License.
4 *
5 * src/inc/xtc_iosched.h
6 * Adaptive write-batching scheduler for a single writer over the
7 * async file path (xtc_aio_*), intended for the direct-I/O
8 * writeback hot path (a buffer-manager flusher, a WAL writer).
9 *
10 * Queued writes are coalesced into batches; each flush issues the
11 * batch via xtc_aio_pwrite and measures throughput. When adaptive
12 * mode is on, a genetic tuner (xtc_dio_sched) evolves the batch size
13 * to maximise observed throughput and re-adapts when the workload
14 * or device behaviour shifts. Opt-in per scheduler (per device):
15 * with adaptive off it uses a fixed batch size.
16 *
17 * Single-writer contract: all calls for one scheduler must come
18 * from one fiber (the device's writer). This keeps batching free
19 * of cross-fiber coordination; flushing parks only that fiber.
20 */
21
22#ifndef XTC_IOSCHED_H
23#define XTC_IOSCHED_H
24
25#include <stdint.h>
26
27#include "xtc.h"
28
29typedef struct xtc_iosched xtc_iosched_t;
30
31typedef struct xtc_iosched_opts {
32 int fd; /* target descriptor (direct or buffered) */
33 int adaptive; /* 1 = GA-tune the batch size; 0 = fixed */
34 int batch_size; /* fixed (adaptive=0) or initial (adaptive=1) */
35 int min_batch; /* gene bounds when adaptive (>=1) */
36 int max_batch;
37 uint64_t seed; /* tuner PRNG seed (0 = default) */
39
40typedef struct xtc_iosched_stats {
41 uint64_t writes; /* total writes queued */
42 uint64_t bytes; /* total bytes written */
43 uint64_t flushes; /* batches issued */
44 int cur_batch; /* batch size currently in use */
45 double last_mbps; /* throughput of the last flush, MiB/s */
46 double mutation_rate; /* tuner mutation rate (adaptive only) */
48
49/*
50 * PUBLIC: int xtc_iosched_create __P((const xtc_iosched_opts_t *, xtc_iosched_t **));
51 * PUBLIC: void xtc_iosched_destroy __P((xtc_iosched_t *));
52 * PUBLIC: int xtc_iosched_write __P((xtc_iosched_t *, const void *, uint32_t, int64_t));
53 * PUBLIC: int xtc_iosched_flush __P((xtc_iosched_t *));
54 * PUBLIC: void xtc_iosched_get_stats __P((const xtc_iosched_t *, xtc_iosched_stats_t *));
55 */
56int xtc_iosched_create(const xtc_iosched_opts_t *opts, xtc_iosched_t **out);
57void xtc_iosched_destroy(xtc_iosched_t *s);
58
59/* Queue a write at off. The buffer must remain valid until the next
60 * flush completes. When the queue reaches the current batch size an
61 * implicit flush runs (issuing the batch and possibly parking the
62 * writer fiber). Returns XTC_OK, or a negative errno from a flush. */
63int xtc_iosched_write(xtc_iosched_t *s, const void *buf, uint32_t len,
64 int64_t off);
65
66/* Issue all queued writes now. Returns XTC_OK or a negative errno. */
67int xtc_iosched_flush(xtc_iosched_t *s);
68
69void xtc_iosched_get_stats(const xtc_iosched_t *s, xtc_iosched_stats_t *out);
70
71#endif /* XTC_IOSCHED_H */