1 /*
   2  * Copyright (c) 2018, 2020, Oracle and/or its affiliates. All rights reserved.
   3  * Copyright (c) 2018, 2020 SAP SE. 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 GTEST_METASPACE_METASPACE_RANGEHELPERS_HPP
  27 #define GTEST_METASPACE_METASPACE_RANGEHELPERS_HPP
  28 
  29 // We use ranges-of-things in these tests a lot so some helpers help
  30 // keeping the code small.
  31 
  32 #include "memory/allocation.hpp"
  33 #include "memory/metaspace/chunkLevel.hpp"
  34 #include "runtime/os.hpp"
  35 #include "utilities/align.hpp"
  36 #include "utilities/debug.hpp"
  37 #include "utilities/globalDefinitions.hpp"
  38 
  39 
  40 using metaspace::chunklevel_t;
  41 using namespace metaspace::chunklevel;
  42 
  43 
  44 // A range of numerical values.
  45 template <typename T, typename Td>
  46 class Range : public StackObj {
  47 
  48   // start and size of range
  49   T   _start;
  50   Td  _size;
  51 
  52   static Td random_uncapped_offset() {
  53     if (sizeof(Td) > 4) {
  54       return (Td)((uint64_t)os::random() * os::random());
  55     } else {
  56       return (Td)os::random();
  57     }
  58   }
  59 
  60 protected:
  61 
  62   static void swap_if_needed(T& lo, T& hi) {
  63     if (lo > hi) {
  64       T v = lo;
  65       lo = hi;
  66       hi = v;
  67     }
  68   }
  69 
  70 public:
  71 
  72   // Lowest value in range
  73   T lowest() const      { return _start; }
  74 
  75   // Highest value in range (including)
  76   T highest() const     { return _start + (_size - 1); }
  77 
  78   T start() const       { return _start; }
  79   T end() const         { return _start + _size; }
  80 
  81   // Number of values in range
  82   Td size() const       { return _size; }
  83 
  84   bool is_empty() const { return size() == 0; }
  85 
  86   bool contains(T v) const {
  87     return v >= _start && v < end();
  88   }
  89 
  90   bool contains(Range<T, Td> r) const {
  91     return contains(r.lowest()) && contains(r.highest());
  92   }
  93 
  94   // Create a range from [start, end)
  95   Range(T start, T end) : _start(start), _size(end - start) {
  96     assert(end >= start, "start and end reversed");
  97   }
  98 
  99   // a range with a given size, starting at 0
 100   Range(Td size) : _start(0), _size(size) {}
 101 
 102   // Return a random offset
 103   Td random_offset() const {
 104     assert(!is_empty(), "Range too small");
 105     Td v = random_uncapped_offset() % size();
 106     return v;
 107   }
 108 
 109   // Return a random value within the range
 110   T random_value() const {
 111     assert(!is_empty(), "Range too small");
 112     T v = _start + random_offset();
 113     assert(contains(v), "Sanity");
 114     return v;
 115   }
 116 
 117   // Return the head of this range up to but excluding <split_point>
 118   Range<T, Td> head(Td split_point) const {
 119     assert(_size >= split_point, "Sanity");
 120     return Range<T, Td>(_start, _start + split_point);
 121   }
 122 
 123   // Return the tail of this range, starting at <split_point>
 124   Range<T, Td> tail(Td split_point) const {
 125     assert(_size > split_point, "Sanity");
 126     return Range<T, Td>(_start + split_point, end());
 127   }
 128 
 129   // Return a non-empty random sub range.
 130   Range<T, Td> random_subrange() const {
 131     assert(size() > 1, "Range too small");
 132     Td sz = MAX2((Td)1, random_offset());
 133     return random_sized_subrange(sz);
 134   }
 135 
 136   // Return a subrange of given size at a random start position
 137   Range<T, Td> random_sized_subrange(Td subrange_size) const {
 138     assert(subrange_size > 0 && subrange_size < _size, "invalid size");
 139     T start = head(_size - subrange_size).random_value();
 140     return Range<T, Td>(start, start + subrange_size);
 141   }
 142 
 143   //// aligned ranges ////
 144 
 145   bool range_is_aligned(Td alignment) const {
 146     return is_aligned(_size, alignment) && is_aligned(_start, alignment);
 147   }
 148 
 149   // Return a non-empty aligned random sub range.
 150   Range<T, Td> random_aligned_subrange(Td alignment) const {
 151     assert(alignment > 0, "Sanity");
 152     assert(range_is_aligned(alignment), "Outer range needs to be aligned"); // to keep matters simple
 153     assert(_size >= alignment, "Outer range too small.");
 154     Td sz = MAX2((Td)1, random_offset());
 155     sz = align_up(sz, alignment);
 156     return random_aligned_sized_subrange(sz, alignment);
 157   }
 158 
 159   // Return a subrange of given size at a random aligned start position
 160   Range<T, Td> random_aligned_sized_subrange(Td subrange_size, Td alignment) const {
 161     assert(alignment > 0, "Sanity");
 162     assert(range_is_aligned(alignment), "Outer range needs to be aligned"); // to keep matters simple
 163     assert(subrange_size > 0 && subrange_size <= _size &&
 164            is_aligned(subrange_size, alignment), "invalid subrange size");
 165     if (_size == subrange_size) {
 166       return *this;
 167     }
 168     T start = head(_size - subrange_size).random_value();
 169     start = align_down(start, alignment);
 170     return Range<T, Td>(start, start + subrange_size);
 171   }
 172 
 173 };
 174 
 175 typedef Range<int, int> IntRange;
 176 typedef Range<size_t, size_t> SizeRange;
 177 typedef Range<chunklevel_t, int> ChunkLevelRange;
 178 
 179 struct ChunkLevelRanges : public AllStatic {
 180   static ChunkLevelRange small_chunks()  { return ChunkLevelRange(CHUNK_LEVEL_32K, CHUNK_LEVEL_1K + 1); }
 181   static ChunkLevelRange medium_chunks() { return ChunkLevelRange(CHUNK_LEVEL_512K, CHUNK_LEVEL_32K + 1); }
 182   static ChunkLevelRange large_chunks()  { return ChunkLevelRange(CHUNK_LEVEL_4M, CHUNK_LEVEL_512K + 1); }
 183   static ChunkLevelRange all_chunks()    { return ChunkLevelRange(CHUNK_LEVEL_4M, CHUNK_LEVEL_1K + 1); }
 184 };
 185 
 186 #endif // GTEST_METASPACE_METASPACE_RANGEHELPERS_HPP