A Discrete-Event Network Simulator
API
sqlite-data-output.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2008 Drexel University
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation;
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * Author: Joe Kopena (tjkopena@cs.drexel.edu)
19  */
20 
21 #include <sstream>
22 
23 #include <sqlite3.h>
24 
25 #include "ns3/log.h"
26 #include "ns3/nstime.h"
27 
28 #include "data-collector.h"
29 #include "data-calculator.h"
30 #include "sqlite-data-output.h"
31 
32 using namespace ns3;
33 
34 NS_LOG_COMPONENT_DEFINE ("SqliteDataOutput");
35 
36 //--------------------------------------------------------------
37 //----------------------------------------------
39 {
40  NS_LOG_FUNCTION (this);
41 
42  m_filePrefix = "data";
43 }
45 {
46  NS_LOG_FUNCTION (this);
47 }
48 /* static */
49 TypeId
51 {
52  static TypeId tid = TypeId ("ns3::SqliteDataOutput")
54  .SetGroupName ("Stats")
55  .AddConstructor<SqliteDataOutput> ();
56  return tid;
57 }
58 
59 void
61 {
62  NS_LOG_FUNCTION (this);
63 
65  // end SqliteDataOutput::DoDispose
66 }
67 
68 int
69 SqliteDataOutput::Exec (std::string exe) {
70  NS_LOG_FUNCTION (this << exe);
71 
72  int res;
73  char **result;
74  int nrows, ncols;
75  char *errMsg = 0;
76 
77  NS_LOG_INFO ("executing '" << exe << "'");
78 
79  res = sqlite3_get_table (m_db,
80  exe.c_str (),
81  &result, &nrows, &ncols,
82  &errMsg);
83 
84  if (res != SQLITE_OK) {
85  NS_LOG_ERROR ("sqlite3 error: \"" << errMsg << "\"");
86  /*
87  } else {
88  // std::cout << "nrows " << nrows << " ncols " << ncols << std::endl;
89 
90  if (nrows > 0) {
91  for (int i = 0; i < ncols; i++) {
92  std::cout << " " << result[i];
93  }
94  std::cout << std::endl;
95 
96  for (int r = 1; r <= nrows; r++) {
97  for (int c = 0; c < ncols; c++) {
98  std::cout << " " << result[(r*ncols)+c];
99  }
100  std::cout << std::endl;
101  }
102  std::cout << std::endl;
103  }
104  */
105  }
106 
107  sqlite3_free_table (result);
108  return res;
109 
110  // end SqliteDataOutput::Exec
111 }
112 
113 //----------------------------------------------
114 void
116 {
117  NS_LOG_FUNCTION (this << &dc);
118 
119  std::string m_dbFile = m_filePrefix + ".db";
120 
121  if (sqlite3_open (m_dbFile.c_str (), &m_db)) {
122  NS_LOG_ERROR ("Could not open sqlite3 database \"" << m_dbFile << "\"");
123  NS_LOG_ERROR ("sqlite3 error \"" << sqlite3_errmsg (m_db) << "\"");
124  sqlite3_close (m_db);
126  return;
127  }
128 
129  std::string run = dc.GetRunLabel ();
130 
131  Exec ("create table if not exists Experiments (run, experiment, strategy, input, description text)");
132  Exec ("insert into Experiments (run,experiment,strategy,input,description) values ('" +
133  run + "', '" +
134  dc.GetExperimentLabel () + "', '" +
135  dc.GetStrategyLabel () + "', '" +
136  dc.GetInputLabel () + "', '" +
137  dc.GetDescription () + "')");
138 
139  Exec ("create table if not exists Metadata ( run text, key text, value)");
140 
141  for (MetadataList::iterator i = dc.MetadataBegin ();
142  i != dc.MetadataEnd (); i++) {
143  std::pair<std::string, std::string> blob = (*i);
144  Exec ("insert into Metadata (run,key,value) values ('" +
145  run + "', '" +
146  blob.first + "', '" +
147  blob.second + "')");
148  }
149 
150  Exec ("BEGIN");
151  SqliteOutputCallback callback (this, run);
152  for (DataCalculatorList::iterator i = dc.DataCalculatorBegin ();
153  i != dc.DataCalculatorEnd (); i++) {
154  (*i)->Output (callback);
155  }
156  Exec ("COMMIT");
157 
158  sqlite3_close (m_db);
159 
160  // end SqliteDataOutput::Output
161 }
162 
164  (Ptr<SqliteDataOutput> owner, std::string run) :
165  m_owner (owner),
166  m_runLabel (run)
167 {
168  NS_LOG_FUNCTION (this << owner << run);
169 
170  m_owner->Exec ("create table if not exists Singletons ( run text, name text, variable text, value )");
171 
172  // end SqliteDataOutput::SqliteOutputCallback::SqliteOutputCallback
173 }
174 
175 void
177  std::string variable,
178  const StatisticalSummary *statSum)
179 {
180  NS_LOG_FUNCTION (this << key << variable << statSum);
181 
182  OutputSingleton (key,variable+"-count", (double)statSum->getCount ());
183  if (!isNaN (statSum->getSum ()))
184  OutputSingleton (key,variable+"-total", statSum->getSum ());
185  if (!isNaN (statSum->getMax ()))
186  OutputSingleton (key,variable+"-max", statSum->getMax ());
187  if (!isNaN (statSum->getMin ()))
188  OutputSingleton (key,variable+"-min", statSum->getMin ());
189  if (!isNaN (statSum->getSqrSum ()))
190  OutputSingleton (key,variable+"-sqrsum", statSum->getSqrSum ());
191  if (!isNaN (statSum->getStddev ()))
192  OutputSingleton (key,variable+"-stddev", statSum->getStddev ());
193 }
194 
195 
196 void
198  std::string variable,
199  int val)
200 {
201  NS_LOG_FUNCTION (this << key << variable << val);
202 
203  std::stringstream sstr;
204  sstr << "insert into Singletons (run,name,variable,value) values ('" <<
205  m_runLabel << "', '" <<
206  key << "', '" <<
207  variable << "', " <<
208  val << ")";
209  m_owner->Exec (sstr.str ());
210 
211  // end SqliteDataOutput::SqliteOutputCallback::OutputSingleton
212 }
213 void
215  std::string variable,
216  uint32_t val)
217 {
218  NS_LOG_FUNCTION (this << key << variable << val);
219 
220  std::stringstream sstr;
221  sstr << "insert into Singletons (run,name,variable,value) values ('" <<
222  m_runLabel << "', '" <<
223  key << "', '" <<
224  variable << "', " <<
225  val << ")";
226  m_owner->Exec (sstr.str ());
227  // end SqliteDataOutput::SqliteOutputCallback::OutputSingleton
228 }
229 void
231  std::string variable,
232  double val)
233 {
234  NS_LOG_FUNCTION (this << key << variable << val);
235 
236  std::stringstream sstr;
237  sstr << "insert into Singletons (run,name,variable,value) values ('" <<
238  m_runLabel << "', '" <<
239  key << "', '" <<
240  variable << "', " <<
241  val << ")";
242  m_owner->Exec (sstr.str ());
243  // end SqliteDataOutput::SqliteOutputCallback::OutputSingleton
244 }
245 void
247  std::string variable,
248  std::string val)
249 {
250  NS_LOG_FUNCTION (this << key << variable << val);
251 
252  std::stringstream sstr;
253  sstr << "insert into Singletons (run,name,variable,value) values ('" <<
254  m_runLabel << "', '" <<
255  key << "', '" <<
256  variable << "', '" <<
257  val << "')";
258  m_owner->Exec (sstr.str ());
259  // end SqliteDataOutput::SqliteOutputCallback::OutputSingleton
260 }
261 void
263  std::string variable,
264  Time val)
265 {
266  NS_LOG_FUNCTION (this << key << variable << val);
267 
268  std::stringstream sstr;
269  sstr << "insert into Singletons (run,name,variable,value) values ('" <<
270  m_runLabel << "', '" <<
271  key << "', '" <<
272  variable << "', " <<
273  val.GetTimeStep () << ")";
274  m_owner->Exec (sstr.str ());
275  // end SqliteDataOutput::SqliteOutputCallback::OutputSingleton
276 }
DataCalculatorList::iterator DataCalculatorEnd()
Returns an iterator to the past-the-end of the DataCalculator list.
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:102
Abstract Data Output Interface class s.
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:73
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by "...
virtual double getMin() const =0
Returns the minimum of the values.
Abstract class for calculating statistical data.
virtual double getSqrSum() const =0
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:201
std::string GetRunLabel() const
Return the runID label.
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition: log.h:244
Class to generate OMNeT output.
virtual double getStddev() const =0
Returns the standard deviation of the (weighted) observations.
MetadataList::iterator MetadataBegin()
Returns an iterator to the beginning of the metadata list.
virtual double getMax() const =0
Returns the maximum of the values.
virtual void DoDispose()
Destructor implementation.
std::string GetStrategyLabel() const
Return the strategy label.
std::string GetExperimentLabel() const
Return the experiment label.
SqliteOutputCallback(Ptr< SqliteDataOutput > owner, std::string run)
Constructor.
std::string m_filePrefix
File prefix for the DataOutputInterface.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Outputs data in a format compatible with SQLite.
int64_t GetTimeStep(void) const
Get the raw time value, in the current resolution unit.
Definition: nstime.h:377
virtual double getSum() const =0
static TypeId GetTypeId(void)
Register this type.
void OutputSingleton(std::string key, std::string variable, int val)
Generates a single data output.
Collects data.
std::string GetDescription() const
Return the description label.
virtual void DoDispose()
Destructor implementation.
DataCalculatorList::iterator DataCalculatorBegin()
Returns an iterator to the beginning of the DataCalculator list.
sqlite3 * m_db
pointer to the SQL database
bool isNaN(double x)
true if x is NaN
#define NS_LOG_ERROR(msg)
Use NS_LOG to output a message of level LOG_ERROR.
Definition: log.h:220
void OutputStatistic(std::string key, std::string variable, const StatisticalSummary *statSum)
Generates data statistics.
std::string GetInputLabel() const
Return the input label.
int Exec(std::string exe)
Execute a sqlite3 query.
virtual long getCount() const =0
Returns the number of observations.
virtual void Output(DataCollector &dc)
Outputs information from the provided DataCollector.
a unique identifier for an interface.
Definition: type-id.h:58
MetadataList::iterator MetadataEnd()
Returns an iterator to the past-the-end of the metadata list.
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:826