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