instrumt.c revision 677833bc953b6cb418c701facbdcf4aa18d6c44e
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is the Netscape Portable Runtime (NSPR).
*
* The Initial Developer of the Original Code is
* Netscape Communications Corporation.
* Portions created by the Initial Developer are Copyright (C) 1998-2000
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
/*
** File: instrumt.c
** Description: This test is for the NSPR debug aids defined in
** prcountr.h, prtrace.h, prolock.h
**
** The test case tests the three debug aids in NSPR:
**
** Diagnostic messages can be enabled using "instrumt -v 6"
** This sets the msgLevel to something that PR_LOG() likes.
** Also define in the environment "NSPR_LOG_MODULES=Test:6"
**
** CounterTest() tests the counter facility. This test
** creates 4 threads. Each thread either increments, decrements,
** adds to or subtracts from a counter, depending on an argument
** passed to the thread at thread-create time. Each of these threads
** does COUNT_LIMIT iterations doing its thing. When all 4 threads
** are done, the result of the counter is evaluated. If all was atomic,
** the the value of the counter should be zero.
**
** TraceTest():
** This test mingles with the counter test. Counters trace.
** A thread to extract trace entries on the fly is started.
** A thread to dump trace entries to a file is started.
**
** OrderedLockTest():
**
**
**
**
**
*/
#include <stdio.h>
#include <plstr.h>
#include <prclist.h>
#include <prmem.h>
#include <plgetopt.h>
#include <prlog.h>
#include <prmon.h>
#include <pratom.h>
#include <prtrace.h>
#include <prcountr.h>
#include <prolock.h>
typedef enum
{
CountLoop = 1,
TraceLoop = 2,
TraceFlow = 3
} TraceTypes;
PRInt32 activeThreads = 0;
static void Help(void)
{
printf("Help? ... Ha!\n");
}
static void ListCounters(void)
{
PR_DEFINE_COUNTER( qh );
PR_DEFINE_COUNTER( rh );
{
{
( "QName: %s RName: %s Desc: %s Value: %ld\n",
}
}
return;
} /* end ListCounters() */
static void ListTraces(void)
{
PR_DEFINE_TRACE( qh );
PR_DEFINE_TRACE( rh );
{
{
( "QName: %s RName: %s Desc: %s",
}
}
return;
} /* end ListCounters() */
/*
** Thread to iteratively count something.
*/
{
PRInt32 i;
("CountSomething: begin thread %ld", switchVar ));
for ( i = 0; i < COUNT_LIMIT ; i++)
{
switch ( switchVar )
{
case 1 :
break;
case 2 :
break;
case 3 :
break;
case 4 :
break;
default :
PR_ASSERT( 0 );
break;
}
} /* end for() */
("CounterSomething: end thread %ld", switchVar ));
return;
} /* end CountSomething() */
/*
** Create the counter threads.
*/
static void CounterTest( void )
{
PRIntn i = 0;
PR_DEFINE_COUNTER( tc );
("Begin CounterTest"));
/*
** Test Get and Set of a counter.
**
*/
PR_GET_COUNTER( i, zCounter );
if ( i != 9 )
{
}
activeThreads += 4;
0);
0);
0);
0);
("Counter Threads started"));
ListCounters();
return;
} /* end CounterTest() */
/*
** Thread to dump trace buffer to a file.
*/
{
return;
} /* end RecordTrace() */
#define NUM_TRACE_RECORDS ( 10000 )
/*
** Thread to extract and print trace entries from the buffer.
*/
{
#if defined(DEBUG) || defined(FORCE_NSPR_TRACE)
PRInt32 i;
do
{
if ( found != 0)
{
for ( i = 0 ; i < found; i++ )
{
("SampleTrace, detail: Thread: %p, Time: %llX, UD0: %ld, UD1: %ld, UD2: %8.8ld",
(foundEntries +i)->thread,
(foundEntries +i)->time,
(foundEntries +i)->userData[0],
}
}
}
PR_Free( foundEntries );
("SampleTrace(): exiting"));
#endif
return;
} /* end RecordTrace() */
/*
** Basic trace test.
*/
static void TraceTest( void )
{
PRInt32 i;
PR_DEFINE_TRACE( th );
("Begin TraceTest"));
PR_GET_TRACE_OPTION( PRTraceBufSize, &i );
activeThreads += 2;
0);
SampleTrace, 0,
0);
ListTraces();
("End TraceTest"));
return;
} /* end TraceTest() */
/*
** Ordered lock test.
*/
static void OrderedLockTest( void )
{
("Begin OrderedLockTest"));
} /* end OrderedLockTest() */
{
#if defined(DEBUG) || defined(FORCE_NSPR_TRACE)
{
if (PL_OPT_BAD == os) continue;
{
case 'v': /* verbose mode */
break;
case 'h': /* help message */
Help();
break;
default:
break;
}
}
mon = PR_NewMonitor();
PR_EnterMonitor( mon );
TraceTest();
CounterTest();
/* Wait for all threads to exit */
while ( activeThreads > 0 ) {
if ( activeThreads == 1 )
}
PR_ExitMonitor( mon );
/*
** Evaluate results
*/
if ( counter != 0 )
{
("Expected counter == 0, found: %ld", counter));
printf("FAIL\n");
}
else
{
printf("PASS\n");
}
PR_DestroyMonitor( mon );
#else
printf("Test not defined\n");
#endif
return 0;
} /* main() */
/* end instrumt.c */