1 /* This testcase is part of GDB, the GNU debugger.
3 Copyright 2010-2022 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>
33 int trace_speed_test (void);
35 /* We mark these globals as volatile so the speed-measuring loops
36 don't get totally emptied out at high optimization levels. */
38 volatile int globfoo
, globfoo2
, globfoo3
;
40 volatile short globarr
[80000];
42 int init_iters
= 10 * 1000;
46 int max_iters
= 1000 * 1000 * 1000;
50 unsigned long long now2
, now3
, now4
, now5
;
51 int total1
, total2
, idelta
, mindelta
, nsdelta
;
54 /* Return CPU usage (both user and system - trap-based tracepoints use
55 a bunch of system time). */
61 gettimeofday (&tm
, NULL
);
62 return (((unsigned long long) tm
.tv_sec
) * 1000000) + tm
.tv_usec
;
66 main(int argc
, char **argv
)
74 numtps
= 1; /* set pre-run breakpoint here */
76 /* Keep trying the speed test, with more iterations, until
77 we get to a reasonable number. */
78 while ((problem
= trace_speed_test()))
80 /* If iteration isn't working, give up. */
81 if (iters
> max_iters
)
83 printf ("Gone over %d iterations, giving up\n", max_iters
);
88 printf ("Negative times, giving up\n");
93 printf ("Doubled iterations to %d\n", iters
);
96 printf ("Tracepoint time is %d ns\n", nspertp
);
98 /* This is for the benefit of interactive testing and attaching,
99 keeps the program from pegging the machine. */
100 sleep (1); /* set post-run breakpoint here */
102 /* Issue a little bit of output periodically, so we can see if
103 program is alive or hung. */
104 printf ("%s keeping busy, clock=%llu\n", argv
[0], myclock ());
110 trace_speed_test (void)
114 /* Overall loop run time deltas under 1 ms are likely noise and
115 should be ignored. */
118 // The bodies of the two loops following must be identical.
122 for (i
= 0; i
< iters
; ++i
)
125 globfoo2
+= globfoo
+ globfoo3
;
126 globfoo2
*= globfoo
+ globfoo3
;
127 globfoo2
-= globarr
[4] + globfoo3
;
128 globfoo2
*= globfoo
+ globfoo3
;
129 globfoo2
+= globfoo
+ globfoo3
;
132 total1
= now3
- now2
;
136 for (i
= 0; i
< iters
; ++i
)
139 globfoo2
+= globfoo
+ globfoo3
; /* set tracepoint here */
140 globfoo2
*= globfoo
+ globfoo3
;
141 globfoo2
-= globarr
[4] + globfoo3
;
142 globfoo2
*= globfoo
+ globfoo3
;
143 globfoo2
+= globfoo
+ globfoo3
;
146 total2
= now5
- now4
;
148 /* Report on the test results. */
152 idelta
= total2
- total1
;
154 printf ("Loops took %d usec and %d usec, delta is %d usec, %d iterations\n",
155 total1
, total2
, idelta
, iters
);
157 /* If the second loop seems to run faster, things are weird so give up. */
161 if (idelta
> mindelta
162 /* Total test time should be between 15 and 30 seconds. */
163 && (total1
+ total2
) > (15 * 1000000)
164 && (total1
+ total2
) < (30 * 1000000))
166 nsdelta
= (((unsigned long long) idelta
) * 1000) / iters
;
167 printf ("Second loop took %d ns longer per iter than first\n", nsdelta
);
168 nspertp
= nsdelta
/ numtps
;
169 printf ("%d ns per tracepoint\n", nspertp
);
170 printf ("Base iteration time %d ns\n",
171 ((int) (((unsigned long long) total1
) * 1000) / iters
));
172 printf ("Total test time %d secs\n", ((int) ((now5
- now2
) / 1000000)));
174 /* Speed test ran with no problem. */
178 /* The test run was too brief, or otherwise not useful. */