PAPI  5.0.1.0
zero_fork.c
Go to the documentation of this file.
00001 /* 
00002 * File:    zero_fork.c
00003 * CVS:     $Id$
00004 * Author:  Philip Mucci
00005 *          mucci@cs.utk.edu
00006 * Mods:    <your name here>
00007 *          <your email address>
00008 */
00009 
00010 /* This file performs the following test: start, stop and timer
00011 functionality for a parent and a forked child. */
00012 
00013 #include "papi_test.h"
00014 #include <sys/wait.h>
00015 
00016 int EventSet1 = PAPI_NULL;
00017 int PAPI_event, mask1;
00018 int num_events1 = 2;
00019 long long elapsed_us, elapsed_cyc;
00020 long long **values;
00021 char event_name[PAPI_MAX_STR_LEN];
00022 int retval, num_tests = 1;
00023 
00024 void
00025 process_init( void )
00026 {
00027     printf( "Process %d \n", ( int ) getpid(  ) );
00028 
00029     /* Initialize PAPI library */
00030     retval = PAPI_library_init( PAPI_VER_CURRENT );
00031     if ( retval != PAPI_VER_CURRENT ) {
00032        test_fail( __FILE__, __LINE__, "PAPI_library_init", retval );
00033     }
00034 
00035     /* add PAPI_TOT_CYC and one of the events in 
00036        PAPI_FP_INS, PAPI_FP_OPS or PAPI_TOT_INS, 
00037        depends on the availability of the event 
00038        on the platform                           */
00039     EventSet1 = add_two_events( &num_events1, &PAPI_event, &mask1 );
00040 
00041     values = allocate_test_space( num_tests, num_events1 );
00042 
00043     retval = PAPI_event_code_to_name( PAPI_event, event_name );
00044     if ( retval != PAPI_OK ) {
00045        test_fail( __FILE__, __LINE__, "PAPI_event_code_to_name", retval );
00046     }
00047 
00048     elapsed_us = PAPI_get_real_usec(  );
00049     elapsed_cyc = PAPI_get_real_cyc(  );
00050 
00051     retval = PAPI_start( EventSet1 );
00052     if ( retval != PAPI_OK ) {
00053         test_fail( __FILE__, __LINE__, "PAPI_start", retval );
00054     }
00055 }
00056 
00057 void
00058 process_fini( void )
00059 {
00060     retval = PAPI_stop( EventSet1, values[0] );
00061     if ( retval != PAPI_OK ) {
00062         test_fail( __FILE__, __LINE__, "PAPI_stop", retval );
00063     }
00064 
00065     elapsed_us = PAPI_get_real_usec(  ) - elapsed_us;
00066     elapsed_cyc = PAPI_get_real_cyc(  ) - elapsed_cyc;
00067 
00068     remove_test_events( &EventSet1, mask1 );
00069 
00070     printf( "Process %d %-12s : \t%lld\n", ( int ) getpid(  ), event_name,
00071             values[0][1] );
00072     printf( "Process %d PAPI_TOT_CYC : \t%lld\n", ( int ) getpid(  ),
00073             values[0][0] );
00074     printf( "Process %d Real usec    : \t%lld\n", ( int ) getpid(  ),
00075             elapsed_us );
00076     printf( "Process %d Real cycles  : \t%lld\n", ( int ) getpid(  ),
00077             elapsed_cyc );
00078 
00079     free_test_space( values, num_tests );
00080 
00081 }
00082 
00083 int
00084 main( int argc, char **argv )
00085 {
00086     int flops1;
00087     int retval;
00088 
00089     tests_quiet( argc, argv );  /* Set TESTS_QUIET variable */
00090 # if (defined(__ALPHA) && defined(__osf__))
00091     test_skip( __FILE__, __LINE__, "main: fork not supported.", 0 );
00092 #endif
00093 
00094     printf( "This tests if PAPI_library_init(),2*fork(),PAPI_library_init() works.\n" );
00095     /* Initialize PAPI for this process */
00096     process_init(  );
00097     flops1 = 1000000;
00098     if ( fork(  ) == 0 ) {
00099         /* Initialize PAPI for the child process */
00100         process_init(  );
00101         /* Let the child process do work */
00102         do_flops( flops1 );
00103         /* Measure the child process */
00104         process_fini(  );
00105         exit( 0 );
00106     }
00107     flops1 = 2000000;
00108     if ( fork(  ) == 0 ) {
00109         /* Initialize PAPI for the child process */
00110         process_init(  );
00111         /* Let the child process do work */
00112         do_flops( flops1 );
00113         /* Measure the child process */
00114         process_fini(  );
00115         exit( 0 );
00116     }
00117     /* Let this process do work */
00118     flops1 = 4000000;
00119     do_flops( flops1 );
00120 
00121     /* Wait for child to finish */
00122     wait( &retval );
00123     /* Wait for child to finish */
00124     wait( &retval );
00125 
00126     /* Measure this process */
00127     process_fini(  );
00128 
00129     test_pass( __FILE__, NULL, 0 );
00130     return 0;
00131 }
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines