-
Notifications
You must be signed in to change notification settings - Fork 0
/
gen_config_vars.py
488 lines (440 loc) · 14 KB
/
gen_config_vars.py
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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
#!/usr/bin/env python3
# Copyright 2023 gRPC authors.
#
# 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.
from __future__ import print_function
import binascii
import collections
import ctypes
import datetime
import json
import math
import os
import re
import sys
import yaml
with open("src/core/lib/config/config_vars.yaml") as f:
attrs = yaml.safe_load(f.read())
error = False
today = datetime.date.today()
two_quarters_from_now = today + datetime.timedelta(days=180)
for attr in attrs:
if "name" not in attr:
print("config has no name: %r" % attr)
error = True
continue
if "experiment" in attr["name"] and attr["name"] != "experiments":
print("use experiment system for experiments")
error = True
if "description" not in attr:
print("no description for %s" % attr["name"])
error = True
if "default" not in attr:
print("no default for %s" % attr["name"])
error = True
if error:
sys.exit(1)
def c_str(s, encoding="ascii"):
if s is None:
return '""'
if isinstance(s, str):
s = s.encode(encoding)
result = ""
for c in s:
c = chr(c) if isinstance(c, int) else c
if not (32 <= ord(c) < 127) or c in ("\\", '"'):
result += "\\%03o" % ord(c)
else:
result += c
return '"' + result + '"'
def snake_to_pascal(s):
return "".join(x.capitalize() for x in s.split("_"))
# utility: print a big comment block into a set of files
def put_banner(files, banner):
for f in files:
for line in banner:
print("// %s" % line, file=f)
print(file=f)
def put_copyright(file):
# copy-paste copyright notice from this file
with open(sys.argv[0]) as my_source:
copyright = []
for line in my_source:
if line[0] != "#":
break
for line in my_source:
if line[0] == "#":
copyright.append(line)
break
for line in my_source:
if line[0] != "#":
break
copyright.append(line)
put_banner([file], [line[2:].rstrip() for line in copyright])
RETURN_TYPE = {
"int": "int32_t",
"string": "absl::string_view",
"comma_separated_string": "absl::string_view",
"bool": "bool",
}
MEMBER_TYPE = {
"int": "int32_t",
"string": "std::string",
"comma_separated_string": "std::string",
"bool": "bool",
}
FLAG_TYPE = {
"int": "absl::optional<int32_t>",
"string": "absl::optional<std::string>",
"comma_separated_string": "std::vector<std::string>",
"bool": "absl::optional<bool>",
}
PROTO_TYPE = {
"int": "int32",
"string": "string",
"comma_separated_string": "string",
"bool": "bool",
}
SORT_ORDER_FOR_PACKING = {
"int": 0,
"bool": 1,
"string": 2,
"comma_separated_string": 3,
}
def bool_default_value(x, name):
if x == True:
return "true"
if x == False:
return "false"
if isinstance(x, str) and x.startswith("$"):
return x[1:]
return x
def int_default_value(x, name):
if isinstance(x, str) and x.startswith("$"):
return x[1:]
return x
def string_default_value(x, name):
if x is None:
return '""'
if x.startswith("$"):
return x[1:]
return c_str(x)
DEFAULT_VALUE = {
"int": int_default_value,
"bool": bool_default_value,
"string": string_default_value,
"comma_separated_string": string_default_value,
}
TO_STRING = {
"int": "$",
"bool": '$?"true":"false"',
"string": '"\\"", absl::CEscape($), "\\""',
"comma_separated_string": '"\\"", absl::CEscape($), "\\""',
}
attrs_in_packing_order = sorted(
attrs, key=lambda a: SORT_ORDER_FOR_PACKING[a["type"]]
)
with open("test/core/util/fuzz_config_vars.proto", "w") as P:
put_copyright(P)
put_banner(
[P],
[
"",
"Automatically generated by tools/codegen/core/gen_config_vars.py",
"",
],
)
print('syntax = "proto3";', file=P)
print(file=P)
print("package grpc.testing;", file=P)
print(file=P)
print("message FuzzConfigVars {", file=P)
for attr in attrs_in_packing_order:
if attr.get("fuzz", False) == False:
continue
print(
" optional %s %s = %d;"
% (
attr.get("fuzz_type", PROTO_TYPE[attr["type"]]),
attr["name"],
binascii.crc32(attr["name"].encode("ascii")) & 0x1FFFFFFF,
),
file=P,
)
print("};", file=P)
with open("test/core/util/fuzz_config_vars.h", "w") as H:
put_copyright(H)
put_banner(
[H],
[
"",
"Automatically generated by tools/codegen/core/gen_config_vars.py",
"",
],
)
print("#ifndef GRPC_TEST_CORE_UTIL_FUZZ_CONFIG_VARS_H", file=H)
print("#define GRPC_TEST_CORE_UTIL_FUZZ_CONFIG_VARS_H", file=H)
print(file=H)
print("#include <grpc/support/port_platform.h>", file=H)
print(file=H)
print('#include "test/core/util/fuzz_config_vars.pb.h"', file=H)
print('#include "src/core/lib/config/config_vars.h"', file=H)
print(file=H)
print("namespace grpc_core {", file=H)
print(file=H)
print(
(
"ConfigVars::Overrides OverridesFromFuzzConfigVars(const"
" grpc::testing::FuzzConfigVars& vars);"
),
file=H,
)
print(
"void ApplyFuzzConfigVars(const grpc::testing::FuzzConfigVars& vars);",
file=H,
)
print(file=H)
print("} // namespace grpc_core", file=H)
print(file=H)
print("#endif // GRPC_TEST_CORE_UTIL_FUZZ_CONFIG_VARS_H", file=H)
with open("test/core/util/fuzz_config_vars.cc", "w") as C:
put_copyright(C)
put_banner(
[C],
[
"",
"Automatically generated by tools/codegen/core/gen_config_vars.py",
"",
],
)
print('#include "test/core/util/fuzz_config_vars.h"', file=C)
print('#include "test/core/util/fuzz_config_vars_helpers.h"', file=C)
print(file=C)
print("namespace grpc_core {", file=C)
print(file=C)
print(
(
"ConfigVars::Overrides OverridesFromFuzzConfigVars(const"
" grpc::testing::FuzzConfigVars& vars) {"
),
file=C,
)
print(" ConfigVars::Overrides overrides;", file=C)
for attr in attrs_in_packing_order:
fuzz = attr.get("fuzz", False)
if not fuzz:
continue
print(" if (vars.has_%s()) {" % attr["name"], file=C)
if isinstance(fuzz, str):
print(
" overrides.%s = %s(vars.%s());"
% (attr["name"], fuzz, attr["name"]),
file=C,
)
else:
print(
" overrides.%s = vars.%s();" % (attr["name"], attr["name"]),
file=C,
)
print(" }", file=C)
print(" return overrides;", file=C)
print("}", file=C)
print(
"void ApplyFuzzConfigVars(const grpc::testing::FuzzConfigVars& vars) {",
file=C,
)
print(
" ConfigVars::SetOverrides(OverridesFromFuzzConfigVars(vars));", file=C
)
print("}", file=C)
print(file=C)
print("} // namespace grpc_core", file=C)
with open("src/core/lib/config/config_vars.h", "w") as H:
put_copyright(H)
put_banner(
[H],
[
"",
"Automatically generated by tools/codegen/core/gen_config_vars.py",
"",
],
)
print("#ifndef GRPC_SRC_CORE_LIB_CONFIG_CONFIG_VARS_H", file=H)
print("#define GRPC_SRC_CORE_LIB_CONFIG_CONFIG_VARS_H", file=H)
print(file=H)
print("#include <grpc/support/port_platform.h>", file=H)
print(file=H)
print("#include <string>", file=H)
print("#include <atomic>", file=H)
print("#include <stdint.h>", file=H)
print('#include "absl/strings/string_view.h"', file=H)
print('#include "absl/types/optional.h"', file=H)
print(file=H)
print("namespace grpc_core {", file=H)
print(file=H)
print("class GPR_DLL ConfigVars {", file=H)
print(" public:", file=H)
print(" struct Overrides {", file=H)
for attr in attrs_in_packing_order:
print(
" absl::optional<%s> %s;"
% (MEMBER_TYPE[attr["type"]], attr["name"]),
file=H,
)
print(" };", file=H)
print(" ConfigVars(const ConfigVars&) = delete;", file=H)
print(" ConfigVars& operator=(const ConfigVars&) = delete;", file=H)
print(
" // Get the core configuration; if it does not exist, create it.",
file=H,
)
print(" static const ConfigVars& Get() {", file=H)
print(" auto* p = config_vars_.load(std::memory_order_acquire);", file=H)
print(" if (p != nullptr) return *p;", file=H)
print(" return Load();", file=H)
print(" }", file=H)
print(" static void SetOverrides(const Overrides& overrides);", file=H)
print(
" // Drop the config vars. Users must ensure no other threads are",
file=H,
)
print(" // accessing the configuration.", file=H)
print(" static void Reset();", file=H)
print(" std::string ToString() const;", file=H)
for attr in attrs:
for line in attr["description"].splitlines():
print(" // %s" % line, file=H)
if attr.get("force-load-on-access", False):
print(
" %s %s() const;"
% (MEMBER_TYPE[attr["type"]], snake_to_pascal(attr["name"])),
file=H,
)
else:
print(
" %s %s() const { return %s_; }"
% (
RETURN_TYPE[attr["type"]],
snake_to_pascal(attr["name"]),
attr["name"],
),
file=H,
)
print(" private:", file=H)
print(" explicit ConfigVars(const Overrides& overrides);", file=H)
print(" static const ConfigVars& Load();", file=H)
print(" static std::atomic<ConfigVars*> config_vars_;", file=H)
for attr in attrs_in_packing_order:
if attr.get("force-load-on-access", False):
continue
print(" %s %s_;" % (MEMBER_TYPE[attr["type"]], attr["name"]), file=H)
for attr in attrs_in_packing_order:
if attr.get("force-load-on-access", False) == False:
continue
print(
" absl::optional<%s> override_%s_;"
% (MEMBER_TYPE[attr["type"]], attr["name"]),
file=H,
)
print("};", file=H)
print(file=H)
print("} // namespace grpc_core", file=H)
print(file=H)
print("#endif // GRPC_SRC_CORE_LIB_CONFIG_CONFIG_VARS_H", file=H)
with open("src/core/lib/config/config_vars.cc", "w") as C:
put_copyright(C)
put_banner(
[C],
[
"",
"Automatically generated by tools/codegen/core/gen_config_vars.py",
"",
],
)
print("#include <grpc/support/port_platform.h>", file=C)
print('#include "src/core/lib/config/config_vars.h"', file=C)
print('#include "src/core/lib/config/load_config.h"', file=C)
print('#include "absl/strings/escaping.h"', file=C)
print('#include "absl/flags/flag.h"', file=C)
print(file=C)
for attr in attrs:
if "prelude" in attr:
print(attr["prelude"], file=C)
for attr in attrs:
print(
"ABSL_FLAG(%s, %s, {}, %s);"
% (
FLAG_TYPE[attr["type"]],
"grpc_" + attr["name"],
c_str(attr["description"]),
),
file=C,
)
print(file=C)
print("namespace grpc_core {", file=C)
print(file=C)
print("ConfigVars::ConfigVars(const Overrides& overrides) :", file=C)
initializers = [
'%s_(LoadConfig(FLAGS_grpc_%s, "GRPC_%s", overrides.%s, %s))'
% (
attr["name"],
attr["name"],
attr["name"].upper(),
attr["name"],
DEFAULT_VALUE[attr["type"]](attr["default"], attr["name"]),
)
for attr in attrs_in_packing_order
if attr.get("force-load-on-access", False) == False
]
initializers += [
"override_%s_(overrides.%s)" % (attr["name"], attr["name"])
for attr in attrs_in_packing_order
if attr.get("force-load-on-access", False)
]
print(",".join(initializers), file=C)
print("{}", file=C)
print(file=C)
for attr in attrs:
if attr.get("force-load-on-access", False):
print(
"%s ConfigVars::%s() const { return LoadConfig(FLAGS_grpc_%s,"
' "GRPC_%s", override_%s_, %s); }'
% (
MEMBER_TYPE[attr["type"]],
snake_to_pascal(attr["name"]),
attr["name"],
attr["name"].upper(),
attr["name"],
DEFAULT_VALUE[attr["type"]](attr["default"], attr["name"]),
),
file=C,
)
print(file=C)
print("std::string ConfigVars::ToString() const {", file=C)
print(" return absl::StrCat(", file=C)
for i, attr in enumerate(attrs):
if i:
print(",", file=C)
print(c_str(", " + attr["name"] + ": "), file=C)
else:
print(c_str(attr["name"] + ": "), file=C)
print(
",",
TO_STRING[attr["type"]].replace(
"$", snake_to_pascal(attr["name"]) + "()"
),
file=C,
)
print(");}", file=C)
print(file=C)
print("}", file=C)