Repository navigation
perf: keep the log file open across writes on a rotating rule - #373
Merged
Merged
Conversation
A rule with a size limit opened the file, wrote one line, closed it and stat'ed the path, for every message: four syscalls where two would do, and the "frequent open, close and stat" that HardySimpson#136 measured as kernel time. Counted with strace over 20000 messages: before openat 20016 write 20000 newfstatat 20017 close 20006 after openat 16 write 20000 newfstatat 20017 close 6 and 200000 messages through a rotating rule went from 0.508s to 0.283s. The descriptor is kept, as the consumer path already kept its own, and the single stat now answers both questions it needs: the inode says whether the file at the path is still the one the rule holds -- which a rotation, ours or logrotate's, changes -- and the size says whether this message is the one that takes it over archive_max_size. The rotation threshold is computed exactly as before, so the archives come out byte for byte the same: 4848 bytes live and 10224 per archive over the same run, before and after. Reopening goes through dup2() rather than close() and open(). The rule is shared by every thread logging through it, and a close() would free the number for the next open() anywhere in the process while another thread sits between its stat() and its write(). dup2() swaps the description under the same number in one step. That also closes the same hole in the non-rotating path, which has had the close-then-open sequence all along. static_fd, static_dev and static_ino become atomics: with the descriptor kept, several threads reach the reopen together, and ThreadSanitizer flagged the plain writes as a race under six threads rotating a 64KB file. Relaxed ordering is enough -- a thread reading a stale inode reopens once too often, which dup2() makes harmless, or once too late, which the next message catches. dev_t and ino_t are held as atomic_ullong because the _Atomic qualifier is C11 and this builds as C99 with -pedantic. rotate_fd goes away: it was the consumer's version of the same descriptor. test_rotate_reopen covers what the caching puts at risk -- the rule's own rotation, and an external rename with zlog still running. Six threads writing 18000 lines through a rotating rule come out complete, in order within each thread, with nothing malformed and no duplicates, and clean under ThreadSanitizer. 39/39 tests pass in Debug, 36/36 in Release, 38/38 under Tsan. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
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.
A rule with a size limit opened the file, wrote one line, closed it and stat'ed the path, for every message: four syscalls where two would do, and the "frequent open, close and stat" that #136 measured as kernel time. Counted with strace over 20000 messages:
and 200000 messages through a rotating rule went from 0.508s to 0.283s.
The descriptor is kept, as the consumer path already kept its own, and the single stat now answers both questions it needs: the inode says whether the file at the path is still the one the rule holds -- which a rotation, ours or logrotate's, changes -- and the size says whether this message is the one that takes it over archive_max_size. The rotation threshold is computed exactly as before, so the archives come out byte for byte the same: 4848 bytes live and 10224 per archive over the same run, before and after.
Reopening goes through dup2() rather than close() and open(). The rule is shared by every thread logging through it, and a close() would free the number for the next open() anywhere in the process while another thread sits between its stat() and its write(). dup2() swaps the description under the same number in one step. That also closes the same hole in the non-rotating path, which has had the close-then-open sequence all along.
static_fd, static_dev and static_ino become atomics: with the descriptor kept, several threads reach the reopen together, and ThreadSanitizer flagged the plain writes as a race under six threads rotating a 64KB file. Relaxed ordering is enough -- a thread reading a stale inode reopens once too often, which dup2() makes harmless, or once too late, which the next message catches. dev_t and ino_t are held as atomic_ullong because the _Atomic qualifier is C11 and this builds as C99 with -pedantic.
rotate_fd goes away: it was the consumer's version of the same descriptor.
test_rotate_reopen covers what the caching puts at risk -- the rule's own rotation, and an external rename with zlog still running. Six threads writing 18000 lines through a rotating rule come out complete, in order within each thread, with nothing malformed and no duplicates, and clean under ThreadSanitizer. 39/39 tests pass in Debug, 36/36 in Release, 38/38 under Tsan.