memTrackWorker.cpp 6.8 KB
Newer Older
Z
zgu 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
/*
 * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 *
 */

#include "precompiled.hpp"
#include "runtime/threadCritical.hpp"
#include "services/memTracker.hpp"
#include "services/memTrackWorker.hpp"
#include "utilities/decoder.hpp"
#include "utilities/vmError.hpp"

32 33 34 35 36 37 38 39 40 41

void GenerationData::reset() {
  _number_of_classes = 0;
  while (_recorder_list != NULL) {
    MemRecorder* tmp = _recorder_list;
    _recorder_list = _recorder_list->next();
    MemTracker::release_thread_recorder(tmp);
  }
}

42
MemTrackWorker::MemTrackWorker(MemSnapshot* snapshot): _snapshot(snapshot) {
Z
zgu 已提交
43 44 45 46 47 48 49 50 51
  // create thread uses cgc thread type for now. We should revisit
  // the option, or create new thread type.
  _has_error = !os::create_thread(this, os::cgc_thread);
  set_name("MemTrackWorker", 0);

  // initial generation circuit buffer
  if (!has_error()) {
    _head = _tail = 0;
    for(int index = 0; index < MAX_GENERATIONS; index ++) {
52
      ::new ((void*)&_gen[index]) GenerationData();
Z
zgu 已提交
53 54 55 56 57 58 59 60 61
    }
  }
  NOT_PRODUCT(_sync_point_count = 0;)
  NOT_PRODUCT(_merge_count = 0;)
  NOT_PRODUCT(_last_gen_in_use = 0;)
}

MemTrackWorker::~MemTrackWorker() {
  for (int index = 0; index < MAX_GENERATIONS; index ++) {
62
    _gen[index].reset();
Z
zgu 已提交
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
  }
}

void* MemTrackWorker::operator new(size_t size) {
  assert(false, "use nothrow version");
  return NULL;
}

void* MemTrackWorker::operator new(size_t size, const std::nothrow_t& nothrow_constant) {
  return allocate(size, false, mtNMT);
}

void MemTrackWorker::start() {
  os::start_thread(this);
}

/*
 * Native memory tracking worker thread loop:
 *   1. merge one generation of memory recorders to staging area
 *   2. promote staging data to memory snapshot
 *
 * This thread can run through safepoint.
 */

void MemTrackWorker::run() {
  assert(MemTracker::is_on(), "native memory tracking is off");
  this->initialize_thread_local_storage();
  this->record_stack_base_and_size();
91
  assert(_snapshot != NULL, "Worker should not be started");
Z
zgu 已提交
92
  MemRecorder* rec;
93 94
  unsigned long processing_generation = 0;
  bool          worker_idle = false;
Z
zgu 已提交
95 96 97 98 99 100

  while (!MemTracker::shutdown_in_progress()) {
    NOT_PRODUCT(_last_gen_in_use = generations_in_use();)
    {
      // take a recorder from earliest generation in buffer
      ThreadCritical tc;
101
      rec = _gen[_head].next_recorder();
Z
zgu 已提交
102 103
    }
    if (rec != NULL) {
104 105 106 107 108 109
      if (rec->get_generation() != processing_generation || worker_idle) {
        processing_generation = rec->get_generation();
        worker_idle = false;
        MemTracker::set_current_processing_generation(processing_generation);
      }

Z
zgu 已提交
110
      // merge the recorder into staging area
111
      if (!_snapshot->merge(rec)) {
112 113 114 115
        MemTracker::shutdown(MemTracker::NMT_out_of_memory);
      } else {
        NOT_PRODUCT(_merge_count ++;)
      }
Z
zgu 已提交
116 117 118 119 120
      MemTracker::release_thread_recorder(rec);
    } else {
      // no more recorder to merge, promote staging area
      // to snapshot
      if (_head != _tail) {
121
        long number_of_classes;
Z
zgu 已提交
122 123
        {
          ThreadCritical tc;
124
          if (_gen[_head].has_more_recorder() || _head == _tail) {
Z
zgu 已提交
125 126
            continue;
          }
127 128 129
          number_of_classes = _gen[_head].number_of_classes();
          _gen[_head].reset();

Z
zgu 已提交
130 131 132 133
          // done with this generation, increment _head pointer
          _head = (_head + 1) % MAX_GENERATIONS;
        }
        // promote this generation data to snapshot
134
        if (!_snapshot->promote(number_of_classes)) {
135 136 137
          // failed to promote, means out of memory
          MemTracker::shutdown(MemTracker::NMT_out_of_memory);
        }
Z
zgu 已提交
138
      } else {
139 140 141
        // worker thread is idle
        worker_idle = true;
        MemTracker::report_worker_idle();
142
        _snapshot->wait(1000);
Z
zgu 已提交
143 144
        ThreadCritical tc;
        // check if more data arrived
145 146
        if (!_gen[_head].has_more_recorder()) {
          _gen[_head].add_recorders(MemTracker::get_pending_recorders());
Z
zgu 已提交
147 148 149 150 151 152
        }
      }
    }
  }
  assert(MemTracker::shutdown_in_progress(), "just check");

153
  // transits to final shutdown
Z
zgu 已提交
154 155 156 157 158 159 160 161 162 163 164 165
  MemTracker::final_shutdown();
}

// at synchronization point, where 'safepoint visible' Java threads are blocked
// at a safepoint, and the rest of threads are blocked on ThreadCritical lock.
// The caller MemTracker::sync() already takes ThreadCritical before calling this
// method.
//
// Following tasks are performed:
//   1. add all recorders in pending queue to current generation
//   2. increase generation

166
void MemTrackWorker::at_sync_point(MemRecorder* rec, int number_of_classes) {
Z
zgu 已提交
167 168 169 170 171 172 173
  NOT_PRODUCT(_sync_point_count ++;)
  assert(count_recorder(rec) <= MemRecorder::_instance_count,
    "pending queue has infinite loop");

  bool out_of_generation_buffer = false;
  // check shutdown state inside ThreadCritical
  if (MemTracker::shutdown_in_progress()) return;
174 175

  _gen[_tail].set_number_of_classes(number_of_classes);
Z
zgu 已提交
176
  // append the recorders to the end of the generation
177 178
  _gen[_tail].add_recorders(rec);
  assert(count_recorder(_gen[_tail].peek()) <= MemRecorder::_instance_count,
Z
zgu 已提交
179 180 181
    "after add to current generation has infinite loop");
  // we have collected all recorders for this generation. If there is data,
  // we need to increment _tail to start a new generation.
182
  if (_gen[_tail].has_more_recorder()  || _head == _tail) {
Z
zgu 已提交
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
    _tail = (_tail + 1) % MAX_GENERATIONS;
    out_of_generation_buffer = (_tail == _head);
  }

  if (out_of_generation_buffer) {
    MemTracker::shutdown(MemTracker::NMT_out_of_generation);
  }
}

#ifndef PRODUCT
int MemTrackWorker::count_recorder(const MemRecorder* head) {
  int count = 0;
  while(head != NULL) {
    count ++;
    head = head->next();
  }
  return count;
}

int MemTrackWorker::count_pending_recorders() const {
  int count = 0;
  for (int index = 0; index < MAX_GENERATIONS; index ++) {
205
    MemRecorder* head = _gen[index].peek();
Z
zgu 已提交
206 207 208 209 210 211 212
    if (head != NULL) {
      count += count_recorder(head);
    }
  }
  return count;
}
#endif