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