task.cpp revision 356:1ee8caae33af
1/*
2 * Copyright 1997-2008 Sun Microsystems, Inc.  All Rights Reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any questions.
22 *
23 */
24
25# include "incls/_precompiled.incl"
26# include "incls/_task.cpp.incl"
27
28int PeriodicTask::_num_tasks = 0;
29PeriodicTask* PeriodicTask::_tasks[PeriodicTask::max_tasks];
30#ifndef PRODUCT
31elapsedTimer PeriodicTask::_timer;
32int PeriodicTask::_intervalHistogram[PeriodicTask::max_interval];
33int PeriodicTask::_ticks;
34
35void PeriodicTask::print_intervals() {
36  if (ProfilerCheckIntervals) {
37    for (int i = 0; i < PeriodicTask::max_interval; i++) {
38      int n = _intervalHistogram[i];
39      if (n > 0) tty->print_cr("%3d: %5d (%4.1f%%)", i, n, 100.0 * n / _ticks);
40    }
41  }
42}
43#endif
44
45void PeriodicTask::real_time_tick(size_t delay_time) {
46#ifndef PRODUCT
47  if (ProfilerCheckIntervals) {
48    _ticks++;
49    _timer.stop();
50    int ms = (int)(_timer.seconds() * 1000.0);
51    _timer.reset();
52    _timer.start();
53    if (ms >= PeriodicTask::max_interval) ms = PeriodicTask::max_interval - 1;
54    _intervalHistogram[ms]++;
55  }
56#endif
57  int orig_num_tasks = _num_tasks;
58  for(int index = 0; index < _num_tasks; index++) {
59    _tasks[index]->execute_if_pending(delay_time);
60    if (_num_tasks < orig_num_tasks) { // task dis-enrolled itself
61      index--;  // re-do current slot as it has changed
62      orig_num_tasks = _num_tasks;
63    }
64  }
65}
66
67
68PeriodicTask::PeriodicTask(size_t interval_time) :
69  _counter(0), _interval(interval_time) {
70  // Sanity check the interval time
71  assert(_interval >= PeriodicTask::min_interval &&
72         _interval <= PeriodicTask::max_interval &&
73         _interval %  PeriodicTask::interval_gran == 0,
74              "improper PeriodicTask interval time");
75}
76
77PeriodicTask::~PeriodicTask() {
78  if (is_enrolled())
79    disenroll();
80}
81
82bool PeriodicTask::is_enrolled() const {
83  for(int index = 0; index < _num_tasks; index++)
84    if (_tasks[index] == this) return true;
85  return false;
86}
87
88void PeriodicTask::enroll() {
89  assert(WatcherThread::watcher_thread() == NULL, "dynamic enrollment of tasks not yet supported");
90
91  if (_num_tasks == PeriodicTask::max_tasks)
92    fatal("Overflow in PeriodicTask table");
93  _tasks[_num_tasks++] = this;
94}
95
96void PeriodicTask::disenroll() {
97  assert(WatcherThread::watcher_thread() == NULL ||
98         Thread::current() == WatcherThread::watcher_thread(),
99         "dynamic disenrollment currently only handled from WatcherThread from within task() method");
100
101  int index;
102  for(index = 0; index < _num_tasks && _tasks[index] != this; index++);
103  if (index == _num_tasks) return;
104  _num_tasks--;
105  for (; index < _num_tasks; index++) {
106    _tasks[index] = _tasks[index+1];
107  }
108}
109