forked from estraier/tkrzw
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tkrzw_file_mmap.cc
1196 lines (1073 loc) · 33.4 KB
/
tkrzw_file_mmap.cc
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*************************************************************************************************
* File implementations by memory mapping
*
* Copyright 2020 Google LLC
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of the License at
* https://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software distributed under the
* License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the specific language governing permissions
* and limitations under the License.
*************************************************************************************************/
#include "tkrzw_sys_config.h"
#if defined(_TKRZW_STDONLY)
#include "tkrzw_sys_file_mmap_std.h"
#elif defined(_SYS_WINDOWS_)
#include "tkrzw_sys_file_mmap_windows.h"
#else
#include "tkrzw_file.h"
#include "tkrzw_file_mmap.h"
#include "tkrzw_file_util.h"
#include "tkrzw_lib_common.h"
#include "tkrzw_str_util.h"
#include "tkrzw_thread_util.h"
namespace tkrzw {
inline void AdviseMemoryRandomAccessPattern(void* addr, size_t len) {
#if defined(_SYS_LINUX_) || defined(_SYS_MACOSX_)
posix_madvise(addr, len, POSIX_MADV_RANDOM);
#endif
}
inline void* tkrzw_mremap(
void *old_address, size_t old_size, size_t new_size, int32_t fd) {
#if defined(_SYS_LINUX_)
return mremap(old_address, old_size, new_size, MREMAP_MAYMOVE);
#else
if (munmap(old_address, old_size) != 0) {
return MAP_FAILED;
}
return mmap(0, new_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
#endif
}
class MemoryMapParallelFileImpl final {
friend class MemoryMapParallelFile::Zone;
public:
MemoryMapParallelFileImpl();
~MemoryMapParallelFileImpl();
Status Open(const std::string& path, bool writable, int32_t options);
Status Close();
Status Truncate(int64_t size);
Status TruncateFakely(int64_t size);
Status Synchronize(bool hard, int64_t off, int64_t size);
Status GetSize(int64_t* size);
Status SetAllocationStrategy(int64_t init_size, double inc_factor);
Status CopyProperties(File* file);
Status LockMemory(size_t size);
Status GetPath(std::string* path);
Status Rename(const std::string& new_path);
Status DisablePathOperations();
private:
Status AllocateSpace(int64_t min_size);
std::atomic_int32_t fd_;
std::string path_;
std::atomic_int64_t file_size_;
char* map_;
std::atomic_int64_t map_size_;
std::atomic_int64_t lock_size_;
bool writable_;
int32_t open_options_;
int64_t alloc_init_size_;
double alloc_inc_factor_;
SpinSharedMutex mutex_;
};
MemoryMapParallelFileImpl::MemoryMapParallelFileImpl() :
fd_(-1), file_size_(-0), map_(nullptr), map_size_(0), lock_size_(0),
writable_(false), open_options_(0),
alloc_init_size_(File::DEFAULT_ALLOC_INIT_SIZE),
alloc_inc_factor_(File::DEFAULT_ALLOC_INC_FACTOR), mutex_() {}
MemoryMapParallelFileImpl::~MemoryMapParallelFileImpl() {
if (fd_ >= 0) {
Close();
}
}
Status MemoryMapParallelFileImpl::Open(
const std::string& path, bool writable, int32_t options) {
if (fd_ >= 0) {
return Status(Status::PRECONDITION_ERROR, "opened file");
}
// Opens the file.
int32_t oflags = O_RDONLY;
if (writable) {
oflags = O_RDWR;
if (!(options & File::OPEN_NO_CREATE)) {
oflags |= O_CREAT;
}
if (options & File::OPEN_TRUNCATE) {
oflags |= O_TRUNC;
}
}
const int32_t fd = open(path.c_str(), oflags, FILEPERM);
if (fd < 0) {
return GetErrnoStatus("open", errno);
}
// Locks the file.
if (!(options & File::OPEN_NO_LOCK)) {
struct flock flbuf;
std::memset(&flbuf, 0, sizeof(flbuf));
flbuf.l_type = writable ? F_WRLCK : F_RDLCK;
flbuf.l_whence = SEEK_SET;
flbuf.l_start = 0;
flbuf.l_len = 0;
flbuf.l_pid = 0;
const int32_t flcmd = options & File::OPEN_NO_WAIT ? F_SETLK : F_SETLKW;
if (fcntl(fd, flcmd, &flbuf) != 0) {
const Status status = GetErrnoStatus("fcntl-lock", errno);
close(fd);
return status;
}
}
// Checks the file size and type.
struct stat sbuf;
if (fstat(fd, &sbuf) != 0) {
const Status status = GetErrnoStatus("fstat", errno);
close(fd);
return status;
}
if (!S_ISREG(sbuf.st_mode)) {
close(fd);
return Status(Status::INFEASIBLE_ERROR, "not a regular file");
}
const int64_t file_size = sbuf.st_size;
if (file_size > MAX_MEMORY_SIZE) {
close(fd);
return Status(Status::INFEASIBLE_ERROR, "too large file");
}
// Maps the memory.
int64_t map_size = file_size;
int32_t mprot = PROT_READ;
if (writable) {
map_size = std::max(map_size, alloc_init_size_);
map_size = AlignNumber(map_size, PAGE_SIZE);
mprot |= PROT_WRITE;
if (ftruncate(fd, map_size) != 0) {
const Status status = GetErrnoStatus("ftruncate", errno);
close(fd);
return status;
}
} else {
map_size = std::max(map_size, static_cast<int64_t>(PAGE_SIZE));
}
void* map = mmap(0, map_size, mprot, MAP_SHARED, fd, 0);
if (map == MAP_FAILED) {
const Status status = GetErrnoStatus("mmap", errno);
close(fd);
return status;
}
AdviseMemoryRandomAccessPattern(map, map_size);
// Updates the internal data.
fd_ = fd;
path_ = path;
file_size_.store(file_size);
map_ = static_cast<char*>(map);
map_size_.store(map_size);
writable_ = writable;
open_options_ = options;
return Status(Status::SUCCESS);
}
Status MemoryMapParallelFileImpl::Close() {
if (fd_ < 0) {
return Status(Status::PRECONDITION_ERROR, "not opened file");
}
Status status(Status::SUCCESS);
// Unmaps the memory.
const int64_t unmap_size = std::max(map_size_.load(), static_cast<int64_t>(PAGE_SIZE));
if (munmap(map_, unmap_size) != 0) {
status |= GetErrnoStatus("munmap", errno);
}
// Truncates the file.
if (writable_ && ftruncate(fd_, file_size_.load()) != 0) {
status |= GetErrnoStatus("ftruncate", errno);
}
// Unlocks the file.
if (!(open_options_ & File::OPEN_NO_LOCK)) {
struct flock flbuf;
std::memset(&flbuf, 0, sizeof(flbuf));
flbuf.l_type = F_UNLCK;
flbuf.l_whence = SEEK_SET;
flbuf.l_start = 0;
flbuf.l_len = 0;
flbuf.l_pid = 0;
if (fcntl(fd_, F_SETLKW, &flbuf) != 0) {
status |= GetErrnoStatus("fcntl-unlock", errno);
}
}
// Close the file.
if (close(fd_) != 0) {
status |= GetErrnoStatus("close", errno);
}
// Updates the internal data.
fd_ = -1;
path_.clear();
file_size_ .store(0);
map_ = nullptr;
map_size_.store(0);
lock_size_.store(0);
writable_ = false;
open_options_ = 0;
return status;
}
Status MemoryMapParallelFileImpl::Truncate(int64_t size) {
if (fd_ < 0) {
return Status(Status::PRECONDITION_ERROR, "not opened file");
}
if (!writable_) {
return Status(Status::PRECONDITION_ERROR, "not writable file");
}
if (lock_size_.load() > 0 && munlock(map_, lock_size_.load()) != 0) {
return GetErrnoStatus("munlock", errno);
}
lock_size_.store(0);
int64_t new_map_size =
std::max(std::max(size, static_cast<int64_t>(PAGE_SIZE)), alloc_init_size_);
new_map_size = AlignNumber(new_map_size, PAGE_SIZE);
void* new_map = tkrzw_mremap(map_, map_size_.load(), new_map_size, fd_);
if (new_map == MAP_FAILED) {
const Status status = GetErrnoStatus("mremap", errno);
map_ = nullptr;
close(fd_);
fd_ = -1;
return status;
}
AdviseMemoryRandomAccessPattern(new_map, new_map_size);
map_ = static_cast<char*>(new_map);
map_size_.store(new_map_size);
if (ftruncate(fd_, new_map_size) != 0) {
return GetErrnoStatus("ftruncate", errno);
}
file_size_.store(size);
return Status(Status::SUCCESS);
}
Status MemoryMapParallelFileImpl::TruncateFakely(int64_t size) {
if (fd_ < 0) {
return Status(Status::PRECONDITION_ERROR, "not opened file");
}
if (size > map_size_.load()) {
return Status(Status::INFEASIBLE_ERROR, "unable to increase the file size");
}
file_size_.store(size);
return Status(Status::SUCCESS);
}
Status MemoryMapParallelFileImpl::Synchronize(bool hard, int64_t off, int64_t size) {
if (fd_ < 0) {
return Status(Status::PRECONDITION_ERROR, "not opened file");
}
if (!writable_) {
return Status(Status::PRECONDITION_ERROR, "not writable file");
}
std::lock_guard<SpinSharedMutex> lock(mutex_);
Status status(Status::SUCCESS);
map_size_.store(file_size_.load());
if (ftruncate(fd_, map_size_.load()) != 0) {
status |= GetErrnoStatus("ftruncate", errno);
}
if (hard) {
if (size == 0) {
size = map_size_;
}
const int64_t end = off + size;
off -= off % PAGE_SIZE;
size = end - off;
off = std::min<int64_t>(map_size_, off);
size = std::min<int64_t>(map_size_ - off, size);
if (msync(map_ + off, size, MS_SYNC) != 0) {
status |= GetErrnoStatus("msync", errno);
}
}
return status;
}
Status MemoryMapParallelFileImpl::GetSize(int64_t* size) {
if (fd_ < 0) {
return Status(Status::PRECONDITION_ERROR, "not opened file");
}
*size = file_size_.load();
return Status(Status::SUCCESS);
}
Status MemoryMapParallelFileImpl::SetAllocationStrategy(int64_t init_size, double inc_factor) {
if (fd_ >= 0) {
return Status(Status::PRECONDITION_ERROR, "alread opened file");
}
alloc_init_size_ = init_size;
alloc_inc_factor_ = inc_factor;
return Status(Status::SUCCESS);
}
Status MemoryMapParallelFileImpl::CopyProperties(File* file) {
return file->SetAllocationStrategy(alloc_init_size_, alloc_inc_factor_);
}
Status MemoryMapParallelFileImpl::LockMemory(size_t size) {
if (fd_ < 0) {
return Status(Status::PRECONDITION_ERROR, "not opened file");
}
if (lock_size_.load() > 0 && munlock(map_, lock_size_.load()) != 0) {
return GetErrnoStatus("munlock", errno);
}
lock_size_.store(0);
if (size > 0 && mlock(map_, size) != 0) {
return GetErrnoStatus("mlock", errno);
}
lock_size_.store(size);
return Status(Status::SUCCESS);
}
Status MemoryMapParallelFileImpl::GetPath(std::string* path) {
if (fd_ < 0) {
return Status(Status::PRECONDITION_ERROR, "not opened file");
}
if (path_.empty()) {
return Status(Status::PRECONDITION_ERROR, "disabled path operatione");
}
*path = path_;
return Status(Status::SUCCESS);
}
Status MemoryMapParallelFileImpl::Rename(const std::string& new_path) {
if (fd_ < 0) {
return Status(Status::PRECONDITION_ERROR, "not opened file");
}
if (path_.empty()) {
return Status(Status::PRECONDITION_ERROR, "disabled path operatione");
}
Status status = RenameFile(path_, new_path);
if (status == Status::SUCCESS) {
path_ = new_path;
}
return status;
}
Status MemoryMapParallelFileImpl::DisablePathOperations() {
if (fd_ < 0) {
return Status(Status::PRECONDITION_ERROR, "not opened file");
}
path_.clear();
return Status(Status::SUCCESS);
}
Status MemoryMapParallelFileImpl::AllocateSpace(int64_t min_size) {
if (min_size <= map_size_.load()) {
return Status(Status::SUCCESS);
}
std::lock_guard<SpinSharedMutex> lock(mutex_);
if (min_size <= map_size_.load()) {
return Status(Status::SUCCESS);
}
if (lock_size_.load() > 0 && munlock(map_, lock_size_.load()) != 0) {
return GetErrnoStatus("munlock", errno);
}
int64_t new_map_size =
std::max(std::max(min_size, static_cast<int64_t>(
map_size_.load() * alloc_inc_factor_)), static_cast<int64_t>(PAGE_SIZE));
new_map_size = AlignNumber(new_map_size, PAGE_SIZE);
if (ftruncate(fd_, new_map_size) != 0) {
return GetErrnoStatus("ftruncate", errno);
}
void* new_map = tkrzw_mremap(map_, map_size_.load(), new_map_size, fd_);
if (new_map == MAP_FAILED) {
const Status status = GetErrnoStatus("mremap", errno);
map_ = nullptr;
close(fd_);
fd_ = -1;
return status;
}
AdviseMemoryRandomAccessPattern(new_map, new_map_size);
map_ = static_cast<char*>(new_map);
map_size_.store(new_map_size);
if (lock_size_.load() > 0 && mlock(map_, lock_size_.load()) != 0) {
lock_size_.store(0);
return GetErrnoStatus("mlock", errno);
}
return Status(Status::SUCCESS);
}
MemoryMapParallelFile::MemoryMapParallelFile() {
impl_ = new MemoryMapParallelFileImpl();
}
MemoryMapParallelFile::~MemoryMapParallelFile() {
delete impl_;
}
Status MemoryMapParallelFile::Open(const std::string& path, bool writable, int32_t options) {
return impl_->Open(path, writable, options);
}
Status MemoryMapParallelFile::Close() {
return impl_->Close();
}
Status MemoryMapParallelFile::MakeZone(
bool writable, int64_t off, size_t size, std::unique_ptr<Zone>* zone) {
Status status(Status::SUCCESS);
zone->reset(new Zone(impl_, writable, off, size, &status));
return status;
}
Status MemoryMapParallelFile::Read(int64_t off, void* buf, size_t size) {
assert(off >= 0 && buf != nullptr);
Status status(Status::SUCCESS);
Zone zone(impl_, false, off, size, &status);
if (status != Status::SUCCESS) {
return status;
}
if (zone.Size() != size) {
return Status(Status::INFEASIBLE_ERROR, "excessive size");
}
std::memcpy(buf, zone.Pointer(), zone.Size());
return Status(Status::SUCCESS);
}
std::string MemoryMapParallelFile::ReadSimple(int64_t off, size_t size) {
assert(off >= 0);
Status status(Status::SUCCESS);
Zone zone(impl_, false, off, size, &status);
if (status != Status::SUCCESS || zone.Size() != size) {
return "";
}
std::string result(zone.Pointer(), size);
return result;
}
Status MemoryMapParallelFile::Write(int64_t off, const void* buf, size_t size) {
assert(off >= 0 && buf != nullptr && size <= MAX_MEMORY_SIZE);
Status status(Status::SUCCESS);
Zone zone(impl_, true, off, size, &status);
if (status != Status::SUCCESS) {
return status;
}
std::memcpy(zone.Pointer(), buf, zone.Size());
return Status(Status::SUCCESS);
}
Status MemoryMapParallelFile::Append(const void* buf, size_t size, int64_t* off) {
assert(buf != nullptr && size <= MAX_MEMORY_SIZE);
Status status(Status::SUCCESS);
Zone zone(impl_, true, -1, size, &status);
if (status != Status::SUCCESS) {
return status;
}
std::memcpy(zone.Pointer(), buf, zone.Size());
if (off != nullptr) {
*off = zone.Offset();
}
return Status(Status::SUCCESS);
}
Status MemoryMapParallelFile::Expand(size_t inc_size, int64_t* old_size) {
assert(inc_size <= MAX_MEMORY_SIZE);
Status status(Status::SUCCESS);
Zone zone(impl_, true, -1, inc_size, &status);
if (status != Status::SUCCESS) {
return status;
}
if (old_size != nullptr) {
*old_size = zone.Offset();
}
return Status(Status::SUCCESS);
}
Status MemoryMapParallelFile::Truncate(int64_t size) {
assert(size >= 0 && size <= MAX_MEMORY_SIZE);
return impl_->Truncate(size);
}
Status MemoryMapParallelFile::TruncateFakely(int64_t size) {
assert(size >= 0 && size <= MAX_MEMORY_SIZE);
return impl_->TruncateFakely(size);
}
Status MemoryMapParallelFile::Synchronize(bool hard, int64_t off, int64_t size) {
assert(off >= 0 && size >= 0);
return impl_->Synchronize(hard, off, size);
}
Status MemoryMapParallelFile::GetSize(int64_t* size) {
assert(size != nullptr);
return impl_->GetSize(size);
}
Status MemoryMapParallelFile::SetAllocationStrategy(int64_t init_size, double inc_factor) {
assert(init_size > 0 && inc_factor > 0);
return impl_->SetAllocationStrategy(init_size, inc_factor);
}
Status MemoryMapParallelFile::CopyProperties(File* file) {
assert(file != nullptr);
return impl_->CopyProperties(file);
}
Status MemoryMapParallelFile::LockMemory(size_t size) {
assert(size <= MAX_MEMORY_SIZE);
return impl_->LockMemory(size);
}
Status MemoryMapParallelFile::GetPath(std::string* path) {
assert(path != nullptr);
return impl_->GetPath(path);
}
Status MemoryMapParallelFile::Rename(const std::string& new_path) {
return impl_->Rename(new_path);
}
Status MemoryMapParallelFile::DisablePathOperations() {
return impl_->DisablePathOperations();
}
MemoryMapParallelFile::Zone::Zone(
MemoryMapParallelFileImpl* file, bool writable, int64_t off, size_t size,
Status* status)
: file_(nullptr), off_(-1), size_(0), writable_(writable) {
if (file->fd_ < 0) {
status->Set(Status::PRECONDITION_ERROR, "not opened file");
return;
}
if (writable) {
if (!file->writable_) {
status->Set(Status::PRECONDITION_ERROR, "not writable file");
return;
}
if (off < 0) {
int64_t old_file_size = 0;
while (true) {
old_file_size = file->file_size_.load();
const int64_t end_position = old_file_size + size;
const Status adjust_status = file->AllocateSpace(end_position);
if (adjust_status != Status::SUCCESS) {
*status = adjust_status;
return;
}
if (file->file_size_.compare_exchange_weak(old_file_size, end_position)) {
break;
}
}
off = old_file_size;
} else {
const int64_t end_position = off + size;
const Status adjust_status = file->AllocateSpace(end_position);
if (adjust_status != Status::SUCCESS) {
*status = adjust_status;
return;
}
while (true) {
int64_t old_file_size = file->file_size_.load();
if (end_position <= old_file_size ||
file->file_size_.compare_exchange_weak(old_file_size, end_position)) {
break;
}
}
}
} else {
if (off < 0) {
status->Set(Status::PRECONDITION_ERROR, "negative offset");
return;
}
if (off > file->file_size_.load()) {
status->Set(Status::INFEASIBLE_ERROR, "excessive offset");
return;
}
size = std::min(static_cast<int64_t>(size), file->file_size_.load() - off);
}
file_ = file;
file_->mutex_.lock_shared();
off_ = off;
size_ = size;
}
MemoryMapParallelFile::Zone::~Zone() {
if (file_ != nullptr) {
file_->mutex_.unlock_shared();
}
}
int64_t MemoryMapParallelFile::Zone::Offset() const {
return off_;
}
char* MemoryMapParallelFile::Zone::Pointer() const {
return file_->map_ + off_;
}
size_t MemoryMapParallelFile::Zone::Size() const {
return size_;
}
class MemoryMapAtomicFileImpl final {
friend class MemoryMapAtomicFile::Zone;
public:
MemoryMapAtomicFileImpl();
~MemoryMapAtomicFileImpl();
Status Open(const std::string& path, bool writable, int32_t options);
Status Close();
Status Truncate(int64_t size);
Status TruncateFakely(int64_t size);
Status Synchronize(bool hard, int64_t off, int64_t size);
Status GetSize(int64_t* size);
Status SetAllocationStrategy(int64_t init_size, double inc_factor);
Status CopyProperties(File* file);
Status LockMemory(size_t size);
Status GetPath(std::string* path);
Status Rename(const std::string& new_path);
Status DisablePathOperations();
private:
Status AllocateSpace(int64_t min_size);
int32_t fd_;
std::string path_;
int64_t file_size_;
char* map_;
int64_t map_size_;
int64_t lock_size_;
bool writable_;
int32_t open_options_;
int64_t alloc_init_size_;
double alloc_inc_factor_;
SpinSharedMutex mutex_;
};
MemoryMapAtomicFileImpl::MemoryMapAtomicFileImpl() :
fd_(-1), file_size_(0), map_(nullptr), map_size_(0), lock_size_(0),
writable_(false), open_options_(0),
alloc_init_size_(File::DEFAULT_ALLOC_INIT_SIZE),
alloc_inc_factor_(File::DEFAULT_ALLOC_INC_FACTOR), mutex_() {}
MemoryMapAtomicFileImpl::~MemoryMapAtomicFileImpl() {
if (fd_ >= 0) {
Close();
}
}
Status MemoryMapAtomicFileImpl::Open(
const std::string& path, bool writable, int32_t options) {
std::lock_guard<SpinSharedMutex> lock(mutex_);
if (fd_ >= 0) {
return Status(Status::PRECONDITION_ERROR, "opened file");
}
// Opens the file.
int32_t oflags = O_RDONLY;
if (writable) {
oflags = O_RDWR;
if (!(options & File::OPEN_NO_CREATE)) {
oflags |= O_CREAT;
}
if (options & File::OPEN_TRUNCATE) {
oflags |= O_TRUNC;
}
}
const int32_t fd = open(path.c_str(), oflags, FILEPERM);
if (fd < 0) {
return GetErrnoStatus("open", errno);
}
// Locks the file.
if (!(options & File::OPEN_NO_LOCK)) {
struct flock flbuf;
std::memset(&flbuf, 0, sizeof(flbuf));
flbuf.l_type = writable ? F_WRLCK : F_RDLCK;
flbuf.l_whence = SEEK_SET;
flbuf.l_start = 0;
flbuf.l_len = 0;
flbuf.l_pid = 0;
const int32_t flcmd = options & File::OPEN_NO_WAIT ? F_SETLK : F_SETLKW;
if (fcntl(fd, flcmd, &flbuf) != 0) {
const Status status = GetErrnoStatus("fcntl-lock", errno);
close(fd);
return status;
}
}
// Checks the file size and type.
struct stat sbuf;
if (fstat(fd, &sbuf) != 0) {
const Status status = GetErrnoStatus("fstat", errno);
close(fd);
return status;
}
if (!S_ISREG(sbuf.st_mode)) {
close(fd);
return Status(Status::INFEASIBLE_ERROR, "not a regular file");
}
const int64_t file_size = sbuf.st_size;
if (file_size > MAX_MEMORY_SIZE) {
close(fd);
return Status(Status::INFEASIBLE_ERROR, "too large file");
}
// Maps the memory.
int64_t map_size = file_size;
int32_t mprot = PROT_READ;
if (writable) {
map_size = std::max(map_size, alloc_init_size_);
map_size = AlignNumber(map_size, PAGE_SIZE);
mprot |= PROT_WRITE;
if (ftruncate(fd, map_size) != 0) {
const Status status = GetErrnoStatus("ftruncate", errno);
close(fd);
return status;
}
} else {
map_size = std::max(map_size, static_cast<int64_t>(PAGE_SIZE));
}
void* map = mmap(0, map_size, mprot, MAP_SHARED, fd, 0);
if (map == MAP_FAILED) {
const Status status = GetErrnoStatus("mmap", errno);
close(fd);
return status;
}
AdviseMemoryRandomAccessPattern(map, map_size);
// Updates the internal data.
fd_ = fd;
path_ = path;
file_size_ = file_size;
map_ = static_cast<char*>(map);
map_size_ = map_size;
writable_ = writable;
open_options_ = options;
return Status(Status::SUCCESS);
}
Status MemoryMapAtomicFileImpl::Close() {
std::lock_guard<SpinSharedMutex> lock(mutex_);
if (fd_ < 0) {
return Status(Status::PRECONDITION_ERROR, "not opened file");
}
Status status(Status::SUCCESS);
// Unmaps the memory.
const int64_t unmap_size = std::max(map_size_, static_cast<int64_t>(PAGE_SIZE));
if (munmap(map_, unmap_size) != 0) {
status |= GetErrnoStatus("munmap", errno);
}
// Truncates the file.
if (writable_ && ftruncate(fd_, file_size_) != 0) {
status |= GetErrnoStatus("ftruncate", errno);
}
// Unlocks the file.
if (!(open_options_ & File::OPEN_NO_LOCK)) {
struct flock flbuf;
std::memset(&flbuf, 0, sizeof(flbuf));
flbuf.l_type = F_UNLCK;
flbuf.l_whence = SEEK_SET;
flbuf.l_start = 0;
flbuf.l_len = 0;
flbuf.l_pid = 0;
if (fcntl(fd_, F_SETLKW, &flbuf) != 0) {
status |= GetErrnoStatus("fcntl-unlock", errno);
}
}
// Close the file.
if (close(fd_) != 0) {
status |= GetErrnoStatus("close", errno);
}
// Updates the internal data.
fd_ = -1;
path_.clear();
file_size_ = 0;
map_ = nullptr;
map_size_ = 0;
lock_size_ = 0;
writable_ = false;
open_options_ = 0;
return status;
}
Status MemoryMapAtomicFileImpl::Truncate(int64_t size) {
std::lock_guard<SpinSharedMutex> lock(mutex_);
if (fd_ < 0) {
return Status(Status::PRECONDITION_ERROR, "not opened file");
}
if (!writable_) {
return Status(Status::PRECONDITION_ERROR, "not writable file");
}
if (lock_size_ > 0 && munlock(map_, lock_size_) != 0) {
return GetErrnoStatus("munlock", errno);
}
lock_size_ = 0;
int64_t new_map_size =
std::max(std::max(size, static_cast<int64_t>(PAGE_SIZE)), alloc_init_size_);
new_map_size = AlignNumber(new_map_size, PAGE_SIZE);
void* new_map = tkrzw_mremap(map_, map_size_, new_map_size, fd_);
if (new_map == MAP_FAILED) {
const Status status = GetErrnoStatus("mremap", errno);
map_ = nullptr;
close(fd_);
fd_ = -1;
return status;
}
map_ = static_cast<char*>(new_map);
map_size_ = new_map_size;
if (ftruncate(fd_, new_map_size) != 0) {
return GetErrnoStatus("ftruncate", errno);
}
file_size_ = size;
return Status(Status::SUCCESS);
}
Status MemoryMapAtomicFileImpl::TruncateFakely(int64_t size) {
std::lock_guard<SpinSharedMutex> lock(mutex_);
if (fd_ < 0) {
return Status(Status::PRECONDITION_ERROR, "not opened file");
}
if (size > map_size_) {
return Status(Status::INFEASIBLE_ERROR, "unable to increase the file size");
}
file_size_ = size;
return Status(Status::SUCCESS);
}
Status MemoryMapAtomicFileImpl::Synchronize(bool hard, int64_t off, int64_t size) {
std::lock_guard<SpinSharedMutex> lock(mutex_);
if (fd_ < 0) {
return Status(Status::PRECONDITION_ERROR, "not opened file");
}
if (!writable_) {
return Status(Status::PRECONDITION_ERROR, "not writable file");
}
Status status(Status::SUCCESS);
map_size_ = file_size_;
if (ftruncate(fd_, map_size_) != 0) {
status |= GetErrnoStatus("ftruncate", errno);
}
if (hard) {
if (size == 0) {
size = map_size_;
}
const int64_t end = off + size;
off -= off % PAGE_SIZE;
size = end - off;
off = std::min<int64_t>(map_size_, off);
size = std::min<int64_t>(map_size_ - off, size);
if (msync(map_ + off, size, MS_SYNC) != 0) {
status |= GetErrnoStatus("msync", errno);
}
}
return status;
}
Status MemoryMapAtomicFileImpl::GetSize(int64_t* size) {
std::shared_lock<SpinSharedMutex> lock(mutex_);
if (fd_ < 0) {
return Status(Status::PRECONDITION_ERROR, "not opened file");
}
*size = file_size_;
return Status(Status::SUCCESS);
}
Status MemoryMapAtomicFileImpl::SetAllocationStrategy(int64_t init_size, double inc_factor) {
std::lock_guard<SpinSharedMutex> lock(mutex_);
if (fd_ >= 0) {
return Status(Status::PRECONDITION_ERROR, "alread opened file");
}
alloc_init_size_ = init_size;
alloc_inc_factor_ = inc_factor;
return Status(Status::SUCCESS);
}
Status MemoryMapAtomicFileImpl::CopyProperties(File* file) {
std::shared_lock<SpinSharedMutex> lock(mutex_);
return file->SetAllocationStrategy(alloc_init_size_, alloc_inc_factor_);
}
Status MemoryMapAtomicFileImpl::GetPath(std::string* path) {
std::shared_lock<SpinSharedMutex> lock(mutex_);
if (fd_ < 0) {
return Status(Status::PRECONDITION_ERROR, "not opened file");
}
if (path_.empty()) {
return Status(Status::PRECONDITION_ERROR, "disabled path operatione");
}
*path = path_;
return Status(Status::SUCCESS);
}
Status MemoryMapAtomicFileImpl::Rename(const std::string& new_path) {
std::lock_guard<SpinSharedMutex> lock(mutex_);
if (fd_ < 0) {
return Status(Status::PRECONDITION_ERROR, "not opened file");
}
if (path_.empty()) {
return Status(Status::PRECONDITION_ERROR, "disabled path operatione");
}
Status status = RenameFile(path_, new_path);
if (status == Status::SUCCESS) {
path_ = new_path;
}
return status;
}
Status MemoryMapAtomicFileImpl::DisablePathOperations() {
std::lock_guard<SpinSharedMutex> lock(mutex_);
if (fd_ < 0) {
return Status(Status::PRECONDITION_ERROR, "not opened file");
}
path_.clear();
return Status(Status::SUCCESS);
}
Status MemoryMapAtomicFileImpl::LockMemory(size_t size) {
std::lock_guard<SpinSharedMutex> lock(mutex_);
if (fd_ < 0) {
return Status(Status::PRECONDITION_ERROR, "not opened file");
}
if (lock_size_ > 0 && munlock(map_, lock_size_) != 0) {
return GetErrnoStatus("munlock", errno);
}
lock_size_ = 0;
if (size > 0 && mlock(map_, size) != 0) {
return GetErrnoStatus("mlock", errno);
}
lock_size_ = size;
return Status(Status::SUCCESS);
}
Status MemoryMapAtomicFileImpl::AllocateSpace(int64_t min_size) {
if (min_size <= map_size_) {
return Status(Status::SUCCESS);
}
if (lock_size_ > 0 && munlock(map_, lock_size_) != 0) {
return GetErrnoStatus("munlock", errno);
}
int64_t new_map_size =
std::max(std::max(min_size, static_cast<int64_t>(
map_size_ * alloc_inc_factor_)), static_cast<int64_t>(PAGE_SIZE));
new_map_size = AlignNumber(new_map_size, PAGE_SIZE);
if (ftruncate(fd_, new_map_size) != 0) {
return GetErrnoStatus("ftruncate", errno);
}
void* new_map = tkrzw_mremap(map_, map_size_, new_map_size, fd_);
if (new_map == MAP_FAILED) {
const Status status = GetErrnoStatus("mremap", errno);
map_ = nullptr;
close(fd_);
fd_ = -1;
return status;
}
map_ = static_cast<char*>(new_map);
map_size_ = new_map_size;
if (lock_size_ > 0 && mlock(map_, lock_size_) != 0) {
lock_size_ = 0;
return GetErrnoStatus("mlock", errno);
}
return Status(Status::SUCCESS);
}
MemoryMapAtomicFile::MemoryMapAtomicFile() {
impl_ = new MemoryMapAtomicFileImpl();
}
MemoryMapAtomicFile::~MemoryMapAtomicFile() {