-
Notifications
You must be signed in to change notification settings - Fork 149
/
Copy pathmy_file.cc
285 lines (239 loc) · 8.23 KB
/
my_file.cc
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
/* Copyright (c) 2000, 2024, Oracle and/or its affiliates.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2.0,
as published by the Free Software Foundation.
This program is designed to work with certain software (including
but not limited to OpenSSL) that is licensed under separate terms,
as designated in a particular file or component or in included license
documentation. The authors of MySQL hereby grant you an additional
permission to link the program and your derivative works with the
separately licensed software that they have either included with
the program or referenced in the documentation.
Without limiting anything contained in the foregoing, this file,
which is part of C Driver for MySQL (Connector/C), is also subject to the
Universal FOSS Exception, version 1.0, a copy of which can be found at
http://oss.oracle.com/licenses/universal-foss-exception.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License, version 2.0, for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
/**
@file mysys/my_file.cc
*/
#include "my_config.h"
#include <string.h>
#include <sys/types.h>
#include <algorithm>
#include <iostream>
#include <limits>
#include <thread>
#include <vector>
#include "sql/malloc_allocator.h"
#include "mutex_lock.h" // MUTEX_LOCK
#include "my_dbug.h"
#include "my_inttypes.h"
#include "my_io.h"
#include "my_macros.h"
#include "my_sys.h"
#include "mysql/psi/mysql_mutex.h"
#include "mysql/service_mysql_alloc.h"
#include "mysys/my_static.h"
#include "mysys/mysys_priv.h"
#include "sql/malloc_allocator.h"
#include "sql/stateless_allocator.h"
#ifdef HAVE_SYS_RESOURCE_H
#include <sys/resource.h> /* RLIMIT_NOFILE */
#endif
namespace {
/**
Set the OS limit on the number of open files. On POSIX systems this
calls setrlimit(RLIMIT_NOFILE, ...). On Windows there is no
corresponding api so the requested value is returned. The assumption
being that the request will never be larger than OS_FILE_LIMIT, @see
my_set_max_open_files.
@param max_file_limit Files to open
@note The request may not fulfilled because of system limitations
@return Files available to open. May be more or less than max_file_limit!
*/
uint SetOsLimitMaxOpenFiles(uint max_file_limit) {
DBUG_TRACE;
#ifndef _WIN32
rlimit existing;
if (getrlimit(RLIMIT_NOFILE, &existing) == -1) {
DBUG_PRINT("warning", ("getrlimit(RLIMIT_NOFILE) failed: %s (%d)",
strerror(errno), errno));
return max_file_limit;
}
// If rlim_cur is larger than what is requested, we use that
// instead, but capped to the largest value an uint can hold,
// (rlim_t can be 64 bit).
if (existing.rlim_cur >= max_file_limit) {
constexpr const rlim_t uim = std::numeric_limits<uint>::max();
return std::min(existing.rlim_cur, uim);
}
// Attempt to modify OS setting
rlimit request;
request.rlim_cur = max_file_limit;
request.rlim_max = max_file_limit;
if (setrlimit(RLIMIT_NOFILE, &request) == -1) {
DBUG_PRINT("warning", ("setrlimit(RLIMIT_NOFILE)=%u failed: %s (%d)",
max_file_limit, strerror(errno), errno));
return existing.rlim_cur; /* Use original value */
}
#ifndef NDEBUG
// Read back new value to check "what we got". Seems overly
// pessimistic to assume that a successful setrlimit did not
// actually set the requested values.
rlimit readback;
if (getrlimit(RLIMIT_NOFILE, &readback) == -1) {
DBUG_PRINT("warning",
("getrlimit(RLIMIT_NOFILE) (after set) failed: %s (%d)",
strerror(errno), errno));
return max_file_limit;
}
assert(readback.rlim_cur == request.rlim_cur &&
readback.rlim_max == request.rlim_max);
#endif /* NDEBUG */
return request.rlim_cur;
#else /* not defined(_WIN32) */
// We don't know the limit.
assert(max_file_limit <= OS_FILE_LIMIT);
return max_file_limit;
#endif /* not defined _WIN32 */
}
/**
Rule of 5 class.
@see https://en.cppreference.com/w/cpp/language/rule_of_three
@see https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Rc-five
*/
class FileInfo {
const char *m_name = nullptr;
file_info::OpenType m_type = file_info::OpenType::UNOPEN;
public:
FileInfo() = default;
FileInfo(const char *n, file_info::OpenType t)
: m_name{my_strdup(key_memory_my_file_info, n,
MYF(MY_WME | ME_FATALERROR))},
m_type{t} {}
// Rule of 5 (2)
FileInfo(const FileInfo &) = delete;
// Rule of 5 (4)
FileInfo(FileInfo &&src) noexcept
: m_name{std::exchange(src.m_name, nullptr)},
m_type{std::exchange(src.m_type, file_info::OpenType::UNOPEN)} {}
// Rule of 5 (1)
~FileInfo() { my_free(const_cast<char *>(m_name)); }
// Rule of 5 (3)
FileInfo &operator=(const FileInfo &) = delete;
// Rule of 5 (5)
FileInfo &operator=(FileInfo &&src) {
FileInfo tmp{std::move(src)};
Swap(&tmp);
return *this;
}
// Member swap for move assignment.
void Swap(FileInfo *src) noexcept {
std::swap(m_type, src->m_type);
std::swap(m_name, src->m_name);
}
const char *name() const { return m_name; }
file_info::OpenType type() const { return m_type; }
};
using FileInfoAllocator = Malloc_allocator<FileInfo>;
using FileInfoVector = std::vector<FileInfo, FileInfoAllocator>;
FileInfoVector *fivp = nullptr;
} // namespace
namespace file_info {
/**
Add FileInfo entry for file descriptor. Increments status variable
for open files/streams.
@relates file_info::RegisterFilename
@param fd file descriptor
@param file_name name of file
@param type_of_file tag indicating how the fd was created
*/
void RegisterFilename(File fd, const char *file_name, OpenType type_of_file) {
assert(fd > -1);
FileInfoVector &fiv = *fivp;
MUTEX_LOCK(g, &THR_LOCK_open);
if (static_cast<size_t>(fd) >= fiv.size()) {
fiv.resize(fd + 1);
}
CountFileOpen(fiv[fd].type(), type_of_file);
fiv[fd] = {file_name, type_of_file};
dbug("fileinfo", [&]() {
std::cerr << "Registering (" << fd << ", '" << file_name << ")"
<< std::endl;
});
}
/**
Remove FileInfo entry for file descriptor. Decrements status
variables for open files/streams.
@relates file_info::UnregisterFilename
@param fd file descriptor
*/
void UnregisterFilename(File fd) {
FileInfoVector &fiv = *fivp;
MUTEX_LOCK(g, &THR_LOCK_open);
if (static_cast<size_t>(fd) >= fiv.size()) {
dbug("fileinfo", [&]() {
std::cerr << "Un-registering unknown fd:" << fd << "!" << std::endl;
});
return;
}
if (fiv[fd].type() == OpenType::UNOPEN) {
dbug("fileinfo", [&]() {
std::cerr << "Un-registering already UNOPEN fd:" << fd << std::endl;
});
return;
}
CountFileClose(fiv[fd].type());
dbug("fileinfo", [&]() {
std::cerr << "Un-registering (" << fd << ", '" << fiv[fd].name() << "')"
<< std::endl;
});
fiv[fd] = {};
}
} // namespace file_info
/**
Get filename of file.
@param fd file descriptor
@return file name in file_info object
*/
const char *my_filename(File fd) {
DBUG_TRACE;
const FileInfoVector &fiv = *fivp;
MUTEX_LOCK(g, &THR_LOCK_open);
if (fd < 0 || fd >= static_cast<int>(fiv.size())) {
return "<fd out of range>";
}
const FileInfo &fi = fiv[fd];
if (fi.type() == file_info::OpenType::UNOPEN) {
return "<unopen fd>";
}
return fi.name();
}
/**
Sets the OS limit on the number of open files (if supported).
@param files Number of requested files
@return The actual new OS limit which may be both more or less than
what was requested.
*/
uint my_set_max_open_files(uint files) {
DBUG_TRACE;
return SetOsLimitMaxOpenFiles(std::min(files + MY_FILE_MIN, OS_FILE_LIMIT));
}
/**
Constructs static objects.
*/
void MyFileInit() {
fivp =
new FileInfoVector(Malloc_allocator<FileInfo>{key_memory_my_file_info});
}
/**
Destroys static objects.
*/
void MyFileEnd() { delete fivp; }