1 /*
2  * Copyright (c) 2016, 2019, 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 #ifndef SHARE_RUNTIME_TIMERTRACE_HPP
26 #define SHARE_RUNTIME_TIMERTRACE_HPP
27 
28 #include "logging/log.hpp"
29 #include "utilities/globalDefinitions.hpp"
30 
31 // TraceTime is used for tracing the execution time of a block
32 // Usage:
33 //  {
34 //    TraceTime t("some timer", TIMERTRACE_LOG(Info, startuptime, tagX...));
35 //    some_code();
36 //  }
37 //
38 
39 typedef void (*TraceTimerLogPrintFunc)(const char*, ...);
40 
41 // We need to explicit take address of LogImpl<>write<> and static cast
42 // due to MSVC is not compliant with templates two-phase lookup
43 #define TRACETIME_LOG(TT_LEVEL, ...) \
44     log_is_enabled(TT_LEVEL, __VA_ARGS__) ? static_cast<TraceTimerLogPrintFunc>(&LogImpl<LOG_TAGS(__VA_ARGS__)>::write<LogLevel::TT_LEVEL>) : (TraceTimerLogPrintFunc)NULL
45 
46 class TraceTime: public StackObj {
47  private:
48   bool          _active;    // do timing
49   bool          _verbose;   // report every timing
50   elapsedTimer  _t;         // timer
51   elapsedTimer* _accum;     // accumulator
52   const char*   _title;     // name of timer
53   TraceTimerLogPrintFunc _print;
54 
55  public:
56   // Constructors
57   TraceTime(const char* title,
58             bool doit = true);
59 
60   TraceTime(const char* title,
61             elapsedTimer* accumulator,
62             bool doit = true,
63             bool verbose = false);
64 
65   TraceTime(const char* title,
66             TraceTimerLogPrintFunc ttlpf);
67 
68   ~TraceTime();
69 
70   // Accessors
set_verbose(bool verbose)71   void set_verbose(bool verbose)  { _verbose = verbose; }
verbose() const72   bool verbose() const            { return _verbose;    }
73 
74   // Activation
suspend()75   void suspend()  { if (_active) _t.stop();  }
resume()76   void resume()   { if (_active) _t.start(); }
77 };
78 
79 
80 #endif // SHARE_RUNTIME_TIMERTRACE_HPP
81