src/share/vm/runtime/task.cpp

Sat, 01 Dec 2007 00:00:00 +0000

author
duke
date
Sat, 01 Dec 2007 00:00:00 +0000
changeset 435
a61af66fc99e
child 496
5a76ab815e34
permissions
-rw-r--r--

Initial load

duke@435 1 /*
duke@435 2 * Copyright 1997-2007 Sun Microsystems, Inc. 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 *
duke@435 19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
duke@435 20 * CA 95054 USA or visit www.sun.com if you need additional information or
duke@435 21 * have any questions.
duke@435 22 *
duke@435 23 */
duke@435 24
duke@435 25 # include "incls/_precompiled.incl"
duke@435 26 # include "incls/_task.cpp.incl"
duke@435 27
duke@435 28 int PeriodicTask::_num_tasks = 0;
duke@435 29 PeriodicTask* PeriodicTask::_tasks[PeriodicTask::max_tasks];
duke@435 30 #ifndef PRODUCT
duke@435 31 elapsedTimer PeriodicTask::_timer;
duke@435 32 int PeriodicTask::_intervalHistogram[PeriodicTask::max_interval];
duke@435 33 int PeriodicTask::_ticks;
duke@435 34
duke@435 35 void PeriodicTask::print_intervals() {
duke@435 36 if (ProfilerCheckIntervals) {
duke@435 37 for (int i = 0; i < PeriodicTask::max_interval; i++) {
duke@435 38 int n = _intervalHistogram[i];
duke@435 39 if (n > 0) tty->print_cr("%3d: %5d (%4.1f%%)", i, n, 100.0 * n / _ticks);
duke@435 40 }
duke@435 41 }
duke@435 42 }
duke@435 43 #endif
duke@435 44
duke@435 45 void PeriodicTask::real_time_tick(size_t delay_time) {
duke@435 46 #ifndef PRODUCT
duke@435 47 if (ProfilerCheckIntervals) {
duke@435 48 _ticks++;
duke@435 49 _timer.stop();
duke@435 50 int ms = (int)(_timer.seconds() * 1000.0);
duke@435 51 _timer.reset();
duke@435 52 _timer.start();
duke@435 53 if (ms >= PeriodicTask::max_interval) ms = PeriodicTask::max_interval - 1;
duke@435 54 _intervalHistogram[ms]++;
duke@435 55 }
duke@435 56 #endif
duke@435 57 int orig_num_tasks = _num_tasks;
duke@435 58 for(int index = 0; index < _num_tasks; index++) {
duke@435 59 _tasks[index]->execute_if_pending(delay_time);
duke@435 60 if (_num_tasks < orig_num_tasks) { // task dis-enrolled itself
duke@435 61 index--; // re-do current slot as it has changed
duke@435 62 orig_num_tasks = _num_tasks;
duke@435 63 }
duke@435 64 }
duke@435 65 }
duke@435 66
duke@435 67
duke@435 68 PeriodicTask::PeriodicTask(size_t interval_time) :
duke@435 69 _counter(0), _interval(interval_time) {
duke@435 70 assert(is_init_completed(), "Periodic tasks should not start during VM initialization");
duke@435 71 // Sanity check the interval time
duke@435 72 assert(_interval >= PeriodicTask::min_interval &&
duke@435 73 _interval <= PeriodicTask::max_interval &&
duke@435 74 _interval % PeriodicTask::interval_gran == 0,
duke@435 75 "improper PeriodicTask interval time");
duke@435 76 }
duke@435 77
duke@435 78 PeriodicTask::~PeriodicTask() {
duke@435 79 if (is_enrolled())
duke@435 80 disenroll();
duke@435 81 }
duke@435 82
duke@435 83 bool PeriodicTask::is_enrolled() const {
duke@435 84 for(int index = 0; index < _num_tasks; index++)
duke@435 85 if (_tasks[index] == this) return true;
duke@435 86 return false;
duke@435 87 }
duke@435 88
duke@435 89 void PeriodicTask::enroll() {
duke@435 90 assert(WatcherThread::watcher_thread() == NULL, "dynamic enrollment of tasks not yet supported");
duke@435 91
duke@435 92 if (_num_tasks == PeriodicTask::max_tasks)
duke@435 93 fatal("Overflow in PeriodicTask table");
duke@435 94 _tasks[_num_tasks++] = this;
duke@435 95 }
duke@435 96
duke@435 97 void PeriodicTask::disenroll() {
duke@435 98 assert(WatcherThread::watcher_thread() == NULL ||
duke@435 99 Thread::current() == WatcherThread::watcher_thread(),
duke@435 100 "dynamic disenrollment currently only handled from WatcherThread from within task() method");
duke@435 101
duke@435 102 int index;
duke@435 103 for(index = 0; index < _num_tasks && _tasks[index] != this; index++);
duke@435 104 if (index == _num_tasks) return;
duke@435 105 _num_tasks--;
duke@435 106 for (; index < _num_tasks; index++) {
duke@435 107 _tasks[index] = _tasks[index+1];
duke@435 108 }
duke@435 109 }
duke@435 110
duke@435 111 TimeMillisUpdateTask* TimeMillisUpdateTask::_task = NULL;
duke@435 112
duke@435 113 void TimeMillisUpdateTask::task() {
duke@435 114 os::update_global_time();
duke@435 115 }
duke@435 116
duke@435 117 void TimeMillisUpdateTask::engage() {
duke@435 118 assert(_task == NULL, "init twice?");
duke@435 119 os::update_global_time(); // initial update
duke@435 120 os::enable_global_time();
duke@435 121 _task = new TimeMillisUpdateTask(CacheTimeMillisGranularity);
duke@435 122 _task->enroll();
duke@435 123 }
duke@435 124
duke@435 125 void TimeMillisUpdateTask::disengage() {
duke@435 126 assert(_task != NULL, "uninit twice?");
duke@435 127 os::disable_global_time();
duke@435 128 _task->disenroll();
duke@435 129 delete _task;
duke@435 130 _task = NULL;
duke@435 131 }

mercurial