test/hotspot/gtest/metaspace/metaspaceTestsCommon.hpp
branchstuefe-new-metaspace-branch
changeset 58063 bdf136b8ae0e
child 58099 5aeb07390c74
equal deleted inserted replaced
58062:65cad575ace3 58063:bdf136b8ae0e
       
     1 /*
       
     2  * Copyright (c) 2019, SAP SE. All rights reserved.
       
     3  * Copyright (c) 2019, Oracle 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 #ifndef GTEST_METASPACE_METASPACETESTCOMMON_HPP
       
    26 
       
    27 #include "memory/allocation.hpp"
       
    28 
       
    29 #include "memory/metaspace/chunkHeaderPool.hpp"
       
    30 #include "memory/metaspace/chunkLevel.hpp"
       
    31 #include "memory/metaspace/chunkManager.hpp"
       
    32 #include "memory/metaspace/counter.hpp"
       
    33 #include "memory/metaspace/commitLimiter.hpp"
       
    34 #include "memory/metaspace/metachunk.hpp"
       
    35 #include "memory/metaspace/metaspaceCommon.hpp"
       
    36 #include "memory/metaspace/metaspaceStatistics.hpp"
       
    37 #include "memory/metaspace/virtualSpaceList.hpp"
       
    38 #include "memory/metaspace/spaceManager.hpp"
       
    39 #include "runtime/mutexLocker.hpp"
       
    40 #include "runtime/os.hpp"
       
    41 
       
    42 #include "utilities/align.hpp"
       
    43 #include "utilities/debug.hpp"
       
    44 #include "utilities/globalDefinitions.hpp"
       
    45 
       
    46 #include "unittest.hpp"
       
    47 
       
    48 #include <stdio.h>
       
    49 
       
    50 
       
    51 //////////////////////////////////////////////////////////
       
    52 // handy aliases
       
    53 
       
    54 using metaspace::ChunkAllocSequence;
       
    55 using metaspace::ChunkHeaderPool;
       
    56 using metaspace::ChunkManager;
       
    57 using metaspace::CommitLimiter;
       
    58 using metaspace::CommitMask;
       
    59 using metaspace::SizeCounter;
       
    60 using metaspace::SizeAtomicCounter;
       
    61 using metaspace::IntCounter;
       
    62 using metaspace::Metachunk;
       
    63 using metaspace::MetachunkList;
       
    64 using metaspace::MetachunkListCluster;
       
    65 using metaspace::Settings;
       
    66 using metaspace::sm_stats_t;
       
    67 using metaspace::in_use_chunk_stats_t;
       
    68 using metaspace::cm_stats_t;
       
    69 using metaspace::SizeCounter;
       
    70 using metaspace::SpaceManager;
       
    71 using metaspace::VirtualSpaceList;
       
    72 using metaspace::VirtualSpaceNode;
       
    73 
       
    74 using metaspace::chklvl_t;
       
    75 using metaspace::chklvl::HIGHEST_CHUNK_LEVEL;
       
    76 using metaspace::chklvl::MAX_CHUNK_WORD_SIZE;
       
    77 using metaspace::chklvl::MAX_CHUNK_BYTE_SIZE;
       
    78 using metaspace::chklvl::LOWEST_CHUNK_LEVEL;
       
    79 using metaspace::chklvl::NUM_CHUNK_LEVELS;
       
    80 
       
    81 
       
    82 /////////////////////////////////////////////////////////////////////
       
    83 // A little mockup to mimick and test the CommitMask in various tests
       
    84 
       
    85 class TestMap {
       
    86   const int _len;
       
    87   char* _arr;
       
    88 public:
       
    89   TestMap(int len) : _len(len), _arr(NULL) {
       
    90     _arr = NEW_C_HEAP_ARRAY(char, len, mtInternal);
       
    91     memset(_arr, 0, _len);
       
    92   }
       
    93   ~TestMap() { FREE_C_HEAP_ARRAY(char, _arr); }
       
    94 
       
    95   int get_num_set(int from, int to) const {
       
    96     int result = 0;
       
    97     for(int i = from; i < to; i ++) {
       
    98       if (_arr[i] > 0) {
       
    99         result ++;
       
   100       }
       
   101     }
       
   102     return result;
       
   103   }
       
   104 
       
   105   int get_num_set() const { return get_num_set(0, _len); }
       
   106 
       
   107   void set_range(int from, int to) {
       
   108     memset(_arr + from, 1, to - from);
       
   109   }
       
   110 
       
   111   void clear_range(int from, int to) {
       
   112     memset(_arr + from, 0, to - from);
       
   113   }
       
   114 
       
   115 };
       
   116 
       
   117 
       
   118 
       
   119 ///////////////////////////////////////////////////////////////
       
   120 // Functions to calculate random ranges in outer ranges
       
   121 
       
   122 // Note: [ from..to )
       
   123 struct range_t {
       
   124   size_t from; size_t to;
       
   125 };
       
   126 
       
   127 void calc_random_range(size_t outer_range_len, range_t* out, size_t alignment);
       
   128 
       
   129 // Note:  [ p..p+word_size )
       
   130 struct address_range_t {
       
   131   MetaWord* p; size_t word_size;
       
   132 };
       
   133 
       
   134 void calc_random_address_range(const address_range_t* outer_range, address_range_t* out, size_t alignment);
       
   135 
       
   136 
       
   137 ///////////////////////////////////////////////////////////
       
   138 // Helper class for generating random allocation sizes
       
   139 class RandSizeGenerator {
       
   140   const size_t _min; // [
       
   141   const size_t _max; // )
       
   142   const float _outlier_chance; // 0.0 -- 1.0
       
   143   const size_t _outlier_min; // [
       
   144   const size_t _outlier_max; // )
       
   145 public:
       
   146   RandSizeGenerator(size_t min, size_t max)
       
   147     : _min(min), _max(max), _outlier_chance(0.0), _outlier_min(min), _outlier_max(max)
       
   148   {}
       
   149 
       
   150   RandSizeGenerator(size_t min, size_t max, float outlier_chance, size_t outlier_min, size_t outlier_max)
       
   151     : _min(min), _max(max), _outlier_chance(outlier_chance), _outlier_min(outlier_min), _outlier_max(outlier_max)
       
   152   {}
       
   153 
       
   154   size_t get() const {
       
   155     size_t l1 = _min;
       
   156     size_t l2 = _max;
       
   157     int r = os::random() % 1000;
       
   158     if ((float)r < _outlier_chance * 1000.0) {
       
   159       l1 = _outlier_min;
       
   160       l2 = _outlier_max;
       
   161     }
       
   162     const size_t d = l2 - l1;
       
   163     return l1 + (os::random() % d);
       
   164   }
       
   165 
       
   166 }; // end RandSizeGenerator
       
   167 
       
   168 
       
   169 ///////////////////////////////////////////////////////////
       
   170 // Function to test-access a memory range
       
   171 
       
   172 void zap_range(MetaWord* p, size_t word_size);
       
   173 
       
   174 // "fill_range_with_pattern" fills a range of heap words with pointers to itself.
       
   175 //
       
   176 // The idea is to fill a memory range with a pattern which is both marked clearly to the caller
       
   177 // and cannot be moved without becoming invalid.
       
   178 //
       
   179 // The filled range can be checked with check_range_for_pattern. One also can only check
       
   180 // a sub range of the original range.
       
   181 void fill_range_with_pattern(MetaWord* p, uintx pattern, size_t word_size);
       
   182 bool check_range_for_pattern(const MetaWord* p, uintx pattern, size_t word_size);
       
   183 
       
   184 // Writes a uniqe pattern to p
       
   185 void mark_address(MetaWord* p, uintx pattern);
       
   186 // checks pattern at address
       
   187 bool check_marked_address(const MetaWord* p, uintx pattern);
       
   188 
       
   189 // Similar to fill_range_with_pattern, but only marks start and end. This is optimized for cases
       
   190 // where fill_range_with_pattern just is too slow.
       
   191 // Use check_marked_range to check the range. In contrast to check_range_for_pattern, only the original
       
   192 // range can be checked.
       
   193 void mark_range(MetaWord* p, uintx pattern, size_t word_size);
       
   194 bool check_marked_range(const MetaWord* p, uintx pattern, size_t word_size);
       
   195 
       
   196 //////////////////////////////////////////////////////////
       
   197 // Some helpers to avoid typing out those annoying casts for NULL
       
   198 
       
   199 #define ASSERT_NOT_NULL(ptr)      ASSERT_NE((void*)NULL, (void*)ptr)
       
   200 #define ASSERT_NULL(ptr)          ASSERT_EQ((void*)NULL, (void*)ptr)
       
   201 #define EXPECT_NOT_NULL(ptr)      EXPECT_NE((void*)NULL, (void*)ptr)
       
   202 #define EXPECT_NULL(ptr)          EXPECT_EQ((void*)NULL, (void*)ptr)
       
   203 
       
   204 
       
   205 //////////////////////////////////////////////////////////
       
   206 // logging
       
   207 
       
   208 // Define "LOG_PLEASE" to switch on logging for a particular test before inclusion of this header.
       
   209 #ifdef LOG_PLEASE
       
   210   #define LOG(...) { printf(__VA_ARGS__); printf("\n"); }
       
   211 #else
       
   212   #define LOG(...)
       
   213 #endif
       
   214 
       
   215 //////////////////////////////////////////////////////////
       
   216 // Helper
       
   217 
       
   218 size_t get_workingset_size();
       
   219 
       
   220 
       
   221 #endif // GTEST_METASPACE_METASPACETESTCOMMON_HPP