8000 log: expand BCLog::LogFlags (categories) to 64 bits by LarryRuane · Pull Request #26619 · bitcoin/bitcoin · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

log: expand BCLog::LogFlags (categories) to 64 bits #26619

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 3, 2024

Conversation

LarryRuane
Copy link
Contributor
@LarryRuane LarryRuane commented Dec 1, 2022

Increase the maximum number of logging categories from 32 to 64.

We're currently using 29 of the 32 available logging categories (there are only 3 remaining). It would be good to increase the limit soon; the fourth PR to be merged that adds a new logging category will be blocked until something like this is done.

@DrahtBot
Copy link
Contributor
DrahtBot commented Dec 1, 2022

The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

Code Coverage

For detailed information about the code coverage, see the test coverage report.

Reviews

See the guideline for information on the review process.

Type Reviewers
ACK vasild, ryanofsky, theStack, achow101
Approach ACK stickies-v

If your review is incorrectly listed, please react with 👎 to this comment and the bot will ignore it on the next update.

Conflicts

Reviewers, this pull request conflicts with the following ones:

  • #30595 (kernel: Introduce initial C header API by TheCharlatan)
  • #30315 (Stratum v2 Transport by Sjors)
  • #29432 (Stratum v2 Template Provider (take 3) by Sjors)
  • #29415 (Broadcast own transactions only via short-lived Tor or I2P connections by vasild)
  • #29346 (Stratum v2 Noise Protocol by Sjors)
  • #26697 (logging: use bitset for categories by LarryRuane)

If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first.

Copy link
Contributor
@stickies-v stickies-v left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Concept ACK - makes sense to expand this now

There are still a couple of places that use uint32_t, e.g. here

uint32_t GetCategoryMask() const { return m_categories.load(); }
and callsites of GetCategoryMask() (and possibly further upstream)

@LarryRuane LarryRuane force-pushed the 2022-11-log-categories-64 branch from c91cfa8 to fe99f68 Compare December 1, 2022 20:25
@LarryRuane
Copy link
Contributor Author

Force-pushed to address review comments -- thanks, @stickies-v!

One further change this PR could make is to modify all the existing BCLog::LogFlags definitions as follows:

-        NET         = (1 <<  0),
+        NET         = (1ULL <<  0),

More lines would change, but greater consistency. Any opinions?

Copy link
Contributor
@theStack theStack left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Concept ACK

@stickies-v
Copy link
Contributor

More lines would change, but greater consistency. Any opinions?

I wouldn't, there's not really a connection with this PR and the lines of code aren't touched.

Copy link
Contributor
@stickies-v stickies-v left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approach ACK fe99f68

I can't make up my mind if I think the LOGTEST flag is helpful. PR states it helps unearth wrong 32bits assumptions, but... we only use it in a single function call (LogPrint) - so, does it really? Atm I'm leaning towards preferring leaving it out.

Otherwise, everything looks good to me with some basic testing.

@luke-jr
Copy link
Member
luke-jr commented Dec 7, 2022

What's the benefit from growing it prematurely?

Maybe there should be a typedef? (Been thinking we should have a blockheight_t or such too, but no real benefit to the refactor)

@stickies-v
Copy link
Contributor

What's the benefit from growing it prematurely?

If we have multiple PRs open that each need to add their own new log category, it would be unclear which PR is to eventually exceed the limit and/or multiple PRs would have to implement this upgrade independently and then rebase. It makes sense to me do to this in advance, and I think we're close enough to the limit to go ahead? Could probably wait for another category to be added first too, but... since it's already implemented and we'll need it soon anyway, I think it's best to not waste more review/dev time by letting this go stale.

Maybe there should be a typedef?

That seems sensible. I don't think we'll need to replace the uint64_t anytime soon, but we're changing all the types now so might as well include that.

@LarryRuane
Copy link
Contributor Author

but we're changing all the types now so might as well include that

