1 /*
   2  * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
   3  * Copyright (c) 2018, SAP and/or its affiliates. All rights reserved.
   4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   5  *
   6  * This code is free software; you can redistribute it and/or modify it
   7  * under the terms of the GNU General Public License version 2 only, as
   8  * published by the Free Software Foundation.
   9  *
  10  * This code is distributed in the hope that it will be useful, but WITHOUT
  11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  13  * version 2 for more details (a copy is included in the LICENSE file that
  14  * accompanied this code).
  15  *
  16  * You should have received a copy of the GNU General Public License version
  17  * 2 along with this work; if not, write to the Free Software Foundation,
  18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  19  *
  20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  21  * or visit www.oracle.com if you need additional information or have any
  22  * questions.
  23  *
  24  */
  25 
  26 #ifndef SHARE_MEMORY_METASPACE_METASPACESTATISTICS_HPP_
  27 #define SHARE_MEMORY_METASPACE_METASPACESTATISTICS_HPP_
  28 
  29 #include "utilities/globalDefinitions.hpp"
  30 #include "memory/metaspace.hpp" // for MetadataType enum
  31 #include "memory/metaspace/metachunk.hpp" // for ChunkIndex enum
  32 
  33 class outputStream;
  34 
  35 namespace metaspace {
  36 namespace internals {
  37 
  38 // Contains statistics for a number of free chunks.
  39 class FreeChunksStatistics {
  40   uintx _num;         // Number of chunks
  41   size_t _cap;        // Total capacity, in words
  42 
  43 public:
  44   FreeChunksStatistics();
  45 
  46   void reset();
  47 
  48   uintx num() const { return _num; }
  49   size_t cap() const { return _cap; }
  50 
  51   void add(uintx n, size_t s);
  52   void add(const FreeChunksStatistics& other);
  53   void print_on(outputStream* st, size_t scale) const;
  54 
  55 }; // end: FreeChunksStatistics
  56 
  57 
  58 // Contains statistics for a ChunkManager.
  59 class ChunkManagerStatistics {
  60 
  61   FreeChunksStatistics _chunk_stats[NumberOfInUseLists];
  62 
  63 public:
  64 
  65   // Free chunk statistics, by chunk index.
  66   const FreeChunksStatistics& chunk_stats(ChunkIndex index) const   { return _chunk_stats[index]; }
  67   FreeChunksStatistics& chunk_stats(ChunkIndex index)               { return _chunk_stats[index]; }
  68 
  69   void reset();
  70   size_t total_capacity() const;
  71 
  72   void print_on(outputStream* st, size_t scale) const;
  73 
  74 }; // ChunkManagerStatistics
  75 
  76 // Contains statistics for a number of chunks in use.
  77 // Each chunk has a used and free portion; however, there are current chunks (serving
  78 // potential future metaspace allocations) and non-current chunks. Unused portion of the
  79 // former is counted as free, unused portion of the latter counts as waste.
  80 class UsedChunksStatistics {
  81   uintx _num;     // Number of chunks
  82   size_t _cap;    // Total capacity in words.
  83   size_t _used;   // Total used area, in words
  84   size_t _free;   // Total free area (unused portions of current chunks), in words
  85   size_t _waste;  // Total waste area (unused portions of non-current chunks), in words
  86   size_t _overhead; // Total sum of chunk overheads, in words.
  87 
  88 public:
  89 
  90   UsedChunksStatistics();
  91 
  92   void reset();
  93 
  94   uintx num() const { return _num; }
  95 
  96   // Total capacity, in words
  97   size_t cap() const { return _cap; }
  98 
  99   // Total used area, in words
 100   size_t used() const { return _used; }
 101 
 102   // Total free area (unused portions of current chunks), in words
 103   size_t free() const { return _free; }
 104 
 105   // Total waste area (unused portions of non-current chunks), in words
 106   size_t waste() const { return _waste; }
 107 
 108   // Total area spent in overhead (chunk headers), in words
 109   size_t overhead() const { return _overhead; }
 110 
 111   void add_num(uintx n) { _num += n; }
 112   void add_cap(size_t s) { _cap += s; }
 113   void add_used(size_t s) { _used += s; }
 114   void add_free(size_t s) { _free += s; }
 115   void add_waste(size_t s) { _waste += s; }
 116   void add_overhead(size_t s) { _overhead += s; }
 117 
 118   void add(const UsedChunksStatistics& other);
 119 
 120   void print_on(outputStream* st, size_t scale) const;
 121 
 122 #ifdef ASSERT
 123   void check_sanity() const;
 124 #endif
 125 
 126 }; // UsedChunksStatistics
 127 
 128 // Class containing statistics for one or more space managers.
 129 class SpaceManagerStatistics {
 130 
 131   UsedChunksStatistics _chunk_stats[NumberOfInUseLists];
 132   uintx _free_blocks_num;
 133   size_t _free_blocks_cap_words;
 134 
 135 public:
 136 
 137   SpaceManagerStatistics();
 138 
 139   // Chunk statistics by chunk index
 140   const UsedChunksStatistics& chunk_stats(ChunkIndex index) const   { return _chunk_stats[index]; }
 141   UsedChunksStatistics& chunk_stats(ChunkIndex index)               { return _chunk_stats[index]; }
 142 
 143   uintx free_blocks_num () const                                    { return _free_blocks_num; }
 144   size_t free_blocks_cap_words () const                             { return _free_blocks_cap_words; }
 145 
 146   void reset();
 147 
 148   void add_free_blocks_info(uintx num, size_t cap);
 149 
 150   // Returns total chunk statistics over all chunk types.
 151   UsedChunksStatistics totals() const;
 152 
 153   void add(const SpaceManagerStatistics& other);
 154 
 155   void print_on(outputStream* st, size_t scale,  bool detailed) const;
 156 
 157 }; // SpaceManagerStatistics
 158 
 159 class ClassLoaderMetaspaceStatistics {
 160 
 161   SpaceManagerStatistics _sm_stats[Metaspace::MetadataTypeCount];
 162 
 163 public:
 164 
 165   ClassLoaderMetaspaceStatistics();
 166 
 167   const SpaceManagerStatistics& sm_stats(Metaspace::MetadataType mdType) const { return _sm_stats[mdType]; }
 168   SpaceManagerStatistics& sm_stats(Metaspace::MetadataType mdType)             { return _sm_stats[mdType]; }
 169 
 170   const SpaceManagerStatistics& nonclass_sm_stats() const { return sm_stats(Metaspace::NonClassType); }
 171   SpaceManagerStatistics& nonclass_sm_stats()             { return sm_stats(Metaspace::NonClassType); }
 172   const SpaceManagerStatistics& class_sm_stats() const    { return sm_stats(Metaspace::ClassType); }
 173   SpaceManagerStatistics& class_sm_stats()                { return sm_stats(Metaspace::ClassType); }
 174 
 175   void reset();
 176 
 177   void add(const ClassLoaderMetaspaceStatistics& other);
 178 
 179   // Returns total space manager statistics for both class and non-class metaspace
 180   SpaceManagerStatistics totals() const;
 181 
 182   void print_on(outputStream* st, size_t scale, bool detailed) const;
 183 
 184 }; // ClassLoaderMetaspaceStatistics
 185 
 186 } // namespace internals
 187 } // namespace metaspace
 188 
 189 #endif /* SHARE_MEMORY_METASPACE_METASPACESTATISTICS_HPP_ */