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