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 "jfr/jfrEvents.hpp"
  27 #include "jfr/periodic/jfrThreadCPULoadEvent.hpp"
  28 #include "jfr/support/jfrThreadId.hpp"
  29 #include "jfr/support/jfrThreadLocal.hpp"
  30 #include "jfr/utilities/jfrTime.hpp"
  31 #include "utilities/globalDefinitions.hpp"
  32 #include "runtime/os.hpp"
  33 #include "runtime/thread.inline.hpp"
  34 
  35 jlong JfrThreadCPULoadEvent::get_wallclock_time() {
  36   return os::javaTimeNanos();
  37 }
  38 
  39 int JfrThreadCPULoadEvent::_last_active_processor_count = 0;
  40 
  41 int JfrThreadCPULoadEvent::get_processor_count() {
  42   int cur_processor_count = os::active_processor_count();
  43   int last_processor_count = _last_active_processor_count;
  44   _last_active_processor_count = cur_processor_count;
  45 
  46   // If the number of processors decreases, we don't know at what point during
  47   // the sample interval this happened, so use the largest number to try
  48   // to avoid percentages above 100%
  49   return MAX2(cur_processor_count, last_processor_count);
  50 }
  51 
  52 // Returns false if the thread has not been scheduled since the last call to updateEvent
  53 // (i.e. the delta for both system and user time is 0 milliseconds)
  54 bool JfrThreadCPULoadEvent::update_event(EventThreadCPULoad& event, JavaThread* thread, jlong cur_wallclock_time, int processor_count) {
  55   JfrThreadLocal* const tl = thread->jfr_thread_local();
  56 
  57   jlong cur_cpu_time = os::thread_cpu_time(thread, true);
  58   jlong prev_cpu_time = tl->get_cpu_time();
  59 
  60   jlong prev_wallclock_time = tl->get_wallclock_time();
  61   tl->set_wallclock_time(cur_wallclock_time);
  62 
  63   // Threshold of 1 ms
  64   if (cur_cpu_time - prev_cpu_time < 1 * NANOSECS_PER_MILLISEC) {
  65     return false;
  66   }
  67 
  68   jlong cur_user_time = os::thread_cpu_time(thread, false);
  69   jlong prev_user_time = tl->get_user_time();
  70 
  71   jlong cur_system_time = cur_cpu_time - cur_user_time;
  72   jlong prev_system_time = prev_cpu_time - prev_user_time;
  73 
  74   // The user and total cpu usage clocks can have different resolutions, which can
  75   // make us see decreasing system time. Ensure time doesn't go backwards.
  76   if (prev_system_time > cur_system_time) {
  77     cur_cpu_time += 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       cur_cpu_time -= excess;
  93     } else {
  94       cur_cpu_time -= excess;
  95       excess -= user_time;
  96       user_time = 0;
  97       cur_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   {
 117     MutexLockerEx ml(Threads_lock);
 118     unsigned jt_count = 0;
 119     for (JavaThread *jt = Threads::first(); jt != NULL; jt = jt->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       jt_count++;
 132     }
 133     if (LogJFR && Verbose) tty->print_cr("Measured CPU usage for %d threads in %.3f milliseconds", jt_count,
 134       (double)(JfrTicks::now() - event_time).milliseconds());
 135   }
 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 }