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
|
//! A mmapping `BufferedReader` implementation for files.
//!
//! On my (Justus) system, this implementation improves the
//! performance of the statistics example by ~10% over the
//! Generic.
use libc::{c_void, size_t, mmap, munmap, PROT_READ, MAP_PRIVATE};
use std::fmt;
use std::fs;
use std::io;
use std::os::unix::io::AsRawFd;
use std::slice;
use std::path::Path;
use std::ptr;
use super::*;
// For small files, the overhead of manipulating the page table is not
// worth the gain. This threshold has been chosen so that on my
// (Justus) system, mmaping is faster than sequentially reading.
const MMAP_THRESHOLD: u64 = 16 * 4096;
/// Wraps files using `mmap`().
///
/// This implementation tries to mmap the file, falling back to
/// just using a generic reader.
pub struct File<'a, C>(Imp<'a, C>);
impl<'a, C> fmt::Display for File<'a, C> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.0)
}
}
impl<'a, C> fmt::Debug for File<'a, C> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_tuple("File")
.field(&self.0)
.finish()
}
}
/// The implementation.
enum Imp<'a, C> {
Generic(Generic<fs::File, C>),
MMAP {
addr: *mut c_void,
length: size_t,
reader: Memory<'a, C>,
}
}
impl<'a, C> Drop for Imp<'a, C> {
fn drop(&mut self) {
match self {
Imp::Generic(_) => (),
Imp::MMAP { addr, length, .. } =>
unsafe {
munmap(*addr, *length);
},
}
}
}
impl<'a, C> fmt::Display for Imp<'a, C> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "File(")?;
match self {
Imp::Generic(_) => write!(f, "Generic")?,
Imp::MMAP { .. } => write!(f, "Memory")?,
};
write!(f, ")")
}
}
impl<'a, C> fmt::Debug for Imp<'a, C> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Imp::Generic(ref g) =>
f.debug_tuple("Generic")
.field(&g)
.finish(),
Imp::MMAP { ref addr, ref length, ref reader } =>
f.debug_struct("MMAP")
.field("addr", addr)
.field("length", length)
.field("reader", reader)
.finish(),
}
}
}
impl<'a> File<'a, ()> {
/// Opens the given file.
pub fn open<P: AsRef<Path>>(path: P) -> io::Result<Self> {
Self::with_cookie(path, ())
}
}
impl<'a, C> File<'a, C> {
/// Like `open()`, but sets a cookie.
pub fn with_cookie<P: AsRef<Path>>(path: P, cookie: C) -> io::Result<Self> {
// As fallback, we use a generic reader.
let generic = |file, cookie| {
Ok(File(
Imp::Generic(
Generic::with_cookie(file, None, cookie))))
};
let file = fs::File::open(path)?;
// For testing and benchmarking purposes, we use the variable
// SEQUOIA_DONT_MMAP to turn off mmapping.
if ::std::env::var_os("SEQUOIA_DONT_MMAP").is_some() {
return generic(file, cookie);
}
let length = file.metadata()?.len();
// For small files, the overhead of manipulating the page
// table is not worth the gain.
if length < MMAP_THRESHOLD {
return generic(file, cookie);
}
// Be nice to 32 bit systems.
if length > usize::max_value() as u64 {
return generic(file, cookie);
}
let length = length as usize;
let fd = file.as_raw_fd();
let addr = unsafe {
mmap(ptr::null_mut(), length, PROT_READ, MAP_PRIVATE,
fd, 0)
};
if addr.is_null() {
return generic(file, cookie);
}
let slice = unsafe {
slice::from_raw_parts(addr as *const u8, length)
};
Ok(File(
Imp::MMAP {
addr: addr,
length: length,
reader: Memory::with_cookie(slice, cookie),
}
))
}
}
impl<'a, C> io::Read for File<'a, C> {
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
match self.0 {
Imp::Generic(ref mut reader) => reader.read(buf),
Imp::MMAP { ref mut reader, .. } => reader.read(buf),
}
}
}
impl<'a, C> BufferedReader<C> for File<'a, C> {
fn buffer(&self) -> &[u8] {
match self.0 {
Imp::Generic(ref reader) => reader.buffer(),
Imp::MMAP { ref reader, .. } => reader.buffer(),
}
}
fn data(&mut self, amount: usize) -> io::Result<&[u8]> {
match self.0 {
Imp::Generic(ref mut reader) => reader.data(amount),
Imp::MMAP { ref mut reader, .. } => reader.data(amount),
}
}
fn data_hard(&mut self, amount: usize) -> io::Result<&[u8]> {
match self.0 {
Imp::Generic(ref mut reader) => reader.data_hard(amount),
Imp::MMAP
|