-
Notifications
You must be signed in to change notification settings - Fork 29
/
mmap.lisp
266 lines (237 loc) · 9.1 KB
/
mmap.lisp
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
(in-package :graph-db)
#+lispworks(deftype word () '(unsigned-byte 64))
(cffi:defctype size :unsigned-int)
(deftype uint32 () '(integer 0 4294967295))
(deftype uint40 () '(integer 0 1099511627775))
(deftype uint64 () '(integer 0 18446744073709551615))
(defstruct (mapped-file
(:conc-name m-)
(:predicate mapped-file-p))
path pointer fd
;;(remap-lock (make-rw-lock))
)
(defstruct mpointer mmap loc)
(defmethod mapped-file-length ((mapped-file mapped-file))
(osicat-posix:stat-size (osicat-posix:fstat (m-fd mapped-file))))
(defmethod set-byte :around (mf offset byte)
(handler-case
(call-next-method)
#+sbcl
(sb-kernel::memory-fault-error (c)
(log:error "SEGV: GOT ~A in ~A; retrying." c mf)
(set-byte mf offset byte))
#+ccl
(CCL::INVALID-MEMORY-ACCESS (c)
(log:error "SEGV: GOT ~A in ~A; retrying." c mf)
(set-byte mf offset byte))
))
(defmethod set-byte ((mapped-file mapped-file) offset byte)
(declare (type word offset))
(declare (type (integer 0 255) byte))
;;(log:debug "SET-BYTE: ~A ADDR ~A TO ~A" (m-path mapped-file) offset byte)
(setf (cffi:mem-aref (m-pointer mapped-file) :unsigned-char offset) byte))
(defmethod get-byte :around (mf offset)
(handler-case
(call-next-method)
#+sbcl
(sb-kernel::memory-fault-error (c)
(log:error "SEGV: GOT ~A in ~A; retrying." c mf)
(get-byte mf offset))
#+ccl
(CCL::INVALID-MEMORY-ACCESS (c)
(log:error "SEGV: GOT ~A in ~A; retrying." c mf)
(get-byte mf offset))))
(defmethod get-byte ((mapped-file mapped-file) offset)
(declare (type word offset))
(cffi:mem-aref (m-pointer mapped-file) :unsigned-char offset))
(defmethod get-bytes :around (mf offset length)
(handler-case
(call-next-method)
#+sbcl
(sb-kernel::memory-fault-error (c)
(log:error "SEGV: GOT ~A in ~A; retrying." c mf)
(get-bytes mf offset length))
#+ccl
(CCL::INVALID-MEMORY-ACCESS (c)
(log:error "SEGV: GOT ~A in ~A; retrying." c mf)
(get-bytes mf offset length))))
(defmethod get-bytes ((mapped-file mapped-file) offset length)
(declare (type word offset length))
(let ((vec (make-byte-vector length)))
(dotimes (i length)
(setf (aref vec i) (get-byte mapped-file (+ i offset))))
vec))
(defmethod set-bytes :around (mf vec offset length)
(handler-case
(call-next-method)
#+sbcl
(sb-kernel::memory-fault-error (c)
(log:error "SEGV: GOT ~A in ~A; retrying." c mf)
(set-bytes mf offset length))
#+ccl
(CCL::INVALID-MEMORY-ACCESS (c)
(log:error "SEGV: GOT ~A in ~A; retrying." c mf)
(set-bytes mf offset length))))
(defmethod set-bytes ((mapped-file mapped-file) vec offset length)
(declare (type word offset length))
(dotimes (i length)
(set-byte mapped-file (+ i offset) (aref vec i)))
vec)
(defmethod size-of ((mmap mapped-file))
(osicat-posix:stat-size (osicat-posix:stat (m-path mmap))))
(defun mmap-file (file &key (create-p t) (size (* 4096 25600)))
"Use mmap() to map FILE into memory."
(log:debug "Opening mmap ~A" file)
(when (and (not create-p) (not (probe-file file)))
(error "mmap-file: ~A does not exist and create-p is not true." file))
(let* ((fd (osicat-posix:open
file
(if create-p
(logior osicat-posix:O-CREAT osicat-posix:O-RDWR)
osicat-posix:O-RDWR))))
(when create-p
(osicat-posix:lseek fd (1- size) osicat-posix:seek-set)
(cffi:with-foreign-string (null (format nil "~A" (code-char 0)))
(cffi:foreign-funcall "write"
:int fd
:pointer null
size 1)))
;; Make sure the file size is set right!
(setq size (osicat-posix:stat-size (osicat-posix:fstat fd)))
(let* ((pointer
(osicat-posix:mmap
(cffi:null-pointer)
size
(logior osicat-posix:prot-read osicat-posix:prot-write)
osicat-posix:map-shared
fd
0)))
; (cffi:foreign-funcall "madvise"
; :int fd
; size size
; :int MADV_RANDOM))) ;; NEED MADV_RANDOM FROM ?
(make-mapped-file :path (truename file)
:fd fd
:pointer pointer))))
(defmethod sync-region ((mapped-file mapped-file) &key addr length
(sync osicat-posix:ms-sync))
(osicat-posix:msync (or addr (m-pointer mapped-file))
(or length (mapped-file-length mapped-file))
sync))
(defmethod munmap-file ((mapped-file mapped-file) &key (save-p nil)
(sync osicat-posix:ms-sync))
(when save-p
;;(log:debug "Calling msync on ~S" mapped-file)
(osicat-posix:msync (m-pointer mapped-file)
(mapped-file-length mapped-file)
sync))
;;(log:debug "Calling munmap on ~S" mapped-file)
(osicat-posix:munmap (m-pointer mapped-file) (mapped-file-length mapped-file))
(osicat-posix:close (m-fd mapped-file))
(setf (m-pointer mapped-file) nil)
nil)
#+linux
(defmethod extend-mapped-file ((mapped-file mapped-file) (length integer))
(log:debug "EXTENDING MMAP ~A" mapped-file)
(let ((ptr (osicat-posix:mremap (m-pointer mapped-file)
(mapped-file-length mapped-file)
(+ length (mapped-file-length mapped-file))
osicat-posix:MREMAP-MAYMOVE)))
(setf (m-pointer mapped-file) ptr)
(osicat-posix:lseek (m-fd mapped-file)
(1- (+ length (mapped-file-length mapped-file)))
osicat-posix:seek-set)
(cffi:with-foreign-string (null (format nil "~A" (code-char 0)))
(cffi:foreign-funcall "write"
:int (m-fd mapped-file)
:pointer null
size 1))
mapped-file))
#+darwin
(defmethod extend-mapped-file ((mapped-file mapped-file) (length integer))
(log:debug "EXTENDING MMAP ~A" mapped-file)
(let ((len (mapped-file-length mapped-file)))
(munmap-file mapped-file)
(setf (m-pointer mapped-file)
(osicat-posix:mmap (cffi:null-pointer) (+ len length)
(logior osicat-posix:PROT-READ osicat-posix:PROT-WRITE)
osicat-posix:MAP-SHARED (m-fd mapped-file) 0))
(osicat-posix:lseek (m-fd mapped-file)
(1- (+ length (mapped-file-length mapped-file)))
osicat-posix:seek-set)
(cffi:with-foreign-string (null (format nil "~A" (code-char 0)))
(cffi:foreign-funcall "write"
:int (m-fd mapped-file)
:pointer null
size 1))
mapped-file))
(defmethod serialize-uint64 ((mf mapped-file) int offset)
(declare (type word int offset))
;;(log:debug "MMAP: SERIALIZING UINT64 ~A TO ADDR ~A" int offset)
(dotimes (i 8)
;;(log:debug "WRITING BYTE ~X" (ldb (byte 8 0) int))
(set-byte mf offset (ldb (byte 8 (* i 8)) int))
;;(log:debug " WROTE BYTE ~A AT OFFSET ~X" (ldb (byte 8 0) int) offset)
(incf offset))
;; (incf offset))
offset)
(defmethod deserialize-uint64 ((mf mapped-file) offset)
"Decode a UINT64."
(declare (type word offset))
(let ((int 0))
(declare (type word int))
(dotimes (i 8)
(setq int (dpb (get-byte mf (+ i offset)) (byte 8 (* i 8)) int)))
int))
(defmethod deserialize-uint64 ((array array) offset)
"Decode a UINT64."
(let ((int 0))
(declare (type word int))
(dotimes (i 8)
(setq int (dpb (aref array (+ i offset)) (byte 8 (* i 8)) int)))
int))
(defmethod incf-uint64 ((mf mapped-file) offset)
(declare (type word offset))
(let ((int (deserialize-uint64 mf offset)))
(incf int)
(serialize-uint64 mf int offset)
int))
(defmethod decf-uint64 ((mf mapped-file) offset)
(declare (type word offset))
(let ((int (deserialize-uint64 mf offset)))
(serialize-uint64 mf (decf int) offset)
int))
(defmethod serialize-pointer ((mf mapped-file) pointer offset)
;;(log:debug "SERIALIZING POINTER ~A TO ADDR ~A" pointer offset)
(serialize-uint64 mf pointer offset))
(defmethod deserialize-pointer ((mf mapped-file) offset)
;;(log:debug "DESERIALIZING POINTER AT ADDR ~A" offset)
(deserialize-uint64 mf offset))
(defmethod serialize-uint32 ((mf mapped-file) int offset)
(declare (type uint32 int))
(declare (type word offset))
(dotimes (i 4)
(set-byte mf offset (ldb (byte 8 (* i 8)) int))
(incf offset))
(incf offset))
(defmethod deserialize-uint32 ((mf mapped-file) offset)
(declare (type word offset))
(let ((int 0))
(declare (type uint32 int))
(dotimes (i 4)
(setq int (dpb (get-byte mf (+ i offset)) (byte 8 (* i 8)) int)))
int))
(defmethod serialize-uint40 ((mf mapped-file) int offset)
(declare (type uint40 int))
(declare (type word offset))
(dotimes (i 5)
(set-byte mf offset (ldb (byte 8 (* i 8)) int))
(incf offset))
(incf offset))
(defmethod deserialize-uint40 ((mf mapped-file) offset)
(declare (type word offset))
(let ((int 0))
(declare (type uint40 int))
(dotimes (i 5)
(setq int (dpb (get-byte mf (+ i offset)) (byte 8 (* i 8)) int)))
int))