4 * Copyright (c) 2003, 2004
7 * This material is provided "as is", with absolutely no warranty expressed
8 * or implied. Any use is at your own risk.
10 * Permission to use or copy this software for any purpose is hereby granted
11 * without fee, provided the above notices are retained on all copies.
12 * Permission to modify the code and to distribute modified code is granted,
13 * provided the above notices are retained, and a notice that the code was
14 * modified is included with the above copyright notice.
19 #if defined(_DEBUG) && _MSC_VER == 1500
20 # define _CRTDBG_MAP_ALLOC
26 #include "cppunit/cppunit_proxy.h"
27 #include "cppunit/file_reporter.h"
30 #include <cds/gc/hp.h>
31 #include <cds/gc/dhp.h>
32 #include <cds/urcu/general_instant.h>
33 #include <cds/urcu/general_buffered.h>
34 #include <cds/urcu/general_threaded.h>
35 #include <cds/urcu/signal_buffered.h>
36 #include <cds/urcu/signal_threaded.h>
37 #include <cds/os/topology.h>
43 #include <boost/date_time/posix_time/posix_time.hpp>
44 #include <boost/thread/mutex.hpp>
46 // Visual leak detector (see http://vld.codeplex.com/)
47 #if defined(CDS_USE_VLD) && CDS_COMPILER == CDS_COMPILER_MSVC
54 std::ostream& operator << (std::ostream& s, const cds::gc::hp::GarbageCollector::InternalState& stat)
56 s << "\nHZP GC internal state:"
57 << "\n\t HP record allocated=" << stat.nHPRecAllocated
58 << "\n\t HP records used=" << stat.nHPRecUsed
59 << "\n\t Total retired ptr count=" << stat.nTotalRetiredPtrCount
60 << "\n\t Retired ptr in free HP records=" << stat.nRetiredPtrInFreeHPRecs
62 << "\n\t HPRec allocations=" << stat.evcAllocHPRec
63 << "\n\t HPRec retire events=" << stat.evcRetireHPRec
64 << "\n\tnew HPRec allocations from heap=" << stat.evcAllocNewHPRec
65 << "\n\t HPRec deletions=" << stat.evcDeleteHPRec
66 << "\n\t Scan call count=" << stat.evcScanCall
67 << "\n\t HelpScan call count=" << stat.evcHelpScanCall
68 << "\n\t Scan calls from HelpScan=" << stat.evcScanFromHelpScan
69 << "\n\t retired object deleting=" << stat.evcDeletedNode
70 << "\n\t guarded object on Scan=" << stat.evcDeferredNode
78 int TestCase::m_numErrors = 0;
79 int TestCase::m_numTests = 0;
80 std::vector<std::string> TestCase::m_arrStrings;
81 bool TestCase::m_bPrintGCState = false;
82 std::string TestCase::m_strTestDataDir(".");
83 Config TestCase::m_Cfg;
84 bool TestCase::m_bExactMatch = false;
86 // random shuffle support
87 /*static*/ std::random_device TestCase::m_RandomDevice;
88 /*static*/ std::mt19937 TestCase::m_RandomGen( TestCase::m_RandomDevice() );
90 TestCase * TestCase::m_pCurTestCase = nullptr;
92 TestCase *TestCase::m_root = 0;
93 Reporter *TestCase::m_reporter = 0;
95 void TestCase::registerTestCase(TestCase *in_testCase) {
96 in_testCase->m_next = m_root;
100 int TestCase::run(Reporter *in_reporter, const char *in_testName, bool invert)
102 TestCase::m_reporter = in_reporter;
107 TestCase *tmp = m_root;
109 m_pCurTestCase = tmp;
111 tmp->myRun(in_testName, invert);
112 } catch ( std::exception& ex ) {
113 in_reporter->message( "EXCEPTION: ");
114 in_reporter->message( ex.what() );
122 bool TestCase::shouldRunThis(const char *in_desiredTest, const char *in_className, const char *in_functionName,
123 bool invert, bool explicit_test, bool &do_progress)
125 if ((in_desiredTest) && (in_desiredTest[0] != '\0')) {
127 const char *ptr = strstr(in_desiredTest, "::");
130 if ( m_bExactMatch ) {
131 match = (strncmp( in_desiredTest, in_className, strlen( in_className )) == 0 && in_desiredTest[strlen( in_className )] == ':')
132 && (strcmp( ptr + 2, in_functionName ) == 0);
135 match = (strncmp( in_desiredTest, in_className, strlen( in_className )) == 0 && in_desiredTest[strlen( in_className )] == ':')
136 && (strncmp( ptr + 2, in_functionName, strlen( ptr + 2 ) ) == 0);
138 // Invert shall not make explicit test run:
139 return invert ? (match ? !match : !explicit_test)
142 bool match = (strcmp(in_desiredTest, in_className) == 0);
144 return !explicit_test && (match == !invert);
147 return !explicit_test;
151 void TestCase::print_gc_state()
153 if ( m_bPrintGCState ) {
155 cds::gc::hp::GarbageCollector::InternalState stat;
156 std::cout << cds::gc::hp::GarbageCollector::instance().getInternalState( stat ) << std::endl;
161 void Config::load( const char * fileName )
165 if ( !s.is_open() ) {
166 std::cerr << "WARNING: Cannot open test cfg file " << fileName
167 << "\n\tUse default settings"
172 std::cout << "Using test config file: " << fileName << std::endl;
176 TestCfg * pMap = nullptr;
178 s.getline( buf, sizeof(buf)/sizeof(buf[0]) );
181 while ( *pszStr != 0 && (*pszStr == ' ' || *pszStr == '\t' )) ++pszStr;
183 char * pszEnd = strchr( pszStr, 0 );
184 if ( pszEnd == pszStr ) // empty srtring
187 while ( pszEnd != pszStr && (*pszEnd ==' ' || *pszEnd=='\t' || *pszEnd=='\n' || *pszEnd=='\r' )) --pszEnd;
189 if ( pszStr == pszEnd ) // empty string
194 if ( *pszStr == '#' ) // comment
197 if ( *pszStr == '[' && *pszEnd == ']' ) { // chapter header
199 pMap = &( m_Cfg[ pszStr + 1 ] );
206 char * pszEq = strchr( pszStr, '=' );
209 if ( pszEq == pszStr )
213 while ( pszStr <= --pszEnd && (*pszEnd ==' ' || *pszEnd=='\t' || *pszEnd=='\n' || *pszEnd=='\r') );
215 if ( pszEnd <= pszStr )
218 pMap->m_Cfg[ pszStr ] = pszEq + 1;
223 std::vector<std::string> const & TestCase::getTestStrings()
225 if ( m_arrStrings.empty() ) {
226 std::string strTestDir = m_strTestDataDir;
230 std::cout << "Loading test data " << strTestDir << "/dictionary.txt..." << std::endl;
231 fDict.open( (strTestDir + "/dictionary.txt").c_str() );
232 if ( fDict.is_open() ) {
233 cds::OS::Timer timer;
235 fDict >> str ; // number of lines in file
237 // Assume that dictionary.txt does not contain doubles.
238 CppUnitMini::TestCase::m_arrStrings.reserve( atol(str.c_str()) );
239 while ( !fDict.eof() ) {
240 fDict.getline( bufLine, sizeof(bufLine)/sizeof(bufLine[0]) );
242 m_arrStrings.push_back( bufLine );
246 std::cout << " Duration=" << timer.duration() << " String count " << CppUnitMini::TestCase::m_arrStrings.size() << std::endl;
249 std::cout << " Failed, file not found" << std::endl;
256 static void usage(const char* name)
258 printf("Usage : %s [-t=<class>[::<test>]] [-x=<class>[::<test>]] [-f=<file>] [-m]\n", name);
259 printf("\t[-t=<class>[::<test>]] : test class or class::test to execute\n");
260 printf("\t[-x=<class>[::<test>]] : test class or class::test to exclude from execution\n");
261 printf("\t[-d=dir] : test data directory (default is .)\n");
262 printf( "\t[-f=<file>] : output file\n" );
263 //printf(";\n\t[-m] : monitor test execution, display time duration for each test\n");
264 printf( "\t[-exact-match] : class::test should be exactly matched to existing test\n" );
265 printf("\t[-gc_state] : print gc state after each test\n");
266 printf("\t[-cfg=<file>] : config file name for tests\n");
269 int main(int argc, char** argv)
272 #ifdef CDS_MSVC_MEMORY_LEAKS_DETECTING_ENABLED
273 _CrtSetDbgFlag ( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );
276 // CppUnit(mini) test launcher
277 // command line option syntax:
280 // -t=CLASS[::TEST] run the test class CLASS or member test CLASS::TEST
281 // -x=CLASS[::TEST] run all except the test class CLASS or member test CLASS::TEST
282 // -d=dir test data directory (default is .)
283 // -f=FILE save output in file FILE instead of stdout
284 // -m monitor test(s) execution
285 // -gc_state print GC state after test
286 const char *fileName = 0;
287 const char *testName = "";
288 const char *xtestName = "";
289 const char *testDataDir = ".";
290 const char *cfgFileName =
297 bool doMonitoring = false;
299 for (int i = 1; i < argc; ++i) {
300 if (argv[i][0] == '-') {
301 if ( strncmp(argv[i], "-t=", 3) == 0 ) {
302 testName = argv[i]+3;
305 else if ( strncmp(argv[i], "-f=", 3) == 0 ) {
306 fileName = argv[i]+3;
309 else if ( strncmp(argv[i], "-x=", 3) == 0 ) {
310 xtestName = argv[i]+3;
313 else if ( strncmp(argv[i], "-d=", 3) == 0 ) {
314 testDataDir = argv[i] + 3;
317 else if ( strncmp( argv[i], "-m", 2 ) == 0 ) {
321 else if ( strncmp( argv[i], "-exact-match", 12 ) == 0 ) {
322 CppUnitMini::TestCase::m_bExactMatch = true;
325 else if ( strncmp(argv[i], "-gc_state", 9) == 0 ) {
326 CppUnitMini::TestCase::m_bPrintGCState = true;
329 else if( strncmp(argv[i], "-cfg=", 5) == 0 ) {
330 cfgFileName = argv[i] + 5;
335 // invalid option, we display normal usage.
342 boost::posix_time::ptime cur( boost::posix_time::second_clock::local_time());
344 std::cout << "libcds version " << CDS_VERSION_STRING << "\n";
345 std::cout << "Test started " << cur << std::endl;
349 CppUnitMini::TestCase::m_strTestDataDir = testDataDir;
351 CppUnitMini::Reporter* reporter;
353 reporter = new CppUnitMini::FileReporter(fileName, doMonitoring);
355 reporter = new CppUnitMini::FileReporter(stdout, doMonitoring);
357 // Load config params
358 CppUnitMini::TestCase::m_Cfg.load( cfgFileName );
365 size_t nHazardPtrCount = 0;
367 CppUnitMini::TestCfg& cfg = CppUnitMini::TestCase::m_Cfg.get( "General" );
368 nHazardPtrCount = cfg.getULong( "hazard_pointer_count", 0 );
371 // Safe reclamation schemes
372 cds::gc::HP hzpGC( nHazardPtrCount );
376 typedef cds::urcu::gc< cds::urcu::general_instant<> > rcu_gpi;
379 typedef cds::urcu::gc< cds::urcu::general_buffered<> > rcu_gpb;
382 typedef cds::urcu::gc< cds::urcu::general_threaded<> > rcu_gpt;
385 #ifdef CDS_URCU_SIGNAL_HANDLING_ENABLED
386 typedef cds::urcu::gc< cds::urcu::signal_buffered<> > rcu_shb;
387 rcu_shb shbRCU( 256, SIGUSR1 );
389 typedef cds::urcu::gc< cds::urcu::signal_threaded<> > rcu_sht;
390 rcu_sht shtRCU( 256, SIGUSR2 );
396 << "System topology:\n"
397 << " Logical processor count: " << cds::OS::topology::processor_count() << "\n";
398 std::cout << std::endl;
402 CppUnitMini::TestCfg& cfg = CppUnitMini::TestCase::m_Cfg.get( "General" );
403 std::string strHZPScanStrategy = cfg.get( "HZP_scan_strategy", std::string("classic") );
404 if ( strHZPScanStrategy == "inplace" )
405 hzpGC.setScanType( cds::gc::HP::scan_type::inplace );
406 else if ( strHZPScanStrategy == "classic" )
407 hzpGC.setScanType( cds::gc::HP::scan_type::classic );
409 std::cout << "Error value of HZP_scan_strategy in General section of test config\n";
412 switch (hzpGC.getScanType()) {
413 case cds::gc::HP::scan_type::inplace:
414 std::cout << "Use in-place scan strategy for Hazard Pointer memory reclamation algorithm\n";
416 case cds::gc::HP::scan_type::classic:
417 std::cout << "Use classic scan strategy for Hazard Pointer memory reclamation algorithm\n";
420 std::cout << "ERROR: use unknown scan strategy for Hazard Pointer memory reclamation algorithm\n";
424 std::cout << " Hazard Pointer count: " << hzpGC.max_hazard_count() << "\n"
425 << " Max thread count for HP: " << hzpGC.max_thread_count() << "\n"
426 << "Retired HP array capacity: " << hzpGC.retired_array_capacity() << "\n";
429 if ( CppUnitMini::TestCase::m_bPrintGCState ) {
430 cds::gc::hp::GarbageCollector::InternalState stat;
431 cds::gc::hp::GarbageCollector::instance().getInternalState( stat );
433 std::cout << "HP constants:"
434 << "\n\tHP count per thread=" << stat.nHPCount
435 << "\n\tMax thread count=" << stat.nMaxThreadCount
436 << "\n\tMax retired pointer count per thread=" << stat.nMaxRetiredPtrCount
437 << "\n\tHP record size in bytes=" << stat.nHPRecSize
438 << "\n" << std::endl;
441 // Attach main thread to CDS GC
442 cds::threading::Manager::attachThread();
444 if (xtestName[0] != 0)
445 num_errors = CppUnitMini::TestCase::run(reporter, xtestName, true);
447 num_errors = CppUnitMini::TestCase::run(reporter, testName);
449 // Detach main thread from CDS GC
450 cds::threading::Manager::detachThread();
454 // Finalize CDS runtime
457 reporter->printSummary();