]> gcc.gnu.org Git - gcc.git/blob - libstdc++-v3/testsuite/testsuite_performance.h
testsuite_performance.h (resource_counter): Don't use mallinfo at the moment.
[gcc.git] / libstdc++-v3 / testsuite / testsuite_performance.h
1 // -*- C++ -*-
2 // Testing performance utilities for the C++ library testsuite.
3 //
4 // Copyright (C) 2003 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.
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 COPYING. If not, write to the Free
19 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
20 // USA.
21 //
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.
30
31 #ifndef _GLIBCPP_PERFORMANCE_H
32 #define _GLIBCPP_PERFORMANCE_H
33
34 #include <sys/times.h>
35 #include <sys/resource.h>
36 #include <malloc.h>
37 #include <string>
38 #include <fstream>
39 #include <iomanip>
40
41 namespace __gnu_cxx_test
42 {
43 class time_counter
44 {
45 clock_t elapsed_begin;
46 clock_t elapsed_end;
47 tms tms_begin;
48 tms tms_end;
49
50 public:
51 time_counter()
52 { this->clear(); }
53
54 void
55 clear()
56 {
57 elapsed_begin = 0;
58 elapsed_end = 0;
59 memset(&tms_begin, 0, sizeof(tms));
60 memset(&tms_end, 0, sizeof(tms));
61 }
62
63 void
64 start()
65 { elapsed_begin = times(&tms_begin); }
66
67 void
68 stop()
69 { elapsed_end = times(&tms_end); }
70
71 size_t
72 real_time() const
73 { return elapsed_end - elapsed_begin; }
74
75 size_t
76 user_time() const
77 { return tms_end.tms_utime - tms_begin.tms_utime; }
78
79 size_t
80 system_time() const
81 { return tms_end.tms_stime - tms_begin.tms_stime; }
82 };
83
84 class resource_counter
85 {
86 int who;
87 rusage rusage_begin;
88 rusage rusage_end;
89 struct mallinfo allocation_begin;
90 struct mallinfo allocation_end;
91
92 public:
93 resource_counter(int i = RUSAGE_SELF) : who(i)
94 { this->clear(); }
95
96 void
97 clear()
98 {
99 memset(&rusage_begin, 0, sizeof(rusage_begin));
100 memset(&rusage_end, 0, sizeof(rusage_end));
101 memset(&allocation_begin, 0, sizeof(allocation_begin));
102 memset(&allocation_end, 0, sizeof(allocation_end));
103 }
104
105 void
106 start()
107 {
108 if (getrusage(who, &rusage_begin) != 0 )
109 memset(&rusage_begin, 0, sizeof(rusage_begin));
110 // allocation_begin = mallinfo();
111 }
112
113 void
114 stop()
115 {
116 if (getrusage(who, &rusage_end) != 0 )
117 memset(&rusage_end, 0, sizeof(rusage_end));
118 // allocation_end = mallinfo();
119 }
120
121 int
122 allocated_memory() const
123 { return allocation_end.arena - allocation_begin.arena; }
124
125 long
126 hard_page_fault() const
127 { return rusage_end.ru_majflt - rusage_begin.ru_majflt; }
128
129 long
130 swapped() const
131 { return rusage_end.ru_nswap - rusage_begin.ru_nswap; }
132 };
133
134 void
135 start_counters(time_counter& t, resource_counter& r)
136 {
137 t.start();
138 r.start();
139 }
140
141 void
142 stop_counters(time_counter& t, resource_counter& r)
143 {
144 t.stop();
145 r.stop();
146 }
147
148 void
149 clear_counters(time_counter& t, resource_counter& r)
150 {
151 t.clear();
152 r.clear();
153 }
154
155 void
156 report_performance(const std::string file, const std::string comment,
157 const time_counter& t, const resource_counter& r)
158 {
159 const char space = ' ';
160 const char tab = '\t';
161 const char* name = "libstdc++-v3.performance";
162 std::string::const_iterator i = file.begin() + file.find_last_of('/') + 1;
163 std::string testname(i, file.end());
164
165 std::ofstream out(name, std::ios_base::app);
166
167 out.setf(std::ios_base::left);
168 out << std::setw(25) << testname << tab;
169 out << std::setw(10) << comment << tab;
170
171 out.setf(std::ios_base::right);
172 out << std::setw(4) << t.real_time() << "r" << space;
173 out << std::setw(4) << t.user_time() << "u" << space;
174 out << std::setw(4) << t.system_time() << "s" << space;
175 // out << std::setw(4) << r.allocated_memory() << "mem" << space;
176 out << std::setw(4) << r.hard_page_fault() << "pf" << space;
177
178 out << std::endl;
179 out.close();
180 }
181 }; // namespace __gnu_cxx_test
182
183 #endif // _GLIBCPP_PERFORMANCE_H
184
This page took 0.048733 seconds and 6 git commands to generate.