1 /*
 2  * Copyright (c) 2016, 2023, Oracle and/or its affiliates. All rights reserved.
 3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 4  *
 5  * This code is free software; you can redistribute it and/or modify it
 6  * under the terms of the GNU General Public License version 2 only, as
 7  * published by the Free Software Foundation.
 8  *
 9  * This code is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12  * version 2 for more details (a copy is included in the LICENSE file that
13  * accompanied this code).
14  *
15  * You should have received a copy of the GNU General Public License version
16  * 2 along with this work; if not, write to the Free Software Foundation,
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18  *
19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20  * or visit www.oracle.com if you need additional information or have any
21  * questions.
22  *
23  */
24 
25 #include "precompiled.hpp"
26 #include "runtime/timerTrace.hpp"
27 #include "utilities/ostream.hpp"
28 
29 TraceTime::TraceTime(const char* title,
30                      bool doit) {
31   _active   = doit;
32   _verbose  = true;
33   _title    = title;
34   _print    = nullptr;
35 
36   if (_active) {
37     _accum = nullptr;
38     _t.start();
39   }
40 }
41 
42 TraceTime::TraceTime(const char* title,
43                      elapsedTimer* accumulator,
44                      bool doit,
45                      bool verbose) {
46   _active   = doit;
47   _verbose  = verbose;
48   _title    = title;
49   _print    = nullptr;
50 
51   if (_active) {
52     _accum = accumulator;
53     _t.start();
54   }
55 }
56 
57 TraceTime::TraceTime(const char* title,
58                      TraceTimerLogPrintFunc ttlpf) {
59   _active   = ttlpf!= nullptr;
60   _verbose  = true;
61   _title    = title;
62   _print    = ttlpf;
63 
64   if (_active) {
65     _accum = nullptr;
66     _t.start();
67   }
68 }
69 
70 TraceTime::~TraceTime() {
71   if (!_active) {
72     return;
73   }
74   _t.stop();
75   if (_accum != nullptr) {
76     _accum->add(_t);
77   }
78   if (!_verbose) {
79     return;
80   }
81   if (_print) {
82     _print("%s, %3.7f secs", _title, _t.seconds());
83   } else {
84     tty->print_cr("[%s, %3.7f secs]", _title, _t.seconds());
85     tty->flush();
86   }
87 }
88