From 71aeedb6ec79a91e41251a88c1ab3235c40a9b70 Mon Sep 17 00:00:00 2001 From: Thomas Wolf Date: Wed, 5 Aug 2020 00:58:00 +0200 Subject: [PATCH] Delay WindowCache statistics JMX MBean registration The WindowCache is configured statically with a default WindowCacheConfig. The default config says (for backwards compatibility reasons) to publish the MBean. As a result, the MBean always gets published. By delaying the MBean registration until the first call to getInstance() or get(PackFile, long) we can avoid the forced registration and do it only if not re-configured in the meantime not to publish the bean. (As is done by Egit, to avoid a very early costly access to the user and system config during plug-in activation.) Bug: 563740 Change-Id: I8a941342c0833acee2107515e64299aada7e0520 Signed-off-by: Thomas Wolf --- .../internal/storage/file/WindowCache.java | 22 +++++++++++++------ 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/file/WindowCache.java b/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/file/WindowCache.java index 80c8e10de..3e8cb3a3f 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/file/WindowCache.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/file/WindowCache.java @@ -1,6 +1,6 @@ /* - * Copyright (C) 2008-2009, Google Inc. - * Copyright (C) 2008, Shawn O. Pearce and others + * Copyright (C) 2008, 2009 Google Inc. + * Copyright (C) 2008, 2020 Shawn O. Pearce and others * * This program and the accompanying materials are made available under the * terms of the Eclipse Distribution License v. 1.0 which is available at @@ -19,6 +19,7 @@ import java.util.Random; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentLinkedQueue; +import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicLong; import java.util.concurrent.atomic.AtomicReferenceArray; import java.util.concurrent.atomic.LongAdder; @@ -376,14 +377,14 @@ static int getStreamFileThreshold() { * @return the cached instance. */ public static WindowCache getInstance() { - return cache; + return cache.publishMBeanIfNeeded(); } static final ByteWindow get(PackFile pack, long offset) throws IOException { final WindowCache c = cache; final ByteWindow r = c.getOrLoad(pack, c.toStart(offset)); - if (c != cache) { + if (c != cache.publishMBeanIfNeeded()) { // The cache was reconfigured while we were using the old one // to load this window. The window is still valid, but our // cache may think its still live. Ensure the window is removed @@ -433,6 +434,8 @@ static final void purge(PackFile pack) { private final StatsRecorderImpl mbean; + private final AtomicBoolean publishMBean = new AtomicBoolean(); + private boolean useStrongRefs; private WindowCache(WindowCacheConfig cfg) { @@ -470,9 +473,7 @@ else if (eb < 4) mbean = new StatsRecorderImpl(); statsRecorder = mbean; - if (cfg.getExposeStatsViaJmx()) { - Monitoring.registerMBean(mbean, "block_cache"); //$NON-NLS-1$ - } + publishMBean.set(cfg.getExposeStatsViaJmx()); if (maxFiles < 1) throw new IllegalArgumentException(JGitText.get().openFilesMustBeAtLeast1); @@ -480,6 +481,13 @@ else if (eb < 4) throw new IllegalArgumentException(JGitText.get().windowSizeMustBeLesserThanLimit); } + private WindowCache publishMBeanIfNeeded() { + if (publishMBean.getAndSet(false)) { + Monitoring.registerMBean(mbean, "block_cache"); //$NON-NLS-1$ + } + return this; + } + /** * @return cache statistics for the WindowCache */