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
|
// Copyright Epic Games, Inc. All Rights Reserved.
#include <zencore/compactbinary.h>
#include <zencore/compactbinarybuilder.h>
#include <zencore/compactbinarypackage.h>
#include <zencore/compress.h>
#include <zencore/filesystem.h>
#include <zencore/fmtutils.h>
#include <zencore/stream.h>
#if ZEN_WITH_TESTS
# define ZEN_TEST_WITH_RUNNER 1
# include <zencore/testing.h>
#endif
#include <fmt/format.h>
#include <stdio.h>
#include <algorithm>
#include <chrono>
#include <cstdlib>
#include <cstring>
#include <filesystem>
#include <string>
#include <system_error>
#include <thread>
using namespace std::literals;
using namespace zen;
#if !defined(_MSC_VER)
# define _strnicmp strncasecmp // TEMPORARY WORKAROUND - should not be using this
#endif
// Some basic functions to implement some test "compute" functions
std::string
Rot13Function(std::string_view InputString)
{
std::string OutputString{InputString};
std::transform(OutputString.begin(),
OutputString.end(),
OutputString.begin(),
[](std::string::value_type c) -> std::string::value_type {
if (c >= 'a' && c <= 'z')
{
return 'a' + (c - 'a' + 13) % 26;
}
else if (c >= 'A' && c <= 'Z')
{
return 'A' + (c - 'A' + 13) % 26;
}
else
{
return c;
}
});
return OutputString;
}
std::string
ReverseFunction(std::string_view InputString)
{
std::string OutputString{InputString};
std::reverse(OutputString.begin(), OutputString.end());
return OutputString;
}
std::string
IdentityFunction(std::string_view InputString)
{
return std::string{InputString};
}
std::string
NullFunction(std::string_view)
{
return {};
}
zen::CbObject
DescribeFunctions()
{
CbObjectWriter Versions;
Versions << "BuildSystemVersion" << Guid::FromString("17fe280d-ccd8-4be8-a9d1-89c944a70969"sv);
Versions.BeginArray("Functions"sv);
Versions.BeginObject();
Versions << "Name"sv
<< "Null"sv;
Versions << "Version"sv << Guid::FromString("00000000-0000-0000-0000-000000000000"sv);
Versions.EndObject();
Versions.BeginObject();
Versions << "Name"sv
<< "Identity"sv;
Versions << "Version"sv << Guid::FromString("11111111-1111-1111-1111-111111111111"sv);
Versions.EndObject();
Versions.BeginObject();
Versions << "Name"sv
<< "Rot13"sv;
Versions << "Version"sv << Guid::FromString("13131313-1313-1313-1313-131313131313"sv);
Versions.EndObject();
Versions.BeginObject();
Versions << "Name"sv
<< "Reverse"sv;
Versions << "Version"sv << Guid::FromString("31313131-3131-3131-3131-313131313131"sv);
Versions.EndObject();
Versions.EndArray();
return Versions.Save();
}
struct ContentResolver
{
std::filesystem::path InputsRoot;
CompressedBuffer ResolveChunk(IoHash Hash, uint64_t ExpectedSize)
{
std::filesystem::path ChunkPath = InputsRoot / Hash.ToHexString();
IoBuffer ChunkBuffer = IoBufferBuilder::MakeFromFile(ChunkPath);
IoHash RawHash;
uint64_t RawSize = 0;
CompressedBuffer AsCompressed = CompressedBuffer::FromCompressed(SharedBuffer(ChunkBuffer), RawHash, RawSize);
if (RawSize != ExpectedSize)
{
throw std::runtime_error(
fmt::format("chunk size mismatch - expected {}, got {} for '{}'", ExpectedSize, ChunkBuffer.Size(), ChunkPath));
}
if (RawHash != Hash)
{
throw std::runtime_error(fmt::format("chunk hash mismatch - expected {}, got {} for '{}'", Hash, RawHash, ChunkPath));
}
return AsCompressed;
}
};
zen::CbPackage
ExecuteFunction(CbObject Action, ContentResolver ChunkResolver)
{
auto Apply = [&](auto Func) {
zen::CbPackage Result;
auto Source = Action["Inputs"sv].AsObjectView()["Source"sv].AsObjectView();
IoHash InputRawHash = Source["RawHash"sv].AsHash();
uint64_t InputRawSize = Source["RawSize"sv].AsUInt64();
zen::CompressedBuffer InputData = ChunkResolver.ResolveChunk(InputRawHash, InputRawSize);
SharedBuffer Input = InputData.Decompress();
std::string Output = Func(std::string_view(static_cast<const char*>(Input.GetData()), Input.GetSize()));
zen::CompressedBuffer OutputData =
zen::CompressedBuffer::Compress(SharedBuffer::MakeView(Output), OodleCompressor::Selkie, OodleCompressionLevel::HyperFast4);
IoHash OutputRawHash = OutputData.DecodeRawHash();
CbAttachment OutputAttachment(std::move(OutputData), OutputRawHash);
CbObjectWriter Cbo;
Cbo.BeginArray("Values"sv);
Cbo.BeginObject();
Cbo << "Id" << Oid{1, 2, 3};
Cbo.AddAttachment("RawHash", OutputAttachment);
Cbo << "RawSize" << Output.size();
Cbo.EndObject();
Cbo.EndArray();
Result.SetObject(Cbo.Save());
Result.AddAttachment(std::move(OutputAttachment));
return Result;
};
std::string_view Function = Action["Function"sv].AsString();
if (Function == "Rot13"sv)
{
return Apply(Rot13Function);
}
else if (Function == "Reverse"sv)
{
return Apply(ReverseFunction);
}
else if (Function == "Identity"sv)
{
return Apply(IdentityFunction);
}
else if (Function == "Null"sv)
{
return Apply(NullFunction);
}
else
{
return {};
}
}
/* This implements a minimal application to help testing of process launch-related
functionality
It also mimics the DDC2 worker command line interface, so it may be used to
exercise compute infrastructure.
*/
int
main(int argc, char* argv[])
{
int ExitCode = 0;
try
{
std::filesystem::path BasePath = std::filesystem::current_path();
std::filesystem::path InputPath = std::filesystem::current_path() / "Inputs";
std::filesystem::path OutputPath = std::filesystem::current_path() / "Outputs";
std::filesystem::path VersionPath = std::filesystem::current_path() / "Versions";
std::vector<std::filesystem::path> ActionPaths;
/*
GetSwitchValues(TEXT("-B="), ActionPathPatterns);
GetSwitchValues(TEXT("-Build="), ActionPathPatterns);
GetSwitchValues(TEXT("-I="), InputDirectoryPaths);
GetSwitchValues(TEXT("-Input="), InputDirectoryPaths);
GetSwitchValues(TEXT("-O="), OutputDirectoryPaths);
GetSwitchValues(TEXT("-Output="), OutputDirectoryPaths);
GetSwitchValues(TEXT("-V="), VersionPaths);
GetSwitchValues(TEXT("-Version="), VersionPaths);
*/
auto SplitArg = [](const char* Arg) -> std::string_view {
std::string_view ArgView{Arg};
if (auto SplitPos = ArgView.find_first_of('='); SplitPos != std::string_view::npos)
{
return ArgView.substr(SplitPos + 1);
}
else
{
return {};
}
};
auto ParseIntArg = [](std::string_view Arg) -> int {
int Rv = 0;
const auto Result = std::from_chars(Arg.data(), Arg.data() + Arg.size(), Rv);
if (Result.ec != std::errc{})
{
throw std::invalid_argument(fmt::format("bad argument (not an integer): {}", Arg).c_str());
}
return Rv;
};
for (int i = 1; i < argc; ++i)
{
std::string_view Arg = argv[i];
if (Arg.compare(0, 1, "-"))
{
continue;
}
if (std::strncmp(argv[i], "-t=", 3) == 0)
{
const int SleepTime = std::atoi(argv[i] + 3);
printf("[zentest] sleeping for %ds...\n", SleepTime);
std::this_thread::sleep_for(SleepTime * 1s);
}
else if (std::strncmp(argv[i], "-f=", 3) == 0)
{
// Force a "failure" process exit code to return to the invoker
// This may throw for invalid arguments, which makes this useful for
// testing exception handling
std::string_view ErrorArg = SplitArg(argv[i]);
ExitCode = ParseIntArg(ErrorArg);
}
else if ((_strnicmp(argv[i], "-input=", 7) == 0) || (_strnicmp(argv[i], "-i=", 3) == 0))
{
/* mimic DDC2
GetSwitchValues(TEXT("-I="), InputDirectoryPaths);
GetSwitchValues(TEXT("-Input="), InputDirectoryPaths);
*/
std::string_view InputArg = SplitArg(argv[i]);
InputPath = InputArg;
}
else if ((_strnicmp(argv[i], "-output=", 8) == 0) || (_strnicmp(argv[i], "-o=", 3) == 0))
{
/* mimic DDC2 handling of where files storing output chunk files are directed
GetSwitchValues(TEXT("-O="), OutputDirectoryPaths);
GetSwitchValues(TEXT("-Output="), OutputDirectoryPaths);
*/
std::string_view OutputArg = SplitArg(argv[i]);
OutputPath = OutputArg;
}
else if ((_strnicmp(argv[i], "-version=", 8) == 0) || (_strnicmp(argv[i], "-v=", 3) == 0))
{
/* mimic DDC2
GetSwitchValues(TEXT("-V="), VersionPaths);
GetSwitchValues(TEXT("-Version="), VersionPaths);
*/
std::string_view VersionArg = SplitArg(argv[i]);
VersionPath = VersionArg;
}
else if ((_strnicmp(argv[i], "-build=", 7) == 0) || (_strnicmp(argv[i], "-b=", 3) == 0))
{
/* mimic DDC2
GetSwitchValues(TEXT("-B="), ActionPathPatterns);
GetSwitchValues(TEXT("-Build="), ActionPathPatterns);
*/
std::string_view BuildActionArg = SplitArg(argv[i]);
std::filesystem::path ActionPath{BuildActionArg};
ActionPaths.push_back(ActionPath);
ExitCode = 0;
}
}
// Emit version information
if (!VersionPath.empty())
{
CbObjectWriter Version;
Version << "BuildSystemVersion" << Guid::FromString("17fe280d-ccd8-4be8-a9d1-89c944a70969"sv);
Version.BeginArray("Functions");
Version.BeginObject();
Version << "Name"
<< "Rot13"
<< "Version" << Guid::FromString("13131313-1313-1313-1313-131313131313"sv);
Version.EndObject();
Version.BeginObject();
Version << "Name"
<< "Reverse"
<< "Version" << Guid::FromString("98765432-1000-0000-0000-000000000000"sv);
Version.EndObject();
Version.BeginObject();
Version << "Name"
<< "Identity"
<< "Version" << Guid::FromString("11111111-1111-1111-1111-111111111111"sv);
Version.EndObject();
Version.BeginObject();
Version << "Name"
<< "Null"
<< "Version" << Guid::FromString("00000000-0000-0000-0000-000000000000"sv);
Version.EndObject();
Version.EndArray();
CbObject VersionObject = Version.Save();
BinaryWriter Writer;
zen::SaveCompactBinary(Writer, VersionObject);
zen::WriteFile(VersionPath, IoBufferBuilder::MakeFromMemory(Writer.GetView()));
}
// Evaluate actions
ContentResolver Resolver;
Resolver.InputsRoot = InputPath;
for (std::filesystem::path ActionPath : ActionPaths)
{
IoBuffer ActionDescBuffer = ReadFile(ActionPath).Flatten();
CbObject ActionDesc = LoadCompactBinaryObject(ActionDescBuffer);
CbPackage Result = ExecuteFunction(ActionDesc, Resolver);
CbObject ResultObject = Result.GetObject();
BinaryWriter Writer;
zen::SaveCompactBinary(Writer, ResultObject);
zen::WriteFile(ActionPath.replace_extension(".output"), IoBufferBuilder::MakeFromMemory(Writer.GetView()));
// Also marshal outputs
for (const auto& Attachment : Result.GetAttachments())
{
const CompositeBuffer& AttachmentBuffer = Attachment.AsCompressedBinary().GetCompressed();
zen::WriteFile(OutputPath / Attachment.GetHash().ToHexString(), AttachmentBuffer.Flatten().AsIoBuffer());
}
}
}
catch (std::exception& Ex)
{
printf("[zentest] exception caught in main: '%s'\n", Ex.what());
ExitCode = 99;
}
printf("[zentest] exiting with exit code: %d\n", ExitCode);
return ExitCode;
}
|