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 | package com.google.devtools.build.lib.runtime; |
| 15 | |
| 16 | import com.google.devtools.build.lib.util.BlazeClock; |
| 17 | |
| 18 | import java.lang.management.GarbageCollectorMXBean; |
| 19 | import java.lang.management.ManagementFactory; |
| 20 | import java.util.Date; |
| 21 | |
| 22 | /** |
| 23 | * Base class for Command events that includes some resource fields. |
| 24 | */ |
| 25 | public abstract class CommandEvent { |
| 26 | |
| 27 | private final long eventTimeInNanos; |
| 28 | private final long eventTimeInEpochTime; |
| 29 | private final long gcTimeInMillis; |
| 30 | |
| 31 | protected CommandEvent() { |
| 32 | eventTimeInNanos = BlazeClock.nanoTime(); |
| 33 | eventTimeInEpochTime = new Date().getTime(); |
| 34 | gcTimeInMillis = collectGcTimeInMillis(); |
| 35 | } |
| 36 | |
| 37 | /** |
| 38 | * Returns time spent in garbage collection since the start of the JVM process. |
| 39 | */ |
| 40 | private static long collectGcTimeInMillis() { |
| 41 | long gcTime = 0; |
| 42 | for (GarbageCollectorMXBean gcBean : ManagementFactory.getGarbageCollectorMXBeans()) { |
| 43 | gcTime += gcBean.getCollectionTime(); |
| 44 | } |
| 45 | return gcTime; |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * Get the time-stamp in ns for the event. |
| 50 | */ |
| 51 | public long getEventTimeInNanos() { |
| 52 | return eventTimeInNanos; |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Get the time-stamp as epoch-time for the event. |
| 57 | */ |
| 58 | public long getEventTimeInEpochTime() { |
| 59 | return eventTimeInEpochTime; |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Get the cumulative GC time for the event. |
| 64 | */ |
| 65 | public long getGCTimeInMillis() { |
| 66 | return gcTimeInMillis; |
| 67 | } |
| 68 | } |