3863N/A/*
4168N/A * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
3863N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3863N/A *
3863N/A * This code is free software; you can redistribute it and/or modify it
3863N/A * under the terms of the GNU General Public License version 2 only, as
3863N/A * published by the Free Software Foundation.
3863N/A *
3863N/A * This code is distributed in the hope that it will be useful, but WITHOUT
3863N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
3863N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
3863N/A * version 2 for more details (a copy is included in the LICENSE file that
3863N/A * accompanied this code).
3863N/A *
3863N/A * You should have received a copy of the GNU General Public License version
3863N/A * 2 along with this work; if not, write to the Free Software Foundation,
3863N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
3863N/A *
3863N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
3863N/A * or visit www.oracle.com if you need additional information or have any
3863N/A * questions.
3863N/A *
3863N/A */
3863N/A
3863N/A#include "precompiled.hpp"
3863N/A#include "runtime/threadCritical.hpp"
3863N/A#include "services/memTracker.hpp"
3863N/A#include "services/memTrackWorker.hpp"
3863N/A#include "utilities/decoder.hpp"
3863N/A#include "utilities/vmError.hpp"
3863N/A
4168N/A
4168N/Avoid GenerationData::reset() {
4168N/A _number_of_classes = 0;
4168N/A while (_recorder_list != NULL) {
4168N/A MemRecorder* tmp = _recorder_list;
4168N/A _recorder_list = _recorder_list->next();
4168N/A MemTracker::release_thread_recorder(tmp);
4168N/A }
4168N/A}
4168N/A
4413N/AMemTrackWorker::MemTrackWorker(MemSnapshot* snapshot): _snapshot(snapshot) {
3863N/A // create thread uses cgc thread type for now. We should revisit
3863N/A // the option, or create new thread type.
3863N/A _has_error = !os::create_thread(this, os::cgc_thread);
3863N/A set_name("MemTrackWorker", 0);
3863N/A
3863N/A // initial generation circuit buffer
3863N/A if (!has_error()) {
3863N/A _head = _tail = 0;
3863N/A for(int index = 0; index < MAX_GENERATIONS; index ++) {
4168N/A ::new ((void*)&_gen[index]) GenerationData();
3863N/A }
3863N/A }
3863N/A NOT_PRODUCT(_sync_point_count = 0;)
3863N/A NOT_PRODUCT(_merge_count = 0;)
3863N/A NOT_PRODUCT(_last_gen_in_use = 0;)
3863N/A}
3863N/A
3863N/AMemTrackWorker::~MemTrackWorker() {
3863N/A for (int index = 0; index < MAX_GENERATIONS; index ++) {
4168N/A _gen[index].reset();
3863N/A }
3863N/A}
3863N/A
3863N/Avoid* MemTrackWorker::operator new(size_t size) {
3863N/A assert(false, "use nothrow version");
3863N/A return NULL;
3863N/A}
3863N/A
3863N/Avoid* MemTrackWorker::operator new(size_t size, const std::nothrow_t& nothrow_constant) {
3863N/A return allocate(size, false, mtNMT);
3863N/A}
3863N/A
3863N/Avoid MemTrackWorker::start() {
3863N/A os::start_thread(this);
3863N/A}
3863N/A
3863N/A/*
3863N/A * Native memory tracking worker thread loop:
3863N/A * 1. merge one generation of memory recorders to staging area
3863N/A * 2. promote staging data to memory snapshot
3863N/A *
3863N/A * This thread can run through safepoint.
3863N/A */
3863N/A
3863N/Avoid MemTrackWorker::run() {
3863N/A assert(MemTracker::is_on(), "native memory tracking is off");
3863N/A this->initialize_thread_local_storage();
3863N/A this->record_stack_base_and_size();
4413N/A assert(_snapshot != NULL, "Worker should not be started");
3863N/A MemRecorder* rec;
4186N/A unsigned long processing_generation = 0;
4186N/A bool worker_idle = false;
3863N/A
3863N/A while (!MemTracker::shutdown_in_progress()) {
3863N/A NOT_PRODUCT(_last_gen_in_use = generations_in_use();)
3863N/A {
3863N/A // take a recorder from earliest generation in buffer
3863N/A ThreadCritical tc;
4168N/A rec = _gen[_head].next_recorder();
3863N/A }
3863N/A if (rec != NULL) {
4186N/A if (rec->get_generation() != processing_generation || worker_idle) {
4186N/A processing_generation = rec->get_generation();
4186N/A worker_idle = false;
4186N/A MemTracker::set_current_processing_generation(processing_generation);
4186N/A }
4186N/A
3863N/A // merge the recorder into staging area
4413N/A if (!_snapshot->merge(rec)) {
3949N/A MemTracker::shutdown(MemTracker::NMT_out_of_memory);
3949N/A } else {
3949N/A NOT_PRODUCT(_merge_count ++;)
3949N/A }
3863N/A MemTracker::release_thread_recorder(rec);
3863N/A } else {
3863N/A // no more recorder to merge, promote staging area
3863N/A // to snapshot
3863N/A if (_head != _tail) {
4168N/A long number_of_classes;
3863N/A {
3863N/A ThreadCritical tc;
4168N/A if (_gen[_head].has_more_recorder() || _head == _tail) {
3863N/A continue;
3863N/A }
4168N/A number_of_classes = _gen[_head].number_of_classes();
4168N/A _gen[_head].reset();
4168N/A
3863N/A // done with this generation, increment _head pointer
3863N/A _head = (_head + 1) % MAX_GENERATIONS;
3863N/A }
3863N/A // promote this generation data to snapshot
4413N/A if (!_snapshot->promote(number_of_classes)) {
4058N/A // failed to promote, means out of memory
4058N/A MemTracker::shutdown(MemTracker::NMT_out_of_memory);
4058N/A }
3863N/A } else {
4186N/A // worker thread is idle
4186N/A worker_idle = true;
4186N/A MemTracker::report_worker_idle();
4413N/A _snapshot->wait(1000);
3863N/A ThreadCritical tc;
3863N/A // check if more data arrived
4168N/A if (!_gen[_head].has_more_recorder()) {
4168N/A _gen[_head].add_recorders(MemTracker::get_pending_recorders());
3863N/A }
3863N/A }
3863N/A }
3863N/A }
3863N/A assert(MemTracker::shutdown_in_progress(), "just check");
3863N/A
3949N/A // transits to final shutdown
3863N/A MemTracker::final_shutdown();
3863N/A}
3863N/A
3863N/A// at synchronization point, where 'safepoint visible' Java threads are blocked
3863N/A// at a safepoint, and the rest of threads are blocked on ThreadCritical lock.
3863N/A// The caller MemTracker::sync() already takes ThreadCritical before calling this
3863N/A// method.
3863N/A//
3863N/A// Following tasks are performed:
3863N/A// 1. add all recorders in pending queue to current generation
3863N/A// 2. increase generation
3863N/A
4168N/Avoid MemTrackWorker::at_sync_point(MemRecorder* rec, int number_of_classes) {
3863N/A NOT_PRODUCT(_sync_point_count ++;)
3863N/A assert(count_recorder(rec) <= MemRecorder::_instance_count,
3863N/A "pending queue has infinite loop");
3863N/A
3863N/A bool out_of_generation_buffer = false;
3863N/A // check shutdown state inside ThreadCritical
3863N/A if (MemTracker::shutdown_in_progress()) return;
4168N/A
4168N/A _gen[_tail].set_number_of_classes(number_of_classes);
3863N/A // append the recorders to the end of the generation
4168N/A _gen[_tail].add_recorders(rec);
4168N/A assert(count_recorder(_gen[_tail].peek()) <= MemRecorder::_instance_count,
3863N/A "after add to current generation has infinite loop");
3863N/A // we have collected all recorders for this generation. If there is data,
3863N/A // we need to increment _tail to start a new generation.
4168N/A if (_gen[_tail].has_more_recorder() || _head == _tail) {
3863N/A _tail = (_tail + 1) % MAX_GENERATIONS;
3863N/A out_of_generation_buffer = (_tail == _head);
3863N/A }
3863N/A
3863N/A if (out_of_generation_buffer) {
3863N/A MemTracker::shutdown(MemTracker::NMT_out_of_generation);
3863N/A }
3863N/A}
3863N/A
3863N/A#ifndef PRODUCT
3863N/Aint MemTrackWorker::count_recorder(const MemRecorder* head) {
3863N/A int count = 0;
3863N/A while(head != NULL) {
3863N/A count ++;
3863N/A head = head->next();
3863N/A }
3863N/A return count;
3863N/A}
3863N/A
3863N/Aint MemTrackWorker::count_pending_recorders() const {
3863N/A int count = 0;
3863N/A for (int index = 0; index < MAX_GENERATIONS; index ++) {
4168N/A MemRecorder* head = _gen[index].peek();
3863N/A if (head != NULL) {
3863N/A count += count_recorder(head);
3863N/A }
3863N/A }
3863N/A return count;
3863N/A}
3863N/A#endif