test/hotspot/gtest/memory/test_metaspace_allocation.cpp
changeset 49852 4d3218e5f170
parent 49401 0c71baee49a7
child 51444 3e5d28e6de32
equal deleted inserted replaced
49851:9082914ccaf2 49852:4d3218e5f170
    24 
    24 
    25 #include "precompiled.hpp"
    25 #include "precompiled.hpp"
    26 #include "memory/allocation.inline.hpp"
    26 #include "memory/allocation.inline.hpp"
    27 #include "memory/metaspace.hpp"
    27 #include "memory/metaspace.hpp"
    28 #include "runtime/mutex.hpp"
    28 #include "runtime/mutex.hpp"
       
    29 #include "runtime/mutexLocker.hpp"
    29 #include "runtime/os.hpp"
    30 #include "runtime/os.hpp"
    30 #include "utilities/align.hpp"
    31 #include "utilities/align.hpp"
    31 #include "utilities/debug.hpp"
    32 #include "utilities/debug.hpp"
    32 #include "utilities/globalDefinitions.hpp"
    33 #include "utilities/globalDefinitions.hpp"
    33 #include "utilities/ostream.hpp"
    34 #include "utilities/ostream.hpp"
   102       ASSERT_TRUE(_spaces[i].lock != NULL);
   103       ASSERT_TRUE(_spaces[i].lock != NULL);
   103     }
   104     }
   104     // Let every ~10th space be an anonymous one to test different allocation patterns.
   105     // Let every ~10th space be an anonymous one to test different allocation patterns.
   105     const Metaspace::MetaspaceType msType = (os::random() % 100 < 10) ?
   106     const Metaspace::MetaspaceType msType = (os::random() % 100 < 10) ?
   106       Metaspace::AnonymousMetaspaceType : Metaspace::StandardMetaspaceType;
   107       Metaspace::AnonymousMetaspaceType : Metaspace::StandardMetaspaceType;
   107     _spaces[i].space = new ClassLoaderMetaspace(_spaces[i].lock, msType);
   108     {
       
   109       // Pull lock during space creation, since this is what happens in the VM too
       
   110       // (see ClassLoaderData::metaspace_non_null(), which we mimick here).
       
   111       MutexLockerEx ml(_spaces[i].lock,  Mutex::_no_safepoint_check_flag);
       
   112       _spaces[i].space = new ClassLoaderMetaspace(_spaces[i].lock, msType);
       
   113     }
   108     _spaces[i].allocated = 0;
   114     _spaces[i].allocated = 0;
   109     ASSERT_TRUE(_spaces[i].space != NULL);
   115     ASSERT_TRUE(_spaces[i].space != NULL);
   110   }
   116   }
   111 
   117 
   112   // Returns the index of a random space where index is [0..metaspaces) and which is
   118   // Returns the index of a random space where index is [0..metaspaces) and which is
   169             if ((float)r < probability_for_large_allocations * 1000.0) {
   175             if ((float)r < probability_for_large_allocations * 1000.0) {
   170               size = (os::random() % _chunk_geometry.medium_chunk_word_size) + _chunk_geometry.medium_chunk_word_size;
   176               size = (os::random() % _chunk_geometry.medium_chunk_word_size) + _chunk_geometry.medium_chunk_word_size;
   171             } else {
   177             } else {
   172               size = os::random() % 64;
   178               size = os::random() % 64;
   173             }
   179             }
       
   180             // Note: In contrast to space creation, no need to lock here. ClassLoaderMetaspace::allocate() will lock itself.
   174             MetaWord* const p = _spaces[index].space->allocate(size, mdType);
   181             MetaWord* const p = _spaces[index].space->allocate(size, mdType);
   175             if (p == NULL) {
   182             if (p == NULL) {
   176               // We very probably did hit the metaspace "until-gc" limit.
   183               // We very probably did hit the metaspace "until-gc" limit.
   177 #ifdef DEBUG_VERBOSE
   184 #ifdef DEBUG_VERBOSE
   178               tty->print_cr("OOM for " SIZE_FORMAT " words. ", size);
   185               tty->print_cr("OOM for " SIZE_FORMAT " words. ", size);
   194         }
   201         }
   195         if (index == -1) {
   202         if (index == -1) {
   196           force_switch = true;
   203           force_switch = true;
   197         } else {
   204         } else {
   198           assert(_spaces[index].space != NULL && _spaces[index].allocated > 0, "Sanity");
   205           assert(_spaces[index].space != NULL && _spaces[index].allocated > 0, "Sanity");
       
   206           // Note: do not lock here. In the "wild" (the VM), we do not so either (see ~ClassLoaderData()).
   199           delete _spaces[index].space;
   207           delete _spaces[index].space;
   200           _spaces[index].space = NULL;
   208           _spaces[index].space = NULL;
   201           _spaces[index].allocated = 0;
   209           _spaces[index].allocated = 0;
   202         }
   210         }
   203       }
   211       }