5G-LENA nr-v4.0
The 5G/NR module for the ns-3 simulator
Loading...
Searching...
No Matches
3gpp-outdoor-calibration/power-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 "power-output-stats.h"
6
7#include "ns3/abort.h"
8#include "ns3/rng-seed-manager.h"
9
10namespace ns3
11{
12
16
17void
18PowerOutputStats::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 " ("
27 "Frame INTEGER NOT NULL, "
28 "SubFrame INTEGER NOT NULL,"
29 "Slot INTEGER NOT NULL,"
30 "Rnti INTEGER NOT NULL,"
31 "Imsi INTEGER NOT NULL,"
32 "BwpId INTEGER NOT NULL,"
33 "CellId INTEGER NOT NULL,"
34 "txPowerRb DOUBLE NOT NULL,"
35 "txPowerTotal DOUBLE NOT NULL,"
36 "rbNumActive INTEGER NOT NULL,"
37 "rbNumTotal INTEGER NOT NULL,"
38 "Seed INTEGER NOT NULL,"
39 "Run INTEGER NOT NULL);");
40 NS_ASSERT(ret);
41
42 PowerOutputStats::DeleteWhere(m_db,
43 RngSeedManager::GetSeed(),
44 RngSeedManager::GetRun(),
45 tableName);
46}
47
48void
50 Ptr<const SpectrumValue> txPsd,
51 [[maybe_unused]] const Time& t,
52 uint16_t rnti,
53 uint64_t imsi,
54 uint16_t bwpId,
55 uint16_t cellId)
56{
57 PowerResultCache c;
58 c.frame = sfnSf.GetFrame();
59 c.subFrame = sfnSf.GetSubframe();
60 c.slot = sfnSf.GetSlot();
61 c.rnti = rnti;
62 c.imsi = imsi;
63 c.bwpId = bwpId;
64 c.cellId = cellId;
65
66 uint32_t rbNumTotal = txPsd->GetValuesN();
67 uint32_t rbNumActive = 0;
68
69 for (uint32_t rbIndex = 0; rbIndex < rbNumTotal; rbIndex++)
70 {
71 if ((*txPsd)[rbIndex] != 0)
72 {
73 rbNumActive++;
74 }
75 }
76
77 if (rbNumActive == 0)
78 {
79 return; // ignore this entry
80 }
81
82 c.txPowerTotal = (Integral(*txPsd));
83 c.txPowerRb = c.txPowerTotal / rbNumActive;
84 c.rbNumActive = rbNumActive;
85 c.rbNumTotal = rbNumTotal;
86
87 m_powerCache.emplace_back(c);
88
89 // Let's wait until ~1MB of entries before storing it in the database
90 if (m_powerCache.size() * sizeof(PowerResultCache) > 1000000)
91 {
92 WriteCache();
93 }
94}
95
96void
98{
99 WriteCache();
100}
101
102void
103PowerOutputStats::DeleteWhere(SQLiteOutput* p,
104 uint32_t seed,
105 uint32_t run,
106 const std::string& table)
107{
108 bool ret;
109 sqlite3_stmt* stmt;
110 ret = p->SpinPrepare(&stmt, "DELETE FROM \"" + table + "\" WHERE SEED = ? AND RUN = ?;");
111 NS_ABORT_IF(ret == false);
112 ret = p->Bind(stmt, 1, seed);
113 NS_ABORT_IF(ret == false);
114 ret = p->Bind(stmt, 2, run);
115
116 ret = p->SpinExec(stmt);
117 NS_ABORT_IF(ret == false);
118}
119
120void
121PowerOutputStats::WriteCache()
122{
123 bool ret = m_db->SpinExec("BEGIN TRANSACTION;");
124 for (const auto& v : m_powerCache)
125 {
126 sqlite3_stmt* stmt;
127 ret = m_db->SpinPrepare(&stmt,
128 "INSERT INTO " + m_tableName +
129 " VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?);");
130 NS_ASSERT(ret);
131 ret = m_db->Bind(stmt, 1, v.frame);
132 NS_ASSERT(ret);
133 ret = m_db->Bind(stmt, 2, v.subFrame);
134 NS_ASSERT(ret);
135 ret = m_db->Bind(stmt, 3, v.slot);
136 NS_ASSERT(ret);
137 ret = m_db->Bind(stmt, 4, v.rnti);
138 NS_ASSERT(ret);
139 ret = m_db->Bind(stmt, 5, static_cast<uint32_t>(v.imsi));
140 NS_ASSERT(ret);
141 ret = m_db->Bind(stmt, 6, v.bwpId);
142 NS_ASSERT(ret);
143 ret = m_db->Bind(stmt, 7, v.cellId);
144 NS_ASSERT(ret);
145 ret = m_db->Bind(stmt, 8, v.txPowerRb);
146 NS_ASSERT(ret);
147 ret = m_db->Bind(stmt, 9, v.txPowerTotal);
148 NS_ASSERT(ret);
149 ret = m_db->Bind(stmt, 10, v.rbNumActive);
150 NS_ASSERT(ret);
151 ret = m_db->Bind(stmt, 11, v.rbNumTotal);
152 NS_ASSERT(ret);
153 ret = m_db->Bind(stmt, 12, RngSeedManager::GetSeed());
154 NS_ASSERT(ret);
155 ret = m_db->Bind(stmt, 13, static_cast<uint32_t>(RngSeedManager::GetRun()));
156 NS_ASSERT(ret);
157
158 ret = m_db->SpinExec(stmt);
159 NS_ASSERT(ret);
160 }
161 m_powerCache.clear();
162 ret = m_db->SpinExec("END TRANSACTION;");
163 NS_ASSERT(ret);
164}
165
166} // namespace ns3
void SavePower(const SfnSf &sfnSf, Ptr< const SpectrumValue > txPsd, const Time &t, uint16_t rnti, uint64_t imsi, uint16_t bwpId, uint16_t cellId)
Store power values.
void EmptyCache()
Force the cache write to disk, emptying the cache itself.
void SetDb(SQLiteOutput *db, const std::string &tableName="power")
Install the output database.
The SfnSf class.
Definition sfnsf.h:32
uint8_t GetSubframe() const
GetSubframe.
Definition sfnsf.cc:170
uint8_t GetSlot() const
GetSlot.
Definition sfnsf.cc:176
uint32_t GetFrame() const
GetFrame.
Definition sfnsf.cc:164