PAPI 7.1.0.0
Loading...
Searching...
No Matches
attach2.c
Go to the documentation of this file.
1/* This file performs the following test: start, stop and timer functionality for
2 attached processes.
3
4 - It attempts to use the following two counters. It may use less depending on
5 hardware counter resource limitations. These are counted in the default counting
6 domain and default granularity, depending on the platform. Usually this is
7 the user domain (PAPI_DOM_USER) and thread context (PAPI_GRN_THR).
8 + PAPI_FP_INS
9 + PAPI_TOT_CYC
10 - Get us.
11 - Start counters
12 - Do flops
13 - Stop and read counters
14 - Get us.
15*/
16
17#include <stdio.h>
18#include <stdlib.h>
19#include <unistd.h>
20#include <string.h>
21#include <limits.h>
22#include <sys/ptrace.h>
23#include <sys/wait.h>
24
25#include "papi.h"
26#include "papi_test.h"
27
28#include "do_loops.h"
29
30
31#ifdef _AIX
32#define _LINUX_SOURCE_COMPAT
33#endif
34
35#if defined(__FreeBSD__)
36# define PTRACE_ATTACH PT_ATTACH
37# define PTRACE_TRACEME PT_TRACE_ME
38#endif
39
40static int
42{
43 char *path;
44 char newpath[PATH_MAX];
45 path = getenv("PATH");
46
47 sprintf(newpath, "PATH=./:%s", (path)?path:"\0" );
48 putenv(newpath);
49
50 if (ptrace(PTRACE_TRACEME, 0, 0, 0) == 0) {
51 execlp("attach_target","attach_target","100000000",NULL);
52 perror("execl(attach_target) failed");
53 }
54 perror("PTRACE_TRACEME");
55 return ( 1 );
56}
57
58int
59main( int argc, char **argv )
60{
61 int status, retval, tmp;
62 int EventSet1 = PAPI_NULL;
63 long long **values;
64 long long elapsed_us, elapsed_cyc, elapsed_virt_us, elapsed_virt_cyc;
67 const PAPI_component_info_t *cmpinfo;
68 pid_t pid;
69 int quiet;
70
71 /* Fork before doing anything with the PMU */
72
73 setbuf(stdout,NULL);
74 pid = fork( );
75 if ( pid < 0 )
76 test_fail( __FILE__, __LINE__, "fork()", PAPI_ESYS );
77 if ( pid == 0 )
78 exit( wait_for_attach_and_loop( ) );
79
80 /* Set TESTS_QUIET variable */
81 quiet=tests_quiet( argc, argv );
82
83
84 /* Master only process below here */
85
87 if ( retval != PAPI_VER_CURRENT )
88 test_fail( __FILE__, __LINE__, "PAPI_library_init", retval );
89
90 if ( ( cmpinfo = PAPI_get_component_info( 0 ) ) == NULL )
91 test_fail( __FILE__, __LINE__, "PAPI_get_component_info", 0 );
92
93 if ( cmpinfo->attach == 0 )
94 test_skip( __FILE__, __LINE__, "Platform does not support attaching",
95 0 );
96
98 if ( hw_info == NULL )
99 test_fail( __FILE__, __LINE__, "PAPI_get_hardware_info", 0 );
100
101 /* add PAPI_TOT_CYC and one of the events in PAPI_FP_INS, PAPI_FP_OPS or
102 PAPI_TOT_INS, depending on the availability of the event on the
103 platform */
105 if ( retval != PAPI_OK )
106 test_fail( __FILE__, __LINE__, "PAPI_create_eventset", retval );
107
108 /* Here we are testing that this does not cause a fail */
109
111 if ( retval != PAPI_OK )
112 test_fail( __FILE__, __LINE__, "PAPI_assign_eventset_component",
113 retval );
114
115 retval = PAPI_attach( EventSet1, ( unsigned long ) pid );
116 if ( retval != PAPI_OK ) {
117 if (!quiet) printf("Cannot attach: %s\n",PAPI_strerror(retval));
118 test_skip( __FILE__, __LINE__, "PAPI_attach", retval );
119 }
120
122 if ( retval != PAPI_OK ) {
123 if (!quiet) printf("Problem adding PAPI_TOT_CYC\n");
124 test_skip( __FILE__, __LINE__, "PAPI_add_event", retval );
125 }
126
127 strcpy(event_name,"PAPI_FP_INS");
128
130 if ( retval == PAPI_ENOEVNT ) {
131 strcpy(event_name,"PAPI_TOT_INS");
133 }
134
135 if ( retval != PAPI_OK ) {
136 test_fail( __FILE__, __LINE__, "PAPI_add_event", retval );
137 }
138
140
142
144
145 elapsed_virt_us = PAPI_get_virt_usec( );
146
147 elapsed_virt_cyc = PAPI_get_virt_cyc( );
148
149 if (!quiet) printf("must_ptrace is %d\n",cmpinfo->attach_must_ptrace);
150 pid_t child = wait( &status );
151 if (!quiet) printf( "Debugger exited wait() with %d\n",child );
152 if (WIFSTOPPED( status ))
153 {
154 if (!quiet) printf( "Child has stopped due to signal %d (%s)\n",
155 WSTOPSIG( status ), strsignal(WSTOPSIG( status )) );
156 }
157 if (WIFSIGNALED( status ))
158 {
159 if (!quiet) printf( "Child %ld received signal %d (%s)\n",
160 (long)child,
161 WTERMSIG(status) , strsignal(WTERMSIG( status )) );
162 }
163 if (!quiet) printf("After %d\n",retval);
164
166 if ( retval != PAPI_OK )
167 test_fail( __FILE__, __LINE__, "PAPI_start", retval );
168
169 if (!quiet) printf("Continuing\n");
170#if defined(__FreeBSD__)
171 if ( ptrace( PT_CONTINUE, pid, (vptr_t) 1, 0 ) == -1 ) {
172#else
173 if ( ptrace( PTRACE_CONT, pid, NULL, NULL ) == -1 ) {
174#endif
175 perror( "ptrace(PTRACE_CONT)" );
176 return 1;
177 }
178
179
180 do {
181 child = wait( &status );
182 if (!quiet) printf( "Debugger exited wait() with %d\n", child);
183 if (WIFSTOPPED( status ))
184 {
185 if (!quiet) printf( "Child has stopped due to signal %d (%s)\n",
186 WSTOPSIG( status ), strsignal(WSTOPSIG( status )) );
187 }
188 if (WIFSIGNALED( status ))
189 {
190 if (!quiet) printf( "Child %ld received signal %d (%s)\n",
191 (long)child,
192 WTERMSIG(status) , strsignal(WTERMSIG( status )) );
193 }
194 } while (!WIFEXITED( status ));
195
196 if (!quiet) printf("Child exited with value %d\n",WEXITSTATUS(status));
197 if (WEXITSTATUS(status) != 0) {
198 test_fail( __FILE__, __LINE__, "Exit status of child to attach to", PAPI_EMISC);
199 }
201 if ( retval != PAPI_OK )
202 test_fail( __FILE__, __LINE__, "PAPI_stop", retval );
203
204 elapsed_virt_us = PAPI_get_virt_usec( ) - elapsed_virt_us;
205
206 elapsed_virt_cyc = PAPI_get_virt_cyc( ) - elapsed_virt_cyc;
207
209
211
213 if (retval != PAPI_OK)
214 test_fail( __FILE__, __LINE__, "PAPI_cleanup_eventset", retval );
215
217 if (retval != PAPI_OK)
218 test_fail( __FILE__, __LINE__, "PAPI_destroy_eventset", retval );
219
220 if (!quiet) {
221 printf( "Test case: 3rd party attach start, stop.\n" );
222 printf( "-----------------------------------------------\n" );
223 tmp = PAPI_get_opt( PAPI_DEFDOM, NULL );
224 printf( "Default domain is: %d (%s)\n", tmp, stringify_all_domains( tmp ) );
225 tmp = PAPI_get_opt( PAPI_DEFGRN, NULL );
226 printf( "Default granularity is: %d (%s)\n", tmp,
228 printf( "Using %d iterations of c += a*b\n", NUM_FLOPS );
229 printf( "-------------------------------------------------------------------------\n" );
230
231 printf( "Test type : \t 1\n" );
232
233 printf( TAB1, "PAPI_TOT_CYC : \t", ( values[0] )[0] );
234 printf( "%s : \t %12lld\n",event_name, ( values[0] )[1]);
235 printf( TAB1, "Real usec : \t", elapsed_us );
236 printf( TAB1, "Real cycles : \t", elapsed_cyc );
237 printf( TAB1, "Virt usec : \t", elapsed_virt_us );
238 printf( TAB1, "Virt cycles : \t", elapsed_virt_cyc );
239
240 printf( "-------------------------------------------------------------------------\n" );
241
242 printf( "Verification: none\n" );
243 }
244
245 test_pass( __FILE__ );
246
247 return 0;
248
249}
double tmp
static int wait_for_attach_and_loop(void)
Definition: attach2.c:41
static const PAPI_hw_info_t * hw_info
Definition: byte_profile.c:28
add PAPI preset or native hardware event to an event set
add PAPI preset or native hardware event by name to an EventSet
Assign a component index to an existing but empty EventSet.
Attach PAPI event set to the specified thread id.
Empty and destroy an EventSet.
Create a new empty PAPI EventSet.
Empty and destroy an EventSet.
get information about a specific software component
get information about the system hardware
Get PAPI library or event set options.
get real time counter value in clock cycles Returns the total real time passed since some arbitrary s...
get real time counter value in microseconds
get virtual time counter value in clock cycles
get virtual time counter values in microseconds
initialize the PAPI library.
Start counting hardware events in an event set.
Stop counting hardware events in an event set.
Returns a string describing the PAPI error code.
char event_name[2][PAPI_MAX_STR_LEN]
Definition: data_range.c:29
#define PAPI_VER_CURRENT
Definition: f90papi.h:54
#define PAPI_OK
Definition: f90papi.h:73
#define PAPI_NULL
Definition: f90papi.h:78
#define PAPI_DEFGRN
Definition: f90papi.h:26
#define PAPI_TOT_CYC
Definition: f90papi.h:308
#define PAPI_ENOEVNT
Definition: f90papi.h:139
#define PAPI_MAX_STR_LEN
Definition: f90papi.h:77
#define PAPI_EMISC
Definition: f90papi.h:122
#define PAPI_ESYS
Definition: f90papi.h:136
#define PAPI_DEFDOM
Definition: f90papi.h:188
static long long values[NUM_EVENTS]
Definition: init_fini.c:10
Return codes and api definitions.
void * vptr_t
Definition: papi.h:576
FILE * stdout
int tests_quiet(int argc, char **argv)
Definition: test_utils.c:376
char * stringify_all_domains(int domains)
Definition: test_utils.c:293
char * stringify_granularity(int granularity)
Definition: test_utils.c:353
#define TAB1
Definition: papi_test.h:98
void PAPI_NORETURN test_fail(const char *file, int line, const char *call, int retval)
Definition: test_utils.c:491
long long ** allocate_test_space(int num_tests, int num_events)
Definition: test_utils.c:46
void PAPI_NORETURN test_pass(const char *filename)
Definition: test_utils.c:432
void PAPI_NORETURN test_skip(const char *file, int line, const char *call, int retval)
Definition: test_utils.c:584
int main()
Definition: pernode.c:20
int quiet
Definition: rapl_overflow.c:19
#define NUM_FLOPS
Definition: sdsc-mpx.c:24
static int pid
unsigned int attach
Definition: papi.h:658
unsigned int attach_must_ptrace
Definition: papi.h:659
Hardware info structure.
Definition: papi.h:774
int EventSet1
Definition: zero_fork.c:47
long long elapsed_cyc
Definition: zero_fork.c:50
long long elapsed_us
Definition: zero_fork.c:50
int retval
Definition: zero_fork.c:53