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