NCBI C++ Toolkit Cross Reference

  C++/src/app/netschedule/test/test_netschedule_crash.cpp


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 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650
/* $Id: test_netschedule_crash.cpp 77858 2017-05-10 19:09:17Z satskyse $ * =========================================================================== * * PUBLIC DOMAIN NOTICE * National Center for Biotechnology Information * * This software/database is a "United States Government Work" under the * terms of the United States Copyright Act. It was written as part of * the author's official duties as a United States Government employee and * thus cannot be copyrighted. This software/database is freely available * to the public for use. The National Library of Medicine and the U.S. * Government have not placed any restriction on its use or reproduction. * * Although all reasonable efforts have been taken to ensure the accuracy * and reliability of the software and data, the NLM and the U.S. * Government do not and cannot warrant the performance or results that * may be obtained by using this software or data. The NLM and the U.S. * Government disclaim all warranties, express or implied, including * warranties of performance, merchantability or fitness for any particular * purpose. * * Please cite the author in any work or product based on this material. * * =========================================================================== * * Authors: Anatoliy Kuznetsov * * File Description: NetSchedule crash test. Used to run many instances of it * to simulate load for the netscheduled daemon. * */ #include <ncbi_pch.hpp> #include <corelib/ncbiapp.hpp> #include <corelib/ncbiargs.hpp> #include <corelib/ncbienv.hpp> #include <corelib/ncbireg.hpp> #include <corelib/ncbi_system.hpp> #include <corelib/ncbimisc.hpp> #include <connect/services/netschedule_api.hpp> #include <connect/services/netschedule_key.hpp> #include <connect/ncbi_socket.hpp> #include <connect/ncbi_core_cxx.hpp> #include <connect/ncbi_types.h> #include <sys/types.h> #include <unistd.h> USING_NCBI_SCOPE; /////////////////////////////////////////////////////////////////////// #define WORKER_NODE_PORT 9595 /// Test application /// /// @internal /// class CTestNetScheduleCrash : public CNcbiApplication { public: void Init(void); int Run(void); void GetStatus( CNetScheduleExecutor & executor, const vector<unsigned int> & jobs ); void GetReturn( vector<CNetScheduleAPI> & clients, unsigned int count, unsigned int nclients ); vector<unsigned int> Submit( vector<CNetScheduleAPI> & clients, const string & service, unsigned int jcount, unsigned int naff, unsigned int ngroup, unsigned int notif_port, unsigned int nclients, const string & queue ); vector<unsigned int> GetDone( vector<CNetScheduleAPI> & clients, unsigned int nclients ); void MainLoop( CNetScheduleSubmitter & submitter, CNetScheduleExecutor & executor, unsigned int jcount, const string & queue); CTestNetScheduleCrash() : m_KeyGenerator(NULL), m_ProcessedJobs(0) {} ~CTestNetScheduleCrash() { if (m_KeyGenerator) delete m_KeyGenerator; } private: int x_Run(void); private: CNetScheduleKeyGenerator * m_KeyGenerator; CCompoundIDPool m_CompoundIDPool; unsigned int m_ProcessedJobs; }; void CTestNetScheduleCrash::Init(void) { // Avoid sockets to stay in TIME_WAIT state GetRWConfig().Set("netservice_api", "use_linger2", "true", IRegistry::fNoOverride); // CONNECT_Init(); SetDiagPostFlag(eDPF_Trace); SetDiagPostLevel(eDiag_Info); // Setup command line arguments and parameters // Create command-line argument descriptions class unique_ptr<CArgDescriptions> arg_desc(new CArgDescriptions); // Specify USAGE context arg_desc->SetUsageContext(GetArguments().GetProgramBasename(), "NetSchedule crash test prog='test 1.0'"); arg_desc->AddKey("service", "service_name", "NetSchedule service name (format: host:port or servcie_name).", CArgDescriptions::eString); arg_desc->AddKey("queue", "queue_name", "NetSchedule queue name (like: noname).", CArgDescriptions::eString); arg_desc->AddOptionalKey("jobs", "jobs", "Number of jobs to submit", CArgDescriptions::eInteger); arg_desc->AddOptionalKey("delay", "delay", "Number of seconds between operations", CArgDescriptions::eInteger); arg_desc->AddOptionalKey("naff", "naff", "Number of affinities", CArgDescriptions::eInteger); arg_desc->AddOptionalKey("ngroup", "ngroup", "Number of groups", CArgDescriptions::eInteger); arg_desc->AddOptionalKey("notifport", "notifport", "If given then jobs are submitted with notif port" " and 100 sec notif timeout", CArgDescriptions::eInteger); arg_desc->AddOptionalKey("batch", "batch", "Max number of jobs in a loop", CArgDescriptions::eInteger); arg_desc->AddOptionalKey("nclients", "nclients", "Number of clients to simulate", CArgDescriptions::eInteger); arg_desc->AddOptionalKey("main", "main", "Main loop (submit-get-put) only", CArgDescriptions::eBoolean); // Setup arg.descriptions for this application SetupArgDescriptions(arg_desc.release()); } vector<unsigned int> CTestNetScheduleCrash::Submit( vector<CNetScheduleAPI> & clients, const string & service, unsigned int jcount, unsigned int naff, unsigned int ngroup, unsigned int notif_port, unsigned int nclients, const string & queue ) { string input = "Crash test for " + queue; vector<unsigned int> jobs; unsigned int caff = 0; unsigned int cgroup = 0; unsigned int client = 0; string aff = ""; string group = ""; CNetServer server = clients[0].GetService().Iterate().GetServer(); CNetScheduleSubmitter submitter = clients[0].GetSubmitter(); jobs.reserve(jcount); NcbiCout << "Submit " << jcount << " jobs..." << NcbiEndl; CStopWatch sw(CStopWatch::eStart); for (unsigned i = 0; i < jcount; ++i) { if (nclients > 0) { server = clients[client].GetService().Iterate().GetServer(); submitter = clients[client].GetSubmitter(); if (++client >= nclients) client = 0; } if (naff > 0) { char buffer[ 16 ]; sprintf( buffer, "aff%d", caff++ ); if (caff >= naff) caff = 0; aff = string( buffer ); } if (ngroup > 0) { char buffer[ 16 ]; sprintf( buffer, "group%d", cgroup++ ); if (cgroup >= ngroup) cgroup = 0; group = string( buffer ); } // Two options here: with/without notifications if (notif_port == 0) { CNetScheduleJob job(input); job.affinity = aff; job.group = group; submitter.SubmitJob(job); CNetScheduleKey key( job.job_id, m_CompoundIDPool ); jobs.push_back( key.id ); if (m_KeyGenerator == NULL) m_KeyGenerator = new CNetScheduleKeyGenerator( key.host, key.port, queue ); } else { // back door - use manually formed command char buffer[ 1024 ]; sprintf( buffer, "SUBMIT input=\"Crash test for %s\" port=%d timeout=100", queue.c_str(), notif_port ); string cmd = string( buffer ); if (aff.empty() == false) cmd += " aff=" + aff; if (group.empty() == false) cmd += " group=" + group; CNetServer::SExecResult result = server.ExecWithRetry( cmd ); // Dirty hack: 'OK:' need to be stripped CNetScheduleKey key( result.response.c_str(), m_CompoundIDPool ); jobs.push_back( key.id ); if (m_KeyGenerator == NULL) m_KeyGenerator = new CNetScheduleKeyGenerator( key.host, key.port, queue ); } ++m_ProcessedJobs; if (i % 1000 == 0) { NcbiCout << "." << flush; } } double elapsed = sw.Elapsed(); double avg = elapsed / jcount; NcbiCout << NcbiEndl << "Done." << NcbiEndl; NcbiCout.setf(IOS_BASE::fixed, IOS_BASE::floatfield); NcbiCout << "Elapsed: " << elapsed << " sec." << NcbiEndl; NcbiCout << "Avg time: " << avg << " sec." << NcbiEndl; return jobs; } void CTestNetScheduleCrash::GetStatus( CNetScheduleExecutor & executor, const vector<unsigned int> & jobs ) { NcbiCout << NcbiEndl << "Get status of " << jobs.size() << " jobs..." << NcbiEndl; //CNetScheduleAPI::EJobStatus status; unsigned i = 0; CStopWatch sw(CStopWatch::eStart); CNetScheduleJob job; ITERATE(vector<unsigned int>, it, jobs) { job.job_id = m_KeyGenerator->Generate(*it); //status = executor.GetJobStatus(job); if (i++ % 1000 == 0) { NcbiCout << "." << flush; } } double elapsed = sw.Elapsed(); double avg = elapsed / (double)jobs.size(); NcbiCout.setf(IOS_BASE::fixed, IOS_BASE::floatfield); NcbiCout << NcbiEndl << "Elapsed :" << elapsed << " sec." << NcbiEndl; NcbiCout << "Avg time :" << avg << " sec." << NcbiEndl; return; } /* Returns up to count jobs */ void CTestNetScheduleCrash::GetReturn( vector<CNetScheduleAPI> & clients, unsigned int count, unsigned int nclients ) { NcbiCout << NcbiEndl << "Take and Return " << count << " jobs..." << NcbiEndl; typedef pair<string, string> TJobIdAuthTokenPair; vector<TJobIdAuthTokenPair> jobs_returned; jobs_returned.reserve(count); unsigned int client = 0; CNetScheduleExecutor executor = clients[0].GetExecutor(); unsigned cnt = 0; CStopWatch sw(CStopWatch::eStart); for (; cnt < count; ++cnt) { if (nclients > 0) { executor = clients[client].GetExecutor(); if (++client >= nclients) client = 0; } CNetScheduleJob job; bool job_exists = executor.GetJob(job); if (!job_exists) continue; jobs_returned.push_back( TJobIdAuthTokenPair(job.job_id, job.auth_token)); } ITERATE(vector<TJobIdAuthTokenPair>, it, jobs_returned) { try { CNetScheduleJob job; job.job_id = it->first; job.auth_token = it->second; executor.ReturnJob(job); } catch (CException& e) { NcbiCout << e.what() << NcbiEndl; } } double elapsed = sw.Elapsed(); if (cnt > 0) { double avg = elapsed / cnt; NcbiCout << "Returned " << cnt << " jobs." << NcbiEndl; NcbiCout.setf(IOS_BASE::fixed, IOS_BASE::floatfield); NcbiCout << "Jobs processed: " << cnt << NcbiEndl; NcbiCout << "Elapsed: " << elapsed << " sec." << NcbiEndl; NcbiCout << "Avg time: " << avg << " sec." << NcbiEndl; } else NcbiCout << "Returned 0 jobs." << NcbiEndl; return; } vector<unsigned int> CTestNetScheduleCrash::GetDone( vector<CNetScheduleAPI> & clients, unsigned int nclients ) { unsigned cnt = 0; vector<unsigned int> jobs_processed; jobs_processed.reserve(10000); unsigned int client = 0; CNetScheduleExecutor executor = clients[0].GetExecutor(); NcbiCout << NcbiEndl << "Processing..." << NcbiEndl; CStopWatch sw(CStopWatch::eStart); for (; 1; ++cnt) { if (nclients > 0) { executor = clients[client].GetExecutor(); if (++client >= nclients) client = 0; } CNetScheduleJob job; bool job_exists = executor.GetJob(job); if (!job_exists) break; CNetScheduleKey key( job.job_id, m_CompoundIDPool ); jobs_processed.push_back( key.id ); job.output = "JOB DONE "; executor.PutResult(job); if (cnt % 1000 == 0) NcbiCout << "." << flush; } double elapsed = sw.Elapsed(); if (cnt) { double avg = elapsed / cnt; NcbiCout.setf(IOS_BASE::fixed, IOS_BASE::floatfield); NcbiCout << "Jobs processed: " << cnt << NcbiEndl; NcbiCout << "Elapsed: " << elapsed << " sec." << NcbiEndl; NcbiCout << "Avg time: " << avg << " sec." << NcbiEndl; } else NcbiCout << "Processed 0 jobs." << NcbiEndl; return jobs_processed; } void CTestNetScheduleCrash::MainLoop( CNetScheduleSubmitter & submitter, CNetScheduleExecutor & executor, unsigned int jcount, const string & queue) { NcbiCout << NcbiEndl << "Loop of SUBMIT->GET->PUT for " << jcount << " jobs. Each dot denotes 1000 loops." << NcbiEndl; CNetScheduleJob job( "SUBMIT->GET->PUT loop test input" ); string job_key; CStopWatch sw( CStopWatch::eStart ); for (m_ProcessedJobs = 0; m_ProcessedJobs < jcount; ++m_ProcessedJobs ) { submitter.SubmitJob( job ); if ( ! executor.GetJob( job ) ) continue; job.output = "JOB DONE"; executor.PutResult( job ); if (m_ProcessedJobs % 1000 == 0) { NcbiCout << "." << flush; } } double elapsed = sw.Elapsed(); double avg = elapsed / jcount; NcbiCout.setf(IOS_BASE::fixed, IOS_BASE::floatfield); NcbiCout << NcbiEndl << "Jobs processed: " << jcount << NcbiEndl; NcbiCout << "Elapsed: " << elapsed << " sec." << NcbiEndl; NcbiCout << "Avg time: " << avg << " sec." << NcbiEndl; return; } int CTestNetScheduleCrash::Run(void) { try { return x_Run(); } catch (const exception & exc) { NcbiCerr << "Exception: " << exc.what() << "\nAfter submitting " << m_ProcessedJobs << " jobs\n"; } catch (...) { NcbiCerr << "Unknown exception\n" "After submitting " << m_ProcessedJobs << " jobs\n"; } return 1; } int CTestNetScheduleCrash::x_Run(void) { const CArgs & args = GetArgs(); const string & service = args["service"].AsString(); const string & queue = args["queue"].AsString(); unsigned total_jobs = 10000; if (args["jobs"]) total_jobs = args["jobs"].AsInteger(); unsigned int delay = 10; if (args["delay"]) delay = args["delay"].AsInteger(); unsigned int naff = 0; if (args["naff"]) naff = args["naff"].AsInteger(); unsigned int ngroup = 0; if (args["ngroup"]) ngroup = args["ngroup"].AsInteger(); unsigned int notif_port = 0; if (args["notifport"]) notif_port = args["notifport"].AsInteger(); unsigned int nclients = 0; if (args["nclients"]) nclients = args["nclients"].AsInteger(); unsigned int batch = 10000; if (args["batch"]) { batch = args["batch"].AsInteger(); if (batch == 0) batch = 10000; } bool main_only = args["main"]; vector<CNetScheduleAPI> clients; pid_t pid = getpid(); if (nclients > 0 && main_only == false) { for (unsigned int k = 0; k < nclients; ++k) { char buffer[ 64 ]; sprintf( buffer, "node_%d_%d", pid, k ); CNetScheduleAPI cl = CNetScheduleAPI(service, "crash_test", queue); cl.SetProgramVersion("crash_test wn 1.0.4"); cl.SetClientNode( buffer ); cl.SetClientSession("crash_test_session"); clients.push_back(cl); } } else { char buffer[ 64 ]; sprintf( buffer, "node_%d", pid ); CNetScheduleAPI cl = CNetScheduleAPI(service, "crash_test", queue); cl.SetProgramVersion("crash_test wn 1.0.4"); cl.SetClientNode( buffer ); cl.SetClientSession("crash_test_session"); clients.push_back(cl); } // At least one exists for sure clients[0].GetAdmin().PrintServerVersion(NcbiCout); CNetScheduleSubmitter submitter = clients[0].GetSubmitter(); CNetScheduleExecutor executor = clients[0].GetExecutor(); if (naff > 0) { // Enable retrieving affinities executor.SetAffinityPreference( CNetScheduleExecutor::eClaimNewPreferredAffs); } if (main_only) { this->MainLoop(submitter, executor, total_jobs, queue); return 0; } m_ProcessedJobs = 0; unsigned int jcount; while (total_jobs > 0) { if (total_jobs < batch) jcount = total_jobs; else jcount = batch; total_jobs -= jcount; /* ----- Submit jobs ----- */ vector<unsigned int> jobs = this->Submit(clients, service, jcount, naff, ngroup, notif_port, nclients, queue); NcbiCout << NcbiEndl << "Waiting " << delay << " second(s) ..." << NcbiEndl; SleepMilliSec(delay * 1000); NcbiCout << NcbiEndl << "Ok." << NcbiEndl; /* ----- Get status ----- */ this->GetStatus(executor, jobs); NcbiCout << NcbiEndl << "Waiting " << delay << " second(s) ..." << NcbiEndl; SleepMilliSec(delay * 1000); NcbiCout << NcbiEndl << "Ok." << NcbiEndl; /* ----- Get and return some jobs ----- */ this->GetReturn(clients, jcount/100, nclients); NcbiCout << NcbiEndl << "Waiting " << delay << " second(s) ..." << NcbiEndl; SleepMilliSec(delay * 1000); NcbiCout << NcbiEndl << "Ok." << NcbiEndl; /* ----- Get status ----- */ this->GetStatus(executor, jobs); NcbiCout << NcbiEndl << "Waiting " << delay << " second(s) ..." << NcbiEndl; SleepMilliSec(delay * 1000); NcbiCout << NcbiEndl << "Ok." << NcbiEndl; /* ----- Get jobs and say they are done ----- */ vector<unsigned int> jobs_processed = this->GetDone(clients, nclients); NcbiCout << NcbiEndl << "Waiting " << delay << " second(s) ..." << NcbiEndl; SleepMilliSec(delay * 1000); NcbiCout << NcbiEndl << "Ok." << NcbiEndl; /* ----- Get status ----- */ this->GetStatus(executor, jobs); } return 0; } int main(int argc, const char* argv[]) { return CTestNetScheduleCrash().AppMain(argc, argv, 0, eDS_Default); }

source navigation ]   [ diff markup ]   [ identifier search ]   [ freetext search ]   [ file search ]  

This page was automatically generated by the LXR engine.
Visit the LXR main site for more information.