Files
unison/src/stats/model/sqlite-data-output.cc

277 lines
8.3 KiB
C++
Raw Normal View History

2008-08-29 13:22:09 -04:00
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
* Copyright (c) 2008 Drexel University
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation;
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* Author: Joe Kopena (tjkopena@cs.drexel.edu)
*/
#include <sstream>
#include <sqlite3.h>
#include "ns3/log.h"
#include "ns3/nstime.h"
#include "data-collector.h"
#include "data-calculator.h"
#include "sqlite-data-output.h"
using namespace ns3;
NS_LOG_COMPONENT_DEFINE ("SqliteDataOutput");
2008-08-29 13:22:09 -04:00
//--------------------------------------------------------------
//----------------------------------------------
2009-06-24 10:43:52 -04:00
SqliteDataOutput::SqliteDataOutput()
2008-08-29 13:22:09 -04:00
{
NS_LOG_FUNCTION (this);
2009-06-24 10:43:52 -04:00
m_filePrefix = "data";
2008-08-29 13:22:09 -04:00
}
SqliteDataOutput::~SqliteDataOutput()
{
NS_LOG_FUNCTION (this);
2008-08-29 13:22:09 -04:00
}
/* static */
TypeId
SqliteDataOutput::GetTypeId (void)
{
static TypeId tid = TypeId ("ns3::SqliteDataOutput")
.SetParent<DataOutputInterface> ()
.SetGroupName ("Stats")
.AddConstructor<SqliteDataOutput> ();
return tid;
}
2008-08-29 13:22:09 -04:00
void
SqliteDataOutput::DoDispose ()
2008-08-29 13:22:09 -04:00
{
NS_LOG_FUNCTION (this);
2008-08-29 13:22:09 -04:00
DataOutputInterface::DoDispose ();
2008-08-29 13:22:09 -04:00
// end SqliteDataOutput::DoDispose
}
int
SqliteDataOutput::Exec (std::string exe) {
NS_LOG_FUNCTION (this << exe);
2008-08-29 13:22:09 -04:00
int res;
char **result;
int nrows, ncols;
char *errMsg = 0;
NS_LOG_INFO ("executing '" << exe << "'");
2008-08-29 13:22:09 -04:00
res = sqlite3_get_table (m_db,
exe.c_str (),
&result, &nrows, &ncols,
&errMsg);
2008-08-29 13:22:09 -04:00
if (res != SQLITE_OK) {
NS_LOG_ERROR ("sqlite3 error: \"" << errMsg << "\"");
2011-05-13 14:59:56 -04:00
/*
} else {
// std::cout << "nrows " << nrows << " ncols " << ncols << std::endl;
if (nrows > 0) {
for (int i = 0; i < ncols; i++) {
std::cout << " " << result[i];
}
std::cout << std::endl;
for (int r = 1; r <= nrows; r++) {
for (int c = 0; c < ncols; c++) {
std::cout << " " << result[(r*ncols)+c];
}
std::cout << std::endl;
}
std::cout << std::endl;
2008-08-29 13:22:09 -04:00
}
2011-05-13 14:59:56 -04:00
*/
2008-08-29 13:22:09 -04:00
}
sqlite3_free_table (result);
2008-08-29 13:22:09 -04:00
return res;
// end SqliteDataOutput::Exec
}
//----------------------------------------------
void
SqliteDataOutput::Output (DataCollector &dc)
2008-08-29 13:22:09 -04:00
{
NS_LOG_FUNCTION (this << &dc);
2009-06-24 10:43:52 -04:00
std::string m_dbFile = m_filePrefix + ".db";
2008-08-29 13:22:09 -04:00
if (sqlite3_open (m_dbFile.c_str (), &m_db)) {
NS_LOG_ERROR ("Could not open sqlite3 database \"" << m_dbFile << "\"");
NS_LOG_ERROR ("sqlite3 error \"" << sqlite3_errmsg (m_db) << "\"");
sqlite3_close (m_db);
2013-06-11 17:02:23 -07:00
/// \todo Better error reporting, management!
2011-05-13 14:59:56 -04:00
return;
}
2008-08-29 13:22:09 -04:00
std::string run = dc.GetRunLabel ();
2008-08-29 13:22:09 -04:00
Exec ("create table if not exists Experiments (run, experiment, strategy, input, description text)");
Exec ("insert into Experiments (run,experiment,strategy,input,description) values ('" +
run + "', '" +
dc.GetExperimentLabel () + "', '" +
dc.GetStrategyLabel () + "', '" +
dc.GetInputLabel () + "', '" +
dc.GetDescription () + "')");
2008-08-29 13:22:09 -04:00
Exec ("create table if not exists Metadata ( run text, key text, value)");
2008-08-29 13:22:09 -04:00
for (MetadataList::iterator i = dc.MetadataBegin ();
i != dc.MetadataEnd (); i++) {
2011-05-13 14:59:56 -04:00
std::pair<std::string, std::string> blob = (*i);
Exec ("insert into Metadata (run,key,value) values ('" +
run + "', '" +
blob.first + "', '" +
blob.second + "')");
2011-05-13 14:59:56 -04:00
}
2008-08-29 13:22:09 -04:00
Exec ("BEGIN");
SqliteOutputCallback callback (this, run);
for (DataCalculatorList::iterator i = dc.DataCalculatorBegin ();
i != dc.DataCalculatorEnd (); i++) {
(*i)->Output (callback);
2011-05-13 14:59:56 -04:00
}
Exec ("COMMIT");
2008-08-29 13:22:09 -04:00
sqlite3_close (m_db);
2008-08-29 13:22:09 -04:00
// end SqliteDataOutput::Output
}
SqliteDataOutput::SqliteOutputCallback::SqliteOutputCallback
(Ptr<SqliteDataOutput> owner, std::string run) :
m_owner (owner),
m_runLabel (run)
2008-08-29 13:22:09 -04:00
{
NS_LOG_FUNCTION (this << owner << run);
2008-08-29 13:22:09 -04:00
m_owner->Exec ("create table if not exists Singletons ( run text, name text, variable text, value )");
2008-08-29 13:22:09 -04:00
// end SqliteDataOutput::SqliteOutputCallback::SqliteOutputCallback
}
void
SqliteDataOutput::SqliteOutputCallback::OutputStatistic (std::string key,
std::string variable,
const StatisticalSummary *statSum)
{
NS_LOG_FUNCTION (this << key << variable << statSum);
OutputSingleton (key,variable+"-count", (double)statSum->getCount ());
if (!isNaN (statSum->getSum ()))
OutputSingleton (key,variable+"-total", statSum->getSum ());
if (!isNaN (statSum->getMax ()))
OutputSingleton (key,variable+"-max", statSum->getMax ());
if (!isNaN (statSum->getMin ()))
OutputSingleton (key,variable+"-min", statSum->getMin ());
if (!isNaN (statSum->getSqrSum ()))
OutputSingleton (key,variable+"-sqrsum", statSum->getSqrSum ());
if (!isNaN (statSum->getStddev ()))
OutputSingleton (key,variable+"-stddev", statSum->getStddev ());
}
2008-08-29 13:22:09 -04:00
void
SqliteDataOutput::SqliteOutputCallback::OutputSingleton (std::string key,
std::string variable,
int val)
2008-08-29 13:22:09 -04:00
{
NS_LOG_FUNCTION (this << key << variable << val);
2008-08-29 13:22:09 -04:00
std::stringstream sstr;
sstr << "insert into Singletons (run,name,variable,value) values ('" <<
2011-05-13 14:59:56 -04:00
m_runLabel << "', '" <<
key << "', '" <<
variable << "', " <<
val << ")";
m_owner->Exec (sstr.str ());
2008-08-29 13:22:09 -04:00
// end SqliteDataOutput::SqliteOutputCallback::OutputSingleton
}
void
SqliteDataOutput::SqliteOutputCallback::OutputSingleton (std::string key,
std::string variable,
uint32_t val)
2008-08-29 13:22:09 -04:00
{
NS_LOG_FUNCTION (this << key << variable << val);
2008-08-29 13:22:09 -04:00
std::stringstream sstr;
sstr << "insert into Singletons (run,name,variable,value) values ('" <<
2011-05-13 14:59:56 -04:00
m_runLabel << "', '" <<
key << "', '" <<
variable << "', " <<
val << ")";
m_owner->Exec (sstr.str ());
2008-08-29 13:22:09 -04:00
// end SqliteDataOutput::SqliteOutputCallback::OutputSingleton
}
void
SqliteDataOutput::SqliteOutputCallback::OutputSingleton (std::string key,
std::string variable,
double val)
2008-08-29 13:22:09 -04:00
{
NS_LOG_FUNCTION (this << key << variable << val);
2008-08-29 13:22:09 -04:00
std::stringstream sstr;
sstr << "insert into Singletons (run,name,variable,value) values ('" <<
2011-05-13 14:59:56 -04:00
m_runLabel << "', '" <<
key << "', '" <<
variable << "', " <<
val << ")";
m_owner->Exec (sstr.str ());
2008-08-29 13:22:09 -04:00
// end SqliteDataOutput::SqliteOutputCallback::OutputSingleton
}
void
SqliteDataOutput::SqliteOutputCallback::OutputSingleton (std::string key,
std::string variable,
std::string val)
2008-08-29 13:22:09 -04:00
{
NS_LOG_FUNCTION (this << key << variable << val);
2008-08-29 13:22:09 -04:00
std::stringstream sstr;
sstr << "insert into Singletons (run,name,variable,value) values ('" <<
2011-05-13 14:59:56 -04:00
m_runLabel << "', '" <<
key << "', '" <<
variable << "', '" <<
val << "')";
m_owner->Exec (sstr.str ());
2008-08-29 13:22:09 -04:00
// end SqliteDataOutput::SqliteOutputCallback::OutputSingleton
}
void
SqliteDataOutput::SqliteOutputCallback::OutputSingleton (std::string key,
std::string variable,
Time val)
2008-08-29 13:22:09 -04:00
{
NS_LOG_FUNCTION (this << key << variable << val);
2008-08-29 13:22:09 -04:00
std::stringstream sstr;
sstr << "insert into Singletons (run,name,variable,value) values ('" <<
2011-05-13 14:59:56 -04:00
m_runLabel << "', '" <<
key << "', '" <<
variable << "', " <<
val.GetTimeStep () << ")";
m_owner->Exec (sstr.str ());
2008-08-29 13:22:09 -04:00
// end SqliteDataOutput::SqliteOutputCallback::OutputSingleton
}