Skip to main content

Module counted

Module counted 

Source
Expand description

The counting #[global_allocator] of the memory tests (plan section 7.3), and the thread it counts: only the thread under test.

libtest runs a test on a thread of its own while its main thread goes on allocating (just after the spawn, and for its “has been running for over 60 seconds” notice), so an allocator that counts every thread reads such an allocation inside a measured window as the test’s own, and a check with no slack fails now and then. The core starts no thread of its own (crate::waits), so the test’s thread sees every allocation the core makes for it. A test’s first line is count_this_thread, then others_are_not_counted with the allocator’s count.

Allocator is every such allocator: a test file installs one with a Tally of its own, which says what a block that became live or was freed does to the file’s counts. Only the allocator decides what happened: a block counts once System has made it, so a failed allocation or realloc counts nothing and the block a failed realloc leaves stays counted (review round 4: seven test files’ copies counted the new block before realloc and freed the old one whatever it returned). stray_allocators finds a global allocator of another kind.

Owner: package B.

Structs§

Allocator
A global allocator that forwards to System and tells its Tally of each block made or freed on a counted thread: of a request before System sees it (Tally::growing, undone by Tally::failed), and of its block once System has answered: a failure counts nothing, and a realloc that fails leaves its old block live and counted. A growing realloc that succeeds counts its new block before it frees the old one, the worst case of a moving realloc: both blocks live at once. A shrinking one frees the old block first, since glibc shrinks in place, unless System allocates, copies and frees instead (an alignment over malloc’s, or a new size below the alignment), which counts as a growth does: the engine’s allocator’s rule (unityrs_batch::alloc). A tally that panics aborts the process (Tally). A test file installs it with its own tally: #[global_allocator] static ALLOC: Allocator<Mine> = Allocator(Mine);.

Traits§

Tally
What a test file counts of the blocks its Allocator sees. It is called inside the allocator, so it must not allocate, and it must not panic: a global allocator that unwinds is undefined behaviour (GlobalAlloc’s safety contract), so Allocator ends any unwind out of a tally’s method by aborting the process (review round 6: a tally that panicked unwound through the allocator). A failed assert! or unwrap in a tally is therefore a SIGABRT, after the panic’s message, and never a test failure that unwinds.

Functions§

count_this_thread
Counts the calling thread’s allocations from now on.
counted
Whether the calling thread’s allocations are counted. It allocates nothing, so a global allocator may ask it.
others_are_not_counted
Checks that an allocation on another thread does not move live, the allocator’s count: a second thread allocates 64 KiB and holds it while the calling thread, which allocates nothing meanwhile, reads live before and after. Run it before the test takes any baseline: the second thread’s start and end may leave a constant in the count (a block the calling thread allocated for it and the other thread freed).
stop_counting_this_thread
Stops counting the calling thread’s allocations. A registered thread calls it before it ends: its exit’s frees (its thread-locals’ destructors, its handle) come after thread::scope has joined it, and counted they would land in whatever window a later test has armed.
stray_allocators
Every file:line of a .rs file under root (target directories left out) that implements GlobalAlloc itself, outside this module: a counting allocator of its own, whose accounting no test here checks. The engine’s (unityrs_batch::alloc, the binary’s) is the one other, its accounting held by its own tests, and its file is left out for that one implementation only: a count there other than one is reported as <file>: <n> implementations, the exemption is for one. Comment lines are not scanned.