1 /*
   2  * Copyright (c) 2014, 2015, Dynatrace and/or its affiliates. All rights reserved.
   3  *
   4  * This file is part of the Lock Contention Tracing Subsystem for the HotSpot
   5  * Virtual Machine, which is developed at Christian Doppler Laboratory on
   6  * Monitoring and Evolution of Very-Large-Scale Software Systems. Please
   7  * contact us at <http://mevss.jku.at/> if you need additional information
   8  * or have any questions.
   9  *
  10  * This code is free software; you can redistribute it and/or modify it
  11  * under the terms of the GNU General Public License version 2 only, as
  12  * published by the Free Software Foundation.
  13  *
  14  * This code is distributed in the hope that it will be useful, but WITHOUT
  15  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  16  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  17  * version 2 for more details (a copy is included in the LICENSE file that
  18  * accompanied this code).
  19  *
  20  * You should have received a copy of the GNU General Public License version
  21  * 2 along with this work. If not, see <http://www.gnu.org/licenses/>.
  22  *
  23  */
  24 
  25 #include "evtrace/traceMetadata.hpp"
  26 
  27 TraceMetadata::TraceMetadata() {
  28   _stack_cache = new TraceStackCache(this);
  29   _last_stack_id = 0;
  30   _last_global_seq = 0;
  31 }
  32 
  33 TraceMetadata::~TraceMetadata() {
  34   delete _stack_cache;
  35 }
  36 
  37 class TraceMetadata::MarkKlassUnknownClosure: public KlassClosure {
  38   static void mark_unknown(Method *m) {
  39     m->reset_tracing_known();
  40   }
  41 
  42 public:
  43   virtual void do_klass(Klass *k) {
  44     k->reset_tracing_known();
  45     if (k->oop_is_instance()) {
  46       InstanceKlass::cast(k)->methods_do(mark_unknown);
  47     }
  48   }
  49 };
  50 
  51 class TraceMetadata::ClearInvalidatedMementoStacksClosure: public ThreadClosure {
  52   virtual void do_thread(Thread *t) {
  53     assert(t != NULL, "null thread");
  54     if (t->memento_stack_trace() != NULL && !t->memento_stack_trace()->is_valid()) {
  55       t->set_memento_stack_trace(NULL);
  56     }
  57   }
  58 };
  59 
  60 void TraceMetadata::purge_all() {
  61   assert(SafepointSynchronize::is_at_safepoint() && Thread::current()->is_VM_thread(), "must be done in VM thread at safepoint");
  62 
  63   MarkKlassUnknownClosure mark_unknown;
  64   ClassLoaderDataGraph::classes_do(&mark_unknown);
  65 
  66   _stack_cache->purge_all();
  67 }
  68 
  69 void TraceMetadata::do_maintenance() {
  70   assert(SafepointSynchronize::is_at_safepoint() && Thread::current()->is_VM_thread(), "must be done in VM thread at safepoint");
  71 
  72   if (_stack_cache->has_invalid_stacks()) {
  73     ClearInvalidatedMementoStacksClosure clear_invalid;
  74     Threads::threads_do(&clear_invalid);
  75   }
  76 
  77   _stack_cache->do_maintenance();
  78   assert(!_stack_cache->has_invalid_stacks(), "sanity");
  79 }