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
|
#include <cassert>
#include <chrono>
#include <cstddef> // IWYU pragma: keep
#include <cstdlib>
#include <functional>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <thread>
#include <utility>
#include <vector>
#include "cmsys/Encoding.hxx"
#include "cmsys/FStream.hxx"
#include "cmCTestMultiProcessHandler.h"
#include "cmCTestResourceAllocator.h"
#include "cmCTestResourceSpec.h"
#include "cmCTestTestHandler.h"
#include "cmFileLock.h"
#include "cmFileLockResult.h"
#include "cmList.h"
#include "cmStringAlgorithms.h"
#include "cmSystemTools.h"
/*
* This helper program is used to verify that the CTest resource allocation
* feature is working correctly. It consists of two stages:
*
* 1) write - This stage receives the RESOURCE_GROUPS property of the test and
* compares it with the values passed in the CTEST_RESOURCE_GROUP_*
* environment variables. If it received all of the resources it expected,
* then it writes this information to a log file, which will be read in
* the verify stage.
* 2) verify - This stage compares the log file with the resource spec file to
* make sure that no resources were over-subscribed, deallocated without
* being allocated, or allocated without being deallocated.
*/
static int usage(char const* argv0)
{
std::cout << "Usage: " << argv0 << " (write|verify) <args...>" << std::endl;
return 1;
}
static int usageWrite(char const* argv0)
{
std::cout << "Usage: " << argv0
<< " write <log-file> <test-name> <sleep-time-secs>"
" [<resource-groups-property>]"
<< std::endl;
return 1;
}
static int usageVerify(char const* argv0)
{
std::cout << "Usage: " << argv0
<< " verify <log-file> <resource-spec-file> [<test-names>]"
<< std::endl;
return 1;
}
static int doWrite(int argc, char const* const* argv)
{
if (argc < 5 || argc > 6) {
return usageWrite(argv[0]);
}
std::string logFile = argv[2];
std::string testName = argv[3];
unsigned int sleepTime = std::atoi(argv[4]);
std::vector<std::map<
std::string, std::vector<cmCTestMultiProcessHandler::ResourceAllocation>>>
resources;
if (argc == 6) {
// Parse RESOURCE_GROUPS property
std::string resourceGroupsProperty = argv[5];
std::vector<
std::vector<cmCTestTestHandler::cmCTestTestResourceRequirement>>
resourceGroups;
bool result = cmCTestTestHandler::ParseResourceGroupsProperty(
resourceGroupsProperty, resourceGroups);
(void)result;
assert(result);
// Verify group count
char const* resourceGroupCountEnv =
cmSystemTools::GetEnv("CTEST_RESOURCE_GROUP_COUNT");
if (!resourceGroupCountEnv) {
std::cout << "CTEST_RESOURCE_GROUP_COUNT should be defined" << std::endl;
return 1;
}
int resourceGroupCount = std::atoi(resourceGroupCountEnv);
if (resourceGroups.size() !=
static_cast<std::size_t>(resourceGroupCount)) {
std::cout
<< "CTEST_RESOURCE_GROUP_COUNT does not match expected resource groups"
<< std::endl
<< "Expected: " << resourceGroups.size() << std::endl
<< "Actual: " << resourceGroupCount << std::endl;
return 1;
}
if (!cmSystemTools::Touch(logFile + ".lock", true)) {
std::cout << "Could not create lock file" << std::endl;
return 1;
}
cmFileLock lock;
auto lockResult =
lock.Lock(logFile + ".lock", static_cast<unsigned long>(-1));
if (!lockResult.IsOk()) {
std::cout << "Could not lock file" << std::endl;
return 1;
}
std::size_t i = 0;
cmsys::ofstream fout(logFile.c_str(), std::ios::app);
fout << "begin " << testName << std::endl;
for (auto& resourceGroup : resourceGroups) {
try {
// Build and verify set of expected resources
std::set<std::string> expectedResources;
for (auto const& it : resourceGroup) {
expectedResources.insert(it.ResourceType);
}
std::string prefix = cmStrCat("CTEST_RESOURCE_GROUP_", i);
char const* actualResourcesCStr = cmSystemTools::GetEnv(prefix);
if (!actualResourcesCStr) {
std::cout << prefix << " should be defined" << std::endl;
return 1;
}
auto actualResourcesVec =
cmSystemTools::SplitString(actualResourcesCStr, ',');
std::set<std::string> actualResources;
for (auto const& r : actualResourcesVec) {
if (!r.empty()) {
actualResources.insert(r);
}
}
if (actualResources != expectedResources) {
std::cout << prefix << " did not list expected resources"
<< std::endl;
return 1;
}
// Verify that we got what we asked for and write it to the log
prefix += '_';
std::map<std::string,
std::vector<cmCTestMultiProcessHandler::ResourceAllocation>>
resEntry;
for (auto const& type : actualResources) {
auto it = resourceGroup.begin();
std::string varName = prefix;
varName += cmSystemTools::UpperCase(type);
char const* varVal = cmSystemTools::GetEnv(varName);
if (!varVal) {
std::cout << varName << " should be defined" << std::endl;
return 1;
}
auto received = cmSystemTools::SplitString(varVal, ';');
for (auto const& r : received) {
while (it->ResourceType != type || it->UnitsNeeded == 0) {
++it;
if (it == resourceGroup.end()) {
std::cout << varName << " did not list expected resources"
<< std::endl;
return 1;
}
}
auto split = cmSystemTools::SplitString(r, ',');
if (split.size() != 2) {
std::cout << varName << " was ill-formed" << std::endl;
return 1;
}
if (!cmHasLiteralPrefix(split[0], "id:")) {
std::cout << varName << " was ill-formed" << std::endl;
return 1;
}
auto id = split[0].substr(3);
if (!cmHasLiteralPrefix(split[1], "slots:")) {
std::cout << varName << " was ill-formed" << std::endl;
return 1;
}
auto slots = split[1].substr(6);
unsigned int amount = std::atoi(slots.c_str());
if (amount != static_cast<unsigned int>(it->SlotsNeeded)) {
std::cout << varName << " did not list expected resources"
<< std::endl;
return 1;
}
--it->UnitsNeeded;
fout << "alloc " << type << " " << id << " " << amount
<< std::endl;
resEntry[type].push_back({ id, amount });
}
bool ended = false;
while (it->ResourceType != type || it->UnitsNeeded == 0) {
++it;
if (it == resourceGroup.end()) {
ended = true;
break;
}
}
if (!ended) {
std::cout << varName << " did not list expected resources"
<< std::endl;
return 1;
}
}
resources.push_back(resEntry);
++i;
} catch (...) {
std::cout << "Unknown error while processing resources" << std::endl;
return 1;
}
}
auto unlockResult = lock.Release();
if (!unlockResult.IsOk()) {
std::cout << "Could not unlock file" << std::endl;
return 1;
}
} else {
if (cmSystemTools::GetEnv("CTEST_RESOURCE_GROUP_COUNT")) {
std::cout << "CTEST_RESOURCE_GROUP_COUNT should not be defined"
<< std::endl;
return 1;
}
}
std::this_thread::sleep_for(std::chrono::seconds(sleepTime));
if (argc == 6) {
if (!cmSystemTools::Touch(logFile + ".lock", true)) {
std::cout << "Could not create lock file" << std::endl;
return 1;
}
cmFileLock lock;
auto lockResult =
lock.Lock(logFile + ".lock", static_cast<unsigned long>(-1));
if (!lockResult.IsOk()) {
std::cout << "Could not lock file" << std::endl;
return 1;
}
cmsys::ofstream fout(logFile.c_str(), std::ios::app);
for (auto const& group : resources) {
for (auto const& it : group) {
for (auto const& it2 : it.second) {
fout << "dealloc " << it.first << " " << it2.Id << " " << it2.Slots
<< std::endl;
}
}
}
fout << "end " << testName << std::endl;
auto unlockResult = lock.Release();
if (!unlockResult.IsOk()) {
std::cout << "Could not unlock file" << std::endl;
return 1;
}
}
return 0;
}
static int doVerify(int argc, char const* const* argv)
{
if (argc < 4 || argc > 5) {
return usageVerify(argv[0]);
}
std::string logFile = argv[2];
std::string resFile = argv[3];
std::string testNames;
if (argc == 5) {
testNames = argv[4];
}
cmList testNameList{ testNames };
std::set<std::string> testNameSet(testNameList.begin(), testNameList.end());
cmCTestResourceSpec spec;
if (spec.ReadFromJSONFile(resFile) != true) {
std::cout << "Could not read resource spec " << resFile << std::endl;
return 1;
}
cmCTestResourceAllocator allocator;
allocator.InitializeFromResourceSpec(spec);
cmsys::ifstream fin(logFile.c_str(), std::ios::in);
if (!fin) {
std::cout << "Could not open log file " << logFile << std::endl;
return 1;
}
std::string command;
std::string resourceName;
std::string resourceId;
std::string testName;
unsigned int amount;
std::set<std::string> inProgressTests;
std::set<std::string> completedTests;
try {
while (fin >> command) {
if (command == "begin") {
if (!(fin >> testName)) {
std::cout << "Could not read begin line" << std::endl;
return 1;
}
if (!testNameSet.count(testName) || inProgressTests.count(testName) ||
completedTests.count(testName)) {
std::cout << "Could not begin test" << std::endl;
return 1;
}
inProgressTests.insert(testName);
} else if (command == "alloc") {
if (!(fin >> resourceName) || !(fin >> resourceId) ||
!(fin >> amount)) {
std::cout << "Could not read alloc line" << std::endl;
return 1;
}
if (!allocator.AllocateResource(resourceName, resourceId, amount)) {
std::cout << "Could not allocate resources" << std::endl;
return 1;
}
} else if (command == "dealloc") {
if (!(fin >> resourceName) || !(fin >> resourceId) ||
!(fin >> amount)) {
std::cout << "Could not read dealloc line" << std::endl;
return 1;
}
if (!allocator.DeallocateResource(resourceName, resourceId, amount)) {
std::cout << "Could not deallocate resources" << std::endl;
return 1;
}
} else if (command == "end") {
if (!(fin >> testName)) {
std::cout << "Could not read end line" << std::endl;
return 1;
}
if (!inProgressTests.erase(testName)) {
std::cout << "Could not end test" << std::endl;
return 1;
}
if (!completedTests.insert(testName).second) {
std::cout << "Could not end test" << std::endl;
return 1;
}
}
}
} catch (...) {
std::cout << "Unknown error while reading log file" << std::endl;
return 1;
}
auto const& avail = allocator.GetResources();
for (auto const& it : avail) {
for (auto const& it2 : it.second) {
if (it2.second.Locked != 0) {
std::cout << "Resource was not unlocked" << std::endl;
return 1;
}
}
}
if (completedTests != testNameSet) {
std::cout << "Tests were not ended" << std::endl;
return 1;
}
return 0;
}
int main(int argc, char const* const* argv)
{
cmsys::Encoding::CommandLineArguments args =
cmsys::Encoding::CommandLineArguments::Main(argc, argv);
argc = args.argc();
argv = args.argv();
if (argc < 2) {
return usage(argv[0]);
}
std::string argv1 = argv[1];
if (argv1 == "write") {
return doWrite(argc, argv);
}
if (argv1 == "verify") {
return doVerify(argc, argv);
}
return usage(argv[0]);
}
|