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 #ifndef SHARE_VM_JFR_CHECKPOINT_CONSTANT_TRACEID_JFRTYPEIDS_INLINE_HPP
  26 #define SHARE_VM_JFR_CHECKPOINT_CONSTANT_TRACEID_JFRTYPEIDS_INLINE_HPP
  27 
  28 #include "jfr/recorder/checkpoint/constant/traceid/jfrTraceIdMacros.hpp"
  29 #include "jfr/recorder/checkpoint/constant/traceid/jfrTraceId.hpp"
  30 #include "runtime/thread.hpp"
  31 #include "utilities/debug.hpp"
  32 
  33 template <typename T>
  34 inline traceid set_used_and_get(const T* type, bool leakp) {
  35   assert(type != NULL, "invariant");
  36   if (leakp) {
  37     SET_LEAKP_USED_THIS_EPOCH(type);
  38     assert(LEAKP_USED_THIS_EPOCH(type), "invariant");
  39   }
  40   SET_USED_THIS_EPOCH(type);
  41   assert(USED_THIS_EPOCH(type), "invariant");
  42   return TRACE_ID_MASKED_PTR(type);
  43 }
  44 
  45 template <typename T>
  46 inline traceid set_used_and_get_shifted(const T* type, bool leakp) {
  47   assert(type != NULL, "invariant");
  48   return set_used_and_get(type, leakp) >> TRACE_ID_SHIFT;
  49 }
  50 
  51 inline traceid JfrTraceId::get(const Klass* klass) {
  52   assert(klass != NULL, "invariant");
  53   return TRACE_ID(klass);
  54 }
  55 
  56 inline traceid JfrTraceId::get(const Thread* t) {
  57   assert(t != NULL, "invariant");
  58   return TRACE_ID_RAW(t->trace_data());
  59 }
  60 
  61 inline traceid JfrTraceId::use(const Klass* klass, bool leakp /* false */) {
  62   assert(klass != NULL, "invariant");
  63   return set_used_and_get_shifted(klass, leakp);
  64 }
  65 
  66 inline traceid JfrTraceId::use(const Method* method, bool leakp /* false */) {
  67   assert(method != NULL, "invariant");
  68   SET_METHOD_FLAG_USED_THIS_EPOCH(method);
  69   const Klass* const klass = method->method_holder();
  70   assert(klass != NULL, "invariant");
  71   if (leakp) {
  72     SET_LEAKP_USED_THIS_EPOCH(klass);
  73     assert(LEAKP_USED_THIS_EPOCH(klass), "invariant");
  74   }
  75   SET_METHOD_AND_CLASS_USED_THIS_EPOCH(klass);
  76   assert(METHOD_AND_CLASS_USED_THIS_EPOCH(klass), "invariant");
  77   return (METHOD_ID(klass, method));
  78 }
  79 
  80 inline traceid JfrTraceId::use(const ModuleEntry* module, bool leakp /* false */) {
  81   assert(module != NULL, "invariant");
  82   return set_used_and_get_shifted(module, leakp);
  83 }
  84 
  85 inline traceid JfrTraceId::use(const PackageEntry* package, bool leakp /* false */) {
  86   assert(package != NULL, "invariant");
  87   return set_used_and_get_shifted(package, leakp);
  88 }
  89 
  90 inline traceid JfrTraceId::use(const ClassLoaderData* cld, bool leakp /* false */) {
  91   assert(cld != NULL, "invariant");
  92   return cld->is_anonymous() ? 0 : set_used_and_get_shifted(cld, leakp);
  93 }
  94 
  95 inline bool JfrTraceId::in_visible_set(const Klass* klass) {
  96   assert(klass != NULL, "invariant");
  97   assert(((JavaThread*)Thread::current())->thread_state() == _thread_in_vm, "invariant");
  98   return (IS_JDK_JFR_EVENT_SUBKLASS(klass) && !klass->is_abstract()) || IS_EVENT_HOST_KLASS(klass);
  99 }
 100 
 101 inline bool JfrTraceId::is_jdk_jfr_event(const Klass* k) {
 102   assert(k != NULL, "invariant");
 103   return IS_JDK_JFR_EVENT_KLASS(k);
 104 }
 105 
 106 inline void JfrTraceId::tag_as_jdk_jfr_event(const Klass* klass) {
 107   assert(klass != NULL, "invariant");
 108   assert(IS_NOT_AN_EVENT_KLASS(klass), "invariant");
 109   SET_TAG(klass, JDK_JFR_EVENT_KLASS);
 110   assert(IS_JDK_JFR_EVENT_KLASS(klass), "invariant");
 111   assert(IS_NOT_AN_EVENT_SUB_KLASS(klass), "invariant");
 112 }
 113 
 114 inline bool JfrTraceId::is_jdk_jfr_event_sub(const Klass* k) {
 115   assert(k != NULL, "invariant");
 116   return IS_JDK_JFR_EVENT_SUBKLASS(k);
 117 }
 118 
 119 inline void JfrTraceId::tag_as_jdk_jfr_event_sub(const Klass* k) {
 120   assert(k != NULL, "invariant");
 121   if (IS_NOT_AN_EVENT_KLASS(k)) {
 122     SET_TAG(k, JDK_JFR_EVENT_SUBKLASS);
 123   }
 124   assert(IS_JDK_JFR_EVENT_SUBKLASS(k), "invariant");
 125 }
 126 
 127 inline bool JfrTraceId::in_jdk_jfr_event_hierarchy(const Klass* klass) {
 128   assert(klass != NULL, "invariant");
 129   if (is_jdk_jfr_event(klass)) {
 130     return true;
 131   }
 132   const Klass* const super = klass->super();
 133   return super != NULL ? IS_EVENT_KLASS(super) : false;
 134 }
 135 
 136 inline bool JfrTraceId::is_event_host(const Klass* k) {
 137   assert(k != NULL, "invariant");
 138   return IS_EVENT_HOST_KLASS(k);
 139 }
 140 
 141 inline void JfrTraceId::tag_as_event_host(const Klass* k) {
 142   assert(k != NULL, "invariant");
 143   SET_TAG(k, EVENT_HOST_KLASS);
 144   assert(IS_EVENT_HOST_KLASS(k), "invariant");
 145 }
 146 
 147 #endif // SHARE_VM_JFR_CHECKPOINT_CONSTANT_TRACEID_JFRTYPEIDS_INLINE_HPP