src/share/vm/runtime/memprofiler.cpp

Wed, 14 Oct 2020 17:44:48 +0800

author
aoqi
date
Wed, 14 Oct 2020 17:44:48 +0800
changeset 9931
fd44df5e3bc3
parent 9448
73d689add964
permissions
-rw-r--r--

Merge

aoqi@0 1 /*
kevinw@9351 2 * Copyright (c) 1998, 2018, 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 "classfile/systemDictionary.hpp"
aoqi@0 27 #include "code/codeCache.hpp"
aoqi@0 28 #include "gc_interface/collectedHeap.inline.hpp"
aoqi@0 29 #include "interpreter/oopMapCache.hpp"
aoqi@0 30 #include "memory/generation.hpp"
aoqi@0 31 #include "memory/resourceArea.hpp"
aoqi@0 32 #include "runtime/handles.inline.hpp"
aoqi@0 33 #include "runtime/jniHandles.hpp"
aoqi@0 34 #include "runtime/memprofiler.hpp"
aoqi@0 35 #include "runtime/mutexLocker.hpp"
aoqi@0 36 #include "runtime/os.hpp"
aoqi@0 37 #include "runtime/task.hpp"
aoqi@0 38 #include "runtime/thread.inline.hpp"
aoqi@0 39 #include "runtime/vmThread.hpp"
aoqi@0 40
aoqi@0 41 #ifndef PRODUCT
aoqi@0 42
aoqi@0 43 // --------------------------------------------------------
aoqi@0 44 // MemProfilerTask
aoqi@0 45
aoqi@0 46 class MemProfilerTask : public PeriodicTask {
aoqi@0 47 public:
aoqi@0 48 MemProfilerTask(int interval_time) : PeriodicTask(interval_time) {}
aoqi@0 49 void task();
aoqi@0 50 };
aoqi@0 51
aoqi@0 52
aoqi@0 53 void MemProfilerTask::task() {
aoqi@0 54 // Get thread lock to provide mutual exclusion, and so we can iterate safely over the thread list.
aoqi@0 55 MutexLocker mu(Threads_lock);
aoqi@0 56 MemProfiler::do_trace();
aoqi@0 57 }
aoqi@0 58
aoqi@0 59
aoqi@0 60 //----------------------------------------------------------
aoqi@0 61 // Implementation of MemProfiler
aoqi@0 62
aoqi@0 63 MemProfilerTask* MemProfiler::_task = NULL;
aoqi@0 64 FILE* MemProfiler::_log_fp = NULL;
aoqi@0 65
aoqi@0 66
aoqi@0 67 bool MemProfiler::is_active() {
aoqi@0 68 return _task != NULL;
aoqi@0 69 }
aoqi@0 70
aoqi@0 71
aoqi@0 72 void MemProfiler::engage() {
aoqi@0 73 const char *log_name = "mprofile.log";
aoqi@0 74 if (!is_active()) {
aoqi@0 75 // Create log file
aoqi@0 76 _log_fp = fopen(log_name , "w+");
aoqi@0 77 if (_log_fp == NULL) {
aoqi@0 78 fatal(err_msg("MemProfiler: Cannot create log file: %s", log_name));
aoqi@0 79 }
aoqi@0 80 fprintf(_log_fp, "MemProfiler: sizes are in Kb, time is in seconds since startup\n\n");
aoqi@0 81 fprintf(_log_fp, " time, #thr, #cls, heap, heap, perm, perm, code, hndls, rescs, oopmp\n");
aoqi@0 82 fprintf(_log_fp, " used, total, used, total, total, total, total, total\n");
aoqi@0 83 fprintf(_log_fp, "--------------------------------------------------------------------------\n");
aoqi@0 84
aoqi@0 85 _task = new MemProfilerTask(MemProfilingInterval);
aoqi@0 86 _task->enroll();
aoqi@0 87 }
aoqi@0 88 }
aoqi@0 89
aoqi@0 90
aoqi@0 91 void MemProfiler::disengage() {
aoqi@0 92 if (!is_active()) return;
aoqi@0 93 // Do one last trace at disengage time
aoqi@0 94 do_trace();
aoqi@0 95
aoqi@0 96 // Close logfile
aoqi@0 97 fprintf(_log_fp, "MemProfiler detached\n");
aoqi@0 98 fclose(_log_fp);
aoqi@0 99
aoqi@0 100 // remove MemProfilerTask
aoqi@0 101 assert(_task != NULL, "sanity check");
aoqi@0 102 _task->disenroll();
aoqi@0 103 delete _task;
aoqi@0 104 _task = NULL;
aoqi@0 105 }
aoqi@0 106
aoqi@0 107
aoqi@0 108 void MemProfiler::do_trace() {
aoqi@0 109 // Calculate thread local sizes
aoqi@0 110 size_t handles_memory_usage = VMThread::vm_thread()->handle_area()->size_in_bytes();
aoqi@0 111 size_t resource_memory_usage = VMThread::vm_thread()->resource_area()->size_in_bytes();
aoqi@0 112 JavaThread *cur = Threads::first();
aoqi@0 113 while (cur != NULL) {
aoqi@0 114 handles_memory_usage += cur->handle_area()->size_in_bytes();
aoqi@0 115 resource_memory_usage += cur->resource_area()->size_in_bytes();
aoqi@0 116 cur = cur->next();
aoqi@0 117 }
aoqi@0 118
aoqi@0 119 // Print trace line in log
aoqi@0 120 fprintf(_log_fp, "%6.1f,%5d,%5d," UINTX_FORMAT_W(6) "," UINTX_FORMAT_W(6) ",",
aoqi@0 121 os::elapsedTime(),
aoqi@0 122 Threads::number_of_threads(),
aoqi@0 123 SystemDictionary::number_of_classes(),
aoqi@0 124 Universe::heap()->used() / K,
aoqi@0 125 Universe::heap()->capacity() / K);
aoqi@0 126
aoqi@0 127 fprintf(_log_fp, UINTX_FORMAT_W(6) ",", CodeCache::capacity() / K);
aoqi@0 128
kevinw@9351 129 fprintf(_log_fp, UINTX_FORMAT_W(6) "," UINTX_FORMAT_W(6) "," UINTX_FORMAT_W(6) "\n",
aoqi@0 130 handles_memory_usage / K,
aoqi@0 131 resource_memory_usage / K,
aoqi@0 132 OopMapCache::memory_usage() / K);
aoqi@0 133 fflush(_log_fp);
aoqi@0 134 }
aoqi@0 135
aoqi@0 136 #endif

mercurial