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