nodemap: display percentage of unused in `hg debugnodemap`
authorPierre-Yves David <pierre-yves.david@octobus.net>
Fri, 28 Feb 2020 03:18:57 +0100
changeset 44635 99ea74cbed74
parent 44634 01b0805534bb
child 44636 c70bcaf7927b
nodemap: display percentage of unused in `hg debugnodemap` This is useful to assess the density of the cache. Differential Revision: https://phab.mercurial-scm.org/D8192
mercurial/debugcommands.py
mercurial/revlogutils/nodemap.py
tests/test-persistent-nodemap.t
--- a/mercurial/debugcommands.py	Fri Feb 28 03:05:52 2020 +0100
+++ b/mercurial/debugcommands.py	Fri Feb 28 03:18:57 2020 +0100
@@ -2146,6 +2146,8 @@
             ui.write((b"tip-node: %s\n") % hex(docket.tip_node))
             ui.write((b"data-length: %d\n") % docket.data_length)
             ui.write((b"data-unused: %d\n") % docket.data_unused)
+            unused_perc = docket.data_unused * 100.0 / docket.data_length
+            ui.write((b"data-unused: %2.3f%%\n") % unused_perc)
 
 
 @command(
--- a/mercurial/revlogutils/nodemap.py	Fri Feb 28 03:05:52 2020 +0100
+++ b/mercurial/revlogutils/nodemap.py	Fri Feb 28 03:18:57 2020 +0100
@@ -102,6 +102,9 @@
     def add(self, *args, **kwargs):
         pass
 
+    def addabort(self, *args, **kwargs):
+        pass
+
 
 def update_persistent_nodemap(revlog):
     """update the persistent nodemap right now
@@ -171,7 +174,17 @@
             data = persistent_data(revlog.index)
         # EXP-TODO: if this is a cache, this should use a cache vfs, not a
         # store vfs
-        tr.add(datafile, 0)
+
+        tryunlink = revlog.opener.tryunlink
+
+        def abortck(tr):
+            tryunlink(datafile)
+
+        callback_id = b"delete-%s" % datafile
+
+        # some flavor of the transaction abort does not cleanup new file, it
+        # simply empty them.
+        tr.addabort(callback_id, abortck)
         with revlog.opener(datafile, b'w+') as fd:
             fd.write(data)
             if feed_data:
@@ -198,9 +211,6 @@
     if feed_data:
         revlog.index.update_nodemap_data(target_docket, new_data)
 
-    # EXP-TODO: if the transaction abort, we should remove the new data and
-    # reinstall the old one.
-
     # search for old index file in all cases, some older process might have
     # left one behind.
     olds = _other_rawdata_filepath(revlog, target_docket)
--- a/tests/test-persistent-nodemap.t	Fri Feb 28 03:05:52 2020 +0100
+++ b/tests/test-persistent-nodemap.t	Fri Feb 28 03:18:57 2020 +0100
@@ -17,6 +17,7 @@
   tip-node: 06ddac466af534d365326c13c3879f97caca3cb1
   data-length: 122880
   data-unused: 0
+  data-unused: 0.000%
   $ f --size .hg/store/00changelog.n
   .hg/store/00changelog.n: size=70
 
@@ -99,6 +100,7 @@
   tip-node: 2dd9b5258caa46469ff07d4a3da1eb3529a51f49
   data-length: 122880
   data-unused: 0
+  data-unused: 0.000%
 #else
   $ hg debugnodemap --metadata
   uid: ???????????????? (glob)
@@ -106,6 +108,7 @@
   tip-node: 2dd9b5258caa46469ff07d4a3da1eb3529a51f49
   data-length: 123072
   data-unused: 192
+  data-unused: 0.156%
 #endif
 
   $ f --size .hg/store/00changelog.n
@@ -154,6 +157,7 @@
   tip-node: 6ce944fafcee85af91f29ea5b51654cc6101ad7e
   data-length: 123328
   data-unused: 384
+  data-unused: 0.311%
   $ f --sha256 .hg/store/00changelog-*.nd --size
   .hg/store/00changelog-????????????????.nd: size=123328, sha256=10d26e9776b6596af0f89143a54eba8cc581e929c38242a02a7b0760698c6c70 (glob)
 #endif
@@ -164,6 +168,7 @@
   tip-node: 6ce944fafcee85af91f29ea5b51654cc6101ad7e
   data-length: 123328
   data-unused: 384
+  data-unused: 0.311%
   $ f --sha256 .hg/store/00changelog-*.nd --size
   .hg/store/00changelog-????????????????.nd: size=123328, sha256=081eec9eb6708f2bf085d939b4c97bc0b6762bc8336bc4b93838f7fffa1516bf (glob)
 #endif
@@ -174,6 +179,7 @@
   tip-node: 6ce944fafcee85af91f29ea5b51654cc6101ad7e
   data-length: 122944
   data-unused: 0
+  data-unused: 0.000%
   $ f --sha256 .hg/store/00changelog-*.nd --size
   .hg/store/00changelog-????????????????.nd: size=122944, sha256=755976b22b64ab680401b45395953504e64e7fa8c31ac570f58dee21e15f9bc0 (glob)
 #endif
@@ -190,6 +196,7 @@
   tip-node: 6ce944fafcee85af91f29ea5b51654cc6101ad7e
   data-length: 122944
   data-unused: 0
+  data-unused: 0.000%
 #else
   $ hg debugnodemap --metadata
   uid: ???????????????? (glob)
@@ -197,6 +204,7 @@
   tip-node: 6ce944fafcee85af91f29ea5b51654cc6101ad7e
   data-length: 122944
   data-unused: 0
+  data-unused: 0.000%
 #endif
 
 Check out of sync nodemap
@@ -230,6 +238,9 @@
   data-unused: 256 (pure !)
   data-unused: 256 (rust !)
   data-unused: 0 (no-rust no-pure !)
+  data-unused: 0.208% (pure !)
+  data-unused: 0.208% (rust !)
+  data-unused: 0.000% (no-rust no-pure !)
   $ cp -f ../tmp-copies/* .hg/store/
   $ hg debugnodemap --metadata
   uid: ???????????????? (glob)
@@ -237,6 +248,7 @@
   tip-node: 6ce944fafcee85af91f29ea5b51654cc6101ad7e
   data-length: 122944
   data-unused: 0
+  data-unused: 0.000%
   $ hg log -r "$NODE" -T '{rev}\n'
   5003
 
@@ -271,6 +283,9 @@
   data-unused: 448 (pure !)
   data-unused: 123904 (rust !)
   data-unused: 0 (no-pure no-rust !)
+  data-unused: 50.273% (rust !)
+  data-unused: 0.363% (pure !)
+  data-unused: 0.000% (no-pure no-rust !)
 
   $ cp -f ../tmp-copies/* .hg/store/
   $ hg debugnodemap --metadata
@@ -279,6 +294,7 @@
   tip-node: 6ce944fafcee85af91f29ea5b51654cc6101ad7e
   data-length: 122944
   data-unused: 0
+  data-unused: 0.000%
   $ hg log -r "$OTHERNODE" -T '{rev}\n'
   5002
 
@@ -296,6 +312,7 @@
   tip-node: c91af76d172f1053cca41b83f7c2e4e514fe2bcf
   data-length: 123008
   data-unused: 0
+  data-unused: 0.000%
   $ echo babar2 > babar
   $ hg ci -m 'babar2' --config "hooks.pretxnclose.nodemap-test=hg debugnodemap --metadata"
   uid: ???????????????? (glob)
@@ -307,6 +324,9 @@
   data-unused: 192 (pure !)
   data-unused: 192 (rust !)
   data-unused: 0 (no-pure no-rust !)
+  data-unused: 0.156% (pure !)
+  data-unused: 0.156% (rust !)
+  data-unused: 0.000% (no-pure no-rust !)
   $ hg debugnodemap --metadata
   uid: ???????????????? (glob)
   tip-rev: 5004
@@ -317,6 +337,9 @@
   data-unused: 192 (pure !)
   data-unused: 192 (rust !)
   data-unused: 0 (no-pure no-rust !)
+  data-unused: 0.156% (pure !)
+  data-unused: 0.156% (rust !)
+  data-unused: 0.000% (no-pure no-rust !)
 
 Another process does not see the pending nodemap content during run.
 
@@ -340,6 +363,9 @@
   data-unused: 192 (pure !)
   data-unused: 192 (rust !)
   data-unused: 0 (no-pure no-rust !)
+  data-unused: 0.156% (pure !)
+  data-unused: 0.156% (rust !)
+  data-unused: 0.000% (no-pure no-rust !)
   $ hg debugnodemap --metadata
   uid: ???????????????? (glob)
   tip-rev: 5005
@@ -350,6 +376,9 @@
   data-unused: 448 (pure !)
   data-unused: 448 (rust !)
   data-unused: 0 (no-pure no-rust !)
+  data-unused: 0.363% (pure !)
+  data-unused: 0.363% (rust !)
+  data-unused: 0.000% (no-pure no-rust !)
 
   $ cat output.txt
 
@@ -369,8 +398,15 @@
   uid: ???????????????? (glob)
   tip-rev: 5005
   tip-node: bae4d45c759e30f1cb1a40e1382cf0e0414154db
-  data-length: 123584
-  data-unused: 448
+  data-length: 123584 (pure !)
+  data-length: 123584 (rust !)
+  data-length: 123136 (no-pure no-rust !)
+  data-unused: 448 (pure !)
+  data-unused: 448 (rust !)
+  data-unused: 0 (no-pure no-rust !)
+  data-unused: 0.363% (pure !)
+  data-unused: 0.363% (rust !)
+  data-unused: 0.000% (no-pure no-rust !)
   $ f --size --sha256 .hg/store/00changelog-*.nd
   .hg/store/00changelog-????????????????.nd: size=123584, sha256=8c6cef6fd3d3fac291968793ee19a4be6d0b8375e9508bd5c7d4a8879e8df180 (glob) (pure !)
   .hg/store/00changelog-????????????????.nd: size=123584, sha256=eb9e9a4bcafdb5e1344bc8a0cbb3288b2106413b8efae6265fb8a7973d7e97f9 (glob) (rust !)