Path: blob/master/test/hotspot/gtest/classfile/test_symbolTable.cpp
41145 views
/*1* Copyright (c) 2016, 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223#include "precompiled.hpp"24#include "runtime/interfaceSupport.inline.hpp"25#include "classfile/symbolTable.hpp"26#include "threadHelper.inline.hpp"27#include "unittest.hpp"2829TEST_VM(SymbolTable, temp_new_symbol) {30// Assert messages assume these symbols are unique, and the refcounts start at31// one, but code does not rely on this.32JavaThread* THREAD = JavaThread::current();33// the thread should be in vm to use locks34ThreadInVMfromNative ThreadInVMfromNative(THREAD);3536Symbol* abc = SymbolTable::new_symbol("abc");37int abccount = abc->refcount();38TempNewSymbol ss = abc;39ASSERT_EQ(ss->refcount(), abccount) << "only one abc";40ASSERT_EQ(ss->refcount(), abc->refcount()) << "should match TempNewSymbol";4142Symbol* efg = SymbolTable::new_symbol("efg");43Symbol* hij = SymbolTable::new_symbol("hij");44int efgcount = efg->refcount();45int hijcount = hij->refcount();4647TempNewSymbol s1 = efg;48TempNewSymbol s2 = hij;49ASSERT_EQ(s1->refcount(), efgcount) << "one efg";50ASSERT_EQ(s2->refcount(), hijcount) << "one hij";5152// Assignment operator53s1 = s2;54ASSERT_EQ(hij->refcount(), hijcount + 1) << "should be two hij";55ASSERT_EQ(efg->refcount(), efgcount - 1) << "should be no efg";5657s1 = ss; // s1 is abc58ASSERT_EQ(s1->refcount(), abccount + 1) << "should be two abc (s1 and ss)";59ASSERT_EQ(hij->refcount(), hijcount) << "should only have one hij now (s2)";6061s1 = s1; // self assignment62ASSERT_EQ(s1->refcount(), abccount + 1) << "should still be two abc (s1 and ss)";6364TempNewSymbol s3;65Symbol* klm = SymbolTable::new_symbol("klm");66int klmcount = klm->refcount();67s3 = klm; // assignment68ASSERT_EQ(s3->refcount(), klmcount) << "only one klm now";6970Symbol* xyz = SymbolTable::new_symbol("xyz");71int xyzcount = xyz->refcount();72{ // inner scope73TempNewSymbol s_inner = xyz;74}75ASSERT_EQ(xyz->refcount(), xyzcount - 1)76<< "Should have been decremented by dtor in inner scope";7778// Test overflowing refcount making symbol permanent79Symbol* bigsym = SymbolTable::new_symbol("bigsym");80for (int i = 0; i < PERM_REFCOUNT + 100; i++) {81bigsym->increment_refcount();82}83ASSERT_EQ(bigsym->refcount(), PERM_REFCOUNT) << "should not have overflowed";8485// Test that PERM_REFCOUNT is sticky86for (int i = 0; i < 10; i++) {87bigsym->decrement_refcount();88}89ASSERT_EQ(bigsym->refcount(), PERM_REFCOUNT) << "should be sticky";90}9192// TODO: Make two threads one decrementing the refcount and the other trying to increment.93// try_increment_refcount should return false9495#define SYM_NAME_LENGTH 3096static char symbol_name[SYM_NAME_LENGTH];9798class SymbolThread : public JavaTestThread {99public:100SymbolThread(Semaphore* post) : JavaTestThread(post) {}101virtual ~SymbolThread() {}102void main_run() {103for (int i = 0; i < 1000; i++) {104TempNewSymbol sym = SymbolTable::new_symbol(symbol_name);105// Create and destroy new symbol106EXPECT_TRUE(sym->refcount() != 0) << "Symbol refcount unexpectedly zeroed";107}108}109};110111#define SYM_TEST_THREAD_COUNT 5112113class DriverSymbolThread : public JavaTestThread {114public:115Semaphore _done;116DriverSymbolThread(Semaphore* post) : JavaTestThread(post) { };117virtual ~DriverSymbolThread(){}118119void main_run() {120Semaphore done(0);121122// Find a symbol where there will probably be only one instance.123for (int i = 0; i < 100; i++) {124os::snprintf(symbol_name, SYM_NAME_LENGTH, "some_symbol%d", i);125TempNewSymbol ts = SymbolTable::new_symbol(symbol_name);126if (ts->refcount() == 1) {127EXPECT_TRUE(ts->refcount() == 1) << "Symbol is just created";128break; // found a unique symbol129}130}131132SymbolThread* st[SYM_TEST_THREAD_COUNT];133for (int i = 0; i < SYM_TEST_THREAD_COUNT; i++) {134st[i] = new SymbolThread(&done);135st[i]->doit();136}137138for (int i = 0; i < SYM_TEST_THREAD_COUNT; i++) {139done.wait();140}141}142};143144TEST_VM(SymbolTable, test_symbol_refcount_parallel) {145mt_test_doer<DriverSymbolThread>();146}147148149