Merge with head.
[gem5.git] / src / base / stats / mysql.hh
1 /*
2 * Copyright (c) 2004-2005 The Regents of The University of Michigan
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;
9 * redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution;
12 * neither the name of the copyright holders nor the names of its
13 * contributors may be used to endorse or promote products derived from
14 * this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 *
28 * Authors: Nathan Binkert
29 */
30
31 #ifndef __BASE_STATS_MYSQL_HH__
32 #define __BASE_STATS_MYSQL_HH__
33
34 #include <map>
35 #include <string>
36
37 #include "base/stats/output.hh"
38 #include "config/use_mysql.hh"
39
40 namespace MySQL { class Connection; }
41 namespace Stats {
42
43 class DistDataData;
44 class MySqlRun;
45
46 struct SetupStat
47 {
48 std::string name;
49 std::string descr;
50 std::string type;
51 bool print;
52 uint16_t prereq;
53 int8_t prec;
54 bool nozero;
55 bool nonan;
56 bool total;
57 bool pdf;
58 bool cdf;
59 double min;
60 double max;
61 double bktsize;
62 uint16_t size;
63
64 void init();
65 unsigned setup(MySqlRun *run);
66 };
67
68 class InsertData
69 {
70 private:
71 char *query;
72 int size;
73 bool first;
74 static const int maxsize = 1024*1024;
75
76 public:
77 MySqlRun *run;
78
79 public:
80 uint64_t tick;
81 double data;
82 uint16_t stat;
83 int16_t x;
84 int16_t y;
85
86 public:
87 InsertData(MySqlRun *_run);
88 ~InsertData();
89
90 void flush();
91 void insert();
92 };
93
94 class InsertEvent
95 {
96 private:
97 char *query;
98 int size;
99 bool first;
100 static const int maxsize = 1024*1024;
101
102 typedef std::map<std::string, uint32_t> event_map_t;
103 event_map_t events;
104
105 MySqlRun *run;
106
107 public:
108 InsertEvent(MySqlRun *_run);
109 ~InsertEvent();
110
111 void flush();
112 void insert(const std::string &stat);
113 };
114
115 class MySql : public Output
116 {
117 protected:
118 MySqlRun *run; /* Hide the implementation so we don't have a
119 #include mess */
120
121 SetupStat stat;
122 InsertData newdata;
123 InsertEvent newevent;
124 std::list<FormulaData *> formulas;
125 bool configured;
126
127 protected:
128 std::map<int, int> idmap;
129
130 void insert(int sim_id, int db_id)
131 {
132 using namespace std;
133 idmap.insert(make_pair(sim_id, db_id));
134 }
135
136 int find(int sim_id)
137 {
138 using namespace std;
139 map<int,int>::const_iterator i = idmap.find(sim_id);
140 assert(i != idmap.end());
141 return (*i).second;
142 }
143
144 public:
145 MySql();
146 ~MySql();
147
148 void connect(const std::string &host, const std::string &user,
149 const std::string &passwd, const std::string &db,
150 const std::string &name, const std::string &sample,
151 const std::string &project);
152 bool connected() const;
153
154 public:
155 // Implement Visit
156 virtual void visit(const ScalarData &data);
157 virtual void visit(const VectorData &data);
158 virtual void visit(const DistData &data);
159 virtual void visit(const VectorDistData &data);
160 virtual void visit(const Vector2dData &data);
161 virtual void visit(const FormulaData &data);
162
163 // Implement Output
164 virtual bool valid() const;
165 virtual void output();
166
167 // Implement Event Output
168 virtual void event(const std::string &event);
169
170 protected:
171 // Output helper
172 void output(const DistDataData &data);
173 void output(const ScalarData &data);
174 void output(const VectorData &data);
175 void output(const DistData &data);
176 void output(const VectorDistData &data);
177 void output(const Vector2dData &data);
178 void output(const FormulaData &data);
179
180 void configure();
181 bool configure(const StatData &data, std::string type);
182 void configure(const ScalarData &data);
183 void configure(const VectorData &data);
184 void configure(const DistData &data);
185 void configure(const VectorDistData &data);
186 void configure(const Vector2dData &data);
187 void configure(const FormulaData &data);
188 };
189
190 bool initMySQL(std::string host, std::string database, std::string user,
191 std::string passwd, std::string project, std::string name,
192 std::string sample);
193
194 #if !USE_MYSQL
195 inline bool
196 initMySQL(std::string host, std::string user, std::string password,
197 std::string database, std::string project, std::string name,
198 std::string sample)
199 {
200 return false;
201 }
202 #endif
203
204 /* namespace Stats */ }
205
206 #endif // __BASE_STATS_MYSQL_HH__