-
Notifications
You must be signed in to change notification settings - Fork 160
/
memory.clj
230 lines (185 loc) · 7.18 KB
/
memory.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
(ns crux.memory
(:require [clojure.java.io :as io]
[clojure.string :as str]
[clojure.tools.logging :as log]
[crux.io :as cio])
(:import [java.io DataOutputStream]
java.nio.ByteBuffer
java.util.Comparator
java.util.function.Supplier
[org.agrona DirectBuffer ExpandableDirectByteBuffer MutableDirectBuffer]
org.agrona.concurrent.UnsafeBuffer
org.agrona.io.ExpandableDirectBufferOutputStream
crux.ByteUtils))
(defprotocol MemoryRegion
(->on-heap ^bytes [this])
(->off-heap
^org.agrona.MutableDirectBuffer [this]
^org.agrona.MutableDirectBuffer [this ^MutableDirectBuffer to])
(off-heap? [this])
(as-buffer ^org.agrona.MutableDirectBuffer [this])
(^long capacity [this]))
(def ^:private ^:const default-chunk-size (* 128 1024))
(def ^:private ^:const large-buffer-size (quot default-chunk-size 4))
(defonce ^:private pool-allocation-stats (atom {:allocated 0
:deallocated 0}))
(defn- log-pool-memory [{:keys [allocated deallocated] :as pool-allocation-stats}]
(log/debug :pool-allocation-stats (assoc pool-allocation-stats :in-use (- (long allocated) (long deallocated)))))
(defn- allocate-pooled-buffer [^long size]
(let [chunk (ByteBuffer/allocateDirect size)]
(assert (= size (.capacity chunk)))
(log-pool-memory (swap! pool-allocation-stats update :allocated + (.capacity chunk)))
(cio/register-cleaner chunk #(log-pool-memory (swap! pool-allocation-stats update :deallocated + size)))
chunk))
(def ^:private ^ThreadLocal chunk-tl
(ThreadLocal/withInitial
(reify Supplier
(get [_]
(allocate-pooled-buffer default-chunk-size)))))
(defn allocate-unpooled-buffer ^org.agrona.MutableDirectBuffer [^long size]
(UnsafeBuffer. (ByteBuffer/allocateDirect size) 0 size))
(def ^:private ^:const alignment-round-mask 0xf)
(defn allocate-buffer ^org.agrona.MutableDirectBuffer [^long size]
(let [chunk ^ByteBuffer (.get chunk-tl)
offset (.position chunk)]
(cond
(> size large-buffer-size)
(allocate-unpooled-buffer size)
(> size (.remaining chunk))
(let [chunk (allocate-pooled-buffer default-chunk-size)]
(.set chunk-tl chunk)
(recur size))
:else
;; TODO: This limit is safer if the byte buffer itself is used,
;; but slower.
(let [buffer (.limit (.slice chunk) size)
new-aligned-offset (bit-and (+ offset size alignment-round-mask)
(bit-not alignment-round-mask))]
(.position chunk new-aligned-offset)
(UnsafeBuffer. ^ByteBuffer buffer 0 size)))))
(defn copy-buffer
(^org.agrona.MutableDirectBuffer [^DirectBuffer from]
(copy-buffer from (capacity from)))
(^org.agrona.MutableDirectBuffer [^DirectBuffer from ^long limit]
(copy-buffer from limit (allocate-buffer limit)))
(^org.agrona.MutableDirectBuffer [^DirectBuffer from ^long limit ^MutableDirectBuffer to]
(doto to
(.putBytes 0 from 0 limit))))
(defn copy-to-unpooled-buffer ^org.agrona.MutableDirectBuffer [^DirectBuffer from]
(copy-buffer from (capacity from) (allocate-unpooled-buffer (capacity from))))
(defn slice-buffer ^org.agrona.MutableDirectBuffer [^DirectBuffer buffer ^long offset ^long limit]
(UnsafeBuffer. buffer offset limit))
(defn limit-buffer ^org.agrona.MutableDirectBuffer [^DirectBuffer buffer ^long limit]
(slice-buffer buffer 0 limit))
(defn with-buffer-out
(^org.agrona.DirectBuffer [b f]
(with-buffer-out b f true))
(^org.agrona.DirectBuffer [b f copy?]
(with-buffer-out b f copy? 0))
(^org.agrona.DirectBuffer [b f copy? ^long offset]
(let [b-out (ExpandableDirectBufferOutputStream. (or b (ExpandableDirectByteBuffer.)) offset)]
(with-open [out (DataOutputStream. b-out)]
(f out))
(cond-> (UnsafeBuffer. (.buffer b-out) 0 (+ (.position b-out) offset))
copy? (copy-buffer)))))
(extend-protocol MemoryRegion
(class (byte-array 0))
(->on-heap [this]
this)
(->off-heap
([this]
(let [b (allocate-buffer (alength ^bytes this))]
(->off-heap this b)))
([this ^MutableDirectBuffer to]
(doto to
(.putBytes 0 ^bytes this))))
(off-heap? [this]
false)
(as-buffer [this]
(UnsafeBuffer. ^bytes this))
(capacity [this]
(alength ^bytes this))
DirectBuffer
(->on-heap [this]
(if (and (.byteArray this)
(= (.capacity this)
(alength (.byteArray this))))
(.byteArray this)
(let [bytes (byte-array (.capacity this))]
(.getBytes this 0 bytes)
bytes)))
(->off-heap
([this]
(if (off-heap? this)
this
(->off-heap this (allocate-buffer (.capacity this)))))
([this ^MutableDirectBuffer to]
(doto to
(.putBytes 0 this 0 (.capacity this)))))
(off-heap? [this]
(or (some-> (.byteBuffer this) (.isDirect))
(and (nil? (.byteArray this))
(nil? (.byteBuffer this)))))
(as-buffer [this]
this)
(capacity [this]
(.capacity this))
ByteBuffer
(->on-heap [this]
(if (and (.hasArray this)
(= (.remaining this)
(alength (.array this))))
(.array this)
(doto (byte-array (.remaining this))
(->> (.get this)))))
(->off-heap
([this]
(if (.isDirect this)
(as-buffer this)
(->off-heap this (allocate-buffer (.remaining this)))))
([this ^MutableDirectBuffer to]
(doto to
(.putBytes 0 this (.position this) (.remaining this)))))
(off-heap? [this]
(.isDirect this))
(as-buffer [this]
(UnsafeBuffer. this (.position this) (.remaining this)))
(capacity [this]
(.remaining this)))
(defn ensure-off-heap ^org.agrona.DirectBuffer [b ^MutableDirectBuffer tmp]
(if (off-heap? b)
b
(UnsafeBuffer. (->off-heap b tmp) 0 (capacity b))))
(defn on-heap-buffer ^org.agrona.DirectBuffer [^bytes b]
(UnsafeBuffer. b))
(defn buffer->hex ^String [^DirectBuffer b]
(some-> b (ByteUtils/bufferToHex)))
(defn hex->buffer
(^org.agrona.DirectBuffer [^String b]
(hex->buffer b (ExpandableDirectByteBuffer.)))
(^org.agrona.DirectBuffer [^String b ^MutableDirectBuffer to]
(some-> b (ByteUtils/hexToBuffer to))))
(defn compare-buffers
(^long [^DirectBuffer a ^DirectBuffer b]
(ByteUtils/compareBuffers a b))
(^long [^DirectBuffer a ^DirectBuffer b ^long max-length]
(ByteUtils/compareBuffers a b max-length)))
(def ^java.util.Comparator buffer-comparator
ByteUtils/UNSIGNED_BUFFER_COMPARATOR)
(defn buffers=?
([^DirectBuffer a ^DirectBuffer b]
(ByteUtils/equalBuffers a b))
([^DirectBuffer a ^DirectBuffer b ^long max-length]
(ByteUtils/equalBuffers a b max-length)))
(defn inc-unsigned-buffer!
(^org.agrona.MutableDirectBuffer [^MutableDirectBuffer buffer]
(inc-unsigned-buffer! buffer (.capacity buffer)))
(^org.agrona.MutableDirectBuffer [^MutableDirectBuffer buffer ^long prefix-length]
(loop [idx (dec (int prefix-length))]
(when-not (neg? idx)
(let [b (Byte/toUnsignedInt (.getByte buffer idx))]
(if (= 0xff b)
(do (.putByte buffer idx (byte 0))
(recur (dec idx)))
(doto buffer
(.putByte idx (unchecked-byte (inc b))))))))))