Skip to content

Don't pay vote Debug logging cost in normal operation #2847

Open

Description

@ValarDragon

Feature Request

Summary

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):
Screenshot from 2024-04-18 20-55-22
These are both debug line calls:

Peerstate.setHasVote taking 15.4GB, this is entirely in debug logging:

ps.logger.Debug("setHasVote",
"peerH/R",
log.NewLazySprintf("%d/%d", ps.PRS.Height, ps.PRS.Round),
"H/R",
log.NewLazySprintf("%d/%d", height, round),
"type", voteType, "index", index)

Problem Definition

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

Activity

  1. added
    enhancement
    needs-triage
    on Apr 18, 2024
  2. cason commented on Apr 18, 2024

    @cason

    I think this can do the job:

    // NewLazySprintf defers fmt.Sprintf until the Stringer interface is invoked.
    // This is particularly useful for avoiding calling Sprintf when debugging is not
    // active.
    func NewLazySprintf(format string, args ...any) *LazySprintf {
    return &LazySprintf{format, args}
    }

    for the first issue. But apparently it is not working in the second scenario...

  3. added
    logs
    on Apr 18, 2024
  4. ValarDragon commented on Apr 18, 2024

    @ValarDragon
    ContributorAuthor

    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!

  5. ValarDragon commented on Apr 18, 2024

    @ValarDragon
    ContributorAuthor

    Heres a screenshot for SetHasVote, sorry for not including that in OP:
    image

    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)

  6. cason commented on Apr 18, 2024

    @cason

    We would need something like if logger.IsDebug() then produce the message. But the logger implementation does not support that...

  7. cason commented on Apr 18, 2024

    @cason

    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.

  8. andynog commented on May 1, 2024

    @andynog
    Contributor

    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.

    What do you think about this approach @ValarDragon and @cason ?

    My branch can be found here and the commit is -> d6dfcf1

  9. ValarDragon commented on May 1, 2024

    @ValarDragon
    ContributorAuthor

    This sounds great to me, I really like it! Can you add it to these two spots:

    Then can help with getting a mainnet profile for it! With the cumulative changes here. I think this will be a big performance difference!

  10. andynog commented on May 2, 2024

    @andynog
    Contributor

    Cool, I'll look into these other places too. If you can do a profile that would be amazing to validate the performance improvement

  11. 18 remaining items

  12. added a commit that references this issue on Oct 29, 2024
  13. added a commit that references this issue on Oct 31, 2024
    51e9948
  14. moved this from In Progress to Done in CometBFTon Oct 31, 2024
  15. added a commit that references this issue on Oct 31, 2024
    38d382a
  16. cason commented on Oct 31, 2024

    @cason

    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.

  17. reopened this on Oct 31, 2024
  18. added a commit that references this issue on Oct 31, 2024
    a02eea3
  19. removed their assignment
    on Dec 12, 2024
  20. andynog commented on Dec 12, 2024

    @andynog
    Contributor

    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 !

  21. added 5 commits that reference this issue on Sep 9, 2025
    272be73
    d766ae2
    6fe37b7
    1cb4eab
    f8dcf1a
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    consensusenhancementlogs

    Type

    No type

    Projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions