inital commit
[c11concurrency-benchmarks.git] / mabain / src / rollable_file.h
1 /**
2  * Copyright (C) 2017 Cisco Inc.
3  *
4  * This program is free software: you can redistribute it and/or  modify
5  * it under the terms of the GNU General Public License, version 2,
6  * as published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
15  */
16
17 // @author Changxue Deng <chadeng@cisco.com>
18
19 #ifndef __ROLLABLE_FILE_H__
20 #define __ROLLABLE_FILE_H__
21
22 #include <string>
23 #include <vector>
24 #include <stdint.h>
25 #include <sys/mman.h>
26 #include <assert.h>
27 #include <atomic>
28 #include <memory>
29
30 #include "mmap_file.h"
31 #include "logger.h"
32
33 namespace mabain {
34
35 // Memory mapped file that can be rolled based on block size
36 class RollableFile {
37 public:
38     RollableFile(const std::string &fpath, size_t blocksize,
39                  size_t memcap, int access_mode, long max_block=0, int rc_offset_percentage=75);
40     ~RollableFile();
41
42     size_t   RandomWrite(const void *data, size_t size, off_t offset);
43     size_t   RandomRead(void *buff, size_t size, off_t offset);
44     void     InitShmSlidingAddr(std::atomic<size_t> *shm_sliding_addr);
45     int      Reserve(size_t &offset, int size, uint8_t* &ptr, bool map_new_sliding=true);
46     uint8_t* GetShmPtr(size_t offset, int size);
47     size_t   CheckAlignment(size_t offset, int size);
48     void     PrintStats(std::ostream &out_stream = std::cout) const;
49     void     Close();
50     void     ResetSlidingWindow();
51
52     void     Flush();
53     size_t   GetResourceCollectionOffset() const;
54     void     RemoveUnused(size_t max_size, bool writer_mode);
55
56     static const long page_size;
57     static int ShmSync(uint8_t *addr, int size);
58
59 private:
60     int      OpenAndMapBlockFile(size_t block_order, bool create_file);
61     int      CheckAndOpenFile(size_t block_order, bool create_file);
62     uint8_t* NewSlidingMapAddr(size_t order, size_t offset, int size);
63     void*    NewReaderSlidingMap(size_t order);
64
65     std::string path;
66     size_t block_size;
67     size_t mmap_mem;
68     bool sliding_mmap;
69     int mode;
70     size_t sliding_mem_size;
71     // shared memory sliding start offset for reader
72     // Note writer does not flush sliding mmap during writing.
73     // Readers have to mmap the same region so that they won't
74     // read unflushed data from disk.
75     std::atomic<size_t> *shm_sliding_start_ptr;
76
77     size_t max_num_block;
78
79     std::vector<std::shared_ptr<MmapFileIO>> files;
80     uint8_t* sliding_addr;
81     size_t sliding_size;
82     off_t sliding_start;
83     off_t sliding_map_off;
84
85     int rc_offset_percentage;
86     size_t mem_used;
87 };
88
89 }
90
91 #endif