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