-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathconcurrent_cache_bench.ae
More file actions
314 lines (289 loc) · 11.4 KB
/
Copy pathconcurrent_cache_bench.ae
File metadata and controls
314 lines (289 loc) · 11.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
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
// Concurrent Cache Designs — a scaling study, the Aether way (issue #841).
//
// Replicates the classic "concurrent cache designs" study (single global
// lock negatively scales; lock striping is the general winner; copy-on-
// write wins read-mostly) using the concurrency models Aether ships. It
// compares THREE designs for a string -> int map across four read/write
// mixes and reports throughput in ops/sec.
//
// 1. SINGLE-OWNER ACTOR — one actor owns the whole map. Every op funnels
// through ONE mailbox: the actor-native "one mutex" trap.
// (docs/sharded-actor-map.md)
// 2. SHARDED ACTOR MAP — lock striping, actor-native: N owner actors,
// key routed by hash(key) % N, so unrelated keys hit different
// mailboxes. Fire-and-forget writes to distinct shards drain in
// parallel. (examples/actors/sharded-map.ae)
// 3. COW SNAPSHOT CELL — std.snapshot. One atomic pointer to an
// immutable map; reads are LOCK-FREE acquire loads with NO mailbox
// round-trip; a write rebuilds the map and publishes with store(),
// deferring the displaced snapshot's free by one generation (the RCU
// grace-period discipline). (docs/snapshot-cell.md)
//
// Methodology: a fixed op budget per design x mix over a fixed key space,
// driven by one deterministic LCG so every design sees the SAME op stream.
// Timing is the monotonic clock; throughput = ops * 1e9 / elapsed_ns in
// long math. Reads on the actor designs are `?` asks (mailbox round-trip);
// COW reads are direct lock-free loads. Every handler uses a SINGLE
// trailing `reply` (multiple branch replies corrupt the `?` result).
//
// HONESTY (see docs/concurrent-cache-benchmark.md): this is a single,
// non-core-pinned run on whatever machine executes it — it prints the
// numbers it actually measures, nothing is hardcoded. The full refinement
// (pin threads to cores 1..N, sweep core counts, add concurrent read
// drivers to expose shard parallelism on the read path, and a Zipfian
// hot-key distribution) is documented there as the next step and is out of
// scope for this self-contained, runnable smoke test.
//
// Run: AETHER_HOME=. ae run benchmarks/concurrent-cache/concurrent_cache_bench.ae
import std.os
import std.string
import std.map
import std.snapshot
// ---- Tunables ----------------------------------------------------------
ops_budget() -> int { return 100000 }
nkeys() -> int { return 64 }
nshards() -> int { return 8 }
// ---- Messages ----------------------------------------------------------
// Map values are boxed as decimal strings (map values are pointers) and
// parsed back on read. The ask reply puts the value first (the asker
// receives the first field); ABSENT (-1) signals a missing key.
message Set { key: string, val: int }
message Get { key: string }
message GetReply { value: int }
// ---- Design 1: single-owner actor (the "one mutex" trap) ---------------
actor SingleOwner {
state store = map_new()
receive {
Set(key, val) -> {
map_put_string_owned(store, key, string.from_int(val))
}
Get(key) -> {
result = 0 - 1 // ABSENT
v = map_get_raw(store, key)
if v != null {
n, _ = string.to_int(v)
result = n
}
reply GetReply { value: result } // single trailing reply
}
}
}
// ---- Design 2: sharded actor map (lock striping) -----------------------
// Identical handler to SingleOwner; the only difference is that main()
// routes each key to shards[hash(key) % N], so unrelated keys hit
// different mailboxes and fire-and-forget writes drain in parallel.
actor Shard {
state store = map_new()
receive {
Set(key, val) -> {
map_put_string_owned(store, key, string.from_int(val))
}
Get(key) -> {
result = 0 - 1
v = map_get_raw(store, key)
if v != null {
n, _ = string.to_int(v)
result = n
}
reply GetReply { value: result }
}
}
}
// ---- Helpers -----------------------------------------------------------
// FNV-1a over the key bytes, masked non-negative, mod n. A standard fast
// non-cryptographic hash — the right tool for routing (crypto hashes
// return hex strings; far too heavy here).
shard_for(key: string, n: int) -> int {
h = 2166136261
len = string.length(key)
i = 0
while i < len {
c = string.char_at(key, i)
h = (h ^ c) * 16777619
h = h & 2147483647
i = i + 1
}
return h % n
}
// key index -> key string ("key0", "key1", ...). Both the driver and the
// COW writer regenerate keys from indices, so no shared key array exists.
key_at(i: int) -> string {
return string.concat("key", string.from_int(i))
}
// 31-bit LCG step. Threading one seed through the loop gives every design
// the IDENTICAL stream of (key, read/write) decisions.
next_rng(seed: int) -> int {
return (seed * 1103515245 + 12345) & 2147483647
}
// ops/sec from an op count and an elapsed-ns duration. The numerator
// `ops * 1e9` reaches ~1e14, so it MUST be 64-bit: widen `ops` to long
// first (int*int would overflow 32 bits). Guards a zero/negative interval.
ops_per_sec(ops: int, elapsed_ns: long) -> long {
if elapsed_ns <= 0 {
return 0
}
long total = ops // widen int -> long
total = total * 1000000000 // long * int -> long, no overflow
return total / elapsed_ns // long / long
}
// Rebuild a fresh immutable snapshot map from `master` over the whole key
// space. Readers still holding the old snapshot are unaffected.
build_snapshot(master: ptr, nk: int) -> ptr {
next = map_new()
i = 0
while i < nk {
k = key_at(i)
v = map_get_raw(master, k)
if v != null {
map_put_string_owned(next, k, string.copy(v))
}
i = i + 1
}
return next
}
// ---- Main --------------------------------------------------------------
// Actors are spawned ONCE here (before the mix loop) and the shard
// ref-array is built once — re-binding an actor-ref array literal inside a
// loop is not expressible. All four mixes reuse the same actors; each mix
// re-seeds the deterministic stream so the comparison stays apples-to-
// apples across designs.
main() {
ops = ops_budget()
nk = nkeys()
ns = nshards()
println("=============================================================")
println(" Concurrent Cache Designs — Aether scaling study (#841)")
println("=============================================================")
println(" ops/design/mix : ${ops}")
println(" key space : ${nk} keys")
println(" shards (D2) : ${ns} actors")
println(" timing : os.now_monotonic_ns (monotonic clock)")
println(" note : single non-core-pinned run; see")
println(" docs/concurrent-cache-benchmark.md")
println("")
// Spawn the actor designs ONCE.
owner = spawn(SingleOwner())
s0 = spawn(Shard())
s1 = spawn(Shard())
s2 = spawn(Shard())
s3 = spawn(Shard())
s4 = spawn(Shard())
s5 = spawn(Shard())
s6 = spawn(Shard())
s7 = spawn(Shard())
shards = [s0, s1, s2, s3, s4, s5, s6, s7]
// COW: a private master map (the writer's working copy) + the cell.
master = map_new()
cell = snapshot.new(build_snapshot(master, nk))
cow_prev = null
// Pre-populate all three designs with the full key space.
i = 0
while i < nk {
k = key_at(i)
owner ! Set { key: k, val: i }
shards[shard_for(k, ns)] ! Set { key: k, val: i }
map_put_string_owned(master, k, string.from_int(i))
i = i + 1
}
// Publish the initial COW snapshot.
cow_prev = snapshot.store(cell, build_snapshot(master, nk))
wait_for_idle()
base_seed = 2463534242 & 2147483647
mix_reads = [100, 90, 50, 10]
mi = 0
while mi < 4 {
read_pct = mix_reads[mi]
seed = next_rng(base_seed + mi * 7919)
mname = "read-only (100% read)"
if read_pct == 90 { mname = "read-heavy ( 90% read)" }
if read_pct == 50 { mname = "balanced ( 50% read)" }
if read_pct == 10 { mname = "write-heavy ( 10% read)" }
println("-------------------------------------------------------------")
println(" Workload: ${mname}")
println("-------------------------------------------------------------")
// ---- Design 1: single-owner actor ----
t0 = os.now_monotonic_ns()
rng = seed
n = 0
sink = 0
while n < ops {
rng = next_rng(rng)
ki = rng % nk
rng = next_rng(rng)
roll = rng % 100
k = key_at(ki)
if roll < read_pct {
got = owner ? Get { key: k } // serializes at one mailbox
sink = sink + got
} else {
owner ! Set { key: k, val: n }
}
n = n + 1
}
wait_for_idle()
t1 = os.now_monotonic_ns()
if sink < 0 { println(" (checksum ${sink})") }
println(" single-owner actor : ${ops_per_sec(ops, t1 - t0)} ops/sec")
// ---- Design 2: sharded actor map ----
t0 = os.now_monotonic_ns()
rng = seed
n = 0
sink = 0
while n < ops {
rng = next_rng(rng)
ki = rng % nk
rng = next_rng(rng)
roll = rng % 100
k = key_at(ki)
idx = shard_for(k, ns)
if roll < read_pct {
got = shards[idx] ? Get { key: k }
sink = sink + got
} else {
shards[idx] ! Set { key: k, val: n } // distinct shards drain in parallel
}
n = n + 1
}
wait_for_idle()
t1 = os.now_monotonic_ns()
if sink < 0 { println(" (checksum ${sink})") }
println(" sharded actor map : ${ops_per_sec(ops, t1 - t0)} ops/sec")
// ---- Design 3: COW snapshot cell ----
// Reads are direct lock-free loads (no mailbox). A write updates
// the master, rebuilds + publishes a fresh snapshot, and defers
// the displaced snapshot's free by one generation (RCU).
t0 = os.now_monotonic_ns()
rng = seed
n = 0
sink = 0
while n < ops {
rng = next_rng(rng)
ki = rng % nk
rng = next_rng(rng)
roll = rng % 100
k = key_at(ki)
if roll < read_pct {
snap = snapshot.load(cell) // LOCK-FREE, no round-trip
v = map_get_raw(snap, k)
if v != null {
m, _ = string.to_int(v)
sink = sink + m
}
} else {
map_put_string_owned(master, k, string.from_int(n))
displaced = snapshot.store(cell, build_snapshot(master, nk))
if cow_prev != null {
map_free(cow_prev) // free N-1 (grace period elapsed)
}
cow_prev = displaced
}
n = n + 1
}
t1 = os.now_monotonic_ns()
if sink < 0 { println(" (checksum ${sink})") }
println(" COW snapshot cell : ${ops_per_sec(ops, t1 - t0)} ops/sec")
mi = mi + 1
}
println("")
println("PASS concurrent_cache_bench")
}