1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/vm/runtime/task.hpp Wed Apr 27 01:25:04 2016 +0800 1.3 @@ -0,0 +1,107 @@ 1.4 +/* 1.5 + * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. 1.11 + * 1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.15 + * version 2 for more details (a copy is included in the LICENSE file that 1.16 + * accompanied this code). 1.17 + * 1.18 + * You should have received a copy of the GNU General Public License version 1.19 + * 2 along with this work; if not, write to the Free Software Foundation, 1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.21 + * 1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.23 + * or visit www.oracle.com if you need additional information or have any 1.24 + * questions. 1.25 + * 1.26 + */ 1.27 + 1.28 +#ifndef SHARE_VM_RUNTIME_TASK_HPP 1.29 +#define SHARE_VM_RUNTIME_TASK_HPP 1.30 + 1.31 +#include "utilities/top.hpp" 1.32 + 1.33 +// A PeriodicTask has the sole purpose of executing its task 1.34 +// function with regular intervals. 1.35 +// Usage: 1.36 +// PeriodicTask pf(10); 1.37 +// pf.enroll(); 1.38 +// ... 1.39 +// pf.disenroll(); 1.40 + 1.41 +class PeriodicTask: public CHeapObj<mtInternal> { 1.42 + public: 1.43 + // Useful constants. 1.44 + // The interval constants are used to ensure the declared interval 1.45 + // is appropriate; it must be between min_interval and max_interval, 1.46 + // and have a granularity of interval_gran (all in millis). 1.47 + enum { max_tasks = 10, // Max number of periodic tasks in system 1.48 + interval_gran = 10, 1.49 + min_interval = 10, 1.50 + max_interval = 10000 }; 1.51 + 1.52 + static int num_tasks() { return _num_tasks; } 1.53 + 1.54 + private: 1.55 + int _counter; 1.56 + const int _interval; 1.57 + 1.58 + static int _num_tasks; 1.59 + static PeriodicTask* _tasks[PeriodicTask::max_tasks]; 1.60 + static void real_time_tick(int delay_time); 1.61 + 1.62 +#ifndef PRODUCT 1.63 + static elapsedTimer _timer; // measures time between ticks 1.64 + static int _ticks; // total number of ticks 1.65 + static int _intervalHistogram[max_interval]; // to check spacing of timer interrupts 1.66 + public: 1.67 + static void print_intervals(); 1.68 +#endif 1.69 + // Only the WatcherThread can cause us to execute PeriodicTasks 1.70 + friend class WatcherThread; 1.71 + public: 1.72 + PeriodicTask(size_t interval_time); // interval is in milliseconds of elapsed time 1.73 + ~PeriodicTask(); 1.74 + 1.75 + // Make the task active 1.76 + // For dynamic enrollment at the time T, the task will execute somewhere 1.77 + // between T and T + interval_time. 1.78 + void enroll(); 1.79 + 1.80 + // Make the task deactive 1.81 + void disenroll(); 1.82 + 1.83 + void execute_if_pending(int delay_time) { 1.84 + // make sure we don't overflow 1.85 + jlong tmp = (jlong) _counter + (jlong) delay_time; 1.86 + 1.87 + if (tmp >= (jlong) _interval) { 1.88 + _counter = 0; 1.89 + task(); 1.90 + } else { 1.91 + _counter += delay_time; 1.92 + } 1.93 + } 1.94 + 1.95 + // Returns how long (time in milliseconds) before the next time we should 1.96 + // execute this task. 1.97 + int time_to_next_interval() const { 1.98 + assert(_interval > _counter, "task counter greater than interval?"); 1.99 + return _interval - _counter; 1.100 + } 1.101 + 1.102 + // Calculate when the next periodic task will fire. 1.103 + // Called by the WatcherThread's run method. 1.104 + static int time_to_wait(); 1.105 + 1.106 + // The task to perform at each period 1.107 + virtual void task() = 0; 1.108 +}; 1.109 + 1.110 +#endif // SHARE_VM_RUNTIME_TASK_HPP