view test/hotspot/gtest/gc/z/test_zAddress.cpp @ 50525:767cdb97f103

8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental) Reviewed-by: pliden, stefank, eosterlund, ehelin, sjohanss, rbackman, coleenp, ihse, jgeorge, lmesnik, rkennke Contributed-by: per.liden@oracle.com, stefan.karlsson@oracle.com, erik.osterlund@oracle.com, mikael.gerdin@oracle.com, kim.barrett@oracle.com, nils.eliasson@oracle.com, rickard.backman@oracle.com, rwestrel@redhat.com, coleen.phillimore@oracle.com, robbin.ehn@oracle.com, gerard.ziemski@oracle.com, hugh.wilkinson@intel.com, sandhya.viswanathan@intel.com, bill.npo.wheeler@intel.com, vinay.k.awasthi@intel.com, yasuenag@gmail.com
author pliden
date Tue, 12 Jun 2018 17:40:28 +0200
parents
children b833992fa8fa
line wrap: on
line source
/*
 * Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
 * ORACLE PROPRIETARY/CONFIDENTIAL.  Use is subject to license terms.
 */

#include "precompiled.hpp"
#include "gc/z/zAddress.inline.hpp"
#include "gc/z/zGlobals.hpp"
#include "unittest.hpp"

class ZAddressTest : public ::testing::Test {
protected:
  static void is_good_bit(uintptr_t bit_mask) {
    // Setup
    uintptr_t mask_before = ZAddressGoodMask;

    ZAddressMasks::set_good_mask(bit_mask);

    // Test that a pointer with only the given bit is considered good.
    EXPECT_EQ(ZAddress::is_good(ZAddressMetadataMarked0),  (bit_mask == ZAddressMetadataMarked0));
    EXPECT_EQ(ZAddress::is_good(ZAddressMetadataMarked1),  (bit_mask == ZAddressMetadataMarked1));
    EXPECT_EQ(ZAddress::is_good(ZAddressMetadataRemapped), (bit_mask == ZAddressMetadataRemapped));

    // Test that a pointer with the given bit and some extra bits is considered good.
    EXPECT_EQ(ZAddress::is_good(ZAddressMetadataMarked0  | 0x8),(bit_mask == ZAddressMetadataMarked0));
    EXPECT_EQ(ZAddress::is_good(ZAddressMetadataMarked1  | 0x8), (bit_mask == ZAddressMetadataMarked1));
    EXPECT_EQ(ZAddress::is_good(ZAddressMetadataRemapped | 0x8), (bit_mask == ZAddressMetadataRemapped));

    // Test that null is not considered good.
    EXPECT_FALSE(ZAddress::is_good(0));

    // Teardown
    ZAddressMasks::set_good_mask(mask_before);
  }

  static void is_good_or_null_bit(uintptr_t bit_mask) {
    // Setup
    uintptr_t mask_before = ZAddressGoodMask;

    ZAddressMasks::set_good_mask(bit_mask);

    // Test that a pointer with only the given bit is considered good.
    EXPECT_EQ(ZAddress::is_good_or_null(ZAddressMetadataMarked0),  (bit_mask == ZAddressMetadataMarked0));
    EXPECT_EQ(ZAddress::is_good_or_null(ZAddressMetadataMarked1),  (bit_mask == ZAddressMetadataMarked1));
    EXPECT_EQ(ZAddress::is_good_or_null(ZAddressMetadataRemapped), (bit_mask == ZAddressMetadataRemapped));

    // Test that a pointer with the given bit and some extra bits is considered good.
    EXPECT_EQ(ZAddress::is_good_or_null(ZAddressMetadataMarked0  | 0x8), (bit_mask == ZAddressMetadataMarked0));
    EXPECT_EQ(ZAddress::is_good_or_null(ZAddressMetadataMarked1  | 0x8), (bit_mask == ZAddressMetadataMarked1));
    EXPECT_EQ(ZAddress::is_good_or_null(ZAddressMetadataRemapped | 0x8), (bit_mask == ZAddressMetadataRemapped));

    // Test that null is considered good_or_null.
    EXPECT_TRUE(ZAddress::is_good_or_null(0));

    // Teardown
    ZAddressMasks::set_good_mask(mask_before);
  }

