hotspot/src/share/vm/runtime/task.cpp
changeset 1 489c9b5090e2
child 234 4da9c1bbc810
equal deleted inserted replaced
0:fd16c54261b3 1:489c9b5090e2
       
     1 /*
       
     2  * Copyright 1997-2007 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 
       
    28 int PeriodicTask::_num_tasks = 0;
       
    29 PeriodicTask* PeriodicTask::_tasks[PeriodicTask::max_tasks];
       
    30 #ifndef PRODUCT
       
    31 elapsedTimer PeriodicTask::_timer;
       
    32 int PeriodicTask::_intervalHistogram[PeriodicTask::max_interval];
       
    33 int PeriodicTask::_ticks;
       
    34 
       
    35 void 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 
       
    45 void 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 
       
    68 PeriodicTask::PeriodicTask(size_t interval_time) :
       
    69   _counter(0), _interval(interval_time) {
       
    70   assert(is_init_completed(), "Periodic tasks should not start during VM initialization");
       
    71   // Sanity check the interval time
       
    72   assert(_interval >= PeriodicTask::min_interval &&
       
    73          _interval <= PeriodicTask::max_interval &&
       
    74          _interval %  PeriodicTask::interval_gran == 0,
       
    75               "improper PeriodicTask interval time");
       
    76 }
       
    77 
       
    78 PeriodicTask::~PeriodicTask() {
       
    79   if (is_enrolled())
       
    80     disenroll();
       
    81 }
       
    82 
       
    83 bool PeriodicTask::is_enrolled() const {
       
    84   for(int index = 0; index < _num_tasks; index++)
       
    85     if (_tasks[index] == this) return true;
       
    86   return false;
       
    87 }
       
    88 
       
    89 void PeriodicTask::enroll() {
       
    90   assert(WatcherThread::watcher_thread() == NULL, "dynamic enrollment of tasks not yet supported");
       
    91 
       
    92   if (_num_tasks == PeriodicTask::max_tasks)
       
    93     fatal("Overflow in PeriodicTask table");
       
    94   _tasks[_num_tasks++] = this;
       
    95 }
       
    96 
       
    97 void PeriodicTask::disenroll() {
       
    98   assert(WatcherThread::watcher_thread() == NULL ||
       
    99          Thread::current() == WatcherThread::watcher_thread(),
       
   100          "dynamic disenrollment currently only handled from WatcherThread from within task() method");
       
   101 
       
   102   int index;
       
   103   for(index = 0; index < _num_tasks && _tasks[index] != this; index++);
       
   104   if (index == _num_tasks) return;
       
   105   _num_tasks--;
       
   106   for (; index < _num_tasks; index++) {
       
   107     _tasks[index] = _tasks[index+1];
       
   108   }
       
   109 }
       
   110 
       
   111 TimeMillisUpdateTask* TimeMillisUpdateTask::_task = NULL;
       
   112 
       
   113 void TimeMillisUpdateTask::task() {
       
   114   os::update_global_time();
       
   115 }
       
   116 
       
   117 void TimeMillisUpdateTask::engage() {
       
   118   assert(_task == NULL, "init twice?");
       
   119   os::update_global_time(); // initial update
       
   120   os::enable_global_time();
       
   121   _task = new TimeMillisUpdateTask(CacheTimeMillisGranularity);
       
   122   _task->enroll();
       
   123 }
       
   124 
       
   125 void TimeMillisUpdateTask::disengage() {
       
   126   assert(_task != NULL, "uninit twice?");
       
   127   os::disable_global_time();
       
   128   _task->disenroll();
       
   129   delete _task;
       
   130   _task = NULL;
       
   131 }