2 * Copyright (C) 2017 Cisco Inc.
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.
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.
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/>.
17 // @author Changxue Deng <chadeng@cisco.com>
19 #ifndef __ROLLABLE_FILE_H__
20 #define __ROLLABLE_FILE_H__
30 #include "mmap_file.h"
35 // Memory mapped file that can be rolled based on block size
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);
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;
50 void ResetSlidingWindow();
53 size_t GetResourceCollectionOffset() const;
54 void RemoveUnused(size_t max_size, bool writer_mode);
56 static const long page_size;
57 static int ShmSync(uint8_t *addr, int size);
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);
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;
79 std::vector<std::shared_ptr<MmapFileIO>> files;
80 uint8_t* sliding_addr;
83 off_t sliding_map_off;
85 int rc_offset_percentage;