-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexec.c
149 lines (126 loc) · 3.11 KB
/
exec.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*
* Test PAPI across exec().
*
* On some old perfctr systems, if a process execs with an actively
* running PAPI_overflow(), then PAPI_library_init() will fail in the
* child. The workaround is to call PAPI_shutdown() before the exec.
*
* Copyright (c) 2009-2013, Rice University.
* See the file LICENSE for details.
*
* Mark W. Krentel, Rice University
* October 2009
*
* $Id: exec.c 278 2013-01-03 04:17:10Z krentel $
*/
#include <sys/time.h>
#include <sys/types.h>
#include <err.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <papi.h>
#include "papi-tests.h"
static struct prog_args args;
static int EventSet;
static struct timeval start, last;
static long count = 0;
static long total = 0;
void
my_handler(int EventSet, void *pc, long long ovec, void *context)
{
count++;
total++;
}
void
wait_for_time(int len)
{
struct timeval begin_cycles, now;
gettimeofday(&begin_cycles, NULL);
last = begin_cycles;
count = 0;
for (;;) {
run_flops(10);
gettimeofday(&now, NULL);
if (now.tv_sec > last.tv_sec) {
printf("pid: %d, time: %ld, count = %ld\n",
getpid(), now.tv_sec - start.tv_sec, count);
count = 0;
last = now;
}
if (now.tv_sec >= begin_cycles.tv_sec + len)
break;
}
}
void
my_papi_start(void)
{
if (PAPI_library_init(PAPI_VER_CURRENT) != PAPI_VER_CURRENT)
errx(1, "PAPI_library_init failed");
EventSet = event_set_for_overflow(&args, &my_handler);
if (PAPI_start(EventSet) != PAPI_OK)
errx(1, "PAPI_start failed");
}
/*
* For convenience, main() serves as both the parent and the child,
* as determined by the command-line arguments.
*
* No args: exec() with active PAPI overflow.
*
* One arg: add PAPI_stop() before exec().
*
* Two args: add PAPI_shutdown().
*
* Three args: be the child and don't exec().
*/
int
main(int argc, char **argv)
{
int parent = (argc < 4);
set_default_args(&args);
TOT_CYC_DEFAULT(args);
if (argc >= 2 && strcmp(argv[1], "-h") == 0) {
usage(argv[0]);
exit(0);
}
if (argc == 1)
printf("Exec test, with active PAPI_overflow across exec\n");
else if (argc == 2)
printf("Exec test, with PAPI_stop workaround\n");
else if (argc == 3)
printf("Exec test, with PAPI_shutdown workaround\n");
if (parent)
print_event_list(&args);
gettimeofday(&start, NULL);
/*
* Have the child wait a short time to see if the parent's PAPI
* interrupts the child.
*/
if (! parent) {
wait_for_time(4);
}
printf("---> PAPI start\n");
my_papi_start();
wait_for_time(4);
if (parent) {
if (argc >= 2) {
printf("---> PAPI stop\n");
PAPI_stop(EventSet, NULL);
}
if (argc >= 3) {
printf("---> PAPI shutdown\n");
PAPI_shutdown();
}
printf("---> exec\n");
execl(argv[0], argv[0], "x", "x", "x", NULL);
errx(1, "execl failed");
}
/* child process */
printf("---> PAPI shutdown\n");
PAPI_stop(EventSet, NULL);
PAPI_shutdown();
EXIT_PASS_FAIL(total > 50);
return (0);
}