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 #ifndef SHARE_VM_EVTRACE_TRACEMANAGER_HPP
  26 #define SHARE_VM_EVTRACE_TRACEMANAGER_HPP
  27 
  28 #include "runtime/handles.hpp"
  29 #include "utilities/exceptions.hpp"
  30 
  31 class TraceBuffer;
  32 class TraceBufferQueue;
  33 class TraceReaderThread;
  34 class TraceMetadata;
  35 class Monitor;
  36 
  37 class TraceStatistics {
  38 public:
  39   virtual ~TraceStatistics() {}
  40 
  41   virtual void add_entry(const char* name, jdouble value) = 0;
  42 };
  43 
  44 class TraceManager {
  45 private:
  46   static void assert_initialized();
  47 
  48   static volatile bool      _is_initialized;
  49   static TraceReaderThread *_thread;
  50   static volatile bool      _thread_running;
  51   static Monitor           *_thread_mtx;
  52   static TraceBufferQueue  *_free_queue;
  53   static TraceBufferQueue  *_flush_queue;
  54   static TraceMetadata     *_metadata;
  55   static bool               _classes_unloaded_in_current_safepoint;
  56   DEBUG_ONLY(static int     _current_safepoint_counter);
  57 
  58   // statistics
  59   static volatile intptr_t _buffer_count, _max_buffer_count, _allocated_buffer_count;
  60   static volatile intptr_t _submitted_trace_bytes;
  61   static intptr_t          _reclaimed_trace_bytes;
  62   static volatile intptr_t _reclaim_time_nanos;
  63   static intptr_t          _stored_free_dequeue_ops, _stored_free_enqueue_ops;
  64   static intptr_t          _stored_flush_dequeue_ops, _stored_flush_enqueue_ops;
  65   static volatile intptr_t _total_stack_traces, _truncated_stack_traces, _reused_memento_stack_traces, _total_stack_frames, _reused_memento_stack_frames;
  66 
  67   static Handle create_queue_object(TraceBufferQueue *q, instanceKlassHandle klass, TRAPS);
  68 
  69   static jlong do_reclaim_buffers_in_safepoint(bool deinit);
  70   static void pre_submit_buffer(TraceBuffer *buffer);
  71 
  72   static TraceBuffer *allocate_buffer();
  73 
  74 public:
  75   class VM_ReclaimTraceBuffers;
  76   class VM_ResetTraceMetadata;
  77   class ReclaimTraceBuffersClosure;
  78 
  79   static void initialize();
  80   static bool is_initialized();
  81   static void start_threads(TRAPS);
  82   static void reclaim_buffers_in_safepoint(bool wait_until_processed);
  83   static void finish_and_destroy(TRAPS);
  84 
  85   static TraceBuffer * request_buffer();
  86   static void submit_buffer(TraceBuffer *buffer);
  87   static void free_buffer(TraceBuffer *buffer);
  88 
  89   static TraceMetadata * metadata();
  90   static void reset_metadata();
  91 
  92   static void do_work_before_safepoint_end();
  93 
  94   static void nmethod_is_unloading(const nmethod *nm);
  95   static void class_loader_is_unloading(ClassLoaderData *loader);
  96 
  97   static void thread_is_exiting(TraceReaderThread *t);
  98 
  99   // statistics
 100   static void write_stats(TraceStatistics *stats);
 101   static void reset_stats();
 102   static void update_stack_trace_stats(bool truncated, intptr_t total_frames, intptr_t memento_frames);
 103 };
 104 
 105 #include "evtrace/traceManager.inline.hpp"
 106 
 107 #endif /* SHARE_VM_EVTRACE_TRACEMANAGER_HPP */