src/share/vm/runtime/task.cpp

Print this page


   1 /*
   2  * Copyright (c) 1997, 2014, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *


  30 #include "runtime/timer.hpp"
  31 
  32 int PeriodicTask::_num_tasks = 0;
  33 PeriodicTask* PeriodicTask::_tasks[PeriodicTask::max_tasks];
  34 #ifndef PRODUCT
  35 elapsedTimer PeriodicTask::_timer;
  36 int PeriodicTask::_intervalHistogram[PeriodicTask::max_interval];
  37 int PeriodicTask::_ticks;
  38 
  39 void PeriodicTask::print_intervals() {
  40   if (ProfilerCheckIntervals) {
  41     for (int i = 0; i < PeriodicTask::max_interval; i++) {
  42       int n = _intervalHistogram[i];
  43       if (n > 0) tty->print_cr("%3d: %5d (%4.1f%%)", i, n, 100.0 * n / _ticks);
  44     }
  45   }
  46 }
  47 #endif
  48 
  49 void PeriodicTask::real_time_tick(int delay_time) {


  50 #ifndef PRODUCT
  51   if (ProfilerCheckIntervals) {
  52     _ticks++;
  53     _timer.stop();
  54     int ms = (int)(_timer.seconds() * 1000.0);
  55     _timer.reset();
  56     _timer.start();
  57     if (ms >= PeriodicTask::max_interval) ms = PeriodicTask::max_interval - 1;
  58     _intervalHistogram[ms]++;
  59   }
  60 #endif
  61 
  62   {



  63     MutexLockerEx ml(PeriodicTask_lock, Mutex::_no_safepoint_check_flag);
  64     int orig_num_tasks = _num_tasks;
  65 
  66     for(int index = 0; index < _num_tasks; index++) {
  67       _tasks[index]->execute_if_pending(delay_time);
  68       if (_num_tasks < orig_num_tasks) { // task dis-enrolled itself
  69         index--;  // re-do current slot as it has changed
  70         orig_num_tasks = _num_tasks;
  71       }
  72     }
  73   }
  74 }
  75 
  76 int PeriodicTask::time_to_wait() {
  77   MutexLockerEx ml(PeriodicTask_lock->owned_by_self() ?
  78                      NULL : PeriodicTask_lock, Mutex::_no_safepoint_check_flag);
  79 
  80   if (_num_tasks == 0) {
  81     return 0; // sleep until shutdown or a task is enrolled
  82   }
  83 
  84   int delay = _tasks[0]->time_to_next_interval();
  85   for (int index = 1; index < _num_tasks; index++) {
  86     delay = MIN2(delay, _tasks[index]->time_to_next_interval());
  87   }
  88   return delay;
  89 }
  90 
  91 
  92 PeriodicTask::PeriodicTask(size_t interval_time) :
  93   _counter(0), _interval((int) interval_time) {
  94   // Sanity check the interval time
  95   assert(_interval >= PeriodicTask::min_interval &&
  96          _interval %  PeriodicTask::interval_gran == 0,
  97               "improper PeriodicTask interval time");
  98 }
  99 
 100 PeriodicTask::~PeriodicTask() {


 101   disenroll();
 102 }
 103 
 104 /* enroll could be called from a JavaThread, so we have to check for
 105  * safepoint when taking the lock to avoid deadlocking */
 106 void PeriodicTask::enroll() {
 107   MutexLockerEx ml(PeriodicTask_lock->owned_by_self() ?
 108                      NULL : PeriodicTask_lock);





 109 
 110   if (_num_tasks == PeriodicTask::max_tasks) {
 111     fatal("Overflow in PeriodicTask table");
 112   }
 113   _tasks[_num_tasks++] = this;
 114 
 115   WatcherThread* thread = WatcherThread::watcher_thread();
 116   if (thread) {
 117     thread->unpark();
 118   } else {
 119     WatcherThread::start();
 120   }
 121 }
 122 
 123 /* disenroll could be called from a JavaThread, so we have to check for
 124  * safepoint when taking the lock to avoid deadlocking */
 125 void PeriodicTask::disenroll() {
 126   MutexLockerEx ml(PeriodicTask_lock->owned_by_self() ?
 127                      NULL : PeriodicTask_lock);





 128 
 129   int index;
 130   for(index = 0; index < _num_tasks && _tasks[index] != this; index++)
 131     ;
 132 
 133   if (index == _num_tasks) {
 134     return;
 135   }
 136 
 137   _num_tasks--;
 138 
 139   for (; index < _num_tasks; index++) {
 140     _tasks[index] = _tasks[index+1];
 141   }
 142 }
   1 /*
   2  * Copyright (c) 1997, 2015, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *


  30 #include "runtime/timer.hpp"
  31 
  32 int PeriodicTask::_num_tasks = 0;
  33 PeriodicTask* PeriodicTask::_tasks[PeriodicTask::max_tasks];
  34 #ifndef PRODUCT
  35 elapsedTimer PeriodicTask::_timer;
  36 int PeriodicTask::_intervalHistogram[PeriodicTask::max_interval];
  37 int PeriodicTask::_ticks;
  38 
  39 void PeriodicTask::print_intervals() {
  40   if (ProfilerCheckIntervals) {
  41     for (int i = 0; i < PeriodicTask::max_interval; i++) {
  42       int n = _intervalHistogram[i];
  43       if (n > 0) tty->print_cr("%3d: %5d (%4.1f%%)", i, n, 100.0 * n / _ticks);
  44     }
  45   }
  46 }
  47 #endif
  48 
  49 void PeriodicTask::real_time_tick(int delay_time) {
  50   assert(Thread::current()->is_Watcher_thread(), "must be WatcherThread");
  51 
  52 #ifndef PRODUCT
  53   if (ProfilerCheckIntervals) {
  54     _ticks++;
  55     _timer.stop();
  56     int ms = (int)(_timer.seconds() * 1000.0);
  57     _timer.reset();
  58     _timer.start();
  59     if (ms >= PeriodicTask::max_interval) ms = PeriodicTask::max_interval - 1;
  60     _intervalHistogram[ms]++;
  61   }
  62 #endif
  63 
  64   {
  65     // The WatcherThread does not honor the safepoint protocol for
  66     // PeriodicTask_lock in order to provide more consistent task
  67     // execution timing.
  68     MutexLockerEx ml(PeriodicTask_lock, Mutex::_no_safepoint_check_flag);
  69     int orig_num_tasks = _num_tasks;
  70 
  71     for(int index = 0; index < _num_tasks; index++) {
  72       _tasks[index]->execute_if_pending(delay_time);
  73       if (_num_tasks < orig_num_tasks) { // task dis-enrolled itself
  74         index--;  // re-do current slot as it has changed
  75         orig_num_tasks = _num_tasks;
  76       }
  77     }
  78   }
  79 }
  80 
  81 int PeriodicTask::time_to_wait() {
  82   assert(Thread::current()->is_Watcher_thread(), "must be WatcherThread");
  83   assert(PeriodicTask_lock->owned_by_self(), "PeriodicTask_lock required");
  84 
  85   if (_num_tasks == 0) {
  86     return 0; // sleep until shutdown or a task is enrolled
  87   }
  88 
  89   int delay = _tasks[0]->time_to_next_interval();
  90   for (int index = 1; index < _num_tasks; index++) {
  91     delay = MIN2(delay, _tasks[index]->time_to_next_interval());
  92   }
  93   return delay;
  94 }
  95 
  96 
  97 PeriodicTask::PeriodicTask(size_t interval_time) :
  98   _counter(0), _interval((int) interval_time) {
  99   // Sanity check the interval time
 100   assert(_interval >= PeriodicTask::min_interval &&
 101          _interval %  PeriodicTask::interval_gran == 0,
 102               "improper PeriodicTask interval time");
 103 }
 104 
 105 PeriodicTask::~PeriodicTask() {
 106   // This PeriodicTask may have already been disenrolled by a
 107   // subclass with a non-trivial destructor.
 108   disenroll();
 109 }
 110 
 111 // enroll the current PeriodicTask

 112 void PeriodicTask::enroll() {
 113   // Follow normal safepoint aware lock enter protocol if the caller does
 114   // not already own the PeriodicTask_lock. Otherwise, we don't try to
 115   // enter it again to avoid deadlocking with a safepoint that started
 116   // after the initial enter and before this enter.
 117   //
 118   MutexLockerEx ml(PeriodicTask_lock->owned_by_self() ? NULL
 119                                                       : PeriodicTask_lock);
 120 
 121   if (_num_tasks == PeriodicTask::max_tasks) {
 122     fatal("Overflow in PeriodicTask table");
 123   }
 124   _tasks[_num_tasks++] = this;
 125 
 126   WatcherThread* thread = WatcherThread::watcher_thread();
 127   if (thread != NULL) {
 128     thread->unpark();
 129   } else {
 130     WatcherThread::start();
 131   }
 132 }
 133 
 134 // disenroll the current PeriodicTask

 135 void PeriodicTask::disenroll() {
 136   // Follow normal safepoint aware lock enter protocol if the caller does
 137   // not already own the PeriodicTask_lock. Otherwise, we don't try to
 138   // enter it again to avoid deadlocking with a safepoint that started
 139   // after the initial enter and before this enter.
 140   //
 141   MutexLockerEx ml(PeriodicTask_lock->owned_by_self() ? NULL
 142                                                       : PeriodicTask_lock);
 143 
 144   int index;
 145   for(index = 0; index < _num_tasks && _tasks[index] != this; index++)
 146     ;
 147 
 148   if (index == _num_tasks) {
 149     return;
 150   }
 151 
 152   _num_tasks--;
 153 
 154   for (; index < _num_tasks; index++) {
 155     _tasks[index] = _tasks[index+1];
 156   }
 157 }