Skip to content

perf: keep the log file open across writes on a rotating rule - #373

Merged
deemar merged 1 commit into
HardySimpson:masterfrom
yegorich:perf/rotate-keep-fd
Oct 8, 2026
Merged

deemar merged 1 commit into
HardySimpson:masterfrom
yegorich:perf/rotate-keep-fd

Conversation

@yegorich

@yegorich yegorich commented Oct 1, 2026

Copy link
Copy Markdown
Contributor

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:

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.

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>
@deemar
deemar merged commit d7a7a55 into HardySimpson:master Oct 8, 2026
32 checks passed
@yegorich
yegorich deleted the perf/rotate-keep-fd branch October 8, 2026 07:19
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.

2 participants