]>
git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/testsuite/gdb.trace/tspeed.c
1 /* This testcase is part of GDB, the GNU debugger.
3 Copyright 2010-2018 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). */
60 gettimeofday (&tm
, NULL
);
61 return (((unsigned long long) tm
.tv_sec
) * 1000000) + tm
.tv_usec
;
65 main(int argc
, char **argv
)
73 numtps
= 1; /* set pre-run breakpoint here */
75 /* Keep trying the speed test, with more iterations, until
76 we get to a reasonable number. */
77 while (problem
= trace_speed_test())
79 /* If iteration isn't working, give up. */
80 if (iters
> max_iters
)
82 printf ("Gone over %d iterations, giving up\n", max_iters
);
87 printf ("Negative times, giving up\n", max_iters
);
92 printf ("Doubled iterations to %d\n", iters
);
95 printf ("Tracepoint time is %d ns\n", nspertp
);
97 /* This is for the benefit of interactive testing and attaching,
98 keeps the program from pegging the machine. */
99 sleep (1); /* set post-run breakpoint here */
101 /* Issue a little bit of output periodically, so we can see if
102 program is alive or hung. */
103 printf ("%s keeping busy, clock=%llu\n", argv
[0], myclock ());
109 trace_speed_test (void)
113 /* Overall loop run time deltas under 1 ms are likely noise and
114 should be ignored. */
117 // The bodies of the two loops following must be identical.
121 for (i
= 0; i
< iters
; ++i
)
124 globfoo2
+= globfoo
+ globfoo3
;
125 globfoo2
*= globfoo
+ globfoo3
;
126 globfoo2
-= globarr
[4] + globfoo3
;
127 globfoo2
*= globfoo
+ globfoo3
;
128 globfoo2
+= globfoo
+ globfoo3
;
131 total1
= now3
- now2
;
135 for (i
= 0; i
< iters
; ++i
)
138 globfoo2
+= globfoo
+ globfoo3
; /* set tracepoint here */
139 globfoo2
*= globfoo
+ globfoo3
;
140 globfoo2
-= globarr
[4] + globfoo3
;
141 globfoo2
*= globfoo
+ globfoo3
;
142 globfoo2
+= globfoo
+ globfoo3
;
145 total2
= now5
- now4
;
147 /* Report on the test results. */
151 idelta
= total2
- total1
;
153 printf ("Loops took %d usec and %d usec, delta is %d usec, %d iterations\n",
154 total1
, total2
, idelta
, iters
);
156 /* If the second loop seems to run faster, things are weird so give up. */
160 if (idelta
> mindelta
161 /* Total test time should be between 15 and 30 seconds. */
162 && (total1
+ total2
) > (15 * 1000000)
163 && (total1
+ total2
) < (30 * 1000000))
165 nsdelta
= (((unsigned long long) idelta
) * 1000) / iters
;
166 printf ("Second loop took %d ns longer per iter than first\n", nsdelta
);
167 nspertp
= nsdelta
/ numtps
;
168 printf ("%d ns per tracepoint\n", nspertp
);
169 printf ("Base iteration time %d ns\n",
170 ((int) (((unsigned long long) total1
) * 1000) / iters
));
171 printf ("Total test time %d secs\n", ((int) ((now5
- now2
) / 1000000)));
173 /* Speed test ran with no problem. */
177 /* The test run was too brief, or otherwise not useful. */