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>
25 #include "free_list.h"
28 #include "lock_free.h"
32 FreeList::FreeList(const std::string &file_path, size_t buff_alignment,
33 size_t max_n_buff, size_t max_buff_per_list)
34 : list_path(file_path),
35 alignment(buff_alignment),
36 max_num_buffer(max_n_buff),
37 max_buffer_per_list(max_buff_per_list),
38 buffer_free_list(NULL),
42 // rel_parent_off in ResourceCollection is defined as 2-byte signed integer.
43 // The maximal buffer size cannot be greather than 32767.
44 assert(GetBufferSizeByIndex(max_n_buff-1) <= 65535);
46 Logger::Log(LOG_LEVEL_INFO, "%s maximum number of buffers: %d", file_path.c_str(),
48 buffer_free_list = new MBlsq*[max_num_buffer];
49 for(size_t i = 0; i < max_num_buffer; i++)
51 buffer_free_list[i] = new MBlsq(NULL);
59 for(size_t i = 0; i < max_num_buffer; i++)
61 if(buffer_free_list[i])
63 while(buffer_free_list[i]->Count())
64 buffer_free_list[i]->RemoveIntFromHead();
65 delete buffer_free_list[i];
68 delete [] buffer_free_list;
72 int FreeList::ReuseBuffer(size_t buf_index, size_t offset)
74 int rval = MBError::BUFFER_LOST;
75 for(size_t i = buf_index - 1; i > 0; i--)
77 if(buffer_free_list[i]->Count() > max_buffer_per_list)
80 if(buffer_free_list[i]->AddIntToTail(offset) == MBError::SUCCESS)
83 tot_size += (i + 1) * alignment;
84 rval = MBError::SUCCESS;
92 int FreeList::AddBuffer(size_t offset, size_t size)
94 int rval = MBError::SUCCESS;
96 size_t buf_index = GetBufferIndex(size);
98 assert(buf_index < max_num_buffer);
101 if(buffer_free_list[buf_index]->Count() > (unsigned)max_buffer_per_list)
103 ReuseBuffer(buf_index, offset);
107 buffer_free_list[buf_index]->AddIntToTail(offset);
109 tot_size += (buf_index + 1) * alignment;
113 int FreeList::RemoveBuffer(size_t &offset, size_t size)
115 int rval = MBError::NO_MEMORY;
117 size_t buf_index = GetBufferIndex(size);
118 if(buffer_free_list[buf_index]->Count() > 0)
120 offset = buffer_free_list[buf_index]->RemoveIntFromHead();
121 rval = MBError::SUCCESS;
123 tot_size -= (buf_index + 1) * alignment;
129 size_t FreeList::GetTotSize() const
134 int64_t FreeList::Count() const
139 int FreeList::StoreListOnDisk()
141 if(buffer_free_list == NULL)
142 return MBError::NOT_ALLOWED;
144 int rval = MBError::SUCCESS;
149 std::ofstream freelist_f(list_path.c_str(), std::fstream::out | std::fstream::binary);
150 if(!freelist_f.is_open())
152 Logger::Log(LOG_LEVEL_ERROR, "cannot open file " + list_path);
153 return MBError::OPEN_FAILURE;
156 Logger::Log(LOG_LEVEL_INFO, "%s write %lld buffers to list disk: %llu", list_path.c_str(),
158 for(size_t buf_index = 0; buf_index < max_num_buffer; buf_index++)
160 int64_t buf_count = buffer_free_list[buf_index]->Count();
163 // write list header (buffer index, buffer count)
164 freelist_f.write((char *)&buf_index, sizeof(size_t));
165 freelist_f.write((char *)&buf_count, sizeof(int64_t));
166 for(int64_t i = 0; i < buf_count; i++)
168 size_t offset = buffer_free_list[buf_index]->RemoveIntFromHead();
169 freelist_f.write((char *) &offset, sizeof(size_t));
171 tot_size -= (buf_index + 1) * alignment;
181 int FreeList::LoadListFromDisk()
183 if(buffer_free_list == NULL)
184 return MBError::NOT_ALLOWED;
186 if(access(list_path.c_str(), F_OK) != 0)
190 Logger::Log(LOG_LEVEL_INFO, list_path + " does not exist");
191 return MBError::SUCCESS;
194 Logger::Log(LOG_LEVEL_ERROR, "cannot access %s with full permission: %d",
195 list_path.c_str(), errno);
196 return MBError::NOT_ALLOWED;
200 std::ifstream freelist_f(list_path.c_str(), std::fstream::in | std::fstream::binary);
201 if(!freelist_f.is_open())
202 return MBError::OPEN_FAILURE;
204 while(!freelist_f.eof())
209 freelist_f.read((char *) &buf_index, sizeof(size_t));
210 freelist_f.read((char *) &buf_count, sizeof(int64_t));
213 for(int64_t i = 0; i < buf_count; i++)
216 freelist_f.read((char *) &offset, sizeof(size_t));
217 buffer_free_list[buf_index]->AddIntToTail(offset);
219 tot_size += (buf_index + 1) * alignment;
226 if(unlink(list_path.c_str()) != 0)
228 Logger::Log(LOG_LEVEL_ERROR, "failed to delete file %s: %d ",
229 list_path.c_str(), errno);
230 return MBError::WRITE_ERROR;
233 Logger::Log(LOG_LEVEL_INFO, "%s read %lld buffers to free list: %llu",
234 list_path.c_str(), count, tot_size);
236 return MBError::SUCCESS;
239 void FreeList::ReleaseAlignmentBuffer(size_t old_offset, size_t alignment_offset)
241 if(alignment_offset <= old_offset)
244 int rval = AddBuffer(old_offset, alignment_offset - old_offset);
245 if(rval != MBError::SUCCESS)
246 Logger::Log(LOG_LEVEL_ERROR, "failed to release alignment buffer");
249 void FreeList::Empty()
251 for(size_t i = 0; i < max_num_buffer; i++)
253 if(buffer_free_list[i])
255 buffer_free_list[i]->Clear();
262 bool FreeList::GetBufferByIndex(size_t buf_index, size_t &offset)
265 assert(buf_index < max_num_buffer);
267 MBlsq *flist = buffer_free_list[buf_index];
268 if(flist->Count() > 0)
270 offset = flist->RemoveIntFromHead();
272 tot_size -= (buf_index + 1) * alignment;