Just so I understand, is the suggestion (for this PR) to add a typedef rather than using uint64_t directly? I'd be happy to do that, and I think you're right, we can keep this PR smaller by eliminating the test stuff, I could do that as well. Please give this comment a thumbs up (or down) to let me know, thanks.

@LarryRuane LarryRuane force-pushed the 2022-11-log-categories-64 branch from fe99f68 to adcb4a8 Compare December 9, 2022 05:37
@LarryRuane
Copy link
Contributor Author

Force-pushed to remove the test; I agree it doesn't add enough value for the additional complexity.

Maybe there should be a typedef?

I don't think we'll need to replace the uint64_t anytime soon, but we're changing all the types now so might as well include that.

I'm now leaning toward keeping this patch as simple as possible, just the straightforward change from uint32_t to uint64_t. If we introduce a new typedef, then if we ever need more than 64 logging categories, we won't be able to just change that one typedef because there's no standard uint128_t, so we'd need bigger changes anyway.

8000
@luke-jr
Copy link
Member
luke-jr commented Dec 9, 2022

If we introduce a new typedef, then if we ever need more than 64 logging categories, we won't be able to just change that one typedef because there's no standard uint128_t, so we'd need bigger changes anyway.

We'd be able to introduce a class and change the typedef to that. Perhaps similar to QFlags.

But actually, it doesn't really make sense for LogFlags to be allocated by bit at all. The only time we need to indicate multiple is the configuration for logging them, right? That could easily be a std::bitset.

@LarryRuane
Copy link
Contributor Author

But actually, it doesn't really make sense for LogFlags to be allocated by bit at all. The only time we need to indicate multiple is the configuration for logging them, right? That could easily be a std::bitset.

That's an excellent suggestion; std::bitset looks perfect for this. Would you favor changing this PR to implement that? Or maybe do this simple one now, and then do a follow-up?

@luke-jr
Copy link
Member
luke-jr commented Dec 9, 2022

I think there's no urgent need for >32 bits, so might as well go straight to bitset. Perhaps close this and open a new PR for it, to avoid confusion?

@LarryRuane
Copy link
Contributor Author

I think there's no urgent need for >32 bits, so might as well go straight to bitset. Perhaps close this and open a new PR for it, to avoid confusion?

I just created #26697 as a replacement for this PR, closing. If someone can think of a reason this PR would be better than #26697, leave a comment here and I'll reopen it.

@LarryRuane LarryRuane closed this Dec 14, 2022
@LarryRuane
Copy link
Contributor Author

Some additional historical context: #9424 (comment)

@LarryRuane
Copy link
Contributor Author
LarryRuane commented Jan 23, 2023

I closed the current PR because #26697 was intended to replace it, but the current PR may be preferable after all, so I'm now reopening it.

@LarryRuane LarryRuane reopened this Jan 23, 2023
@LarryRuane LarryRuane force-pushed the 2022-11-log-categories-64 branch from adcb4a8 to 90c3d90 Compare January 23, 2023 00:19
@LarryRuane
Copy link
Contributor Author

Force pushed to make the enum LogFlags literals' type match the enum type, uint64_t (instead of uint32_t). This isn't necessary unless the shift amount is 32 or more, but this way all these constants will be defined consistently.

@fanquake
Copy link
Member

but the current PR may be preferable after all

Looks like the concerns expressed in #26697, which were reason for reopening here, are no-longer valid? Going to reclose this.

@fanquake fanquake closed this Mar 20, 2023
achow101 added a commit that referenced this pull request Mar 23, 2023
8c47d59 doc: improve -debuglogfile help to be a bit clearer (jonatack)
20d89d6 bench: document expected results in logging benchmarks (jonatack)
d8deba8 bench: add LogPrintfCategory and LogPrintLevel benchmarks (Jon Atack)
102b203 bench: order the logging benchmark code by output (Jon Atack)
4b3fdbf bench: update logging benchmark naming for clarity (Jon Atack)
4684aa8 bench: allow logging benchmarks to be order-independent (Larry Ruane)

