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 TestCase * TestCase::m_pCurTestCase = nullptr;
88 TestCase *TestCase::m_root = 0;
89 Reporter *TestCase::m_reporter = 0;
91 void TestCase::registerTestCase(TestCase *in_testCase) {
92 in_testCase->m_next = m_root;
96 int TestCase::run(Reporter *in_reporter, const char *in_testName, bool invert)
98 TestCase::m_reporter = in_reporter;
103 TestCase *tmp = m_root;
105 m_pCurTestCase = tmp;
107 tmp->myRun(in_testName, invert);
108 } catch ( std::exception& ex ) {
109 in_reporter->message( "EXCEPTION: ");
110 in_reporter->message( ex.what() );
118 bool TestCase::shouldRunThis(const char *in_desiredTest, const char *in_className, const char *in_functionName,
119 bool invert, bool explicit_test, bool &do_progress)
121 if ((in_desiredTest) && (in_desiredTest[0] != '\0')) {
123 const char *ptr = strstr(in_desiredTest, "::");
126 if ( m_bExactMatch ) {
127 match = (strncmp( in_desiredTest, in_className, strlen( in_className )) == 0 && in_desiredTest[strlen( in_className )] == ':')
128 && (strcmp( ptr + 2, in_functionName ) == 0);
131 match = (strncmp( in_desiredTest, in_className, strlen( in_className )) == 0 && in_desiredTest[strlen( in_className )] == ':')
132 && (strncmp( ptr + 2, in_functionName, strlen( ptr + 2 ) ) == 0);
134 // Invert shall not make explicit test run:
135 return invert ? (match ? !match : !explicit_test)
138 bool match = (strcmp(in_desiredTest, in_className) == 0);
140 return !explicit_test && (match == !invert);
143 return !explicit_test;
147 void TestCase::print_gc_state()
149 if ( m_bPrintGCState ) {
151 cds::gc::hp::GarbageCollector::InternalState stat;
152 std::cout << cds::gc::hp::GarbageCollector::instance().getInternalState( stat ) << std::endl;
157 void Config::load( const char * fileName )
161 if ( !s.is_open() ) {
162 std::cerr << "WARNING: Cannot open test cfg file " << fileName
163 << "\n\tUse default settings"
168 std::cout << "Using test config file: " << fileName << std::endl;
172 TestCfg * pMap = nullptr;
174 s.getline( buf, sizeof(buf)/sizeof(buf[0]) );
177 while ( *pszStr != 0 && (*pszStr == ' ' || *pszStr == '\t' )) ++pszStr;
179 char * pszEnd = strchr( pszStr, 0 );
180 if ( pszEnd == pszStr ) // empty srtring
183 while ( pszEnd != pszStr && (*pszEnd ==' ' || *pszEnd=='\t' || *pszEnd=='\n' || *pszEnd=='\r' )) --pszEnd;
185 if ( pszStr == pszEnd ) // empty string
190 if ( *pszStr == '#' ) // comment
193 if ( *pszStr == '[' && *pszEnd == ']' ) { // chapter header
195 pMap = &( m_Cfg[ pszStr + 1 ] );
202 char * pszEq = strchr( pszStr, '=' );
205 if ( pszEq == pszStr )
209 while ( pszStr <= --pszEnd && (*pszEnd ==' ' || *pszEnd=='\t' || *pszEnd=='\n' || *pszEnd=='\r') );
211 if ( pszEnd <= pszStr )
214 pMap->m_Cfg[ pszStr ] = pszEq + 1;
219 std::vector<std::string> const & TestCase::getTestStrings()
221 if ( m_arrStrings.empty() ) {
222 std::string strTestDir = m_strTestDataDir;
226 std::cout << "Loading test data " << strTestDir << "/dictionary.txt..." << std::endl;
227 fDict.open( (strTestDir + "/dictionary.txt").c_str() );
228 if ( fDict.is_open() ) {
229 cds::OS::Timer timer;
231 fDict >> str ; // number of lines in file
233 // Assume that dictionary.txt does not contain doubles.
234 CppUnitMini::TestCase::m_arrStrings.reserve( atol(str.c_str()) );
235 while ( !fDict.eof() ) {
236 fDict.getline( bufLine, sizeof(bufLine)/sizeof(bufLine[0]) );
238 m_arrStrings.push_back( bufLine );
242 std::cout << " Duration=" << timer.duration() << " String count " << CppUnitMini::TestCase::m_arrStrings.size() << std::endl;
245 std::cout << " Failed, file not found" << std::endl;
252 static void usage(const char* name)
254 printf("Usage : %s [-t=<class>[::<test>]] [-x=<class>[::<test>]] [-f=<file>] [-m]\n", name);
255 printf("\t[-t=<class>[::<test>]] : test class or class::test to execute\n");
256 printf("\t[-x=<class>[::<test>]] : test class or class::test to exclude from execution\n");
257 printf("\t[-d=dir] : test data directory (default is .)\n");
258 printf( "\t[-f=<file>] : output file\n" );
259 //printf(";\n\t[-m] : monitor test execution, display time duration for each test\n");
260 printf( "\t[-exact-match] : class::test should be exactly matched to existing test\n" );
261 printf("\t[-gc_state] : print gc state after each test\n");
262 printf("\t[-cfg=<file>] : config file name for tests\n");
265 int main(int argc, char** argv)
268 #ifdef CDS_MSVC_MEMORY_LEAKS_DETECTING_ENABLED
269 _CrtSetDbgFlag ( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );
272 // CppUnit(mini) test launcher
273 // command line option syntax:
276 // -t=CLASS[::TEST] run the test class CLASS or member test CLASS::TEST
277 // -x=CLASS[::TEST] run all except the test class CLASS or member test CLASS::TEST
278 // -d=dir test data directory (default is .)
279 // -f=FILE save output in file FILE instead of stdout
280 // -m monitor test(s) execution
281 // -gc_state print GC state after test
282 const char *fileName = 0;
283 const char *testName = "";
284 const char *xtestName = "";
285 const char *testDataDir = ".";
286 const char *cfgFileName =
293 bool doMonitoring = false;
295 for (int i = 1; i < argc; ++i) {
296 if (argv[i][0] == '-') {
297 if ( strncmp(argv[i], "-t=", 3) == 0 ) {
298 testName = argv[i]+3;
301 else if ( strncmp(argv[i], "-f=", 3) == 0 ) {
302 fileName = argv[i]+3;
305 else if ( strncmp(argv[i], "-x=", 3) == 0 ) {
306 xtestName = argv[i]+3;
309 else if ( strncmp(argv[i], "-d=", 3) == 0 ) {
310 testDataDir = argv[i] + 3;
313 else if ( strncmp( argv[i], "-m", 2 ) == 0 ) {
317 else if ( strncmp( argv[i], "-exact-match", 12 ) == 0 ) {
318 CppUnitMini::TestCase::m_bExactMatch = true;
321 else if ( strncmp(argv[i], "-gc_state", 9) == 0 ) {
322 CppUnitMini::TestCase::m_bPrintGCState = true;
325 else if( strncmp(argv[i], "-cfg=", 5) == 0 ) {
326 cfgFileName = argv[i] + 5;
331 // invalid option, we display normal usage.
338 boost::posix_time::ptime cur( boost::posix_time::second_clock::local_time());
340 std::cout << "libcds version " << CDS_VERSION_STRING << "\n";
341 std::cout << "Test started " << cur << std::endl;
345 CppUnitMini::TestCase::m_strTestDataDir = testDataDir;
347 CppUnitMini::Reporter* reporter;
349 reporter = new CppUnitMini::FileReporter(fileName, doMonitoring);
351 reporter = new CppUnitMini::FileReporter(stdout, doMonitoring);
353 // Load config params
354 CppUnitMini::TestCase::m_Cfg.load( cfgFileName );
361 size_t nHazardPtrCount = 0;
363 CppUnitMini::TestCfg& cfg = CppUnitMini::TestCase::m_Cfg.get( "General" );
364 nHazardPtrCount = cfg.getULong( "hazard_pointer_count", 0 );
367 // Safe reclamation schemes
368 cds::gc::HP hzpGC( nHazardPtrCount );
372 typedef cds::urcu::gc< cds::urcu::general_instant<> > rcu_gpi;
375 typedef cds::urcu::gc< cds::urcu::general_buffered<> > rcu_gpb;
378 typedef cds::urcu::gc< cds::urcu::general_threaded<> > rcu_gpt;
381 #ifdef CDS_URCU_SIGNAL_HANDLING_ENABLED
382 typedef cds::urcu::gc< cds::urcu::signal_buffered<> > rcu_shb;
383 rcu_shb shbRCU( 256, SIGUSR1 );
385 typedef cds::urcu::gc< cds::urcu::signal_threaded<> > rcu_sht;
386 rcu_sht shtRCU( 256, SIGUSR2 );
392 << "System topology:\n"
393 << " Logical processor count: " << cds::OS::topology::processor_count() << "\n";
394 std::cout << std::endl;
398 CppUnitMini::TestCfg& cfg = CppUnitMini::TestCase::m_Cfg.get( "General" );
399 std::string strHZPScanStrategy = cfg.get( "HZP_scan_strategy", std::string("classic") );
400 if ( strHZPScanStrategy == "inplace" )
401 hzpGC.setScanType( cds::gc::HP::scan_type::inplace );
402 else if ( strHZPScanStrategy == "classic" )
403 hzpGC.setScanType( cds::gc::HP::scan_type::classic );
405 std::cout << "Error value of HZP_scan_strategy in General section of test config\n";
408 switch (hzpGC.getScanType()) {
409 case cds::gc::HP::scan_type::inplace:
410 std::cout << "Use in-place scan strategy for Hazard Pointer memory reclamation algorithm\n";
412 case cds::gc::HP::scan_type::classic:
413 std::cout << "Use classic scan strategy for Hazard Pointer memory reclamation algorithm\n";
416 std::cout << "ERROR: use unknown scan strategy for Hazard Pointer memory reclamation algorithm\n";
420 std::cout << " Hazard Pointer count: " << hzpGC.max_hazard_count() << "\n"
421 << " Max thread count for HP: " << hzpGC.max_thread_count() << "\n"
422 << "Retired HP array capacity: " << hzpGC.retired_array_capacity() << "\n";
425 if ( CppUnitMini::TestCase::m_bPrintGCState ) {
426 cds::gc::hp::GarbageCollector::InternalState stat;
427 cds::gc::hp::GarbageCollector::instance().getInternalState( stat );
429 std::cout << "HP constants:"
430 << "\n\tHP count per thread=" << stat.nHPCount
431 << "\n\tMax thread count=" << stat.nMaxThreadCount
432 << "\n\tMax retired pointer count per thread=" << stat.nMaxRetiredPtrCount
433 << "\n\tHP record size in bytes=" << stat.nHPRecSize
434 << "\n" << std::endl;
437 // Attach main thread to CDS GC
438 cds::threading::Manager::attachThread();
440 if (xtestName[0] != 0)
441 num_errors = CppUnitMini::TestCase::run(reporter, xtestName, true);
443 num_errors = CppUnitMini::TestCase::run(reporter, testName);
445 // Detach main thread from CDS GC
446 cds::threading::Manager::detachThread();
450 // Finalize CDS runtime
453 reporter->printSummary();
459 // See doc/README.intel for explanation about this code
460 #if defined (STLPORT) && defined (__ICL) && (__ICL >= 900) && \
461 (_STLP_MSVC_LIB < 1300) && defined (_STLP_USE_DYNAMIC_LIB)
462 # include <exception>
467 void _STLP_CALL unexpected() {
468 unexpected_handler hdl;
469 set_unexpected(hdl = set_unexpected((unexpected_handler)0));