1 /*
  2  * Copyright (c) 2017, 2019, 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/jfrThreadIterator.hpp"
 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     cur_cpu_time -= excess;
 91     if (user_time > excess) {
 92       user_time -= excess;
 93       cur_user_time -= excess;
 94     } else {
 95       excess -= user_time;
 96       cur_user_time -= user_time;
 97       user_time = 0;
 98       system_time -= excess;
 99     }
100   }
101   event.set_user(total_available_time > 0 ? (double)user_time / total_available_time : 0);
102   event.set_system(total_available_time > 0 ? (double)system_time / total_available_time : 0);
103   tl->set_user_time(cur_user_time);
104   tl->set_cpu_time(cur_cpu_time);
105   return true;
106 }
107 
108 void JfrThreadCPULoadEvent::send_events() {
109   Thread* periodic_thread = Thread::current();
110   JfrThreadLocal* const periodic_thread_tl = periodic_thread->jfr_thread_local();
111   traceid periodic_thread_id = periodic_thread_tl->thread_id();
112   const int processor_count = JfrThreadCPULoadEvent::get_processor_count();
113   JfrTicks event_time = JfrTicks::now();
114   jlong cur_wallclock_time = JfrThreadCPULoadEvent::get_wallclock_time();
115 
116   JfrJavaThreadIterator iter;
117   int number_of_threads = 0;
118   while (iter.has_next()) {
119     JavaThread* const jt = iter.next();
120     assert(jt != NULL, "invariant");
121     ++number_of_threads;
122     EventThreadCPULoad event(UNTIMED);
123     if (JfrThreadCPULoadEvent::update_event(event, jt, cur_wallclock_time, processor_count)) {
124       event.set_starttime(event_time);
125       if (jt != periodic_thread) {
126         // Commit reads the thread id from this thread's trace data, so put it there temporarily
127         periodic_thread_tl->set_thread_id(JFR_THREAD_ID(jt));
128       } else {
129         periodic_thread_tl->set_thread_id(periodic_thread_id);
130       }
131       event.commit();
132     }
133   }
134   log_trace(jfr)("Measured CPU usage for %d threads in %.3f milliseconds", number_of_threads,
135     (double)(JfrTicks::now() - event_time).milliseconds());
136   // Restore this thread's thread id
137   periodic_thread_tl->set_thread_id(periodic_thread_id);
138 }
139 
140 void JfrThreadCPULoadEvent::send_event_for_thread(JavaThread* jt) {
141   EventThreadCPULoad event;
142   if (event.should_commit()) {
143     if (update_event(event, jt, get_wallclock_time(), get_processor_count())) {
144       event.commit();
145     }
146   }
147 }