Pull request description:

  Update our logging benchmarks for evaluating ongoing work like #25203 and refactoring proposals like #26619 and #26697.

  - make the logging benchmarks order-independent (Larry Ruane)
  - add missing benchmarks for the `LogPrintLevel` and `LogPrintfCategory` macros that our logging is migrating to; at some later point it should be feasible to drop some of the previous logging benchmarks
  - update the logging benchmark naming to be clear which benchmark corresponds to which log macro, and update the ordering to be the same as the output
  - add clarifying documentation to the logging benchmarks
  - improve the `-debuglogfile` config option help to be clearer; can be tested by running `./src/bitcoind -help | grep -A4 '\-debuglogfile'`

  Reviewers can run the logging benchmarks with:
  ```bash
  ./src/bench/bench_bitcoin -filter='LogP*.*'
  ```

ACKs for top commit:
  LarryRuane:
    ACK 8c47d59
  martinus:
    code review & tested ACK 8c47d59, here are my benchmark results:
  achow101:
    ACK 8c47d59

Tree-SHA512: 705f8720c9ceaf14a1945039c7578a0c17a12215cbc44908099af4ac444561c3f95d833c5a91b325cdd4470737d8a01e2da64db2d542dd7c9a3747fbfdbf213e
sidhujag pushed a commit to syscoin/syscoin that referenced this pull request Mar 24, 2023
8c47d59 doc: improve -debuglogfile help to be a bit clearer (jonatack)
20d89d6 bench: document expected results in logging benchmarks (jonatack)
d8deba8 bench: add LogPrintfCategory and LogPrintLevel benchmarks (Jon Atack)
102b203 bench: order the logging benchmark code by output (Jon Atack)
4b3fdbf bench: update logging benchmark naming for clarity (Jon Atack)
4684aa8 bench: allow logging benchmarks to be order-independent (Larry Ruane)

Pull request description:

  Update our logging benchmarks for evaluating ongoing work like bitcoin#25203 and refactoring proposals like bitcoin#26619 and bitcoin#26697.

  - make the logging benchmarks order-independent (Larry Ruane)
  - add missing benchmarks for the `LogPrintLevel` and `LogPrintfCategory` macros that our logging is migrating to; at some later point it should be feasible to drop some of the previous logging benchmarks
  - update the logging benchmark naming to be clear which benchmark corresponds to which log macro, and update the ordering to be the same as the output
  - add clarifying documentation to the logging benchmarks
  - improve the `-debuglogfile` config option help to be clearer; can be tested by running `./src/bitcoind -help | grep -A4 '\-debuglogfile'`

  Reviewers can run the logging benchmarks with:
  ```bash
  ./src/bench/bench_bitcoin -filter='LogP*.*'
  ```

ACKs for top commit:
  LarryRuane:
    ACK 8c47d59
  martinus:
    code review & tested ACK 8c47d59, here are my benchmark results:
  achow101:
    ACK 8c47d59

Tree-SHA512: 705f8720c9ceaf14a1945039c7578a0c17a12215cbc44908099af4ac444561c3f95d833c5a91b325cdd4470737d8a01e2da64db2d542dd7c9a3747fbfdbf213e
@bitcoin bitcoin locked and limited conversation to collaborators Mar 19, 2024
@bitcoin bitcoin unlocked this conversation Aug 12, 2024
Copy link
Contributor
@vasild vasild left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ACK 90c3d90

This probably needs a rebase. It is a trivial change.

10 new categories were added in the last 7 years. That is 1-2 per year. 32 extra should suffice for a few decades.

Copy link
Contributor
@vasild vasild left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ACK 9bed303

Copy link
Contributor
@ryanofsky ryanofsky left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code review ACK 9bed303. I do think #26697 is a nicer alternative to this PR because it makes code more safe and readable. But this approach is also fine.

