Share unused mempool memory with coincache #8610

pull sipa wants to merge 1 commits into bitcoin:master from sipa:sharemem changing 2 files +9 −5
  1. sipa commented at 1:24 pm on August 27, 2016: member

    A suggestion from IRC: allow the chainstate cache to use unused mempool memory.

    If the mempool is not completely full, treat the difference between the maximum size and the actual usage as available for the coin cache.

    This also changes the early flush trigger from (usage > 0.9 * space) to (usage > 0.9 * space && usage > space - 100MB). This means we’re not permanently leaving 10% of the space unused when the space is large.

  2. sipa force-pushed on Aug 27, 2016
  3. jonasschnelli added the label UTXO Db and Indexes on Aug 29, 2016
  4. jonasschnelli added the label Mempool on Aug 29, 2016
  5. jonasschnelli commented at 11:40 am on August 29, 2016: contributor
    This is indeed a nice idea. utACK.
  6. instagibbs commented at 2:38 pm on August 29, 2016: member

    At the risk of it being blatantly obvious, this would especially allow nodes with default settings during IBD to sync faster, yes?

    concept ACK

  7. sipa commented at 3:13 pm on August 29, 2016: member

    At the risk of it being blatantly obvious, this would especially allow nodes with default settings during IBD to sync faster, yes?

    Indeed!

  8. gmaxwell commented at 9:22 am on September 3, 2016: contributor
    utACK.
  9. rebroad commented at 3:02 pm on September 15, 2016: contributor

    At the risk of appearing stupid, how does this help nodes with default settings sync faster during IBD?

    …coincache helps to validate blocks..?

  10. sipa commented at 3:07 pm on September 15, 2016: member
    Because during IBD, the mempool is nearly empty. With this patch, all that unused mempool space (300 Mbyte by default currently) will be used for the chainstate cache, which is the bottleneck for block validation during IBD.
  11. morcos commented at 4:22 pm on September 15, 2016: member

    @sipa do you have any concern that this gives outside peers some control over when your coinsviewcache is flushed? That seems not ideal to me, at least until we are smarter about flushing it.

    I had previously imagined doing this by setting the mempool limit low initially and then increasing it to the desired setting after IBD was finished. But I’m not sure I succeeded in having the code architected to make that possible.

  12. sipa commented at 10:47 am on September 19, 2016: member
    @morcos I think that’s already a concern, but I’m not sure this patch worsens it significantly. External peers can cause an increase in memory usage of the cache or the mempool, but the latter is already harder anyway.
  13. in src/init.cpp: in 8b85edee37 outdated
    1281+    nCoinCachePlusMempoolUsage = nMempoolSizeMax + nTotalCache; // the rest goes to in-memory cache
    1282     LogPrintf("Cache configuration:\n");
    1283     LogPrintf("* Using %.1fMiB for block index database\n", nBlockTreeDBCache * (1.0 / 1024 / 1024));
    1284     LogPrintf("* Using %.1fMiB for chain state database\n", nCoinDBCache * (1.0 / 1024 / 1024));
    1285-    LogPrintf("* Using %.1fMiB for in-memory UTXO set\n", nCoinCacheUsage * (1.0 / 1024 / 1024));
    1286+    LogPrintf("* Using %.1fMiB for UTXO set (shared with mempool)\n", nCoinCachePlusMempoolUsage * (1.0 / 1024 / 1024));
    


    TheBlueMatt commented at 7:37 pm on September 21, 2016:
    This is confusing to me - maybe “Using $cache_only_size for UTXO set (+ up to $mempool_size from mempool)”

    sipa commented at 0:23 am on October 3, 2016:
    Done.
  14. sipa force-pushed on Oct 3, 2016
  15. luke-jr referenced this in commit 8c6813b8d5 on Oct 20, 2016
  16. fanquake commented at 8:26 am on November 7, 2016: member

    Seeing a roughly 30% decrease in time to reindex to 270'000 with this change.

    Master -reindex-chainstate

    08:04:43 Cache configuration: 08:04:43 * Using 2.0MiB for block index database 08:04:43 * Using 8.0MiB for chain state database 08:04:43 * Using 290.0MiB for in-memory UTXO set

    08:04:45 UpdateTip: height=0 cache=0.0MiB(0tx) 08:08:09 UpdateTip: height=200000 cache=172.9MiB(553629tx) 08:12:52 UpdateTip: height=230000 cache=274.9MiB(668943tx) 08:19:08 UpdateTip: height=260488 cache=239.4MiB(318281tx) 08:19:08 UpdateTip: height=260489 cache=239.4MiB(318525tx) 08:22:11 UpdateTip: height=270000 cache=31.7MiB(8943tx)

    1046s

    #8610 -reindex-chainstate

    07:50:16 Cache configuration: 07:50:16 * Using 2.0MiB for block index database 07:50:16 * Using 8.0MiB for chain state database 07:50:16 * Using 290.0MiB for UTXO set (plus up to 286.1MiB of unused mempool space) … 07:50:18 UpdateTip: height=0 cache=0.0MiB(0tx) 07:53:33 UpdateTip: height=200000 cache=356.1MiB(1336689tx) 07:56:58 UpdateTip: height=230000 cache=575.5MiB(1900937tx) 08:01:02 UpdateTip: height=260488 cache=576.1MiB(1324684tx) 08:01:11 UpdateTip: height=260489 cache=16.0MiB(0tx) 08:02:32 UpdateTip: height=270000 cache=486.6MiB(972452tx)

    734s

  17. in src/main.cpp: in 27562efde1 outdated
    2552@@ -2553,6 +2553,7 @@ enum FlushStateMode {
    2553  * or always and in all cases if we're in prune mode and are deleting files.
    2554  */
    2555 bool static FlushStateToDisk(CValidationState &state, FlushStateMode mode) {
    2556+    size_t cacheSize = mempool.DynamicMemoryUsage();
    


    morcos commented at 9:30 pm on November 25, 2016:

    I wonder if it would be better to call LimitMempoolSize right before this? During a reorg the mempool size isn’t limited until the end, so theoretically FlushStateToDisk could get called and have its effective cache size reduced below the expected minimum. @sdaftuar Is there any harm in calling an extra LimitMempoolSize in the middle of a reorg other than possible inefficiencies of what you’re evicting?

    EDIT: Shoot this isn’t exactly what I was imagining. I was thinking it would only limit the mempool if it was going to flush anyway, but this suggestion will limit the mempool every time FSTD is called which defeats the whole point of not limiting inside a reorg.

  18. morcos commented at 9:31 pm on November 25, 2016: member

    utACK modulo suggestion about calling LimitMempoolSize in FSTD.
    However if we decide there are downsides to that suggestion, I’m fine with leaving it out.

    EDIT: maybe 0dedace? Though I’m not sure it’s worth it..

  19. sipa commented at 0:22 am on November 26, 2016: member
    @morcos Looks good, I’ve included it.
  20. in src/main.cpp: in 3ec401e9a5 outdated
    2583@@ -2583,11 +2584,16 @@ bool static FlushStateToDisk(CValidationState &state, FlushStateMode mode) {
    2584     if (nLastSetChain == 0) {
    2585         nLastSetChain = nNow;
    2586     }
    2587-    size_t cacheSize = pcoinsTip->DynamicMemoryUsage();
    2588+    cacheSize += pcoinsTip->DynamicMemoryUsage();
    2589     // The cache is large and close to the limit, but we have time now (not in the middle of a block processing).
    2590-    bool fCacheLarge = mode == FLUSH_STATE_PERIODIC && cacheSize * (10.0/9) > nCoinCacheUsage;
    2591+    bool fCacheLarge = mode == FLUSH_STATE_PERIODIC && cacheSize * (10.0/9) > nCoinCachePlusMempoolUsage;
    


    morcos commented at 1:11 am on November 26, 2016:

    @sipa sorry, one more issue. this line would be pretty broken if someone sets a large maxmempool. i was thinking maybe we should change it to 20.0/19 so it doesn’t make the base case behavior a smaller effective cache size, but then realized if someone sets say a 2GB mempool and doesn’t make a larger dbcache, then they could be flushing all the time.

    Perhaps this problem and the other one could be avoided by just calculating any extra available space max(0,(maxmempool - usage)) and comparing only the pcoinstip usage to (dbcache limit + any extra available space).

  21. sdaftuar commented at 1:15 am on November 26, 2016: member

    @morcos Not sure I’m a fan of 0dedace. I believe this code will work correctly for now, but only because we don’t seem to call FlushStateToDisk anywhere the mempool might be in an inconsistent state. I don’t think it’s safe to call LimitMempoolSize() when the mempool is in an inconsistent state, because CTxMemPool::TrimToSize() and CTxMemPool::Expire rely on CTxMemPool::CalculateDescendants() to do the right thing, and that function assumes that setMemPoolChildren is correct for all entries. If CalculateDescendants() doesn’t catch everything it should, then we could wind up with orphans in the mempool.

    So in particular, during a reorg, we call AcceptToMemoryPool() for all the transactions in a disconnected block. If someone were to add a FlushStateToDisk() call inside ATMP (which I think seems harmless on the face of it), then that would introduce a bug that could cause orphans in the mempool after a reorg.

    So at the very least, we would need to document that FlushStateToDisk() has a new requirement, that the mempool is in a consistent state when invoked. But as this condition is difficult to assert for and test, I think my preference would be to not impose this requirement, just to reduce the chance we introduce a bug in the future.

    But also, note that #9208 should solve the problem of the mempool growing beyond its configured limit during a reorg…

  22. sipa force-pushed on Nov 26, 2016
  23. sipa commented at 3:27 am on November 26, 2016: member
    I removed the commit again.
  24. laanwj commented at 5:52 am on November 28, 2016: member
    Concept ACK
  25. sipa commented at 6:58 am on November 28, 2016: member
    Going to address @morcos’s issue soon.
  26. sipa force-pushed on Dec 22, 2016
  27. sipa commented at 3:23 am on December 22, 2016: member
    Rebased, and rewritten to take #8610 (review) into account.
  28. jtimon commented at 8:17 am on December 22, 2016: contributor
    Concept ACK.
  29. morcos commented at 4:04 pm on December 22, 2016: member

    Looks good @sipa , thanks

    Seems like an actual lock inversion, I think you fixed it previously by moving the mempool.DynamicMemoryUsage() to the top of FlustStateToDisk, which makes sense to me. However I don’t understand why CWallet::ReacceptWalletTransactions is locking mempool.cs anyway, so maybe that should also be removed.

    I guess you lost the advantage of the extra space in VerifyDB, but that seems no big loss to me.

  30. sipa force-pushed on Dec 22, 2016
  31. Share unused mempool memory with coincache
    If the mempool is not completely full, treat the difference between
    the maximum size and the actual usage as available for the coin cache.
    
    This also changes the early flush trigger from (usage > 0.9 * space)
    to (usage > 0.9 * space && usage > space - 100MB). This means we're not
    permanently leaving 10% of the space unused when the space is large.
    ba3cecf5c4
  32. sipa force-pushed on Dec 22, 2016
  33. luke-jr referenced this in commit dd934d6d10 on Dec 23, 2016
  34. fanquake commented at 0:05 am on January 4, 2017: member

    #8610 on top of master (2a524b8e8fe69ef487fd8ea1b4f7a03f473ed201) with -reindex-chainstate -dbcache=2048:

    02017-01-03 22:39:59 Cache configuration:
    12017-01-03 22:39:59 * Using 2.0MiB for block index database
    22017-01-03 22:39:59 * Using 8.0MiB for chain state database
    32017-01-03 22:39:59 * Using 2038.0MiB for in-memory UTXO set (plus up to 286.1MiB of unused mempool space)
    42017-01-03 22:43:04 height=200000 cache=356.1MiB(1336689tx)
    52017-01-03 22:46:20 height=230000 cache=575.5MiB(1900937tx)
    62017-01-03 22:51:26 height=270000 cache=915.5MiB(2499199tx)
    72017-01-03 22:53:26 height=280000 cache=1050.5MiB(2816546tx)
    

    807s (to 280'000)

    Master (2a524b8e8fe69ef487fd8ea1b4f7a03f473ed201) -reindex-chainstate -dbcache=2048:

    02017-01-03 23:01:24 Cache configuration:
    12017-01-03 23:01:24 * Using 2.0MiB for block index database
    22017-01-03 23:01:24 * Using 8.0MiB for chain state database
    32017-01-03 23:01:24 * Using 2038.0MiB for in-memory UTXO set
    42017-01-03 23:04:29 height=200000 cache=356.1MiB(1336689tx)
    52017-01-03 23:07:46 height=230000 cache=575.5MiB(1900937tx)
    62017-01-03 23:12:48 height=270000 cache=915.5MiB(2499199tx)
    72017-01-03 23:14:45 height=280000 cache=1050.5MiB(2816546tx)
    

    801s (to 280'000)

    Master (2a524b8e8fe69ef487fd8ea1b4f7a03f473ed201) -reindex-chainstate -dbcache=300:

    02017-01-03 23:24:34 Cache configuration:
    12017-01-03 23:24:34 * Using 2.0MiB for block index database
    22017-01-03 23:24:34 * Using 8.0MiB for chain state database
    32017-01-03 23:24:34 * Using 290.0MiB for in-memory UTXO set
    42017-01-03 23:27:55 height=200000 cache=172.9MiB(553629tx)
    52017-01-03 23:32:32 height=230000 cache=274.9MiB(668943tx)
    62017-01-03 23:36:41 height=250000 cache=128.6MiB(113010tx)
    72017-01-03 23:42:36 height=270000 cache=31.7MiB(8943tx)
    

    1082s (to 270'000)

    #8610 on top of master (2a524b8e8fe69ef487fd8ea1b4f7a03f473ed201) -reindex-chainstate -dbcache=300

    02017-01-03 23:47:30 Cache configuration:
    12017-01-03 23:47:30 * Using 2.0MiB for block index database
    22017-01-03 23:47:30 * Using 8.0MiB for chain state database
    32017-01-03 23:47:30 * Using 290.0MiB for in-memory UTXO set (plus up to 286.1MiB of unused mempool space)
    42017-01-03 23:50:37 height=200000 cache=356.1MiB(1336689tx)
    52017-01-03 23:53:58 height=230000 cache=575.5MiB(1900937tx)
    62017-01-03 23:56:50 height=250000 cache=504.4MiB(1124858tx)
    72017-01-03 23:59:30 height=270000 cache=486.6MiB(972452tx)
    82017-01-04 00:02:10 height=280000 cache=75.2MiB(48075tx)
    

    720s (to 270'000)

  35. morcos commented at 6:57 pm on January 5, 2017: member
    utACK ba3cecf (for the avoidance of doubt)
  36. TheBlueMatt commented at 6:58 pm on January 5, 2017: member
    utACK ba3cecf5c436bf38efad045d46e0aa26210d2234
  37. sipa merged this on Jan 5, 2017
  38. sipa closed this on Jan 5, 2017

  39. sipa referenced this in commit c252685aa5 on Jan 5, 2017
  40. rebroad commented at 6:46 am on January 6, 2017: contributor
    utACK - will merge and test
  41. jnewbery referenced this in commit 99daee057d on Jan 17, 2017
  42. jnewbery referenced this in commit 2838d28f20 on Jan 17, 2017
  43. codablock referenced this in commit 2259f656dd on Sep 27, 2017
  44. codablock referenced this in commit a6d3735412 on Oct 12, 2017
  45. codablock referenced this in commit 24ffe13242 on Oct 23, 2017
  46. codablock referenced this in commit ceb64fcd47 on Oct 23, 2017
  47. UdjinM6 referenced this in commit 43823e1914 on Nov 8, 2017
  48. MarcoFalke locked this on Sep 8, 2021

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-11-23 15:12 UTC

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