inital commit
[c11concurrency-benchmarks.git] / mabain / src / free_list.cpp
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 #include <iostream>
20 #include <unistd.h>
21 #include <errno.h>
22 #include <string.h>
23 #include <assert.h>
24
25 #include "free_list.h"
26 #include "error.h"
27 #include "logger.h"
28 #include "lock_free.h"
29
30 namespace mabain {
31
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),
39                  count(0),
40                  tot_size(0)
41 {
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);
45
46     Logger::Log(LOG_LEVEL_INFO, "%s maximum number of buffers: %d", file_path.c_str(),
47                 max_num_buffer);
48     buffer_free_list = new MBlsq*[max_num_buffer];
49     for(size_t i = 0; i < max_num_buffer; i++)
50     {
51         buffer_free_list[i] = new MBlsq(NULL);
52     }
53 }
54
55 FreeList::~FreeList()
56 {
57     if(buffer_free_list)
58     {
59         for(size_t i = 0; i < max_num_buffer; i++)
60         {
61             if(buffer_free_list[i])
62             {
63                 while(buffer_free_list[i]->Count())
64                     buffer_free_list[i]->RemoveIntFromHead();
65                 delete buffer_free_list[i];
66             }
67         }
68         delete [] buffer_free_list;
69     }
70 }
71
72 int FreeList::ReuseBuffer(size_t buf_index, size_t offset)
73 {
74     int rval = MBError::BUFFER_LOST;
75     for(size_t i = buf_index - 1; i > 0; i--)
76     {
77         if(buffer_free_list[i]->Count() > max_buffer_per_list)
78             continue;
79
80         if(buffer_free_list[i]->AddIntToTail(offset) == MBError::SUCCESS)
81         {
82             count++;
83             tot_size += (i + 1) * alignment;
84             rval = MBError::SUCCESS;
85         }
86         break;
87     }
88
89     return rval;
90 }
91
92 int FreeList::AddBuffer(size_t offset, size_t size)
93 {
94     int rval = MBError::SUCCESS;
95
96     size_t buf_index = GetBufferIndex(size);
97 #ifdef __DEBUG__
98     assert(buf_index < max_num_buffer);
99 #endif
100
101     if(buffer_free_list[buf_index]->Count() > (unsigned)max_buffer_per_list)
102     {
103         ReuseBuffer(buf_index, offset); 
104         return rval;
105     }
106
107     buffer_free_list[buf_index]->AddIntToTail(offset);
108     count++;
109     tot_size += (buf_index + 1) * alignment;
110     return rval;
111 }
112
113 int FreeList::RemoveBuffer(size_t &offset, size_t size)
114 {
115     int rval = MBError::NO_MEMORY;
116
117     size_t buf_index = GetBufferIndex(size);
118     if(buffer_free_list[buf_index]->Count() > 0)
119     {
120         offset = buffer_free_list[buf_index]->RemoveIntFromHead();
121         rval = MBError::SUCCESS;
122         count--;
123         tot_size -= (buf_index + 1) * alignment;
124     }
125
126     return rval;
127 }
128
129 size_t FreeList::GetTotSize() const
130 {
131     return tot_size;
132 }
133
134 int64_t FreeList::Count() const
135 {
136     return count;
137 }
138
139 int FreeList::StoreListOnDisk()
140 {
141     if(buffer_free_list == NULL)
142         return MBError::NOT_ALLOWED;
143
144     int rval = MBError::SUCCESS;
145
146     if(count == 0)
147         return rval;
148
149     std::ofstream freelist_f(list_path.c_str(), std::fstream::out | std::fstream::binary);
150     if(!freelist_f.is_open())
151     {
152         Logger::Log(LOG_LEVEL_ERROR, "cannot open file " + list_path);
153         return MBError::OPEN_FAILURE;
154     }
155
156     Logger::Log(LOG_LEVEL_INFO, "%s write %lld buffers to list disk: %llu", list_path.c_str(),
157                 count, tot_size);
158     for(size_t buf_index = 0; buf_index < max_num_buffer; buf_index++)
159     {
160         int64_t buf_count = buffer_free_list[buf_index]->Count();
161         if(buf_count > 0)
162         {
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++)
167             {
168                 size_t offset = buffer_free_list[buf_index]->RemoveIntFromHead();
169                 freelist_f.write((char *) &offset, sizeof(size_t));
170                 count--;
171                 tot_size -= (buf_index + 1) * alignment;
172             }
173         }
174     }
175
176     freelist_f.close();
177
178     return rval;
179 }
180
181 int FreeList::LoadListFromDisk()
182 {
183     if(buffer_free_list == NULL)
184         return MBError::NOT_ALLOWED;
185
186     if(access(list_path.c_str(), F_OK) != 0)
187     {
188         if(errno == ENOENT)
189         {
190             Logger::Log(LOG_LEVEL_INFO, list_path + " does not exist");
191             return MBError::SUCCESS;
192         }
193
194         Logger::Log(LOG_LEVEL_ERROR, "cannot access %s with full permission: %d",
195                     list_path.c_str(), errno);
196         return MBError::NOT_ALLOWED;
197     }
198
199     // Read the file
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;
203
204     while(!freelist_f.eof())
205     {
206         size_t buf_index;
207         int64_t buf_count;
208         // Read header
209         freelist_f.read((char *) &buf_index, sizeof(size_t));
210         freelist_f.read((char *) &buf_count, sizeof(int64_t));
211         if(freelist_f.eof())
212             break;
213         for(int64_t i = 0; i < buf_count; i++)
214         {
215             size_t offset;
216             freelist_f.read((char *) &offset, sizeof(size_t));
217             buffer_free_list[buf_index]->AddIntToTail(offset);
218             count++;
219             tot_size += (buf_index + 1) * alignment;
220         }
221     }
222
223     freelist_f.close();
224
225     // Remove the file
226     if(unlink(list_path.c_str()) != 0)
227     {
228         Logger::Log(LOG_LEVEL_ERROR, "failed to delete file %s: %d ",
229                     list_path.c_str(), errno);
230         return MBError::WRITE_ERROR;
231     }
232
233     Logger::Log(LOG_LEVEL_INFO, "%s read %lld buffers to free list: %llu",
234                 list_path.c_str(), count, tot_size);
235
236     return MBError::SUCCESS;
237 }
238
239 void FreeList::ReleaseAlignmentBuffer(size_t old_offset, size_t alignment_offset)
240 {
241     if(alignment_offset <= old_offset)
242         return;
243
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");
247 }
248
249 void FreeList::Empty()
250 {
251     for(size_t i = 0; i < max_num_buffer; i++)
252     {
253         if(buffer_free_list[i])
254         {
255             buffer_free_list[i]->Clear();
256         }
257     }
258     count = 0;
259     tot_size = 0;
260 }
261
262 bool FreeList::GetBufferByIndex(size_t buf_index, size_t &offset)
263 {
264 #ifdef __DEBUG__
265     assert(buf_index < max_num_buffer);
266 #endif
267     MBlsq *flist = buffer_free_list[buf_index];
268     if(flist->Count() > 0)
269     {
270         offset = flist->RemoveIntFromHead();
271         count--;
272         tot_size -= (buf_index + 1) * alignment;
273         return true;
274     }
275
276     return false;
277 }
278
279 }