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