atap 0.1.0

Threadsafe futureless async runtime for macOS
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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
//! # Benchmark
//! What each kind of task costs as more of them run at once: the
//! cpu it takes, the memory the process peaks at while they run,
//! and how long from the first spawn to the last join
//!
//! Every number is process wide, so it all runs in one test, in
//! order, and the binary holds nothing else:
//!
//! `cargo test --release --features tls --test benchmark -- --ignored --nocapture`

mod common;

use atap::{
    Runtime, Task, TaskHandle,
    builder::{HandleKind, Standalone},
    channel::Channel,
    compute::Compute,
    fs::File,
    process::Process,
    signal::{Signal, SignalKind},
    sleep::Sleep,
    tcp::Tcp,
    udp::Udp,
    unix::Unix,
};
use common::{
    Resources, TestPath, cpu_time, footprint, mebibytes, raise_descriptor_limit, send_signal,
};
use std::{
    hint::black_box,
    sync::{
        Arc,
        atomic::{AtomicBool, AtomicU64, Ordering},
    },
    thread::{self, JoinHandle},
    time::{Duration, Instant},
};

/// Tasks each row runs at once
const COUNTS: [usize; 6] = [0, 1, 100, 1_000, 10_000, 100_000];

/// How often the memory is read while a row runs
const SAMPLE: Duration = Duration::from_millis(5);

/// How long the process is given to settle before a row is
/// measured
const SETTLE: Duration = Duration::from_millis(400);

/// How long a row waits for a task that ought to have settled
const PATIENCE: Duration = Duration::from_secs(60);

/// Every table, in order
#[test]
#[ignore]
fn benchmark() {
    Runtime::init().expect("the runtime starts");

    println!("\natap benchmark");
    println!("  {}", Resources::now());
    println!(
        "  {} cores, {} descriptors",
        common::cores(),
        raise_descriptor_limit(),
    );
    println!("  cpu is the share of one core the row used, so 800 is every core busy");
    println!("  memory is the peak the process reached while the row ran");
    println!("  a signal or a watch row also waits for every task to be watching");
    println!("  the pool is woken before each row, so none of them start from idle");

    computes();
    sleeps();
    channels();
    files();
    signals();
    watches();
    datagrams();
    pipes();
    connections();
    streams();
    tls();
    children();

    println!("\n  {}", Resources::now());
}

/// Closures run on a worker
fn computes() {
    table(
        "Compute",
        100_000,
        "",
        20_000,
        || {
            black_box(blocked(Compute::compute(|()| black_box(1u64))));
        },
        |count| {
        let handles: Vec<_> = (0..count)
            .map(|value| Runtime::task(Compute::compute(move |()| value * 2)).spawn())
            .collect();

        for handle in handles {
            handle.join().expect("every compute finishes");
        }
    });
}

/// Waits of both kinds: one long enough to be handed to a sleep
/// thread, and one short enough to be spun on a worker
fn sleeps() {
    table(
        "Sleep 1ms",
        100_000,
        "",
        200,
        || {
            black_box(blocked(Sleep::sleep(Duration::from_millis(1))));
        },
        |count| {
        let handles: Vec<_> = (0..count)
            .map(|_| Runtime::task(Sleep::sleep(Duration::from_millis(1))).spawn())
            .collect();

        for handle in handles {
            handle.join().expect("every sleep finishes");
        }
    });

    table(
        "Sleep 400us",
        100_000,
        "",
        500,
        || {
            black_box(blocked(Sleep::sleep(Duration::from_micros(400))));
        },
        |count| {
        let handles: Vec<_> = (0..count)
            .map(|_| Runtime::task(Sleep::sleep(Duration::from_micros(400))).spawn())
            .collect();

        for handle in handles {
            handle.join().expect("every sleep finishes");
        }
    });
}

/// Receives parked on one channel, woken by a value each
fn channels() {
    let (ready, taking) = Channel::new::<u64>().open().expect("a channel opens");

    table(
        "Channel recv",
        100_000,
        "",
        20_000,
        || {
            ready.send(1).expect("the send lands");
            black_box(blocked(taking.recv())).expect("the value is there");
        },
        |count| {
        let (tx, rx) = Channel::new::<u64>().open().expect("a channel opens");

        let handles: Vec<_> = (0..count)
            .map(|_| Runtime::task(rx.recv()).spawn())
            .collect();

        for value in 0..count as u64 {
            tx.send(value).expect("the send lands");
        }

        for handle in handles {
            handle
                .join()
                .expect("every receive settles")
                .expect("every receive gets a value");
        }
    });
}

