src/share/vm/runtime/task.cpp

Wed, 03 Jul 2019 20:42:37 +0800

author
aoqi
date
Wed, 03 Jul 2019 20:42:37 +0800
changeset 9637
eef07cd490d4
parent 6876
710a3c8b516e
permissions
-rw-r--r--

Merge

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation.
aoqi@0 8 *
aoqi@0 9 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 12 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 13 * accompanied this code).
aoqi@0 14 *
aoqi@0 15 * You should have received a copy of the GNU General Public License version
aoqi@0 16 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 18 *
aoqi@0 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 20 * or visit www.oracle.com if you need additional information or have any
aoqi@0 21 * questions.
aoqi@0 22 *
aoqi@0 23 */
aoqi@0 24
aoqi@0 25 #include "precompiled.hpp"
aoqi@0 26 #include "memory/allocation.hpp"
aoqi@0 27 #include "runtime/init.hpp"
aoqi@0 28 #include "runtime/task.hpp"
aoqi@0 29 #include "runtime/thread.inline.hpp"
aoqi@0 30 #include "runtime/timer.hpp"
aoqi@0 31 #ifdef TARGET_OS_FAMILY_linux
aoqi@0 32 # include "os_linux.inline.hpp"
aoqi@0 33 #endif
aoqi@0 34 #ifdef TARGET_OS_FAMILY_solaris
aoqi@0 35 # include "os_solaris.inline.hpp"
aoqi@0 36 #endif
aoqi@0 37 #ifdef TARGET_OS_FAMILY_windows
aoqi@0 38 # include "os_windows.inline.hpp"
aoqi@0 39 #endif
aoqi@0 40 #ifdef TARGET_OS_FAMILY_bsd
aoqi@0 41 # include "os_bsd.inline.hpp"
aoqi@0 42 #endif
aoqi@0 43
aoqi@0 44 int PeriodicTask::_num_tasks = 0;
aoqi@0 45 PeriodicTask* PeriodicTask::_tasks[PeriodicTask::max_tasks];
aoqi@0 46 #ifndef PRODUCT
aoqi@0 47 elapsedTimer PeriodicTask::_timer;
aoqi@0 48 int PeriodicTask::_intervalHistogram[PeriodicTask::max_interval];
aoqi@0 49 int PeriodicTask::_ticks;
aoqi@0 50
aoqi@0 51 void PeriodicTask::print_intervals() {
aoqi@0 52 if (ProfilerCheckIntervals) {
aoqi@0 53 for (int i = 0; i < PeriodicTask::max_interval; i++) {
aoqi@0 54 int n = _intervalHistogram[i];
aoqi@0 55 if (n > 0) tty->print_cr("%3d: %5d (%4.1f%%)", i, n, 100.0 * n / _ticks);
aoqi@0 56 }
aoqi@0 57 }
aoqi@0 58 }
aoqi@0 59 #endif
aoqi@0 60
aoqi@0 61 void PeriodicTask::real_time_tick(int delay_time) {
aoqi@0 62 #ifndef PRODUCT
aoqi@0 63 if (ProfilerCheckIntervals) {
aoqi@0 64 _ticks++;
aoqi@0 65 _timer.stop();
aoqi@0 66 int ms = (int)(_timer.seconds() * 1000.0);
aoqi@0 67 _timer.reset();
aoqi@0 68 _timer.start();
aoqi@0 69 if (ms >= PeriodicTask::max_interval) ms = PeriodicTask::max_interval - 1;
aoqi@0 70 _intervalHistogram[ms]++;
aoqi@0 71 }
aoqi@0 72 #endif
aoqi@0 73
aoqi@0 74 {
aoqi@0 75 MutexLockerEx ml(PeriodicTask_lock, Mutex::_no_safepoint_check_flag);
aoqi@0 76 int orig_num_tasks = _num_tasks;
aoqi@0 77
aoqi@0 78 for(int index = 0; index < _num_tasks; index++) {
aoqi@0 79 _tasks[index]->execute_if_pending(delay_time);
aoqi@0 80 if (_num_tasks < orig_num_tasks) { // task dis-enrolled itself
aoqi@0 81 index--; // re-do current slot as it has changed
aoqi@0 82 orig_num_tasks = _num_tasks;
aoqi@0 83 }
aoqi@0 84 }
aoqi@0 85 }
aoqi@0 86 }
aoqi@0 87
aoqi@0 88 int PeriodicTask::time_to_wait() {
aoqi@0 89 MutexLockerEx ml(PeriodicTask_lock->owned_by_self() ?
aoqi@0 90 NULL : PeriodicTask_lock, Mutex::_no_safepoint_check_flag);
aoqi@0 91
aoqi@0 92 if (_num_tasks == 0) {
aoqi@0 93 return 0; // sleep until shutdown or a task is enrolled
aoqi@0 94 }
aoqi@0 95
aoqi@0 96 int delay = _tasks[0]->time_to_next_interval();
aoqi@0 97 for (int index = 1; index < _num_tasks; index++) {
aoqi@0 98 delay = MIN2(delay, _tasks[index]->time_to_next_interval());
aoqi@0 99 }
aoqi@0 100 return delay;
aoqi@0 101 }
aoqi@0 102
aoqi@0 103
aoqi@0 104 PeriodicTask::PeriodicTask(size_t interval_time) :
aoqi@0 105 _counter(0), _interval((int) interval_time) {
aoqi@0 106 // Sanity check the interval time
aoqi@0 107 assert(_interval >= PeriodicTask::min_interval &&
aoqi@0 108 _interval % PeriodicTask::interval_gran == 0,
aoqi@0 109 "improper PeriodicTask interval time");
aoqi@0 110 }
aoqi@0 111
aoqi@0 112 PeriodicTask::~PeriodicTask() {
aoqi@0 113 disenroll();
aoqi@0 114 }
aoqi@0 115
aoqi@0 116 /* enroll could be called from a JavaThread, so we have to check for
aoqi@0 117 * safepoint when taking the lock to avoid deadlocking */
aoqi@0 118 void PeriodicTask::enroll() {
aoqi@0 119 MutexLockerEx ml(PeriodicTask_lock->owned_by_self() ?
aoqi@0 120 NULL : PeriodicTask_lock);
aoqi@0 121
aoqi@0 122 if (_num_tasks == PeriodicTask::max_tasks) {
aoqi@0 123 fatal("Overflow in PeriodicTask table");
aoqi@0 124 }
aoqi@0 125 _tasks[_num_tasks++] = this;
aoqi@0 126
aoqi@0 127 WatcherThread* thread = WatcherThread::watcher_thread();
aoqi@0 128 if (thread) {
aoqi@0 129 thread->unpark();
aoqi@0 130 } else {
aoqi@0 131 WatcherThread::start();
aoqi@0 132 }
aoqi@0 133 }
aoqi@0 134
aoqi@0 135 /* disenroll could be called from a JavaThread, so we have to check for
aoqi@0 136 * safepoint when taking the lock to avoid deadlocking */
aoqi@0 137 void PeriodicTask::disenroll() {
aoqi@0 138 MutexLockerEx ml(PeriodicTask_lock->owned_by_self() ?
aoqi@0 139 NULL : PeriodicTask_lock);
aoqi@0 140
aoqi@0 141 int index;
aoqi@0 142 for(index = 0; index < _num_tasks && _tasks[index] != this; index++)
aoqi@0 143 ;
aoqi@0 144
aoqi@0 145 if (index == _num_tasks) {
aoqi@0 146 return;
aoqi@0 147 }
aoqi@0 148
aoqi@0 149 _num_tasks--;
aoqi@0 150
aoqi@0 151 for (; index < _num_tasks; index++) {
aoqi@0 152 _tasks[index] = _tasks[index+1];
aoqi@0 153 }
aoqi@0 154 }

mercurial