memoryService.hpp revision 7081:39231c6e51fe
1/*
2 * Copyright (c) 2003, 2013, 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#ifndef SHARE_VM_SERVICES_MEMORYSERVICE_HPP
26#define SHARE_VM_SERVICES_MEMORYSERVICE_HPP
27
28#include "memory/allocation.hpp"
29#include "memory/generation.hpp"
30#include "runtime/handles.hpp"
31#include "services/memoryUsage.hpp"
32#include "gc_interface/gcCause.hpp"
33
34// Forward declaration
35class MemoryPool;
36class MemoryManager;
37class GCMemoryManager;
38class CollectedHeap;
39class Generation;
40class DefNewGeneration;
41class PSYoungGen;
42class PSOldGen;
43class CodeHeap;
44class ContiguousSpace;
45class CompactibleFreeListSpace;
46class GenCollectedHeap;
47class ParallelScavengeHeap;
48class G1CollectedHeap;
49
50// VM Monitoring and Management Support
51
52class MemoryService : public AllStatic {
53private:
54  enum {
55    init_pools_list_size = 10,
56    init_managers_list_size = 5,
57    init_code_heap_pools_size = 9
58  };
59
60  // index for minor and major generations
61  enum {
62    minor = 0,
63    major = 1,
64    n_gens = 2
65  };
66
67  static GrowableArray<MemoryPool*>*    _pools_list;
68  static GrowableArray<MemoryManager*>* _managers_list;
69
70  // memory managers for minor and major GC statistics
71  static GCMemoryManager*               _major_gc_manager;
72  static GCMemoryManager*               _minor_gc_manager;
73
74  // memory manager and code heap pools for the CodeCache
75  static MemoryManager*                 _code_cache_manager;
76  static GrowableArray<MemoryPool*>*    _code_heap_pools;
77
78  static MemoryPool*                    _metaspace_pool;
79  static MemoryPool*                    _compressed_class_pool;
80
81  static void add_generation_memory_pool(Generation* gen,
82                                         MemoryManager* major_mgr,
83                                         MemoryManager* minor_mgr);
84  static void add_generation_memory_pool(Generation* gen,
85                                         MemoryManager* major_mgr) {
86    add_generation_memory_pool(gen, major_mgr, NULL);
87  }
88
89
90  static void add_psYoung_memory_pool(PSYoungGen* gen,
91                                      MemoryManager* major_mgr,
92                                      MemoryManager* minor_mgr);
93  static void add_psOld_memory_pool(PSOldGen* gen,
94                                    MemoryManager* mgr);
95
96  static void add_g1YoungGen_memory_pool(G1CollectedHeap* g1h,
97                                         MemoryManager* major_mgr,
98                                         MemoryManager* minor_mgr);
99  static void add_g1OldGen_memory_pool(G1CollectedHeap* g1h,
100                                       MemoryManager* mgr);
101
102  static MemoryPool* add_space(ContiguousSpace* space,
103                               const char* name,
104                               bool is_heap,
105                               size_t max_size,
106                               bool support_usage_threshold);
107  static MemoryPool* add_survivor_spaces(DefNewGeneration* gen,
108                                         const char* name,
109                                         bool is_heap,
110                                         size_t max_size,
111                                         bool support_usage_threshold);
112  static MemoryPool* add_gen(Generation* gen,
113                             const char* name,
114                             bool is_heap,
115                             bool support_usage_threshold);
116  static MemoryPool* add_cms_space(CompactibleFreeListSpace* space,
117                                   const char* name,
118                                   bool is_heap,
119                                   size_t max_size,
120                                   bool support_usage_threshold);
121
122  static void add_gen_collected_heap_info(GenCollectedHeap* heap);
123  static void add_parallel_scavenge_heap_info(ParallelScavengeHeap* heap);
124  static void add_g1_heap_info(G1CollectedHeap* g1h);
125
126public:
127  static void set_universe_heap(CollectedHeap* heap);
128  static void add_code_heap_memory_pool(CodeHeap* heap, const char* name);
129  static void add_metaspace_memory_pools();
130
131  static MemoryPool*    get_memory_pool(instanceHandle pool);
132  static MemoryManager* get_memory_manager(instanceHandle mgr);
133
134  static const int num_memory_pools() {
135    return _pools_list->length();
136  }
137  static const int num_memory_managers() {
138    return _managers_list->length();
139  }
140
141  static MemoryPool* get_memory_pool(int index) {
142    return _pools_list->at(index);
143  }
144
145  static MemoryManager* get_memory_manager(int index) {
146    return _managers_list->at(index);
147  }
148
149  static void track_memory_usage();
150  static void track_code_cache_memory_usage() {
151    // Track memory pool usage of all CodeCache memory pools
152    for (int i = 0; i < _code_heap_pools->length(); ++i) {
153      track_memory_pool_usage(_code_heap_pools->at(i));
154    }
155  }
156  static void track_metaspace_memory_usage() {
157    track_memory_pool_usage(_metaspace_pool);
158  }
159  static void track_compressed_class_memory_usage() {
160    track_memory_pool_usage(_compressed_class_pool);
161  }
162  static void track_memory_pool_usage(MemoryPool* pool);
163
164  static void gc_begin(bool fullGC, bool recordGCBeginTime,
165                       bool recordAccumulatedGCTime,
166                       bool recordPreGCUsage, bool recordPeakUsage);
167  static void gc_end(bool fullGC, bool recordPostGCUsage,
168                     bool recordAccumulatedGCTime,
169                     bool recordGCEndTime, bool countCollection,
170                     GCCause::Cause cause);
171
172
173  static void oops_do(OopClosure* f);
174
175  static bool get_verbose() { return PrintGC; }
176  static bool set_verbose(bool verbose);
177
178  // Create an instance of java/lang/management/MemoryUsage
179  static Handle create_MemoryUsage_obj(MemoryUsage usage, TRAPS);
180
181  static const GCMemoryManager* get_minor_gc_manager() {
182      return _minor_gc_manager;
183  }
184
185  static const GCMemoryManager* get_major_gc_manager() {
186      return _major_gc_manager;
187  }
188};
189
190class TraceMemoryManagerStats : public StackObj {
191private:
192  bool         _fullGC;
193  bool         _recordGCBeginTime;
194  bool         _recordPreGCUsage;
195  bool         _recordPeakUsage;
196  bool         _recordPostGCUsage;
197  bool         _recordAccumulatedGCTime;
198  bool         _recordGCEndTime;
199  bool         _countCollection;
200  GCCause::Cause _cause;
201public:
202  TraceMemoryManagerStats() {}
203  TraceMemoryManagerStats(bool fullGC,
204                          GCCause::Cause cause,
205                          bool recordGCBeginTime = true,
206                          bool recordPreGCUsage = true,
207                          bool recordPeakUsage = true,
208                          bool recordPostGCUsage = true,
209                          bool recordAccumulatedGCTime = true,
210                          bool recordGCEndTime = true,
211                          bool countCollection = true);
212
213  void initialize(bool fullGC,
214                  GCCause::Cause cause,
215                  bool recordGCBeginTime,
216                  bool recordPreGCUsage,
217                  bool recordPeakUsage,
218                  bool recordPostGCUsage,
219                  bool recordAccumulatedGCTime,
220                  bool recordGCEndTime,
221                  bool countCollection);
222
223  TraceMemoryManagerStats(Generation::Name kind, GCCause::Cause cause);
224  ~TraceMemoryManagerStats();
225};
226
227#endif // SHARE_VM_SERVICES_MEMORYSERVICE_HPP
228