OSDN Git Service

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