< prev index next >
src/hotspot/share/memory/metaspace/virtualSpaceNode.hpp
Print this page
rev 57380 : [mq]: metaspace-improvement
@@ -23,141 +23,140 @@
*/
#ifndef SHARE_MEMORY_METASPACE_VIRTUALSPACENODE_HPP
#define SHARE_MEMORY_METASPACE_VIRTUALSPACENODE_HPP
+#include "memory/metaspace/constants.hpp"
+#include "memory/metaspace/chunkTree.hpp"
+#include "memory/metaspace/commitMask.hpp"
#include "memory/virtualspace.hpp"
#include "memory/memRegion.hpp"
#include "utilities/debug.hpp"
+#include "utilities/bitMap.hpp"
#include "utilities/globalDefinitions.hpp"
class outputStream;
namespace metaspace {
-class Metachunk;
-class ChunkManager;
-class OccupancyMap;
-
-// A VirtualSpaceList node.
+// VirtualSpaceNode manage a single address range of the Metaspace.
+//
+// That address range may contain interleaved committed and uncommitted
+// regions. It keeps track of which regions have committed and offers
+// functions to commit and uncommit regions.
+//
+// It allocates and hands out memory ranges, starting at the bottom.
+//
+// Address range must be aligned to root chunk size.
+//
class VirtualSpaceNode : public CHeapObj<mtClass> {
- friend class VirtualSpaceList;
// Link to next VirtualSpaceNode
VirtualSpaceNode* _next;
- // Whether this node is contained in class or metaspace.
- const bool _is_class;
-
- // total in the VirtualSpace
ReservedSpace _rs;
- VirtualSpace _virtual_space;
- MetaWord* _top;
- // count of chunks contained in this VirtualSpace
- uintx _container_count;
-
- OccupancyMap* _occupancy_map;
-
- // Convenience functions to access the _virtual_space
- char* low() const { return virtual_space()->low(); }
- char* high() const { return virtual_space()->high(); }
- char* low_boundary() const { return virtual_space()->low_boundary(); }
- char* high_boundary() const { return virtual_space()->high_boundary(); }
-
- // The first Metachunk will be allocated at the bottom of the
- // VirtualSpace
- Metachunk* first_chunk() { return (Metachunk*) bottom(); }
-
- // Committed but unused space in the virtual space
- size_t free_words_in_vs() const;
-
- // True if this node belongs to class metaspace.
- bool is_class() const { return _is_class; }
-
- // Helper function for take_from_committed: allocate padding chunks
- // until top is at the given address.
- void allocate_padding_chunks_until_top_is_at(MetaWord* target_top);
-
- public:
-
- VirtualSpaceNode(bool is_class, size_t byte_size);
- VirtualSpaceNode(bool is_class, ReservedSpace rs) :
- _next(NULL), _is_class(is_class), _rs(rs), _top(NULL), _container_count(0), _occupancy_map(NULL) {}
- ~VirtualSpaceNode();
-
- // Convenience functions for logical bottom and end
- MetaWord* bottom() const { return (MetaWord*) _virtual_space.low(); }
- MetaWord* end() const { return (MetaWord*) _virtual_space.high(); }
-
- const OccupancyMap* occupancy_map() const { return _occupancy_map; }
- OccupancyMap* occupancy_map() { return _occupancy_map; }
- bool contains(const void* ptr) { return ptr >= low() && ptr < high(); }
+ // Start pointer of the area.
+ MetaWord* const _base;
- size_t reserved_words() const { return _virtual_space.reserved_size() / BytesPerWord; }
- size_t committed_words() const { return _virtual_space.actual_committed_size() / BytesPerWord; }
+ // Size, in words, of the whole node
+ const size_t _word_size;
- bool is_pre_committed() const { return _virtual_space.special(); }
+ // Size, in words, of the used portion of the node
+ size_t _used_words;
- // address of next available space in _virtual_space;
- // Accessors
- VirtualSpaceNode* next() { return _next; }
- void set_next(VirtualSpaceNode* v) { _next = v; }
+ // The bitmap describing the commit state of the region:
+ // Each bit covers a region of 64K (see constants::commit_granule_size).
+ CommitMask _commit_mask;
- void set_top(MetaWord* v) { _top = v; }
+ // An array of chunk trees. Each one describes fragmentation inside the associated root chunk.
+ ChunkTreeArray _chunk_tree_array;
- // Accessors
- VirtualSpace* virtual_space() const { return (VirtualSpace*) &_virtual_space; }
+ /// committing, uncommitting ///
- // Returns true if "word_size" is available in the VirtualSpace
- bool is_available(size_t word_size) { return word_size <= pointer_delta(end(), _top, sizeof(MetaWord)); }
+ // Given a pointer into this node, calculate the start of the commit granule
+ // the pointer points into.
+ MetaWord* calc_start_of_granule(MetaWord* p) const {
+ DEBUG_ONLY(check_pointer(p));
+ return align_down(p, constants::commit_granule_bytes);
+ }
- MetaWord* top() const { return _top; }
- void inc_top(size_t word_size) { _top += word_size; }
+ // Given an address range (which has to be aligned to commit granule size):
+ // - commit it
+ // - mark it as committed in the commit mask
+ // May fail and return NULL if we hit a global commit limit (GC threshold, MaxMetaspaceSize).
+ // Will assert if we run out of memory.
+ bool commit_range(MetaWord* p, size_t word_size);
- uintx container_count() { return _container_count; }
- void inc_container_count();
- void dec_container_count();
+ //// creation ////
- // used and capacity in this single entry in the list
- size_t used_words_in_vs() const;
- size_t capacity_words_in_vs() const;
+ // Create a new empty node spanning the given reserved space.
+ VirtualSpaceNode(ReservedSpace rs);
- bool initialize();
+public:
- // get space from the virtual space
- Metachunk* take_from_committed(size_t chunk_word_size);
+ // Create a node of a given size
+ static VirtualSpaceNode* create_node(size_t word_size);
- // Allocate a chunk from the virtual space and return it.
- Metachunk* get_chunk_vs(size_t chunk_word_size);
+ // Create a node over an existing space
+ static VirtualSpaceNode* create_node(ReservedSpace rs);
- // Expands the committed space by at least min_words words.
- bool expand_by(size_t min_words, size_t preferred_words);
-
- // In preparation for deleting this node, remove all the chunks
- // in the node from any freelist.
- void purge(ChunkManager* chunk_manager);
-
- // If an allocation doesn't fit in the current node a new node is created.
- // Allocate chunks out of the remaining committed space in this node
- // to avoid wasting that memory.
- // This always adds up because all the chunk sizes are multiples of
- // the smallest chunk size.
- void retire(ChunkManager* chunk_manager);
+ ~VirtualSpaceNode();
- void print_on(outputStream* st) const { print_on(st, K); }
- void print_on(outputStream* st, size_t scale) const;
- void print_map(outputStream* st, bool is_class) const;
+ //// Chunk allocation, splitting, merging /////
- // Debug support
- DEBUG_ONLY(void mangle();)
- // Verify counters and basic structure. Slow mode: verify all chunks in depth and occupancy map.
- DEBUG_ONLY(void verify(bool slow);)
- // Verify that all free chunks in this node are ideally merged
- // (there should not be multiple small chunks where a large chunk could exist.)
- DEBUG_ONLY(void verify_free_chunks_are_ideally_merged();)
+ // Allocate a root chunk from this node. Will fail and return NULL
+ // if the node is full.
+ // Note that the chunk memory may or may not be committed. Commit its payload
+ // area before using it.
+ Metachunk* allocate_root_chunk();
+
+ // Given a chunk c, split it recursively until you get a chunk of the given target_level.
+ // Returns pointer to the result chunk; returns split off chunks in p_splinters as linked list.
+ // Returns NULL if chunk cannot be split at least once.
+ Metachunk* split(chklvl_t target_level, Metachunk* c, Metachunk** p_splinters);
+
+ // Given a chunk, attempt to merge it recursively with its neighboring chunks.
+ // If successful (merged at least once), returns address of
+ // the merged chunk; NULL otherwise.
+ //
+ // !!! Please note that if this method returns a non-NULL value, the
+ // original chunk will be invalid and should not be accessed anymore! !!!
+ Metachunk* merge(Metachunk* c);
+
+
+ //// Committing/uncommitting memory /////
+
+ // Given an address range (which has to be aligned to commit granule size), ensure
+ // it is committed.
+ // - commit it
+ // - mark it as committed in the commit mask
+ // May fail and return NULL if we hit a global commit limit (GC threshold, MaxMetaspaceSize).
+ // Will assert if we run out of memory.
+ bool ensure_range_is_committed(MetaWord* p, size_t word_size);
+
+ // Given an address range (which has to be aligned to commit granule size):
+ // - uncommit it
+ // - mark it as uncommitted in the commit mask
+ bool uncommit_range(MetaWord* p, size_t word_size);
+
+
+ /// Debug stuff ////
+
+ // Verify counters and basic structure. Slow mode: verify all chunks in depth
+ bool contains(const MetaWord* p) const {
+ return p >= _base && p < _base + _used_words;
+ }
+
+#ifdef ASSERT
+ void check_pointer(const MetaWord* p) const {
+ assert(contains(p), "invalid pointer");
+ }
+ void verify(bool slow);
+#endif
};
+
} // namespace metaspace
#endif // SHARE_MEMORY_METASPACE_VIRTUALSPACENODE_HPP
< prev index next >