duke@435: /* mikael@4153: * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved. duke@435: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. duke@435: * duke@435: * This code is free software; you can redistribute it and/or modify it duke@435: * under the terms of the GNU General Public License version 2 only, as duke@435: * published by the Free Software Foundation. duke@435: * duke@435: * This code is distributed in the hope that it will be useful, but WITHOUT duke@435: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or duke@435: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License duke@435: * version 2 for more details (a copy is included in the LICENSE file that duke@435: * accompanied this code). duke@435: * duke@435: * You should have received a copy of the GNU General Public License version duke@435: * 2 along with this work; if not, write to the Free Software Foundation, duke@435: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. duke@435: * trims@1907: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA trims@1907: * or visit www.oracle.com if you need additional information or have any trims@1907: * questions. duke@435: * duke@435: */ duke@435: stefank@2314: #ifndef SHARE_VM_RUNTIME_TASK_HPP stefank@2314: #define SHARE_VM_RUNTIME_TASK_HPP stefank@2314: stefank@2314: #include "utilities/top.hpp" stefank@2314: duke@435: // A PeriodicTask has the sole purpose of executing its task duke@435: // function with regular intervals. duke@435: // Usage: duke@435: // PeriodicTask pf(10); duke@435: // pf.enroll(); duke@435: // ... duke@435: // pf.disenroll(); duke@435: zgu@3900: class PeriodicTask: public CHeapObj { duke@435: public: duke@435: // Useful constants. duke@435: // The interval constants are used to ensure the declared interval duke@435: // is appropriate; it must be between min_interval and max_interval, duke@435: // and have a granularity of interval_gran (all in millis). duke@435: enum { max_tasks = 10, // Max number of periodic tasks in system duke@435: interval_gran = 10, duke@435: min_interval = 10, duke@435: max_interval = 10000 }; duke@435: duke@435: static int num_tasks() { return _num_tasks; } duke@435: duke@435: private: rbackman@4250: int _counter; rbackman@4250: const int _interval; duke@435: duke@435: static int _num_tasks; duke@435: static PeriodicTask* _tasks[PeriodicTask::max_tasks]; rbackman@4250: static void real_time_tick(int delay_time); duke@435: duke@435: #ifndef PRODUCT duke@435: static elapsedTimer _timer; // measures time between ticks duke@435: static int _ticks; // total number of ticks duke@435: static int _intervalHistogram[max_interval]; // to check spacing of timer interrupts duke@435: public: duke@435: static void print_intervals(); duke@435: #endif duke@435: // Only the WatcherThread can cause us to execute PeriodicTasks duke@435: friend class WatcherThread; duke@435: public: duke@435: PeriodicTask(size_t interval_time); // interval is in milliseconds of elapsed time duke@435: ~PeriodicTask(); duke@435: duke@435: // Make the task active rbackman@4250: // For dynamic enrollment at the time T, the task will execute somewhere rbackman@4250: // between T and T + interval_time. duke@435: void enroll(); duke@435: duke@435: // Make the task deactive duke@435: void disenroll(); duke@435: rbackman@4250: void execute_if_pending(int delay_time) { rbackman@4250: // make sure we don't overflow rbackman@4250: jlong tmp = (jlong) _counter + (jlong) delay_time; rbackman@4250: rbackman@4250: if (tmp >= (jlong) _interval) { duke@435: _counter = 0; duke@435: task(); rbackman@4250: } else { rbackman@4250: _counter += delay_time; duke@435: } duke@435: } duke@435: duke@435: // Returns how long (time in milliseconds) before the next time we should duke@435: // execute this task. rbackman@4250: int time_to_next_interval() const { duke@435: assert(_interval > _counter, "task counter greater than interval?"); duke@435: return _interval - _counter; duke@435: } duke@435: duke@435: // Calculate when the next periodic task will fire. duke@435: // Called by the WatcherThread's run method. rbackman@4250: static int time_to_wait(); duke@435: duke@435: // The task to perform at each period duke@435: virtual void task() = 0; duke@435: }; stefank@2314: stefank@2314: #endif // SHARE_VM_RUNTIME_TASK_HPP