test/hotspot/gtest/runtime/test_threadstack_tracking.cpp
changeset 49349 7194eb9e8f19
parent 49348 fde3feaaa4ed
child 49350 cebb0e943ab2
equal deleted inserted replaced
49348:fde3feaaa4ed 49349:7194eb9e8f19
     1 /*
       
     2  * Copyright (c) 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 
       
    26 // Included early because the NMT flags don't include it.
       
    27 #include "utilities/macros.hpp"
       
    28 
       
    29 #include "runtime/thread.hpp"
       
    30 #include "services/memTracker.hpp"
       
    31 #include "services/virtualMemoryTracker.hpp"
       
    32 #include "utilities/globalDefinitions.hpp"
       
    33 #include "unittest.hpp"
       
    34 
       
    35 
       
    36 class ThreadStackTrackingTest {
       
    37 public:
       
    38   static void test() {
       
    39     VirtualMemoryTracker::initialize(NMT_detail);
       
    40     VirtualMemoryTracker::late_initialize(NMT_detail);
       
    41 
       
    42     Thread* thr = Thread::current();
       
    43     address stack_end = thr->stack_end();
       
    44     size_t  stack_size = thr->stack_size();
       
    45 
       
    46     MemTracker::record_thread_stack(stack_end, stack_size);
       
    47 
       
    48     VirtualMemoryTracker::add_reserved_region(stack_end, stack_size, CALLER_PC, mtThreadStack);
       
    49 
       
    50     // snapshot current stack usage
       
    51     VirtualMemoryTracker::snapshot_thread_stacks();
       
    52 
       
    53     ReservedMemoryRegion* rmr = VirtualMemoryTracker::_reserved_regions->find(ReservedMemoryRegion(stack_end, stack_size));
       
    54     ASSERT_TRUE(rmr != NULL);
       
    55 
       
    56     ASSERT_EQ(rmr->base(), stack_end);
       
    57     ASSERT_EQ(rmr->size(), stack_size);
       
    58 
       
    59     CommittedRegionIterator iter = rmr->iterate_committed_regions();
       
    60     int i = 0;
       
    61     address i_addr = (address)&i;
       
    62 
       
    63     // stack grows downward
       
    64     address stack_top = stack_end + stack_size;
       
    65     bool found_stack_top = false;
       
    66 
       
    67     for (const CommittedMemoryRegion* region = iter.next(); region != NULL; region = iter.next()) {
       
    68       if (region->base() + region->size() == stack_top) {
       
    69         // This should be active part, "i" should be here
       
    70         ASSERT_TRUE(i_addr < stack_top && i_addr >= region->base());
       
    71         ASSERT_TRUE(region->size() <= stack_size);
       
    72         found_stack_top = true;
       
    73       }
       
    74 
       
    75       i++;
       
    76     }
       
    77 
       
    78     // NMT was not turned on when the thread was created, so we don't have guard pages
       
    79     ASSERT_TRUE(i == 1);
       
    80     ASSERT_TRUE(found_stack_top);
       
    81   }
       
    82 };
       
    83 
       
    84 TEST_VM(VirtualMemoryTracker, thread_stack_tracking) {
       
    85   ThreadStackTrackingTest::test();
       
    86 }