tspeed.c revision 1.6 1 1.1 christos /* This testcase is part of GDB, the GNU debugger.
2 1.1 christos
3 1.6 christos Copyright 2010-2016 Free Software Foundation, Inc.
4 1.1 christos
5 1.1 christos This program is free software; you can redistribute it and/or modify
6 1.1 christos it under the terms of the GNU General Public License as published by
7 1.1 christos the Free Software Foundation; either version 3 of the License, or
8 1.1 christos (at your option) any later version.
9 1.1 christos
10 1.1 christos This program is distributed in the hope that it will be useful,
11 1.1 christos but WITHOUT ANY WARRANTY; without even the implied warranty of
12 1.1 christos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 1.1 christos GNU General Public License for more details.
14 1.1 christos
15 1.1 christos You should have received a copy of the GNU General Public License
16 1.1 christos along with this program. If not, see <http://www.gnu.org/licenses/>. */
17 1.1 christos
18 1.1 christos /* This program tests tracepoint speed. It consists of two identical
19 1.1 christos loops, which in normal execution will run for exactly the same
20 1.1 christos amount of time. A tracepoint in the second loop will slow it down
21 1.1 christos by some amount, and then the program will report the slowdown
22 1.1 christos observed. */
23 1.1 christos
24 1.1 christos /* While primarily designed for the testsuite, it can also be used
25 1.1 christos for interactive testing. */
26 1.1 christos
27 1.1 christos #include <stdio.h>
28 1.1 christos #include <time.h>
29 1.1 christos #include <sys/time.h>
30 1.1 christos #include <sys/resource.h>
31 1.1 christos
32 1.1 christos int trace_speed_test (void);
33 1.1 christos
34 1.1 christos /* We mark these globals as volatile so the speed-measuring loops
35 1.1 christos don't get totally emptied out at high optimization levels. */
36 1.1 christos
37 1.1 christos volatile int globfoo, globfoo2, globfoo3;
38 1.1 christos
39 1.1 christos volatile short globarr[80000];
40 1.1 christos
41 1.1 christos int init_iters = 10 * 1000;
42 1.1 christos
43 1.1 christos int iters;
44 1.1 christos
45 1.1 christos int max_iters = 1000 * 1000 * 1000;
46 1.1 christos
47 1.1 christos int numtps = 1;
48 1.1 christos
49 1.1 christos unsigned long long now2, now3, now4, now5;
50 1.1 christos int total1, total2, idelta, mindelta, nsdelta;
51 1.1 christos int nspertp = 0;
52 1.1 christos
53 1.1 christos /* Return CPU usage (both user and system - trap-based tracepoints use
54 1.1 christos a bunch of system time). */
55 1.1 christos
56 1.1 christos unsigned long long
57 1.1 christos myclock ()
58 1.1 christos {
59 1.6 christos struct timeval tm;
60 1.6 christos gettimeofday (&tm, NULL);
61 1.6 christos return (((unsigned long long) tm.tv_sec) * 1000000) + tm.tv_usec;
62 1.1 christos }
63 1.1 christos
64 1.1 christos int
65 1.1 christos main(int argc, char **argv)
66 1.1 christos {
67 1.1 christos int problem;
68 1.1 christos
69 1.1 christos iters = init_iters;
70 1.1 christos
71 1.1 christos while (1)
72 1.1 christos {
73 1.1 christos numtps = 1; /* set pre-run breakpoint here */
74 1.1 christos
75 1.1 christos /* Keep trying the speed test, with more iterations, until
76 1.1 christos we get to a reasonable number. */
77 1.1 christos while (problem = trace_speed_test())
78 1.1 christos {
79 1.1 christos /* If iteration isn't working, give up. */
80 1.1 christos if (iters > max_iters)
81 1.1 christos {
82 1.1 christos printf ("Gone over %d iterations, giving up\n", max_iters);
83 1.1 christos break;
84 1.1 christos }
85 1.1 christos if (problem < 0)
86 1.1 christos {
87 1.1 christos printf ("Negative times, giving up\n", max_iters);
88 1.1 christos break;
89 1.1 christos }
90 1.1 christos
91 1.1 christos iters *= 2;
92 1.1 christos printf ("Doubled iterations to %d\n", iters);
93 1.1 christos }
94 1.1 christos
95 1.1 christos printf ("Tracepoint time is %d ns\n", nspertp);
96 1.1 christos
97 1.1 christos /* This is for the benefit of interactive testing and attaching,
98 1.1 christos keeps the program from pegging the machine. */
99 1.1 christos sleep (1); /* set post-run breakpoint here */
100 1.1 christos
101 1.1 christos /* Issue a little bit of output periodically, so we can see if
102 1.1 christos program is alive or hung. */
103 1.1 christos printf ("%s keeping busy, clock=%llu\n", argv[0], myclock ());
104 1.1 christos }
105 1.1 christos return 0;
106 1.1 christos }
107 1.1 christos
108 1.1 christos int
109 1.1 christos trace_speed_test (void)
110 1.1 christos {
111 1.1 christos int i;
112 1.1 christos
113 1.1 christos /* Overall loop run time deltas under 1 ms are likely noise and
114 1.1 christos should be ignored. */
115 1.1 christos mindelta = 1000;
116 1.1 christos
117 1.1 christos // The bodies of the two loops following must be identical.
118 1.1 christos
119 1.1 christos now2 = myclock ();
120 1.1 christos globfoo2 = 1;
121 1.1 christos for (i = 0; i < iters; ++i)
122 1.1 christos {
123 1.1 christos globfoo2 *= 45;
124 1.1 christos globfoo2 += globfoo + globfoo3;
125 1.1 christos globfoo2 *= globfoo + globfoo3;
126 1.1 christos globfoo2 -= globarr[4] + globfoo3;
127 1.1 christos globfoo2 *= globfoo + globfoo3;
128 1.1 christos globfoo2 += globfoo + globfoo3;
129 1.1 christos }
130 1.1 christos now3 = myclock ();
131 1.1 christos total1 = now3 - now2;
132 1.1 christos
133 1.1 christos now4 = myclock ();
134 1.1 christos globfoo2 = 1;
135 1.1 christos for (i = 0; i < iters; ++i)
136 1.1 christos {
137 1.1 christos globfoo2 *= 45;
138 1.1 christos globfoo2 += globfoo + globfoo3; /* set tracepoint here */
139 1.1 christos globfoo2 *= globfoo + globfoo3;
140 1.1 christos globfoo2 -= globarr[4] + globfoo3;
141 1.1 christos globfoo2 *= globfoo + globfoo3;
142 1.1 christos globfoo2 += globfoo + globfoo3;
143 1.1 christos }
144 1.1 christos now5 = myclock ();
145 1.1 christos total2 = now5 - now4;
146 1.1 christos
147 1.1 christos /* Report on the test results. */
148 1.1 christos
149 1.1 christos nspertp = 0;
150 1.1 christos
151 1.1 christos idelta = total2 - total1;
152 1.1 christos
153 1.1 christos printf ("Loops took %d usec and %d usec, delta is %d usec, %d iterations\n",
154 1.1 christos total1, total2, idelta, iters);
155 1.1 christos
156 1.1 christos /* If the second loop seems to run faster, things are weird so give up. */
157 1.1 christos if (idelta < 0)
158 1.1 christos return -1;
159 1.1 christos
160 1.1 christos if (idelta > mindelta
161 1.6 christos /* Total test time should be between 15 and 30 seconds. */
162 1.6 christos && (total1 + total2) > (15 * 1000000)
163 1.6 christos && (total1 + total2) < (30 * 1000000))
164 1.1 christos {
165 1.1 christos nsdelta = (((unsigned long long) idelta) * 1000) / iters;
166 1.1 christos printf ("Second loop took %d ns longer per iter than first\n", nsdelta);
167 1.1 christos nspertp = nsdelta / numtps;
168 1.1 christos printf ("%d ns per tracepoint\n", nspertp);
169 1.1 christos printf ("Base iteration time %d ns\n",
170 1.1 christos ((int) (((unsigned long long) total1) * 1000) / iters));
171 1.1 christos printf ("Total test time %d secs\n", ((int) ((now5 - now2) / 1000000)));
172 1.1 christos
173 1.1 christos /* Speed test ran with no problem. */
174 1.1 christos return 0;
175 1.1 christos }
176 1.1 christos
177 1.1 christos /* The test run was too brief, or otherwise not useful. */
178 1.1 christos return 1;
179 1.1 christos }
180