1 /*
 2  * Copyright (c) 2016, 2025, 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 "runtime/timerTrace.hpp"
26 #include "utilities/ostream.hpp"
27 
28 TraceTime::TraceTime(const char* title,
29                      bool doit) {
30   _active   = doit;
31   _verbose  = true;
32   _title    = title;
33   _print    = nullptr;
34 
35   if (_active) {
36     _accum = nullptr;
37     _t.start();
38   }
39 }
40 
41 TraceTime::TraceTime(const char* title,
42                      elapsedTimer* accumulator,
43                      bool doit,
44                      bool verbose) {
45   _active   = doit;
46   _verbose  = verbose;
47   _title    = title;
48   _print    = nullptr;
49 
50   if (_active) {
51     _accum = accumulator;
52     _t.start();
53   }
54 }
55 
56 TraceTime::TraceTime(const char* title,
57                      TraceTimerLogPrintFunc ttlpf) {
58   _active   = ttlpf!= nullptr;
59   _verbose  = true;
60   _title    = title;
61   _print    = ttlpf;
62 
63   if (_active) {
64     _accum = nullptr;
65     _t.start();
66   }
67 }
68 
69 TraceTime::~TraceTime() {
70   if (!_active) {
71     return;
72   }
73   _t.stop();
74   if (_accum != nullptr) {
75     _accum->add(&_t);
76   }
77   if (!_verbose) {
78     return;
79   }
80   if (_print) {
81     _print("%s, %3.7f secs", _title, _t.seconds());
82   } else {
83     tty->print_cr("[%s, %3.7f secs]", _title, _t.seconds());
84     tty->flush();
85   }
86 }
87