5G-LENA nr-v3.3-49-g235218b1
The 5G/NR module for the ns-3 simulator
Loading...
Searching...
No Matches
lena-lte-comparison/slot-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 "slot-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
18SlotOutputStats::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 "BwpId INTEGER NOT NULL,"
30 "CellId INTEGER NOT NULL,"
31 "ScheduledUe INTEGER NOT NULL,"
32 "UsedReg INTEGER NOT NULL,"
33 "UsedSym INTEGER NOT NULL,"
34 "AvailableRb INTEGER NOT NULL,"
35 "AvailableSym INTEGER NOT NULL,"
36 "Seed INTEGER NOT NULL,"
37 "Run INTEGER NOT NULL);");
38 NS_ASSERT(ret);
39
40 SlotOutputStats::DeleteWhere(m_db,
41 RngSeedManager::GetSeed(),
42 RngSeedManager::GetRun(),
43 tableName);
44}
45
46void
47SlotOutputStats::SaveSlotStats(const SfnSf& sfnSf,
48 uint32_t scheduledUe,
49 uint32_t usedReg,
50 uint32_t usedSym,
51 uint32_t availableRb,
52 uint32_t availableSym,
53 uint16_t bwpId,
54 uint16_t cellId)
55{
56 SlotCache c;
57 c.sfnSf = sfnSf;
58 c.scheduledUe = scheduledUe;
59 c.usedReg = usedReg;
60 c.usedSym = usedSym;
61 c.availableRb = availableRb;
62 c.availableSym = availableSym;
63 c.bwpId = bwpId;
64 c.cellId = cellId;
65
66 m_slotCache.emplace_back(c);
67
68 // Let's wait until ~1MB of entries before storing it in the database
69 if (m_slotCache.size() * sizeof(SlotCache) > 1000000)
70 {
71 WriteCache();
72 }
73}
74
75void
77{
78 WriteCache();
79}
80
81void
82SlotOutputStats::DeleteWhere(SQLiteOutput* p, uint32_t seed, uint32_t run, const std::string& table)
83{
84 bool ret;
85 sqlite3_stmt* stmt;
86 ret = p->SpinPrepare(&stmt, "DELETE FROM \"" + table + "\" WHERE SEED = ? AND RUN = ?;");
87 NS_ABORT_IF(ret == false);
88 ret = p->Bind(stmt, 1, seed);
89 NS_ABORT_IF(ret == false);
90 ret = p->Bind(stmt, 2, run);
91
92 ret = p->SpinExec(stmt);
93 NS_ABORT_IF(ret == false);
94}
95
96void
97SlotOutputStats::WriteCache()
98{
99 bool ret = m_db->SpinExec("BEGIN TRANSACTION;");
100 for (const auto& v : m_slotCache)
101 {
102 sqlite3_stmt* stmt;
103 ret =
104 m_db->SpinPrepare(&stmt,
105 "INSERT INTO " + m_tableName + " VALUES (?,?,?,?,?,?,?,?,?,?,?,?);");
106 NS_ASSERT(ret);
107 ret = m_db->Bind(stmt, 1, v.sfnSf.GetFrame());
108 NS_ASSERT(ret);
109 ret = m_db->Bind(stmt, 2, v.sfnSf.GetSubframe());
110 NS_ASSERT(ret);
111 ret = m_db->Bind(stmt, 3, v.sfnSf.GetSlot());
112 NS_ASSERT(ret);
113 ret = m_db->Bind(stmt, 4, v.bwpId);
114 NS_ASSERT(ret);
115 ret = m_db->Bind(stmt, 5, v.cellId);
116 NS_ASSERT(ret);
117 ret = m_db->Bind(stmt, 6, v.scheduledUe);
118 NS_ASSERT(ret);
119 ret = m_db->Bind(stmt, 7, v.usedReg);
120 NS_ASSERT(ret);
121 ret = m_db->Bind(stmt, 8, v.usedSym);
122 NS_ASSERT(ret);
123 ret = m_db->Bind(stmt, 9, v.availableRb);
124 NS_ASSERT(ret);
125 ret = m_db->Bind(stmt, 10, v.availableSym);
126 NS_ASSERT(ret);
127 ret = m_db->Bind(stmt, 11, RngSeedManager::GetSeed());
128 NS_ASSERT(ret);
129 ret = m_db->Bind(stmt, 12, static_cast<uint32_t>(RngSeedManager::GetRun()));
130 NS_ASSERT(ret);
131
132 ret = m_db->SpinExec(stmt);
133 NS_ASSERT(ret);
134 }
135 m_slotCache.clear();
136 ret = m_db->SpinExec("END TRANSACTION;");
137 NS_ASSERT(ret);
138}
139
140} // namespace ns3
void EmptyCache()
Force the cache write to disk, emptying the cache itself.
void SaveSlotStats(const SfnSf &sfnSf, uint32_t scheduledUe, uint32_t usedReg, uint32_t usedSym, uint32_t availableRb, uint32_t availableSym, uint16_t bwpId, uint16_t cellId)
Save the slot statistics.
void SetDb(SQLiteOutput *db, const std::string &tableName="slotStats")
Install the output database.