2 // Testing performance utilities for the C++ library testsuite.
4 // Copyright (C) 2003, 2004, 2005, 2007, 2008 Free Software Foundation, Inc.
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)
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,
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>
45 #include <testsuite_common_types.h>
49 #elif defined (__FreeBSD__)
61 struct mallinfo m
= { (((size_t) sbrk (0) + 1023) / 1024), 0 };
65 #elif !defined (__hpux__)
74 struct mallinfo empty
= { 0, 0 };
87 clock_t elapsed_begin
;
94 time_counter() : elapsed_begin(), elapsed_end(), tms_begin(), tms_end()
100 elapsed_begin
= clock_t();
101 elapsed_end
= clock_t();
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");
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");
127 { return elapsed_end
- elapsed_begin
; }
131 { return tms_end
.tms_utime
- tms_begin
.tms_utime
; }
135 { return tms_end
.tms_stime
- tms_begin
.tms_stime
; }
138 class resource_counter
143 struct mallinfo allocation_begin
;
144 struct mallinfo allocation_end
;
147 resource_counter(int i
= RUSAGE_SELF
) : who(i
)
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
));
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();
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
)); }
182 hard_page_fault() const
183 { return rusage_end
.ru_majflt
- rusage_begin
.ru_majflt
; }
187 { return rusage_end
.ru_nswap
- rusage_begin
.ru_nswap
; }
191 start_counters(time_counter
& t
, resource_counter
& r
)
198 stop_counters(time_counter
& t
, resource_counter
& r
)
205 clear_counters(time_counter
& t
, resource_counter
& r
)
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
);
224 if (__gthread_active_p())
225 testname
.append("-thread");
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
;
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
);
255 if (__gthread_active_p ())
256 testname
.append("-thread");
259 out
.setf(std::ios_base::left
);
260 out
<< std::setw(25) << testname
<< tab
;
261 out
<< std::setw(40) << header
<< tab
;
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
>
277 write_viz_container(callback_type find_container
, const char* filename
)
279 typedef std::string string
;
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
);
297 std::boolalpha(title
);
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.
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
+= '<';
331 typename
Container::key_type v
;
332 compressed_type
+= typeid(v
).name();
334 compressed_type
+= "int";
336 compressed_type
+= ", A>";
340 compressed_type
+= " thread";
341 compressed_type
+= '"';
343 std::ofstream
file(filename
, std::ios_base::app
);
345 throw std::runtime_error("write_viz_data cannot open filename");
347 file
<< compressed_type
;
348 first_container
= false;
353 write_viz_data(__gnu_test::time_counter
& time
, const char* filename
)
355 std::ofstream
file(filename
, std::ios_base::app
);
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
;
366 write_viz_endl(const char* filename
)
368 std::ofstream
file(filename
, std::ios_base::app
);
370 throw std::runtime_error("write_viz_endl cannot open filename");
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++()
385 inline operator TestType() const { return this->second
; }
388 template<typename Container
, int Iter
>
392 template<typename Container
, int Iter
>
394 do_thread(void* p
= NULL
)
396 do_loop
<Container
, Iter
>();
400 template<typename Container
, int Iter
, bool Thread
>
402 test_container(const char* filename
)
404 using namespace __gnu_test
;
406 resource_counter resource
;
408 start_counters(time
, resource
);
411 // No threads, so run 4x.
412 do_loop
<Container
, Iter
* 4>();
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
);
429 stop_counters(time
, resource
);
431 // Detailed text data.
434 std::ostringstream comment
;
435 comment
<< "type: " << abi::__cxa_demangle(typeid(obj
).name(),
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
>
450 test_sequence(const char* filename
) : _M_filename(filename
) { }
452 template<class Container
>
454 operator()(Container
)
457 test_container
<Container
, i
, Thread
>(_M_filename
);
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
));
477 throw std::runtime_error("sequence_find_container not found");
480 inline std::string::size_type
481 associative_find_container(std::string
& type
)
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
);
489 throw std::runtime_error("associative_find_container not found");
492 #endif // _GLIBCXX_PERFORMANCE_H