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