memoryManager.cpp revision 0:a61af66fc99e
1/*
2 * Copyright 2003-2005 Sun Microsystems, Inc.  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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any questions.
22 *
23 */
24
25# include "incls/_precompiled.incl"
26# include "incls/_memoryManager.cpp.incl"
27
28HS_DTRACE_PROBE_DECL8(hotspot, mem__pool__gc__begin, char*, int, char*, int,
29  size_t, size_t, size_t, size_t);
30HS_DTRACE_PROBE_DECL8(hotspot, mem__pool__gc__end, char*, int, char*, int,
31  size_t, size_t, size_t, size_t);
32
33MemoryManager::MemoryManager() {
34  _num_pools = 0;
35  _memory_mgr_obj = NULL;
36}
37
38void MemoryManager::add_pool(MemoryPool* pool) {
39  assert(_num_pools < MemoryManager::max_num_pools, "_num_pools exceeds the max");
40  if (_num_pools < MemoryManager::max_num_pools) {
41    _pools[_num_pools] = pool;
42    _num_pools++;
43  }
44  pool->add_manager(this);
45}
46
47MemoryManager* MemoryManager::get_code_cache_memory_manager() {
48  return (MemoryManager*) new CodeCacheMemoryManager();
49}
50
51GCMemoryManager* MemoryManager::get_copy_memory_manager() {
52  return (GCMemoryManager*) new CopyMemoryManager();
53}
54
55GCMemoryManager* MemoryManager::get_msc_memory_manager() {
56  return (GCMemoryManager*) new MSCMemoryManager();
57}
58
59GCMemoryManager* MemoryManager::get_parnew_memory_manager() {
60  return (GCMemoryManager*) new ParNewMemoryManager();
61}
62
63GCMemoryManager* MemoryManager::get_cms_memory_manager() {
64  return (GCMemoryManager*) new CMSMemoryManager();
65}
66
67GCMemoryManager* MemoryManager::get_psScavenge_memory_manager() {
68  return (GCMemoryManager*) new PSScavengeMemoryManager();
69}
70
71GCMemoryManager* MemoryManager::get_psMarkSweep_memory_manager() {
72  return (GCMemoryManager*) new PSMarkSweepMemoryManager();
73}
74
75instanceOop MemoryManager::get_memory_manager_instance(TRAPS) {
76  // Must do an acquire so as to force ordering of subsequent
77  // loads from anything _memory_mgr_obj points to or implies.
78  instanceOop mgr_obj = (instanceOop)OrderAccess::load_ptr_acquire(&_memory_mgr_obj);
79  if (mgr_obj == NULL) {
80    // It's ok for more than one thread to execute the code up to the locked region.
81    // Extra manager instances will just be gc'ed.
82    klassOop k = Management::sun_management_ManagementFactory_klass(CHECK_0);
83    instanceKlassHandle ik(THREAD, k);
84
85    Handle mgr_name = java_lang_String::create_from_str(name(), CHECK_0);
86
87    JavaValue result(T_OBJECT);
88    JavaCallArguments args;
89    args.push_oop(mgr_name);    // Argument 1
90
91    symbolHandle method_name;
92    symbolHandle signature;
93    if (is_gc_memory_manager()) {
94      method_name = vmSymbolHandles::createGarbageCollector_name();
95      signature = vmSymbolHandles::createGarbageCollector_signature();
96      args.push_oop(Handle());      // Argument 2 (for future extension)
97    } else {
98      method_name = vmSymbolHandles::createMemoryManager_name();
99      signature = vmSymbolHandles::createMemoryManager_signature();
100    }
101
102    JavaCalls::call_static(&result,
103                           ik,
104                           method_name,
105                           signature,
106                           &args,
107                           CHECK_0);
108
109    instanceOop m = (instanceOop) result.get_jobject();
110    instanceHandle mgr(THREAD, m);
111
112    {
113      // Get lock before setting _memory_mgr_obj
114      // since another thread may have created the instance
115      MutexLocker ml(Management_lock);
116
117      // Check if another thread has created the management object.  We reload
118      // _memory_mgr_obj here because some other thread may have initialized
119      // it while we were executing the code before the lock.
120      //
121      // The lock has done an acquire, so the load can't float above it, but
122      // we need to do a load_acquire as above.
123      mgr_obj = (instanceOop)OrderAccess::load_ptr_acquire(&_memory_mgr_obj);
124      if (mgr_obj != NULL) {
125         return mgr_obj;
126      }
127
128      // Get the address of the object we created via call_special.
129      mgr_obj = mgr();
130
131      // Use store barrier to make sure the memory accesses associated
132      // with creating the management object are visible before publishing
133      // its address.  The unlock will publish the store to _memory_mgr_obj
134      // because it does a release first.
135      OrderAccess::release_store_ptr(&_memory_mgr_obj, mgr_obj);
136    }
137  }
138
139  return mgr_obj;
140}
141
142void MemoryManager::oops_do(OopClosure* f) {
143  f->do_oop((oop*) &_memory_mgr_obj);
144}
145
146GCStatInfo::GCStatInfo(int num_pools) {
147  // initialize the arrays for memory usage
148  _before_gc_usage_array = (MemoryUsage*) NEW_C_HEAP_ARRAY(MemoryUsage, num_pools);
149  _after_gc_usage_array  = (MemoryUsage*) NEW_C_HEAP_ARRAY(MemoryUsage, num_pools);
150  size_t len = num_pools * sizeof(MemoryUsage);
151  memset(_before_gc_usage_array, 0, len);
152  memset(_after_gc_usage_array, 0, len);
153  _usage_array_size = num_pools;
154}
155
156GCStatInfo::~GCStatInfo() {
157  FREE_C_HEAP_ARRAY(MemoryUsage*, _before_gc_usage_array);
158  FREE_C_HEAP_ARRAY(MemoryUsage*, _after_gc_usage_array);
159}
160
161void GCStatInfo::copy_stat(GCStatInfo* stat) {
162  set_index(stat->gc_index());
163  set_start_time(stat->start_time());
164  set_end_time(stat->end_time());
165  assert(_usage_array_size == stat->usage_array_size(), "Must have same array size");
166  for (int i = 0; i < _usage_array_size; i++) {
167    set_before_gc_usage(i, stat->before_gc_usage_for_pool(i));
168    set_after_gc_usage(i, stat->after_gc_usage_for_pool(i));
169  }
170}
171
172void GCStatInfo::set_gc_usage(int pool_index, MemoryUsage usage, bool before_gc) {
173  MemoryUsage* gc_usage_array;
174  if (before_gc) {
175    gc_usage_array = _before_gc_usage_array;
176  } else {
177    gc_usage_array = _after_gc_usage_array;
178  }
179  gc_usage_array[pool_index] = usage;
180}
181
182GCMemoryManager::GCMemoryManager() : MemoryManager() {
183  _num_collections = 0;
184  _last_gc_stat = NULL;
185  _num_gc_threads = 1;
186}
187
188GCMemoryManager::~GCMemoryManager() {
189  delete _last_gc_stat;
190}
191
192void GCMemoryManager::initialize_gc_stat_info() {
193  assert(MemoryService::num_memory_pools() > 0, "should have one or more memory pools");
194  _last_gc_stat = new GCStatInfo(MemoryService::num_memory_pools());
195}
196
197void GCMemoryManager::gc_begin() {
198  assert(_last_gc_stat != NULL, "Just checking");
199  _accumulated_timer.start();
200  _num_collections++;
201  _last_gc_stat->set_index(_num_collections);
202  _last_gc_stat->set_start_time(Management::timestamp());
203
204  // Keep memory usage of all memory pools
205  for (int i = 0; i < MemoryService::num_memory_pools(); i++) {
206    MemoryPool* pool = MemoryService::get_memory_pool(i);
207    MemoryUsage usage = pool->get_memory_usage();
208    _last_gc_stat->set_before_gc_usage(i, usage);
209    HS_DTRACE_PROBE8(hotspot, mem__pool__gc__begin,
210      name(), strlen(name()),
211      pool->name(), strlen(pool->name()),
212      usage.init_size(), usage.used(),
213      usage.committed(), usage.max_size());
214  }
215}
216
217void GCMemoryManager::gc_end() {
218  _accumulated_timer.stop();
219  _last_gc_stat->set_end_time(Management::timestamp());
220
221  int i;
222  // keep the last gc statistics for all memory pools
223  for (i = 0; i < MemoryService::num_memory_pools(); i++) {
224    MemoryPool* pool = MemoryService::get_memory_pool(i);
225    MemoryUsage usage = pool->get_memory_usage();
226
227    HS_DTRACE_PROBE8(hotspot, mem__pool__gc__end,
228      name(), strlen(name()),
229      pool->name(), strlen(pool->name()),
230      usage.init_size(), usage.used(),
231      usage.committed(), usage.max_size());
232
233    _last_gc_stat->set_after_gc_usage(i, usage);
234  }
235
236  // Set last collection usage of the memory pools managed by this collector
237  for (i = 0; i < num_memory_pools(); i++) {
238    MemoryPool* pool = get_memory_pool(i);
239    MemoryUsage usage = pool->get_memory_usage();
240
241    // Compare with GC usage threshold
242    pool->set_last_collection_usage(usage);
243    LowMemoryDetector::detect_after_gc_memory(pool);
244  }
245}
246