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/periodic/jfrThreadCPULoadEvent.hpp"
  28 #include "jfr/recorder/access/jfrThreadData.hpp"
  29 #include "jfr/utilities/jfrTraceTime.hpp"
  30 #include "utilities/globalDefinitions.hpp"
  31 #include "runtime/os.hpp"
  32 #include "runtime/thread.inline.hpp"
  33 #include "runtime/threadSMR.inline.hpp"
  34 #include "tracefiles/traceEventClasses.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   JfrThreadData* thread_data = thread->trace_data();
  57 
  58   jlong cur_cpu_time = os::thread_cpu_time(thread, true);
  59   jlong prev_cpu_time = thread_data->get_cpu_time();
  60 
  61   jlong prev_wallclock_time = thread_data->get_wallclock_time();
  62   thread_data->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 = thread_data->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_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     if (user_time > excess) {
  90       user_time -= excess;
  91       cur_user_time -= excess;
  92     } else {
  93       excess -= user_time;
  94       user_time = 0;
  95       cur_user_time = 0;
  96       system_time -= excess;
  97       cur_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   thread_data->set_user_time(cur_user_time);
 103   thread_data->set_cpu_time(cur_cpu_time);
 104   return true;
 105 }
 106 
 107 void JfrThreadCPULoadEvent::send_events() {
 108   Thread* periodic_thread = Thread::current();
 109   JfrThreadData* const periodic_trace_data = periodic_thread->trace_data();
 110   traceid periodic_thread_id = periodic_trace_data->thread_id();
 111   const int processor_count = JfrThreadCPULoadEvent::get_processor_count();
 112   JfrTraceTime event_time = JfrTraceTime::now();
 113   jlong cur_wallclock_time = JfrThreadCPULoadEvent::get_wallclock_time();
 114 
 115   JavaThreadIteratorWithHandle jtiwh;
 116   while (JavaThread* jt = jtiwh.next()) {
 117     EventThreadCPULoad event(UNTIMED);
 118     if (JfrThreadCPULoadEvent::update_event(event, jt, cur_wallclock_time, processor_count)) {
 119       event.set_starttime(event_time);
 120       if (jt != periodic_thread) {
 121         // Commit reads the thread id from this thread's trace data, so put it there temporarily
 122         periodic_trace_data->set_thread_id(THREAD_TRACE_ID(jt));
 123       } else {
 124         periodic_trace_data->set_thread_id(periodic_thread_id);
 125       }
 126       event.commit();
 127     }
 128   }
 129   log_trace(jfr)("Measured CPU usage for %d threads in %.3f milliseconds", jtiwh.length(),
 130     (double)(JfrTraceTime::now() - event_time) / (JfrTraceTime::frequency() / 1000));
 131   // Restore this thread's thread id
 132   periodic_trace_data->set_thread_id(periodic_thread_id);
 133 }
 134 
 135 void JfrThreadCPULoadEvent::send_event_for_thread(JavaThread* jt) {
 136   EventThreadCPULoad event;
 137   if (event.should_commit()) {
 138     if (update_event(event, jt, get_wallclock_time(), get_processor_count())) {
 139       event.commit();
 140     }
 141   }
 142 }