Damien Martin-Guillerez | f88f4d8 | 2015-09-25 13:56:55 +0000 | [diff] [blame] | 1 | // Copyright 2014 The Bazel Authors. All rights reserved. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package com.google.devtools.build.lib.server; |
| 16 | |
tomlu | a155b53 | 2017-11-08 20:12:47 +0100 | [diff] [blame] | 17 | import com.google.common.base.Preconditions; |
janakr | c3bcb98 | 2020-04-14 06:50:08 -0700 | [diff] [blame] | 18 | import com.google.common.flogger.GoogleLogger; |
ulfjack | 2602a17 | 2018-11-05 07:39:43 -0800 | [diff] [blame] | 19 | import com.google.common.util.concurrent.ThreadFactoryBuilder; |
Nathan Harmata | d480301 | 2015-09-08 20:03:22 +0000 | [diff] [blame] | 20 | import com.google.devtools.build.lib.profiler.AutoProfiler; |
janakr | 30e9915 | 2020-04-03 08:01:25 -0700 | [diff] [blame] | 21 | import com.google.devtools.build.lib.profiler.GoogleAutoProfilerUtils; |
Benjamin Peterson | f05c0a4 | 2018-08-08 05:29:34 -0700 | [diff] [blame] | 22 | import com.google.devtools.build.lib.util.StringUtilities; |
| 23 | import java.lang.management.ManagementFactory; |
| 24 | import java.lang.management.MemoryMXBean; |
| 25 | import java.lang.management.MemoryUsage; |
Googler | 78cae6d | 2017-01-24 23:07:40 +0000 | [diff] [blame] | 26 | import java.util.concurrent.Future; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 27 | import java.util.concurrent.ScheduledThreadPoolExecutor; |
| 28 | import java.util.concurrent.TimeUnit; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 29 | |
| 30 | /** |
| 31 | * Run cleanup-related tasks during idle periods in the server. |
| 32 | * idle() and busy() must be called in that order, and only once. |
| 33 | */ |
| 34 | class IdleServerTasks { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 35 | private final ScheduledThreadPoolExecutor executor; |
janakr | c3bcb98 | 2020-04-14 06:50:08 -0700 | [diff] [blame] | 36 | private static final GoogleLogger logger = GoogleLogger.forEnclosingClass(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 37 | |
Benjamin Peterson | f05c0a4 | 2018-08-08 05:29:34 -0700 | [diff] [blame] | 38 | /** Must be called from the main thread. */ |
lberki | d9b1082 | 2018-04-03 03:17:21 -0700 | [diff] [blame] | 39 | public IdleServerTasks() { |
ulfjack | 2602a17 | 2018-11-05 07:39:43 -0800 | [diff] [blame] | 40 | this.executor = new ScheduledThreadPoolExecutor( |
| 41 | 1, |
| 42 | new ThreadFactoryBuilder().setNameFormat("idle-server-tasks-%d").build()); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Called when the server becomes idle. Should not block, but may invoke |
| 47 | * new threads. |
| 48 | */ |
| 49 | public void idle() { |
| 50 | Preconditions.checkState(!executor.isShutdown()); |
| 51 | |
lberki | 97abb52 | 2017-09-04 18:51:57 +0200 | [diff] [blame] | 52 | @SuppressWarnings("unused") |
Googler | 78cae6d | 2017-01-24 23:07:40 +0000 | [diff] [blame] | 53 | Future<?> possiblyIgnoredError = |
lberki | 97abb52 | 2017-09-04 18:51:57 +0200 | [diff] [blame] | 54 | executor.schedule( |
| 55 | () -> { |
Benjamin Peterson | f05c0a4 | 2018-08-08 05:29:34 -0700 | [diff] [blame] | 56 | MemoryMXBean memBean = ManagementFactory.getMemoryMXBean(); |
| 57 | MemoryUsage before = memBean.getHeapMemoryUsage(); |
janakr | 30e9915 | 2020-04-03 08:01:25 -0700 | [diff] [blame] | 58 | try (AutoProfiler p = GoogleAutoProfilerUtils.logged("Idle GC")) { |
lberki | 8981f7e | 2017-06-29 11:48:10 +0200 | [diff] [blame] | 59 | System.gc(); |
Googler | 78cae6d | 2017-01-24 23:07:40 +0000 | [diff] [blame] | 60 | } |
Benjamin Peterson | f05c0a4 | 2018-08-08 05:29:34 -0700 | [diff] [blame] | 61 | MemoryUsage after = memBean.getHeapMemoryUsage(); |
janakr | c3bcb98 | 2020-04-14 06:50:08 -0700 | [diff] [blame] | 62 | logger.atInfo().log( |
| 63 | "[Idle GC] used: %s -> %s, committed: %s -> %s", |
| 64 | StringUtilities.prettyPrintBytes(before.getUsed()), |
| 65 | StringUtilities.prettyPrintBytes(after.getUsed()), |
| 66 | StringUtilities.prettyPrintBytes(before.getCommitted()), |
| 67 | StringUtilities.prettyPrintBytes(after.getCommitted())); |
Googler | 78cae6d | 2017-01-24 23:07:40 +0000 | [diff] [blame] | 68 | }, |
| 69 | 10, |
| 70 | TimeUnit.SECONDS); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Called by the main thread when the server gets to work. |
| 75 | * Should return quickly. |
| 76 | */ |
| 77 | public void busy() { |
| 78 | Preconditions.checkState(!executor.isShutdown()); |
| 79 | |
| 80 | // Make sure tasks are finished after shutdown(), so they do not intefere |
| 81 | // with subsequent server invocations. |
| 82 | executor.shutdown(); |
| 83 | executor.setContinueExistingPeriodicTasksAfterShutdownPolicy(false); |
| 84 | executor.setExecuteExistingDelayedTasksAfterShutdownPolicy(false); |
| 85 | |
| 86 | boolean interrupted = false; |
| 87 | while (true) { |
| 88 | try { |
| 89 | executor.awaitTermination(Long.MAX_VALUE, TimeUnit.HOURS); |
| 90 | break; |
| 91 | } catch (InterruptedException e) { |
| 92 | // It's unsafe to leak threads - just reset the interrupt bit later. |
| 93 | interrupted = true; |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | if (interrupted) { |
| 98 | Thread.currentThread().interrupt(); |
| 99 | } |
| 100 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 101 | } |