From 3e2d55524104f071acca940abbc554142b8a5481 Mon Sep 17 00:00:00 2001 From: Henning Baldersheim Date: Fri, 15 Oct 2021 09:41:57 +0200 Subject: Metrics Q size and capacity must into account what kind of Q is used. --- .../handler/threadpool/ExecutorServiceWrapper.java | 24 ++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) (limited to 'container-core/src/main/java/com/yahoo/container/handler') diff --git a/container-core/src/main/java/com/yahoo/container/handler/threadpool/ExecutorServiceWrapper.java b/container-core/src/main/java/com/yahoo/container/handler/threadpool/ExecutorServiceWrapper.java index 5ba5985db37..99f49f10526 100644 --- a/container-core/src/main/java/com/yahoo/container/handler/threadpool/ExecutorServiceWrapper.java +++ b/container-core/src/main/java/com/yahoo/container/handler/threadpool/ExecutorServiceWrapper.java @@ -26,6 +26,7 @@ class ExecutorServiceWrapper extends ForwardingExecutorService { private final long maxThreadExecutionTimeMillis; private final int queueCapacity; private final Thread metricReporter; + private final boolean threadPoolIsOnlyQ; private final AtomicBoolean closed = new AtomicBoolean(false); ExecutorServiceWrapper(WorkerCompletionTimingThreadPoolExecutor wrapped, @@ -37,21 +38,32 @@ class ExecutorServiceWrapper extends ForwardingExecutorService { this.metric = metric; this.processTerminator = processTerminator; this.maxThreadExecutionTimeMillis = maxThreadExecutionTimeMillis; - this.queueCapacity = wrapped.getMaximumPoolSize() + wrapped.getQueue().remainingCapacity() + wrapped.getQueue().size(); + int maxQueueCapacity = wrapped.getQueue().remainingCapacity() + wrapped.getQueue().size(); + this.threadPoolIsOnlyQ = (maxQueueCapacity == 0); + this.queueCapacity = threadPoolIsOnlyQ + ? wrapped.getMaximumPoolSize() + : maxQueueCapacity; metric.reportThreadPoolSize(wrapped.getPoolSize()); metric.reportActiveThreads(wrapped.getActiveCount()); - metricReporter = new Thread(this::reportMetrics); + reportMetrics(); + metricReporter = new Thread(this::reportMetricsRegularly); metricReporter.setName(name + "-threadpool-metric-reporter"); metricReporter.start(); } private void reportMetrics() { + int activeThreads = wrapped.getActiveCount(); + metric.reportThreadPoolSize(wrapped.getPoolSize()); + metric.reportActiveThreads(activeThreads); + int queueSize = threadPoolIsOnlyQ ? activeThreads : wrapped.getQueue().size(); + metric.reportWorkQueueSize(queueSize); + metric.reportWorkQueueCapacity(queueCapacity); + } + + private void reportMetricsRegularly() { while (timeToReportMetricsAgain(100)) { - metric.reportThreadPoolSize(wrapped.getPoolSize()); - metric.reportActiveThreads(wrapped.getActiveCount()); - metric.reportWorkQueueSize(wrapped.getQueue().size()); - metric.reportWorkQueueCapacity(queueCapacity); + reportMetrics(); } } private boolean timeToReportMetricsAgain(int timeoutMS) { -- cgit v1.2.3