test/hotspot/gtest/oops/test_markWord.cpp
branchJDK-8200758-branch
changeset 58818 a9316bb4c0e8
parent 57912 2faaa38c86a4
parent 58783 d55b62dbe688
equal deleted inserted replaced
58792:72daf2e005cd 58818:a9316bb4c0e8
       
     1 /*
       
     2  * Copyright (c) 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 "classfile/systemDictionary.hpp"
       
    26 #include "memory/resourceArea.hpp"
       
    27 #include "memory/universe.hpp"
       
    28 #include "oops/oop.inline.hpp"
       
    29 #include "runtime/atomic.hpp"
       
    30 #include "runtime/biasedLocking.hpp"
       
    31 #include "runtime/interfaceSupport.inline.hpp"
       
    32 #include "runtime/orderAccess.hpp"
       
    33 #include "runtime/os.hpp"
       
    34 #include "runtime/synchronizer.hpp"
       
    35 #include "runtime/semaphore.inline.hpp"
       
    36 #include "threadHelper.inline.hpp"
       
    37 #include "unittest.hpp"
       
    38 #include "utilities/globalDefinitions.hpp"
       
    39 #include "utilities/ostream.hpp"
       
    40 
       
    41 // The test doesn't work for PRODUCT because it needs WizardMode
       
    42 #ifndef PRODUCT
       
    43 static bool test_pattern(stringStream* st, const char* pattern) {
       
    44   return (strstr(st->as_string(), pattern) != NULL);
       
    45 }
       
    46 
       
    47 static void assert_test_pattern(Handle object, const char* pattern) {
       
    48   stringStream st;
       
    49   object->print_on(&st);
       
    50   ASSERT_TRUE(test_pattern(&st, pattern)) << pattern << " not in " << st.as_string();
       
    51 }
       
    52 
       
    53 static void assert_not_test_pattern(Handle object, const char* pattern) {
       
    54   stringStream st;
       
    55   object->print_on(&st);
       
    56   ASSERT_FALSE(test_pattern(&st, pattern)) << pattern << " found in " << st.as_string();
       
    57 }
       
    58 
       
    59 class LockerThread : public JavaTestThread {
       
    60   oop _obj;
       
    61   public:
       
    62   LockerThread(Semaphore* post, oop obj) : JavaTestThread(post), _obj(obj) {}
       
    63   virtual ~LockerThread() {}
       
    64 
       
    65   void main_run() {
       
    66     Thread* THREAD = Thread::current();
       
    67     HandleMark hm(THREAD);
       
    68     Handle h_obj(THREAD, _obj);
       
    69     ResourceMark rm(THREAD);
       
    70 
       
    71     // Wait gets the lock inflated.
       
    72     // The object will stay locked for the context of 'ol' so the lock will
       
    73     // still be inflated after the notify_all() call. Deflation can't happen
       
    74     // while an ObjectMonitor is "busy" and being locked is the most "busy"
       
    75     // state we have...
       
    76     ObjectLocker ol(h_obj, THREAD);
       
    77     ol.notify_all(THREAD);
       
    78     assert_test_pattern(h_obj, "monitor");
       
    79   }
       
    80 };
       
    81 
       
    82 
       
    83 TEST_VM(markWord, printing) {
       
    84   JavaThread* THREAD = JavaThread::current();
       
    85   ThreadInVMfromNative invm(THREAD);
       
    86   ResourceMark rm(THREAD);
       
    87 
       
    88   if (!UseBiasedLocking || !BiasedLocking::enabled()) {
       
    89     // Can't test this with biased locking disabled.
       
    90     return;
       
    91   }
       
    92 
       
    93   oop obj = SystemDictionary::Byte_klass()->allocate_instance(THREAD);
       
    94 
       
    95   FlagSetting fs(WizardMode, true);
       
    96 
       
    97   HandleMark hm(THREAD);
       
    98   Handle h_obj(THREAD, obj);
       
    99 
       
   100   // Biased locking is initially enabled for this java.lang.Byte object.
       
   101   assert_test_pattern(h_obj, "is_biased");
       
   102 
       
   103   // Lock using biased locking.
       
   104   BasicObjectLock lock;
       
   105   lock.set_obj(obj);
       
   106   markWord prototype_header = obj->klass()->prototype_header();
       
   107   markWord mark = obj->mark();
       
   108   markWord biased_mark = markWord::encode((JavaThread*) THREAD, mark.age(), prototype_header.bias_epoch());
       
   109   obj->set_mark(biased_mark);
       
   110   // Look for the biased_locker in markWord, not prototype_header.
       
   111 #ifdef _LP64
       
   112   assert_not_test_pattern(h_obj, "mark(is_biased biased_locker=0x0000000000000000");
       
   113 #else
       
   114   assert_not_test_pattern(h_obj, "mark(is_biased biased_locker=0x00000000");
       
   115 #endif
       
   116 
       
   117   // Same thread tries to lock it again.
       
   118   {
       
   119     ObjectLocker ol(h_obj, THREAD);
       
   120     assert_test_pattern(h_obj, "locked");
       
   121   }
       
   122 
       
   123   // This is no longer biased, because ObjectLocker revokes the bias.
       
   124   assert_test_pattern(h_obj, "is_neutral no_hash");
       
   125 
       
   126   // Wait gets the lock inflated.
       
   127   {
       
   128     ObjectLocker ol(h_obj, THREAD);
       
   129 
       
   130     Semaphore done(0);
       
   131     LockerThread* st;
       
   132     st = new LockerThread(&done, h_obj());
       
   133     st->doit();
       
   134 
       
   135     ol.wait(THREAD);
       
   136     assert_test_pattern(h_obj, "monitor");
       
   137     done.wait_with_safepoint_check(THREAD);  // wait till the thread is done.
       
   138   }
       
   139 
       
   140   // Make the object older. Not all GCs use this field.
       
   141   Universe::heap()->collect(GCCause::_java_lang_system_gc);
       
   142   if (UseParallelGC) {
       
   143     assert_test_pattern(h_obj, "is_neutral no_hash age 1");
       
   144   }
       
   145 
       
   146   // Hash the object then print it.
       
   147   intx hash = h_obj->identity_hash();
       
   148   assert_test_pattern(h_obj, "is_neutral hash=0x");
       
   149 }
       
   150 #endif // PRODUCT