5G-LENA nr-v4.0
The 5G/NR module for the ns-3 simulator
Loading...
Searching...
No Matches
lena-lte-comparison/rb-output-stats.cc
1// Copyright (c) 2020 Centre Tecnologic de Telecomunicacions de Catalunya (CTTC)
2//
3// SPDX-License-Identifier: GPL-2.0-only
4
5#include "rb-output-stats.h"
6
7#include "ns3/abort.h"
8#include "ns3/rng-seed-manager.h"
9
10namespace ns3
11{
12
14{
15}
16
17void
18RbOutputStats::SetDb(SQLiteOutput* db, const std::string& tableName)
19{
20 m_db = db;
21 m_tableName = tableName;
22
23 bool ret;
24
25 ret = m_db->SpinExec("CREATE TABLE IF NOT EXISTS " + tableName +
26 "(Frame INTEGER NOT NULL, "
27 "SubFrame INTEGER NOT NULL,"
28 "Slot INTEGER NOT NULL,"
29 "Symbol INTEGER NOT NULL,"
30 "RBIndexActive INTEGER NOT NULL,"
31 "BwpId INTEGER NOT NULL,"
32 "CellId INTEGER NOT NULL,"
33 "Seed INTEGER NOT NULL,"
34 "Run INTEGER NOT NULL);");
35 NS_ASSERT(ret);
36
37 RbOutputStats::DeleteWhere(m_db,
38 RngSeedManager::GetSeed(),
39 RngSeedManager::GetRun(),
40 tableName);
41}
42
43void
44RbOutputStats::SaveRbStats(const SfnSf& sfnSf,
45 uint8_t sym,
46 const std::vector<int> rbUsed,
47 uint16_t bwpId,
48 uint16_t cellId)
49{
50 RbCache c;
51 c.sym = sym;
52 c.sfnSf = sfnSf;
53 c.rbUsed = rbUsed;
54 c.bwpId = bwpId;
55 c.cellId = cellId;
56
57 m_slotCache.emplace_back(c);
58
59 // Let's wait until ~500 KB of entries before storing it in the database.
60 // the entries value is an approximation, as the vector of RB size can
61 // vary.
62 if (m_slotCache.size() * c.GetSize() > 500000)
63 {
64 WriteCache();
65 }
66}
67
68void
70{
71 WriteCache();
72}
73
74void
75RbOutputStats::DeleteWhere(SQLiteOutput* p, uint32_t seed, uint32_t run, const std::string& table)
76{
77 bool ret;
78 sqlite3_stmt* stmt;
79 ret = p->SpinPrepare(&stmt, "DELETE FROM \"" + table + "\" WHERE SEED = ? AND RUN = ?;");
80 NS_ABORT_IF(ret == false);
81 ret = p->Bind(stmt, 1, seed);
82 NS_ABORT_IF(ret == false);
83 ret = p->Bind(stmt, 2, run);
84
85 ret = p->SpinExec(stmt);
86 NS_ABORT_IF(ret == false);
87}
88
89void
90RbOutputStats::WriteCache()
91{
92 bool ret = m_db->SpinExec("BEGIN TRANSACTION;");
93 for (const auto& v : m_slotCache)
94 {
95 for (const auto& rb : v.rbUsed)
96 {
97 sqlite3_stmt* stmt;
98 ret = m_db->SpinPrepare(&stmt,
99 "INSERT INTO " + m_tableName + " VALUES (?,?,?,?,?,?,?,?,?);");
100 NS_ASSERT(ret);
101 ret = m_db->Bind(stmt, 1, v.sfnSf.GetFrame());
102 NS_ASSERT(ret);
103 ret = m_db->Bind(stmt, 2, v.sfnSf.GetSubframe());
104 NS_ASSERT(ret);
105 ret = m_db->Bind(stmt, 3, v.sfnSf.GetSlot());
106 NS_ASSERT(ret);
107 ret = m_db->Bind(stmt, 4, v.sym);
108 NS_ASSERT(ret);
109 ret = m_db->Bind(stmt, 5, rb);
110 NS_ASSERT(ret);
111 ret = m_db->Bind(stmt, 6, v.bwpId);
112 NS_ASSERT(ret);
113 ret = m_db->Bind(stmt, 7, v.cellId);
114 NS_ASSERT(ret);
115 ret = m_db->Bind(stmt, 8, RngSeedManager::GetSeed());
116 NS_ASSERT(ret);
117 ret = m_db->Bind(stmt, 9, static_cast<uint32_t>(RngSeedManager::GetRun()));
118 NS_ASSERT(ret);
119
120 ret = m_db->SpinExec(stmt);
121 NS_ASSERT(ret);
122 }
123 }
124 m_slotCache.clear();
125 ret = m_db->SpinExec("END TRANSACTION;");
126 NS_ASSERT(ret);
127}
128
129} // namespace ns3
void EmptyCache()
Force the cache write to disk, emptying the cache itself.
void SetDb(SQLiteOutput *db, const std::string &tableName="rbStats")
Install the output database.
void SaveRbStats(const SfnSf &sfnSf, uint8_t sym, const std::vector< int > rbUsed, uint16_t bwpId, uint16_t cellId)
Save the slot statistics.