1 /* This testcase is part of GDB, the GNU debugger.
3 Copyright 2010-2016 Free Software Foundation, Inc.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */
18 /* This program tests tracepoint speed. It consists of two identical
19 loops, which in normal execution will run for exactly the same
20 amount of time. A tracepoint in the second loop will slow it down
21 by some amount, and then the program will report the slowdown
24 /* While primarily designed for the testsuite, it can also be used
25 for interactive testing. */
30 #include <sys/resource.h>
32 int trace_speed_test (void);
34 /* We mark these globals as volatile so the speed-measuring loops
35 don't get totally emptied out at high optimization levels. */
37 volatile int globfoo
, globfoo2
, globfoo3
;
39 volatile short globarr
[80000];
41 int init_iters
= 10 * 1000;
45 int max_iters
= 1000 * 1000 * 1000;
49 unsigned long long now2
, now3
, now4
, now5
;
50 int total1
, total2
, idelta
, mindelta
, nsdelta
;
53 /* Return CPU usage (both user and system - trap-based tracepoints use
54 a bunch of system time). */
59 struct timeval tm
, tm2
;
61 getrusage (RUSAGE_SELF
, &ru
);
64 return (((unsigned long long) tm
.tv_sec
) * 1000000) + tm
.tv_usec
65 + (((unsigned long long) tm2
.tv_sec
) * 1000000) + tm2
.tv_usec
;
69 main(int argc
, char **argv
)
77 numtps
= 1; /* set pre-run breakpoint here */
79 /* Keep trying the speed test, with more iterations, until
80 we get to a reasonable number. */
81 while (problem
= trace_speed_test())
83 /* If iteration isn't working, give up. */
84 if (iters
> max_iters
)
86 printf ("Gone over %d iterations, giving up\n", max_iters
);
91 printf ("Negative times, giving up\n", max_iters
);
96 printf ("Doubled iterations to %d\n", iters
);
99 printf ("Tracepoint time is %d ns\n", nspertp
);
101 /* This is for the benefit of interactive testing and attaching,
102 keeps the program from pegging the machine. */
103 sleep (1); /* set post-run breakpoint here */
105 /* Issue a little bit of output periodically, so we can see if
106 program is alive or hung. */
107 printf ("%s keeping busy, clock=%llu\n", argv
[0], myclock ());
113 trace_speed_test (void)
117 /* Overall loop run time deltas under 1 ms are likely noise and
118 should be ignored. */
121 // The bodies of the two loops following must be identical.
125 for (i
= 0; i
< iters
; ++i
)
128 globfoo2
+= globfoo
+ globfoo3
;
129 globfoo2
*= globfoo
+ globfoo3
;
130 globfoo2
-= globarr
[4] + globfoo3
;
131 globfoo2
*= globfoo
+ globfoo3
;
132 globfoo2
+= globfoo
+ globfoo3
;
135 total1
= now3
- now2
;
139 for (i
= 0; i
< iters
; ++i
)
142 globfoo2
+= globfoo
+ globfoo3
; /* set tracepoint here */
143 globfoo2
*= globfoo
+ globfoo3
;
144 globfoo2
-= globarr
[4] + globfoo3
;
145 globfoo2
*= globfoo
+ globfoo3
;
146 globfoo2
+= globfoo
+ globfoo3
;
149 total2
= now5
- now4
;
151 /* Report on the test results. */
155 idelta
= total2
- total1
;
157 printf ("Loops took %d usec and %d usec, delta is %d usec, %d iterations\n",
158 total1
, total2
, idelta
, iters
);
160 /* If the second loop seems to run faster, things are weird so give up. */
164 if (idelta
> mindelta
165 /* Total test time should be between 2 and 5 seconds. */
166 && (total1
+ total2
) > (2 * 1000000)
167 && (total1
+ total2
) < (5 * 1000000))
169 nsdelta
= (((unsigned long long) idelta
) * 1000) / iters
;
170 printf ("Second loop took %d ns longer per iter than first\n", nsdelta
);
171 nspertp
= nsdelta
/ numtps
;
172 printf ("%d ns per tracepoint\n", nspertp
);
173 printf ("Base iteration time %d ns\n",
174 ((int) (((unsigned long long) total1
) * 1000) / iters
));
175 printf ("Total test time %d secs\n", ((int) ((now5
- now2
) / 1000000)));
177 /* Speed test ran with no problem. */
181 /* The test run was too brief, or otherwise not useful. */
This page took 0.039224 seconds and 4 git commands to generate.