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
);
176 SetupStat::setup(MySqlRun
*run
)
178 MySQL::Connection
&mysql
= run
->conn();
183 "stats(st_name, st_descr, st_type, st_print, st_prereq, "
184 "st_prec, st_nozero, st_nonan, st_total, st_pdf, st_cdf, "
185 "st_min, st_max, st_bktsize, st_size)"
186 "values(\"%s\",\"%s\",\"%s\","
187 " %d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d)",
188 name
, descr
, type
, print
, prereq
, (int)prec
, nozero
, nonan
,
190 min
, max
, bktsize
, size
);
194 int id
= mysql
.insert_id();
196 panic("could not commit transaction\n%s\n", mysql
.error
);
201 ccprintf(select
, "SELECT * FROM stats WHERE st_name=\"%s\"", name
);
204 MySQL::Result result
= mysql
.store_result();
206 panic("could not find stat\n%s\n", mysql
.error
);
208 assert(result
.num_fields() == 16);
209 MySQL::Row row
= result
.fetch_row();
211 panic("could not get stat row\n%s\n", mysql
.error
);
219 if (name
!= (char *)row
[1])
220 panic("failed stat check on %s:name. %s != %s\n",
223 if (descr
!= (char *)row
[2])
224 panic("failed stat check on %s:descr. %s != %s\n",
225 name
, descr
, row
[2]);
227 if (type
!= (char *)row
[3])
228 panic("failed stat check on %s:type. %s != %s\n",
231 if (!to_number(row
[4], tb
) || print
!= tb
)
232 panic("failed stat check on %s:print. %d != %d\n",
235 if (!to_number(row
[6], ti8
) || prec
!= ti8
)
236 panic("failed stat check on %s:prec. %d != %d\n",
239 if (!to_number(row
[7], tb
) || nozero
!= tb
)
240 panic("failed stat check on %s:nozero. %d != %d\n",
243 if (!to_number(row
[8], tb
) || nonan
!= tb
)
244 panic("failed stat check on %s:nonan. %d != %d\n",
247 if (!to_number(row
[9], tb
) || total
!= tb
)
248 panic("failed stat check on %s:total. %d != %d\n",
251 if (!to_number(row
[10], tb
) || pdf
!= tb
)
252 panic("failed stat check on %s:pdf. %d != %d\n",
255 if (!to_number(row
[11], tb
) || cdf
!= tb
)
256 panic("failed stat check on %s:cdf. %d != %d\n",
259 if (!to_number(row
[12], ti64
) || min
!= ti64
)
260 panic("failed stat check on %s:min. %d != %d\n",
263 if (!to_number(row
[13], ti64
) || max
!= ti64
)
264 panic("failed stat check on %s:max. %d != %d\n",
267 if (!to_number(row
[14], tu64
) || bktsize
!= tu64
)
268 panic("failed stat check on %s:bktsize. %d != %d\n",
269 name
, bktsize
, tu64
);
271 if (!to_number(row
[15], tu16
) || size
!= tu16
)
272 panic("failed stat check on %s:size. %d != %d\n",
275 to_number(row
[5], prereq
);
277 to_number(row
[0], statid
);
281 InsertData::InsertData(MySqlRun
*_run
)
284 query
= new char[maxsize
+ 1];
289 InsertData::~InsertData()
298 MySQL::Connection
&mysql
= run
->conn();
299 assert(mysql
.connected());
302 panic("could not insert data\n%s\n", mysql
.error
);
304 panic("could not commit transaction\n%s\n", mysql
.error
);
310 strcpy(query
, "INSERT INTO "
311 "data(dt_stat,dt_x,dt_y,dt_run,dt_tick,dt_data) "
313 size
= strlen(query
);
319 if (size
+ 1024 > maxsize
)
329 size
+= sprintf(query
+ size
, "(%u,%d,%d,%u,%llu,\"%f\")",
330 stat
, x
, y
, run
->run(), (unsigned long long)tick
,
342 void setup(MySqlRun
*run
);
346 InsertSubData::setup(MySqlRun
*run
)
348 MySQL::Connection
&mysql
= run
->conn();
349 assert(mysql
.connected());
352 "INSERT INTO subdata(sd_stat,sd_x,sd_y,sd_name,sd_descr) "
353 "values(%d,%d,%d,\"%s\",\"%s\")",
354 stat
, x
, y
, name
, descr
);
358 // panic("could not insert subdata\n%s\n", mysql.error);
361 panic("could not commit transaction\n%s\n", mysql
.error
);
365 : run(new MySqlRun
), newdata(run
)
374 MySql::connect(const string
&host
, const string
&user
, const string
&passwd
,
375 const string
&db
, const string
&name
, const string
&sample
,
376 const string
&project
)
378 run
->connect(host
, user
, passwd
, db
, name
, sample
, project
);
382 MySql::connected() const
384 return run
->connected();
393 MySQL::Connection
&mysql
= run
->conn();
395 list
<Info
*>::const_iterator i
, end
= statsList().end();
396 for (i
= statsList().begin(); i
!= end
; ++i
) {
400 for (i
= statsList().begin(); i
!= end
; ++i
) {
403 // update the prerequisite
404 uint16_t stat_id
= find(info
->id
);
405 uint16_t prereq_id
= find(info
->prereq
->id
);
406 assert(stat_id
&& prereq_id
);
409 ccprintf(update
, "UPDATE stats SET st_prereq=%d WHERE st_id=%d",
413 panic("could not update prereq\n%s\n", mysql
.error
);
416 panic("could not commit transaction\n%s\n", mysql
.error
);
421 panic("could not commit transaction\n%s\n", mysql
.error
);
427 MySql::configure(const Info
&info
, string type
)
430 stat
.name
= info
.name
;
431 stat
.descr
= info
.desc
;
433 stat
.print
= info
.flags
& display
;
434 stat
.prec
= info
.precision
;
435 stat
.nozero
= info
.flags
& nozero
;
436 stat
.nonan
= info
.flags
& nonan
;
437 stat
.total
= info
.flags
& total
;
438 stat
.pdf
= info
.flags
& pdf
;
439 stat
.cdf
= info
.flags
& cdf
;
445 MySql::configure(const ScalarInfo
&info
)
447 if (!configure(info
, "SCALAR"))
450 insert(info
.id
, stat
.setup(run
));
454 MySql::configure(const VectorInfo
&info
)
456 if (!configure(info
, "VECTOR"))
459 uint16_t statid
= stat
.setup(run
);
461 if (!info
.subnames
.empty()) {
462 InsertSubData subdata
;
463 subdata
.stat
= statid
;
465 for (off_type i
= 0; i
< info
.subnames
.size(); ++i
) {
467 subdata
.name
= info
.subnames
[i
];
468 subdata
.descr
= info
.subdescs
.empty() ? "" : info
.subdescs
[i
];
470 if (!subdata
.name
.empty() || !subdata
.descr
.empty())
475 insert(info
.id
, statid
);
479 MySql::configure(const DistInfo
&info
)
481 if (!configure(info
, "DIST"))
484 const DistParams
*params
=
485 safe_cast
<const DistParams
*>(info
.storageParams
);
486 if (params
->type
== Dist
) {
487 stat
.size
= params
->buckets
;
488 stat
.min
= params
->min
;
489 stat
.max
= params
->max
;
490 stat
.bktsize
= params
->bucket_size
;
492 insert(info
.id
, stat
.setup(run
));
496 MySql::configure(const VectorDistInfo
&info
)
498 if (!configure(info
, "VECTORDIST"))
501 const DistParams
*params
=
502 safe_cast
<const DistParams
*>(info
.storageParams
);
503 if (params
->type
== Dist
) {
504 stat
.size
= params
->buckets
;
505 stat
.min
= params
->min
;
506 stat
.max
= params
->max
;
507 stat
.bktsize
= params
->bucket_size
;
510 uint16_t statid
= stat
.setup(run
);
512 if (!info
.subnames
.empty()) {
513 InsertSubData subdata
;
514 subdata
.stat
= statid
;
516 for (off_type i
= 0; i
< info
.subnames
.size(); ++i
) {
518 subdata
.name
= info
.subnames
[i
];
519 subdata
.descr
= info
.subdescs
.empty() ? "" : info
.subdescs
[i
];
520 if (!subdata
.name
.empty() || !subdata
.descr
.empty())
525 insert(info
.id
, statid
);
529 MySql::configure(const Vector2dInfo
&info
)
531 if (!configure(info
, "VECTOR2D"))
534 uint16_t statid
= stat
.setup(run
);
536 if (!info
.subnames
.empty()) {
537 InsertSubData subdata
;
538 subdata
.stat
= statid
;
540 for (off_type i
= 0; i
< info
.subnames
.size(); ++i
) {
542 subdata
.name
= info
.subnames
[i
];
543 subdata
.descr
= info
.subdescs
.empty() ? "" : info
.subdescs
[i
];
544 if (!subdata
.name
.empty() || !subdata
.descr
.empty())
549 if (!info
.y_subnames
.empty()) {
550 InsertSubData subdata
;
551 subdata
.stat
= statid
;
554 for (off_type i
= 0; i
< info
.y_subnames
.size(); ++i
) {
556 subdata
.name
= info
.y_subnames
[i
];
557 if (!subdata
.name
.empty())
562 insert(info
.id
, statid
);
566 MySql::configure(const FormulaInfo
&info
)
568 MySQL::Connection
&mysql
= run
->conn();
569 assert(mysql
.connected());
571 configure(info
, "FORMULA");
572 insert(info
.id
, stat
.setup(run
));
574 uint16_t stat
= find(info
.id
);
575 string formula
= info
.str();
577 stringstream insert_formula
;
578 ccprintf(insert_formula
,
579 "INSERT INTO formulas(fm_stat,fm_formula) values(%d, \"%s\")",
582 mysql
.query(insert_formula
);
584 // panic("could not insert formula\n%s\n", mysql.error);
586 stringstream insert_ref
;
588 "INSERT INTO formula_ref(fr_stat,fr_run) values(%d, %d)",
591 mysql
.query(insert_ref
);
593 // panic("could not insert formula reference\n%s\n", mysql.error);
596 panic("could not commit transaction\n%s\n", mysql
.error
);
602 return run
->connected();
614 newdata
.tick
= curTick
;
616 MySQL::Connection
&mysql
= run
->conn();
618 list
<Info
*>::const_iterator i
, end
= statsList().end();
619 for (i
= statsList().begin(); i
!= end
; ++i
) {
623 panic("could not commit transaction\n%s\n", mysql
.error
);
630 MySql::output(const ScalarInfo
&info
)
632 if (!(info
.flags
& display
))
635 newdata
.stat
= find(info
.id
);
638 newdata
.data
= info
.value();
644 MySql::output(const VectorInfo
&info
)
646 if (!(info
.flags
& display
))
649 newdata
.stat
= find(info
.id
);
652 const VCounter
&cvec
= info
.value();
653 size_type size
= info
.size();
654 for (off_type x
= 0; x
< size
; x
++) {
656 newdata
.data
= cvec
[x
];
662 MySql::output(const DistData
&data
, const DistParams
*params
)
664 const int db_sum
= -1;
665 const int db_squares
= -2;
666 const int db_samples
= -3;
667 const int db_min_val
= -4;
668 const int db_max_val
= -5;
669 const int db_underflow
= -6;
670 const int db_overflow
= -7;
673 newdata
.data
= data
.sum
;
676 newdata
.x
= db_squares
;
677 newdata
.data
= data
.squares
;
680 newdata
.x
= db_samples
;
681 newdata
.data
= data
.samples
;
684 if (data
.samples
&& params
->type
== Dist
) {
685 newdata
.x
= db_min_val
;
686 newdata
.data
= data
.min_val
;
689 newdata
.x
= db_max_val
;
690 newdata
.data
= data
.max_val
;
693 newdata
.x
= db_underflow
;
694 newdata
.data
= data
.underflow
;
697 newdata
.x
= db_overflow
;
698 newdata
.data
= data
.overflow
;
701 size_type size
= data
.cvec
.size();
702 for (off_type x
= 0; x
< size
; x
++) {
704 newdata
.data
= data
.cvec
[x
];
711 MySql::output(const DistInfo
&info
)
713 if (!(info
.flags
& display
))
716 newdata
.stat
= find(info
.id
);
718 output(info
.data
, safe_cast
<const DistParams
*>(info
.storageParams
));
722 MySql::output(const VectorDistInfo
&info
)
724 if (!(info
.flags
& display
))
727 newdata
.stat
= find(info
.id
);
729 size_type size
= info
.data
.size();
730 for (off_type y
= 0; y
< size
; ++y
) {
733 safe_cast
<const DistParams
*>(info
.storageParams
));
738 MySql::output(const Vector2dInfo
&info
)
740 if (!(info
.flags
& display
))
743 newdata
.stat
= find(info
.id
);
746 for (off_type x
= 0; x
< info
.x
; x
++) {
748 for (off_type y
= 0; y
< info
.y
; y
++) {
750 newdata
.data
= info
.cvec
[index
++];
757 MySql::output(const FormulaInfo
&info
)
762 * Implement the visitor
765 MySql::visit(const ScalarInfo
&info
)
774 MySql::visit(const VectorInfo
&info
)
783 MySql::visit(const DistInfo
&info
)
793 MySql::visit(const VectorDistInfo
&info
)
803 MySql::visit(const Vector2dInfo
&info
)
813 MySql::visit(const FormulaInfo
&info
)
822 initMySQL(string host
, string user
, string password
, string database
,
823 string project
, string name
, string sample
)
825 extern list
<Output
*> OutputList
;
828 if (mysql
.connected())
831 mysql
.connect(host
, user
, password
, database
, name
, sample
, project
);
832 OutputList
.push_back(&mysql
);
837 /* end namespace Stats */ }