1 /*
   2  * Copyright (c) 2016, 2019, 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 #ifndef SHARE_JFR_RECORDER_CHECKPOINT_TYPES_TRACEID_JFRTRACEID_INLINE_HPP
  26 #define SHARE_JFR_RECORDER_CHECKPOINT_TYPES_TRACEID_JFRTRACEID_INLINE_HPP
  27 
  28 #include "classfile/classLoaderData.hpp"
  29 #include "classfile/moduleEntry.hpp"
  30 #include "classfile/packageEntry.hpp"
  31 #include "jfr/recorder/checkpoint/types/traceid/jfrTraceId.hpp"
  32 #include "jfr/recorder/checkpoint/types/traceid/jfrTraceIdBits.inline.hpp"
  33 #include "jfr/recorder/checkpoint/types/traceid/jfrTraceIdEpoch.hpp"
  34 #include "jfr/support/jfrKlassExtension.hpp"
  35 #include "oops/arrayKlass.hpp"
  36 #include "oops/klass.hpp"
  37 #include "oops/instanceKlass.hpp"
  38 #include "oops/method.hpp"
  39 #include "runtime/thread.inline.hpp"
  40 #include "utilities/debug.hpp"
  41 
  42 template <typename T>
  43 inline traceid set_used_and_get(const T* type) {
  44   assert(type != NULL, "invariant");
  45   if (SHOULD_TAG(type)) {
  46     SET_USED_THIS_EPOCH(type);
  47     JfrTraceIdEpoch::set_changed_tag_state();
  48   }
  49   assert(USED_THIS_EPOCH(type), "invariant");
  50   return TRACE_ID(type);
  51 }
  52 
  53 inline traceid JfrTraceId::get(const Klass* klass) {
  54   assert(klass != NULL, "invariant");
  55   return TRACE_ID(klass);
  56 }
  57 
  58 inline traceid JfrTraceId::get(const Thread* t) {
  59   assert(t != NULL, "invariant");
  60   return TRACE_ID_RAW(t->jfr_thread_local());
  61 }
  62 
  63 inline traceid JfrTraceId::use(const Klass* klass) {
  64   return set_used_and_get(klass);
  65 }
  66 
  67 inline traceid JfrTraceId::use(const Method* method) {
  68   return use(method->method_holder(), method);
  69 }
  70 
  71 inline traceid JfrTraceId::use(const Klass* klass, const Method* method) {
  72   assert(klass != NULL, "invariant");
  73   assert(method != NULL, "invariant");
  74   if (SHOULD_TAG_KLASS_METHOD(klass)) {
  75     SET_METHOD_AND_CLASS_USED_THIS_EPOCH(klass);
  76   }
  77   assert(METHOD_AND_CLASS_USED_THIS_EPOCH(klass), "invariant");
  78   if (METHOD_FLAG_NOT_USED_THIS_EPOCH(method)) {
  79     assert(USED_THIS_EPOCH(klass), "invariant");
  80     SET_METHOD_FLAG_USED_THIS_EPOCH(method);
  81     JfrTraceIdEpoch::set_changed_tag_state();
  82   }
  83   assert(METHOD_FLAG_USED_THIS_EPOCH(method), "invariant");
  84   return (METHOD_ID(klass, method));
  85 }
  86 
  87 inline traceid JfrTraceId::use(const ModuleEntry* module) {
  88   return set_used_and_get(module);
  89 }
  90 
  91 inline traceid JfrTraceId::use(const PackageEntry* package) {
  92   return set_used_and_get(package);
  93 }
  94 
  95 inline traceid JfrTraceId::use(const ClassLoaderData* cld) {
  96   assert(cld != NULL, "invariant");
  97   return cld->is_unsafe_anonymous() ? 0 : set_used_and_get(cld);
  98 }
  99 
 100 inline void JfrTraceId::set_leakp(const Method* method) {
 101   assert(method != NULL, "invariant");
 102   const Klass* const klass = method->method_holder();
 103   assert(klass != NULL, "invariant");
 104   assert(METHOD_AND_CLASS_USED_THIS_EPOCH(klass), "invariant");
 105   assert(METHOD_FLAG_USED_THIS_EPOCH(method), "invariant");
 106   SET_LEAKP(klass);
 107   SET_METHOD_LEAKP(method);
 108 }
 109 
 110 inline bool JfrTraceId::in_visible_set(const Klass* klass) {
 111   assert(klass != NULL, "invariant");
 112   assert(((JavaThread*)Thread::current())->thread_state() == _thread_in_vm, "invariant");
 113   return (IS_JDK_JFR_EVENT_SUBKLASS(klass) && !klass->is_abstract()) || IS_EVENT_HOST_KLASS(klass);
 114 }
 115 
 116 inline bool JfrTraceId::is_jdk_jfr_event(const Klass* k) {
 117   assert(k != NULL, "invariant");
 118   return IS_JDK_JFR_EVENT_KLASS(k);
 119 }
 120 
 121 inline void JfrTraceId::tag_as_jdk_jfr_event(const Klass* klass) {
 122   assert(klass != NULL, "invariant");
 123   SET_JDK_JFR_EVENT_KLASS(klass);
 124   assert(IS_JDK_JFR_EVENT_KLASS(klass), "invariant");
 125 }
 126 
 127 inline bool JfrTraceId::is_jdk_jfr_event_sub(const Klass* k) {
 128   assert(k != NULL, "invariant");
 129   return IS_JDK_JFR_EVENT_SUBKLASS(k);
 130 }
 131 
 132 inline void JfrTraceId::tag_as_jdk_jfr_event_sub(const Klass* k) {
 133   assert(k != NULL, "invariant");
 134   if (IS_NOT_AN_EVENT_SUB_KLASS(k)) {
 135     SET_JDK_JFR_EVENT_SUBKLASS(k);
 136   }
 137   assert(IS_JDK_JFR_EVENT_SUBKLASS(k), "invariant");
 138 }
 139 
 140 inline bool JfrTraceId::in_jdk_jfr_event_hierarchy(const Klass* klass) {
 141   assert(klass != NULL, "invariant");
 142   if (is_jdk_jfr_event(klass)) {
 143     return true;
 144   }
 145   const Klass* const super = klass->super();
 146   return super != NULL ? IS_EVENT_KLASS(super) : false;
 147 }
 148 
 149 inline bool JfrTraceId::is_event_host(const Klass* k) {
 150   assert(k != NULL, "invariant");
 151   return IS_EVENT_HOST_KLASS(k);
 152 }
 153 
 154 inline void JfrTraceId::tag_as_event_host(const Klass* k) {
 155   assert(k != NULL, "invariant");
 156   SET_EVENT_HOST_KLASS(k);
 157   assert(IS_EVENT_HOST_KLASS(k), "invariant");
 158 }
 159 
 160 #endif // SHARE_JFR_RECORDER_CHECKPOINT_TYPES_TRACEID_JFRTRACEID_INLINE_HPP