Path: blob/master/test/hotspot/gtest/metaspace/metaspaceGtestCommon.hpp
41144 views
/*1* Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.2* Copyright (c) 2020 SAP SE. All rights reserved.3* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.4*5* This code is free software; you can redistribute it and/or modify it6* under the terms of the GNU General Public License version 2 only, as7* published by the Free Software Foundation.8*9* This code is distributed in the hope that it will be useful, but WITHOUT10* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or11* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License12* version 2 for more details (a copy is included in the LICENSE file that13* accompanied this code).14*15* You should have received a copy of the GNU General Public License version16* 2 along with this work; if not, write to the Free Software Foundation,17* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.18*19* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA20* or visit www.oracle.com if you need additional information or have any21* questions.22*23*/2425#ifndef GTEST_METASPACE_METASPACEGTESTCOMMON_HPP26#define GTEST_METASPACE_METASPACEGTESTCOMMON_HPP2728#include "memory/allocation.hpp"29#include "utilities/globalDefinitions.hpp"30#include "runtime/os.hpp"31#include "unittest.hpp"3233/////////////////////////////////////////////////////////////////////34// A little mockup to mimick and test the CommitMask in various tests3536class TestMap {37const size_t _len;38char* _arr;39public:40TestMap(size_t len) : _len(len), _arr(NULL) {41_arr = NEW_C_HEAP_ARRAY(char, len, mtInternal);42memset(_arr, 0, _len);43}44~TestMap() { FREE_C_HEAP_ARRAY(char, _arr); }4546int get_num_set(size_t from, size_t to) const {47int result = 0;48for(size_t i = from; i < to; i++) {49if (_arr[i] > 0) {50result++;51}52}53return result;54}5556size_t get_num_set() const { return get_num_set(0, _len); }5758void set_range(size_t from, size_t to) {59memset(_arr + from, 1, to - from);60}6162void clear_range(size_t from, size_t to) {63memset(_arr + from, 0, to - from);64}6566bool at(size_t pos) const {67return _arr[pos] == 1;68}6970};7172///////////////////////////////////////////////////////////73// Helper class for generating random allocation sizes74class RandSizeGenerator {75const size_t _min; // [76const size_t _max; // )77const float _outlier_chance; // 0.0 -- 1.078const size_t _outlier_min; // [79const size_t _outlier_max; // )80public:81RandSizeGenerator(size_t min, size_t max) :82_min(min),83_max(max),84_outlier_chance(0.0),85_outlier_min(min),86_outlier_max(max)87{}8889RandSizeGenerator(size_t min, size_t max, float outlier_chance, size_t outlier_min, size_t outlier_max) :90_min(min),91_max(max),92_outlier_chance(outlier_chance),93_outlier_min(outlier_min),94_outlier_max(outlier_max)95{}9697size_t min() const { return _min; }98size_t max() const { return _max; }99100size_t get() const {101size_t l1 = _min;102size_t l2 = _max;103int r = os::random() % 1000;104if ((float)r < _outlier_chance * 1000.0) {105l1 = _outlier_min;106l2 = _outlier_max;107}108const size_t d = l2 - l1;109return l1 + (os::random() % d);110}111112}; // end RandSizeGenerator113114size_t get_random_size(size_t min, size_t max);115116///////////////////////////////////////////////////////////117// Function to test-access a memory range118119void zap_range(MetaWord* p, size_t word_size);120121// "fill_range_with_pattern" fills a range of heap words with pointers to itself.122//123// The idea is to fill a memory range with a pattern which is both marked clearly to the caller124// and cannot be moved without becoming invalid.125//126// The filled range can be checked with check_range_for_pattern. One also can only check127// a sub range of the original range.128void fill_range_with_pattern(MetaWord* p, uintx pattern, size_t word_size);129void check_range_for_pattern(const MetaWord* p, uintx pattern, size_t word_size);130131// Writes a uniqe pattern to p132void mark_address(MetaWord* p, uintx pattern);133// checks pattern at address134void check_marked_address(const MetaWord* p, uintx pattern);135136// Similar to fill_range_with_pattern, but only marks start and end. This is optimized for cases137// where fill_range_with_pattern just is too slow.138// Use check_marked_range to check the range. In contrast to check_range_for_pattern, only the original139// range can be checked.140void mark_range(MetaWord* p, uintx pattern, size_t word_size);141void check_marked_range(const MetaWord* p, uintx pattern, size_t word_size);142143void mark_range(MetaWord* p, size_t word_size);144void check_marked_range(const MetaWord* p, size_t word_size);145146//////////////////////////////////////////////////////////147// Some helpers to avoid typing out those annoying casts for NULL148149#define ASSERT_NOT_NULL(ptr) ASSERT_NE((void*)NULL, (void*)ptr)150#define ASSERT_NULL(ptr) ASSERT_EQ((void*)NULL, (void*)ptr)151#define EXPECT_NOT_NULL(ptr) EXPECT_NE((void*)NULL, (void*)ptr)152#define EXPECT_NULL(ptr) EXPECT_EQ((void*)NULL, (void*)ptr)153154#define ASSERT_0(v) ASSERT_EQ((intptr_t)0, (intptr_t)v)155#define ASSERT_NOT_0(v) ASSERT_NE((intptr_t)0, (intptr_t)v)156#define EXPECT_0(v) EXPECT_EQ((intptr_t)0, (intptr_t)v)157#define EXPECT_NOT_0(v) EXPECT_NE((intptr_t)0, (intptr_t)v)158#define ASSERT_GT0(v) ASSERT_GT((intptr_t)v, (intptr_t)0)159#define EXPECT_GT0(v) EXPECT_GT((intptr_t)v, (intptr_t)0)160161//////////////////////////////////////////////////////////162// logging163164// Define "LOG_PLEASE" to switch on logging for a particular test before inclusion of this header.165#ifdef LOG_PLEASE166#define LOG(...) { printf(__VA_ARGS__); printf("\n"); fflush(stdout); }167#else168#define LOG(...)169#endif170171//////////////////////////////////////////////////////////172// Helper173174size_t get_workingset_size();175176// A simple preallocated buffer used to "feed" someone.177// Mimicks chunk retirement leftover blocks.178class FeederBuffer {179180MetaWord* _buf;181182// Buffer capacity in size of words.183const size_t _cap;184185// Used words.186size_t _used;187188public:189190FeederBuffer(size_t size) : _buf(NULL), _cap(size), _used(0) {191_buf = NEW_C_HEAP_ARRAY(MetaWord, _cap, mtInternal);192}193194~FeederBuffer() {195FREE_C_HEAP_ARRAY(MetaWord, _buf);196}197198MetaWord* get(size_t word_size) {199if (_used + word_size > _cap) {200return NULL;201}202MetaWord* p = _buf + _used;203_used += word_size;204return p;205}206207bool is_valid_pointer(MetaWord* p) const {208return p >= _buf && p < _buf + _used;209}210211bool is_valid_range(MetaWord* p, size_t word_size) const {212return is_valid_pointer(p) &&213word_size > 0 ? is_valid_pointer(p + word_size - 1) : true;214}215216};217218#endif // GTEST_METASPACE_METASPACEGTESTCOMMON_HPP219220221