2008-05-30 Vladimir Makarov <vmakarov@redhat.com>
[official-gcc.git] / libstdc++-v3 / testsuite / util / testsuite_performance.h
blob246651812d854a6b64c32c5158b9f43c50ac5316
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.
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 COPYING. If not, write to the Free
19 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
20 // USA.
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.
31 #ifndef _GLIBCXX_PERFORMANCE_H
32 #define _GLIBCXX_PERFORMANCE_H
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>
47 #ifdef __linux__
48 #include <malloc.h>
49 #elif defined (__FreeBSD__)
50 extern "C"
52 struct mallinfo
54 int uordblks;
55 int hblkhd;
58 struct mallinfo
59 mallinfo(void)
61 struct mallinfo m = { (((size_t) sbrk (0) + 1023) / 1024), 0 };
62 return m;
65 #elif !defined (__hpux__)
66 extern "C"
68 struct mallinfo
70 int uordblks;
71 int hblkhd;
74 struct mallinfo empty = { 0, 0 };
76 struct mallinfo
77 mallinfo(void)
78 { return empty; }
80 #endif
82 namespace __gnu_test
84 class time_counter
86 private:
87 clock_t elapsed_begin;
88 clock_t elapsed_end;
89 tms tms_begin;
90 tms tms_end;
92 public:
93 explicit
94 time_counter() : elapsed_begin(), elapsed_end(), tms_begin(), tms_end()
95 { }
97 void
98 clear() throw()
100 elapsed_begin = clock_t();
101 elapsed_end = clock_t();
102 tms_begin = tms();
103 tms_end = tms();
106 void
107 start()
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");
116 void
117 stop()
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");
125 size_t
126 real_time() const
127 { return elapsed_end - elapsed_begin; }
129 size_t
130 user_time() const
131 { return tms_end.tms_utime - tms_begin.tms_utime; }
133 size_t
134 system_time() const
135 { return tms_end.tms_stime - tms_begin.tms_stime; }
138 class resource_counter
140 int who;
141 rusage rusage_begin;
142 rusage rusage_end;
143 struct mallinfo allocation_begin;
144 struct mallinfo allocation_end;
146 public:
147 resource_counter(int i = RUSAGE_SELF) : who(i)
148 { this->clear(); }
150 void
151 clear() throw()
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));
159 void
160 start()
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();
168 void
169 stop()
171 if (getrusage(who, &rusage_end) != 0 )
172 memset(&rusage_end, 0, sizeof(rusage_end));
173 allocation_end = mallinfo();
177 allocated_memory() const
178 { return ((allocation_end.uordblks - allocation_begin.uordblks)
179 + (allocation_end.hblkhd - allocation_begin.hblkhd)); }
181 long
182 hard_page_fault() const
183 { return rusage_end.ru_majflt - rusage_begin.ru_majflt; }
185 long
186 swapped() const
187 { return rusage_end.ru_nswap - rusage_begin.ru_nswap; }
190 inline void
191 start_counters(time_counter& t, resource_counter& r)
193 t.start();
194 r.start();
197 inline void
198 stop_counters(time_counter& t, resource_counter& r)
200 t.stop();
201 r.stop();
204 inline void
205 clear_counters(time_counter& t, resource_counter& r)
207 t.clear();
208 r.clear();
211 void
212 report_performance(const std::string file, const std::string comment,
213 const time_counter& t, const resource_counter& r)
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());
221 std::ofstream out(name, std::ios_base::app);
223 #ifdef __GTHREADS
224 if (__gthread_active_p())
225 testname.append("-thread");
226 #endif
228 out.setf(std::ios_base::left);
229 out << std::setw(25) << testname << tab;
230 out << std::setw(25) << comment << tab;
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;
239 out << std::endl;
240 out.close();
243 void
244 report_header(const std::string file, const std::string header)
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());
252 std::ofstream out(name, std::ios_base::app);
254 #ifdef __GTHREADS
255 if (__gthread_active_p ())
256 testname.append("-thread");
257 #endif
259 out.setf(std::ios_base::left);
260 out << std::setw(25) << testname << tab;
261 out << std::setw(40) << header << tab;
263 out << std::endl;
264 out.close();
266 } // namespace __gnu_test
269 // Ah, we wish it wasn't so...
270 bool first_container = false;
271 extern const char* filename;
273 typedef std::string::size_type (*callback_type) (std::string&);
275 template<typename Container, int Iter, bool Thread>
276 void
277 write_viz_container(callback_type find_container, const char* filename)
279 typedef std::string string;
281 // Create title.
283 const char ws(' ');
284 std::ostringstream title;
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);
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
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;
309 // Create compressed type name.
310 Container obj;
311 int status;
312 std::string type(abi::__cxa_demangle(typeid(obj).name(), 0, 0, &status));
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);
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;
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>";
338 // XXX
339 if (Thread == true)
340 compressed_type += " thread";
341 compressed_type += '"';
343 std::ofstream file(filename, std::ios_base::app);
344 if (!file.good())
345 throw std::runtime_error("write_viz_data cannot open filename");
347 file << compressed_type;
348 first_container = false;
352 void
353 write_viz_data(__gnu_test::time_counter& time, const char* filename)
355 std::ofstream file(filename, std::ios_base::app);
356 if (!file.good())
357 throw std::runtime_error("write_viz_data cannot open filename");
359 // Print out score in appropriate column.
360 const char tab('\t');
361 int score = time.real_time();
362 file << tab << score;
365 void
366 write_viz_endl(const char* filename)
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;
375 // Function template, function objects for the tests.
376 template<typename TestType>
377 struct value_type : public std::pair<const TestType, TestType>
379 inline value_type& operator++()
381 ++this->second;
382 return *this;
385 inline operator TestType() const { return this->second; }
388 template<typename Container, int Iter>
389 void
390 do_loop();
392 template<typename Container, int Iter>
393 void*
394 do_thread(void* p = NULL)
396 do_loop<Container, Iter>();
397 return p;
400 template<typename Container, int Iter, bool Thread>
401 void
402 test_container(const char* filename)
404 using namespace __gnu_test;
405 time_counter time;
406 resource_counter resource;
408 start_counters(time, resource);
409 if (!Thread)
411 // No threads, so run 4x.
412 do_loop<Container, Iter * 4>();
414 else
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);
423 pthread_join(t1, NULL);
424 pthread_join(t2, NULL);
425 pthread_join(t3, NULL);
426 pthread_join(t4, NULL);
427 #endif
429 stop_counters(time, resource);
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);
440 // Detailed data for visualization.
441 std::string vizfilename(filename);
442 vizfilename += ".dat";
443 write_viz_data(time, vizfilename.c_str());
447 template<bool Thread>
448 struct test_sequence
450 test_sequence(const char* filename) : _M_filename(filename) { }
452 template<class Container>
453 void
454 operator()(Container)
456 const int i = 20000;
457 test_container<Container, i, Thread>(_M_filename);
460 private:
461 const char* _M_filename;
465 inline std::string::size_type
466 sequence_find_container(std::string& type)
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");
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");
480 inline std::string::size_type
481 associative_find_container(std::string& type)
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");
492 #endif // _GLIBCXX_PERFORMANCE_H