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
|
// Copyright Epic Games, Inc. All Rights Reserved.
// Zen command line client utility
//
#include "zenmaster.h"
#include <zencore/callstack.h>
#include <zencore/filesystem.h>
#include <zencore/fmtutils.h>
#include <zencore/logging.h>
#include <zencore/process.h>
#include <zencore/scopeguard.h>
#include <zencore/sentryintegration.h>
#include <zencore/string.h>
#include <zencore/trace.h>
#include <zencore/windows.h>
#include <zenhttp/httpcommon.h>
#include <zenutil/environmentoptions.h>
#include <zenutil/logging.h>
#include <zenutil/workerpools.h>
#include <zenutil/zenserverprocess.h>
#include <zencore/memory/fmalloc.h>
#include <zencore/memory/llm.h>
#include <zencore/memory/memory.h>
#include <zencore/memory/memorytrace.h>
#include <zencore/memory/newdelete.h>
#if ZEN_WITH_TESTS
# define ZEN_TEST_WITH_RUNNER 1
# include <zencore/testing.h>
#endif
ZEN_THIRD_PARTY_INCLUDES_START
#include <spdlog/sinks/ansicolor_sink.h>
#include <spdlog/spdlog.h>
#include <gsl/gsl-lite.hpp>
ZEN_THIRD_PARTY_INCLUDES_END
#include <zencore/memory/newdelete.h>
//////////////////////////////////////////////////////////////////////////
#if ZEN_PLATFORM_WINDOWS
# include <conio.h>
#else
# include <sys/ioctl.h>
# include <unistd.h>
# include <termios.h>
#endif
namespace zen {
#if ZEN_PLATFORM_WINDOWS
int
getch(void)
{
return _getch();
}
#else
int
getch(void)
{
char buf = 0;
struct termios old = {0};
fflush(stdout);
if (tcgetattr(0, &old) < 0)
perror("tcsetattr()");
old.c_lflag &= ~ICANON;
old.c_lflag &= ~ECHO;
old.c_cc[VMIN] = 1;
old.c_cc[VTIME] = 0;
if (tcsetattr(0, TCSANOW, &old) < 0)
perror("tcsetattr ICANON");
if (read(0, &buf, 1) < 0)
perror("read()");
old.c_lflag |= ICANON;
old.c_lflag |= ECHO;
if (tcsetattr(0, TCSADRAIN, &old) < 0)
perror("tcsetattr ~ICANON");
return buf;
}
#endif
} // namespace zen
int
main(int argc, char** argv)
{
zen::SetCurrentThreadName("main");
std::vector<std::string> Args;
#if ZEN_PLATFORM_WINDOWS
LPWSTR RawCommandLine = GetCommandLine();
std::string CommandLine = zen::WideToUtf8(RawCommandLine);
Args = zen::ParseCommandLine(CommandLine);
#else
Args.reserve(argc);
for (int I = 0; I < argc; I++)
{
std::string Arg(argv[I]);
if ((!Arg.empty()) && (Arg != " "))
{
Args.emplace_back(std::move(Arg));
}
}
#endif
std::vector<char*> RawArgs = zen::StripCommandlineQuotes(Args);
argc = gsl::narrow<int>(RawArgs.size());
argv = RawArgs.data();
using namespace zen;
using namespace std::literals;
#if ZEN_WITH_TRACE
TraceInit("zen");
TraceOptions TraceCommandlineOptions;
if (GetTraceOptionsFromCommandline(TraceCommandlineOptions))
{
TraceConfigure(TraceCommandlineOptions);
}
#endif // ZEN_WITH_TRACE
// Split command line into options, commands and any pass-through arguments
std::string Passthrough;
std::string PassthroughArgs;
std::vector<std::string> PassthroughArgV;
for (int i = 1; i < argc; ++i)
{
if ("--"sv == argv[i])
{
bool IsFirst = true;
zen::ExtendableStringBuilder<256> Line;
zen::ExtendableStringBuilder<256> Arguments;
for (int j = i + 1; j < argc; ++j)
{
auto AppendAscii = [&](auto X) {
Line.Append(X);
if (!IsFirst)
{
Arguments.Append(X);
}
};
if (!IsFirst)
{
AppendAscii(" ");
}
std::string_view ThisArg(argv[j]);
PassthroughArgV.push_back(std::string(ThisArg));
const bool NeedsQuotes =
(ThisArg.find(' ') != std::string_view::npos) && !(ThisArg.starts_with("\"") && ThisArg.ends_with("\""));
if (NeedsQuotes)
{
AppendAscii("\"");
}
AppendAscii(ThisArg);
if (NeedsQuotes)
{
AppendAscii("\"");
}
IsFirst = false;
}
Passthrough = Line.c_str();
PassthroughArgs = Arguments.c_str();
// This will "truncate" the arg vector and terminate the loop
argc = i;
}
}
// Parse global CLI arguments
ZenMasterCliOptions GlobalOptions;
GlobalOptions.PassthroughCommandLine = Passthrough;
GlobalOptions.PassthroughArgs = PassthroughArgs;
GlobalOptions.PassthroughArgV = PassthroughArgV;
std::string MemoryOptions;
std::string SubCommand = "<None>";
cxxopts::Options Options("zenmaster", "Zen master orchestration tool");
Options.add_options()("d, debug", "Enable debugging", cxxopts::value<bool>(GlobalOptions.IsDebug));
Options.add_options()("v, verbose", "Enable verbose logging", cxxopts::value<bool>(GlobalOptions.IsVerbose));
Options.add_options()("malloc", "Configure memory allocator subsystem", cxxopts::value(MemoryOptions)->default_value("mimalloc"));
Options.add_options()("help", "Show command line help");
int ServerSpawnCount = 100;
Options.add_options()("count", "Number of servers to spawn", cxxopts::value<int>(ServerSpawnCount));
#if ZEN_WITH_TRACE
// We only have this in options for command line help purposes - we parse these argument separately earlier using
// GetTraceOptionsFromCommandline()
Options.add_option("ue-trace",
"",
"trace",
"Specify which trace channels should be enabled",
cxxopts::value<std::string>(TraceCommandlineOptions.Channels)->default_value(""),
"");
Options.add_option("ue-trace",
"",
"tracehost",
"Hostname to send the trace to",
cxxopts::value<std::string>(TraceCommandlineOptions.Host)->default_value(""),
"");
Options.add_option("ue-trace",
"",
"tracefile",
"Path to write a trace to",
cxxopts::value<std::string>(TraceCommandlineOptions.File)->default_value(""),
"");
#endif // ZEN_WITH_TRACE
#if ZEN_USE_SENTRY
SentryIntegration::Config SentryConfig;
bool NoSentry = false;
Options
.add_option("sentry", "", "no-sentry", "Disable Sentry crash handler", cxxopts::value<bool>(NoSentry)->default_value("false"), "");
Options.add_option("sentry",
"",
"sentry-allow-personal-info",
"Allow personally identifiable information in sentry crash reports",
cxxopts::value<bool>(SentryConfig.AllowPII)->default_value("false"),
"");
Options.add_option("sentry", "", "sentry-dsn", "Sentry DSN to send events to", cxxopts::value<std::string>(SentryConfig.Dsn), "");
Options.add_option("sentry", "", "sentry-environment", "Sentry environment", cxxopts::value<std::string>(SentryConfig.Environment), "");
Options.add_options()("sentry-debug", "Enable debug mode for Sentry", cxxopts::value<bool>(SentryConfig.Debug)->default_value("false"));
#endif
try
{
cxxopts::ParseResult ParseResult = Options.parse(argc, argv);
if (ParseResult.count("help"))
{
std::string Help = Options.help();
printf("%s\n", Help.c_str());
exit(0);
}
#if ZEN_USE_SENTRY
{
EnvironmentOptions EnvOptions;
EnvOptions.AddOption("UE_ZEN_SENTRY_DSN"sv, SentryConfig.Dsn, "sentry-dsn"sv);
EnvOptions.AddOption("UE_ZEN_SENTRY_ALLOWPERSONALINFO"sv, SentryConfig.AllowPII, "sentry-allow-personal-info"sv);
EnvOptions.AddOption("UE_ZEN_SENTRY_ENVIRONMENT"sv, SentryConfig.Environment, "sentry-environment"sv);
bool EnvEnableSentry = !NoSentry;
EnvOptions.AddOption("UE_ZEN_SENTRY_ENABLED"sv, EnvEnableSentry, "no-sentry"sv);
EnvOptions.AddOption("UE_ZEN_SENTRY_DEBUG"sv, SentryConfig.Debug, "sentry-debug"sv);
EnvOptions.Parse(ParseResult);
if (EnvEnableSentry != !NoSentry)
{
NoSentry = !EnvEnableSentry;
}
}
SentryIntegration Sentry;
if (NoSentry == false)
{
std::string SentryDatabasePath = (std::filesystem::temp_directory_path() / ".zen-sentry-native").string();
ExtendableStringBuilder<512> SB;
for (int i = 0; i < argc; ++i)
{
if (i)
{
SB.Append(' ');
}
SB.Append(argv[i]);
}
SentryConfig.DatabasePath = SentryDatabasePath;
Sentry.Initialize(SentryConfig, SB.ToString());
SentryIntegration::ClearCaches();
}
#endif
zen::LoggingOptions LogOptions;
LogOptions.IsDebug = GlobalOptions.IsDebug;
LogOptions.IsVerbose = GlobalOptions.IsVerbose;
LogOptions.AllowAsync = false;
zen::InitializeLogging(LogOptions);
std::set_terminate([]() {
void* Frames[8];
uint32_t FrameCount = GetCallstack(2, 8, Frames);
CallstackFrames* Callstack = CreateCallstack(FrameCount, Frames);
ZEN_CRITICAL("Program exited abnormally via std::terminate()\n{}", CallstackToString(Callstack, " "));
FreeCallstack(Callstack);
});
zen::MaximizeOpenFileCount();
//////////////////////////////////////////////////////////////////////////
auto _ = zen::MakeGuard([] {
ShutdownWorkerPools();
ShutdownLogging();
});
// Spawn some zenserver processes
zen::ZenServerEnvironment TestEnv;
std::filesystem::path ProgramBaseDir = GetRunningExecutablePath().parent_path();
std::filesystem::path TestBaseDir = std::filesystem::current_path() / ".test";
const std::string ServerClass;
TestEnv.InitializeForTest(ProgramBaseDir, TestBaseDir, ServerClass);
auto TimedBlock = [&](const std::string_view Tag, auto&& Fun) {
Stopwatch t;
ZEN_INFO("BEGIN {}", Tag);
Fun();
ZEN_INFO("END {}, took {}", Tag, NiceTimeSpanMs(t.GetElapsedTimeMs()));
};
std::vector<std::unique_ptr<ZenServerInstance>> Instances;
TimedBlock(fmt::format("Spawning {} instances", ServerSpawnCount), [&] {
TimedBlock("Spawning instances", [&] {
for (int i = 0; i < ServerSpawnCount; ++i)
{
auto& Instance = Instances.emplace_back(std::make_unique<ZenServerInstance>(TestEnv));
std::filesystem::path TestDir1 = TestEnv.CreateNewTestDir();
Instance->SetTestDir(TestDir1);
Instance->SpawnServer("--malloc=ansi --corelimit=4");
}
});
TimedBlock("Waiting for instances", [&] {
for (int i = 0; i < ServerSpawnCount; ++i)
{
auto& Instance = Instances[i];
const uint16_t PortNum = Instance->WaitUntilReady();
ZEN_INFO("Instance #{} UP - port {}", i, PortNum);
}
});
});
ZEN_INFO("press any key to tear instances down");
zen::getch();
TimedBlock("Shutting down instances", [&] {
for (int i = 0; i < ServerSpawnCount; ++i)
{
auto& Instance = Instances[i];
ZEN_INFO("Shutting down instance #{}...", i);
Instance->Shutdown();
ZEN_INFO("Instance #{} DOWN", i);
}
});
}
catch (const OptionParseException& Ex)
{
std::string HelpMessage = Options.help();
printf("Error parsing program arguments: %s\n\n%s", Ex.what(), HelpMessage.c_str());
return 9;
}
catch (const std::system_error& Ex)
{
printf("System Error: %s\n", Ex.what());
return Ex.code() ? Ex.code().value() : 10;
}
catch (const std::exception& Ex)
{
printf("Error: %s\n", Ex.what());
return 11;
}
return 0;
}
|