duke@435: /* sla@5237: * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved. duke@435: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. duke@435: * duke@435: * This code is free software; you can redistribute it and/or modify it duke@435: * under the terms of the GNU General Public License version 2 only, as duke@435: * published by the Free Software Foundation. duke@435: * duke@435: * This code is distributed in the hope that it will be useful, but WITHOUT duke@435: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or duke@435: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License duke@435: * version 2 for more details (a copy is included in the LICENSE file that duke@435: * accompanied this code). duke@435: * duke@435: * You should have received a copy of the GNU General Public License version duke@435: * 2 along with this work; if not, write to the Free Software Foundation, duke@435: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. duke@435: * trims@1907: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA trims@1907: * or visit www.oracle.com if you need additional information or have any trims@1907: * questions. duke@435: * duke@435: */ duke@435: stefank@2314: #include "precompiled.hpp" stefank@2314: #include "memory/allocation.hpp" stefank@2314: #include "runtime/init.hpp" stefank@2314: #include "runtime/task.hpp" stefank@4299: #include "runtime/thread.inline.hpp" stefank@2314: #include "runtime/timer.hpp" stefank@2314: #ifdef TARGET_OS_FAMILY_linux stefank@2314: # include "os_linux.inline.hpp" stefank@2314: #endif stefank@2314: #ifdef TARGET_OS_FAMILY_solaris stefank@2314: # include "os_solaris.inline.hpp" stefank@2314: #endif stefank@2314: #ifdef TARGET_OS_FAMILY_windows stefank@2314: # include "os_windows.inline.hpp" stefank@2314: #endif never@3156: #ifdef TARGET_OS_FAMILY_bsd never@3156: # include "os_bsd.inline.hpp" never@3156: #endif duke@435: duke@435: int PeriodicTask::_num_tasks = 0; duke@435: PeriodicTask* PeriodicTask::_tasks[PeriodicTask::max_tasks]; duke@435: #ifndef PRODUCT duke@435: elapsedTimer PeriodicTask::_timer; duke@435: int PeriodicTask::_intervalHistogram[PeriodicTask::max_interval]; duke@435: int PeriodicTask::_ticks; duke@435: duke@435: void PeriodicTask::print_intervals() { duke@435: if (ProfilerCheckIntervals) { duke@435: for (int i = 0; i < PeriodicTask::max_interval; i++) { duke@435: int n = _intervalHistogram[i]; duke@435: if (n > 0) tty->print_cr("%3d: %5d (%4.1f%%)", i, n, 100.0 * n / _ticks); duke@435: } duke@435: } duke@435: } duke@435: #endif duke@435: rbackman@4250: void PeriodicTask::real_time_tick(int delay_time) { duke@435: #ifndef PRODUCT duke@435: if (ProfilerCheckIntervals) { duke@435: _ticks++; duke@435: _timer.stop(); duke@435: int ms = (int)(_timer.seconds() * 1000.0); duke@435: _timer.reset(); duke@435: _timer.start(); duke@435: if (ms >= PeriodicTask::max_interval) ms = PeriodicTask::max_interval - 1; duke@435: _intervalHistogram[ms]++; duke@435: } duke@435: #endif rbackman@4250: rbackman@4250: { rbackman@4250: MutexLockerEx ml(PeriodicTask_lock, Mutex::_no_safepoint_check_flag); rbackman@4250: int orig_num_tasks = _num_tasks; rbackman@4250: rbackman@4250: for(int index = 0; index < _num_tasks; index++) { rbackman@4250: _tasks[index]->execute_if_pending(delay_time); rbackman@4250: if (_num_tasks < orig_num_tasks) { // task dis-enrolled itself rbackman@4250: index--; // re-do current slot as it has changed rbackman@4250: orig_num_tasks = _num_tasks; rbackman@4250: } duke@435: } duke@435: } duke@435: } duke@435: rbackman@4250: int PeriodicTask::time_to_wait() { rbackman@4250: MutexLockerEx ml(PeriodicTask_lock->owned_by_self() ? rbackman@4250: NULL : PeriodicTask_lock, Mutex::_no_safepoint_check_flag); rbackman@4250: rbackman@4250: if (_num_tasks == 0) { rbackman@4250: return 0; // sleep until shutdown or a task is enrolled rbackman@4250: } rbackman@4250: rbackman@4250: int delay = _tasks[0]->time_to_next_interval(); rbackman@4250: for (int index = 1; index < _num_tasks; index++) { rbackman@4250: delay = MIN2(delay, _tasks[index]->time_to_next_interval()); rbackman@4250: } rbackman@4250: return delay; rbackman@4250: } rbackman@4250: duke@435: duke@435: PeriodicTask::PeriodicTask(size_t interval_time) : rbackman@4250: _counter(0), _interval((int) interval_time) { duke@435: // Sanity check the interval time duke@435: assert(_interval >= PeriodicTask::min_interval && duke@435: _interval % PeriodicTask::interval_gran == 0, duke@435: "improper PeriodicTask interval time"); duke@435: } duke@435: duke@435: PeriodicTask::~PeriodicTask() { rbackman@4250: disenroll(); duke@435: } duke@435: sla@5237: /* enroll could be called from a JavaThread, so we have to check for sla@5237: * safepoint when taking the lock to avoid deadlocking */ duke@435: void PeriodicTask::enroll() { rbackman@4250: MutexLockerEx ml(PeriodicTask_lock->owned_by_self() ? sla@5237: NULL : PeriodicTask_lock); duke@435: rbackman@4250: if (_num_tasks == PeriodicTask::max_tasks) { duke@435: fatal("Overflow in PeriodicTask table"); rbackman@4250: } duke@435: _tasks[_num_tasks++] = this; rbackman@4250: rbackman@4250: WatcherThread* thread = WatcherThread::watcher_thread(); rbackman@4250: if (thread) { rbackman@4250: thread->unpark(); rbackman@4250: } else { rbackman@4250: WatcherThread::start(); rbackman@4250: } duke@435: } duke@435: sla@5237: /* disenroll could be called from a JavaThread, so we have to check for sla@5237: * safepoint when taking the lock to avoid deadlocking */ duke@435: void PeriodicTask::disenroll() { rbackman@4250: MutexLockerEx ml(PeriodicTask_lock->owned_by_self() ? sla@5237: NULL : PeriodicTask_lock); duke@435: duke@435: int index; rbackman@4250: for(index = 0; index < _num_tasks && _tasks[index] != this; index++) rbackman@4250: ; rbackman@4250: rbackman@4250: if (index == _num_tasks) { rbackman@4250: return; rbackman@4250: } rbackman@4250: duke@435: _num_tasks--; rbackman@4250: duke@435: for (; index < _num_tasks; index++) { duke@435: _tasks[index] = _tasks[index+1]; duke@435: } duke@435: }