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_VM_JFR_RECORDER_STORAGE_JFRSTORAGEUTILS_HPP
  26 #define SHARE_VM_JFR_RECORDER_STORAGE_JFRSTORAGEUTILS_HPP
  27 
  28 #include "jfr/recorder/storage/jfrBuffer.hpp"
  29 #include "jfr/recorder/repository/jfrChunkWriter.hpp"
  30 #include "jfr/utilities/jfrAllocation.hpp"
  31 #include "jfr/utilities/jfrTypes.hpp"
  32 #include "runtime/thread.hpp"
  33 
  34 template <typename Operation, typename NextOperation>
  35 class CompositeOperation {
  36  private:
  37   Operation* _op;
  38   NextOperation* _next;
  39  public:
  40   CompositeOperation(Operation* op, NextOperation* next) : _op(op), _next(next) {
  41     assert(_op != NULL, "invariant");
  42   }
  43   typedef typename Operation::Type Type;
  44   bool process(Type* t = NULL) {
  45     return _next == NULL ? _op->process(t) : _op->process(t) && _next->process(t);
  46   }
  47   size_t processed() const {
  48     return _next == NULL ? _op->processed() : _op->processed() + _next->processed();
  49   }
  50 };
  51 
  52 template <typename T>
  53 class UnBufferedWriteToChunk {
  54  private:
  55   JfrChunkWriter& _writer;
  56   size_t _processed;
  57  public:
  58   typedef T Type;
  59   UnBufferedWriteToChunk(JfrChunkWriter& writer) : _writer(writer), _processed(0) {}
  60   bool write(Type* t, const u1* data, size_t size);
  61   size_t processed() { return _processed; }
  62 };
  63 
  64 template <typename T>
  65 class DefaultDiscarder {
  66  private:
  67   size_t _processed;
  68  public:
  69   typedef T Type;
  70   DefaultDiscarder() : _processed() {}
  71   bool discard(Type* t, const u1* data, size_t size);
  72   size_t processed() const { return _processed; }
  73 };
  74 
  75 template <typename Operation>
  76 class ConcurrentWriteOp {
  77  private:
  78   Operation& _operation;
  79  public:
  80   typedef typename Operation::Type Type;
  81   ConcurrentWriteOp(Operation& operation) : _operation(operation) {}
  82   bool process(Type* t);
  83   size_t processed() const { return _operation.processed(); }
  84 };
  85 
  86 template <typename Operation>
  87 class MutexedWriteOp {
  88  private:
  89   Operation& _operation;
  90  public:
  91   typedef typename Operation::Type Type;
  92   MutexedWriteOp(Operation& operation) : _operation(operation) {}
  93   bool process(Type* t);
  94   size_t processed() const { return _operation.processed(); }
  95 };
  96 
  97 enum jfr_operation_mode {
  98   mutexed = 1,
  99   concurrent
 100 };
 101 
 102 template <typename Operation>
 103 class DiscardOp {
 104  private:
 105   Operation _operation;
 106   jfr_operation_mode _mode;
 107  public:
 108   typedef typename Operation::Type Type;
 109   DiscardOp(jfr_operation_mode mode = concurrent) : _operation(), _mode(mode) {}
 110   bool process(Type* t);
 111   size_t processed() const { return _operation.processed(); }
 112 };
 113 
 114 #endif // SHARE_VM_JFR_RECORDER_STORAGE_JFRSTORAGEUTILS_HPP