OSDN Git Service

* testsuite/lib/libstdc++.exp: Load target-supports-dg.exp.
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / testsuite / testsuite_performance.h
1 // -*- C++ -*-
2 // Testing performance utilities for the C++ library testsuite.
3 //
4 // Copyright (C) 2003, 2004, 2005 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 _GLIBCXX_PERFORMANCE_H
32 #define _GLIBCXX_PERFORMANCE_H
33
34 #include <sys/times.h>
35 #include <sys/resource.h>
36 #include <cstdlib>
37 #include <string>
38 #include <fstream>
39 #include <iomanip>
40
41 #ifdef __linux__
42 #include <malloc.h>
43 #elif defined (__FreeBSD__)
44 extern "C"
45 {
46   struct mallinfo
47   {
48     int uordblks;
49     int hblkhd;
50   };
51
52   struct mallinfo
53   mallinfo(void)
54   {
55     struct mallinfo m = { (((size_t) sbrk (0) + 1023) / 1024), 0 };
56     return m;
57   }
58 }
59 #elif !defined (__hpux__)
60 extern "C"
61 {
62   struct mallinfo
63   {
64     int uordblks;
65     int hblkhd;
66   };
67
68   struct mallinfo empty = { 0, 0 };
69
70   struct mallinfo
71   mallinfo(void)
72   { return empty; }
73 }
74 #endif
75
76 namespace __gnu_test
77 {
78   class time_counter
79   {
80   private:
81     clock_t     elapsed_begin;
82     clock_t     elapsed_end;
83     tms         tms_begin;
84     tms         tms_end;
85
86   public:
87     explicit
88     time_counter() : elapsed_begin(), elapsed_end(), tms_begin(), tms_end()
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     }
99
100     void
101     start()
102     { 
103       this->clear();
104       elapsed_begin = times(&tms_begin); 
105       const clock_t err = clock_t(-1);
106       if (elapsed_begin == err)
107         std::__throw_runtime_error("time_counter::start");
108     }
109     
110     void
111     stop()
112     { 
113       elapsed_end = times(&tms_end); 
114       const clock_t err = clock_t(-1);
115       if (elapsed_end == err)
116         std::__throw_runtime_error("time_counter::stop");
117     }
118
119     size_t
120     real_time() const
121     { return elapsed_end - elapsed_begin; }
122
123     size_t
124     user_time() const
125     { return tms_end.tms_utime - tms_begin.tms_utime; }
126
127     size_t
128     system_time() const
129     { return tms_end.tms_stime - tms_begin.tms_stime; }
130   };
131
132   class resource_counter
133   {
134     int                 who;
135     rusage              rusage_begin;
136     rusage              rusage_end;
137     struct mallinfo     allocation_begin;
138     struct mallinfo     allocation_end;
139
140   public:
141     resource_counter(int i = RUSAGE_SELF) : who(i)
142     { this->clear(); }
143     
144     void 
145     clear() throw()
146     { 
147       memset(&rusage_begin, 0, sizeof(rusage_begin)); 
148       memset(&rusage_end, 0, sizeof(rusage_end)); 
149       memset(&allocation_begin, 0, sizeof(allocation_begin)); 
150       memset(&allocation_end, 0, sizeof(allocation_end)); 
151     }
152
153     void
154     start()
155     { 
156       if (getrusage(who, &rusage_begin) != 0 )
157         memset(&rusage_begin, 0, sizeof(rusage_begin));
158       malloc(0); // Needed for some implementations.
159       allocation_begin = mallinfo();
160     }
161     
162     void
163     stop()
164     { 
165       if (getrusage(who, &rusage_end) != 0 )
166         memset(&rusage_end, 0, sizeof(rusage_end));
167       allocation_end = mallinfo();
168     }
169
170     int
171     allocated_memory() const
172     { return ((allocation_end.uordblks - allocation_begin.uordblks)
173               + (allocation_end.hblkhd - allocation_begin.hblkhd)); }
174     
175     long 
176     hard_page_fault() const
177     { return rusage_end.ru_majflt - rusage_begin.ru_majflt; }
178
179     long 
180     swapped() const
181     { return rusage_end.ru_nswap - rusage_begin.ru_nswap; }
182   };
183
184   inline void
185   start_counters(time_counter& t, resource_counter& r)
186   {
187     t.start();
188     r.start();
189   }
190
191   inline void
192   stop_counters(time_counter& t, resource_counter& r)
193   {
194     t.stop();
195     r.stop();
196   }
197
198   inline void
199   clear_counters(time_counter& t, resource_counter& r)
200   {
201     t.clear();
202     r.clear();
203   }
204
205   void
206   report_performance(const std::string file, const std::string comment, 
207                      const time_counter& t, const resource_counter& r)
208   {
209     const char space = ' ';
210     const char tab = '\t';
211     const char* name = "libstdc++-performance.sum";
212     std::string::const_iterator i = file.begin() + file.find_last_of('/') + 1;
213     std::string testname(i, file.end());
214
215     std::ofstream out(name, std::ios_base::app);
216
217 #ifdef __GTHREADS
218     if (__gthread_active_p())
219       testname.append("-thread");
220 #endif
221
222     out.setf(std::ios_base::left);
223     out << std::setw(25) << testname << tab;
224     out << std::setw(25) << comment << tab;
225
226     out.setf(std::ios_base::right);
227     out << std::setw(4) << t.real_time() << "r" << space;
228     out << std::setw(4) << t.user_time() << "u" << space;
229     out << std::setw(4) << t.system_time() << "s" << space;
230     out << std::setw(8) << r.allocated_memory() << "mem" << space;
231     out << std::setw(4) << r.hard_page_fault() << "pf" << space;
232     
233     out << std::endl;
234     out.close();
235   }
236
237   void
238   report_header(const std::string file, const std::string header)
239   {
240     const char space = ' ';
241     const char tab = '\t';
242     const char* name = "libstdc++-performance.sum";
243     std::string::const_iterator i = file.begin() + file.find_last_of('/') + 1;
244     std::string testname(i, file.end());
245
246     std::ofstream out(name, std::ios_base::app);
247
248 #ifdef __GTHREADS
249     if (__gthread_active_p ())
250       testname.append("-thread");
251 #endif
252
253     out.setf(std::ios_base::left);
254     out << std::setw(25) << testname << tab;
255     out << std::setw(40) << header << tab;
256
257     out << std::endl;
258     out.close();
259   }
260 }; // namespace __gnu_test
261
262 #endif // _GLIBCXX_PERFORMANCE_H
263