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