util/env_posix.cc

filter: Statementstmt 67.6% 209 / 309 Branchbran 34.6% 86 / 248 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 52.9% 295 / 557 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« table/table.cc ↑ Summary table/two_level_iterator.cc »
1 // Copyright (c) 2011 The LevelDB Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. See the AUTHORS file for names of contributors.
4  
5 #include <deque>
6 #include <dirent.h>
7 #include <errno.h>
8 #include <fcntl.h>
9 #include <pthread.h>
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
13 #include <sys/mman.h>
14 #include <sys/stat.h>
15 #include <sys/time.h>
16 #include <sys/types.h>
17 #include <time.h>
18 #include <unistd.h>
19 #if defined(LEVELDB_PLATFORM_ANDROID)
20 #include <sys/stat.h>
21 #endif
22 #include "leveldb/env.h"
23 #include "leveldb/slice.h"
24 #include "port/port.h"
25 #include "util/logging.h"
26 #include "util/posix_logger.h"
27  
28 namespace leveldb {
29  
30 namespace {
31  
32 9 static Status IOError(const std::string& context, int err_number) {
33 9 ▶ return Status::IOError(context, strerror(err_number));
Branch: 1 branch
BranchTrueFalseTotal
return Status::IOError(context, strerror(err_number)); 9 0 9
34 }
35  
36 class PosixSequentialFile: public SequentialFile {
37 private:
38 std::string filename_;
39 FILE* file_;
40  
41 public:
42 7 PosixSequentialFile(const std::string& fname, FILE* f)
43 7 ▶ : filename_(fname), file_(f) { }
Branch: 1 branch
BranchTrueFalseTotal
: filename_(fname), file_(f) { } 7 0 7
44 14 virtual ~PosixSequentialFile() { fclose(file_); }
45  
46 10 virtual Status Read(size_t n, Slice* result, char* scratch) {
47 10 Status s;
48 10 ▶ size_t r = fread_unlocked(scratch, 1, n, file_);
Branch: 1 branch
BranchTrueFalseTotal
size_t r = fread_unlocked(scratch, 1, n, file_); 10 0 10
49 10 *result = Slice(scratch, r);
50 10 ▶ if (r < n) {
Branch: 1 branch
BranchTrueFalseTotal
if (r < n) { 10 0 10
51 10 ▶ if (feof(file_)) {
Branch: 1 branch
BranchTrueFalseTotal
if (feof(file_)) { 0 10 10
52 // We leave status as ok if we hit the end of the file
53 } else {
54 // A partial read with an error: return a non-ok status
55 0 ▶ s = IOError(filename_, errno);
Branch: 2 branches
BranchTrueFalseTotal
s = IOError(filename_, errno); 0 0 0
s = IOError(filename_, errno); 0 0 0
56 }
57 }
58 10 return s;
59 }
60  
61 0 virtual Status Skip(uint64_t n) {
62 0 ▶ if (fseek(file_, n, SEEK_CUR)) {
Branch: 1 branch
BranchTrueFalseTotal
if (fseek(file_, n, SEEK_CUR)) { 0 0 0
63 0 return IOError(filename_, errno);
64 }
65 0 return Status::OK();
66 }
67 };
68  
69 class PosixRandomAccessFile: public RandomAccessFile {
70 private:
71 std::string filename_;
72 int fd_;
73  
74 public:
75 1 PosixRandomAccessFile(const std::string& fname, int fd)
76 1 ▶ : filename_(fname), fd_(fd) { }
Branch: 1 branch
BranchTrueFalseTotal
: filename_(fname), fd_(fd) { } 1 0 1
77 2 virtual ~PosixRandomAccessFile() { close(fd_); }
78  
79 3 virtual Status Read(uint64_t offset, size_t n, Slice* result,
80 char* scratch) const {
81 3 Status s;
82 3 ▶ ssize_t r = pread(fd_, scratch, n, static_cast<off_t>(offset));
Branch: 1 branch
BranchTrueFalseTotal
ssize_t r = pread(fd_, scratch, n, static_cast<off_t>(offset)); 3 0 3
83 3 *result = Slice(scratch, (r < 0) ? 0 : r);
84 3 ▶ if (r < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (r < 0) { 0 3 3
85 // An error: return a non-ok status
86 0 ▶ s = IOError(filename_, errno);
Branch: 2 branches
BranchTrueFalseTotal
s = IOError(filename_, errno); 0 0 0
s = IOError(filename_, errno); 0 0 0
87 }
88 3 return s;
89 }
90 };
91  
92 // We preallocate up to an extra megabyte and use memcpy to append new
93 // data to the file. This is safe since we either properly close the
94 // file before reading from it, or for log files, the reading code
95 // knows enough to skip zero suffixes.
96 class PosixMmapFile : public WritableFile {
97 private:
98 std::string filename_;
99 int fd_;
100 size_t page_size_;
101 size_t map_size_; // How much extra memory to map at a time
102 char* base_; // The mapped region
103 char* limit_; // Limit of the mapped region
104 char* dst_; // Where to write next (in range [base_,limit_])
105 char* last_sync_; // Where have we synced up to
106 uint64_t file_offset_; // Offset of base_ in file
107  
108 // Have we done an munmap of unsynced data?
109 bool pending_sync_;
110  
111 // Roundup x to a multiple of y
112 14 static size_t Roundup(size_t x, size_t y) {
113 14 return ((x + y - 1) / y) * y;
114 }
115  
116 8 size_t TruncateToPageBoundary(size_t s) {
117 8 s -= (s & (page_size_ - 1));
118 8 ▶ assert((s % page_size_) == 0);
Branch: 1 branch
BranchTrueFalseTotal
assert((s % page_size_) == 0); 0 8 8
119 8 return s;
120 }
121  
122 28 bool UnmapCurrentRegion() {
123 28 bool result = true;
124 28 ▶ if (base_ != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (base_ != NULL) { 14 14 28
125 14 ▶ if (last_sync_ < limit_) {
Branch: 1 branch
BranchTrueFalseTotal
if (last_sync_ < limit_) { 14 0 14
126 // Defer syncing this data until next Sync() call, if any
127 14 pending_sync_ = true;
128 }
129 14 ▶ if (munmap(base_, limit_ - base_) != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (munmap(base_, limit_ - base_) != 0) { 0 14 14
130 0 result = false;
131 }
132 14 file_offset_ += limit_ - base_;
133 14 base_ = NULL;
134 14 limit_ = NULL;
135 14 last_sync_ = NULL;
136 14 dst_ = NULL;
137  
138 // Increase the amount we map the next time, but capped at 1MB
139 14 ▶ if (map_size_ < (1<<20)) {
Branch: 1 branch
BranchTrueFalseTotal
if (map_size_ < (1<<20)) { 14 0 14
140 14 map_size_ *= 2;
141 }
142 }
143 28 return result;
144 }
145  
146 14 bool MapNewRegion() {
147 14 ▶ assert(base_ == NULL);
Branch: 1 branch
BranchTrueFalseTotal
assert(base_ == NULL); 0 14 14
148 14 ▶ if (ftruncate(fd_, file_offset_ + map_size_) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (ftruncate(fd_, file_offset_ + map_size_) < 0) { 0 14 14
149 0 return false;
150 }
151 14 void* ptr = mmap(NULL, map_size_, PROT_READ | PROT_WRITE, MAP_SHARED,
152 14 fd_, file_offset_);
153 14 ▶ if (ptr == MAP_FAILED) {
Branch: 1 branch
BranchTrueFalseTotal
if (ptr == MAP_FAILED) { 0 14 14
154 0 return false;
155 }
156 14 base_ = reinterpret_cast<char*>(ptr);
157 14 limit_ = base_ + map_size_;
158 14 dst_ = base_;
159 14 last_sync_ = base_;
160 14 return true;
161 }
162  
163 public:
164 14 PosixMmapFile(const std::string& fname, int fd, size_t page_size)
165 28 : filename_(fname),
166 14 fd_(fd),
167 14 page_size_(page_size),
168 14 map_size_(Roundup(65536, page_size)),
169 14 base_(NULL),
170 14 limit_(NULL),
171 14 dst_(NULL),
172 14 last_sync_(NULL),
173 14 file_offset_(0),
174 14 ▶ pending_sync_(false) {
Branch: 1 branch
BranchTrueFalseTotal
pending_sync_(false) { 14 0 14
175 14 ▶ assert((page_size & (page_size - 1)) == 0);
Branch: 1 branch
BranchTrueFalseTotal
assert((page_size & (page_size - 1)) == 0); 0 14 14
176 14 }
177  
178  
179 28 ~PosixMmapFile() {
180 14 ▶ if (fd_ >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (fd_ >= 0) { 6 8 14
181 6 PosixMmapFile::Close();
182 }
183 28 }
184  
185 64 virtual Status Append(const Slice& data) {
186 64 const char* src = data.data();
187 64 size_t left = data.size();
188 142 ▶ while (left > 0) {
Branch: 1 branch
BranchTrueFalseTotal
while (left > 0) { 78 64 142
189 78 ▶ assert(base_ <= dst_);
Branch: 1 branch
BranchTrueFalseTotal
assert(base_ <= dst_); 0 78 78
190 78 ▶ assert(dst_ <= limit_);
Branch: 1 branch
BranchTrueFalseTotal
assert(dst_ <= limit_); 0 78 78
191 78 size_t avail = limit_ - dst_;
192 78 ▶ if (avail == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (avail == 0) { 14 64 78
193 28 ▶ if (!UnmapCurrentRegion() ||
Branch: 2 branches
BranchTrueFalseTotal
if (!UnmapCurrentRegion() || 14 0 14
if (!UnmapCurrentRegion() || 0 14 14
194 14 ▶ !MapNewRegion()) {
Branch: 1 branch
BranchTrueFalseTotal
!MapNewRegion()) { 0 14 14
195 0 return IOError(filename_, errno);
196 }
197 }
198  
199 78 ▶ size_t n = (left <= avail) ? left : avail;
Branch: 1 branch
BranchTrueFalseTotal
size_t n = (left <= avail) ? left : avail; 64 14 78
200 78 memcpy(dst_, src, n);
201 78 dst_ += n;
202 78 src += n;
203 78 left -= n;
204 }
205 64 return Status::OK();
206 }
207  
208 14 virtual Status Close() {
209 14 Status s;
210 14 size_t unused = limit_ - dst_;
211 14 ▶ if (!UnmapCurrentRegion()) {
Branch: 1 branch
BranchTrueFalseTotal
if (!UnmapCurrentRegion()) { 0 14 14
212 0 ▶ s = IOError(filename_, errno);
Branch: 2 branches
BranchTrueFalseTotal
s = IOError(filename_, errno); 0 0 0
s = IOError(filename_, errno); 0 0 0
213 14 ▶ } else if (unused > 0) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (unused > 0) { 14 0 14
214 // Trim the extra space at the end of the file
215 14 ▶ if (ftruncate(fd_, file_offset_ - unused) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (ftruncate(fd_, file_offset_ - unused) < 0) { 0 14 14
216 0 ▶ s = IOError(filename_, errno);
Branch: 2 branches
BranchTrueFalseTotal
s = IOError(filename_, errno); 0 0 0
s = IOError(filename_, errno); 0 0 0
217 }
218 }
219  
220 14 ▶ if (close(fd_) < 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (close(fd_) < 0) { 14 0 14
if (close(fd_) < 0) { 0 14 14
221 0 ▶ if (s.ok()) {
Branch: 1 branch
BranchTrueFalseTotal
if (s.ok()) { 0 0 0
222 0 ▶ s = IOError(filename_, errno);
Branch: 2 branches
BranchTrueFalseTotal
s = IOError(filename_, errno); 0 0 0
s = IOError(filename_, errno); 0 0 0
223 }
224 }
225  
226 14 fd_ = -1;
227 14 base_ = NULL;
228 14 limit_ = NULL;
229 14 return s;
230 }
231  
232 27 virtual Status Flush() {
233 27 return Status::OK();
234 }
235  
236 4 virtual Status Sync() {
237 4 Status s;
238  
239 4 ▶ if (pending_sync_) {
Branch: 1 branch
BranchTrueFalseTotal
if (pending_sync_) { 0 4 4
240 // Some unmapped data was not synced
241 0 pending_sync_ = false;
242 0 ▶ if (fdatasync(fd_) < 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (fdatasync(fd_) < 0) { 0 0 0
if (fdatasync(fd_) < 0) { 0 0 0
243 0 ▶ s = IOError(filename_, errno);
Branch: 2 branches
BranchTrueFalseTotal
s = IOError(filename_, errno); 0 0 0
s = IOError(filename_, errno); 0 0 0
244 }
245 }
246  
247 4 ▶ if (dst_ > last_sync_) {
Branch: 1 branch
BranchTrueFalseTotal
if (dst_ > last_sync_) { 4 0 4
248 // Find the beginnings of the pages that contain the first and last
249 // bytes to be synced.
250 4 size_t p1 = TruncateToPageBoundary(last_sync_ - base_);
251 4 size_t p2 = TruncateToPageBoundary(dst_ - base_ - 1);
252 4 last_sync_ = dst_;
253 4 ▶ if (msync(base_ + p1, p2 - p1 + page_size_, MS_SYNC) < 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (msync(base_ + p1, p2 - p1 + page_size_, MS_SYNC) < 0) { 4 0 4
if (msync(base_ + p1, p2 - p1 + page_size_, MS_SYNC) < 0) { 0 4 4
254 0 ▶ s = IOError(filename_, errno);
Branch: 2 branches
BranchTrueFalseTotal
s = IOError(filename_, errno); 0 0 0
s = IOError(filename_, errno); 0 0 0
255 }
256 }
257  
258 4 return s;
259 }
260 };
261  
262 7 static int LockOrUnlock(int fd, bool lock) {
263 7 errno = 0;
264 struct flock f;
265 7 memset(&f, 0, sizeof(f));
266 7 ▶ f.l_type = (lock ? F_WRLCK : F_UNLCK);
Branch: 1 branch
BranchTrueFalseTotal
f.l_type = (lock ? F_WRLCK : F_UNLCK); 4 3 7
267 7 f.l_whence = SEEK_SET;
268 7 f.l_start = 0;
269 7 f.l_len = 0; // Lock/unlock entire file
270 14 ▶ return fcntl(fd, F_SETLK, &f);
Branch: 1 branch
BranchTrueFalseTotal
return fcntl(fd, F_SETLK, &f); 7 0 7
271 }
272  
273 class PosixFileLock : public FileLock {
274 public:
275 int fd_;
276 };
277  
278 class PosixEnv : public Env {
279 public:
280 PosixEnv();
281 0 virtual ~PosixEnv() {
282 0 fprintf(stderr, "Destroying Env::Default()\n");
283 0 exit(1);
284 0 }
285  
286 7 virtual Status NewSequentialFile(const std::string& fname,
287 SequentialFile** result) {
288 7 FILE* f = fopen(fname.c_str(), "r");
289 7 ▶ if (f == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (f == NULL) { 0 7 7
290 0 *result = NULL;
291 0 return IOError(fname, errno);
292 } else {
293 7 ▶ *result = new PosixSequentialFile(fname, f);
Branch: 2 branches
BranchTrueFalseTotal
*result = new PosixSequentialFile(fname, f); 7 0 7
*result = new PosixSequentialFile(fname, f); 0 0 0
294 7 return Status::OK();
295 }
296 }
297  
298 1 virtual Status NewRandomAccessFile(const std::string& fname,
299 RandomAccessFile** result) {
300 1 int fd = open(fname.c_str(), O_RDONLY);
301 1 ▶ if (fd < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (fd < 0) { 0 1 1
302 0 *result = NULL;
303 0 return IOError(fname, errno);
304 }
305 1 ▶ *result = new PosixRandomAccessFile(fname, fd);
Branch: 2 branches
BranchTrueFalseTotal
*result = new PosixRandomAccessFile(fname, fd); 1 0 1
*result = new PosixRandomAccessFile(fname, fd); 0 0 0
306 1 return Status::OK();
307 }
308  
309 14 virtual Status NewWritableFile(const std::string& fname,
310 WritableFile** result) {
311 14 Status s;
312 14 ▶ const int fd = open(fname.c_str(), O_CREAT | O_RDWR | O_TRUNC, 0644);
Branch: 1 branch
BranchTrueFalseTotal
const int fd = open(fname.c_str(), O_CREAT | O_RDWR | O_TRUNC, 0644); 14 0 14
313 14 ▶ if (fd < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (fd < 0) { 0 14 14
314 0 *result = NULL;
315 0 ▶ s = IOError(fname, errno);
Branch: 2 branches
BranchTrueFalseTotal
s = IOError(fname, errno); 0 0 0
s = IOError(fname, errno); 0 0 0
316 } else {
317 14 ▶ *result = new PosixMmapFile(fname, fd, page_size_);
Branch: 3 branches
BranchTrueFalseTotal
*result = new PosixMmapFile(fname, fd, page_size_); 14 0 14
*result = new PosixMmapFile(fname, fd, page_size_); 14 0 14
*result = new PosixMmapFile(fname, fd, page_size_); 0 0 0
318 }
319 14 return s;
320 }
321  
322 3 virtual bool FileExists(const std::string& fname) {
323 3 return access(fname.c_str(), F_OK) == 0;
324 }
325  
326 6 virtual Status GetChildren(const std::string& dir,
327 std::vector<std::string>* result) {
328 6 result->clear();
329 6 DIR* d = opendir(dir.c_str());
330 6 ▶ if (d == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (d == NULL) { 0 6 6
331 0 return IOError(dir, errno);
332 }
333 struct dirent* entry;
334 53 ▶ while ((entry = readdir(d)) != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
while ((entry = readdir(d)) != NULL) { 47 6 53
335 141 ▶ result->push_back(entry->d_name);
Branch: 2 branches
BranchTrueFalseTotal
result->push_back(entry->d_name); 47 0 47
result->push_back(entry->d_name); 47 0 47
336 }
337 6 closedir(d);
338 6 return Status::OK();
339 }
340  
341 4 virtual Status DeleteFile(const std::string& fname) {
342 4 Status result;
343 4 ▶ if (unlink(fname.c_str()) != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (unlink(fname.c_str()) != 0) { 0 4 4
344 0 ▶ result = IOError(fname, errno);
Branch: 2 branches
BranchTrueFalseTotal
result = IOError(fname, errno); 0 0 0
result = IOError(fname, errno); 0 0 0
345 }
346 4 return result;
347 };
348  
349 8 virtual Status CreateDir(const std::string& name) {
350 8 Status result;
351 8 ▶ if (mkdir(name.c_str(), 0755) != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (mkdir(name.c_str(), 0755) != 0) { 6 2 8
352 6 ▶ result = IOError(name, errno);
Branch: 2 branches
BranchTrueFalseTotal
result = IOError(name, errno); 6 0 6
result = IOError(name, errno); 6 0 6
353 }
354 8 return result;
355 };
356  
357 0 virtual Status DeleteDir(const std::string& name) {
358 0 Status result;
359 0 ▶ if (rmdir(name.c_str()) != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (rmdir(name.c_str()) != 0) { 0 0 0
360 0 ▶ result = IOError(name, errno);
Branch: 2 branches
BranchTrueFalseTotal
result = IOError(name, errno); 0 0 0
result = IOError(name, errno); 0 0 0
361 }
362 0 return result;
363 };
364  
365 0 virtual Status GetFileSize(const std::string& fname, uint64_t* size) {
366 0 Status s;
367 struct stat sbuf;
368 0 ▶ if (stat(fname.c_str(), &sbuf) != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (stat(fname.c_str(), &sbuf) != 0) { 0 0 0
369 0 *size = 0;
370 0 ▶ s = IOError(fname, errno);
Branch: 2 branches
BranchTrueFalseTotal
s = IOError(fname, errno); 0 0 0
s = IOError(fname, errno); 0 0 0
371 } else {
372 0 *size = sbuf.st_size;
373 }
374 0 return s;
375 }
376  
377 9 virtual Status RenameFile(const std::string& src, const std::string& target) {
378 9 Status result;
379 9 ▶ if (rename(src.c_str(), target.c_str()) != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (rename(src.c_str(), target.c_str()) != 0) { 2 7 9
380 2 ▶ result = IOError(src, errno);
Branch: 2 branches
BranchTrueFalseTotal
result = IOError(src, errno); 2 0 2
result = IOError(src, errno); 2 0 2
381 }
382 9 return result;
383 }
384  
385 4 virtual Status LockFile(const std::string& fname, FileLock** lock) {
386 4 *lock = NULL;
387 4 Status result;
388 4 ▶ int fd = open(fname.c_str(), O_RDWR | O_CREAT, 0644);
Branch: 1 branch
BranchTrueFalseTotal
int fd = open(fname.c_str(), O_RDWR | O_CREAT, 0644); 4 0 4
389 4 ▶ if (fd < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (fd < 0) { 0 4 4
390 0 ▶ result = IOError(fname, errno);
Branch: 2 branches
BranchTrueFalseTotal
result = IOError(fname, errno); 0 0 0
result = IOError(fname, errno); 0 0 0
391 4 ▶ } else if (LockOrUnlock(fd, true) == -1) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (LockOrUnlock(fd, true) == -1) { 4 0 4
} else if (LockOrUnlock(fd, true) == -1) { 1 3 4
392 1 ▶ result = IOError("lock " + fname, errno);
Branch: 3 branches
BranchTrueFalseTotal
result = IOError("lock " + fname, errno); 1 0 1
result = IOError("lock " + fname, errno); 1 0 1
result = IOError("lock " + fname, errno); 1 0 1
393 1 ▶ close(fd);
Branch: 1 branch
BranchTrueFalseTotal
close(fd); 1 0 1
394 } else {
395 3 ▶ PosixFileLock* my_lock = new PosixFileLock;
Branch: 1 branch
BranchTrueFalseTotal
PosixFileLock* my_lock = new PosixFileLock; 3 0 3
396 3 my_lock->fd_ = fd;
397 3 *lock = my_lock;
398 }
399 4 return result;
400 }
401  
402 3 virtual Status UnlockFile(FileLock* lock) {
403 3 PosixFileLock* my_lock = reinterpret_cast<PosixFileLock*>(lock);
404 3 Status result;
405 3 ▶ if (LockOrUnlock(my_lock->fd_, false) == -1) {
Branch: 2 branches
BranchTrueFalseTotal
if (LockOrUnlock(my_lock->fd_, false) == -1) { 3 0 3
if (LockOrUnlock(my_lock->fd_, false) == -1) { 0 3 3
406 0 ▶ result = IOError("unlock", errno);
Branch: 3 branches
BranchTrueFalseTotal
result = IOError("unlock", errno); 0 0 0
result = IOError("unlock", errno); 0 0 0
result = IOError("unlock", errno); 0 0 0
407 }
408 3 ▶ close(my_lock->fd_);
Branch: 1 branch
BranchTrueFalseTotal
close(my_lock->fd_); 3 0 3
409 3 ▶ delete my_lock;
Branch: 1 branch
BranchTrueFalseTotal
delete my_lock; 3 0 3
410 3 return result;
411 }
412  
413 virtual void Schedule(void (*function)(void*), void* arg);
414  
415 virtual void StartThread(void (*function)(void* arg), void* arg);
416  
417 0 virtual Status GetTestDirectory(std::string* result) {
418 0 const char* env = getenv("TEST_TMPDIR");
419 0 ▶ if (env && env[0] != '\0') {
Branch: 2 branches
BranchTrueFalseTotal
if (env && env[0] != '\0') { 0 0 0
if (env && env[0] != '\0') { 0 0 0
420 0 *result = env;
421 } else {
422 char buf[100];
423 0 snprintf(buf, sizeof(buf), "/tmp/leveldbtest-%d", int(geteuid()));
424 0 ▶ *result = buf;
Branch: 1 branch
BranchTrueFalseTotal
*result = buf; 0 0 0
425 }
426 // Directory may already exist
427 0 ▶ CreateDir(*result);
Branch: 1 branch
BranchTrueFalseTotal
CreateDir(*result); 0 0 0
428 0 return Status::OK();
429 }
430  
431 7 static uint64_t gettid() {
432 7 pthread_t tid = pthread_self();
433 7 uint64_t thread_id = 0;
434 7 memcpy(&thread_id, &tid, std::min(sizeof(thread_id), sizeof(tid)));
435 7 return thread_id;
436 }
437  
438 4 virtual Status NewLogger(const std::string& fname, Logger** result) {
439 4 FILE* f = fopen(fname.c_str(), "w");
440 4 ▶ if (f == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (f == NULL) { 0 4 4
441 0 *result = NULL;
442 0 return IOError(fname, errno);
443 } else {
444 4 *result = new PosixLogger(f, &PosixEnv::gettid);
445 4 return Status::OK();
446 }
447 }
448  
449 2 virtual uint64_t NowMicros() {
450 struct timeval tv;
451 2 gettimeofday(&tv, NULL);
452 2 return static_cast<uint64_t>(tv.tv_sec) * 1000000 + tv.tv_usec;
453 }
454  
455 0 virtual void SleepForMicroseconds(int micros) {
456 0 usleep(micros);
457 0 }
458  
459 private:
460 4 void PthreadCall(const char* label, int result) {
461 4 ▶ if (result != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (result != 0) { 0 4 4
462 0 fprintf(stderr, "pthread %s: %s\n", label, strerror(result));
463 0 exit(1);
464 }
465 4 }
466  
467 // BGThread() is the body of the background thread
468 void BGThread();
469 0 static void* BGThreadWrapper(void* arg) {
470 0 reinterpret_cast<PosixEnv*>(arg)->BGThread();
471 0 return NULL;
472 }
473  
474 size_t page_size_;
475 pthread_mutex_t mu_;
476 pthread_cond_t bgsignal_;
477 pthread_t bgthread_;
478 bool started_bgthread_;
479  
480 // Entry per Schedule() call
481 struct BGItem { void* arg; void (*function)(void*); };
482 typedef std::deque<BGItem> BGQueue;
483 BGQueue queue_;
484 };
485  
486 4 PosixEnv::PosixEnv() : page_size_(getpagesize()),
487 2 ▶ started_bgthread_(false) {
Branch: 1 branch
BranchTrueFalseTotal
started_bgthread_(false) { 2 0 2
488 2 ▶ PthreadCall("mutex_init", pthread_mutex_init(&mu_, NULL));
Branch: 1 branch
BranchTrueFalseTotal
PthreadCall("mutex_init", pthread_mutex_init(&mu_, NULL)); 2 0 2
489 2 ▶ PthreadCall("cvar_init", pthread_cond_init(&bgsignal_, NULL));
Branch: 1 branch
BranchTrueFalseTotal
PthreadCall("cvar_init", pthread_cond_init(&bgsignal_, NULL)); 2 0 2
490 2 }
491  
492 0 void PosixEnv::Schedule(void (*function)(void*), void* arg) {
493 0 PthreadCall("lock", pthread_mutex_lock(&mu_));
494  
495 // Start background thread if necessary
496 0 ▶ if (!started_bgthread_) {
Branch: 1 branch
BranchTrueFalseTotal
if (!started_bgthread_) { 0 0 0
497 0 started_bgthread_ = true;
498 0 PthreadCall(
499 "create thread",
500 pthread_create(&bgthread_, NULL, &PosixEnv::BGThreadWrapper, this));
501 }
502  
503 // If the queue is currently empty, the background thread may currently be
504 // waiting.
505 0 ▶ if (queue_.empty()) {
Branch: 1 branch
BranchTrueFalseTotal
if (queue_.empty()) { 0 0 0
506 0 PthreadCall("signal", pthread_cond_signal(&bgsignal_));
507 }
508  
509 // Add to priority queue
510 0 ▶ queue_.push_back(BGItem());
Branch: 1 branch
BranchTrueFalseTotal
queue_.push_back(BGItem()); 0 0 0
511 0 queue_.back().function = function;
512 0 queue_.back().arg = arg;
513  
514 0 PthreadCall("unlock", pthread_mutex_unlock(&mu_));
515 0 }
516  
517 0 void PosixEnv::BGThread() {
518 while (true) {
519 // Wait until there is an item that is ready to run
520 0 PthreadCall("lock", pthread_mutex_lock(&mu_));
521 0 ▶ while (queue_.empty()) {
Branch: 1 branch
BranchTrueFalseTotal
while (queue_.empty()) { 0 0 0
522 0 PthreadCall("wait", pthread_cond_wait(&bgsignal_, &mu_));
523 }
524  
525 0 void (*function)(void*) = queue_.front().function;
526 0 void* arg = queue_.front().arg;
527 0 queue_.pop_front();
528  
529 0 PthreadCall("unlock", pthread_mutex_unlock(&mu_));
530 0 (*function)(arg);
531 0 }
532 }
533  
534 namespace {
535 struct StartThreadState {
536 void (*user_function)(void*);
537 void* arg;
538 };
539 }
540 0 static void* StartThreadWrapper(void* arg) {
541 0 StartThreadState* state = reinterpret_cast<StartThreadState*>(arg);
542 0 state->user_function(state->arg);
543 0 ▶ delete state;
Branch: 1 branch
BranchTrueFalseTotal
delete state; 0 0 0
544 0 return NULL;
545 }
546  
547 0 void PosixEnv::StartThread(void (*function)(void* arg), void* arg) {
548 pthread_t t;
549 0 ▶ StartThreadState* state = new StartThreadState;
Branch: 1 branch
BranchTrueFalseTotal
StartThreadState* state = new StartThreadState; 0 0 0
550 0 state->user_function = function;
551 0 state->arg = arg;
552 0 ▶ PthreadCall("start thread",
Branch: 1 branch
BranchTrueFalseTotal
PthreadCall("start thread", 0 0 0
553 pthread_create(&t, NULL, &StartThreadWrapper, state));
554 0 }
555  
556 }
557  
558 static pthread_once_t once = PTHREAD_ONCE_INIT;
559 static Env* default_env;
560 2 ▶static void InitDefaultEnv() { default_env = new PosixEnv; }
Branch: 2 branches
BranchTrueFalseTotal
static void InitDefaultEnv() { default_env = new PosixEnv; } 2 0 2
static void InitDefaultEnv() { default_env = new PosixEnv; } 0 0 0
561  
562 5 Env* Env::Default() {
563 5 pthread_once(&once, InitDefaultEnv);
564 5 return default_env;
565 }
566  
567 }
« table/table.cc ↑ Summary table/two_level_iterator.cc »