-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathbuffer_helper.cpp
More file actions
288 lines (250 loc) · 10.9 KB
/
buffer_helper.cpp
File metadata and controls
288 lines (250 loc) · 10.9 KB
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
// Copyright 2025 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
//
// http://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 "buffer_helper.h"
#include <fcntl.h> // For file open flags like O_CREAT, O_RDWR
#include <string.h> // For strerror_r
#include <sys/mman.h> // For memory mapping (mmap, munmap)
#include <sys/stat.h> // For getting file status (fstat)
#include <unistd.h> // For POSIX API calls like close() and ftruncate()
#include <filesystem>
#include <string>
#include <vector>
#include "absl/log/log.h"
#include "absl/status/status.h"
#include "absl/strings/str_cat.h"
#include "absl/strings/str_join.h"
namespace ml_flashpoint::checkpoint_object_manager::buffer_object::internal {
std::string GetErrnoString(int err_num) {
return std::system_category().message(err_num);
}
absl::Status ErrnoToStatus(const std::string& message) {
return absl::InternalError(
absl::StrCat(message, ": ", GetErrnoString(errno)));
}
absl::Status create_file_and_mmap(const std::string& object_id, size_t size,
int& out_fd, size_t& out_data_size,
void*& out_data_ptr, bool overwrite) {
LOG(INFO) << "create_file_and_mmap: object_id=" << object_id
<< ", size=" << size << ", overwrite=" << overwrite;
if (size <= 0) {
return absl::InvalidArgumentError(
"create_file_and_mmap requires a size greater than 0 for file: " +
object_id);
}
// Create a new file or clear an existing one.
// O_CREAT: Create if it doesn't exist.
// O_RDWR: Open for reading and writing.
// O_TRUNC: Truncate to zero length if it exists.
// O_EXCL: "Exclusive" flag, fail if the file already exists.
// 0666: File permissions (readable/writable by everyone).
int open_flags;
if (overwrite) {
// If overwrite is true, use O_TRUNC to clear the existing file.
open_flags = O_CREAT | O_RDWR | O_TRUNC;
} else {
// If overwrite is false (the default), use O_EXCL.
// When used with O_CREAT, O_EXCL causes open() to fail if the file already
// exists.
open_flags = O_CREAT | O_RDWR | O_EXCL;
}
// Create parent directories if they don't exist.
// TODO: Add tests for this.
std::filesystem::path file_path(object_id);
if (file_path.has_parent_path()) {
std::error_code ec;
std::filesystem::create_directories(file_path.parent_path(), ec);
if (ec) {
return absl::InternalError(absl::StrCat(
"Failed to create directories for: ", object_id, ": ", ec.message()));
}
}
int fd = open(object_id.c_str(), open_flags, 0666);
if (fd == -1) {
// If the failure is due to the file already existing (EEXIST) and we are
// in "no-overwrite" mode, throw a more specific error.
if (errno == EEXIST && !overwrite) {
return absl::AlreadyExistsError(
"open() failed for file: " + object_id +
". File already exists and overwrite is set to false.");
}
// For all other errors, throw a generic open() failure error.
return ErrnoToStatus("open() failed for file: " + object_id);
}
// Set the size of the file.
if (ftruncate(fd, size) == -1) {
close(fd);
return ErrnoToStatus("ftruncate() failed for file: " + object_id);
}
// Map the file into memory.
void* ptr = MAP_FAILED;
ptr = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
if (ptr == MAP_FAILED) {
close(fd);
return ErrnoToStatus("mmap() failed for file: " + object_id);
}
// On success, set kernel memory hints:
// MADV_WILLNEED: expect access in the near future, so can prefetch
// pages into memory in the background.
// MADV_HUGEPAGE: use huge pages when possible, to minimize TLB misses and
// improve read/write throughput.
madvise(ptr, size, MADV_WILLNEED | MADV_HUGEPAGE);
// On success, set all the output parameters.
out_fd = fd;
out_data_size = size;
out_data_ptr = ptr;
LOG(INFO) << "Successfully created and mmapped file for object '" << object_id
<< "'. data_ptr=" << out_data_ptr << ", size=" << out_data_size;
return absl::OkStatus();
}
absl::Status open_file_and_mmap_ro(const std::string& object_id, int& out_fd,
size_t& out_data_size, void*& out_data_ptr) {
// @brief Opens an existing file in read-only mode and maps it into memory.
//
// This function handles opening a file, checking its status, and creating a
// read-only memory-mapped region. It includes checks to ensure
// the path is a file (not a directory) and that the file is not empty.
//
// @param object_id The path to the file to open and map.
// @param[out] out_fd The file descriptor of the opened file.
// @param[out] out_data_size The size of the mapped file in bytes.
// @param[out] out_data_ptr A pointer to the beginning of the memory-mapped
// file data.
// @return absl::OkStatus() on success, or an error status if any step fails
// (e.g., file not found, is a directory, or is empty).
// If the file is a directory, return an error.
if (std::filesystem::is_directory(object_id)) {
return absl::InvalidArgumentError("Path is a directory, not a file: " +
object_id);
}
// Open the file with hardcoded read-only flags (`O_RDONLY`).
int fd = open(object_id.c_str(), O_RDONLY);
if (fd == -1) {
return ErrnoToStatus("open() failed for file: " + object_id);
}
// Get the size of the file using fstat.
struct stat sb;
if (fstat(fd, &sb) == -1) {
close(fd);
return ErrnoToStatus("fstat() failed for file: " + object_id);
}
const size_t size = sb.st_size;
// If the file is zero-sized, return an error.
if (size == 0) {
close(fd);
return absl::InvalidArgumentError(
"File cannot be empty, open_file_and_mmap cannot handle zero-sized "
"file: " +
object_id);
}
// Map the file into memory with hardcoded read-only protection (`PROT_READ`).
void* ptr = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
if (ptr == MAP_FAILED) {
close(fd);
return ErrnoToStatus("mmap() failed for file: " + object_id);
}
// On success, set kernel memory hints:
// MADV_WILLNEED: expect access in the near future, so can prefetch
// pages into memory in the background.
// MADV_HUGEPAGE: use huge pages when possible, to minimize TLB misses and
// improve read throughput.
madvise(ptr, size, MADV_WILLNEED | MADV_HUGEPAGE);
// On success, set all the output parameters.
out_fd = fd;
out_data_size = size;
out_data_ptr = ptr;
LOG(INFO) << "Successfully opened and mmapped file for object '" << object_id
<< "'. data_ptr=" << out_data_ptr << ", size=" << out_data_size;
return absl::OkStatus();
}
absl::Status unmap_and_close(int fd, void* data_ptr, size_t data_size,
std::optional<size_t> truncate_size) {
// @brief Unmaps a memory region, optionally truncates the file, and closes
// the file descriptor.
//
// This function serves as a robust cleanup utility. It attempts to perform
// all specified cleanup operations (unmap, truncate, close) even if some of
// them fail. All errors encountered are collected and returned together in a
// single status, ensuring that a single failure doesn't prevent other cleanup
// steps.
//
// @param fd The file descriptor to close. It is safe to pass -1 if the file
// was not successfully opened.
// @param data_ptr The starting address of the memory region to unmap. It is
// safe to pass MAP_FAILED if the mapping failed.
// @param data_size The size of the memory region to unmap. This should be
// greater than 0 if data_ptr is valid.
// @param truncate_size An optional new size for the file. If this value is
// provided, the function will attempt to truncate the
// file to this size before closing it. This parameter
// should only be used for file descriptors opened in
// read-write mode; providing it for a read-only file
// descriptor will cause the operation to fail.
// @return Returns absl::OkStatus() on complete success. Otherwise, returns an
// absl::InternalError that aggregates all error messages from the
// failed operations, joined by a semicolon.
std::vector<std::string> errors;
// --- Parameter Validation ---
// This section checks for logically inconsistent arguments that likely
// indicate a programming error on the caller's side. It helps catch bugs
// early.
if (fd == -1 && truncate_size.has_value()) {
errors.push_back(
"Programming error: truncate was requested, but the file descriptor is "
"invalid.");
}
if (data_ptr != MAP_FAILED && data_size == 0) {
errors.push_back(
"Programming error: unmap_and_close called with a valid data_ptr but a "
"data_size of 0.");
}
// --- Core Cleanup Logic ---
// Unmap the memory region only if the pointer is valid and the size is
// positive.
if (data_ptr != MAP_FAILED && data_size > 0) {
if (munmap(data_ptr, data_size) == -1) {
// If munmap fails, record the specific system error.
errors.push_back(
absl::StrCat("munmap() failed: ", GetErrnoString(errno)));
}
}
// Perform file operations only if the file descriptor is valid.
// This avoids repeated `fd != -1` checks.
if (fd != -1) {
// If a truncate size is provided, attempt to truncate the file.
if (truncate_size.has_value() &&
ftruncate(fd, truncate_size.value()) == -1) {
errors.push_back(
absl::StrCat("ftruncate() failed: ", GetErrnoString(errno)));
}
if (close(fd) == -1) {
errors.push_back(absl::StrCat("close() failed: ", GetErrnoString(errno)));
}
}
// --- Final Error Reporting ---
// If any errors were collected during the process, return a single status
// containing a semicolon-separated list of all the error messages.
if (!errors.empty()) {
std::string error_message = absl::StrCat("Errors during unmap_and_close: ",
absl::StrJoin(errors, "; "));
LOG(ERROR) << error_message;
return absl::InternalError(error_message);
}
LOG(INFO) << "Successfully unmapped memory and closed fd=" << fd
<< (truncate_size.has_value()
? absl::StrCat(", truncated to size=", *truncate_size)
: "");
return absl::OkStatus();
}
} // namespace
// ml_flashpoint::checkpoint_object_manager::buffer_object::internal