memoryPool.hpp revision 3465:d2a62e0f25eb
1139747Simp/*
2131952Smarcel * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
3131952Smarcel * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4131952Smarcel *
5131952Smarcel * This code is free software; you can redistribute it and/or modify it
6131952Smarcel * under the terms of the GNU General Public License version 2 only, as
7131952Smarcel * published by the Free Software Foundation.
8131952Smarcel *
9131952Smarcel * This code is distributed in the hope that it will be useful, but WITHOUT
10131952Smarcel * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11131952Smarcel * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12131952Smarcel * version 2 for more details (a copy is included in the LICENSE file that
13131952Smarcel * accompanied this code).
14131952Smarcel *
15131952Smarcel * You should have received a copy of the GNU General Public License version
16131952Smarcel * 2 along with this work; if not, write to the Free Software Foundation,
17131952Smarcel * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18131952Smarcel *
19131952Smarcel * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20131952Smarcel * or visit www.oracle.com if you need additional information or have any
21131952Smarcel * questions.
22131952Smarcel *
23131952Smarcel */
24131952Smarcel
25131952Smarcel#ifndef SHARE_VM_SERVICES_MEMORYPOOL_HPP
26131952Smarcel#define SHARE_VM_SERVICES_MEMORYPOOL_HPP
27131952Smarcel
28131952Smarcel#include "gc_implementation/shared/mutableSpace.hpp"
29131952Smarcel#include "memory/defNewGeneration.hpp"
30131952Smarcel#include "memory/heap.hpp"
31131952Smarcel#include "memory/space.hpp"
32131952Smarcel#include "services/memoryUsage.hpp"
33131952Smarcel#ifndef SERIALGC
34131952Smarcel#include "gc_implementation/concurrentMarkSweep/compactibleFreeListSpace.hpp"
35131952Smarcel#endif
36131952Smarcel
37131952Smarcel// A memory pool represents the memory area that the VM manages.
38131952Smarcel// The Java virtual machine has at least one memory pool
39174910Srwatson// and it may create or remove memory pools during execution.
40131952Smarcel// A memory pool can belong to the heap or the non-heap memory.
41131952Smarcel// A Java virtual machine may also have memory pools belonging to
42131952Smarcel// both heap and non-heap memory.
43131952Smarcel
44131952Smarcel// Forward declaration
45131952Smarcelclass MemoryManager;
46131952Smarcelclass SensorInfo;
47131952Smarcelclass Generation;
48131952Smarcelclass DefNewGeneration;
49174910Srwatsonclass PSPermGen;
50174910Srwatsonclass PermGen;
51131952Smarcelclass ThresholdSupport;
52131952Smarcel
53164029Skibclass MemoryPool : public CHeapObj<mtInternal> {
54234196Sjhb  friend class MemoryManager;
55131952Smarcel public:
56234196Sjhb  enum PoolType {
57234196Sjhb    Heap    = 1,
58131952Smarcel    NonHeap = 2
59131952Smarcel  };
60131952Smarcel
61131952Smarcel private:
62131952Smarcel  enum {
63131952Smarcel    max_num_managers = 5
64131952Smarcel  };
65131952Smarcel
66131952Smarcel  // We could make some of the following as performance counters
67131952Smarcel  // for external monitoring.
68131952Smarcel  const char*      _name;
69131952Smarcel  PoolType         _type;
70131952Smarcel  size_t           _initial_size;
71131952Smarcel  size_t           _max_size;
72131952Smarcel  bool             _available_for_allocation; // Default is true
73131952Smarcel  MemoryManager*   _managers[max_num_managers];
74131952Smarcel  int              _num_managers;
75131952Smarcel  MemoryUsage      _peak_usage;               // Peak memory usage
76131952Smarcel  MemoryUsage      _after_gc_usage;           // After GC memory usage
77131952Smarcel
78131952Smarcel  ThresholdSupport* _usage_threshold;
79131952Smarcel  ThresholdSupport* _gc_usage_threshold;
80131952Smarcel
81131952Smarcel  SensorInfo*      _usage_sensor;
82131952Smarcel  SensorInfo*      _gc_usage_sensor;
83131952Smarcel
84131952Smarcel  volatile instanceOop _memory_pool_obj;
85131952Smarcel
86131952Smarcel  void add_manager(MemoryManager* mgr);
87131952Smarcel
88131952Smarcel public:
89131952Smarcel  MemoryPool(const char* name,
90131952Smarcel             PoolType type,
91131952Smarcel             size_t init_size,
92131952Smarcel             size_t max_size,
93131952Smarcel             bool support_usage_threshold,
94131952Smarcel             bool support_gc_threshold);
95131952Smarcel
96131952Smarcel  const char* name()                       { return _name; }
97131952Smarcel  bool        is_heap()                    { return _type == Heap; }
98131952Smarcel  bool        is_non_heap()                { return _type == NonHeap; }
99131952Smarcel  size_t      initial_size()   const       { return _initial_size; }
100131952Smarcel  int         num_memory_managers() const  { return _num_managers; }
101131952Smarcel  // max size could be changed
102131952Smarcel  virtual size_t max_size()    const       { return _max_size; }
103131952Smarcel
104131952Smarcel  bool is_pool(instanceHandle pool) { return (pool() == _memory_pool_obj); }
105131952Smarcel
106131952Smarcel  bool available_for_allocation()   { return _available_for_allocation; }
107131952Smarcel  bool set_available_for_allocation(bool value) {
108131952Smarcel    bool prev = _available_for_allocation;
109131952Smarcel    _available_for_allocation = value;
110131952Smarcel    return prev;
111131952Smarcel  }
112131952Smarcel
113131952Smarcel  MemoryManager* get_memory_manager(int index) {
114131952Smarcel    assert(index >= 0 && index < _num_managers, "Invalid index");
115131952Smarcel    return _managers[index];
116131952Smarcel  }
117131952Smarcel
118131952Smarcel  // Records current memory usage if it's a peak usage
119131952Smarcel  void record_peak_memory_usage();
120131952Smarcel
121131952Smarcel  MemoryUsage get_peak_memory_usage() {
122131952Smarcel    // check current memory usage first and then return peak usage
123131952Smarcel    record_peak_memory_usage();
124131952Smarcel    return _peak_usage;
125131952Smarcel  }
126131952Smarcel  void        reset_peak_memory_usage() {
127131952Smarcel    _peak_usage = get_memory_usage();
128131952Smarcel  }
129131952Smarcel
130131952Smarcel  ThresholdSupport* usage_threshold()      { return _usage_threshold; }
131131952Smarcel  ThresholdSupport* gc_usage_threshold()   { return _gc_usage_threshold; }
132131952Smarcel
133131952Smarcel  SensorInfo*       usage_sensor()         {  return _usage_sensor; }
134131952Smarcel  SensorInfo*       gc_usage_sensor()      { return _gc_usage_sensor; }
135131952Smarcel
136131952Smarcel  void        set_usage_sensor_obj(instanceHandle s);
137131952Smarcel  void        set_gc_usage_sensor_obj(instanceHandle s);
138131952Smarcel  void        set_last_collection_usage(MemoryUsage u)  { _after_gc_usage = u; }
139131952Smarcel
140131952Smarcel  virtual instanceOop get_memory_pool_instance(TRAPS);
141131952Smarcel  virtual MemoryUsage get_memory_usage() = 0;
142131952Smarcel  virtual size_t      used_in_bytes() = 0;
143131952Smarcel  virtual bool        is_collected_pool()         { return false; }
144131952Smarcel  virtual MemoryUsage get_last_collection_usage() { return _after_gc_usage; }
145131952Smarcel
146131952Smarcel  // GC support
147131952Smarcel  void oops_do(OopClosure* f);
148131952Smarcel};
149131952Smarcel
150131952Smarcelclass CollectedMemoryPool : public MemoryPool {
151131952Smarcelpublic:
152131952Smarcel  CollectedMemoryPool(const char* name, PoolType type, size_t init_size, size_t max_size, bool support_usage_threshold) :
153131952Smarcel    MemoryPool(name, type, init_size, max_size, support_usage_threshold, true) {};
154131952Smarcel  bool is_collected_pool()            { return true; }
155131952Smarcel};
156131952Smarcel
157131952Smarcelclass ContiguousSpacePool : public CollectedMemoryPool {
158131952Smarcelprivate:
159131952Smarcel  ContiguousSpace* _space;
160131952Smarcel
161131952Smarcelpublic:
162131952Smarcel  ContiguousSpacePool(ContiguousSpace* space, const char* name, PoolType type, size_t max_size, bool support_usage_threshold);
163131952Smarcel
164131952Smarcel  ContiguousSpace* space()              { return _space; }
165131952Smarcel  MemoryUsage get_memory_usage();
166131952Smarcel  size_t used_in_bytes()                { return space()->used(); }
167131952Smarcel};
168131952Smarcel
169131952Smarcelclass SurvivorContiguousSpacePool : public CollectedMemoryPool {
170131952Smarcelprivate:
171131952Smarcel  DefNewGeneration* _gen;
172131952Smarcel
173131952Smarcelpublic:
174131952Smarcel  SurvivorContiguousSpacePool(DefNewGeneration* gen,
175131952Smarcel                              const char* name,
176131952Smarcel                              PoolType type,
177183360Sjhb                              size_t max_size,
178131952Smarcel                              bool support_usage_threshold);
179131952Smarcel
180132771Skan  MemoryUsage get_memory_usage();
181132791Skan
182131952Smarcel  size_t used_in_bytes() {
183132771Skan    return _gen->from()->used();
184132791Skan  }
185131952Smarcel  size_t committed_in_bytes() {
186131952Smarcel    return _gen->from()->capacity();
187131952Smarcel  }
188131952Smarcel};
189131952Smarcel
190131952Smarcel#ifndef SERIALGC
191131952Smarcelclass CompactibleFreeListSpacePool : public CollectedMemoryPool {
192131952Smarcelprivate:
193131952Smarcel  CompactibleFreeListSpace* _space;
194131952Smarcelpublic:
195131952Smarcel  CompactibleFreeListSpacePool(CompactibleFreeListSpace* space,
196131952Smarcel                               const char* name,
197131952Smarcel                               PoolType type,
198131952Smarcel                               size_t max_size,
199131952Smarcel                               bool support_usage_threshold);
200174914Srwatson
201131952Smarcel  MemoryUsage get_memory_usage();
202131952Smarcel  size_t used_in_bytes()            { return _space->used(); }
203131952Smarcel};
204131952Smarcel#endif // SERIALGC
205131952Smarcel
206131952Smarcel
207131952Smarcelclass GenerationPool : public CollectedMemoryPool {
208131952Smarcelprivate:
209131952Smarcel  Generation* _gen;
210131952Smarcelpublic:
211131952Smarcel  GenerationPool(Generation* gen, const char* name, PoolType type, bool support_usage_threshold);
212131952Smarcel
213131952Smarcel  MemoryUsage get_memory_usage();
214131952Smarcel  size_t used_in_bytes()                { return _gen->used(); }
215131952Smarcel};
216131952Smarcel
217131952Smarcelclass CodeHeapPool: public MemoryPool {
218131952Smarcelprivate:
219131952Smarcel  CodeHeap* _codeHeap;
220131952Smarcelpublic:
221131952Smarcel  CodeHeapPool(CodeHeap* codeHeap, const char* name, bool support_usage_threshold);
222131952Smarcel  MemoryUsage get_memory_usage();
223131952Smarcel  size_t used_in_bytes()            { return _codeHeap->allocated_capacity(); }
224131952Smarcel};
225131952Smarcel
226131952Smarcel#endif // SHARE_VM_SERVICES_MEMORYPOOL_HPP
227131952Smarcel