Tue, 27 Nov 2012 14:20:21 +0100
8003935: Simplify the needed includes for using Thread::current()
Reviewed-by: dholmes, rbackman, coleenp
zgu@3900 | 1 | /* |
zgu@3900 | 2 | * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved. |
zgu@3900 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
zgu@3900 | 4 | * |
zgu@3900 | 5 | * This code is free software; you can redistribute it and/or modify it |
zgu@3900 | 6 | * under the terms of the GNU General Public License version 2 only, as |
zgu@3900 | 7 | * published by the Free Software Foundation. |
zgu@3900 | 8 | * |
zgu@3900 | 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
zgu@3900 | 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
zgu@3900 | 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
zgu@3900 | 12 | * version 2 for more details (a copy is included in the LICENSE file that |
zgu@3900 | 13 | * accompanied this code). |
zgu@3900 | 14 | * |
zgu@3900 | 15 | * You should have received a copy of the GNU General Public License version |
zgu@3900 | 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
zgu@3900 | 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
zgu@3900 | 18 | * |
zgu@3900 | 19 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
zgu@3900 | 20 | * or visit www.oracle.com if you need additional information or have any |
zgu@3900 | 21 | * questions. |
zgu@3900 | 22 | * |
zgu@3900 | 23 | */ |
zgu@3900 | 24 | |
zgu@3900 | 25 | #include "precompiled.hpp" |
zgu@3900 | 26 | #include "runtime/threadCritical.hpp" |
zgu@3900 | 27 | #include "services/memTracker.hpp" |
zgu@3900 | 28 | #include "services/memTrackWorker.hpp" |
zgu@3900 | 29 | #include "utilities/decoder.hpp" |
zgu@3900 | 30 | #include "utilities/vmError.hpp" |
zgu@3900 | 31 | |
zgu@3900 | 32 | MemTrackWorker::MemTrackWorker() { |
zgu@3900 | 33 | // create thread uses cgc thread type for now. We should revisit |
zgu@3900 | 34 | // the option, or create new thread type. |
zgu@3900 | 35 | _has_error = !os::create_thread(this, os::cgc_thread); |
zgu@3900 | 36 | set_name("MemTrackWorker", 0); |
zgu@3900 | 37 | |
zgu@3900 | 38 | // initial generation circuit buffer |
zgu@3900 | 39 | if (!has_error()) { |
zgu@3900 | 40 | _head = _tail = 0; |
zgu@3900 | 41 | for(int index = 0; index < MAX_GENERATIONS; index ++) { |
zgu@3900 | 42 | _gen[index] = NULL; |
zgu@3900 | 43 | } |
zgu@3900 | 44 | } |
zgu@3900 | 45 | NOT_PRODUCT(_sync_point_count = 0;) |
zgu@3900 | 46 | NOT_PRODUCT(_merge_count = 0;) |
zgu@3900 | 47 | NOT_PRODUCT(_last_gen_in_use = 0;) |
zgu@3900 | 48 | } |
zgu@3900 | 49 | |
zgu@3900 | 50 | MemTrackWorker::~MemTrackWorker() { |
zgu@3900 | 51 | for (int index = 0; index < MAX_GENERATIONS; index ++) { |
zgu@3900 | 52 | MemRecorder* rc = _gen[index]; |
zgu@3900 | 53 | if (rc != NULL) { |
zgu@3900 | 54 | delete rc; |
zgu@3900 | 55 | } |
zgu@3900 | 56 | } |
zgu@3900 | 57 | } |
zgu@3900 | 58 | |
zgu@3900 | 59 | void* MemTrackWorker::operator new(size_t size) { |
zgu@3900 | 60 | assert(false, "use nothrow version"); |
zgu@3900 | 61 | return NULL; |
zgu@3900 | 62 | } |
zgu@3900 | 63 | |
zgu@3900 | 64 | void* MemTrackWorker::operator new(size_t size, const std::nothrow_t& nothrow_constant) { |
zgu@3900 | 65 | return allocate(size, false, mtNMT); |
zgu@3900 | 66 | } |
zgu@3900 | 67 | |
zgu@3900 | 68 | void MemTrackWorker::start() { |
zgu@3900 | 69 | os::start_thread(this); |
zgu@3900 | 70 | } |
zgu@3900 | 71 | |
zgu@3900 | 72 | /* |
zgu@3900 | 73 | * Native memory tracking worker thread loop: |
zgu@3900 | 74 | * 1. merge one generation of memory recorders to staging area |
zgu@3900 | 75 | * 2. promote staging data to memory snapshot |
zgu@3900 | 76 | * |
zgu@3900 | 77 | * This thread can run through safepoint. |
zgu@3900 | 78 | */ |
zgu@3900 | 79 | |
zgu@3900 | 80 | void MemTrackWorker::run() { |
zgu@3900 | 81 | assert(MemTracker::is_on(), "native memory tracking is off"); |
zgu@3900 | 82 | this->initialize_thread_local_storage(); |
zgu@3900 | 83 | this->record_stack_base_and_size(); |
zgu@3900 | 84 | MemSnapshot* snapshot = MemTracker::get_snapshot(); |
zgu@3900 | 85 | assert(snapshot != NULL, "Worker should not be started"); |
zgu@3900 | 86 | MemRecorder* rec; |
zgu@3900 | 87 | |
zgu@3900 | 88 | while (!MemTracker::shutdown_in_progress()) { |
zgu@3900 | 89 | NOT_PRODUCT(_last_gen_in_use = generations_in_use();) |
zgu@3900 | 90 | { |
zgu@3900 | 91 | // take a recorder from earliest generation in buffer |
zgu@3900 | 92 | ThreadCritical tc; |
zgu@3900 | 93 | rec = _gen[_head]; |
zgu@3900 | 94 | if (rec != NULL) { |
zgu@3900 | 95 | _gen[_head] = rec->next(); |
zgu@3900 | 96 | } |
zgu@3900 | 97 | assert(count_recorder(_gen[_head]) <= MemRecorder::_instance_count, |
zgu@3900 | 98 | "infinite loop after dequeue"); |
zgu@3900 | 99 | } |
zgu@3900 | 100 | if (rec != NULL) { |
zgu@3900 | 101 | // merge the recorder into staging area |
zgu@3986 | 102 | if (!snapshot->merge(rec)) { |
zgu@3986 | 103 | MemTracker::shutdown(MemTracker::NMT_out_of_memory); |
zgu@3986 | 104 | } else { |
zgu@3986 | 105 | NOT_PRODUCT(_merge_count ++;) |
zgu@3986 | 106 | } |
zgu@3900 | 107 | MemTracker::release_thread_recorder(rec); |
zgu@3900 | 108 | } else { |
zgu@3900 | 109 | // no more recorder to merge, promote staging area |
zgu@3900 | 110 | // to snapshot |
zgu@3900 | 111 | if (_head != _tail) { |
zgu@3900 | 112 | { |
zgu@3900 | 113 | ThreadCritical tc; |
zgu@3900 | 114 | if (_gen[_head] != NULL || _head == _tail) { |
zgu@3900 | 115 | continue; |
zgu@3900 | 116 | } |
zgu@3900 | 117 | // done with this generation, increment _head pointer |
zgu@3900 | 118 | _head = (_head + 1) % MAX_GENERATIONS; |
zgu@3900 | 119 | } |
zgu@3900 | 120 | // promote this generation data to snapshot |
zgu@4053 | 121 | if (!snapshot->promote()) { |
zgu@4053 | 122 | // failed to promote, means out of memory |
zgu@4053 | 123 | MemTracker::shutdown(MemTracker::NMT_out_of_memory); |
zgu@4053 | 124 | } |
zgu@3900 | 125 | } else { |
zgu@3900 | 126 | snapshot->wait(1000); |
zgu@3900 | 127 | ThreadCritical tc; |
zgu@3900 | 128 | // check if more data arrived |
zgu@3900 | 129 | if (_gen[_head] == NULL) { |
zgu@3900 | 130 | _gen[_head] = MemTracker::get_pending_recorders(); |
zgu@3900 | 131 | } |
zgu@3900 | 132 | } |
zgu@3900 | 133 | } |
zgu@3900 | 134 | } |
zgu@3900 | 135 | assert(MemTracker::shutdown_in_progress(), "just check"); |
zgu@3900 | 136 | |
zgu@3986 | 137 | // transits to final shutdown |
zgu@3900 | 138 | MemTracker::final_shutdown(); |
zgu@3900 | 139 | } |
zgu@3900 | 140 | |
zgu@3900 | 141 | // at synchronization point, where 'safepoint visible' Java threads are blocked |
zgu@3900 | 142 | // at a safepoint, and the rest of threads are blocked on ThreadCritical lock. |
zgu@3900 | 143 | // The caller MemTracker::sync() already takes ThreadCritical before calling this |
zgu@3900 | 144 | // method. |
zgu@3900 | 145 | // |
zgu@3900 | 146 | // Following tasks are performed: |
zgu@3900 | 147 | // 1. add all recorders in pending queue to current generation |
zgu@3900 | 148 | // 2. increase generation |
zgu@3900 | 149 | |
zgu@3900 | 150 | void MemTrackWorker::at_sync_point(MemRecorder* rec) { |
zgu@3900 | 151 | NOT_PRODUCT(_sync_point_count ++;) |
zgu@3900 | 152 | assert(count_recorder(rec) <= MemRecorder::_instance_count, |
zgu@3900 | 153 | "pending queue has infinite loop"); |
zgu@3900 | 154 | |
zgu@3900 | 155 | bool out_of_generation_buffer = false; |
zgu@3900 | 156 | // check shutdown state inside ThreadCritical |
zgu@3900 | 157 | if (MemTracker::shutdown_in_progress()) return; |
zgu@3900 | 158 | // append the recorders to the end of the generation |
zgu@3900 | 159 | if( rec != NULL) { |
zgu@3900 | 160 | MemRecorder* cur_head = _gen[_tail]; |
zgu@3900 | 161 | if (cur_head == NULL) { |
zgu@3900 | 162 | _gen[_tail] = rec; |
zgu@3900 | 163 | } else { |
zgu@3900 | 164 | while (cur_head->next() != NULL) { |
zgu@3900 | 165 | cur_head = cur_head->next(); |
zgu@3900 | 166 | } |
zgu@3900 | 167 | cur_head->set_next(rec); |
zgu@3900 | 168 | } |
zgu@3900 | 169 | } |
zgu@3900 | 170 | assert(count_recorder(rec) <= MemRecorder::_instance_count, |
zgu@3900 | 171 | "after add to current generation has infinite loop"); |
zgu@3900 | 172 | // we have collected all recorders for this generation. If there is data, |
zgu@3900 | 173 | // we need to increment _tail to start a new generation. |
zgu@3900 | 174 | if (_gen[_tail] != NULL || _head == _tail) { |
zgu@3900 | 175 | _tail = (_tail + 1) % MAX_GENERATIONS; |
zgu@3900 | 176 | out_of_generation_buffer = (_tail == _head); |
zgu@3900 | 177 | } |
zgu@3900 | 178 | |
zgu@3900 | 179 | if (out_of_generation_buffer) { |
zgu@3900 | 180 | MemTracker::shutdown(MemTracker::NMT_out_of_generation); |
zgu@3900 | 181 | } |
zgu@3900 | 182 | } |
zgu@3900 | 183 | |
zgu@3900 | 184 | #ifndef PRODUCT |
zgu@3900 | 185 | int MemTrackWorker::count_recorder(const MemRecorder* head) { |
zgu@3900 | 186 | int count = 0; |
zgu@3900 | 187 | while(head != NULL) { |
zgu@3900 | 188 | count ++; |
zgu@3900 | 189 | head = head->next(); |
zgu@3900 | 190 | } |
zgu@3900 | 191 | return count; |
zgu@3900 | 192 | } |
zgu@3900 | 193 | |
zgu@3900 | 194 | int MemTrackWorker::count_pending_recorders() const { |
zgu@3900 | 195 | int count = 0; |
zgu@3900 | 196 | for (int index = 0; index < MAX_GENERATIONS; index ++) { |
zgu@3900 | 197 | MemRecorder* head = _gen[index]; |
zgu@3900 | 198 | if (head != NULL) { |
zgu@3900 | 199 | count += count_recorder(head); |
zgu@3900 | 200 | } |
zgu@3900 | 201 | } |
zgu@3900 | 202 | return count; |
zgu@3900 | 203 | } |
zgu@3900 | 204 | #endif |