1 /* 2 * Copyright (c) 1997, 2015, 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 "memory/allocation.hpp" 27 #include "runtime/init.hpp" 28 #include "runtime/task.hpp" 29 #include "runtime/thread.inline.hpp" 30 #include "runtime/timer.hpp" 31 32 int PeriodicTask::_num_tasks = 0; 33 PeriodicTask* PeriodicTask::_tasks[PeriodicTask::max_tasks]; 34 #ifndef PRODUCT 35 elapsedTimer PeriodicTask::_timer; 36 int PeriodicTask::_intervalHistogram[PeriodicTask::max_interval]; 37 int PeriodicTask::_ticks; 38 39 void PeriodicTask::print_intervals() { 40 if (ProfilerCheckIntervals) { 41 for (int i = 0; i < PeriodicTask::max_interval; i++) { 42 int n = _intervalHistogram[i]; 43 if (n > 0) tty->print_cr("%3d: %5d (%4.1f%%)", i, n, 100.0 * n / _ticks); 44 } 45 } 46 } 47 #endif 48 49 void PeriodicTask::real_time_tick(int delay_time) { 50 assert(Thread::current()->is_Watcher_thread(), "must be WatcherThread"); 51 52 #ifndef PRODUCT 53 if (ProfilerCheckIntervals) { 54 _ticks++; 55 _timer.stop(); 56 int ms = (int)(_timer.seconds() * 1000.0); 57 _timer.reset(); 58 _timer.start(); 59 if (ms >= PeriodicTask::max_interval) ms = PeriodicTask::max_interval - 1; 60 _intervalHistogram[ms]++; 61 } 62 #endif 63 64 { 65 // The WatcherThread is not a JavaThread so we do not honor the 66 // safepoint protocol for the PeriodicTask_lock. 67 MutexLockerEx ml(PeriodicTask_lock, Mutex::_no_safepoint_check_flag); 68 int orig_num_tasks = _num_tasks; 69 70 for(int index = 0; index < _num_tasks; index++) { 71 _tasks[index]->execute_if_pending(delay_time); 72 if (_num_tasks < orig_num_tasks) { // task dis-enrolled itself 73 index--; // re-do current slot as it has changed 74 orig_num_tasks = _num_tasks; 75 } 76 } 77 } 78 } 79 80 int PeriodicTask::time_to_wait() { 81 assert(PeriodicTask_lock->owned_by_self(), "PeriodicTask_lock required"); 82 83 if (_num_tasks == 0) { 84 return 0; // sleep until shutdown or a task is enrolled 85 } 86 87 int delay = _tasks[0]->time_to_next_interval(); 88 for (int index = 1; index < _num_tasks; index++) { 89 delay = MIN2(delay, _tasks[index]->time_to_next_interval()); 90 } 91 return delay; 92 } 93 94 95 PeriodicTask::PeriodicTask(size_t interval_time) : 96 _counter(0), _interval((int) interval_time) { 97 // Sanity check the interval time 98 assert(_interval >= PeriodicTask::min_interval && 99 _interval % PeriodicTask::interval_gran == 0, 100 "improper PeriodicTask interval time"); 101 } 102 103 PeriodicTask::~PeriodicTask() { 104 // This PeriodicTask may have already been disenrolled by a call 105 // to disenroll() before the PeriodicTask was deleted. 106 disenroll(); 107 } 108 109 // enroll the current PeriodicTask 110 void PeriodicTask::enroll() { 111 // Follow normal safepoint aware lock enter protocol if the caller does 112 // not already own the PeriodicTask_lock. Otherwise, we don't try to 113 // enter it again because VM internal Mutexes do not support recursion. 114 // 115 MutexLockerEx ml(PeriodicTask_lock->owned_by_self() ? NULL 116 : PeriodicTask_lock); 117 118 if (_num_tasks == PeriodicTask::max_tasks) { 119 fatal("Overflow in PeriodicTask table"); 120 } 121 _tasks[_num_tasks++] = this; 122 123 WatcherThread* thread = WatcherThread::watcher_thread(); 124 if (thread != NULL) { 125 thread->unpark(); 126 } else { 127 WatcherThread::start(); 128 } 129 } 130 131 // disenroll the current PeriodicTask 132 void PeriodicTask::disenroll() { 133 // Follow normal safepoint aware lock enter protocol if the caller does 134 // not already own the PeriodicTask_lock. Otherwise, we don't try to 135 // enter it again because VM internal Mutexes do not support recursion. 136 // 137 MutexLockerEx ml(PeriodicTask_lock->owned_by_self() ? NULL 138 : PeriodicTask_lock); 139 140 int index; 141 for(index = 0; index < _num_tasks && _tasks[index] != this; index++) 142 ; 143 144 if (index == _num_tasks) { 145 return; 146 } 147 148 _num_tasks--; 149 150 for (; index < _num_tasks; index++) { 151 _tasks[index] = _tasks[index+1]; 152 } 153 }