2 * Copyright (c) 2004-2005 The Regents of The University of Michigan
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.
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.
28 * Authors: Nathan Binkert
38 #include "base/misc.hh"
39 #include "base/mysql.hh"
40 #include "base/statistics.hh"
41 #include "base/stats/info.hh"
42 #include "base/stats/mysql.hh"
43 #include "base/stats/mysql_run.hh"
44 #include "base/stats/types.hh"
45 #include "base/str.hh"
46 #include "base/types.hh"
47 #include "base/userinfo.hh"
54 MySqlRun::connect(const string
&host
, const string
&user
, const string
&passwd
,
55 const string
&db
, const string
&name
, const string
&sample
,
56 const string
&project
)
59 panic("can only get one database connection at this time!");
61 mysql
.connect(host
, user
, passwd
, db
);
63 panic("could not connect to database server\n%s\n", mysql
.error
);
65 if (mysql
.autocommit(false))
66 panic("could not set autocommit\n%s\n", mysql
.error
);
70 setup(name
, sample
, user
, project
);
74 MySqlRun::setup(const string
&name
, const string
&sample
, const string
&user
,
75 const string
&project
)
77 assert(mysql
.connected());
82 "runs(rn_name,rn_sample,rn_user,rn_project,rn_date,rn_expire)"
83 "values(\"%s\", \"%s\", \"%s\", \"%s\", NOW(),"
84 "DATE_ADD(CURDATE(), INTERVAL 31 DAY))",
85 name
, sample
, user
, project
);
89 panic("could not get a run\n%s\n", mysql
.error
);
91 run_id
= mysql
.insert_id();
93 panic("could not commit transaction\n%s\n", mysql
.error
);
97 MySqlRun::remove(const string
&name
)
99 assert(mysql
.connected());
101 ccprintf(sql
, "DELETE FROM runs WHERE rn_name=\"%s\"", name
);
104 panic("could not delete run\n%s\n", mysql
.error
);
106 panic("could not commit transaction\n%s\n", mysql
.error
);
112 assert(mysql
.connected());
114 mysql
.query("DELETE data "
116 "LEFT JOIN runs ON dt_run=rn_id "
117 "WHERE rn_id IS NULL");
120 panic("could not commit transaction\n%s\n", mysql
.error
);
122 mysql
.query("DELETE formula_ref "
124 "LEFT JOIN runs ON fr_run=rn_id "
125 "WHERE rn_id IS NULL");
128 panic("could not commit transaction\n%s\n", mysql
.error
);
130 mysql
.query("DELETE formulas "
132 "LEFT JOIN formula_ref ON fm_stat=fr_stat "
133 "WHERE fr_stat IS NULL");
136 panic("could not commit transaction\n%s\n", mysql
.error
);
138 mysql
.query("DELETE stats "
140 "LEFT JOIN data ON st_id=dt_stat "
141 "WHERE dt_stat IS NULL");
144 panic("could not commit transaction\n%s\n", mysql
.error
);
146 mysql
.query("DELETE subdata "
148 "LEFT JOIN data ON sd_stat=dt_stat "
149 "WHERE dt_stat IS NULL");
152 panic("could not commit transaction\n%s\n", mysql
.error
);
154 mysql
.query("DELETE events"
156 "LEFT JOIN runs ON ev_run=rn_id"
157 "WHERE rn_id IS NULL");
160 panic("could not commit transaction\n%s\n", mysql
.error
);
162 mysql
.query("DELETE event_names"
164 "LEFT JOIN events ON en_id=ev_event"
165 "WHERE ev_event IS NULL");
168 panic("could not commit transaction\n%s\n", mysql
.error
);
192 SetupStat::setup(MySqlRun
*run
)
194 MySQL::Connection
&mysql
= run
->conn();
199 "stats(st_name, st_descr, st_type, st_print, st_prereq, "
200 "st_prec, st_nozero, st_nonan, st_total, st_pdf, st_cdf, "
201 "st_min, st_max, st_bktsize, st_size)"
202 "values(\"%s\",\"%s\",\"%s\","
203 " %d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d)",
204 name
, descr
, type
, print
, prereq
, (int)prec
, nozero
, nonan
,
206 min
, max
, bktsize
, size
);
210 int id
= mysql
.insert_id();
212 panic("could not commit transaction\n%s\n", mysql
.error
);
217 ccprintf(select
, "SELECT * FROM stats WHERE st_name=\"%s\"", name
);
220 MySQL::Result result
= mysql
.store_result();
222 panic("could not find stat\n%s\n", mysql
.error
);
224 assert(result
.num_fields() == 16);
225 MySQL::Row row
= result
.fetch_row();
227 panic("could not get stat row\n%s\n", mysql
.error
);
235 if (name
!= (char *)row
[1])
236 panic("failed stat check on %s:name. %s != %s\n",
239 if (descr
!= (char *)row
[2])
240 panic("failed stat check on %s:descr. %s != %s\n",
241 name
, descr
, row
[2]);
243 if (type
!= (char *)row
[3])
244 panic("failed stat check on %s:type. %s != %s\n",
247 if (!to_number(row
[4], tb
) || print
!= tb
)
248 panic("failed stat check on %s:print. %d != %d\n",
251 if (!to_number(row
[6], ti8
) || prec
!= ti8
)
252 panic("failed stat check on %s:prec. %d != %d\n",
255 if (!to_number(row
[7], tb
) || nozero
!= tb
)
256 panic("failed stat check on %s:nozero. %d != %d\n",
259 if (!to_number(row
[8], tb
) || nonan
!= tb
)
260 panic("failed stat check on %s:nonan. %d != %d\n",
263 if (!to_number(row
[9], tb
) || total
!= tb
)
264 panic("failed stat check on %s:total. %d != %d\n",
267 if (!to_number(row
[10], tb
) || pdf
!= tb
)
268 panic("failed stat check on %s:pdf. %d != %d\n",
271 if (!to_number(row
[11], tb
) || cdf
!= tb
)
272 panic("failed stat check on %s:cdf. %d != %d\n",
275 if (!to_number(row
[12], ti64
) || min
!= ti64
)
276 panic("failed stat check on %s:min. %d != %d\n",
279 if (!to_number(row
[13], ti64
) || max
!= ti64
)
280 panic("failed stat check on %s:max. %d != %d\n",
283 if (!to_number(row
[14], tu64
) || bktsize
!= tu64
)
284 panic("failed stat check on %s:bktsize. %d != %d\n",
285 name
, bktsize
, tu64
);
287 if (!to_number(row
[15], tu16
) || size
!= tu16
)
288 panic("failed stat check on %s:size. %d != %d\n",
291 to_number(row
[5], prereq
);
293 to_number(row
[0], statid
);
297 InsertData::InsertData(MySqlRun
*_run
)
300 query
= new char[maxsize
+ 1];
305 InsertData::~InsertData()
314 MySQL::Connection
&mysql
= run
->conn();
315 assert(mysql
.connected());
318 panic("could not insert data\n%s\n", mysql
.error
);
320 panic("could not commit transaction\n%s\n", mysql
.error
);
326 strcpy(query
, "INSERT INTO "
327 "data(dt_stat,dt_x,dt_y,dt_run,dt_tick,dt_data) "
329 size
= strlen(query
);
335 if (size
+ 1024 > maxsize
)
345 size
+= sprintf(query
+ size
, "(%u,%d,%d,%u,%llu,\"%f\")",
346 stat
, x
, y
, run
->run(), (unsigned long long)tick
,
350 InsertEvent::InsertEvent(MySqlRun
*_run
)
353 query
= new char[maxsize
+ 1];
359 InsertEvent::~InsertEvent()
365 InsertEvent::insert(const string
&stat
)
367 MySQL::Connection
&mysql
= run
->conn();
368 assert(mysql
.connected());
370 event_map_t::iterator i
= events
.find(stat
);
372 if (i
== events
.end()) {
374 csprintf("SELECT en_id "
376 "where en_name=\"%s\"",
379 MySQL::Result result
= mysql
.store_result();
381 panic("could not get a run\n%s\n", mysql
.error
);
383 assert(result
.num_fields() == 1);
384 MySQL::Row row
= result
.fetch_row();
386 if (!to_number(row
[0], event
))
387 panic("invalid event id: %s\n", row
[0]);
390 csprintf("INSERT INTO "
391 "event_names(en_name)"
396 panic("could not get a run\n%s\n", mysql
.error
);
398 event
= mysql
.insert_id();
404 if (size
+ 1024 > maxsize
)
414 size
+= sprintf(query
+ size
, "(%u,%u,%llu)",
415 event
, run
->run(), (unsigned long long)curTick
);
421 static const char query_header
[] = "INSERT INTO "
422 "events(ev_event, ev_run, ev_tick)"
425 MySQL::Connection
&mysql
= run
->conn();
426 assert(mysql
.connected());
432 size
= sizeof(query_header
);
434 memcpy(query
, query_header
, size
);
445 void setup(MySqlRun
*run
);
449 InsertSubData::setup(MySqlRun
*run
)
451 MySQL::Connection
&mysql
= run
->conn();
452 assert(mysql
.connected());
455 "INSERT INTO subdata(sd_stat,sd_x,sd_y,sd_name,sd_descr) "
456 "values(%d,%d,%d,\"%s\",\"%s\")",
457 stat
, x
, y
, name
, descr
);
461 // panic("could not insert subdata\n%s\n", mysql.error);
464 panic("could not commit transaction\n%s\n", mysql
.error
);
468 : run(new MySqlRun
), newdata(run
), newevent(run
)
477 MySql::connect(const string
&host
, const string
&user
, const string
&passwd
,
478 const string
&db
, const string
&name
, const string
&sample
,
479 const string
&project
)
481 run
->connect(host
, user
, passwd
, db
, name
, sample
, project
);
485 MySql::connected() const
487 return run
->connected();
496 MySQL::Connection
&mysql
= run
->conn();
498 list
<Info
*>::const_iterator i
, end
= statsList().end();
499 for (i
= statsList().begin(); i
!= end
; ++i
) {
503 for (i
= statsList().begin(); i
!= end
; ++i
) {
506 // update the prerequisite
507 uint16_t stat_id
= find(info
->id
);
508 uint16_t prereq_id
= find(info
->prereq
->id
);
509 assert(stat_id
&& prereq_id
);
512 ccprintf(update
, "UPDATE stats SET st_prereq=%d WHERE st_id=%d",
516 panic("could not update prereq\n%s\n", mysql
.error
);
519 panic("could not commit transaction\n%s\n", mysql
.error
);
524 panic("could not commit transaction\n%s\n", mysql
.error
);
530 MySql::configure(const Info
&info
, string type
)
533 stat
.name
= info
.name
;
534 stat
.descr
= info
.desc
;
536 stat
.print
= info
.flags
& print
;
537 stat
.prec
= info
.precision
;
538 stat
.nozero
= info
.flags
& nozero
;
539 stat
.nonan
= info
.flags
& nonan
;
540 stat
.total
= info
.flags
& total
;
541 stat
.pdf
= info
.flags
& pdf
;
542 stat
.cdf
= info
.flags
& cdf
;
548 MySql::configure(const ScalarInfo
&info
)
550 if (!configure(info
, "SCALAR"))
553 insert(info
.id
, stat
.setup(run
));
557 MySql::configure(const VectorInfo
&info
)
559 if (!configure(info
, "VECTOR"))
562 uint16_t statid
= stat
.setup(run
);
564 if (!info
.subnames
.empty()) {
565 InsertSubData subdata
;
566 subdata
.stat
= statid
;
568 for (off_type i
= 0; i
< info
.subnames
.size(); ++i
) {
570 subdata
.name
= info
.subnames
[i
];
571 subdata
.descr
= info
.subdescs
.empty() ? "" : info
.subdescs
[i
];
573 if (!subdata
.name
.empty() || !subdata
.descr
.empty())
578 insert(info
.id
, statid
);
582 MySql::configure(const DistInfo
&info
)
584 if (!configure(info
, "DIST"))
587 const DistParams
*params
=
588 safe_cast
<const DistParams
*>(info
.storageParams
);
589 if (params
->type
== Dist
) {
590 stat
.size
= params
->buckets
;
591 stat
.min
= params
->min
;
592 stat
.max
= params
->max
;
593 stat
.bktsize
= params
->bucket_size
;
595 insert(info
.id
, stat
.setup(run
));
599 MySql::configure(const VectorDistInfo
&info
)
601 if (!configure(info
, "VECTORDIST"))
604 const DistParams
*params
=
605 safe_cast
<const DistParams
*>(info
.storageParams
);
606 if (params
->type
== Dist
) {
607 stat
.size
= params
->buckets
;
608 stat
.min
= params
->min
;
609 stat
.max
= params
->max
;
610 stat
.bktsize
= params
->bucket_size
;
613 uint16_t statid
= stat
.setup(run
);
615 if (!info
.subnames
.empty()) {
616 InsertSubData subdata
;
617 subdata
.stat
= statid
;
619 for (off_type i
= 0; i
< info
.subnames
.size(); ++i
) {
621 subdata
.name
= info
.subnames
[i
];
622 subdata
.descr
= info
.subdescs
.empty() ? "" : info
.subdescs
[i
];
623 if (!subdata
.name
.empty() || !subdata
.descr
.empty())
628 insert(info
.id
, statid
);
632 MySql::configure(const Vector2dInfo
&info
)
634 if (!configure(info
, "VECTOR2D"))
637 uint16_t statid
= stat
.setup(run
);
639 if (!info
.subnames
.empty()) {
640 InsertSubData subdata
;
641 subdata
.stat
= statid
;
643 for (off_type i
= 0; i
< info
.subnames
.size(); ++i
) {
645 subdata
.name
= info
.subnames
[i
];
646 subdata
.descr
= info
.subdescs
.empty() ? "" : info
.subdescs
[i
];
647 if (!subdata
.name
.empty() || !subdata
.descr
.empty())
652 if (!info
.y_subnames
.empty()) {
653 InsertSubData subdata
;
654 subdata
.stat
= statid
;
657 for (off_type i
= 0; i
< info
.y_subnames
.size(); ++i
) {
659 subdata
.name
= info
.y_subnames
[i
];
660 if (!subdata
.name
.empty())
665 insert(info
.id
, statid
);
669 MySql::configure(const FormulaInfo
&info
)
671 MySQL::Connection
&mysql
= run
->conn();
672 assert(mysql
.connected());
674 configure(info
, "FORMULA");
675 insert(info
.id
, stat
.setup(run
));
677 uint16_t stat
= find(info
.id
);
678 string formula
= info
.str();
680 stringstream insert_formula
;
681 ccprintf(insert_formula
,
682 "INSERT INTO formulas(fm_stat,fm_formula) values(%d, \"%s\")",
685 mysql
.query(insert_formula
);
687 // panic("could not insert formula\n%s\n", mysql.error);
689 stringstream insert_ref
;
691 "INSERT INTO formula_ref(fr_stat,fr_run) values(%d, %d)",
694 mysql
.query(insert_ref
);
696 // panic("could not insert formula reference\n%s\n", mysql.error);
699 panic("could not commit transaction\n%s\n", mysql
.error
);
705 return run
->connected();
717 newdata
.tick
= curTick
;
719 MySQL::Connection
&mysql
= run
->conn();
721 list
<Info
*>::const_iterator i
, end
= statsList().end();
722 for (i
= statsList().begin(); i
!= end
; ++i
) {
726 panic("could not commit transaction\n%s\n", mysql
.error
);
733 MySql::event(const std::string
&event
)
735 newevent
.insert(event
);
739 MySql::output(const ScalarInfo
&info
)
741 if (!(info
.flags
& print
))
744 newdata
.stat
= find(info
.id
);
747 newdata
.data
= info
.value();
753 MySql::output(const VectorInfo
&info
)
755 if (!(info
.flags
& print
))
758 newdata
.stat
= find(info
.id
);
761 const VCounter
&cvec
= info
.value();
762 size_type size
= info
.size();
763 for (off_type x
= 0; x
< size
; x
++) {
765 newdata
.data
= cvec
[x
];
771 MySql::output(const DistData
&data
, const DistParams
*params
)
773 const int db_sum
= -1;
774 const int db_squares
= -2;
775 const int db_samples
= -3;
776 const int db_min_val
= -4;
777 const int db_max_val
= -5;
778 const int db_underflow
= -6;
779 const int db_overflow
= -7;
782 newdata
.data
= data
.sum
;
785 newdata
.x
= db_squares
;
786 newdata
.data
= data
.squares
;
789 newdata
.x
= db_samples
;
790 newdata
.data
= data
.samples
;
793 if (data
.samples
&& params
->type
== Dist
) {
794 newdata
.x
= db_min_val
;
795 newdata
.data
= data
.min_val
;
798 newdata
.x
= db_max_val
;
799 newdata
.data
= data
.max_val
;
802 newdata
.x
= db_underflow
;
803 newdata
.data
= data
.underflow
;
806 newdata
.x
= db_overflow
;
807 newdata
.data
= data
.overflow
;
810 size_type size
= data
.cvec
.size();
811 for (off_type x
= 0; x
< size
; x
++) {
813 newdata
.data
= data
.cvec
[x
];
820 MySql::output(const DistInfo
&info
)
822 if (!(info
.flags
& print
))
825 newdata
.stat
= find(info
.id
);
827 output(info
.data
, safe_cast
<const DistParams
*>(info
.storageParams
));
831 MySql::output(const VectorDistInfo
&info
)
833 if (!(info
.flags
& print
))
836 newdata
.stat
= find(info
.id
);
838 size_type size
= info
.data
.size();
839 for (off_type y
= 0; y
< size
; ++y
) {
842 safe_cast
<const DistParams
*>(info
.storageParams
));
847 MySql::output(const Vector2dInfo
&info
)
849 if (!(info
.flags
& print
))
852 newdata
.stat
= find(info
.id
);
855 for (off_type x
= 0; x
< info
.x
; x
++) {
857 for (off_type y
= 0; y
< info
.y
; y
++) {
859 newdata
.data
= info
.cvec
[index
++];
866 MySql::output(const FormulaInfo
&info
)
871 * Implement the visitor
874 MySql::visit(const ScalarInfo
&info
)
883 MySql::visit(const VectorInfo
&info
)
892 MySql::visit(const DistInfo
&info
)
902 MySql::visit(const VectorDistInfo
&info
)
912 MySql::visit(const Vector2dInfo
&info
)
922 MySql::visit(const FormulaInfo
&info
)
931 initMySQL(string host
, string user
, string password
, string database
,
932 string project
, string name
, string sample
)
934 extern list
<Output
*> OutputList
;
937 if (mysql
.connected())
940 mysql
.connect(host
, user
, password
, database
, name
, sample
, project
);
941 OutputList
.push_back(&mysql
);
946 /* end namespace Stats */ }