1 /* 2 * Copyright (c) 2016, 2018, Oracle and/or its affiliates. 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 it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 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 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 #include "precompiled.hpp" 25 #include "runtime/interfaceSupport.inline.hpp" 26 #include "classfile/symbolTable.hpp" 27 #include "threadHelper.inline.hpp" 28 #include "unittest.hpp" 29 30 TEST_VM(SymbolTable, temp_new_symbol) { 31 // Assert messages assume these symbols are unique, and the refcounts start at 32 // one, but code does not rely on this. 33 JavaThread* THREAD = JavaThread::current(); 34 // the thread should be in vm to use locks 35 ThreadInVMfromNative ThreadInVMfromNative(THREAD); 36 37 Symbol* abc = SymbolTable::new_symbol("abc", CATCH); 38 int abccount = abc->refcount(); 39 TempNewSymbol ss = abc; 40 ASSERT_EQ(ss->refcount(), abccount) << "only one abc"; 41 ASSERT_EQ(ss->refcount(), abc->refcount()) << "should match TempNewSymbol"; 42 43 Symbol* efg = SymbolTable::new_symbol("efg", CATCH); 44 Symbol* hij = SymbolTable::new_symbol("hij", CATCH); 45 int efgcount = efg->refcount(); 46 int hijcount = hij->refcount(); 47 48 TempNewSymbol s1 = efg; 49 TempNewSymbol s2 = hij; 50 ASSERT_EQ(s1->refcount(), efgcount) << "one efg"; 51 ASSERT_EQ(s2->refcount(), hijcount) << "one hij"; 52 53 // Assignment operator 54 s1 = s2; 55 ASSERT_EQ(hij->refcount(), hijcount + 1) << "should be two hij"; 56 ASSERT_EQ(efg->refcount(), efgcount - 1) << "should be no efg"; 57 58 s1 = ss; // s1 is abc 59 ASSERT_EQ(s1->refcount(), abccount + 1) << "should be two abc (s1 and ss)"; 60 ASSERT_EQ(hij->refcount(), hijcount) << "should only have one hij now (s2)"; 61 62 s1 = s1; // self assignment 63 ASSERT_EQ(s1->refcount(), abccount + 1) << "should still be two abc (s1 and ss)"; 64 65 TempNewSymbol s3; 66 Symbol* klm = SymbolTable::new_symbol("klm", CATCH); 67 int klmcount = klm->refcount(); 68 s3 = klm; // assignment 69 ASSERT_EQ(s3->refcount(), klmcount) << "only one klm now"; 70 71 Symbol* xyz = SymbolTable::new_symbol("xyz", CATCH); 72 int xyzcount = xyz->refcount(); 73 { // inner scope 74 TempNewSymbol s_inner = xyz; 75 } 76 ASSERT_EQ(xyz->refcount(), xyzcount - 1) 77 << "Should have been decremented by dtor in inner scope"; 78 79 // Test overflowing refcount making symbol permanent 80 Symbol* bigsym = SymbolTable::new_symbol("bigsym", CATCH); 81 for (int i = 0; i < PERM_REFCOUNT + 100; i++) { 82 bigsym->increment_refcount(); 83 } 84 ASSERT_EQ(bigsym->refcount(), PERM_REFCOUNT) << "should not have overflowed"; 85 86 // Test that PERM_REFCOUNT is sticky 87 for (int i = 0; i < 10; i++) { 88 bigsym->decrement_refcount(); 89 } 90 ASSERT_EQ(bigsym->refcount(), PERM_REFCOUNT) << "should be sticky"; 91 } 92 93 // TODO: Make two threads one decrementing the refcount and the other trying to increment. 94 // try_increment_refcount should return false 95 96 #define SYM_NAME_LENGTH 30 97 static char symbol_name[SYM_NAME_LENGTH]; 98 99 class SymbolThread : public JavaTestThread { 100 public: 101 SymbolThread(Semaphore* post) : JavaTestThread(post) {} 102 virtual ~SymbolThread() {} 103 void main_run() { 104 Thread* THREAD = Thread::current(); 105 for (int i = 0; i < 1000; i++) { 106 TempNewSymbol sym = SymbolTable::new_symbol(symbol_name, CATCH); 107 // Create and destroy new symbol 108 EXPECT_TRUE(sym->refcount() != 0) << "Symbol refcount unexpectedly zeroed"; 109 } 110 } 111 }; 112 113 #define SYM_TEST_THREAD_COUNT 5 114 115 class DriverSymbolThread : public JavaTestThread { 116 public: 117 Semaphore _done; 118 DriverSymbolThread(Semaphore* post) : JavaTestThread(post) { }; 119 virtual ~DriverSymbolThread(){} 120 121 void main_run() { 122 Semaphore done(0); 123 124 Thread* THREAD = Thread::current(); 125 126 // Find a symbol where there will probably be only one instance. 127 for (int i = 0; i < 100; i++) { 128 os::snprintf(symbol_name, SYM_NAME_LENGTH, "some_symbol%d", i); 129 TempNewSymbol ts = SymbolTable::new_symbol(symbol_name, CATCH); 130 if (ts->refcount() == 1) { 131 EXPECT_TRUE(ts->refcount() == 1) << "Symbol is just created"; 132 break; // found a unique symbol 133 } 134 } 135 136 SymbolThread* st[SYM_TEST_THREAD_COUNT]; 137 for (int i = 0; i < SYM_TEST_THREAD_COUNT; i++) { 138 st[i] = new SymbolThread(&done); 139 st[i]->doit(); 140 } 141 142 for (int i = 0; i < SYM_TEST_THREAD_COUNT; i++) { 143 done.wait(); 144 } 145 } 146 }; 147 148 TEST_VM(SymbolTable, test_symbol_refcount_parallel) { 149 mt_test_doer<DriverSymbolThread>(); 150 }