From 76216e061540ee532d09495cabe05ce7c25afc8c Mon Sep 17 00:00:00 2001 From: "Brian J. Murrell" Date: Tue, 30 Jan 2024 11:03:12 -0500 Subject: [PATCH] Show ccache stats at the end of the build Zero the ccache stats at the beginning of the build and then display the ccache stats at the end of the build to see how effective ccache was. Pavel Raiskup added the opt-in knob. Signed-off-by: Brian J. Murrell --- docs/Plugin-CCache.md | 5 +++++ mock/docs/site-defaults.cfg | 1 + mock/py/mockbuild/config.py | 1 + mock/py/mockbuild/plugins/ccache.py | 14 ++++++++++++++ .../release-notes-next/ccache-show-stats.feature | 3 +++ 5 files changed, 24 insertions(+) create mode 100644 releng/release-notes-next/ccache-show-stats.feature diff --git a/docs/Plugin-CCache.md b/docs/Plugin-CCache.md index 30f15b57d..0d07f009d 100644 --- a/docs/Plugin-CCache.md +++ b/docs/Plugin-CCache.md @@ -17,6 +17,7 @@ The ccache plugin is enabled by default and has the following values built-in: config_opts['plugin_conf']['ccache_opts']['dir'] = "%(cache_topdir)s/%(root)s/ccache/u%(chrootuid)s/" config_opts['plugin_conf']['ccache_opts']['hashdir'] = True config_opts['plugin_conf']['ccache_opts']['debug'] = False + config_opts['plugin_conf']['ccache_opts']['show_stats'] = False To turn on ccache compression, use the following in a config file: @@ -36,3 +37,7 @@ This option is available since Mock 5.7. Setting `debug` to `True` creates per-object debug files that are helpful when debugging unexpected cache misses. See [ccache documentation](https://ccache.dev/manual/4.10.html#config_debug). This option is available since Mock 5.7. + +If `show_stats` is set to True, Mock calls `ccache --zero-stats` first (before +doing the build), and then calls `ccache --show-stats`. +This option is available since Mock v5.7+. diff --git a/mock/docs/site-defaults.cfg b/mock/docs/site-defaults.cfg index 274ca8ee3..6e990004b 100644 --- a/mock/docs/site-defaults.cfg +++ b/mock/docs/site-defaults.cfg @@ -247,6 +247,7 @@ # config_opts['plugin_conf']['ccache_opts']['compress'] = None # config_opts['plugin_conf']['ccache_opts']['dir'] = "{{cache_topdir}}/{{root}}/ccache/u{{chrootuid}}/" # config_opts['plugin_conf']['ccache_opts']['hashdir'] = True +# config_opts['plugin_conf']['ccache_opts']['show_stats'] = False # config_opts['plugin_conf']['ccache_opts']['debug'] = False # config_opts['plugin_conf']['yum_cache_enable'] = True # config_opts['plugin_conf']['yum_cache_opts'] = {} diff --git a/mock/py/mockbuild/config.py b/mock/py/mockbuild/config.py index 0baf762fb..a0287438d 100644 --- a/mock/py/mockbuild/config.py +++ b/mock/py/mockbuild/config.py @@ -132,6 +132,7 @@ def setup_default_config_opts(): 'dir': "{{cache_topdir}}/{{root}}/ccache/u{{chrootuid}}/", 'hashdir': True, 'debug': False, + 'show_stats': False, }, 'yum_cache_enable': True, 'yum_cache_opts': { diff --git a/mock/py/mockbuild/plugins/ccache.py b/mock/py/mockbuild/plugins/ccache.py index c7e3649ac..9b79482dd 100644 --- a/mock/py/mockbuild/plugins/ccache.py +++ b/mock/py/mockbuild/plugins/ccache.py @@ -35,6 +35,7 @@ def __init__(self, plugins, conf, buildroot): buildroot.preexisting_deps.append("ccache") plugins.add_hook("prebuild", self._ccacheBuildHook) plugins.add_hook("preinit", self._ccachePreInitHook) + plugins.add_hook("postbuild", self._ccachePostBuildHook) buildroot.mounts.add( BindMountPoint(srcpath=self.ccachePath, bindpath=buildroot.make_chroot_path("/var/tmp/ccache"))) @@ -47,6 +48,11 @@ def __init__(self, plugins, conf, buildroot): @traceLog() def _ccacheBuildHook(self): self.buildroot.doChroot(["ccache", "-M", str(self.ccache_opts['max_cache_size'])], shell=False) + if not self.ccache_opts.get("show_stats"): + return + # zero ccache stats + getLog().info("Zero ccache stats:") + self.buildroot.doChroot(["ccache", "--zero-stats"], printOutput=True, shell=False) # set up the ccache dir. # we also set a few variables used by ccache to find the shared cache. @@ -69,3 +75,11 @@ def _ccachePreInitHook(self): file_util.mkdirIfAbsent(self.buildroot.make_chroot_path('/var/tmp/ccache')) file_util.mkdirIfAbsent(self.ccachePath) self.buildroot.uid_manager.changeOwner(self.ccachePath, recursive=True) + + # get some cache stats + def _ccachePostBuildHook(self): + """ show the cache hit stats """ + if not self.ccache_opts.get("show_stats"): + return + getLog().info("ccache stats:") + self.buildroot.doChroot(["ccache", "--show-stats"], printOutput=True, shell=False) diff --git a/releng/release-notes-next/ccache-show-stats.feature b/releng/release-notes-next/ccache-show-stats.feature new file mode 100644 index 000000000..f33241791 --- /dev/null +++ b/releng/release-notes-next/ccache-show-stats.feature @@ -0,0 +1,3 @@ +There's a new [ccache](Plugin-CCache) plugin option +`config_opts['plugin_conf']['ccache_opts']['show_stats']`; if set to `True`, +Mock prints the ccache statistics (hits/misses) to logs.