src/hotspot/share/jfr/recorder/checkpoint/types/jfrTypeManager.cpp
changeset 50113 caf115bb98ad
child 50525 767cdb97f103
equal deleted inserted replaced
50112:7a2a740815b7 50113:caf115bb98ad
       
     1 /*
       
     2  * Copyright (c) 2016, 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 
       
    25 #include "precompiled.hpp"
       
    26 #include "jfr/recorder/checkpoint/jfrCheckpointWriter.hpp"
       
    27 #include "jfr/recorder/checkpoint/types/jfrType.hpp"
       
    28 #include "jfr/recorder/checkpoint/types/jfrTypeManager.hpp"
       
    29 #include "jfr/utilities/jfrIterator.hpp"
       
    30 #include "runtime/safepoint.hpp"
       
    31 #include "runtime/thread.inline.hpp"
       
    32 #include "utilities/exceptions.hpp"
       
    33 
       
    34 JfrSerializerRegistration::JfrSerializerRegistration(JfrTypeId id, bool permit_cache, JfrSerializer* cs) :
       
    35   _next(NULL),
       
    36   _prev(NULL),
       
    37   _serializer(cs),
       
    38   _cache(),
       
    39   _id(id),
       
    40   _permit_cache(permit_cache) {}
       
    41 
       
    42 JfrSerializerRegistration::~JfrSerializerRegistration() {
       
    43   delete _serializer;
       
    44 }
       
    45 
       
    46 JfrSerializerRegistration* JfrSerializerRegistration::next() const {
       
    47   return _next;
       
    48 }
       
    49 
       
    50 void JfrSerializerRegistration::set_next(JfrSerializerRegistration* next) {
       
    51   _next = next;
       
    52 }
       
    53 
       
    54 JfrSerializerRegistration* JfrSerializerRegistration::prev() const {
       
    55   return _prev;
       
    56 }
       
    57 
       
    58 void JfrSerializerRegistration::set_prev(JfrSerializerRegistration* prev) {
       
    59   _prev = prev;
       
    60 }
       
    61 
       
    62 JfrTypeId JfrSerializerRegistration::id() const {
       
    63   return _id;
       
    64 }
       
    65 
       
    66 void JfrSerializerRegistration::invoke_serializer(JfrCheckpointWriter& writer) const {
       
    67   if (_cache.valid()) {
       
    68     writer.increment();
       
    69     _cache->write(writer);
       
    70     return;
       
    71   }
       
    72   const JfrCheckpointContext ctx = writer.context();
       
    73   writer.write_type(_id);
       
    74   _serializer->serialize(writer);
       
    75   if (_permit_cache) {
       
    76     _cache = writer.copy(&ctx);
       
    77   }
       
    78 }
       
    79 
       
    80 JfrTypeManager::~JfrTypeManager() {
       
    81   Iterator iter(_types);
       
    82   JfrSerializerRegistration* registration;
       
    83   while (iter.has_next()) {
       
    84     registration = _types.remove(iter.next());
       
    85     assert(registration != NULL, "invariant");
       
    86     delete registration;
       
    87   }
       
    88   Iterator sp_type_iter(_safepoint_types);
       
    89   while (sp_type_iter.has_next()) {
       
    90     registration = _safepoint_types.remove(sp_type_iter.next());
       
    91     assert(registration != NULL, "invariant");
       
    92     delete registration;
       
    93   }
       
    94 }
       
    95 
       
    96 size_t JfrTypeManager::number_of_registered_types() const {
       
    97   size_t count = 0;
       
    98   const Iterator iter(_types);
       
    99   while (iter.has_next()) {
       
   100     ++count;
       
   101     iter.next();
       
   102   }
       
   103   const Iterator sp_type_iter(_safepoint_types);
       
   104   while (sp_type_iter.has_next()) {
       
   105     ++count;
       
   106     sp_type_iter.next();
       
   107   }
       
   108   return count;
       
   109 }
       
   110 
       
   111 void JfrTypeManager::write_types(JfrCheckpointWriter& writer) const {
       
   112   const Iterator iter(_types);
       
   113   while (iter.has_next()) {
       
   114     iter.next()->invoke_serializer(writer);
       
   115   }
       
   116 }
       
   117 
       
   118 void JfrTypeManager::write_safepoint_types(JfrCheckpointWriter& writer) const {
       
   119   assert(SafepointSynchronize::is_at_safepoint(), "invariant");
       
   120   const Iterator iter(_safepoint_types);
       
   121   while (iter.has_next()) {
       
   122     iter.next()->invoke_serializer(writer);
       
   123   }
       
   124 }
       
   125 
       
   126 void JfrTypeManager::write_type_set() const {
       
   127   assert(!SafepointSynchronize::is_at_safepoint(), "invariant");
       
   128   // can safepoint here because of Module_lock
       
   129   MutexLockerEx lock(Module_lock);
       
   130   JfrCheckpointWriter writer(true, true, Thread::current());
       
   131   TypeSet set;
       
   132   set.serialize(writer);
       
   133 }
       
   134 
       
   135 void JfrTypeManager::write_type_set_for_unloaded_classes() const {
       
   136   assert(SafepointSynchronize::is_at_safepoint(), "invariant");
       
   137   JfrCheckpointWriter writer(false, true, Thread::current());
       
   138   ClassUnloadTypeSet class_unload_set;
       
   139   class_unload_set.serialize(writer);
       
   140 }
       
   141 
       
   142 void JfrTypeManager::create_thread_checkpoint(JavaThread* jt) const {
       
   143   assert(jt != NULL, "invariant");
       
   144   JfrThreadConstant type_thread(jt);
       
   145   JfrCheckpointWriter writer(false, true, jt);
       
   146   writer.write_type(TYPE_THREAD);
       
   147   type_thread.serialize(writer);
       
   148   // create and install a checkpoint blob
       
   149   jt->jfr_thread_local()->set_thread_checkpoint(writer.checkpoint_blob());
       
   150   assert(jt->jfr_thread_local()->has_thread_checkpoint(), "invariant");
       
   151 }
       
   152 
       
   153 void JfrTypeManager::write_thread_checkpoint(JavaThread* jt) const {
       
   154   assert(jt != NULL, "JavaThread is NULL!");
       
   155   ResourceMark rm(jt);
       
   156   if (jt->jfr_thread_local()->has_thread_checkpoint()) {
       
   157     JfrCheckpointWriter writer(false, false, jt);
       
   158     jt->jfr_thread_local()->thread_checkpoint()->write(writer);
       
   159   } else {
       
   160     JfrThreadConstant type_thread(jt);
       
   161     JfrCheckpointWriter writer(false, true, jt);
       
   162     writer.write_type(TYPE_THREAD);
       
   163     type_thread.serialize(writer);
       
   164   }
       
   165 }
       
   166 
       
   167 #ifdef ASSERT
       
   168 static void assert_not_registered_twice(JfrTypeId id, JfrTypeManager::List& list) {
       
   169   const JfrTypeManager::Iterator iter(list);
       
   170   while (iter.has_next()) {
       
   171     assert(iter.next()->id() != id, "invariant");
       
   172   }
       
   173 }
       
   174 #endif
       
   175 
       
   176 bool JfrTypeManager::register_serializer(JfrTypeId id, bool require_safepoint, bool permit_cache, JfrSerializer* cs) {
       
   177   assert(cs != NULL, "invariant");
       
   178   JfrSerializerRegistration* const registration = new JfrSerializerRegistration(id, permit_cache, cs);
       
   179   if (registration == NULL) {
       
   180     delete cs;
       
   181     return false;
       
   182   }
       
   183   if (require_safepoint) {
       
   184     assert(!_safepoint_types.in_list(registration), "invariant");
       
   185     DEBUG_ONLY(assert_not_registered_twice(id, _safepoint_types);)
       
   186       _safepoint_types.prepend(registration);
       
   187   }
       
   188   else {
       
   189     assert(!_types.in_list(registration), "invariant");
       
   190     DEBUG_ONLY(assert_not_registered_twice(id, _types);)
       
   191       _types.prepend(registration);
       
   192   }
       
   193   return true;
       
   194 }
       
   195 
       
   196 bool JfrTypeManager::initialize() {
       
   197   // register non-safepointing type serialization
       
   198   for (size_t i = 0; i < 16; ++i) {
       
   199     switch (i) {
       
   200     case 0: register_serializer(TYPE_FLAGVALUEORIGIN, false, true, new FlagValueOriginConstant()); break;
       
   201     case 1: register_serializer(TYPE_INFLATECAUSE, false, true, new MonitorInflateCauseConstant()); break;
       
   202     case 2: register_serializer(TYPE_GCCAUSE, false, true, new GCCauseConstant()); break;
       
   203     case 3: register_serializer(TYPE_GCNAME, false, true, new GCNameConstant()); break;
       
   204     case 4: register_serializer(TYPE_GCWHEN, false, true, new GCWhenConstant()); break;
       
   205     case 5: register_serializer(TYPE_G1HEAPREGIONTYPE, false, true, new G1HeapRegionTypeConstant()); break;
       
   206     case 6: register_serializer(TYPE_GCTHRESHOLDUPDATER, false, true, new GCThresholdUpdaterConstant()); break;
       
   207     case 7: register_serializer(TYPE_METADATATYPE, false, true, new MetadataTypeConstant()); break;
       
   208     case 8: register_serializer(TYPE_METASPACEOBJECTTYPE, false, true, new MetaspaceObjectTypeConstant()); break;
       
   209     case 9: register_serializer(TYPE_G1YCTYPE, false, true, new G1YCTypeConstant()); break;
       
   210     case 10: register_serializer(TYPE_REFERENCETYPE, false, true, new ReferenceTypeConstant()); break;
       
   211     case 11: register_serializer(TYPE_NARROWOOPMODE, false, true, new NarrowOopModeConstant()); break;
       
   212     case 12: register_serializer(TYPE_COMPILERPHASETYPE, false, true, new CompilerPhaseTypeConstant()); break;
       
   213     case 13: register_serializer(TYPE_CODEBLOBTYPE, false, true, new CodeBlobTypeConstant()); break;
       
   214     case 14: register_serializer(TYPE_VMOPERATIONTYPE, false, true, new VMOperationTypeConstant()); break;
       
   215     case 15: register_serializer(TYPE_THREADSTATE, false, true, new ThreadStateConstant()); break;
       
   216     default:
       
   217       guarantee(false, "invariant");
       
   218     }
       
   219   }
       
   220 
       
   221   // register safepointing type serialization
       
   222   for (size_t i = 0; i < 2; ++i) {
       
   223     switch (i) {
       
   224     case 0: register_serializer(TYPE_THREADGROUP, true, false, new JfrThreadGroupConstant()); break;
       
   225     case 1: register_serializer(TYPE_THREAD, true, false, new JfrThreadConstantSet()); break;
       
   226     default:
       
   227       guarantee(false, "invariant");
       
   228     }
       
   229   }
       
   230   return true;
       
   231 }
       
   232 
       
   233