< prev index next >

src/hotspot/share/jfr/periodic/jfrThreadCPULoadEvent.cpp

Print this page

  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  *

 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 }

  1 /*
  2  * Copyright (c) 2017, 2020, 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  *

 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   traceid periodic_thread_id = JfrThreadLocal::thread_id(periodic_thread);

111   const int processor_count = JfrThreadCPULoadEvent::get_processor_count();
112   JfrTicks event_time = JfrTicks::now();
113   jlong cur_wallclock_time = JfrThreadCPULoadEvent::get_wallclock_time();
114 
115   JfrJavaThreadIterator iter;
116   int number_of_threads = 0;
117   while (iter.has_next()) {
118     JavaThread* const jt = iter.next();
119     assert(jt != NULL, "invariant");
120     ++number_of_threads;
121     EventThreadCPULoad event(UNTIMED);
122     if (JfrThreadCPULoadEvent::update_event(event, jt, cur_wallclock_time, processor_count)) {
123       event.set_starttime(event_time);
124       if (jt != periodic_thread) {
125         // Commit reads the thread id from this thread's trace data, so put it there temporarily
126         JfrThreadLocal::impersonate(periodic_thread, JFR_THREAD_ID(jt));
127       } else {
128         JfrThreadLocal::impersonate(periodic_thread, periodic_thread_id);
129       }
130       event.commit();
131     }
132   }
133   log_trace(jfr)("Measured CPU usage for %d threads in %.3f milliseconds", number_of_threads,
134     (double)(JfrTicks::now() - event_time).milliseconds());
135   JfrThreadLocal::stop_impersonating(periodic_thread);

136 }
137 
138 void JfrThreadCPULoadEvent::send_event_for_thread(JavaThread* jt) {
139   EventThreadCPULoad event;
140   if (event.should_commit()) {
141     if (update_event(event, jt, get_wallclock_time(), get_processor_count())) {
142       event.commit();
143     }
144   }
145 }
< prev index next >