2 // Testing performance utilities for the C++ library testsuite.
4 // Copyright (C) 2003, 2004, 2005, 2007, 2008, 2009
5 // Free Software Foundation, Inc.
7 // This file is part of the GNU ISO C++ Library. This library is free
8 // software; you can redistribute it and/or modify it under the
9 // terms of the GNU General Public License as published by the
10 // Free Software Foundation; either version 3, or (at your option)
13 // This library is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
18 // You should have received a copy of the GNU General Public License along
19 // with this library; see the file COPYING3. If not see
20 // <http://www.gnu.org/licenses/>.
23 #ifndef _GLIBCXX_PERFORMANCE_H
24 #define _GLIBCXX_PERFORMANCE_H
26 #include <sys/times.h>
27 #include <sys/resource.h>
37 #include <testsuite_common_types.h>
41 #elif defined (__FreeBSD__)
53 struct mallinfo m = { (((size_t) sbrk (0) + 1023) / 1024), 0 };
57 #elif !defined (__hpux__)
66 struct mallinfo empty = { 0, 0 };
79 clock_t elapsed_begin;
86 time_counter() : elapsed_begin(), elapsed_end(), tms_begin(), tms_end()
92 elapsed_begin = clock_t();
93 elapsed_end = clock_t();
102 elapsed_begin = times(&tms_begin);
103 const clock_t err = clock_t(-1);
104 if (elapsed_begin == err)
105 std::__throw_runtime_error("time_counter::start");
111 elapsed_end = times(&tms_end);
112 const clock_t err = clock_t(-1);
113 if (elapsed_end == err)
114 std::__throw_runtime_error("time_counter::stop");
119 { return elapsed_end - elapsed_begin; }
123 { return tms_end.tms_utime - tms_begin.tms_utime; }
127 { return tms_end.tms_stime - tms_begin.tms_stime; }
130 class resource_counter
135 struct mallinfo allocation_begin;
136 struct mallinfo allocation_end;
139 resource_counter(int i = RUSAGE_SELF) : who(i)
145 memset(&rusage_begin, 0, sizeof(rusage_begin));
146 memset(&rusage_end, 0, sizeof(rusage_end));
147 memset(&allocation_begin, 0, sizeof(allocation_begin));
148 memset(&allocation_end, 0, sizeof(allocation_end));
154 if (getrusage(who, &rusage_begin) != 0 )
155 memset(&rusage_begin, 0, sizeof(rusage_begin));
156 malloc(0); // Needed for some implementations.
157 allocation_begin = mallinfo();
163 if (getrusage(who, &rusage_end) != 0 )
164 memset(&rusage_end, 0, sizeof(rusage_end));
165 allocation_end = mallinfo();
169 allocated_memory() const
170 { return ((allocation_end.uordblks - allocation_begin.uordblks)
171 + (allocation_end.hblkhd - allocation_begin.hblkhd)); }
174 hard_page_fault() const
175 { return rusage_end.ru_majflt - rusage_begin.ru_majflt; }
179 { return rusage_end.ru_nswap - rusage_begin.ru_nswap; }
183 start_counters(time_counter& t, resource_counter& r)
190 stop_counters(time_counter& t, resource_counter& r)
197 clear_counters(time_counter& t, resource_counter& r)
204 report_performance(const std::string file, const std::string comment,
205 const time_counter& t, const resource_counter& r)
207 const char space = ' ';
208 const char tab = '\t';
209 const char* name = "libstdc++-performance.sum";
210 std::string::const_iterator i = file.begin() + file.find_last_of('/') + 1;
211 std::string testname(i, file.end());
213 std::ofstream out(name, std::ios_base::app);
216 if (__gthread_active_p())
217 testname.append("-thread");
220 out.setf(std::ios_base::left);
221 out << std::setw(25) << testname << tab;
222 out << std::setw(25) << comment << tab;
224 out.setf(std::ios_base::right);
225 out << std::setw(4) << t.real_time() << "r" << space;
226 out << std::setw(4) << t.user_time() << "u" << space;
227 out << std::setw(4) << t.system_time() << "s" << space;
228 out << std::setw(8) << r.allocated_memory() << "mem" << space;
229 out << std::setw(4) << r.hard_page_fault() << "pf" << space;
236 report_header(const std::string file, const std::string header)
238 const char space = ' ';
239 const char tab = '\t';
240 const char* name = "libstdc++-performance.sum";
241 std::string::const_iterator i = file.begin() + file.find_last_of('/') + 1;
242 std::string testname(i, file.end());
244 std::ofstream out(name, std::ios_base::app);
247 if (__gthread_active_p ())
248 testname.append("-thread");
251 out.setf(std::ios_base::left);
252 out << std::setw(25) << testname << tab;
253 out << std::setw(40) << header << tab;
258 } // namespace __gnu_test
261 // Ah, we wish it wasn't so...
262 bool first_container = false;
263 extern const char* filename;
265 typedef std::string::size_type (*callback_type) (std::string&);
267 template<typename Container, int Iter, bool Thread>
269 write_viz_container(callback_type find_container, const char* filename)
271 typedef std::string string;
276 std::ostringstream title;
278 std::string titlename(filename);
279 std::string::size_type n = titlename.find('.');
280 if (n != string::npos)
281 titlename = std::string(titlename.begin(), titlename.begin() + n);
289 std::boolalpha(title);
294 titlename += ".title";
295 std::ofstream titlefile(titlename.c_str());
296 if (!titlefile.good())
297 throw std::runtime_error("write_viz_data cannot open titlename");
298 titlefile << title.str() << std::endl;
301 // Create compressed type name.
304 std::string type(abi::__cxa_demangle(typeid(obj).name(), 0, 0, &status));
306 // Extract fully-qualified typename.
307 // Assumes "set" or "map" are uniquely determinate.
308 string::iterator beg = type.begin();
309 string::iterator end;
310 string::size_type n = (*find_container)(type);
312 // Find start of fully-qualified name.
313 // Assume map, find end.
314 string::size_type nend = type.find('<', n);
315 if (nend != string::npos)
316 end = type.begin() + nend;
318 string compressed_type;
319 compressed_type += '"';
320 compressed_type += string(beg, end);
321 compressed_type += '<';
323 typename Container::key_type v;
324 compressed_type += typeid(v).name();
326 compressed_type += "int";
328 compressed_type += ", A>";
332 compressed_type += " thread";
333 compressed_type += '"';
335 std::ofstream file(filename, std::ios_base::app);
337 throw std::runtime_error("write_viz_data cannot open filename");
339 file << compressed_type;
340 first_container = false;
345 write_viz_data(__gnu_test::time_counter& time, const char* filename)
347 std::ofstream file(filename, std::ios_base::app);
349 throw std::runtime_error("write_viz_data cannot open filename");
351 // Print out score in appropriate column.
352 const char tab('\t');
353 int score = time.real_time();
354 file << tab << score;
358 write_viz_endl(const char* filename)
360 std::ofstream file(filename, std::ios_base::app);
362 throw std::runtime_error("write_viz_endl cannot open filename");
367 // Function template, function objects for the tests.
368 template<typename TestType>
369 struct value_type : public std::pair<const TestType, TestType>
371 inline value_type& operator++()
377 inline operator TestType() const { return this->second; }
380 template<typename Container, int Iter>
384 template<typename Container, int Iter>
386 do_thread(void* p = NULL)
388 do_loop<Container, Iter>();
392 template<typename Container, int Iter, bool Thread>
394 test_container(const char* filename)
396 using namespace __gnu_test;
398 resource_counter resource;
400 start_counters(time, resource);
403 // No threads, so run 4x.
404 do_loop<Container, Iter * 4>();
408 #if defined (_GLIBCXX_GCC_GTHR_POSIX_H) && !defined (NOTHREAD)
409 pthread_t t1, t2, t3, t4;
410 pthread_create(&t1, 0, &do_thread<Container, Iter>, 0);
411 pthread_create(&t2, 0, &do_thread<Container, Iter>, 0);
412 pthread_create(&t3, 0, &do_thread<Container, Iter>, 0);
413 pthread_create(&t4, 0, &do_thread<Container, Iter>, 0);
415 pthread_join(t1, NULL);
416 pthread_join(t2, NULL);
417 pthread_join(t3, NULL);
418 pthread_join(t4, NULL);
421 stop_counters(time, resource);
423 // Detailed text data.
426 std::ostringstream comment;
427 comment << "type: " << abi::__cxa_demangle(typeid(obj).name(),
429 report_header(filename, comment.str());
430 report_performance("", "", time, resource);
432 // Detailed data for visualization.
433 std::string vizfilename(filename);
434 vizfilename += ".dat";
435 write_viz_data(time, vizfilename.c_str());
439 template<bool Thread>
442 test_sequence(const char* filename) : _M_filename(filename) { }
444 template<class Container>
446 operator()(Container)
449 test_container<Container, i, Thread>(_M_filename);
453 const char* _M_filename;
457 inline std::string::size_type
458 sequence_find_container(std::string& type)
460 const std::string::size_type npos = std::string::npos;
461 std::string::size_type n1 = type.find("vector");
462 std::string::size_type n2 = type.find("list");
463 std::string::size_type n3 = type.find("deque");
464 std::string::size_type n4 = type.find("string");
466 if (n1 != npos || n2 != npos || n3 != npos || n4 != npos)
467 return std::min(std::min(n1, n2), std::min(n3, n4));
469 throw std::runtime_error("sequence_find_container not found");
472 inline std::string::size_type
473 associative_find_container(std::string& type)
476 string::size_type n1 = type.find("map");
477 string::size_type n2 = type.find("set");
478 if (n1 != string::npos || n2 != string::npos)
479 return std::min(n1, n2);
481 throw std::runtime_error("associative_find_container not found");
484 #endif // _GLIBCXX_PERFORMANCE_H