You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Don't pay vote Debug logging cost in normal operation #2847
Currently we spend lots of RAM allocation in Debug logs across votes. Here are some profiles of a query serving full node in its operation over 1-2 hours. We see:
addVote's StringShort calls taking 5GB of allocation (compared to the state machines total 48GB):
These are both debug line calls:
This is certainly adding notable load to the system, and its not even providing utility most of the time!
Proposal
I'm not sure what the most well abstracted way to do the logic for "if debug mode, run this log logic" is. Just wanted to point out that there is a lot were paying right now
LazySprintF allocates all the arguments as a slice unfortunately, so it is still doing heap allocations and is expensive in hotroutines or with several args.
That should fix the first issue though, your right!
Heres a screenshot for SetHasVote, sorry for not including that in OP:
We see that 5GB are in (what I assume is) allocating args inside LazySprintF. (Otherwise it may just be taking the pointer that forces it to go to heap? I really don't know)
But for the first 2 examples, we can do what we already have for block hashes, with LazyBlockHash.
In both cases it is a vote set. The logic is that we pass the vote set (reference), and only call the StringShort() if the logger produces the log message.
I agree that there are too many allocations in this logic, especially on consensus/state. Unfortunately the current logging logic allocates heap even if debug mode is not enabled.
I've played around and implemented some logic on a branch to replace that, adding a new method on the Logger called DebugOn that returns a bool if the debug mode is enabled. We have many loggers implemented on this interface and I implemented the logic only on filter logger.
And then I replaced all the logger.Debug logic in the consensus/state to check first if logger.DebugOn() then you log. It works, the logger.Debug() is not invoked and hopefully these allocations won't happen. But I didn't have time to profile it.
Not sure if we should close this issue, although @andynog's solution is a way to go. The option to move to another provider (e.g., slog) is still in the table.
Activity
cason commentedon Apr 18, 2024
I think this can do the job:
cometbft/libs/log/lazy.go
Lines 14 to 19 in d27732a
for the first issue. But apparently it is not working in the second scenario...
ValarDragon commentedon Apr 18, 2024
LazySprintF allocates all the arguments as a slice unfortunately, so it is still doing heap allocations and is expensive in hotroutines or with several args.
That should fix the first issue though, your right!
ValarDragon commentedon Apr 18, 2024
Heres a screenshot for SetHasVote, sorry for not including that in OP:

We see that 5GB are in (what I assume is) allocating args inside LazySprintF. (Otherwise it may just be taking the pointer that forces it to go to heap? I really don't know)
cason commentedon Apr 18, 2024
We would need something like
if logger.IsDebug()then produce the message. But the logger implementation does not support that...cason commentedon Apr 18, 2024
But for the first 2 examples, we can do what we already have for block hashes, with
LazyBlockHash.In both cases it is a vote set. The logic is that we pass the vote set (reference), and only call the
StringShort()if the logger produces the log message.prototype logic to only log debug output if it is on, no allocations (#…
andynog commentedon May 1, 2024
I agree that there are too many allocations in this logic, especially on
consensus/state. Unfortunately the current logging logic allocates heap even if debug mode is not enabled.I've played around and implemented some logic on a branch to replace that, adding a new method on the
LoggercalledDebugOnthat returns aboolif the debug mode is enabled. We have many loggers implemented on this interface and I implemented the logic only onfilterlogger.And then I replaced all the
logger.Debuglogic in theconsensus/stateto check firstif logger.DebugOn()then you log. It works, thelogger.Debug()is not invoked and hopefully these allocations won't happen. But I didn't have time to profile it.What do you think about this approach @ValarDragon and @cason ?
My branch can be found here and the commit is -> d6dfcf1
ValarDragon commentedon May 1, 2024
This sounds great to me, I really like it! Can you add it to these two spots:
cometbft/internal/consensus/reactor.go
Lines 1444 to 1449 in d27732a
Then can help with getting a mainnet profile for it! With the cumulative changes here. I think this will be a big performance difference!
andynog commentedon May 2, 2024
Cool, I'll look into these other places too. If you can do a profile that would be amazing to validate the performance improvement
18 remaining items
add changelog entry (#2847)
perf(logging): strip debug log messages at compile-time (#4365)
perf(logging): strip debug log messages at compile-time (#4365)
cason commentedon Oct 31, 2024
Not sure if we should close this issue, although @andynog's solution is a way to go. The option to move to another provider (e.g.,
slog) is still in the table.perf(logging): strip debug log messages at compile-time (backport #4365…
andynog commentedon Dec 12, 2024
Hi @ValarDragon, should we leave this open ? We've implemented a solution in #4365 that in certain ways is a solution to the problem.
Please let us know. Thanks !
perf(logging): strip debug log messages at compile-time (#4365)
perf(logging): strip debug log messages at compile-time (#4365)
perf(logging): strip debug log messages at compile-time (#4365)
perf(logging): strip debug log messages at compile-time (#4365)
perf(logging): strip debug log messages at compile-time (#4365)