re PR c++/84661 (internal compiler error: Segmentation fault (strip_array_types()))
[official-gcc.git] / libstdc++-v3 / testsuite / util / testsuite_performance.h
blobfd163914d99adaf6de4ea65cf4c9b03b959d3004
1 // -*- C++ -*-
2 // Testing performance utilities for the C++ library testsuite.
3 //
4 // Copyright (C) 2003-2019 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 3, or (at your option)
10 // any later version.
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.
17 // You should have received a copy of the GNU General Public License along
18 // with this library; see the file COPYING3. If not see
19 // <http://www.gnu.org/licenses/>.
22 #ifndef _GLIBCXX_PERFORMANCE_H
23 #define _GLIBCXX_PERFORMANCE_H
25 #include <sys/times.h>
26 #include <sys/resource.h>
27 #include <cstdlib>
28 #include <cstring>
29 #include <string>
30 #include <fstream>
31 #include <iomanip>
32 #include <typeinfo>
33 #include <stdexcept>
34 #include <sstream>
35 #include <cxxabi.h>
36 #include <testsuite_common_types.h>
38 #if defined (__linux__) || defined (__GLIBC__)
39 #include <malloc.h>
40 #elif defined (__FreeBSD__)
41 extern "C"
43 struct mallinfo
45 int uordblks;
46 int hblkhd;
49 struct mallinfo
50 mallinfo(void)
52 struct mallinfo m = { (((std::size_t) sbrk (0) + 1023) / 1024), 0 };
53 return m;
56 #elif !defined (__hpux__)
57 extern "C"
59 struct mallinfo
61 int uordblks;
62 int hblkhd;
65 struct mallinfo empty = { 0, 0 };
67 struct mallinfo
68 mallinfo(void)
69 { return empty; }
71 #endif
73 namespace __gnu_test
75 class time_counter
77 private:
78 clock_t elapsed_begin;
79 clock_t elapsed_end;
80 tms tms_begin;
81 tms tms_end;
82 std::size_t splits[3];
84 public:
85 explicit
86 time_counter()
87 : elapsed_begin(), elapsed_end(), tms_begin(), tms_end(), splits()
88 { }
90 void
91 clear() throw()
93 elapsed_begin = clock_t();
94 elapsed_end = clock_t();
95 tms_begin = tms();
96 tms_end = tms();
97 splits[0] = splits[1] = splits[2] = 0;
100 void
101 start()
103 this->clear();
104 elapsed_begin = times(&tms_begin);
105 const clock_t err = clock_t(-1);
106 if (elapsed_begin == err)
107 std::__throw_runtime_error("time_counter::start");
110 void
111 stop()
113 elapsed_end = times(&tms_end);
114 const clock_t err = clock_t(-1);
115 if (elapsed_end == err)
116 std::__throw_runtime_error("time_counter::stop");
119 void
120 restart()
122 splits[0] += (elapsed_end - elapsed_begin);
123 splits[1] += (tms_end.tms_utime - tms_begin.tms_utime);
124 splits[2] += (tms_end.tms_stime - tms_begin.tms_stime);
125 elapsed_begin = times(&tms_begin);
126 const clock_t err = clock_t(-1);
127 if (elapsed_begin == err)
128 std::__throw_runtime_error("time_counter::restart");
131 std::size_t
132 real_time() const
133 { return (elapsed_end - elapsed_begin) + splits[0]; }
135 std::size_t
136 user_time() const
137 { return (tms_end.tms_utime - tms_begin.tms_utime) + splits[1]; }
139 std::size_t
140 system_time() const
141 { return (tms_end.tms_stime - tms_begin.tms_stime) + splits[1]; }
144 class resource_counter
146 int who;
147 rusage rusage_begin;
148 rusage rusage_end;
149 struct mallinfo allocation_begin;
150 struct mallinfo allocation_end;
152 public:
153 resource_counter(int i = RUSAGE_SELF) : who(i)
154 { this->clear(); }
156 void
157 clear() throw()
159 memset(&rusage_begin, 0, sizeof(rusage_begin));
160 memset(&rusage_end, 0, sizeof(rusage_end));
161 memset(&allocation_begin, 0, sizeof(allocation_begin));
162 memset(&allocation_end, 0, sizeof(allocation_end));
165 void
166 start()
168 if (getrusage(who, &rusage_begin) != 0 )
169 memset(&rusage_begin, 0, sizeof(rusage_begin));
170 malloc(0); // Needed for some implementations.
171 allocation_begin = mallinfo();
174 void
175 stop()
177 if (getrusage(who, &rusage_end) != 0 )
178 memset(&rusage_end, 0, sizeof(rusage_end));
179 allocation_end = mallinfo();
183 allocated_memory() const
184 { return ((allocation_end.uordblks - allocation_begin.uordblks)
185 + (allocation_end.hblkhd - allocation_begin.hblkhd)); }
187 long
188 hard_page_fault() const
189 { return rusage_end.ru_majflt - rusage_begin.ru_majflt; }
191 long
192 swapped() const
193 { return rusage_end.ru_nswap - rusage_begin.ru_nswap; }
196 inline void
197 start_counters(time_counter& t, resource_counter& r)
199 t.start();
200 r.start();
203 inline void
204 stop_counters(time_counter& t, resource_counter& r)
206 t.stop();
207 r.stop();
210 inline void
211 clear_counters(time_counter& t, resource_counter& r)
213 t.clear();
214 r.clear();
217 void
218 report_performance(const std::string file, const std::string comment,
219 const time_counter& t, const resource_counter& r)
221 const char space = ' ';
222 const char tab = '\t';
223 const char* name = "libstdc++-performance.sum";
224 std::string::const_iterator i = file.begin() + file.find_last_of('/') + 1;
225 std::string testname(i, file.end());
227 std::ofstream out(name, std::ios_base::app);
229 #ifdef __GTHREADS
230 if (__gthread_active_p())
231 testname.append("-thread");
232 #endif
234 out.setf(std::ios_base::left);
235 out << std::setw(25) << testname << tab;
236 out << std::setw(25) << comment << tab;
238 out.setf(std::ios_base::right);
239 out << std::setw(4) << t.real_time() << "r" << space;
240 out << std::setw(4) << t.user_time() << "u" << space;
241 out << std::setw(4) << t.system_time() << "s" << space;
242 out << std::setw(8) << r.allocated_memory() << "mem" << space;
243 out << std::setw(4) << r.hard_page_fault() << "pf" << space;
245 out << std::endl;
246 out.close();
249 void
250 report_header(const std::string file, const std::string header)
252 const char space = ' ';
253 const char tab = '\t';
254 const char* name = "libstdc++-performance.sum";
255 std::string::const_iterator i = file.begin() + file.find_last_of('/') + 1;
256 std::string testname(i, file.end());
258 std::ofstream out(name, std::ios_base::app);
260 #ifdef __GTHREADS
261 if (__gthread_active_p ())
262 testname.append("-thread");
263 #endif
265 out.setf(std::ios_base::left);
266 out << std::setw(25) << testname << tab;
267 out << std::setw(40) << header << tab;
269 out << std::endl;
270 out.close();
272 } // namespace __gnu_test
275 // Ah, we wish it wasn't so...
276 bool first_container = false;
277 extern const char* filename;
279 typedef std::string::size_type (*callback_type) (std::string&);
281 template<typename Container, int Iter, bool Thread>
282 void
283 write_viz_container(callback_type find_container, const char* filename)
285 typedef std::string string;
287 // Create title.
289 const char ws(' ');
290 std::ostringstream title;
292 std::string titlename(filename);
293 std::string::size_type n = titlename.find('.');
294 if (n != string::npos)
295 titlename = std::string(titlename.begin(), titlename.begin() + n);
297 title << titlename;
298 title << ws;
299 title << Iter;
300 title << ws;
301 #if 0
302 title << "thread<";
303 std::boolalpha(title);
304 title << Thread;
305 title << '>';
306 #endif
308 titlename += ".title";
309 std::ofstream titlefile(titlename.c_str());
310 if (!titlefile.good())
311 throw std::runtime_error("write_viz_data cannot open titlename");
312 titlefile << title.str() << std::endl;
315 // Create compressed type name.
316 Container obj;
317 int status;
318 std::string type(abi::__cxa_demangle(typeid(obj).name(), 0, 0, &status));
320 // Extract fully-qualified typename.
321 // Assumes "set" or "map" are uniquely determinate.
322 string::iterator beg = type.begin();
323 string::iterator end;
324 string::size_type n = (*find_container)(type);
326 // Find start of fully-qualified name.
327 // Assume map, find end.
328 string::size_type nend = type.find('<', n);
329 if (nend != string::npos)
330 end = type.begin() + nend;
332 string compressed_type;
333 compressed_type += '"';
334 compressed_type += string(beg, end);
335 compressed_type += '<';
336 #if 0
337 typename Container::key_type v;
338 compressed_type += typeid(v).name();
339 #else
340 compressed_type += "int";
341 #endif
342 compressed_type += ", A>";
344 // XXX
345 if (Thread == true)
346 compressed_type += " thread";
347 compressed_type += '"';
349 std::ofstream file(filename, std::ios_base::app);
350 if (!file.good())
351 throw std::runtime_error("write_viz_data cannot open filename");
353 file << compressed_type;
354 first_container = false;
358 void
359 write_viz_data(__gnu_test::time_counter& time, const char* filename)
361 std::ofstream file(filename, std::ios_base::app);
362 if (!file.good())
363 throw std::runtime_error("write_viz_data cannot open filename");
365 // Print out score in appropriate column.
366 const char tab('\t');
367 int score = time.real_time();
368 file << tab << score;
371 void
372 write_viz_endl(const char* filename)
374 std::ofstream file(filename, std::ios_base::app);
375 if (!file.good())
376 throw std::runtime_error("write_viz_endl cannot open filename");
377 file << std::endl;
381 // Function template, function objects for the tests.
382 template<typename TestType>
383 struct value_type : public std::pair<const TestType, TestType>
385 inline value_type& operator++()
387 ++this->second;
388 return *this;
391 inline operator TestType() const { return this->second; }
394 template<typename Container, int Iter>
395 void
396 do_loop();
398 template<typename Container, int Iter>
399 void*
400 do_thread(void* p = 0)
402 do_loop<Container, Iter>();
403 return p;
406 template<typename Container, int Iter, bool Thread>
407 void
408 test_container(const char* filename)
410 using namespace __gnu_test;
411 time_counter time;
412 resource_counter resource;
414 start_counters(time, resource);
415 if (!Thread)
417 // No threads, so run 4x.
418 do_loop<Container, Iter * 4>();
420 else
422 #if defined (_GLIBCXX_GCC_GTHR_POSIX_H) && !defined (NOTHREAD)
423 pthread_t t1, t2, t3, t4;
424 pthread_create(&t1, 0, &do_thread<Container, Iter>, 0);
425 pthread_create(&t2, 0, &do_thread<Container, Iter>, 0);
426 pthread_create(&t3, 0, &do_thread<Container, Iter>, 0);
427 pthread_create(&t4, 0, &do_thread<Container, Iter>, 0);
429 pthread_join(t1, 0);
430 pthread_join(t2, 0);
431 pthread_join(t3, 0);
432 pthread_join(t4, 0);
433 #endif
435 stop_counters(time, resource);
437 // Detailed text data.
438 Container obj;
439 int status;
440 std::ostringstream comment;
441 comment << "type: " << abi::__cxa_demangle(typeid(obj).name(),
442 0, 0, &status);
443 report_header(filename, comment.str());
444 report_performance("", "", time, resource);
446 // Detailed data for visualization.
447 std::string vizfilename(filename);
448 vizfilename += ".dat";
449 write_viz_data(time, vizfilename.c_str());
453 template<bool Thread>
454 struct test_sequence
456 test_sequence(const char* filename) : _M_filename(filename) { }
458 template<class Container>
459 void
460 operator()(Container)
462 const int i = 20000;
463 test_container<Container, i, Thread>(_M_filename);
466 private:
467 const char* _M_filename;
471 inline std::string::size_type
472 sequence_find_container(std::string& type)
474 const std::string::size_type npos = std::string::npos;
475 std::string::size_type n1 = type.find("vector");
476 std::string::size_type n2 = type.find("list");
477 std::string::size_type n3 = type.find("deque");
478 std::string::size_type n4 = type.find("string");
480 if (n1 != npos || n2 != npos || n3 != npos || n4 != npos)
481 return std::min(std::min(n1, n2), std::min(n3, n4));
482 else
483 throw std::runtime_error("sequence_find_container not found");
486 inline std::string::size_type
487 associative_find_container(std::string& type)
489 using std::string;
490 string::size_type n1 = type.find("map");
491 string::size_type n2 = type.find("set");
492 if (n1 != string::npos || n2 != string::npos)
493 return std::min(n1, n2);
494 else
495 throw std::runtime_error("associative_find_container not found");
498 #endif // _GLIBCXX_PERFORMANCE_H