1 /*
2 * Copyright (c) 2003, 2017, 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 *
27
28 #include "gc/shared/gcCause.hpp"
29 #include "memory/allocation.hpp"
30 #include "oops/oopsHierarchy.hpp"
31 #include "runtime/handles.hpp"
32 #include "runtime/timer.hpp"
33 #include "services/memoryUsage.hpp"
34
35 // A memory manager is responsible for managing one or more memory pools.
36 // The garbage collector is one type of memory managers responsible
37 // for reclaiming memory occupied by unreachable objects. A Java virtual
38 // machine may have one or more memory managers. It may
39 // add or remove memory managers during execution.
40 // A memory pool can be managed by more than one memory managers.
41
42 class MemoryPool;
43 class GCMemoryManager;
44 class OopClosure;
45
46 class MemoryManager : public CHeapObj<mtInternal> {
47 private:
48 enum {
49 max_num_pools = 10
50 };
51
52 MemoryPool* _pools[max_num_pools];
53 int _num_pools;
54
55 const char* _name;
56
57 protected:
58 volatile instanceOop _memory_mgr_obj;
59
60 public:
61 MemoryManager(const char* name);
62
63 int num_memory_pools() const { return _num_pools; }
64 MemoryPool* get_memory_pool(int index) {
65 assert(index >= 0 && index < _num_pools, "Invalid index");
66 return _pools[index];
67 }
68
69 void add_pool(MemoryPool* pool);
70
71 bool is_manager(instanceHandle mh) { return mh() == _memory_mgr_obj; }
72
73 virtual instanceOop get_memory_manager_instance(TRAPS);
74 virtual bool is_gc_memory_manager() { return false; }
75
76 const char* name() const { return _name; }
77
78 // GC support
79 void oops_do(OopClosure* f);
80
81 // Static factory methods to get a memory manager of a specific type
82 static MemoryManager* get_code_cache_memory_manager();
83 static MemoryManager* get_metaspace_memory_manager();
84 };
85
86 class GCStatInfo : public ResourceObj {
87 private:
88 size_t _index;
89 jlong _start_time;
126 void set_after_gc_usage(int pool_index, MemoryUsage usage) {
127 assert(pool_index >= 0 && pool_index < _usage_array_size, "Range checking");
128 set_gc_usage(pool_index, usage, false /* after gc */);
129 }
130
131 void clear();
132 };
133
134 class GCMemoryManager : public MemoryManager {
135 private:
136 // TODO: We should unify the GCCounter and GCMemoryManager statistic
137 size_t _num_collections;
138 elapsedTimer _accumulated_timer;
139 elapsedTimer _gc_timer; // for measuring every GC duration
140 GCStatInfo* _last_gc_stat;
141 Mutex* _last_gc_lock;
142 GCStatInfo* _current_gc_stat;
143 int _num_gc_threads;
144 volatile bool _notification_enabled;
145 const char* _gc_end_message;
146 public:
147 GCMemoryManager(const char* name, const char* gc_end_message);
148 ~GCMemoryManager();
149
150 void initialize_gc_stat_info();
151
152 bool is_gc_memory_manager() { return true; }
153 jlong gc_time_ms() { return _accumulated_timer.milliseconds(); }
154 size_t gc_count() { return _num_collections; }
155 int num_gc_threads() { return _num_gc_threads; }
156 void set_num_gc_threads(int count) { _num_gc_threads = count; }
157
158 void gc_begin(bool recordGCBeginTime, bool recordPreGCUsage,
159 bool recordAccumulatedGCTime);
160 void gc_end(bool recordPostGCUsage, bool recordAccumulatedGCTime,
161 bool recordGCEndTime, bool countCollection, GCCause::Cause cause);
162
163 void reset_gc_stat() { _num_collections = 0; _accumulated_timer.reset(); }
164
165 // Copy out _last_gc_stat to the given destination, returning
166 // the collection count. Zero signifies no gc has taken place.
167 size_t get_last_gc_stat(GCStatInfo* dest);
168
169 void set_notification_enabled(bool enabled) { _notification_enabled = enabled; }
170 bool is_notification_enabled() { return _notification_enabled; }
171 };
172
173 #endif // SHARE_VM_SERVICES_MEMORYMANAGER_HPP
|
1 /*
2 * Copyright (c) 2003, 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 *
27
28 #include "gc/shared/gcCause.hpp"
29 #include "memory/allocation.hpp"
30 #include "oops/oopsHierarchy.hpp"
31 #include "runtime/handles.hpp"
32 #include "runtime/timer.hpp"
33 #include "services/memoryUsage.hpp"
34
35 // A memory manager is responsible for managing one or more memory pools.
36 // The garbage collector is one type of memory managers responsible
37 // for reclaiming memory occupied by unreachable objects. A Java virtual
38 // machine may have one or more memory managers. It may
39 // add or remove memory managers during execution.
40 // A memory pool can be managed by more than one memory managers.
41
42 class MemoryPool;
43 class GCMemoryManager;
44 class OopClosure;
45
46 class MemoryManager : public CHeapObj<mtInternal> {
47 protected:
48 enum {
49 max_num_pools = 10
50 };
51
52 private:
53 MemoryPool* _pools[max_num_pools];
54 int _num_pools;
55
56 const char* _name;
57
58 protected:
59 volatile instanceOop _memory_mgr_obj;
60
61 public:
62 MemoryManager(const char* name);
63
64 int num_memory_pools() const { return _num_pools; }
65 MemoryPool* get_memory_pool(int index) {
66 assert(index >= 0 && index < _num_pools, "Invalid index");
67 return _pools[index];
68 }
69
70 int add_pool(MemoryPool* pool);
71
72 bool is_manager(instanceHandle mh) { return mh() == _memory_mgr_obj; }
73
74 virtual instanceOop get_memory_manager_instance(TRAPS);
75 virtual bool is_gc_memory_manager() { return false; }
76
77 const char* name() const { return _name; }
78
79 // GC support
80 void oops_do(OopClosure* f);
81
82 // Static factory methods to get a memory manager of a specific type
83 static MemoryManager* get_code_cache_memory_manager();
84 static MemoryManager* get_metaspace_memory_manager();
85 };
86
87 class GCStatInfo : public ResourceObj {
88 private:
89 size_t _index;
90 jlong _start_time;
127 void set_after_gc_usage(int pool_index, MemoryUsage usage) {
128 assert(pool_index >= 0 && pool_index < _usage_array_size, "Range checking");
129 set_gc_usage(pool_index, usage, false /* after gc */);
130 }
131
132 void clear();
133 };
134
135 class GCMemoryManager : public MemoryManager {
136 private:
137 // TODO: We should unify the GCCounter and GCMemoryManager statistic
138 size_t _num_collections;
139 elapsedTimer _accumulated_timer;
140 elapsedTimer _gc_timer; // for measuring every GC duration
141 GCStatInfo* _last_gc_stat;
142 Mutex* _last_gc_lock;
143 GCStatInfo* _current_gc_stat;
144 int _num_gc_threads;
145 volatile bool _notification_enabled;
146 const char* _gc_end_message;
147 bool _pool_always_affected_by_gc[MemoryManager::max_num_pools];
148
149 public:
150 GCMemoryManager(const char* name, const char* gc_end_message);
151 ~GCMemoryManager();
152
153 int add_pool(MemoryPool* pool, bool always_affected_by_gc = true);
154 bool pool_always_affected_by_gc(int index) {
155 assert(index >= 0 && index < num_memory_pools(), "Invalid index");
156 return _pool_always_affected_by_gc[index];
157 }
158
159 void initialize_gc_stat_info();
160
161 bool is_gc_memory_manager() { return true; }
162 jlong gc_time_ms() { return _accumulated_timer.milliseconds(); }
163 size_t gc_count() { return _num_collections; }
164 int num_gc_threads() { return _num_gc_threads; }
165 void set_num_gc_threads(int count) { _num_gc_threads = count; }
166
167 void gc_begin(bool recordGCBeginTime, bool recordPreGCUsage,
168 bool recordAccumulatedGCTime);
169 void gc_end(bool recordPostGCUsage, bool recordAccumulatedGCTime,
170 bool recordGCEndTime, bool countCollection, GCCause::Cause cause,
171 bool allMemoryPoolsAffected);
172
173 void reset_gc_stat() { _num_collections = 0; _accumulated_timer.reset(); }
174
175 // Copy out _last_gc_stat to the given destination, returning
176 // the collection count. Zero signifies no gc has taken place.
177 size_t get_last_gc_stat(GCStatInfo* dest);
178
179 void set_notification_enabled(bool enabled) { _notification_enabled = enabled; }
180 bool is_notification_enabled() { return _notification_enabled; }
181 };
182
183 #endif // SHARE_VM_SERVICES_MEMORYMANAGER_HPP
|