a09ee095c74cc7890e9f7aa33540355735a0fb27
[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 DistInfo;
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
60 double min;
61 double max;
62 double bktsize;
63 uint16_t size;
64
65 void init();
66 unsigned setup(MySqlRun *run);
67 };
68
69 class InsertData
70 {
71 private:
72 char *query;
73 size_type size;
74 bool first;
75 static const size_type maxsize = 1024*1024;
76
77 public:
78 MySqlRun *run;
79
80 public:
81 uint64_t tick;
82 double data;
83 uint16_t stat;
84 int16_t x;
85 int16_t y;
86
87 public:
88 InsertData(MySqlRun *_run);
89 ~InsertData();
90
91 void flush();
92 void insert();
93 };
94
95 class MySql : public Output
96 {
97 protected:
98 MySqlRun *run; /* Hide the implementation so we don't have a
99 #include mess */
100
101 SetupStat stat;
102 InsertData newdata;
103 std::list<FormulaInfo *> formulas;
104 bool configured;
105
106 protected:
107 std::map<int, int> idmap;
108
109 void
110 insert(int sim_id, int db_id)
111 {
112 using namespace std;
113 idmap.insert(make_pair(sim_id, db_id));
114 }
115
116 int
117 find(int sim_id)
118 {
119 using namespace std;
120 map<int,int>::const_iterator i = idmap.find(sim_id);
121 assert(i != idmap.end());
122 return (*i).second;
123 }
124
125 public:
126 MySql();
127 ~MySql();
128
129 void connect(const std::string &host, const std::string &user,
130 const std::string &passwd, const std::string &db,
131 const std::string &name, const std::string &sample,
132 const std::string &project);
133 bool connected() const;
134
135 public:
136 // Implement Visit
137 virtual void visit(const ScalarInfo &info);
138 virtual void visit(const VectorInfo &info);
139 virtual void visit(const DistInfo &info);
140 virtual void visit(const VectorDistInfo &info);
141 virtual void visit(const Vector2dInfo &info);
142 virtual void visit(const FormulaInfo &info);
143
144 // Implement Output
145 virtual bool valid() const;
146 virtual void output();
147
148 protected:
149 // Output helper
150 void output(const ScalarInfo &info);
151 void output(const VectorInfo &info);
152 void output(const DistInfo &info);
153 void output(const VectorDistInfo &info);
154 void output(const Vector2dInfo &info);
155 void output(const FormulaInfo &info);
156 void output(const DistData &data, const DistParams *params);
157
158 void configure();
159 bool configure(const Info &info, std::string type);
160 void configure(const ScalarInfo &info);
161 void configure(const VectorInfo &info);
162 void configure(const DistInfo &info);
163 void configure(const VectorDistInfo &info);
164 void configure(const Vector2dInfo &info);
165 void configure(const FormulaInfo &info);
166 };
167
168 bool initMySQL(std::string host, std::string database, std::string user,
169 std::string passwd, std::string project, std::string name,
170 std::string sample);
171
172 #if !USE_MYSQL
173 inline bool
174 initMySQL(std::string host, std::string user, std::string password,
175 std::string database, std::string project, std::string name,
176 std::string sample)
177 {
178 return false;
179 }
180 #endif
181
182 } // namespace Stats
183
184 #endif // __BASE_STATS_MYSQL_HH__