/// Reads of one small file, each opening it again
fn files() {
    let path = TestPath::new("benchmark");

    Runtime::block(File::write(path.path(), vec![b'x'; 4 * 1024])).expect("the write works");

    table(
        "File read 4KiB",
        100_000,
        "",
        2_000,
        || {
            black_box(blocked(File::read(path.path()))).expect("the read works");
        },
        |count| {
        let handles: Vec<_> = (0..count)
            .map(|_| Runtime::task(File::read(path.path())).spawn())
            .collect();

        for handle in handles {
            handle
                .join()
                .expect("every read settles")
                .expect("every read works");
        }
    });
}

/// Waits for one signal, all woken by a single delivery
fn signals() {
    const KIND: SignalKind = SignalKind::User1;

    table(
        "Signal wait",
        10_000,
        "every wait puts its own filter on the one delivery",
        0,
        || {},
        |count| {
        let handles: Vec<_> = (0..count)
            .map(|_| Runtime::task(Signal::wait(KIND)).spawn())
            .collect();

        // A wait only sees a delivery that lands once it is
        // watching, so they all have to be up first
        watching(&handles);

        for _ in 0..count.min(1) {
            send_signal(KIND);
        }

        for handle in handles {
            handle
                .join_with_timeout(PATIENCE)
                .expect("every wait settles")
                .expect("every wait sees the signal");
        }
    });
}

/// Watches of one file, all woken by a single write
fn watches() {
    let path = TestPath::new("benchmark-watch");

    Runtime::block(File::write(path.path(), b"start".as_slice())).expect("the write works");

    table(
        "File watch",
        10_000,
        "every watch puts its own filter on the one change",
        0,
        || {},
        |count| {
        let handles: Vec<_> = (0..count)
            .map(|_| Runtime::task(File::watch(path.path())).spawn())
            .collect();

        // The same: a watch only sees a change made once it is on
        watching(&handles);

        for _ in 0..count.min(1) {
            Runtime::block(File::write(path.path(), b"again".as_slice())).expect("the write works");
        }

        for handle in handles {
            handle
                .join_with_timeout(PATIENCE)
                .expect("every watch settles")
                .expect("every watch sees the change");
        }
    });
}

/// Datagrams sent from one socket
fn datagrams() {
    let socket = Runtime::block(Udp::bind("127.0.0.1:0")).expect("a socket binds");
    let to = Runtime::block(Udp::bind("127.0.0.1:0")).expect("a socket binds");
    let address = to.local_addr();

    table(
        "Udp send",
        100_000,
        "",
        20_000,
        || {
            black_box(blocked(socket.send_to(address, b"hello".as_slice()))).expect("the send works");
        },
        |count| {
        let handles: Vec<_> = (0..count)
            .map(|_| Runtime::task(socket.send_to(address, b"hello".as_slice())).spawn())
            .collect();

        for handle in handles {
            handle
                .join()
                .expect("every send settles")
                .expect("every send works");
        }
    });
}

/// Sends down one Unix socket pair, drained at the far end
fn pipes() {
    let (near, far) = Runtime::block(Unix::pair()).expect("a socket pair opens");

    table(
        "Unix send",
        10_000,
        "the pair's buffer bounds it, not the runtime",
        // Kept well under the pair's buffer, since nothing drains
        // the far end while this runs
        200,
        || {
            black_box(blocked(near.send(b"hello".as_slice()))).expect("the send works");
        },
        |count| {
        let reading = far.clone();

        let drain = thread::spawn(move || {
            let mut left = count * 5;

            while left > 0 {
                match Runtime::block(reading.recv(64 * 1024)) {
                    Ok(bytes) if !bytes.is_empty() => {
                        left = left.saturating_sub(bytes.len())
                    }
                    _ => break,
                }
            }
        });

        let handles: Vec<_> = (0..count)
            .map(|_| Runtime::task(near.send(b"hello".as_slice())).spawn())
            .collect();

        for handle in handles {
            handle
                .join()
                .expect("every send settles")
                .expect("every send works");
        }

        drain.join().expect("the far end keeps up");
    });
}

/// Loopback TCP connections, each opened and closed
fn connections() {
    let listener = Runtime::block(Tcp::listen("127.0.0.1:0")).expect("a listener opens");
    let address = listener.local_addr();

    table(
        "Tcp connect",
        100,
        "the kernel's listen backlog is 128, and the rest are reset",
        100,
        || {
            // The backlog holds these, so no accept has to be posted
            black_box(blocked(Tcp::connect(address))).expect("the connect works");
        },
        |count| {
        let accepting: Vec<_> = (0..count)
            .map(|_| Runtime::task(listener.accept()).spawn())
            .collect();

        let connecting: Vec<_> = (0..count)
            .map(|_| Runtime::task(Tcp::connect(address)).spawn())
            .collect();

        for handle in connecting {
            handle
                .join()
                .expect("every connect settles")
                .expect("every connect works");
        }

        for handle in accepting {
            handle
                .join()
                .expect("every accept settles")
                .expect("every accept works");
        }
    });
}

