OSDN Git Service

* testsuite/testsuite_performance.h (__FreeBSD__): Add fake mallinfo.
[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 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 { int uordblks; };
47   struct mallinfo mallinfo(void)
48     { struct mallinfo m = { (((size_t) sbrk (0) + 1023) / 1024) }; return m; }
49 }
50 #else
51 extern "C"
52 {
53   struct mallinfo { int uordblks; };
54   struct mallinfo empty = { 0 };
55   struct mallinfo mallinfo(void) { return empty; }
56 }
57 #endif
58
59 namespace __gnu_test
60 {
61   class time_counter
62   {
63     clock_t     elapsed_begin;
64     clock_t     elapsed_end;
65     tms         tms_begin;
66     tms         tms_end;
67     
68   public:
69     time_counter() 
70     { this->clear(); }
71
72     void 
73     clear()
74     {
75       elapsed_begin = 0;
76       elapsed_end = 0;
77       memset(&tms_begin, 0, sizeof(tms));
78       memset(&tms_end, 0, sizeof(tms));
79     }
80
81     void
82     start()
83     { elapsed_begin = times(&tms_begin); }
84     
85     void
86     stop()
87     { elapsed_end = times(&tms_end); }
88
89     size_t
90     real_time() const
91     { return elapsed_end - elapsed_begin; }
92
93     size_t
94     user_time() const
95     { return tms_end.tms_utime - tms_begin.tms_utime; }
96
97     size_t
98     system_time() const
99     { return tms_end.tms_stime - tms_begin.tms_stime; }
100   };
101
102   class resource_counter
103   {
104     int         who;
105     rusage      rusage_begin;
106     rusage      rusage_end;
107     struct mallinfo     allocation_begin;
108     struct mallinfo     allocation_end;
109
110   public:
111     resource_counter(int i = RUSAGE_SELF) : who(i)
112     { this->clear(); }
113     
114     void 
115     clear()
116     { 
117       memset(&rusage_begin, 0, sizeof(rusage_begin)); 
118       memset(&rusage_end, 0, sizeof(rusage_end)); 
119       memset(&allocation_begin, 0, sizeof(allocation_begin)); 
120       memset(&allocation_end, 0, sizeof(allocation_end)); 
121     }
122
123     void
124     start()
125     { 
126       if (getrusage(who, &rusage_begin) != 0 )
127         memset(&rusage_begin, 0, sizeof(rusage_begin));
128       malloc(0); // Needed for some implementations.
129       allocation_begin = mallinfo();
130     }
131     
132     void
133     stop()
134     { 
135       if (getrusage(who, &rusage_end) != 0 )
136         memset(&rusage_end, 0, sizeof(rusage_end));
137       allocation_end = mallinfo();
138     }
139
140     int
141     allocated_memory() const
142     { return allocation_end.uordblks - allocation_begin.uordblks; }
143     
144     long 
145     hard_page_fault() const
146     { return rusage_end.ru_majflt - rusage_begin.ru_majflt; }
147
148     long 
149     swapped() const
150     { return rusage_end.ru_nswap - rusage_begin.ru_nswap; }
151   };
152
153   void
154   start_counters(time_counter& t, resource_counter& r)
155   {
156     t.start();
157     r.start();
158   }
159
160   void
161   stop_counters(time_counter& t, resource_counter& r)
162   {
163     t.stop();
164     r.stop();
165   }
166
167   void
168   clear_counters(time_counter& t, resource_counter& r)
169   {
170     t.clear();
171     r.clear();
172   }
173
174   void
175   report_performance(const std::string file, const std::string comment, 
176                      const time_counter& t, const resource_counter& r)
177   {
178     const char space = ' ';
179     const char tab = '\t';
180     const char* name = "libstdc++-v3-performance.sum";
181     std::string::const_iterator i = file.begin() + file.find_last_of('/') + 1;
182     std::string testname(i, file.end());
183
184     std::ofstream out(name, std::ios_base::app);
185
186     out.setf(std::ios_base::left);
187     out << std::setw(25) << testname << tab;
188     out << std::setw(10) << comment << tab;
189
190     out.setf(std::ios_base::right);
191     out << std::setw(4) << t.real_time() << "r" << space;
192     out << std::setw(4) << t.user_time() << "u" << space;
193     out << std::setw(4) << t.system_time() << "s" << space;
194     out << std::setw(4) << r.allocated_memory() << "mem" << space;
195     out << std::setw(4) << r.hard_page_fault() << "pf" << space;
196     
197     out << std::endl;
198     out.close();
199   }
200 }; // namespace __gnu_test
201
202 #endif // _GLIBCXX_PERFORMANCE_H
203