Skip to content

demo: a TPC-C-like client, one protothread per terminal - #31

Merged
LarryRuane merged 2 commits into
masterfrom
demo-tpcc
Nov 4, 2026
Merged

LarryRuane merged 2 commits into
masterfrom
demo-tpcc

Conversation

@LarryRuane

Copy link
Copy Markdown
Owner

Adds demo/tpcc.c, modeled on the client in YDB's Making Coroutines Routine. Their original client needed 150,000 OS threads and 600 GiB. Here each terminal is a protothread running the clerk's loop, each transaction is a nested function, and each query blocks until a simulated, sharded database replies. It uses the timer, queue and lock headers, with a simulated clock.

  • -w 10000 runs 100,000 terminals on one OS thread, about 8 to 10 s of CPU and 52 MiB for 10 simulated minutes.
  • -u drops the row locks, and the consistency check reports the lost updates.
  • -H starts every terminal at once, and the peak queue depth shows the herd.

The demo also makes a case for the timer heap (227b460). Against the old sorted-list timers, -w 1000 took 38 s of CPU vs 0.56 s.

It's built with -DPROTOTHREAD_DEMOS=ON as pttpcc, and CI builds and runs it with the other demos. Checked under ASan+UBSan, PT_SIGNAL_WAKES_ALL=1, PT_NWAIT=1 and -std=gnu99.

🤖 Generated with Claude Code

https://claude.ai/code/session_01SR7v9BVsFrHiEhQBSqZmAs

LarryRuane and others added 2 commits November 3, 2026 12:29
Modeled on the client in YDB's "Making Coroutines Routine", which needed
150,000 OS threads and 600 GiB before it was rewritten with C++20
coroutines. Each terminal is a protothread written as the loop a clerk
follows: keying time, a transaction, think time. Each transaction is a
nested function, and each query inside it sends a request to a simulated
database and blocks until the reply. The database is sharded by
warehouse, each shard a queue and four worker protothreads, and
transactions hold row locks (protothread_lock.h) until they finish.

The clock is simulated: main() jumps it to the next timer deadline, so
ten minutes of 1,000 terminals take a few hundredths of a second, and a
seed reproduces a run exactly. -w 10000 runs 100,000 terminals on one OS
thread (about 8 s for ten simulated minutes, 52 MiB). -u drops the locks,
and the read-wait-write in New-Order and Payment then loses updates,
which the consistency check reports. -H starts every terminal at once.

The arguments of a pt_call(), pt_sleep() included, are evaluated again
each time the call resumes, so the district and the random durations are
stored in the context first. Passing rnd() directly released a lock on
the wrong district.

Checked with ASan and UBSan, PT_SIGNAL_WAKES_ALL=1, PT_NWAIT=1, and
-std=gnu99. Built by -DPROTOTHREAD_DEMOS=ON and run in CI with the other
demos.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01SR7v9BVsFrHiEhQBSqZmAs
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01SR7v9BVsFrHiEhQBSqZmAs
@LarryRuane
LarryRuane merged commit 565e454 into master Nov 4, 2026
31 checks passed
@LarryRuane
LarryRuane deleted the demo-tpcc branch November 4, 2026 07:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant