Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/src/share/vm/jfr/periodic/jfrThreadCPULoadEvent.cpp
38920 views
1
/*
2
* Copyright (c) 2017, 2018, 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 "jfr/jfrEvents.hpp"
27
#include "jfr/periodic/jfrThreadCPULoadEvent.hpp"
28
#include "jfr/support/jfrThreadId.hpp"
29
#include "jfr/support/jfrThreadLocal.hpp"
30
#include "jfr/utilities/jfrTime.hpp"
31
#include "utilities/globalDefinitions.hpp"
32
#include "runtime/os.hpp"
33
#include "runtime/thread.inline.hpp"
34
35
jlong JfrThreadCPULoadEvent::get_wallclock_time() {
36
return os::javaTimeNanos();
37
}
38
39
int JfrThreadCPULoadEvent::_last_active_processor_count = 0;
40
41
int JfrThreadCPULoadEvent::get_processor_count() {
42
int cur_processor_count = os::active_processor_count();
43
int last_processor_count = _last_active_processor_count;
44
_last_active_processor_count = cur_processor_count;
45
46
// If the number of processors decreases, we don't know at what point during
47
// the sample interval this happened, so use the largest number to try
48
// to avoid percentages above 100%
49
return MAX2(cur_processor_count, last_processor_count);
50
}
51
52
// Returns false if the thread has not been scheduled since the last call to updateEvent
53
// (i.e. the delta for both system and user time is 0 milliseconds)
54
bool JfrThreadCPULoadEvent::update_event(EventThreadCPULoad& event, JavaThread* thread, jlong cur_wallclock_time, int processor_count) {
55
JfrThreadLocal* const tl = thread->jfr_thread_local();
56
57
jlong cur_cpu_time = os::thread_cpu_time(thread, true);
58
jlong prev_cpu_time = tl->get_cpu_time();
59
60
jlong prev_wallclock_time = tl->get_wallclock_time();
61
tl->set_wallclock_time(cur_wallclock_time);
62
63
// Threshold of 1 ms
64
if (cur_cpu_time - prev_cpu_time < 1 * NANOSECS_PER_MILLISEC) {
65
return false;
66
}
67
68
jlong cur_user_time = os::thread_cpu_time(thread, false);
69
jlong prev_user_time = tl->get_user_time();
70
71
jlong cur_system_time = cur_cpu_time - cur_user_time;
72
jlong prev_system_time = prev_cpu_time - prev_user_time;
73
74
// The user and total cpu usage clocks can have different resolutions, which can
75
// make us see decreasing system time. Ensure time doesn't go backwards.
76
if (prev_system_time > cur_system_time) {
77
cur_cpu_time += prev_system_time - cur_system_time;
78
cur_system_time = prev_system_time;
79
}
80
81
jlong user_time = cur_user_time - prev_user_time;
82
jlong system_time = cur_system_time - prev_system_time;
83
jlong wallclock_time = cur_wallclock_time - prev_wallclock_time;
84
jlong total_available_time = wallclock_time * processor_count;
85
86
// Avoid reporting percentages above the theoretical max
87
if (user_time + system_time > wallclock_time) {
88
jlong excess = user_time + system_time - wallclock_time;
89
cur_cpu_time -= excess;
90
if (user_time > excess) {
91
user_time -= excess;
92
cur_user_time -= excess;
93
} else {
94
excess -= user_time;
95
cur_user_time -= user_time;
96
user_time = 0;
97
system_time -= excess;
98
}
99
}
100
event.set_user(total_available_time > 0 ? (double)user_time / total_available_time : 0);
101
event.set_system(total_available_time > 0 ? (double)system_time / total_available_time : 0);
102
tl->set_user_time(cur_user_time);
103
tl->set_cpu_time(cur_cpu_time);
104
return true;
105
}
106
107
void JfrThreadCPULoadEvent::send_events() {
108
Thread* periodic_thread = Thread::current();
109
JfrThreadLocal* const periodic_thread_tl = periodic_thread->jfr_thread_local();
110
traceid periodic_thread_id = periodic_thread_tl->thread_id();
111
const int processor_count = JfrThreadCPULoadEvent::get_processor_count();
112
JfrTicks event_time = JfrTicks::now();
113
jlong cur_wallclock_time = JfrThreadCPULoadEvent::get_wallclock_time();
114
115
{
116
MutexLockerEx ml(Threads_lock);
117
unsigned jt_count = 0;
118
for (JavaThread *jt = Threads::first(); jt != NULL; jt = jt->next()) {
119
EventThreadCPULoad event(UNTIMED);
120
if (JfrThreadCPULoadEvent::update_event(event, jt, cur_wallclock_time, processor_count)) {
121
event.set_starttime(event_time);
122
if (jt != periodic_thread) {
123
// Commit reads the thread id from this thread's trace data, so put it there temporarily
124
periodic_thread_tl->set_thread_id(JFR_THREAD_ID(jt));
125
} else {
126
periodic_thread_tl->set_thread_id(periodic_thread_id);
127
}
128
event.commit();
129
}
130
jt_count++;
131
}
132
if (LogJFR && Verbose) tty->print_cr("Measured CPU usage for %d threads in %.3f milliseconds", jt_count,
133
(double)(JfrTicks::now() - event_time).milliseconds());
134
}
135
// Restore this thread's thread id
136
periodic_thread_tl->set_thread_id(periodic_thread_id);
137
}
138
139
void JfrThreadCPULoadEvent::send_event_for_thread(JavaThread* jt) {
140
EventThreadCPULoad event;
141
if (event.should_commit()) {
142
if (update_event(event, jt, get_wallclock_time(), get_processor_count())) {
143
event.commit();
144
}
145
}
146
}
147
148