| 1 |
// -*- C++ -*- |
| 2 |
// Testing performance utilities for the C++ library testsuite. |
| 3 |
// |
| 4 |
// Copyright (C) 2003-2025 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. |
| 11 |
// |
| 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. |
| 16 |
// |
| 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/>. |
| 20 |
// |
| 21 |
|
| 22 |
#ifndef _GLIBCXX_PERFORMANCE_H |
| 23 |
#define _GLIBCXX_PERFORMANCE_H |
| 24 |
|
| 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 <ext/atomicity.h> |
| 37 |
#include <testsuite_common_types.h> |
| 38 |
|
| 39 |
#if defined (__linux__) || defined (__GLIBC__) |
| 40 |
#include <malloc.h> // For mallinfo. |
| 41 |
#endif |
| 42 |
|
| 43 |
namespace __gnu_test |
| 44 |
{ |
| 45 |
struct MallocInfo |
| 46 |
{ |
| 47 |
MallocInfo() : uordblks(0), hblkhd(0) { } |
| 48 |
MallocInfo(std::size_t uordblocks, std::size_t hblockhd) |
| 49 |
: uordblks(uordblocks), hblkhd(hblockhd) |
| 50 |
{ } |
| 51 |
|
| 52 |
std::size_t uordblks; |
| 53 |
std::size_t hblkhd; |
| 54 |
}; |
| 55 |
|
| 56 |
MallocInfo |
| 57 |
malloc_info() |
| 58 |
{ |
| 59 |
#if defined (__linux__) || defined (__hpux__) || defined (__GLIBC__) |
| 60 |
#if __GLIBC__ > 2 || __GLIBC__ == 2 && __GLIBC_MINOR__ >= 33 |
| 61 |
struct mallinfo2 mi = mallinfo2(); |
| 62 |
#else |
| 63 |
struct mallinfo mi = mallinfo(); |
| 64 |
#endif |
| 65 |
return MallocInfo(mi.uordblks, mi.hblkhd); |
| 66 |
#elif defined (__FreeBSD__) |
| 67 |
return MallocInfo((((std::size_t) sbrk (0) + 1023) / 1024), 0); |
| 68 |
#else |
| 69 |
return MallocInfo(); |
| 70 |
#endif |
| 71 |
} |
| 72 |
|
| 73 |
class time_counter |
| 74 |
{ |
| 75 |
private: |
| 76 |
// All times are measured in clock ticks. |
| 77 |
// There are CLOCKS_PER_SEC ticks per second. |
| 78 |
// POSIX requires CLOCKS_PER_SEC == 1000000 so ticks == microseconds. |
| 79 |
clock_t elapsed_begin; |
| 80 |
clock_t elapsed_end; |
| 81 |
tms tms_begin; |
| 82 |
tms tms_end; |
| 83 |
std::size_t splits[3]; |
| 84 |
|
| 85 |
public: |
| 86 |
explicit |
| 87 |
time_counter() |
| 88 |
: elapsed_begin(), elapsed_end(), tms_begin(), tms_end(), splits() |
| 89 |
{ } |
| 90 |
|
| 91 |
void |
| 92 |
clear() throw() |
| 93 |
{ |
| 94 |
elapsed_begin = clock_t(); |
| 95 |
elapsed_end = clock_t(); |
| 96 |
tms_begin = tms(); |
| 97 |
tms_end = tms(); |
| 98 |
splits[0] = splits[1] = splits[2] = 0; |
| 99 |
} |
| 100 |
|
| 101 |
void |
| 102 |
start() |
| 103 |
{ |
| 104 |
this->clear(); |
| 105 |
elapsed_begin = times(&tms_begin); |
| 106 |
const clock_t err = clock_t(-1); |
| 107 |
if (elapsed_begin == err) |
| 108 |
std::__throw_runtime_error("time_counter::start"); |
| 109 |
} |
| 110 |
|
| 111 |
void |
| 112 |
stop() |
| 113 |
{ |
| 114 |
elapsed_end = times(&tms_end); |
| 115 |
const clock_t err = clock_t(-1); |
| 116 |
if (elapsed_end == err) |
| 117 |
std::__throw_runtime_error("time_counter::stop"); |
| 118 |
} |
| 119 |
|
| 120 |
void |
| 121 |
restart() |
| 122 |
{ |
| 123 |
splits[0] += (elapsed_end - elapsed_begin); |
| 124 |
splits[1] += (tms_end.tms_utime - tms_begin.tms_utime); |
| 125 |
splits[2] += (tms_end.tms_stime - tms_begin.tms_stime); |
| 126 |
elapsed_begin = times(&tms_begin); |
| 127 |
const clock_t err = clock_t(-1); |
| 128 |
if (elapsed_begin == err) |
| 129 |
std::__throw_runtime_error("time_counter::restart"); |
| 130 |
} |
| 131 |
|
| 132 |
std::size_t |
| 133 |
real_time() const |
| 134 |
{ return (elapsed_end - elapsed_begin) + splits[0]; } |
| 135 |
|
| 136 |
std::size_t |
| 137 |
user_time() const |
| 138 |
{ return (tms_end.tms_utime - tms_begin.tms_utime) + splits[1]; } |
| 139 |
|
| 140 |
std::size_t |
| 141 |
system_time() const |
| 142 |
{ return (tms_end.tms_stime - tms_begin.tms_stime) + splits[2]; } |
| 143 |
}; |
| 144 |
|
| 145 |
class resource_counter |
| 146 |
{ |
| 147 |
int who; |
| 148 |
rusage rusage_begin; |
| 149 |
rusage rusage_end; |
| 150 |
MallocInfo allocation_begin; |
| 151 |
MallocInfo allocation_end; |
| 152 |
|
| 153 |
public: |
| 154 |
resource_counter(int i = RUSAGE_SELF) : who(i) |
| 155 |
{ this->clear(); } |
| 156 |
|
| 157 |
void |
| 158 |
clear() throw() |
| 159 |
{ |
| 160 |
memset(&rusage_begin, 0, sizeof(rusage_begin)); |
| 161 |
memset(&rusage_end, 0, sizeof(rusage_end)); |
| 162 |
memset(&allocation_begin, 0, sizeof(allocation_begin)); |
| 163 |
memset(&allocation_end, 0, sizeof(allocation_end)); |
| 164 |
} |
| 165 |
|
| 166 |
void |
| 167 |
start() |
| 168 |
{ |
| 169 |
if (getrusage(who, &rusage_begin) != 0 ) |
| 170 |
memset(&rusage_begin, 0, sizeof(rusage_begin)); |
| 171 |
void* p __attribute__((unused)) = malloc(0); // Needed for some implementations. |
| 172 |
allocation_begin = malloc_info(); |
| 173 |
} |
| 174 |
|
| 175 |
void |
| 176 |
stop() |
| 177 |
{ |
| 178 |
if (getrusage(who, &rusage_end) != 0 ) |
| 179 |
memset(&rusage_end, 0, sizeof(rusage_end)); |
| 180 |
allocation_end = malloc_info(); |
| 181 |
} |
| 182 |
|
| 183 |
int |
| 184 |
allocated_memory() const |
| 185 |
{ return ((allocation_end.uordblks - allocation_begin.uordblks) |
| 186 |
+ (allocation_end.hblkhd - allocation_begin.hblkhd)); } |
| 187 |
|
| 188 |
long |
| 189 |
hard_page_fault() const |
| 190 |
{ return rusage_end.ru_majflt - rusage_begin.ru_majflt; } |
| 191 |
|
| 192 |
long |
| 193 |
swapped() const |
| 194 |
{ return rusage_end.ru_nswap - rusage_begin.ru_nswap; } |
| 195 |
}; |
| 196 |
|
| 197 |
inline void |
| 198 |
start_counters(time_counter& t, resource_counter& r) |
| 199 |
{ |
| 200 |
t.start(); |
| 201 |
r.start(); |
| 202 |
} |
| 203 |
|
| 204 |
inline void |
| 205 |
stop_counters(time_counter& t, resource_counter& r) |
| 206 |
{ |
| 207 |
t.stop(); |
| 208 |
r.stop(); |
| 209 |
} |
| 210 |
|
| 211 |
inline void |
| 212 |
clear_counters(time_counter& t, resource_counter& r) |
| 213 |
{ |
| 214 |
t.clear(); |
| 215 |
r.clear(); |
| 216 |
} |
| 217 |
|
| 218 |
void |
| 219 |
report_performance(const std::string file, const std::string comment, |
| 220 |
const time_counter& t, const resource_counter& r) |
| 221 |
{ |
| 222 |
const char space = ' '; |
| 223 |
const char tab = '\t'; |
| 224 |
const char* name = "libstdc++-performance.sum"; |
| 225 |
std::string::const_iterator i = file.begin() + file.find_last_of('/') + 1; |
| 226 |
std::string testname(i, file.end()); |
| 227 |
|
| 228 |
std::ofstream out(name, std::ios_base::app); |
| 229 |
|
| 230 |
if (!__gnu_cxx::__is_single_threaded()) |
| 231 |
testname.append("-thread"); |
| 232 |
|
| 233 |
out.setf(std::ios_base::left); |
| 234 |
out << std::setw(25) << testname << tab; |
| 235 |
out << std::setw(25) << comment << tab; |
| 236 |
|
| 237 |
out.setf(std::ios_base::right); |
| 238 |
out << std::setw(4) << t.real_time() << "r" << space; |
| 239 |
out << std::setw(4) << t.user_time() << "u" << space; |
| 240 |
out << std::setw(4) << t.system_time() << "s" << space; |
| 241 |
out << std::setw(9) << r.allocated_memory() << "mem" << space; |
| 242 |
out << std::setw(4) << r.hard_page_fault() << "pf" << space; |
| 243 |
|
| 244 |
out << std::endl; |
| 245 |
out.close(); |
| 246 |
} |
| 247 |
|
| 248 |
void |
| 249 |
report_header(const std::string file, const std::string header) |
| 250 |
{ |
| 251 |
const char tab = '\t'; |
| 252 |
const char* name = "libstdc++-performance.sum"; |
| 253 |
std::string::const_iterator i = file.begin() + file.find_last_of('/') + 1; |
| 254 |
std::string testname(i, file.end()); |
| 255 |
|
| 256 |
std::ofstream out(name, std::ios_base::app); |
| 257 |
|
| 258 |
if (!__gnu_cxx::__is_single_threaded ()) |
| 259 |
testname.append("-thread"); |
| 260 |
|
| 261 |
out.setf(std::ios_base::left); |
| 262 |
out << std::setw(25) << testname << tab; |
| 263 |
out << std::setw(40) << header << tab; |
| 264 |
|
| 265 |
out << std::endl; |
| 266 |
out.close(); |
| 267 |
} |
| 268 |
} // namespace __gnu_test |
| 269 |
|
| 270 |
|
| 271 |
// Ah, we wish it wasn't so... |
| 272 |
bool first_container = false; |
| 273 |
extern const char* filename; |
| 274 |
|
| 275 |
typedef std::string::size_type (*callback_type) (std::string&); |
| 276 |
|
| 277 |
template<typename Container, int Iter, bool Thread> |
| 278 |
void |
| 279 |
write_viz_container(callback_type find_container, const char* filename) |
| 280 |
{ |
| 281 |
typedef std::string string; |
| 282 |
|
| 283 |
// Create title. |
| 284 |
{ |
| 285 |
const char ws(' '); |
| 286 |
std::ostringstream title; |
| 287 |
|
| 288 |
std::string titlename(filename); |
| 289 |
std::string::size_type n = titlename.find('.'); |
| 290 |
if (n != string::npos) |
| 291 |
titlename = std::string(titlename.begin(), titlename.begin() + n); |
| 292 |
|
| 293 |
title << titlename; |
| 294 |
title << ws; |
| 295 |
title << Iter; |
| 296 |
title << ws; |
| 297 |
#if 0 |
| 298 |
title << "thread<"; |
| 299 |
std::boolalpha(title); |
| 300 |
title << Thread; |
| 301 |
title << '>'; |
| 302 |
#endif |
| 303 |
|
| 304 |
titlename += ".title"; |
| 305 |
std::ofstream titlefile(titlename.c_str()); |
| 306 |
if (!titlefile.good()) |
| 307 |
throw std::runtime_error("write_viz_data cannot open titlename"); |
| 308 |
titlefile << title.str() << std::endl; |
| 309 |
} |
| 310 |
|
| 311 |
// Create compressed type name. |
| 312 |
Container obj; |
| 313 |
int status; |
| 314 |
std::string type(abi::__cxa_demangle(typeid(obj).name(), 0, 0, &status)); |
| 315 |
|
| 316 |
// Extract fully-qualified typename. |
| 317 |
// Assumes "set" or "map" are uniquely determinate. |
| 318 |
string::iterator beg = type.begin(); |
| 319 |
string::iterator end; |
| 320 |
string::size_type n = (*find_container)(type); |
| 321 |
|
| 322 |
// Find start of fully-qualified name. |
| 323 |
// Assume map, find end. |
| 324 |
string::size_type nend = type.find('<', n); |
| 325 |
if (nend != string::npos) |
| 326 |
end = type.begin() + nend; |
| 327 |
|
| 328 |
string compressed_type; |
| 329 |
compressed_type += '"'; |
| 330 |
compressed_type += string(beg, end); |
| 331 |
compressed_type += '<'; |
| 332 |
#if 0 |
| 333 |
typename Container::key_type v; |
| 334 |
compressed_type += typeid(v).name(); |
| 335 |
#else |
| 336 |
compressed_type += "int"; |
| 337 |
#endif |
| 338 |
compressed_type += ", A>"; |
| 339 |
|
| 340 |
// XXX |
| 341 |
if (Thread == true) |
| 342 |
compressed_type += " thread"; |
| 343 |
compressed_type += '"'; |
| 344 |
|
| 345 |
std::ofstream file(filename, std::ios_base::app); |
| 346 |
if (!file.good()) |
| 347 |
throw std::runtime_error("write_viz_data cannot open filename"); |
| 348 |
|
| 349 |
file << compressed_type; |
| 350 |
first_container = false; |
| 351 |
} |
| 352 |
|
| 353 |
|
| 354 |
void |
| 355 |
write_viz_data(__gnu_test::time_counter& time, const char* filename) |
| 356 |
{ |
| 357 |
std::ofstream file(filename, std::ios_base::app); |
| 358 |
if (!file.good()) |
| 359 |
throw std::runtime_error("write_viz_data cannot open filename"); |
| 360 |
|
| 361 |
// Print out score in appropriate column. |
| 362 |
const char tab('\t'); |
| 363 |
int score = time.real_time(); |
| 364 |
file << tab << score; |
| 365 |
} |
| 366 |
|
| 367 |
void |
| 368 |
write_viz_endl(const char* filename) |
| 369 |
{ |
| 370 |
std::ofstream file(filename, std::ios_base::app); |
| 371 |
if (!file.good()) |
| 372 |
throw std::runtime_error("write_viz_endl cannot open filename"); |
| 373 |
file << std::endl; |
| 374 |
} |
| 375 |
|
| 376 |
|
| 377 |
// Function template, function objects for the tests. |
| 378 |
template<typename TestType> |
| 379 |
struct value_type : public std::pair<const TestType, TestType> |
| 380 |
{ |
| 381 |
inline value_type& operator++() |
| 382 |
{ |
| 383 |
++this->second; |
| 384 |
return *this; |
| 385 |
} |
| 386 |
|
| 387 |
inline operator TestType() const { return this->second; } |
| 388 |
}; |
| 389 |
|
| 390 |
template<typename Container, int Iter> |
| 391 |
void |
| 392 |
do_loop(); |
| 393 |
|
| 394 |
template<typename Container, int Iter> |
| 395 |
void* |
| 396 |
do_thread(void* p = 0) |
| 397 |
{ |
| 398 |
do_loop<Container, Iter>(); |
| 399 |
return p; |
| 400 |
} |
| 401 |
|
| 402 |
template<typename Container, int Iter, bool Thread> |
| 403 |
void |
| 404 |
test_container(const char* filename) |
| 405 |
{ |
| 406 |
using namespace __gnu_test; |
| 407 |
time_counter time; |
| 408 |
resource_counter resource; |
| 409 |
{ |
| 410 |
start_counters(time, resource); |
| 411 |
if (!Thread) |
| 412 |
{ |
| 413 |
// No threads, so run 4x. |
| 414 |
do_loop<Container, Iter * 4>(); |
| 415 |
} |
| 416 |
else |
| 417 |
{ |
| 418 |
#if defined (_GLIBCXX_GCC_GTHR_POSIX_H) && !defined (NOTHREAD) |
| 419 |
pthread_t t1, t2, t3, t4; |
| 420 |
pthread_create(&t1, 0, &do_thread<Container, Iter>, 0); |
| 421 |
pthread_create(&t2, 0, &do_thread<Container, Iter>, 0); |
| 422 |
pthread_create(&t3, 0, &do_thread<Container, Iter>, 0); |
| 423 |
pthread_create(&t4, 0, &do_thread<Container, Iter>, 0); |
| 424 |
|
| 425 |
pthread_join(t1, 0); |
| 426 |
pthread_join(t2, 0); |
| 427 |
pthread_join(t3, 0); |
| 428 |
pthread_join(t4, 0); |
| 429 |
#endif |
| 430 |
} |
| 431 |
stop_counters(time, resource); |
| 432 |
|
| 433 |
// Detailed text data. |
| 434 |
Container obj; |
| 435 |
int status; |
| 436 |
std::ostringstream comment; |
| 437 |
comment << "type: " << abi::__cxa_demangle(typeid(obj).name(), |
| 438 |
0, 0, &status); |
| 439 |
report_header(filename, comment.str()); |
| 440 |
report_performance("", "", time, resource); |
| 441 |
|
| 442 |
// Detailed data for visualization. |
| 443 |
std::string vizfilename(filename); |
| 444 |
vizfilename += ".dat"; |
| 445 |
write_viz_data(time, vizfilename.c_str()); |
| 446 |
} |
| 447 |
} |
| 448 |
|
| 449 |
template<bool Thread> |
| 450 |
struct test_sequence |
| 451 |
{ |
| 452 |
test_sequence(const char* filename) : _M_filename(filename) { } |
| 453 |
|
| 454 |
template<class Container> |
| 455 |
void |
| 456 |
operator()(Container) |
| 457 |
{ |
| 458 |
const int i = 20000; |
| 459 |
test_container<Container, i, Thread>(_M_filename); |
| 460 |
} |
| 461 |
|
| 462 |
private: |
| 463 |
const char* _M_filename; |
| 464 |
}; |
| 465 |
|
| 466 |
|
| 467 |
inline std::string::size_type |
| 468 |
sequence_find_container(std::string& type) |
| 469 |
{ |
| 470 |
const std::string::size_type npos = std::string::npos; |
| 471 |
std::string::size_type n1 = type.find("vector"); |
| 472 |
std::string::size_type n2 = type.find("list"); |
| 473 |
std::string::size_type n3 = type.find("deque"); |
| 474 |
std::string::size_type n4 = type.find("string"); |
| 475 |
|
| 476 |
if (n1 != npos || n2 != npos || n3 != npos || n4 != npos) |
| 477 |
return std::min(std::min(n1, n2), std::min(n3, n4)); |
| 478 |
else |
| 479 |
throw std::runtime_error("sequence_find_container not found"); |
| 480 |
} |
| 481 |
|
| 482 |
inline std::string::size_type |
| 483 |
associative_find_container(std::string& type) |
| 484 |
{ |
| 485 |
using std::string; |
| 486 |
string::size_type n1 = type.find("map"); |
| 487 |
string::size_type n2 = type.find("set"); |
| 488 |
if (n1 != string::npos || n2 != string::npos) |
| 489 |
return std::min(n1, n2); |
| 490 |
else |
| 491 |
throw std::runtime_error("associative_find_container not found"); |
| 492 |
} |
| 493 |
|
| 494 |
#endif // _GLIBCXX_PERFORMANCE_H |
| 495 |
|