]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/util/testsuite_performance.h
testsuite_visualization.h: Move contents into...
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / util / testsuite_performance.h
1 // -*- C++ -*-
2 // Testing performance utilities for the C++ library testsuite.
3 //
4 // Copyright (C) 2003, 2004, 2005, 2007, 2008 Free Software Foundation, Inc.
5 //
6 // This file is part of the GNU ISO C++ Library. This library is free
7 // software; you can redistribute it and/or modify it under the
8 // terms of the GNU General Public License as published by the
9 // Free Software Foundation; either version 2, or (at your option)
10 // any later version.
11 //
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License along
18 // with this library; see the file COPYING. If not, write to the Free
19 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
20 // USA.
21 //
22 // As a special exception, you may use this file as part of a free software
23 // library without restriction. Specifically, if other files instantiate
24 // templates or use macros or inline functions from this file, or you compile
25 // this file and link it with other files to produce an executable, this
26 // file does not by itself cause the resulting executable to be covered by
27 // the GNU General Public License. This exception does not however
28 // invalidate any other reasons why the executable file might be covered by
29 // the GNU General Public License.
30
31 #ifndef _GLIBCXX_PERFORMANCE_H
32 #define _GLIBCXX_PERFORMANCE_H
33
34 #include <sys/times.h>
35 #include <sys/resource.h>
36 #include <cstdlib>
37 #include <cstring>
38 #include <string>
39 #include <fstream>
40 #include <iomanip>
41 #include <typeinfo>
42 #include <stdexcept>
43 #include <sstream>
44 #include <cxxabi.h>
45 #include <testsuite_common_types.h>
46
47 #ifdef __linux__
48 #include <malloc.h>
49 #elif defined (__FreeBSD__)
50 extern "C"
51 {
52 struct mallinfo
53 {
54 int uordblks;
55 int hblkhd;
56 };
57
58 struct mallinfo
59 mallinfo(void)
60 {
61 struct mallinfo m = { (((size_t) sbrk (0) + 1023) / 1024), 0 };
62 return m;
63 }
64 }
65 #elif !defined (__hpux__)
66 extern "C"
67 {
68 struct mallinfo
69 {
70 int uordblks;
71 int hblkhd;
72 };
73
74 struct mallinfo empty = { 0, 0 };
75
76 struct mallinfo
77 mallinfo(void)
78 { return empty; }
79 }
80 #endif
81
82 namespace __gnu_test
83 {
84 class time_counter
85 {
86 private:
87 clock_t elapsed_begin;
88 clock_t elapsed_end;
89 tms tms_begin;
90 tms tms_end;
91
92 public:
93 explicit
94 time_counter() : elapsed_begin(), elapsed_end(), tms_begin(), tms_end()
95 { }
96
97 void
98 clear() throw()
99 {
100 elapsed_begin = clock_t();
101 elapsed_end = clock_t();
102 tms_begin = tms();
103 tms_end = tms();
104 }
105
106 void
107 start()
108 {
109 this->clear();
110 elapsed_begin = times(&tms_begin);
111 const clock_t err = clock_t(-1);
112 if (elapsed_begin == err)
113 std::__throw_runtime_error("time_counter::start");
114 }
115
116 void
117 stop()
118 {
119 elapsed_end = times(&tms_end);
120 const clock_t err = clock_t(-1);
121 if (elapsed_end == err)
122 std::__throw_runtime_error("time_counter::stop");
123 }
124
125 size_t
126 real_time() const
127 { return elapsed_end - elapsed_begin; }
128
129 size_t
130 user_time() const
131 { return tms_end.tms_utime - tms_begin.tms_utime; }
132
133 size_t
134 system_time() const
135 { return tms_end.tms_stime - tms_begin.tms_stime; }
136 };
137
138 class resource_counter
139 {
140 int who;
141 rusage rusage_begin;
142 rusage rusage_end;
143 struct mallinfo allocation_begin;
144 struct mallinfo allocation_end;
145
146 public:
147 resource_counter(int i = RUSAGE_SELF) : who(i)
148 { this->clear(); }
149
150 void
151 clear() throw()
152 {
153 memset(&rusage_begin, 0, sizeof(rusage_begin));
154 memset(&rusage_end, 0, sizeof(rusage_end));
155 memset(&allocation_begin, 0, sizeof(allocation_begin));
156 memset(&allocation_end, 0, sizeof(allocation_end));
157 }
158
159 void
160 start()
161 {
162 if (getrusage(who, &rusage_begin) != 0 )
163 memset(&rusage_begin, 0, sizeof(rusage_begin));
164 malloc(0); // Needed for some implementations.
165 allocation_begin = mallinfo();
166 }
167
168 void
169 stop()
170 {
171 if (getrusage(who, &rusage_end) != 0 )
172 memset(&rusage_end, 0, sizeof(rusage_end));
173 allocation_end = mallinfo();
174 }
175
176 int
177 allocated_memory() const
178 { return ((allocation_end.uordblks - allocation_begin.uordblks)
179 + (allocation_end.hblkhd - allocation_begin.hblkhd)); }
180
181 long
182 hard_page_fault() const
183 { return rusage_end.ru_majflt - rusage_begin.ru_majflt; }
184
185 long
186 swapped() const
187 { return rusage_end.ru_nswap - rusage_begin.ru_nswap; }
188 };
189
190 inline void
191 start_counters(time_counter& t, resource_counter& r)
192 {
193 t.start();
194 r.start();
195 }
196
197 inline void
198 stop_counters(time_counter& t, resource_counter& r)
199 {
200 t.stop();
201 r.stop();
202 }
203
204 inline void
205 clear_counters(time_counter& t, resource_counter& r)
206 {
207 t.clear();
208 r.clear();
209 }
210
211 void
212 report_performance(const std::string file, const std::string comment,
213 const time_counter& t, const resource_counter& r)
214 {
215 const char space = ' ';
216 const char tab = '\t';
217 const char* name = "libstdc++-performance.sum";
218 std::string::const_iterator i = file.begin() + file.find_last_of('/') + 1;
219 std::string testname(i, file.end());
220
221 std::ofstream out(name, std::ios_base::app);
222
223 #ifdef __GTHREADS
224 if (__gthread_active_p())
225 testname.append("-thread");
226 #endif
227
228 out.setf(std::ios_base::left);
229 out << std::setw(25) << testname << tab;
230 out << std::setw(25) << comment << tab;
231
232 out.setf(std::ios_base::right);
233 out << std::setw(4) << t.real_time() << "r" << space;
234 out << std::setw(4) << t.user_time() << "u" << space;
235 out << std::setw(4) << t.system_time() << "s" << space;
236 out << std::setw(8) << r.allocated_memory() << "mem" << space;
237 out << std::setw(4) << r.hard_page_fault() << "pf" << space;
238
239 out << std::endl;
240 out.close();
241 }
242
243 void
244 report_header(const std::string file, const std::string header)
245 {
246 const char space = ' ';
247 const char tab = '\t';
248 const char* name = "libstdc++-performance.sum";
249 std::string::const_iterator i = file.begin() + file.find_last_of('/') + 1;
250 std::string testname(i, file.end());
251
252 std::ofstream out(name, std::ios_base::app);
253
254 #ifdef __GTHREADS
255 if (__gthread_active_p ())
256 testname.append("-thread");
257 #endif
258
259 out.setf(std::ios_base::left);
260 out << std::setw(25) << testname << tab;
261 out << std::setw(40) << header << tab;
262
263 out << std::endl;
264 out.close();
265 }
266 } // namespace __gnu_test
267
268
269 // Ah, we wish it wasn't so...
270 bool first_container = false;
271 extern const char* filename;
272
273 typedef std::string::size_type (*callback_type) (std::string&);
274
275 template<typename Container, int Iter, bool Thread>
276 void
277 write_viz_container(callback_type find_container, const char* filename)
278 {
279 typedef std::string string;
280
281 // Create title.
282 {
283 const char ws(' ');
284 std::ostringstream title;
285
286 std::string titlename(filename);
287 std::string::size_type n = titlename.find('.');
288 if (n != string::npos)
289 titlename = std::string(titlename.begin(), titlename.begin() + n);
290
291 title << titlename;
292 title << ws;
293 title << Iter;
294 title << ws;
295 #if 0
296 title << "thread<";
297 std::boolalpha(title);
298 title << Thread;
299 title << '>';
300 #endif
301
302 titlename += ".title";
303 std::ofstream titlefile(titlename.c_str());
304 if (!titlefile.good())
305 throw std::runtime_error("write_viz_data cannot open titlename");
306 titlefile << title.str() << std::endl;
307 }
308
309 // Create compressed type name.
310 Container obj;
311 int status;
312 std::string type(abi::__cxa_demangle(typeid(obj).name(), 0, 0, &status));
313
314 // Extract fully-qualified typename.
315 // Assumes "set" or "map" are uniquely determinate.
316 string::iterator beg = type.begin();
317 string::iterator end;
318 string::size_type n = (*find_container)(type);
319
320 // Find start of fully-qualified name.
321 // Assume map, find end.
322 string::size_type nend = type.find('<', n);
323 if (nend != string::npos)
324 end = type.begin() + nend;
325
326 string compressed_type;
327 compressed_type += '"';
328 compressed_type += string(beg, end);
329 compressed_type += '<';
330 #if 0
331 typename Container::key_type v;
332 compressed_type += typeid(v).name();
333 #else
334 compressed_type += "int";
335 #endif
336 compressed_type += ", A>";
337
338 // XXX
339 if (Thread == true)
340 compressed_type += " thread";
341 compressed_type += '"';
342
343 std::ofstream file(filename, std::ios_base::app);
344 if (!file.good())
345 throw std::runtime_error("write_viz_data cannot open filename");
346
347 file << compressed_type;
348 first_container = false;
349 }
350
351
352 void
353 write_viz_data(__gnu_test::time_counter& time, const char* filename)
354 {
355 std::ofstream file(filename, std::ios_base::app);
356 if (!file.good())
357 throw std::runtime_error("write_viz_data cannot open filename");
358
359 // Print out score in appropriate column.
360 const char tab('\t');
361 int score = time.real_time();
362 file << tab << score;
363 }
364
365 void
366 write_viz_endl(const char* filename)
367 {
368 std::ofstream file(filename, std::ios_base::app);
369 if (!file.good())
370 throw std::runtime_error("write_viz_endl cannot open filename");
371 file << std::endl;
372 }
373
374
375 // Function template, function objects for the tests.
376 template<typename TestType>
377 struct value_type : public std::pair<const TestType, TestType>
378 {
379 inline value_type& operator++()
380 {
381 ++this->second;
382 return *this;
383 }
384
385 inline operator TestType() const { return this->second; }
386 };
387
388 template<typename Container, int Iter>
389 void
390 do_loop();
391
392 template<typename Container, int Iter>
393 void*
394 do_thread(void* p = NULL)
395 {
396 do_loop<Container, Iter>();
397 return p;
398 }
399
400 template<typename Container, int Iter, bool Thread>
401 void
402 test_container(const char* filename)
403 {
404 using namespace __gnu_test;
405 time_counter time;
406 resource_counter resource;
407 {
408 start_counters(time, resource);
409 if (!Thread)
410 {
411 // No threads, so run 4x.
412 do_loop<Container, Iter * 4>();
413 }
414 else
415 {
416 #if defined (_GLIBCXX_GCC_GTHR_POSIX_H) && !defined (NOTHREAD)
417 pthread_t t1, t2, t3, t4;
418 pthread_create(&t1, 0, &do_thread<Container, Iter>, 0);
419 pthread_create(&t2, 0, &do_thread<Container, Iter>, 0);
420 pthread_create(&t3, 0, &do_thread<Container, Iter>, 0);
421 pthread_create(&t4, 0, &do_thread<Container, Iter>, 0);
422
423 pthread_join(t1, NULL);
424 pthread_join(t2, NULL);
425 pthread_join(t3, NULL);
426 pthread_join(t4, NULL);
427 #endif
428 }
429 stop_counters(time, resource);
430
431 // Detailed text data.
432 Container obj;
433 int status;
434 std::ostringstream comment;
435 comment << "type: " << abi::__cxa_demangle(typeid(obj).name(),
436 0, 0, &status);
437 report_header(filename, comment.str());
438 report_performance("", "", time, resource);
439
440 // Detailed data for visualization.
441 std::string vizfilename(filename);
442 vizfilename += ".dat";
443 write_viz_data(time, vizfilename.c_str());
444 }
445 }
446
447 template<bool Thread>
448 struct test_sequence
449 {
450 test_sequence(const char* filename) : _M_filename(filename) { }
451
452 template<class Container>
453 void
454 operator()(Container)
455 {
456 const int i = 20000;
457 test_container<Container, i, Thread>(_M_filename);
458 }
459
460 private:
461 const char* _M_filename;
462 };
463
464
465 inline std::string::size_type
466 sequence_find_container(std::string& type)
467 {
468 const std::string::size_type npos = std::string::npos;
469 std::string::size_type n1 = type.find("vector");
470 std::string::size_type n2 = type.find("list");
471 std::string::size_type n3 = type.find("deque");
472 std::string::size_type n4 = type.find("string");
473
474 if (n1 != npos || n2 != npos || n3 != npos || n4 != npos)
475 return std::min(std::min(n1, n2), std::min(n3, n4));
476 else
477 throw std::runtime_error("sequence_find_container not found");
478 }
479
480 inline std::string::size_type
481 associative_find_container(std::string& type)
482 {
483 using std::string;
484 string::size_type n1 = type.find("map");
485 string::size_type n2 = type.find("set");
486 if (n1 != string::npos || n2 != string::npos)
487 return std::min(n1, n2);
488 else
489 throw std::runtime_error("associative_find_container not found");
490 }
491
492 #endif // _GLIBCXX_PERFORMANCE_H
493