1 /*
2 * Copyright (c) 1998, 2018, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
24
25 #include "precompiled.hpp"
26 #include "classfile/classLoaderDataGraph.inline.hpp"
27 #include "classfile/systemDictionary.hpp"
28 #include "code/codeCache.hpp"
29 #include "gc/shared/collectedHeap.inline.hpp"
30 #include "interpreter/oopMapCache.hpp"
31 #include "memory/resourceArea.hpp"
32 #include "memory/universe.hpp"
33 #include "runtime/handles.inline.hpp"
34 #include "runtime/jniHandles.hpp"
35 #include "runtime/memprofiler.hpp"
36 #include "runtime/mutexLocker.hpp"
37 #include "runtime/os.hpp"
38 #include "runtime/task.hpp"
39 #include "runtime/thread.inline.hpp"
40 #include "runtime/threadSMR.hpp"
41 #include "runtime/vmThread.hpp"
42
43 #ifndef PRODUCT
44
45 // --------------------------------------------------------
46 // MemProfilerTask
47
48 class MemProfilerTask : public PeriodicTask {
49 public:
MemProfilerTask(int interval_time)50 MemProfilerTask(int interval_time) : PeriodicTask(interval_time) {}
51 void task();
52 };
53
54
task()55 void MemProfilerTask::task() {
56 MemProfiler::do_trace();
57 }
58
59
60 //----------------------------------------------------------
61 // Implementation of MemProfiler
62
63 MemProfilerTask* MemProfiler::_task = NULL;
64 FILE* MemProfiler::_log_fp = NULL;
65
66
is_active()67 bool MemProfiler::is_active() {
68 return _task != NULL;
69 }
70
71
engage()72 void MemProfiler::engage() {
73 const char *log_name = "mprofile.log";
74 if (!is_active()) {
75 // Create log file
76 _log_fp = fopen(log_name , "w+");
77 if (_log_fp == NULL) {
78 fatal("MemProfiler: Cannot create log file: %s", log_name);
79 }
80 fprintf(_log_fp, "MemProfiler: sizes are in Kb, time is in seconds since startup\n\n");
81 fprintf(_log_fp, " time, #thr, #cls, heap, heap, perm, perm, code, hndls, rescs, oopmp\n");
82 fprintf(_log_fp, " used, total, used, total, total, total, total, total\n");
83 fprintf(_log_fp, "--------------------------------------------------------------------------\n");
84
85 _task = new MemProfilerTask(MemProfilingInterval);
86 _task->enroll();
87 }
88 }
89
90
disengage()91 void MemProfiler::disengage() {
92 if (!is_active()) return;
93 // Do one last trace at disengage time
94 do_trace();
95
96 // Close logfile
97 fprintf(_log_fp, "MemProfiler detached\n");
98 fclose(_log_fp);
99
100 // remove MemProfilerTask
101 assert(_task != NULL, "sanity check");
102 _task->disenroll();
103 delete _task;
104 _task = NULL;
105 }
106
107
do_trace()108 void MemProfiler::do_trace() {
109 // Calculate thread local sizes
110 size_t handles_memory_usage = VMThread::vm_thread()->handle_area()->size_in_bytes();
111 size_t resource_memory_usage = VMThread::vm_thread()->resource_area()->size_in_bytes();
112 {
113 JavaThreadIteratorWithHandle jtiwh;
114 for (; JavaThread *cur = jtiwh.next(); ) {
115 handles_memory_usage += cur->handle_area()->size_in_bytes();
116 resource_memory_usage += cur->resource_area()->size_in_bytes();
117 }
118
119 // Print trace line in log
120 fprintf(_log_fp, "%6.1f,%5d," SIZE_FORMAT_W(5) "," UINTX_FORMAT_W(6) "," UINTX_FORMAT_W(6) ",",
121 os::elapsedTime(),
122 jtiwh.length(),
123 ClassLoaderDataGraph::num_instance_classes(),
124 Universe::heap()->used() / K,
125 Universe::heap()->capacity() / K);
126 }
127
128 fprintf(_log_fp, UINTX_FORMAT_W(6) ",", CodeCache::capacity() / K);
129
130 fprintf(_log_fp, UINTX_FORMAT_W(6) "," UINTX_FORMAT_W(6) ",%6ld\n",
131 handles_memory_usage / K,
132 resource_memory_usage / K,
133 0L);
134 fflush(_log_fp);
135 }
136
137 #endif
138