author | mgronlun |
Mon, 26 Aug 2019 13:09:14 +0200 | |
branch | JEP-349-branch |
changeset 57878 | bffba8d6611a |
parent 57360 | 5d043a159d5c |
child 58185 | ff328fdeca2d |
permissions | -rw-r--r-- |
50113 | 1 |
/* |
57360 | 2 |
* Copyright (c) 2017, 2019, Oracle and/or its affiliates. All rights reserved. |
50113 | 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 "logging/log.hpp" |
|
27 |
#include "jfr/jfrEvents.hpp" |
|
28 |
#include "jfr/periodic/jfrThreadCPULoadEvent.hpp" |
|
29 |
#include "jfr/support/jfrThreadId.hpp" |
|
30 |
#include "jfr/support/jfrThreadLocal.hpp" |
|
57360 | 31 |
#include "jfr/utilities/jfrThreadIterator.hpp" |
50113 | 32 |
#include "jfr/utilities/jfrTime.hpp" |
33 |
#include "utilities/globalDefinitions.hpp" |
|
34 |
#include "runtime/os.hpp" |
|
35 |
||
36 |
jlong JfrThreadCPULoadEvent::get_wallclock_time() { |
|
37 |
return os::javaTimeNanos(); |
|
38 |
} |
|
39 |
||
40 |
int JfrThreadCPULoadEvent::_last_active_processor_count = 0; |
|
41 |
||
42 |
int JfrThreadCPULoadEvent::get_processor_count() { |
|
43 |
int cur_processor_count = os::active_processor_count(); |
|
44 |
int last_processor_count = _last_active_processor_count; |
|
45 |
_last_active_processor_count = cur_processor_count; |
|
46 |
||
47 |
// If the number of processors decreases, we don't know at what point during |
|
48 |
// the sample interval this happened, so use the largest number to try |
|
49 |
// to avoid percentages above 100% |
|
50 |
return MAX2(cur_processor_count, last_processor_count); |
|
51 |
} |
|
52 |
||
53 |
// Returns false if the thread has not been scheduled since the last call to updateEvent |
|
54 |
// (i.e. the delta for both system and user time is 0 milliseconds) |
|
55 |
bool JfrThreadCPULoadEvent::update_event(EventThreadCPULoad& event, JavaThread* thread, jlong cur_wallclock_time, int processor_count) { |
|
56 |
JfrThreadLocal* const tl = thread->jfr_thread_local(); |
|
57 |
||
58 |
jlong cur_cpu_time = os::thread_cpu_time(thread, true); |
|
59 |
jlong prev_cpu_time = tl->get_cpu_time(); |
|
60 |
||
61 |
jlong prev_wallclock_time = tl->get_wallclock_time(); |
|
62 |
tl->set_wallclock_time(cur_wallclock_time); |
|
63 |
||
64 |
// Threshold of 1 ms |
|
65 |
if (cur_cpu_time - prev_cpu_time < 1 * NANOSECS_PER_MILLISEC) { |
|
66 |
return false; |
|
67 |
} |
|
68 |
||
69 |
jlong cur_user_time = os::thread_cpu_time(thread, false); |
|
70 |
jlong prev_user_time = tl->get_user_time(); |
|
71 |
||
72 |
jlong cur_system_time = cur_cpu_time - cur_user_time; |
|
73 |
jlong prev_system_time = prev_cpu_time - prev_user_time; |
|
74 |
||
75 |
// The user and total cpu usage clocks can have different resolutions, which can |
|
76 |
// make us see decreasing system time. Ensure time doesn't go backwards. |
|
77 |
if (prev_system_time > cur_system_time) { |
|
78 |
cur_cpu_time += prev_system_time - cur_system_time; |
|
79 |
cur_system_time = prev_system_time; |
|
80 |
} |
|
81 |
||
82 |
jlong user_time = cur_user_time - prev_user_time; |
|
83 |
jlong system_time = cur_system_time - prev_system_time; |
|
84 |
jlong wallclock_time = cur_wallclock_time - prev_wallclock_time; |
|
85 |
jlong total_available_time = wallclock_time * processor_count; |
|
86 |
||
87 |
// Avoid reporting percentages above the theoretical max |
|
88 |
if (user_time + system_time > wallclock_time) { |
|
89 |
jlong excess = user_time + system_time - wallclock_time; |
|
90 |
if (user_time > excess) { |
|
91 |
user_time -= excess; |
|
92 |
cur_user_time -= excess; |
|
93 |
cur_cpu_time -= excess; |
|
94 |
} else { |
|
95 |
cur_cpu_time -= excess; |
|
96 |
excess -= user_time; |
|
97 |
user_time = 0; |
|
98 |
cur_user_time = 0; |
|
99 |
system_time -= excess; |
|
100 |
} |
|
101 |
} |
|
102 |
event.set_user(total_available_time > 0 ? (double)user_time / total_available_time : 0); |
|
103 |
event.set_system(total_available_time > 0 ? (double)system_time / total_available_time : 0); |
|
104 |
tl->set_user_time(cur_user_time); |
|
105 |
tl->set_cpu_time(cur_cpu_time); |
|
106 |
return true; |
|
107 |
} |
|
108 |
||
109 |
void JfrThreadCPULoadEvent::send_events() { |
|
110 |
Thread* periodic_thread = Thread::current(); |
|
111 |
JfrThreadLocal* const periodic_thread_tl = periodic_thread->jfr_thread_local(); |
|
112 |
traceid periodic_thread_id = periodic_thread_tl->thread_id(); |
|
113 |
const int processor_count = JfrThreadCPULoadEvent::get_processor_count(); |
|
114 |
JfrTicks event_time = JfrTicks::now(); |
|
115 |
jlong cur_wallclock_time = JfrThreadCPULoadEvent::get_wallclock_time(); |
|
116 |
||
57360 | 117 |
JfrJavaThreadIterator iter; |
118 |
int number_of_threads = 0; |
|
119 |
while (iter.has_next()) { |
|
120 |
JavaThread* const jt = iter.next(); |
|
121 |
assert(jt != NULL, "invariant"); |
|
57878
bffba8d6611a
Thread metadata to include excluded threads, thread sampler to exclude excluded threads, thread name friend
mgronlun
parents:
57360
diff
changeset
|
122 |
if (jt->jfr_thread_local()->is_excluded()) { |
bffba8d6611a
Thread metadata to include excluded threads, thread sampler to exclude excluded threads, thread name friend
mgronlun
parents:
57360
diff
changeset
|
123 |
continue; |
bffba8d6611a
Thread metadata to include excluded threads, thread sampler to exclude excluded threads, thread name friend
mgronlun
parents:
57360
diff
changeset
|
124 |
} |
57360 | 125 |
++number_of_threads; |
50113 | 126 |
EventThreadCPULoad event(UNTIMED); |
127 |
if (JfrThreadCPULoadEvent::update_event(event, jt, cur_wallclock_time, processor_count)) { |
|
128 |
event.set_starttime(event_time); |
|
129 |
if (jt != periodic_thread) { |
|
130 |
// Commit reads the thread id from this thread's trace data, so put it there temporarily |
|
131 |
periodic_thread_tl->set_thread_id(JFR_THREAD_ID(jt)); |
|
132 |
} else { |
|
133 |
periodic_thread_tl->set_thread_id(periodic_thread_id); |
|
134 |
} |
|
135 |
event.commit(); |
|
136 |
} |
|
137 |
} |
|
57360 | 138 |
log_trace(jfr)("Measured CPU usage for %d threads in %.3f milliseconds", number_of_threads, |
50113 | 139 |
(double)(JfrTicks::now() - event_time).milliseconds()); |
140 |
// Restore this thread's thread id |
|
141 |
periodic_thread_tl->set_thread_id(periodic_thread_id); |
|
142 |
} |
|
143 |
||
144 |
void JfrThreadCPULoadEvent::send_event_for_thread(JavaThread* jt) { |
|
145 |
EventThreadCPULoad event; |
|
146 |
if (event.should_commit()) { |
|
147 |
if (update_event(event, jt, get_wallclock_time(), get_processor_count())) { |
|
148 |
event.commit(); |
|
149 |
} |
|
150 |
} |
|
151 |
} |