Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/src/share/vm/jfr/periodic/jfrThreadCPULoadEvent.cpp
38920 views
/*1* Copyright (c) 2017, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324#include "precompiled.hpp"25#include "jfr/jfrEvents.hpp"26#include "jfr/periodic/jfrThreadCPULoadEvent.hpp"27#include "jfr/support/jfrThreadId.hpp"28#include "jfr/support/jfrThreadLocal.hpp"29#include "jfr/utilities/jfrTime.hpp"30#include "utilities/globalDefinitions.hpp"31#include "runtime/os.hpp"32#include "runtime/thread.inline.hpp"3334jlong JfrThreadCPULoadEvent::get_wallclock_time() {35return os::javaTimeNanos();36}3738int JfrThreadCPULoadEvent::_last_active_processor_count = 0;3940int JfrThreadCPULoadEvent::get_processor_count() {41int cur_processor_count = os::active_processor_count();42int last_processor_count = _last_active_processor_count;43_last_active_processor_count = cur_processor_count;4445// If the number of processors decreases, we don't know at what point during46// the sample interval this happened, so use the largest number to try47// to avoid percentages above 100%48return MAX2(cur_processor_count, last_processor_count);49}5051// Returns false if the thread has not been scheduled since the last call to updateEvent52// (i.e. the delta for both system and user time is 0 milliseconds)53bool JfrThreadCPULoadEvent::update_event(EventThreadCPULoad& event, JavaThread* thread, jlong cur_wallclock_time, int processor_count) {54JfrThreadLocal* const tl = thread->jfr_thread_local();5556jlong cur_cpu_time = os::thread_cpu_time(thread, true);57jlong prev_cpu_time = tl->get_cpu_time();5859jlong prev_wallclock_time = tl->get_wallclock_time();60tl->set_wallclock_time(cur_wallclock_time);6162// Threshold of 1 ms63if (cur_cpu_time - prev_cpu_time < 1 * NANOSECS_PER_MILLISEC) {64return false;65}6667jlong cur_user_time = os::thread_cpu_time(thread, false);68jlong prev_user_time = tl->get_user_time();6970jlong cur_system_time = cur_cpu_time - cur_user_time;71jlong prev_system_time = prev_cpu_time - prev_user_time;7273// The user and total cpu usage clocks can have different resolutions, which can74// make us see decreasing system time. Ensure time doesn't go backwards.75if (prev_system_time > cur_system_time) {76cur_cpu_time += prev_system_time - cur_system_time;77cur_system_time = prev_system_time;78}7980jlong user_time = cur_user_time - prev_user_time;81jlong system_time = cur_system_time - prev_system_time;82jlong wallclock_time = cur_wallclock_time - prev_wallclock_time;83jlong total_available_time = wallclock_time * processor_count;8485// Avoid reporting percentages above the theoretical max86if (user_time + system_time > wallclock_time) {87jlong excess = user_time + system_time - wallclock_time;88cur_cpu_time -= excess;89if (user_time > excess) {90user_time -= excess;91cur_user_time -= excess;92} else {93excess -= user_time;94cur_user_time -= user_time;95user_time = 0;96system_time -= excess;97}98}99event.set_user(total_available_time > 0 ? (double)user_time / total_available_time : 0);100event.set_system(total_available_time > 0 ? (double)system_time / total_available_time : 0);101tl->set_user_time(cur_user_time);102tl->set_cpu_time(cur_cpu_time);103return true;104}105106void JfrThreadCPULoadEvent::send_events() {107Thread* periodic_thread = Thread::current();108JfrThreadLocal* const periodic_thread_tl = periodic_thread->jfr_thread_local();109traceid periodic_thread_id = periodic_thread_tl->thread_id();110const int processor_count = JfrThreadCPULoadEvent::get_processor_count();111JfrTicks event_time = JfrTicks::now();112jlong cur_wallclock_time = JfrThreadCPULoadEvent::get_wallclock_time();113114{115MutexLockerEx ml(Threads_lock);116unsigned jt_count = 0;117for (JavaThread *jt = Threads::first(); jt != NULL; jt = jt->next()) {118EventThreadCPULoad event(UNTIMED);119if (JfrThreadCPULoadEvent::update_event(event, jt, cur_wallclock_time, processor_count)) {120event.set_starttime(event_time);121if (jt != periodic_thread) {122// Commit reads the thread id from this thread's trace data, so put it there temporarily123periodic_thread_tl->set_thread_id(JFR_THREAD_ID(jt));124} else {125periodic_thread_tl->set_thread_id(periodic_thread_id);126}127event.commit();128}129jt_count++;130}131if (LogJFR && Verbose) tty->print_cr("Measured CPU usage for %d threads in %.3f milliseconds", jt_count,132(double)(JfrTicks::now() - event_time).milliseconds());133}134// Restore this thread's thread id135periodic_thread_tl->set_thread_id(periodic_thread_id);136}137138void JfrThreadCPULoadEvent::send_event_for_thread(JavaThread* jt) {139EventThreadCPULoad event;140if (event.should_commit()) {141if (update_event(event, jt, get_wallclock_time(), get_processor_count())) {142event.commit();143}144}145}146147148