| 1 | /* |
| 2 | * Copyright (c) 2016, 2019, 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" ); |
| 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" ); |
| 44 | Symbol* hij = SymbolTable::new_symbol("hij" ); |
| 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" ); |
| 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" ); |
| 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" ); |
| 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 | for (int i = 0; i < 1000; i++) { |
| 105 | TempNewSymbol sym = SymbolTable::new_symbol(symbol_name); |
| 106 | // Create and destroy new symbol |
| 107 | EXPECT_TRUE(sym->refcount() != 0) << "Symbol refcount unexpectedly zeroed" ; |
| 108 | } |
| 109 | } |
| 110 | }; |
| 111 | |
| 112 | #define SYM_TEST_THREAD_COUNT 5 |
| 113 | |
| 114 | class DriverSymbolThread : public JavaTestThread { |
| 115 | public: |
| 116 | Semaphore _done; |
| 117 | DriverSymbolThread(Semaphore* post) : JavaTestThread(post) { }; |
| 118 | virtual ~DriverSymbolThread(){} |
| 119 | |
| 120 | void main_run() { |
| 121 | Semaphore done(0); |
| 122 | |
| 123 | // Find a symbol where there will probably be only one instance. |
| 124 | for (int i = 0; i < 100; i++) { |
| 125 | os::snprintf(symbol_name, SYM_NAME_LENGTH, "some_symbol%d" , i); |
| 126 | TempNewSymbol ts = SymbolTable::new_symbol(symbol_name); |
| 127 | if (ts->refcount() == 1) { |
| 128 | EXPECT_TRUE(ts->refcount() == 1) << "Symbol is just created" ; |
| 129 | break; // found a unique symbol |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | SymbolThread* st[SYM_TEST_THREAD_COUNT]; |
| 134 | for (int i = 0; i < SYM_TEST_THREAD_COUNT; i++) { |
| 135 | st[i] = new SymbolThread(&done); |
| 136 | st[i]->doit(); |
| 137 | } |
| 138 | |
| 139 | for (int i = 0; i < SYM_TEST_THREAD_COUNT; i++) { |
| 140 | done.wait(); |
| 141 | } |
| 142 | } |
| 143 | }; |
| 144 | |
| 145 | TEST_VM(SymbolTable, test_symbol_refcount_parallel) { |
| 146 | mt_test_doer<DriverSymbolThread>(); |
| 147 | } |
| 148 | |