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 "logging/log.hpp"
26 #include "oops/oop.inline.hpp"
27 #include "runtime/os.hpp"
28 #include "runtime/timer.hpp"
29 #include "utilities/ostream.hpp"
30
31 double TimeHelper::counter_to_seconds(jlong counter) {
32 double freq = (double) os::elapsed_frequency();
33 return (double)counter / freq;
34 }
35
36 double TimeHelper::counter_to_millis(jlong counter) {
37 return counter_to_seconds(counter) * 1000.0;
38 }
39
40 jlong TimeHelper::millis_to_counter(jlong millis) {
41 jlong freq = os::elapsed_frequency() / MILLIUNITS;
42 return millis * freq;
43 }
44
45 jlong TimeHelper::micros_to_counter(jlong micros) {
46 jlong freq = os::elapsed_frequency() / MICROUNITS;
47 return micros * freq;
48 }
49
50 void elapsedTimer::add(elapsedTimer t) {
51 _counter += t._counter;
52 }
53
54 void elapsedTimer::add_nanoseconds(jlong ns) {
55 jlong freq = os::elapsed_frequency() / NANOUNITS;
56 _counter += ns * freq;
57 }
58
59 void elapsedTimer::start() {
60 if (!_active) {
61 _active = true;
62 _start_counter = os::elapsed_counter();
63 }
64 }
65
66 void elapsedTimer::stop() {
67 if (_active) {
68 _counter += os::elapsed_counter() - _start_counter;
69 _active = false;
70 }
71 }
72
73 double elapsedTimer::seconds() const {
74 return TimeHelper::counter_to_seconds(_counter);
75 }
76
77 jlong elapsedTimer::milliseconds() const {
78 return (jlong)TimeHelper::counter_to_millis(_counter);
79 }
80
81 jlong elapsedTimer::active_ticks() const {
82 if (!_active) {
83 return ticks();
84 }
85 jlong counter = _counter + os::elapsed_counter() - _start_counter;
86 return counter;
87 }
88
89 void TimeStamp::update_to(jlong ticks) {
90 _counter = ticks;
91 if (_counter == 0) _counter = 1;
92 assert(is_updated(), "must not look clear");
93 }
94
95 void TimeStamp::update() {
96 update_to(os::elapsed_counter());
97 }
98
99 double TimeStamp::seconds() const {
100 assert(is_updated(), "must not be clear");
101 jlong new_count = os::elapsed_counter();
102 return TimeHelper::counter_to_seconds(new_count - _counter);
103 }
104
105 jlong TimeStamp::milliseconds() const {
106 assert(is_updated(), "must not be clear");
107 jlong new_count = os::elapsed_counter();
108 return (jlong)TimeHelper::counter_to_millis(new_count - _counter);
|
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 "logging/log.hpp"
26 #include "oops/oop.inline.hpp"
27 #include "runtime/os.hpp"
28 #include "runtime/thread.hpp"
29 #include "runtime/timer.hpp"
30 #include "utilities/ostream.hpp"
31
32 double TimeHelper::counter_to_seconds(jlong counter) {
33 double freq = (double) os::elapsed_frequency();
34 return (double)counter / freq;
35 }
36
37 double TimeHelper::counter_to_millis(jlong counter) {
38 return counter_to_seconds(counter) * 1000.0;
39 }
40
41 jlong TimeHelper::millis_to_counter(jlong millis) {
42 jlong freq = os::elapsed_frequency() / MILLIUNITS;
43 return millis * freq;
44 }
45
46 jlong TimeHelper::micros_to_counter(jlong micros) {
47 jlong freq = os::elapsed_frequency() / MICROUNITS;
48 return micros * freq;
49 }
50
51 void BaseTimer::add(BaseTimer* t) {
52 _counter += t->_counter;
53 }
54
55 void BaseTimer::add_nanoseconds(jlong ns) {
56 jlong freq = os::elapsed_frequency() / NANOUNITS;
57 _counter += ns * freq;
58 }
59
60 void BaseTimer::start() {
61 if (!_active) {
62 _active = true;
63 _start_counter = read_counter();
64 }
65 }
66
67 void BaseTimer::stop() {
68 if (_active) {
69 _counter += read_counter() - _start_counter;
70 _active = false;
71 }
72 }
73
74 double BaseTimer::seconds() const {
75 return TimeHelper::counter_to_seconds(_counter);
76 }
77
78 jlong BaseTimer::milliseconds() const {
79 return (jlong)TimeHelper::counter_to_millis(_counter);
80 }
81
82 jlong BaseTimer::active_ticks() const {
83 if (!_active) {
84 return ticks();
85 }
86 jlong counter = _counter + read_counter() - _start_counter;
87 return counter;
88 }
89
90 jlong elapsedTimer::read_counter() const {
91 return os::elapsed_counter();
92 }
93
94 ThreadTimer::ThreadTimer() {
95 _owner = Thread::current();
96 }
97
98 void ThreadTimer::start() {
99 assert(_owner != nullptr, "timer must be bound to a thread");
100 Thread* current = Thread::current();
101 assert(current == _owner, "timer can only be started by the thread that owns it");
102 BaseTimer::start();
103 }
104
105 void ThreadTimer::stop() {
106 assert(_owner != nullptr, "sanity check");
107 Thread* current = Thread::current();
108 assert(current == _owner, "timer can only be stopped by the thread that owns it");
109 if (_start_counter != -1) {
110 BaseTimer::stop();
111 }
112 }
113
114 jlong ThreadTimer::read_counter() const {
115 assert(_owner != nullptr, "sanity check");
116 return os::thread_cpu_time(_owner);
117 }
118
119 void TimeStamp::update_to(jlong ticks) {
120 _counter = ticks;
121 if (_counter == 0) _counter = 1;
122 assert(is_updated(), "must not look clear");
123 }
124
125 void TimeStamp::update() {
126 update_to(os::elapsed_counter());
127 }
128
129 double TimeStamp::seconds() const {
130 assert(is_updated(), "must not be clear");
131 jlong new_count = os::elapsed_counter();
132 return TimeHelper::counter_to_seconds(new_count - _counter);
133 }
134
135 jlong TimeStamp::milliseconds() const {
136 assert(is_updated(), "must not be clear");
137 jlong new_count = os::elapsed_counter();
138 return (jlong)TimeHelper::counter_to_millis(new_count - _counter);
|