src/logging.h Outdated
@@ -204,7 +204,7 @@ namespace BCLog {
void SetLogLevel(Level level) { m_log_level = level; }
bool SetLogLevel(std::string_view level);

uint32_t GetCategoryMask() const { return m_categories.load(); }
uint64_t GetCategoryMask() const { return m_categories.load(); }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In commit "log: expand BCLog::LogFlags (categories) to 64 bits" (9bed303)

Instead of hardcoding uint64_t throughout this PR, it might make sense to add using CategoryMask = uint64_t; and use CategoryMask as a type name describing purpose of the type rather than how many bits it presently contains.

@LarryRuane LarryRuane force-pushed the 2022-11-log-categories-64 branch from 9bed303 to f64b861 Compare August 13, 2024 15:23
@LarryRuane
Copy link
Contributor Author

@ryanofsky

Instead of hardcoding uint64_t throughout this PR, it might make sense to add using CategoryMask = uint64_t

I took this suggestion, thanks. As a test, I defined CategoryMask to be uint32_t, but the compile failed because of the ULL suffix on all those constants (width exceeds 32 bits). So I'm trying to work around that by defining a mask_bit constant. (Should it have a name like MaskBit? It's only used within the enum definition.) I think it makes the code easy to read. If CI fails, I can change them back to ULL (even though it's kind of old fashioned and ugly).

Copy link
Contributor
@vasild vasild left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ACK f64b861

I was fine with (1ULL << 23) as well. Another way is to use the 0b prefix (then no need for the ULL suffix):

enum LogFlags : CategoryMask {
    NONE =    0b00000000000000000000000000000000,
    NET =     0b00000000000000000000000000000001,
    TOR =     0b00000000000000000000000000000010,
    MEMPOOL = 0b00000000000000000000000000000100,
    HTTP =    0b00000000000000000000000000001000,
    ....
};

@DrahtBot DrahtBot requested a review from ryanofsky August 13, 2024 16:17
@ryanofsky
Copy link
Contributor

Code review ACK f64b861, just added uint64_t typedef since last review.

As a test, I defined CategoryMask to be uint32_t, but the compile failed because of the ULL suffix on all those constants (width exceeds 32 bits). So I'm trying to work around that by defining a mask_bit constant. (Should it have a name like MaskBit? It's only used within the enum definition.) I think it makes the code easy to read. If CI fails, I can change them back to ULL (even though it's kind of old fashioned and ugly).

This seems ok, though I think would just write CategoryMask{1} everywhere instead of having a separate constant. Since the code is already using CategoryMask{0} this seems consistent, and it avoids defining another identifier to look up. I do think either of these approaches are a little better than using ULL suffixes, but those suffixes would be ok too. I have to say I am horrified 😱 by vasild's suggestion to write the flags in binary because its seems dizzying and error prone to me, but maybe this representation seems more natural to other people.

I also still think #26697 is a nicer alternative to this, removing hardcoded numbers and bitwise operations outside of the bitset class.

@vasild
Copy link
Contributor
vasild commented Aug 13, 2024

Maybe 0b00000000'00000000'00000000'00001000 is a bit less horrific ;) The current is ok, no need to change it further.

This will increase the maximum number of logging categories
from 32 to 64.
@LarryRuane LarryRuane force-pushed the 2022-11-log-categories-64 branch from f64b861 to b31a0cd Compare August 13, 2024 19:27
@LarryRuane
Copy link
Contributor Author

I think would just write CategoryMask{1} everywhere instead of having a separate constant

I like that suggestion, I just force-pushed it, thanks!

Copy link
Contributor
@vasild vasild left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ACK b31a0cd

Copy link
Contributor
@ryanofsky ryanofsky left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code review ACK b31a0cd, just dropping mask_bit constant since last review. I still think
#26697 is a better alternative to this PR because it is more type-safe and gets rid of bitwise flags and operations outside the bitset class, but it could also be a followup to this PR.

Copy link
Contributor
@theStack theStack left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code-review ACK b31a0cd

@achow101
Copy link
Member
achow101 commented Sep 3, 2024

ACK b31a0cd

@achow101 achow101 merged commit 27e89bc into bitcoin:master Sep 3, 2024
16 checks passed
@LarryRuane LarryRuane deleted the 2022-11-log-categories-64 branch September 3, 2024 21:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

10 participants
0