OSDN Git Service

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