Merge ktlim@zizzer:/bk/m5
[gem5.git] / src / base / stats / events.cc
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
29 #include <vector>
30
31 #include "base/stats/events.hh"
32
33 #if USE_MYSQL
34 #include "base/cprintf.hh"
35 #include "base/misc.hh"
36 #include "base/mysql.hh"
37 #include "base/stats/mysql.hh"
38 #include "base/stats/mysql_run.hh"
39 #include "base/str.hh"
40 #endif
41
42 #include "base/match.hh"
43 #include "sim/host.hh"
44 #include "sim/sim_object.hh"
45 #include "sim/root.hh"
46
47 using namespace std;
48
49 namespace Stats {
50
51 Tick EventStart = ULL(0x7fffffffffffffff);
52
53 ObjectMatch event_ignore;
54
55 #if USE_MYSQL
56 class InsertEvent
57 {
58 private:
59 char *query;
60 int size;
61 bool first;
62 static const int maxsize = 1024*1024;
63
64 typedef map<string, uint32_t> event_map_t;
65 event_map_t events;
66
67 MySQL::Connection &mysql;
68 uint16_t run;
69
70 public:
71 InsertEvent()
72 : mysql(MySqlDB.conn()), run(MySqlDB.run())
73 {
74 query = new char[maxsize + 1];
75 size = 0;
76 first = true;
77 flush();
78 }
79 ~InsertEvent()
80 {
81 flush();
82 }
83
84 void flush();
85 void insert(const string &stat);
86 };
87
88 void
89 InsertEvent::insert(const string &stat)
90 {
91 assert(mysql.connected());
92
93 event_map_t::iterator i = events.find(stat);
94 uint32_t event;
95 if (i == events.end()) {
96 mysql.query(
97 csprintf("SELECT en_id "
98 "from event_names "
99 "where en_name=\"%s\"",
100 stat));
101
102 MySQL::Result result = mysql.store_result();
103 if (!result)
104 panic("could not get a run\n%s\n", mysql.error);
105
106 assert(result.num_fields() == 1);
107 MySQL::Row row = result.fetch_row();
108 if (row) {
109 if (!to_number(row[0], event))
110 panic("invalid event id: %s\n", row[0]);
111 } else {
112 mysql.query(
113 csprintf("INSERT INTO "
114 "event_names(en_name)"
115 "values(\"%s\")",
116 stat));
117
118 if (mysql.error)
119 panic("could not get a run\n%s\n", mysql.error);
120
121 event = mysql.insert_id();
122 }
123 } else {
124 event = (*i).second;
125 }
126
127 if (size + 1024 > maxsize)
128 flush();
129
130 if (!first) {
131 query[size++] = ',';
132 query[size] = '\0';
133 }
134
135 first = false;
136
137 size += sprintf(query + size, "(%u,%u,%llu)",
138 event, run, (unsigned long long)curTick);
139 }
140
141 void
142 InsertEvent::flush()
143 {
144 static const char query_header[] = "INSERT INTO "
145 "events(ev_event, ev_run, ev_tick)"
146 "values";
147
148 if (size) {
149 MySQL::Connection &mysql = MySqlDB.conn();
150 assert(mysql.connected());
151 mysql.query(query);
152 }
153
154 query[0] = '\0';
155 size = sizeof(query_header);
156 first = true;
157 memcpy(query, query_header, size);
158 }
159
160 void
161 __event(const string &stat)
162 {
163 static InsertEvent event;
164 event.insert(stat);
165 }
166
167 #endif
168
169 /* namespace Stats */ }