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_STORAGE_JFRSTORAGEUTILS_HPP
26#define SHARE_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
34template <typename Operation, typename NextOperation>
35class 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
52template <typename T>
53class 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
64template <typename T>
65class 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
75template <typename Operation>
76class 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
86template <typename Operation>
87class ConcurrentWriteOpExcludeRetired : private ConcurrentWriteOp<Operation> {
88 public:
89 typedef typename Operation::Type Type;
90 ConcurrentWriteOpExcludeRetired(Operation& operation) : ConcurrentWriteOp<Operation>(operation) {}
91 bool process(Type* t);
92 size_t processed() const { return ConcurrentWriteOp<Operation>::processed(); }
93};
94
95template <typename Operation>
96class MutexedWriteOp {
97 private:
98 Operation& _operation;
99 public:
100 typedef typename Operation::Type Type;
101 MutexedWriteOp(Operation& operation) : _operation(operation) {}
102 bool process(Type* t);
103 size_t processed() const { return _operation.processed(); }
104};
105
106template <typename Operation>
107class ExclusiveOp : private MutexedWriteOp<Operation> {
108 public:
109 typedef typename Operation::Type Type;
110 ExclusiveOp(Operation& operation) : MutexedWriteOp<Operation>(operation) {}
111 bool process(Type* t);
112 size_t processed() const { return MutexedWriteOp<Operation>::processed(); }
113};
114
115enum jfr_operation_mode {
116 mutexed = 1,
117 concurrent
118};
119
120template <typename Operation>
121class DiscardOp {
122 private:
123 Operation _operation;
124 jfr_operation_mode _mode;
125 public:
126 typedef typename Operation::Type Type;
127 DiscardOp(jfr_operation_mode mode = concurrent) : _operation(), _mode(mode) {}
128 bool process(Type* t);
129 size_t processed() const { return _operation.processed(); }
130};
131
132#endif // SHARE_JFR_RECORDER_STORAGE_JFRSTORAGEUTILS_HPP
133