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 <thomas.wolf@paranor.ch>
This commit is contained in:
Thomas Wolf 2020-08-05 00:58:00 +02:00 committed by Matthias Sohn
parent dc856c28ba
commit 71aeedb6ec
1 changed files with 15 additions and 7 deletions

View File

@ -1,6 +1,6 @@
/*
* Copyright (C) 2008-2009, Google Inc.
* Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org> and others
* Copyright (C) 2008, 2009 Google Inc.
* Copyright (C) 2008, 2020 Shawn O. Pearce <spearce@spearce.org> 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
*/