src/share/vm/runtime/task.cpp

Mon, 24 Oct 2011 07:53:17 -0700

author
twisti
date
Mon, 24 Oct 2011 07:53:17 -0700
changeset 3238
b20d64f83668
parent 3156
f08d439fab8c
child 4250
c284cf4781f0
permissions
-rw-r--r--

7090904: JSR 292: JRuby junit test crashes in PSScavengeRootsClosure::do_oop
Reviewed-by: kvn, never, jrose

duke@435 1 /*
stefank@2314 2 * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved.
duke@435 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@435 4 *
duke@435 5 * This code is free software; you can redistribute it and/or modify it
duke@435 6 * under the terms of the GNU General Public License version 2 only, as
duke@435 7 * published by the Free Software Foundation.
duke@435 8 *
duke@435 9 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@435 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@435 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@435 12 * version 2 for more details (a copy is included in the LICENSE file that
duke@435 13 * accompanied this code).
duke@435 14 *
duke@435 15 * You should have received a copy of the GNU General Public License version
duke@435 16 * 2 along with this work; if not, write to the Free Software Foundation,
duke@435 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@435 18 *
trims@1907 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
trims@1907 20 * or visit www.oracle.com if you need additional information or have any
trims@1907 21 * questions.
duke@435 22 *
duke@435 23 */
duke@435 24
stefank@2314 25 #include "precompiled.hpp"
stefank@2314 26 #include "memory/allocation.hpp"
stefank@2314 27 #include "runtime/init.hpp"
stefank@2314 28 #include "runtime/task.hpp"
stefank@2314 29 #include "runtime/timer.hpp"
stefank@2314 30 #ifdef TARGET_OS_FAMILY_linux
stefank@2314 31 # include "os_linux.inline.hpp"
stefank@2314 32 # include "thread_linux.inline.hpp"
stefank@2314 33 #endif
stefank@2314 34 #ifdef TARGET_OS_FAMILY_solaris
stefank@2314 35 # include "os_solaris.inline.hpp"
stefank@2314 36 # include "thread_solaris.inline.hpp"
stefank@2314 37 #endif
stefank@2314 38 #ifdef TARGET_OS_FAMILY_windows
stefank@2314 39 # include "os_windows.inline.hpp"
stefank@2314 40 # include "thread_windows.inline.hpp"
stefank@2314 41 #endif
never@3156 42 #ifdef TARGET_OS_FAMILY_bsd
never@3156 43 # include "os_bsd.inline.hpp"
never@3156 44 # include "thread_bsd.inline.hpp"
never@3156 45 #endif
duke@435 46
duke@435 47 int PeriodicTask::_num_tasks = 0;
duke@435 48 PeriodicTask* PeriodicTask::_tasks[PeriodicTask::max_tasks];
duke@435 49 #ifndef PRODUCT
duke@435 50 elapsedTimer PeriodicTask::_timer;
duke@435 51 int PeriodicTask::_intervalHistogram[PeriodicTask::max_interval];
duke@435 52 int PeriodicTask::_ticks;
duke@435 53
duke@435 54 void PeriodicTask::print_intervals() {
duke@435 55 if (ProfilerCheckIntervals) {
duke@435 56 for (int i = 0; i < PeriodicTask::max_interval; i++) {
duke@435 57 int n = _intervalHistogram[i];
duke@435 58 if (n > 0) tty->print_cr("%3d: %5d (%4.1f%%)", i, n, 100.0 * n / _ticks);
duke@435 59 }
duke@435 60 }
duke@435 61 }
duke@435 62 #endif
duke@435 63
duke@435 64 void PeriodicTask::real_time_tick(size_t delay_time) {
duke@435 65 #ifndef PRODUCT
duke@435 66 if (ProfilerCheckIntervals) {
duke@435 67 _ticks++;
duke@435 68 _timer.stop();
duke@435 69 int ms = (int)(_timer.seconds() * 1000.0);
duke@435 70 _timer.reset();
duke@435 71 _timer.start();
duke@435 72 if (ms >= PeriodicTask::max_interval) ms = PeriodicTask::max_interval - 1;
duke@435 73 _intervalHistogram[ms]++;
duke@435 74 }
duke@435 75 #endif
duke@435 76 int orig_num_tasks = _num_tasks;
duke@435 77 for(int index = 0; index < _num_tasks; index++) {
duke@435 78 _tasks[index]->execute_if_pending(delay_time);
duke@435 79 if (_num_tasks < orig_num_tasks) { // task dis-enrolled itself
duke@435 80 index--; // re-do current slot as it has changed
duke@435 81 orig_num_tasks = _num_tasks;
duke@435 82 }
duke@435 83 }
duke@435 84 }
duke@435 85
duke@435 86
duke@435 87 PeriodicTask::PeriodicTask(size_t interval_time) :
duke@435 88 _counter(0), _interval(interval_time) {
duke@435 89 // Sanity check the interval time
duke@435 90 assert(_interval >= PeriodicTask::min_interval &&
duke@435 91 _interval <= PeriodicTask::max_interval &&
duke@435 92 _interval % PeriodicTask::interval_gran == 0,
duke@435 93 "improper PeriodicTask interval time");
duke@435 94 }
duke@435 95
duke@435 96 PeriodicTask::~PeriodicTask() {
duke@435 97 if (is_enrolled())
duke@435 98 disenroll();
duke@435 99 }
duke@435 100
duke@435 101 bool PeriodicTask::is_enrolled() const {
duke@435 102 for(int index = 0; index < _num_tasks; index++)
duke@435 103 if (_tasks[index] == this) return true;
duke@435 104 return false;
duke@435 105 }
duke@435 106
duke@435 107 void PeriodicTask::enroll() {
duke@435 108 assert(WatcherThread::watcher_thread() == NULL, "dynamic enrollment of tasks not yet supported");
duke@435 109
duke@435 110 if (_num_tasks == PeriodicTask::max_tasks)
duke@435 111 fatal("Overflow in PeriodicTask table");
duke@435 112 _tasks[_num_tasks++] = this;
duke@435 113 }
duke@435 114
duke@435 115 void PeriodicTask::disenroll() {
duke@435 116 assert(WatcherThread::watcher_thread() == NULL ||
duke@435 117 Thread::current() == WatcherThread::watcher_thread(),
duke@435 118 "dynamic disenrollment currently only handled from WatcherThread from within task() method");
duke@435 119
duke@435 120 int index;
duke@435 121 for(index = 0; index < _num_tasks && _tasks[index] != this; index++);
duke@435 122 if (index == _num_tasks) return;
duke@435 123 _num_tasks--;
duke@435 124 for (; index < _num_tasks; index++) {
duke@435 125 _tasks[index] = _tasks[index+1];
duke@435 126 }
duke@435 127 }

mercurial