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 "xtc_export.h"
26
27#include <stdint.h>
28
29#include "xtc.h"
30
31typedef struct xtc_iosched xtc_iosched_t;
32
33typedef struct xtc_iosched_opts {
34 int fd; /* target descriptor (direct or buffered) */
35 int adaptive; /* 1 = GA-tune the batch size; 0 = fixed */
36 int batch_size; /* fixed (adaptive=0) or initial (adaptive=1) */
37 int min_batch; /* gene bounds when adaptive (>=1) */
38 int max_batch;
39 uint64_t seed; /* tuner PRNG seed (0 = default) */
41
42typedef struct xtc_iosched_stats {
43 uint64_t writes; /* total writes queued */
44 uint64_t bytes; /* total bytes written */
45 uint64_t flushes; /* batches issued */
46 int cur_batch; /* batch size currently in use */
47 double last_mbps; /* throughput of the last flush, MiB/s */
48 double mutation_rate; /* tuner mutation rate (adaptive only) */
50
51/*
52 * PUBLIC: int xtc_iosched_create __P((const xtc_iosched_opts_t *, xtc_iosched_t **));
53 * PUBLIC: void xtc_iosched_destroy __P((xtc_iosched_t *));
54 * PUBLIC: int xtc_iosched_write __P((xtc_iosched_t *, const void *, uint32_t, int64_t));
55 * PUBLIC: int xtc_iosched_flush __P((xtc_iosched_t *));
56 * PUBLIC: void xtc_iosched_get_stats __P((const xtc_iosched_t *, xtc_iosched_stats_t *));
57 */
58XTC_API int xtc_iosched_create(const xtc_iosched_opts_t *opts, xtc_iosched_t **out);
59XTC_API void xtc_iosched_destroy(xtc_iosched_t *s);
60
61/* Queue a write at off. The buffer must remain valid until the next
62 * flush completes. When the queue reaches the current batch size an
63 * implicit flush runs (issuing the batch and possibly parking the
64 * writer fiber). Returns XTC_OK, or a negative errno from a flush. */
65XTC_API int xtc_iosched_write(xtc_iosched_t *s, const void *buf, uint32_t len,
66 int64_t off);
67
68/* Issue all queued writes now. Returns XTC_OK or a negative errno. */
69XTC_API int xtc_iosched_flush(xtc_iosched_t *s);
70
71XTC_API void xtc_iosched_get_stats(const xtc_iosched_t *s, xtc_iosched_stats_t *out);
72
73#endif /* XTC_IOSCHED_H */