/// Sends down one loopback connection, drained at the far end
fn streams() {
    let listener = Runtime::block(Tcp::listen("127.0.0.1:0")).expect("a listener opens");
    let accepting = Runtime::task(listener.accept()).spawn();
    let near = Runtime::block(Tcp::connect(listener.local_addr())).expect("the connect works");
    let (far, _) = accepting
        .join()
        .expect("the accept settles")
        .expect("the accept works");

    table(
        "Tcp send",
        10_000,
        "the socket's buffer bounds it, not the runtime",
        2_000,
        || {
            black_box(blocked(near.send(b"hello".as_slice()))).expect("the send works");
        },
        |count| {
            let reading = far.clone();

            let drain = thread::spawn(move || {
                let mut left = count * 5;

                while left > 0 {
                    match Runtime::block(reading.recv(64 * 1024)) {
                        Ok(bytes) if !bytes.is_empty() => {
                            left = left.saturating_sub(bytes.len())
                        }
                        _ => break,
                    }
                }
            });

            let handles: Vec<_> = (0..count)
                .map(|_| Runtime::task(near.send(b"hello".as_slice())).spawn())
                .collect();

            for handle in handles {
                handle
                    .join()
                    .expect("every send settles")
                    .expect("every send works");
            }

            drain.join().expect("the far end keeps up");
        },
    );
}

/// The same, with a handshake on top
#[cfg(feature = "tls")]
fn tls() {
    use atap::tls::Tls;

    let certs = common::certs("benchmark");
    let listener = Runtime::block(Tls::listen("127.0.0.1:0", &certs.cert, &certs.key))
        .expect("a TLS listener opens");
    let address = listener.local_addr();

    table(
        "Tls connect",
        100,
        "the kernel's listen backlog is 128, and the rest are reset",
        0,
        || {},
        |count| {
        let accepting: Vec<_> = (0..count)
            .map(|_| Runtime::task(listener.accept()).spawn())
            .collect();

        let connecting: Vec<_> = (0..count)
            .map(|_| {
                Runtime::task(
                    Tls::connect(address)
                        .server_name("localhost")
                        .trust(certs.ca.as_bytes()),
                )
                .spawn()
            })
            .collect();

        for handle in connecting {
            handle
                .join()
                .expect("every connect settles")
                .expect("every connect works");
        }

        for handle in accepting {
            handle
                .join()
                .expect("every accept settles")
                .expect("every accept works");
        }
    });
}

/// Nothing to measure without the feature
#[cfg(not(feature = "tls"))]
fn tls() {
    println!("\nTask: Tls connect");
    println!("  not built, run with --features tls");
}

/// Other programs run and waited for
fn children() {
    table(
        "Process run",
        100,
        "each one is a program of its own",
        100,
        || {
            black_box(blocked(Process::run("/usr/bin/true", Process::NO_ARGS))).expect("true runs");
        },
        |count| {
        let handles: Vec<_> = (0..count)
            .map(|_| Runtime::task(Process::run("/usr/bin/true", Process::NO_ARGS)).spawn())
            .collect();

        for handle in handles {
            handle
                .join()
                .expect("every run settles")
                .expect("every run works");
        }
    });
}