  static void finalizable() {
    // Setup
    ZAddressMasks::initialize();
    ZAddressMasks::flip_to_marked();

    // Test that a normal good pointer is good and weak good, but not finalizable
    const uintptr_t addr1 = ZAddress::good(1);
    EXPECT_FALSE(ZAddress::is_finalizable(addr1));
    EXPECT_TRUE(ZAddress::is_marked(addr1));
    EXPECT_FALSE(ZAddress::is_remapped(addr1));
    EXPECT_TRUE(ZAddress::is_weak_good(addr1));
    EXPECT_TRUE(ZAddress::is_weak_good_or_null(addr1));
    EXPECT_TRUE(ZAddress::is_good(addr1));
    EXPECT_TRUE(ZAddress::is_good_or_null(addr1));

    // Test that a finalizable good pointer is finalizable and weak good, but not good
    const uintptr_t addr2 = ZAddress::finalizable_good(1);
    EXPECT_TRUE(ZAddress::is_finalizable(addr2));
    EXPECT_TRUE(ZAddress::is_marked(addr2));
    EXPECT_FALSE(ZAddress::is_remapped(addr2));
    EXPECT_TRUE(ZAddress::is_weak_good(addr2));
    EXPECT_TRUE(ZAddress::is_weak_good_or_null(addr2));
    EXPECT_FALSE(ZAddress::is_good(addr2));
    EXPECT_FALSE(ZAddress::is_good_or_null(addr2));

    // Flip to remapped and test that it's no longer weak good
    ZAddressMasks::flip_to_remapped();
    EXPECT_TRUE(ZAddress::is_finalizable(addr2));
    EXPECT_TRUE(ZAddress::is_marked(addr2));
    EXPECT_FALSE(ZAddress::is_remapped(addr2));
    EXPECT_FALSE(ZAddress::is_weak_good(addr2));
    EXPECT_FALSE(ZAddress::is_weak_good_or_null(addr2));
    EXPECT_FALSE(ZAddress::is_good(addr2));
    EXPECT_FALSE(ZAddress::is_good_or_null(addr2));
  }
};

TEST_F(ZAddressTest, is_good) {
  is_good_bit(ZAddressMetadataMarked0);
  is_good_bit(ZAddressMetadataMarked1);
  is_good_bit(ZAddressMetadataRemapped);
}

TEST_F(ZAddressTest, is_good_or_null) {
  is_good_or_null_bit(ZAddressMetadataMarked0);
  is_good_or_null_bit(ZAddressMetadataMarked1);
  is_good_or_null_bit(ZAddressMetadataRemapped);
}

TEST_F(ZAddressTest, is_weak_good_or_null) {
#define check_is_weak_good_or_null(value)                                        \
  EXPECT_EQ(ZAddress::is_weak_good_or_null(value),                               \
            (ZAddress::is_good_or_null(value) || ZAddress::is_remapped(value)))  \
    << "is_good_or_null: " << ZAddress::is_good_or_null(value)                   \
    << " is_remaped: " << ZAddress::is_remapped(value)                           \
    << " is_good_or_null_or_remapped: " << ZAddress::is_weak_good_or_null(value)

  check_is_weak_good_or_null((uintptr_t)NULL);
  check_is_weak_good_or_null(ZAddressMetadataMarked0);
  check_is_weak_good_or_null(ZAddressMetadataMarked1);
  check_is_weak_good_or_null(ZAddressMetadataRemapped);
  check_is_weak_good_or_null((uintptr_t)0x123);
}

TEST_F(ZAddressTest, finalizable) {
  finalizable();
}