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_JFRTRACEIDEPOCH_HPP
  26 #define SHARE_JFR_RECORDER_CHECKPOINT_TYPES_TRACEID_JFRTRACEIDEPOCH_HPP
  27 
  28 #include "jfr/utilities/jfrTypes.hpp"
  29 #include "memory/allocation.hpp"
  30 #include "runtime/atomic.hpp"
  31 
  32 #define USED_BIT                             1
  33 #define METHOD_USED_BIT                      (USED_BIT << 2)
  34 #define EPOCH_1_SHIFT                        0
  35 #define EPOCH_2_SHIFT                        1
  36 #define USED_EPOCH_1_BIT                     (USED_BIT << EPOCH_1_SHIFT)
  37 #define USED_EPOCH_2_BIT                     (USED_BIT << EPOCH_2_SHIFT)
  38 #define METHOD_USED_EPOCH_1_BIT              (METHOD_USED_BIT << EPOCH_1_SHIFT)
  39 #define METHOD_USED_EPOCH_2_BIT              (METHOD_USED_BIT << EPOCH_2_SHIFT)
  40 #define METHOD_AND_CLASS_IN_USE_BITS         (METHOD_USED_BIT | USED_BIT)
  41 #define METHOD_AND_CLASS_IN_USE_EPOCH_1_BITS (METHOD_AND_CLASS_IN_USE_BITS << EPOCH_1_SHIFT)
  42 #define METHOD_AND_CLASS_IN_USE_EPOCH_2_BITS (METHOD_AND_CLASS_IN_USE_BITS << EPOCH_2_SHIFT)
  43 
  44 class JfrTraceIdEpoch : AllStatic {
  45   friend class JfrCheckpointManager;
  46  private:
  47   static bool _epoch_state;
  48   static bool volatile _tag_state;
  49 
  50   static void shift_epoch();
  51 
  52  public:
  53   static bool epoch() {
  54     return _epoch_state;
  55   }
  56 
  57   static jlong epoch_address() {
  58     return (jlong)&_epoch_state;
  59   }
  60 
  61   static u1 current() {
  62     return _epoch_state ? (u1)1 : (u1)0;
  63   }
  64 
  65   static u1 previous() {
  66     return _epoch_state ? (u1)0 : (u1)1;
  67   }
  68 
  69   static traceid in_use_this_epoch_bit() {
  70     return _epoch_state ? USED_EPOCH_2_BIT : USED_EPOCH_1_BIT;
  71   }
  72 
  73   static traceid in_use_prev_epoch_bit() {
  74     return _epoch_state ? USED_EPOCH_1_BIT : USED_EPOCH_2_BIT;
  75   }
  76 
  77   static traceid method_in_use_this_epoch_bit() {
  78     return _epoch_state ? METHOD_USED_EPOCH_2_BIT : METHOD_USED_EPOCH_1_BIT;
  79   }
  80 
  81   static traceid method_in_use_prev_epoch_bit() {
  82     return _epoch_state ? METHOD_USED_EPOCH_1_BIT : METHOD_USED_EPOCH_2_BIT;
  83   }
  84 
  85   static traceid method_and_class_in_use_this_epoch_bits() {
  86     return _epoch_state ? METHOD_AND_CLASS_IN_USE_EPOCH_2_BITS : METHOD_AND_CLASS_IN_USE_EPOCH_1_BITS;
  87   }
  88 
  89   static traceid method_and_class_in_use_prev_epoch_bits() {
  90     return _epoch_state ? METHOD_AND_CLASS_IN_USE_EPOCH_1_BITS :  METHOD_AND_CLASS_IN_USE_EPOCH_2_BITS;
  91   }
  92 
  93   static bool has_changed_tag_state() {
  94     if (Atomic::load_acquire(&_tag_state)) {
  95       Atomic::release_store(&_tag_state, false);
  96       return true;
  97     }
  98     return false;
  99   }
 100 
 101   static void set_changed_tag_state() {
 102     if (!Atomic::load_acquire(&_tag_state)) {
 103       Atomic::release_store(&_tag_state, true);
 104     }
 105   }
 106 };
 107 
 108 #endif // SHARE_JFR_RECORDER_CHECKPOINT_TYPES_TRACEID_JFRTRACEIDEPOCH_HPP