2 // Testing performance utilities for the C++ library testsuite.
4 // Copyright (C) 2003-2017 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 3, 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 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>
36 #include <testsuite_common_types.h>
38 #if defined (__linux__) || defined (__GLIBC__)
40 #elif defined (__FreeBSD__)
52 struct mallinfo m
= { (((std::size_t) sbrk (0) + 1023) / 1024), 0 };
56 #elif !defined (__hpux__)
65 struct mallinfo empty
= { 0, 0 };
78 clock_t elapsed_begin
;
85 time_counter() : elapsed_begin(), elapsed_end(), tms_begin(), tms_end()
91 elapsed_begin
= clock_t();
92 elapsed_end
= clock_t();
101 elapsed_begin
= times(&tms_begin
);
102 const clock_t err
= clock_t(-1);
103 if (elapsed_begin
== err
)
104 std::__throw_runtime_error("time_counter::start");
110 elapsed_end
= times(&tms_end
);
111 const clock_t err
= clock_t(-1);
112 if (elapsed_end
== err
)
113 std::__throw_runtime_error("time_counter::stop");
118 { return elapsed_end
- elapsed_begin
; }
122 { return tms_end
.tms_utime
- tms_begin
.tms_utime
; }
126 { return tms_end
.tms_stime
- tms_begin
.tms_stime
; }
129 class resource_counter
134 struct mallinfo allocation_begin
;
135 struct mallinfo allocation_end
;
138 resource_counter(int i
= RUSAGE_SELF
) : who(i
)
144 memset(&rusage_begin
, 0, sizeof(rusage_begin
));
145 memset(&rusage_end
, 0, sizeof(rusage_end
));
146 memset(&allocation_begin
, 0, sizeof(allocation_begin
));
147 memset(&allocation_end
, 0, sizeof(allocation_end
));
153 if (getrusage(who
, &rusage_begin
) != 0 )
154 memset(&rusage_begin
, 0, sizeof(rusage_begin
));
155 malloc(0); // Needed for some implementations.
156 allocation_begin
= mallinfo();
162 if (getrusage(who
, &rusage_end
) != 0 )
163 memset(&rusage_end
, 0, sizeof(rusage_end
));
164 allocation_end
= mallinfo();
168 allocated_memory() const
169 { return ((allocation_end
.uordblks
- allocation_begin
.uordblks
)
170 + (allocation_end
.hblkhd
- allocation_begin
.hblkhd
)); }
173 hard_page_fault() const
174 { return rusage_end
.ru_majflt
- rusage_begin
.ru_majflt
; }
178 { return rusage_end
.ru_nswap
- rusage_begin
.ru_nswap
; }
182 start_counters(time_counter
& t
, resource_counter
& r
)
189 stop_counters(time_counter
& t
, resource_counter
& r
)
196 clear_counters(time_counter
& t
, resource_counter
& r
)
203 report_performance(const std::string file
, const std::string comment
,
204 const time_counter
& t
, const resource_counter
& r
)
206 const char space
= ' ';
207 const char tab
= '\t';
208 const char* name
= "libstdc++-performance.sum";
209 std::string::const_iterator i
= file
.begin() + file
.find_last_of('/') + 1;
210 std::string
testname(i
, file
.end());
212 std::ofstream
out(name
, std::ios_base::app
);
215 if (__gthread_active_p())
216 testname
.append("-thread");
219 out
.setf(std::ios_base::left
);
220 out
<< std::setw(25) << testname
<< tab
;
221 out
<< std::setw(25) << comment
<< tab
;
223 out
.setf(std::ios_base::right
);
224 out
<< std::setw(4) << t
.real_time() << "r" << space
;
225 out
<< std::setw(4) << t
.user_time() << "u" << space
;
226 out
<< std::setw(4) << t
.system_time() << "s" << space
;
227 out
<< std::setw(8) << r
.allocated_memory() << "mem" << space
;
228 out
<< std::setw(4) << r
.hard_page_fault() << "pf" << space
;
235 report_header(const std::string file
, const std::string header
)
237 const char space
= ' ';
238 const char tab
= '\t';
239 const char* name
= "libstdc++-performance.sum";
240 std::string::const_iterator i
= file
.begin() + file
.find_last_of('/') + 1;
241 std::string
testname(i
, file
.end());
243 std::ofstream
out(name
, std::ios_base::app
);
246 if (__gthread_active_p ())
247 testname
.append("-thread");
250 out
.setf(std::ios_base::left
);
251 out
<< std::setw(25) << testname
<< tab
;
252 out
<< std::setw(40) << header
<< tab
;
257 } // namespace __gnu_test
260 // Ah, we wish it wasn't so...
261 bool first_container
= false;
262 extern const char* filename
;
264 typedef std::string::size_type (*callback_type
) (std::string
&);
266 template<typename Container
, int Iter
, bool Thread
>
268 write_viz_container(callback_type find_container
, const char* filename
)
270 typedef std::string string
;
275 std::ostringstream title
;
277 std::string
titlename(filename
);
278 std::string::size_type n
= titlename
.find('.');
279 if (n
!= string::npos
)
280 titlename
= std::string(titlename
.begin(), titlename
.begin() + n
);
288 std::boolalpha(title
);
293 titlename
+= ".title";
294 std::ofstream
titlefile(titlename
.c_str());
295 if (!titlefile
.good())
296 throw std::runtime_error("write_viz_data cannot open titlename");
297 titlefile
<< title
.str() << std::endl
;
300 // Create compressed type name.
303 std::string
type(abi::__cxa_demangle(typeid(obj
).name(), 0, 0, &status
));
305 // Extract fully-qualified typename.
306 // Assumes "set" or "map" are uniquely determinate.
307 string::iterator beg
= type
.begin();
308 string::iterator end
;
309 string::size_type n
= (*find_container
)(type
);
311 // Find start of fully-qualified name.
312 // Assume map, find end.
313 string::size_type nend
= type
.find('<', n
);
314 if (nend
!= string::npos
)
315 end
= type
.begin() + nend
;
317 string compressed_type
;
318 compressed_type
+= '"';
319 compressed_type
+= string(beg
, end
);
320 compressed_type
+= '<';
322 typename
Container::key_type v
;
323 compressed_type
+= typeid(v
).name();
325 compressed_type
+= "int";
327 compressed_type
+= ", A>";
331 compressed_type
+= " thread";
332 compressed_type
+= '"';
334 std::ofstream
file(filename
, std::ios_base::app
);
336 throw std::runtime_error("write_viz_data cannot open filename");
338 file
<< compressed_type
;
339 first_container
= false;
344 write_viz_data(__gnu_test::time_counter
& time
, const char* filename
)
346 std::ofstream
file(filename
, std::ios_base::app
);
348 throw std::runtime_error("write_viz_data cannot open filename");
350 // Print out score in appropriate column.
351 const char tab('\t');
352 int score
= time
.real_time();
353 file
<< tab
<< score
;
357 write_viz_endl(const char* filename
)
359 std::ofstream
file(filename
, std::ios_base::app
);
361 throw std::runtime_error("write_viz_endl cannot open filename");
366 // Function template, function objects for the tests.
367 template<typename TestType
>
368 struct value_type
: public std::pair
<const TestType
, TestType
>
370 inline value_type
& operator++()
376 inline operator TestType() const { return this->second
; }
379 template<typename Container
, int Iter
>
383 template<typename Container
, int Iter
>
385 do_thread(void* p
= 0)
387 do_loop
<Container
, Iter
>();
391 template<typename Container
, int Iter
, bool Thread
>
393 test_container(const char* filename
)
395 using namespace __gnu_test
;
397 resource_counter resource
;
399 start_counters(time
, resource
);
402 // No threads, so run 4x.
403 do_loop
<Container
, Iter
* 4>();
407 #if defined (_GLIBCXX_GCC_GTHR_POSIX_H) && !defined (NOTHREAD)
408 pthread_t t1
, t2
, t3
, t4
;
409 pthread_create(&t1
, 0, &do_thread
<Container
, Iter
>, 0);
410 pthread_create(&t2
, 0, &do_thread
<Container
, Iter
>, 0);
411 pthread_create(&t3
, 0, &do_thread
<Container
, Iter
>, 0);
412 pthread_create(&t4
, 0, &do_thread
<Container
, Iter
>, 0);
420 stop_counters(time
, resource
);
422 // Detailed text data.
425 std::ostringstream comment
;
426 comment
<< "type: " << abi::__cxa_demangle(typeid(obj
).name(),
428 report_header(filename
, comment
.str());
429 report_performance("", "", time
, resource
);
431 // Detailed data for visualization.
432 std::string
vizfilename(filename
);
433 vizfilename
+= ".dat";
434 write_viz_data(time
, vizfilename
.c_str());
438 template<bool Thread
>
441 test_sequence(const char* filename
) : _M_filename(filename
) { }
443 template<class Container
>
445 operator()(Container
)
448 test_container
<Container
, i
, Thread
>(_M_filename
);
452 const char* _M_filename
;
456 inline std::string::size_type
457 sequence_find_container(std::string
& type
)
459 const std::string::size_type npos
= std::string::npos
;
460 std::string::size_type n1
= type
.find("vector");
461 std::string::size_type n2
= type
.find("list");
462 std::string::size_type n3
= type
.find("deque");
463 std::string::size_type n4
= type
.find("string");
465 if (n1
!= npos
|| n2
!= npos
|| n3
!= npos
|| n4
!= npos
)
466 return std::min(std::min(n1
, n2
), std::min(n3
, n4
));
468 throw std::runtime_error("sequence_find_container not found");
471 inline std::string::size_type
472 associative_find_container(std::string
& type
)
475 string::size_type n1
= type
.find("map");
476 string::size_type n2
= type
.find("set");
477 if (n1
!= string::npos
|| n2
!= string::npos
)
478 return std::min(n1
, n2
);
480 throw std::runtime_error("associative_find_container not found");
483 #endif // _GLIBCXX_PERFORMANCE_H