forked from bloomberg/comdb2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathattr.c
308 lines (274 loc) · 10.6 KB
/
attr.c
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
/*
Copyright 2015, 2017 Bloomberg Finance L.P.
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 <assert.h>
#include <pthread.h>
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/socketvar.h>
#include <sys/uio.h>
#include <unistd.h>
#include <stddef.h>
#include <build/db.h>
// #include <peutil.h> /* for time_epoch() */
#include <ctrace.h>
#include <net.h>
#include "bdb_int.h"
#include "locks.h"
#include <logmsg.h>
static void bdb_attr_set_int(bdb_state_type *bdb_state, bdb_attr_type *bdb_attr,
int attr, int value)
{
/* Overrides for special cases */
switch (attr) {
case BDB_ATTR_BLOBSTRIPE:
if (value)
bdb_attr->blobstripe = bdb_attr->dtastripe;
else
bdb_attr->blobstripe = 0;
return;
case BDB_ATTR_COMMITDELAY:
/* set delay */
if (value > bdb_attr->commitdelaymax) {
logmsg(LOGMSG_USER, "Capping delay to commitdelaymax of %d ms\n",
bdb_attr->commitdelaymax);
bdb_attr->commitdelay = bdb_attr->commitdelaymax;
} else
bdb_attr->commitdelay = value;
return;
case BDB_ATTR_REP_WORKERS:
bdb_attr->rep_workers = value;
if (bdb_state && bdb_state->dbenv)
bdb_state->dbenv->set_num_recovery_worker_threads(bdb_state->dbenv,
value);
return;
case BDB_ATTR_REP_PROCESSORS:
bdb_attr->rep_processors = value;
if (bdb_state && bdb_state->dbenv)
bdb_state->dbenv->set_num_recovery_processor_threads(
bdb_state->dbenv, value);
return;
case BDB_ATTR_REP_MEMSIZE:
bdb_attr->rep_memsize = value;
if (bdb_state && bdb_state->dbenv)
bdb_state->dbenv->set_recovery_memsize(bdb_state->dbenv, value);
case BDB_ATTR_PAGE_EXTENT_SIZE:
bdb_attr->page_extent_size = value;
if (bdb_state && bdb_state->dbenv)
bdb_state->dbenv->set_page_extent_size(bdb_state->dbenv, value);
return;
case BDB_ATTR_TRACK_REPLICATION_TIMES:
bdb_attr->track_replication_times = value;
if (bdb_state && value == 0)
bdb_disable_replication_time_tracking(bdb_state);
break;
}
#define DEF_ATTR(NAME, name, type, dflt, desc) \
case BDB_ATTR_##NAME: \
bdb_attr->name = value; \
break;
#define DEF_ATTR_2(NAME, name, type, dflt, desc, flags, verify_fn, update_fn) \
case BDB_ATTR_##NAME: bdb_attr->name = value; break;
switch (attr) {
#include "attr.h"
default:
logmsg(LOGMSG_ERROR, "%s: unknown attribute %d\n", __func__, attr);
break;
}
#undef DEF_ATTR
#undef DEF_ATTR_2
if (attr == BDB_ATTR_REPLIMIT) {
if (bdb_state) {
if (bdb_state->dbenv) {
int rc;
rc = bdb_state->dbenv->set_rep_limit(bdb_state->dbenv, 0,
bdb_attr->replimit);
if (rc)
logmsg(LOGMSG_ERROR, "%s:set_rep_limit: %d %s\n", __func__,
rc, bdb_strerror(rc));
else
logmsg(
LOGMSG_USER,
"dbenv->set_rep_limit called with new rep limit %d\n",
bdb_attr->replimit);
}
} else
logmsg(
LOGMSG_ERROR,
"%s: BDB_ATTR_REPLIMIT changed but environment not updated\n",
__func__);
} else if (attr == BDB_ATTR_REPMETHODMAXSLEEP) {
/* echo the attribute change into the berkdb layer
* (gbl_rep_method_max_sleep_cnt is defined in rep/rep_method.c) */
extern int gbl_rep_method_max_sleep_cnt;
gbl_rep_method_max_sleep_cnt = bdb_attr->repmethodmaxsleep;
}
}
void bdb_attr_set(bdb_attr_type *bdb_attr, int attr, int value)
{
bdb_attr_set_int(NULL, bdb_attr, attr, value);
}
int bdb_attr_set_by_name(bdb_state_type *bdb_handle, bdb_attr_type *bdb_attr,
const char *attrname, int value)
{
#define DEF_ATTR(NAME, name, type, dflt, desc) \
else if (strcasecmp(attrname, #NAME) == 0) \
{ \
bdb_attr_set_int(bdb_handle, bdb_attr, BDB_ATTR_##NAME, value); \
}
#define DEF_ATTR_2(NAME, name, type, dflt, desc, flags, verify_fn, update_fn) \
else if (strcasecmp(attrname, #NAME) == 0) \
{ \
bdb_attr_set_int(bdb_handle, bdb_attr, BDB_ATTR_##NAME, value); \
}
if (!attrname) {
logmsg(LOGMSG_ERROR, "%s: attrname is NULL\n", __func__);
return -1;
}
#include "attr.h"
else {
logmsg(LOGMSG_ERROR, "%s: unknown attribute %s\n", __func__, attrname);
return -1;
}
#undef DEF_ATTR
#undef DEF_ATTR_2
return 0;
}
int bdb_attr_get(bdb_attr_type *bdb_attr, int attr)
{
#define DEF_ATTR(NAME, name, type, dflt, desc) \
case BDB_ATTR_##NAME: \
return bdb_attr->name;
#define DEF_ATTR_2(NAME, name, type, dflt, desc, flags, verify_fn, update_fn) \
case BDB_ATTR_##NAME: return bdb_attr->name;
switch (attr) {
#include "attr.h"
default:
logmsg(LOGMSG_ERROR, "%s: unknown attribute %d\n", __func__, attr);
return -1;
}
#undef DEF_ATTR
#undef DEF_ATTR_2
}
static const char *bdb_attr_units(int type)
{
switch (type) {
case BDB_ATTRTYPE_SECS: return " secs";
case BDB_ATTRTYPE_MSECS: return " msecs";
case BDB_ATTRTYPE_USECS: return " usecs";
case BDB_ATTRTYPE_BYTES: return " bytes";
case BDB_ATTRTYPE_KBYTES: return " kbytes";
case BDB_ATTRTYPE_MBYTES: return " megabytes";
case BDB_ATTRTYPE_BOOLEAN: return " (boolean)";
case BDB_ATTRTYPE_QUANTITY: return "";
case BDB_ATTRTYPE_PERCENT: return "%";
default: return " (unknown type?!)";
}
}
void bdb_attr_dump(FILE *fh, const bdb_attr_type *bdb_attr)
{
#define DEF_ATTR(NAME, name, type, dflt, desc) \
logmsg(LOGMSG_USER, "%-20s = %d%s\n", #NAME, bdb_attr->name, \
bdb_attr_units(BDB_ATTRTYPE_##type));
#define DEF_ATTR_2(NAME, name, type, dflt, desc, flags, verify_fn, update_fn) \
logmsg(LOGMSG_USER, "%-20s = %d%s\n", #NAME, bdb_attr->name, \
bdb_attr_units(BDB_ATTRTYPE_##type));
#include "attr.h"
#undef DEF_ATTR
#undef DEF_ATTR_2
}
static inline comdb2_tunable_type bdb_to_tunable_type(int type)
{
switch (type) {
case BDB_ATTRTYPE_SECS:
case BDB_ATTRTYPE_MSECS:
case BDB_ATTRTYPE_USECS:
case BDB_ATTRTYPE_BYTES:
case BDB_ATTRTYPE_KBYTES:
case BDB_ATTRTYPE_MBYTES:
case BDB_ATTRTYPE_QUANTITY:
case BDB_ATTRTYPE_PERCENT: return TUNABLE_INTEGER;
case BDB_ATTRTYPE_BOOLEAN: return TUNABLE_BOOLEAN;
default: assert(0);
}
return TUNABLE_INVALID;
}
static inline int bdb_to_tunable_flag(int type)
{
switch (type) {
case BDB_ATTRTYPE_SECS:
case BDB_ATTRTYPE_MSECS:
case BDB_ATTRTYPE_USECS:
case BDB_ATTRTYPE_BYTES:
case BDB_ATTRTYPE_KBYTES:
case BDB_ATTRTYPE_MBYTES:
case BDB_ATTRTYPE_QUANTITY:
case BDB_ATTRTYPE_PERCENT: return 0;
case BDB_ATTRTYPE_BOOLEAN: return NOARG;
default: assert(0);
}
return TUNABLE_INVALID;
}
void *bdb_attr_create(void)
{
bdb_attr_type *bdb_attr;
if (!(bdb_attr = mymalloc(sizeof(bdb_attr_type)))) {
logmsg(LOGMSG_ERROR, "%s:%d Out-of-memory", __FILE__, __LINE__);
return NULL;
}
memset(bdb_attr, 0, sizeof(bdb_attr_type));
#define DEF_ATTR(NAME, name, type, dflt, desc) \
bdb_attr->name = (dflt); \
REGISTER_TUNABLE(#NAME, desc, bdb_to_tunable_type(BDB_ATTRTYPE_##type), \
&bdb_attr->name, \
bdb_to_tunable_flag(BDB_ATTRTYPE_##type), NULL, NULL, \
NULL, NULL);
#define DEF_ATTR_2(NAME, name, type, dflt, desc, flags, verify_fn, update_fn) \
bdb_attr->name = (dflt); \
REGISTER_TUNABLE(#NAME, desc, bdb_to_tunable_type(BDB_ATTRTYPE_##type), \
&bdb_attr->name, \
bdb_to_tunable_flag(BDB_ATTRTYPE_##type) | flags, NULL, \
verify_fn, update_fn, NULL);
#include "attr.h"
#undef DEF_ATTR
#undef DEF_ATTR_2
REGISTER_TUNABLE("disable_pageorder_recsz_check",
"If set, allow page-order table scans even for larger "
"record sizes where they don't necessarily lead to "
"improvement.",
TUNABLE_BOOLEAN, &bdb_attr->disable_pageorder_recsz_chk,
NOARG, NULL, NULL, NULL, NULL);
REGISTER_TUNABLE("enable_pageorder_recsz_check",
"Disables 'disable_pageorder_recsz_chk'", TUNABLE_BOOLEAN,
&bdb_attr->disable_pageorder_recsz_chk,
INVERSE_VALUE | NOARG, NULL, NULL, NULL, NULL);
REGISTER_TUNABLE("nochecksums", "Disables 'checksums'", TUNABLE_BOOLEAN,
&bdb_attr->checksums, INVERSE_VALUE | NOARG, NULL, NULL,
NULL, NULL);
/* echo our default attribute setting into the berkdb library */
bdb_attr_set(bdb_attr, BDB_ATTR_REPMETHODMAXSLEEP,
bdb_attr->repmethodmaxsleep);
listc_init(&bdb_attr->deferred_berkdb_options,
offsetof(struct deferred_berkdb_option, lnk));
return bdb_attr;
}