A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
sqlite-data-output.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2008 Drexel University
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: Joe Kopena (tjkopena@cs.drexel.edu)
7 */
8
10
11#include "data-calculator.h"
12#include "data-collector.h"
13#include "sqlite-output.h"
14
15#include "ns3/log.h"
16#include "ns3/nstime.h"
17
18#include <sstream>
19
20namespace ns3
21{
22
23NS_LOG_COMPONENT_DEFINE("SqliteDataOutput");
24
32
37
38/* static */
41{
42 static TypeId tid = TypeId("ns3::SqliteDataOutput")
44 .SetGroupName("Stats")
45 .AddConstructor<SqliteDataOutput>();
46 return tid;
47}
48
49//----------------------------------------------
50void
52{
53 NS_LOG_FUNCTION(this << &dc);
54
55 std::string m_dbFile = m_filePrefix + ".db";
56 std::string run = dc.GetRunLabel();
57 bool res;
58
59 m_sqliteOut = new SQLiteOutput(m_dbFile);
60
61 res = m_sqliteOut->SpinExec("CREATE TABLE IF NOT EXISTS Experiments (run, experiment, "
62 "strategy, input, description text)");
63 NS_ASSERT(res);
64
65 sqlite3_stmt* stmt;
66 res = m_sqliteOut->WaitPrepare(&stmt,
67 "INSERT INTO Experiments "
68 "(run, experiment, strategy, input, description)"
69 "values (?, ?, ?, ?, ?)");
70 NS_ASSERT(res);
71
72 // Create temporary strings to hold their value
73 // throughout the lifetime of the Bind and Step
74 // procedures
75 //
76 // DataCollector could return const std::string&,
77 // but that could break the python bindings
78 res = m_sqliteOut->Bind(stmt, 1, run);
79 NS_ASSERT(res);
80 std::string experimentLabel = dc.GetExperimentLabel();
81 res = m_sqliteOut->Bind(stmt, 2, experimentLabel);
82 NS_ASSERT(res);
83 std::string strategyLabel = dc.GetStrategyLabel();
84 res = m_sqliteOut->Bind(stmt, 3, strategyLabel);
85 NS_ASSERT(res);
86 std::string inputLabel = dc.GetInputLabel();
87 res = m_sqliteOut->Bind(stmt, 4, inputLabel);
88 NS_ASSERT(res);
89 std::string description = dc.GetDescription();
90 res = m_sqliteOut->Bind(stmt, 5, description);
91 NS_ASSERT(res);
92
93 res = m_sqliteOut->SpinStep(stmt);
94 NS_ASSERT(res);
95 res = m_sqliteOut->SpinFinalize(stmt);
96 NS_ASSERT(res == 0);
97
98 res = m_sqliteOut->WaitExec("CREATE TABLE IF NOT EXISTS "
99 "Metadata ( run text, key text, value)");
100 NS_ASSERT(res);
101
102 res = m_sqliteOut->WaitPrepare(&stmt,
103 "INSERT INTO Metadata "
104 "(run, key, value)"
105 "values (?, ?, ?)");
106 NS_ASSERT(res);
107
108 for (auto i = dc.MetadataBegin(); i != dc.MetadataEnd(); i++)
109 {
110 std::pair<std::string, std::string> blob = (*i);
111 m_sqliteOut->SpinReset(stmt);
112 m_sqliteOut->Bind(stmt, 1, run);
113 m_sqliteOut->Bind(stmt, 2, blob.first);
114 m_sqliteOut->Bind(stmt, 3, blob.second);
115 m_sqliteOut->SpinStep(stmt);
116 }
117
118 m_sqliteOut->SpinFinalize(stmt);
119
120 m_sqliteOut->SpinExec("BEGIN");
121 SqliteOutputCallback callback(m_sqliteOut, run);
122 for (auto i = dc.DataCalculatorBegin(); i != dc.DataCalculatorEnd(); i++)
123 {
124 (*i)->Output(callback);
125 }
126 m_sqliteOut->SpinExec("COMMIT");
127 // end SqliteDataOutput::Output
128 m_sqliteOut->Unref();
129}
130
132 std::string run)
133 : m_db(db),
134 m_runLabel(run)
135{
136 NS_LOG_FUNCTION(this << db << run);
137
138 m_db->WaitExec("CREATE TABLE IF NOT EXISTS Singletons "
139 "( run text, name text, variable text, value )");
140
141 m_db->WaitPrepare(&m_insertSingletonStatement,
142 "INSERT INTO Singletons "
143 "(run, name, variable, value)"
144 "values (?, ?, ?, ?)");
146}
147
149{
150 m_db->SpinFinalize(m_insertSingletonStatement);
151}
152
153void
155 std::string variable,
156 const StatisticalSummary* statSum)
157{
158 NS_LOG_FUNCTION(this << key << variable << statSum);
159
160 OutputSingleton(key, variable + "-count", static_cast<double>(statSum->getCount()));
161 if (!std::isnan(statSum->getSum()))
162 {
163 OutputSingleton(key, variable + "-total", statSum->getSum());
164 }
165 if (!std::isnan(statSum->getMax()))
166 {
167 OutputSingleton(key, variable + "-max", statSum->getMax());
168 }
169 if (!std::isnan(statSum->getMin()))
170 {
171 OutputSingleton(key, variable + "-min", statSum->getMin());
172 }
173 if (!std::isnan(statSum->getSqrSum()))
174 {
175 OutputSingleton(key, variable + "-sqrsum", statSum->getSqrSum());
176 }
177 if (!std::isnan(statSum->getStddev()))
178 {
179 OutputSingleton(key, variable + "-stddev", statSum->getStddev());
180 }
181}
182
183void
185 std::string variable,
186 int val)
187{
188 NS_LOG_FUNCTION(this << key << variable << val);
189
190 m_db->SpinReset(m_insertSingletonStatement);
191 m_db->Bind(m_insertSingletonStatement, 2, key);
192 m_db->Bind(m_insertSingletonStatement, 3, variable);
193 m_db->Bind(m_insertSingletonStatement, 4, val);
194 m_db->SpinStep(m_insertSingletonStatement);
195}
196
197void
199 std::string variable,
200 uint32_t val)
201{
202 NS_LOG_FUNCTION(this << key << variable << val);
203
204 m_db->SpinReset(m_insertSingletonStatement);
205 m_db->Bind(m_insertSingletonStatement, 2, key);
206 m_db->Bind(m_insertSingletonStatement, 3, variable);
207 m_db->Bind(m_insertSingletonStatement, 4, val);
208 m_db->SpinStep(m_insertSingletonStatement);
209}
210
211void
213 std::string variable,
214 double val)
215{
216 NS_LOG_FUNCTION(this << key << variable << val);
217
218 m_db->SpinReset(m_insertSingletonStatement);
219 m_db->Bind(m_insertSingletonStatement, 2, key);
220 m_db->Bind(m_insertSingletonStatement, 3, variable);
221 m_db->Bind(m_insertSingletonStatement, 4, val);
222 m_db->SpinStep(m_insertSingletonStatement);
223}
224
225void
227 std::string variable,
228 std::string val)
229{
230 NS_LOG_FUNCTION(this << key << variable << val);
231
232 m_db->SpinReset(m_insertSingletonStatement);
233 m_db->Bind(m_insertSingletonStatement, 2, key);
234 m_db->Bind(m_insertSingletonStatement, 3, variable);
235 m_db->Bind(m_insertSingletonStatement, 4, val);
236 m_db->SpinStep(m_insertSingletonStatement);
237}
238
239void
241 std::string variable,
242 Time val)
243{
244 NS_LOG_FUNCTION(this << key << variable << val);
245
246 m_db->SpinReset(m_insertSingletonStatement);
247 m_db->Bind(m_insertSingletonStatement, 2, key);
248 m_db->Bind(m_insertSingletonStatement, 3, variable);
249 m_db->Bind(m_insertSingletonStatement, 4, val.GetTimeStep());
250 m_db->SpinStep(m_insertSingletonStatement);
251}
252
253} // namespace ns3
Collects data.
std::string GetExperimentLabel() const
Return the experiment label.
DataCalculatorList::iterator DataCalculatorBegin()
Returns an iterator to the beginning of the DataCalculator list.
DataCalculatorList::iterator DataCalculatorEnd()
Returns an iterator to the past-the-end of the DataCalculator list.
std::string GetDescription() const
Return the description label.
MetadataList::iterator MetadataBegin()
Returns an iterator to the beginning of the metadata list.
MetadataList::iterator MetadataEnd()
Returns an iterator to the past-the-end of the metadata list.
std::string GetStrategyLabel() const
Return the strategy label.
std::string GetRunLabel() const
Return the runID label.
std::string GetInputLabel() const
Return the input label.
Abstract Data Output Interface class s.
std::string m_filePrefix
File prefix for the DataOutputInterface.
Smart pointer class similar to boost::intrusive_ptr.
A C++ interface towards an SQLITE database.
void OutputStatistic(std::string key, std::string variable, const StatisticalSummary *statSum) override
Generates data statistics.
sqlite3_stmt * m_insertSingletonStatement
Pointer to a Sqlite3 singleton statement.
SqliteOutputCallback(const Ptr< SQLiteOutput > &db, std::string run)
Constructor.
void OutputSingleton(std::string key, std::string variable, int val) override
Generates a single data output.
Outputs data in a format compatible with SQLite.
void Output(DataCollector &dc) override
Outputs information from the provided DataCollector.
static TypeId GetTypeId()
Register this type.
Ptr< SQLiteOutput > m_sqliteOut
Database.
Abstract class for calculating statistical data.
virtual double getMax() const =0
Returns the maximum of the values.
virtual double getStddev() const =0
Returns the standard deviation of the (weighted) observations.
virtual long getCount() const =0
Returns the number of observations.
virtual double getMin() const =0
Returns the minimum of the values.
virtual double getSum() const =0
virtual double getSqrSum() const =0
Simulation virtual time values and global simulation resolution.
Definition nstime.h:94
int64_t GetTimeStep() const
Get the raw time value, in the current resolution unit.
Definition nstime.h:434
a unique identifier for an interface.
Definition type-id.h:48
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition type-id.cc:1001
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file,...
Definition assert.h:55
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:191
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
Every class exported by the ns3 library is enclosed in the ns3 namespace.