/// Prints one kind of task's table
///
/// `run` spawns that many tasks and waits for every one of them.
/// Counts past `most` are left out, and `why` says what stops
/// them
fn table(
    kind: &str,
    most: usize,
    why: &str,
    blocked_runs: usize,
    blocking: impl Fn(),
    run: impl Fn(usize),
) {
    println!("\nTask: {kind}");
    println!(
        "  {:>7} | {:>9} | {:>11} | {:>12} | {:>11}",
        "count", "cpu", "memory", "time", "each",
    );

    // The same work on the calling thread, with none of the
    // scheduling around it
    if blocked_runs > 0 {
        thread::sleep(SETTLE);

        blocking();

        let before = cpu_time();
        let started = Instant::now();

        for _ in 0..blocked_runs {
            blocking();
        }

        let time = started.elapsed();
        let cpu = cpu_time().saturating_sub(before);

        println!(
            "  {:>7} | {:>9} | {:>11} | {:>12} | {:>11}",
            "blocked",
            share(blocked_runs, cpu, time),
            "-",
            format!("{time:.2?}"),
            each(blocked_runs, time),
        );
    }

    for count in COUNTS {
        if count > most {
            println!(
                "  {count:>7} | {:>9} | {:>11} | {:>12} | {:>11}",
                "-", "-", "-", "-",
            );
            continue;
        }

        // Whatever the last row left behind is given time to go, so
        // the reading belongs to this one
        thread::sleep(SETTLE);

        // One task first, so the row isn't charged for waking a
        // pool the settle put to sleep
        Runtime::task(Compute::compute(|()| 0u64))
            .spawn()
            .join()
            .expect("the pool is awake");

        let watching = Watcher::start();
        let started = Instant::now();

        run(count);

        let time = started.elapsed();
        let (cpu, peak) = watching.stop();

        println!(
            "  {count:>7} | {:>9} | {:>7.1} MiB | {:>12} | {:>11}",
            share(count, cpu, time),
            mebibytes(peak),
            format!("{time:.2?}"),
            each(count, time),
        );
    }

    if most < COUNTS[COUNTS.len() - 1] {
        println!("  past {most}: {why}");
    }
}

/// Waits until every one of these tasks has started
///
/// What they all settle on is one event, so anything still queued
/// would miss it
fn watching<T, W>(handles: &[TaskHandle<T, W>])
where
    W: HandleKind,
{
    if handles.is_empty() {
        return;
    }

    while handles.iter().any(TaskHandle::is_pending) {
        thread::yield_now();
    }

    // Long enough for the last of them to reach its park
    thread::sleep(Duration::from_millis(100));
}

/// The cpu the row used as a share of one core, as text
///
/// 100 is one core busy for the whole row, so a row on eight cores
/// can read up to 800
fn share(count: usize, cpu: Duration, time: Duration) -> String {
    // A row with nothing in it, or one over in a moment, is done
    // before a share means anything
    if count == 0 || time < Duration::from_micros(100) {
        return String::from("-");
    }

    format!("{:.0} %", cpu.as_secs_f64() / time.as_secs_f64() * 100.0)
}

/// Runs one task on the calling thread
///
/// Kept out of line, since a task the compiler can fold into its
/// caller leaves nothing of the call to measure
#[inline(never)]
fn blocked<F>(task: F) -> F::Output
where
    F: Task,
    F::Input: Standalone,
{
    Runtime::block(task)
}

/// How long one task took, as text
fn each(count: usize, time: Duration) -> String {
    if count == 0 {
        return String::from("-");
    }

    let nanos = time.as_secs_f64() * 1e9 / count as f64;

    match nanos < 1000.0 {
        true => format!("{nanos:.0} ns"),
        false => format!("{:.2} us", nanos / 1000.0),
    }
}

/// Reads what the process is using while a row runs
struct Watcher {
    /// Set once the row is over
    done: Arc<AtomicBool>,

    /// The most memory seen, in bytes
    peak: Arc<AtomicU64>,

    /// The thread doing the reading
    reader: JoinHandle<()>,

    /// What the process had used when the row started
    before: Resources,
}

impl Watcher {
    /// Starts reading
    ///
    /// The thread is up and has taken its first reading before the
    /// row's own cpu is counted, so starting it isn't charged to
    /// the row
    fn start() -> Self {
        let done = Arc::new(AtomicBool::new(false));
        let peak = Arc::new(AtomicU64::new(0));
        let up = Arc::new(AtomicBool::new(false));

        let stopping = Arc::clone(&done);
        let highest = Arc::clone(&peak);
        let running = Arc::clone(&up);

        let reader = thread::spawn(move || {
            highest.fetch_max(footprint(), Ordering::Relaxed);
            running.store(true, Ordering::Release);

            while !stopping.load(Ordering::Relaxed) {
                thread::sleep(SAMPLE);

                highest.fetch_max(footprint(), Ordering::Relaxed);
            }
        });

        while !up.load(Ordering::Acquire) {
            thread::yield_now();
        }

        Self {
            done,
            peak,
            reader,
            before: Resources::now(),
        }
    }

    /// Stops reading
    ///
    /// ## Returns
    /// The cpu the row was charged, and the most memory the process
    /// held while it ran
    fn stop(self) -> (Duration, u64) {
        self.done.store(true, Ordering::Relaxed);
        self.reader.join().expect("the reader stops");

        let cpu = Resources::now().cpu.saturating_sub(self.before.cpu);

        (cpu, self.peak.load(Ordering::Relaxed))
    }
}