@@ -354,9 +354,9 @@ class PosixMmapFile : public WritableFile {
354
354
char * dst_; // Where to write next (in range [base_,limit_])
355
355
char * last_sync_; // Where have we synced up to
356
356
uint64_t file_offset_; // Offset of base_ in file
357
-
358
357
// Have we done an munmap of unsynced data?
359
358
bool pending_sync_;
359
+ bool fallocate_with_keep_size_;
360
360
361
361
// Roundup x to a multiple of y
362
362
static size_t Roundup (size_t x, size_t y) {
@@ -399,7 +399,12 @@ class PosixMmapFile : public WritableFile {
399
399
assert (base_ == nullptr );
400
400
401
401
TEST_KILL_RANDOM (rocksdb_kill_odds);
402
- int alloc_status = posix_fallocate (fd_, file_offset_, map_size_);
402
+ // we can't fallocate with FALLOC_FL_KEEP_SIZE here
403
+ int alloc_status = fallocate (fd_, 0 , file_offset_, map_size_);
404
+ if (alloc_status != 0 ) {
405
+ // fallback to posix_fallocate
406
+ alloc_status = posix_fallocate (fd_, file_offset_, map_size_);
407
+ }
403
408
if (alloc_status != 0 ) {
404
409
return Status::IOError (" Error allocating space to file : " + filename_ +
405
410
" Error : " + strerror (alloc_status));
@@ -436,7 +441,8 @@ class PosixMmapFile : public WritableFile {
436
441
dst_(nullptr ),
437
442
last_sync_(nullptr ),
438
443
file_offset_(0 ),
439
- pending_sync_(false ) {
444
+ pending_sync_(false ),
445
+ fallocate_with_keep_size_(options.fallocate_with_keep_size) {
440
446
assert ((page_size & (page_size - 1 )) == 0 );
441
447
assert (options.use_mmap_writes );
442
448
}
@@ -584,7 +590,9 @@ class PosixMmapFile : public WritableFile {
584
590
#ifdef ROCKSDB_FALLOCATE_PRESENT
585
591
virtual Status Allocate (off_t offset, off_t len) {
586
592
TEST_KILL_RANDOM (rocksdb_kill_odds);
587
- if (!fallocate (fd_, FALLOC_FL_KEEP_SIZE, offset, len)) {
593
+ int alloc_status = fallocate (
594
+ fd_, fallocate_with_keep_size_ ? FALLOC_FL_KEEP_SIZE : 0 , offset, len);
595
+ if (alloc_status == 0 ) {
588
596
return Status::OK ();
589
597
} else {
590
598
return IOError (filename_, errno);
@@ -606,20 +614,22 @@ class PosixWritableFile : public WritableFile {
606
614
bool pending_fsync_;
607
615
uint64_t last_sync_size_;
608
616
uint64_t bytes_per_sync_;
617
+ bool fallocate_with_keep_size_;
609
618
610
619
public:
611
620
PosixWritableFile (const std::string& fname, int fd, size_t capacity,
612
- const EnvOptions& options) :
613
- filename_ (fname),
614
- fd_ (fd),
615
- cursize_ (0 ),
616
- capacity_ (capacity),
617
- buf_ (new char [capacity]),
618
- filesize_ (0 ),
619
- pending_sync_ (false ),
620
- pending_fsync_ (false ),
621
- last_sync_size_ (0 ),
622
- bytes_per_sync_ (options.bytes_per_sync) {
621
+ const EnvOptions& options)
622
+ : filename_(fname),
623
+ fd_ (fd),
624
+ cursize_(0 ),
625
+ capacity_(capacity),
626
+ buf_(new char [capacity]),
627
+ filesize_(0 ),
628
+ pending_sync_(false ),
629
+ pending_fsync_(false ),
630
+ last_sync_size_(0 ),
631
+ bytes_per_sync_(options.bytes_per_sync),
632
+ fallocate_with_keep_size_(options.fallocate_with_keep_size) {
623
633
assert (!options.use_mmap_writes );
624
634
}
625
635
@@ -771,7 +781,9 @@ class PosixWritableFile : public WritableFile {
771
781
#ifdef ROCKSDB_FALLOCATE_PRESENT
772
782
virtual Status Allocate (off_t offset, off_t len) {
773
783
TEST_KILL_RANDOM (rocksdb_kill_odds);
774
- if (!fallocate (fd_, FALLOC_FL_KEEP_SIZE, offset, len)) {
784
+ int alloc_status = fallocate (
785
+ fd_, fallocate_with_keep_size_ ? FALLOC_FL_KEEP_SIZE : 0 , offset, len);
786
+ if (alloc_status == 0 ) {
775
787
return Status::OK ();
776
788
} else {
777
789
return IOError (filename_, errno);
@@ -797,14 +809,15 @@ class PosixRandomRWFile : public RandomRWFile {
797
809
int fd_;
798
810
bool pending_sync_;
799
811
bool pending_fsync_;
812
+ bool fallocate_with_keep_size_;
800
813
801
814
public:
802
- PosixRandomRWFile (const std::string& fname, int fd,
803
- const EnvOptions& options) :
804
- filename_ (fname ),
805
- fd_ (fd ),
806
- pending_sync_ (false ),
807
- pending_fsync_ ( false ) {
815
+ PosixRandomRWFile (const std::string& fname, int fd, const EnvOptions& options)
816
+ : filename_(fname),
817
+ fd_ (fd ),
818
+ pending_sync_( false ),
819
+ pending_fsync_ (false ),
820
+ fallocate_with_keep_size_(options.fallocate_with_keep_size ) {
808
821
assert (!options.use_mmap_writes && !options.use_mmap_reads );
809
822
}
810
823
@@ -874,7 +887,10 @@ class PosixRandomRWFile : public RandomRWFile {
874
887
875
888
#ifdef ROCKSDB_FALLOCATE_PRESENT
876
889
virtual Status Allocate (off_t offset, off_t len) {
877
- if (!fallocate (fd_, FALLOC_FL_KEEP_SIZE, offset, len)) {
890
+ TEST_KILL_RANDOM (rocksdb_kill_odds);
891
+ int alloc_status = fallocate (
892
+ fd_, fallocate_with_keep_size_ ? FALLOC_FL_KEEP_SIZE : 0 , offset, len);
893
+ if (alloc_status == 0 ) {
878
894
return Status::OK ();
879
895
} else {
880
896
return IOError (filename_, errno);
@@ -1332,6 +1348,20 @@ class PosixEnv : public Env {
1332
1348
return dummy;
1333
1349
}
1334
1350
1351
+ EnvOptions OptimizeForLogWrite (const EnvOptions& env_options) const {
1352
+ EnvOptions optimized = env_options;
1353
+ optimized.use_mmap_writes = false ;
1354
+ optimized.fallocate_with_keep_size = true ;
1355
+ return optimized;
1356
+ }
1357
+
1358
+ EnvOptions OptimizeForManifestWrite (const EnvOptions& env_options) const {
1359
+ EnvOptions optimized = env_options;
1360
+ optimized.use_mmap_writes = false ;
1361
+ optimized.fallocate_with_keep_size = true ;
1362
+ return optimized;
1363
+ }
1364
+
1335
1365
private:
1336
1366
bool checkedDiskForMmap_;
1337
1367
bool forceMmapOff; // do we override Env options?
0 commit comments