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