sleep: measure the remaining time against a clock, not the kernel's remainder - #644
Closed
cyclistmass wants to merge 1 commit into
Closed
cyclistmass wants to merge 1 commit into
cyclistmass wants to merge 1 commit into
Conversation
…emainder %nanosleep slept again for the remainder the kernel wrote back on EINTR. The kernel computes that value before it runs the signal handler, so the time a thread spends blocked inside suspend_resume_handler during a GC is never subtracted. Every world stop adds its own duration to the sleep. Compute the deadline once from a monotonic clock, and on each EINTR sleep for the time still left. #_nanosleep no longer needs a rem pointer.
Member
|
It looks like this is a duplicate of #642. Is there some difference I should note? |
Contributor
Author
|
No difference. The two diffs were byte-identical, 72 lines each. I opened this That was our tooling. Our duplicate check compares message bodies inside a Keep #642. Your review is there, and I am rebasing it onto get-internal-real-time Closing this one. Sorry for the round trip. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
(sleep n)returns late while another thread allocates. On a machine thatcollects often enough I have found no bound for the overrun. This is issue #639.
The mechanism
%nanosleepcalls#_nanosleepand, onEINTR, sleeps again for the remainingtime the kernel wrote back. Each GC suspends the sleeping thread with a signal,
and
suspend_resume_handlerblocks inside that handler until the collectionfinishes. The kernel computes the remainder before it runs the handler, so the
time the thread spends blocked there is never subtracted. Every world stop adds
its own duration to the sleep, and the error grows with the collection rate.
The fix
Compute the deadline once from a clock the Lisp reads itself. On each
EINTR,sleep for the difference between that deadline and now. The kernel write-back is
never read, so
#_nanosleepnow gets a NULL rem pointer.The clock is
CLOCK_MONOTONICthrough#_clock_gettimewhere the target has it.lib/time.lispalready reads that clock the same way forcurrent-time-in-nanoseconds. On Darwin the deadline comes fromgettimeofday,a kernel import that needs no interface database entry.
I considered
clock_nanosleepwithTIMER_ABSTIMEand did not use it. Darwindoes not have it, so it would need a second loop with a different error
convention for one target. This form is one loop for every target.
The Darwin check goes with the code it guarded. That check read a remainder the
kernel computes as negative and then widens to 64 bits, and nothing reads that
value now.
Cost: one
clock_gettimeper%nanosleepcall, and one more per interruption.process-waitpolls through%nanosleeponce per tick, so this is noise there.Red then green
The control that makes the red mean something is the allocation rate, and it
is flat. On the arm64 pair it is 38,978 allocations per second red against
38,515 green. The red half allocated 18 times more because it ran 18 times
longer, not because the machine changed. Both boxes are burstable 2 vCPU
instances, and a throttled box reproduces this more weakly rather than more
strongly, so these numbers are floors.
What I measured, and what I did not
The two linuxx8664 halves come from
6526e21c. I re-measured the linuxarm64 redat
27073918, and that pair differs in exactly one file, the boot image: samekernel binary, same 86 fasls, same image inputs, and the probe reports
%nanosleep-clockabsent from the red image and present in the green one.I have not measured this on Darwin or on Windows.
%nanosleepis defined onlyfor non-Windows targets, and every one of them shares this body, so the change
is not per-port. Windows does not use this function.
The reproducer is
extended-tests/threads/sleep-vs-alloc.lispinClozure/ccl-tests#10. It must use the value it allocates, or the compiler removes
the allocation and the sleep returns on time.