refactor: Replace thread_local use with a mutex-protected map #18851

pull laanwj wants to merge 1 commits into bitcoin:master from laanwj:2020_05_replace_threadlocal changing 6 files +43 −79
  1. laanwj commented at 10:46 am on May 2, 2020: member

    This replaces the only use of thread_local with a mutex-protected map.

    This leaks per thread, but because bitcoin’s threads are static and permanent, this is not a problem in practice. Add a note to the thread rename function to only call it for permanent threads, just in case.

    Also removes associated build system functionality.

    Closes #18678.

  2. laanwj added the label Refactoring on May 2, 2020
  3. fanquake added the label Utils/log/libs on May 2, 2020
  4. refactor: Replace thread_local use with a mutex-protected map
    This replaces the only use of `thread_local` with a mutex-protected map.
    
    This leaks per thread, but because bitcoin's threads are static and
    permanent, this is not a problem in practice. Add a note to the thread
    rename function to only call it for permanent threads, just in case.
    
    Also removes associated build system functionality.
    
    Closes #18678.
    c7933899e9
  5. laanwj force-pushed on May 2, 2020
  6. meshcollider commented at 11:20 am on May 2, 2020: contributor
    Concept / code review ACK c7933899e93b38f444d87b12858fd5a124f2066c modulo someone testing that it does fix the performance issue linked
  7. MarcoFalke commented at 11:47 am on May 2, 2020: member
    Concept ACK if this fixes the problem. Will test when my new hardware arrives
  8. laanwj commented at 11:57 am on May 2, 2020: member

    If this doesn’t solve the problem, then the problem isn’t thread_local but looking up thread names in the first place, and we should remove thread name logging completely (as well as thread_local).

    A workaround then that would avoid the need for this bookkeeping at all would be:

    • Log thread names once with their associated ID after creation on thread name setting
    • Log thread IDs instead of names in log messages
  9. DrahtBot commented at 12:27 pm on May 2, 2020: member

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

    Conflicts

    Reviewers, this pull request conflicts with the following ones:

    • #18849 (The Zero Allocations project by jb55)

    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.

  10. hebasto commented at 7:18 pm on May 2, 2020: member
    Concept ACK. Keep testing.
  11. hebasto commented at 8:40 pm on May 2, 2020: member

    Testing on ODROID-HC1:

    0$ uname -srm
    1Linux 4.14.173-173 armv7l
    2$ ./autogen.sh && ./configure --disable-wallet --disable-tests --disable-bench --disable-zmq --disable-man --disable-util-tx --disable-util-wallet --without-miniupnpc --without-libs --without-gui CC=clang-9 CXX=clang++-9 && make clean && time taskset --cpu-list 4-7 make --jobs=4
    

    UPDATE: sorry, wrong branch compiled

  12. MarcoFalke added the label Needs gitian build on May 2, 2020
  13. MarcoFalke added the label Needs Guix build on May 2, 2020
  14. hebasto commented at 0:42 am on May 3, 2020: member

    Here are results of three consequent tests on ODROID-HC1:

     0$ uname -srm
     1Linux 4.14.173-173 armv7l
     2$ date; time bitcoin-cli -rpcclienttimeout=10000 gettxoutsetinfoSat May  2 23:18:12 UTC 2020
     3{
     4  "height": 628635,
     5  "bestblock": "0000000000000000000210066becbfb2ebfdd957d3bdaec7190f46d87ce59b7a",
     6  "transactions": 39898110,
     7  "txouts": 66828188,
     8  "bogosize": 5019712922,
     9  "hash_serialized_2": "78c80aed68a14274a1dbadd9213ff6da55618d29c2b077c8f502ce5f900fcd03",
    10  "disk_size": 4027535280,
    11  "total_amount": 18357767.32081895
    12}
    13
    14real	6m32.011s
    15user	0m0.030s
    16sys	0m0.020s
    17$ date; time bitcoin-cli -rpcclienttimeout=10000 gettxoutsetinfoSat May  2 23:31:50 UTC 2020
    18{
    19  "height": 628635,
    20  "bestblock": "0000000000000000000210066becbfb2ebfdd957d3bdaec7190f46d87ce59b7a",
    21  "transactions": 39898110,
    22  "txouts": 66828188,
    23  "bogosize": 5019712922,
    24  "hash_serialized_2": "78c80aed68a14274a1dbadd9213ff6da55618d29c2b077c8f502ce5f900fcd03",
    25  "disk_size": 4035885849,
    26  "total_amount": 18357767.32081895
    27}
    28
    29real	9m48.031s
    30user	0m0.004s
    31sys	0m0.040s
    32$ date; time bitcoin-cli -rpcclienttimeout=10000 gettxoutsetinfoSat May  2 23:44:47 UTC 2020
    33{
    34  "height": 628636,
    35  "bestblock": "0000000000000000000e91c7a666359c09a405e3a5294181100a6aeb7d9d6427",
    36  "transactions": 39898996,
    37  "txouts": 66829911,
    38  "bogosize": 5019841104,
    39  "hash_serialized_2": "065cb1955ffc90c37ef293d8bee8e2ca9af9f9d87b0a50f8d58b4b874b642a82",
    40  "disk_size": 4030980759,
    41  "total_amount": 18357779.82081895
    42}
    43
    44real	53m16.860s
    45user	0m0.008s
    46sys	0m0.035s
    

    The related log:

     02020-05-02T21:11:03Z [init] Bitcoin Core version v0.20.99.0-c7933899e (release build)
     12020-05-02T21:11:03Z [init] InitParameterInteraction: parameter interaction: -externalip set -> setting -discover=0
     22020-05-02T21:11:03Z [init] Assuming ancestors of block 0000000000000000000f2adce67e49b0b6bdeb9de8b7c3d7e93b21e7fc1e819d have valid signatures.
     32020-05-02T21:11:03Z [init] Setting nMinimumChainWork=00000000000000000000000000000000000000000e1ab5ec9348e9f4b8eb8154
     42020-05-02T21:11:03Z [init] Using the 'standard' SHA256 implementation
     52020-05-02T21:11:03Z [init] Default data directory /home/core/.bitcoin
     62020-05-02T21:11:03Z [init] Using data directory /var/lib/bitcoind
     72020-05-02T21:11:03Z [init] Config file: /etc/bitcoin/bitcoin.conf
     82020-05-02T21:11:03Z [init] Config file arg: debug="rpc"
     92020-05-02T21:11:03Z [init] Config file arg: externalip="95.164.65.194"
    102020-05-02T21:11:03Z [init] Config file arg: listenonion="0"
    112020-05-02T21:11:03Z [init] Config file arg: logthreadnames="1"
    122020-05-02T21:11:03Z [init] Config file arg: par="4"
    132020-05-02T21:11:03Z [init] Config file arg: rpcthreads="2"
    142020-05-02T21:11:03Z [init] Config file arg: rpcworkqueue="1"
    15...
    162020-05-02T23:18:12Z [httpworker.0] ThreadRPCServer method=gettxoutsetinfo user=__cookie__
    172020-05-02T23:18:12Z [httpworker.0] FlushStateToDisk: write coins cache to disk (337567 coins, 39627kB) started
    182020-05-02T23:18:14Z [httpworker.0] FlushStateToDisk: write coins cache to disk (337567 coins, 39627kB) completed (2.05s)
    192020-05-02T23:31:50Z [httpworker.1] ThreadRPCServer method=gettxoutsetinfo user=__cookie__
    202020-05-02T23:31:51Z [httpworker.1] FlushStateToDisk: write coins cache to disk (3522 coins, 2177kB) started
    212020-05-02T23:31:51Z [httpworker.1] FlushStateToDisk: write coins cache to disk (3522 coins, 2177kB) completed (0.02s)
    222020-05-02T23:40:36Z [msghand] UpdateTip: new best=0000000000000000000e91c7a666359c09a405e3a5294181100a6aeb7d9d6427 height=628636 version=0x27ffe000 log2_work=91.914443 tx=526304521 date='2020-05-02T23:40:05Z' progress=1.000000 cache=3.2MiB(13600txo) warning='67 of last 100 blocks have unexpected version'
    232020-05-02T23:44:47Z [httpworker.0] ThreadRPCServer method=gettxoutsetinfo user=__cookie__
    242020-05-02T23:44:48Z [httpworker.0] FlushStateToDisk: write coins cache to disk (14579 coins, 3418kB) started
    252020-05-02T23:44:48Z [httpworker.0] FlushStateToDisk: write coins cache to disk (14579 coins, 3418kB) completed (0.09s)
    262020-05-02T23:59:13Z [msghand] UpdateTip: new best=000000000000000000034cd30624babe78f29f0944b2e73d784198d4fd1af594 height=628637 version=0x20000000 log2_work=91.914464 tx=526307327 date='2020-05-02T23:54:25Z' progress=0.999998 cache=3.1MiB(12818txo) warning='66 of last 100 blocks have unexpected version'
    272020-05-02T23:59:30Z [msghand] New outbound peer connected: version: 70015, blocks=628638, peer=1217 (full-relay)
    282020-05-03T00:08:15Z [msghand] UpdateTip: new best=0000000000000000000a9c8ea50980bece5f9168e962c868e2f9d9761d5ddb0c height=628638 version=0x20800000 log2_work=91.914485 tx=526308404 date='2020-05-02T23:54:28Z' progress=0.999995 cache=4.0MiB(21407txo) warning='67 of last 100 blocks have unexpected version'
    292020-05-03T00:08:15Z [net] socket sending timeout: 1382s
    302020-05-03T00:14:30Z [msghand] UpdateTip: new best=00000000000000000006ceea137d6caebd533d6fd95f8c1da6be15fe01c6d828 height=628639 version=0x20c00000 log2_work=91.914506 tx=526310651 date='2020-05-03T00:08:22Z' progress=0.999998 cache=5.0MiB(30426txo) warning='67 of last 100 blocks have unexpected version'
    312020-05-03T00:32:24Z [msghand] UpdateTip: new best=0000000000000000000d0ce69147e7c831a0ea999c5837667a2928960d538cce height=628640 version=0x37ffe000 log2_work=91.914527 tx=526313977 date='2020-05-03T00:31:10Z' progress=1.000000 cache=6.1MiB(41548txo) warning='67 of last 100 blocks have unexpected version'
    
  15. hebasto commented at 9:05 am on May 3, 2020: member
    It seems thread_local is not the root of the performance degradation. Please see #18538 (comment)
  16. DrahtBot commented at 5:38 am on May 4, 2020: member

    Gitian builds

    File commit 68ef9523d1bcd00afbccee2a6585c9f82ddcdb31(master) commit 4c270d2f47d244ef37c4aa1377c0d56b3b922bef(master and this pull)
    *-aarch64-linux-gnu-debug.tar.gz f563236ed2fe55e7... 933e19338f6026a2...
    *-aarch64-linux-gnu.tar.gz 0137ede5b5a3b69d... 04edf669faf37a18...
    *-arm-linux-gnueabihf-debug.tar.gz 6d981a7c8574b29e... 7d83f08b6e80ffcd...
    *-arm-linux-gnueabihf.tar.gz 67333c529c76f1b1... 979373626368c265...
    *-osx-unsigned.dmg ac9fe9134e61be05... b84109845e585992...
    *-osx64.tar.gz b244355f3f895985... 7c0b07f30f22c3b2...
    *-riscv64-linux-gnu-debug.tar.gz e41c9f54fbf8658e... 72209385bc8b457a...
    *-riscv64-linux-gnu.tar.gz 59c0518eadf4a01e... 030748544a4c7572...
    *-win64-debug.zip 3fc1adaa18cf42a3... f38b9c97640b1336...
    *-win64-setup-unsigned.exe ab3a4cdcd55c2557... 48762f310eb61f8f...
    *-win64.zip 86b1e7c9fbce4e1b... bec39b501c6015e1...
    *-x86_64-linux-gnu-debug.tar.gz af837f75521add5d... 81e210871dd6bb84...
    *-x86_64-linux-gnu.tar.gz 9a0c056928b7b1b7... 4e6a632a671596c9...
    *.tar.gz b7e353469956e000... 95d1e48eefb39003...
    bitcoin-core-linux-0.21-res.yml 99533cb54b367d2b... a7487bf63e97549a...
    bitcoin-core-osx-0.21-res.yml 3d3333a8ce2e234c... 7a10a1e2cbf860b4...
    bitcoin-core-win-0.21-res.yml 2d2bcab124ead9ec... 5852959f44562090...
    linux-build.log badd1a4ca8d67232... a006fdb1bb797e8f...
    osx-build.log 0c305199e0e3ebb6... 376adbf1fc9d0a47...
    win-build.log ff86639c1ee0f14a... 6f1f9a6ce56d0410...
    bitcoin-core-linux-0.21-res.yml.diff e25c9488f31761c5...
    bitcoin-core-osx-0.21-res.yml.diff d1fa1259824d80ea...
    bitcoin-core-win-0.21-res.yml.diff 6d751cb9f972f0d2...
    linux-build.log.diff d9417d859fc67f71...
    osx-build.log.diff c1307497c3bfedc6...
    win-build.log.diff f063a09a95217973...
  17. DrahtBot removed the label Needs gitian build on May 4, 2020
  18. laanwj commented at 1:18 pm on May 4, 2020: member
    Okay. So is it still worth keeping this open? It would make thread-name logging work in the gitian-built binaries which lack thread_local. And cleans up the build system a bit.
  19. hebasto commented at 1:20 pm on May 4, 2020: member

    It would make thread-name logging work in the gitian-built binaries which lack thread_local. And cleans up the build system a bit.

    Correct. So keep reviewing.

  20. MarcoFalke commented at 1:32 pm on May 4, 2020: member

    I am currently syncing a node to debug this issue.

    Also, glibc can probably be bumped by one minor version in the next release, which is going to switch to C++17. Then, potentially the same build cleanups can be done?

  21. laanwj closed this on May 4, 2020

  22. MarcoFalke removed the label Needs Guix build on May 6, 2020
  23. DrahtBot locked this on Feb 15, 2022

github-metadata-mirror

This is a metadata mirror of the GitHub repository bitcoin/bitcoin. This site is not affiliated with GitHub. Content is generated from a GitHub metadata backup.
generated: 2024-07-03 10:13 UTC

This site is hosted by @0xB10C
More mirrored repositories can be found on mirror.b10c.me