5G-LENA nr-v4.0
The 5G/NR module for the ns-3 simulator
Loading...
Searching...
No Matches
lena-lte-comparison/sinr-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 "sinr-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
18SinrOutputStats::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 "CellId INTEGER NOT NULL, "
28 "BwpId INTEGER NOT NULL,"
29 "Rnti INTEGER NOT NULL,"
30 "AvgSinr DOUBLE NOT NULL,"
31 "Seed INTEGER NOT NULL,"
32 "Run INTEGER NOT NULL);");
33 NS_ASSERT(ret);
34
35 SinrOutputStats::DeleteWhere(m_db,
36 RngSeedManager::GetSeed(),
37 RngSeedManager::GetRun(),
38 tableName);
39}
40
41void
42SinrOutputStats::SaveSinr(uint16_t cellId, uint16_t rnti, double avgSinr, uint16_t bwpId)
43{
44 m_sinrCache.emplace_back(cellId, bwpId, rnti, avgSinr);
45
46 // Let's wait until ~1MB of entries before storing it in the database
47 if (m_sinrCache.size() * sizeof(SinrResultCache) > 1000000)
48 {
49 WriteCache();
50 }
51}
52
53void
55{
56 WriteCache();
57}
58
59void
60SinrOutputStats::DeleteWhere(SQLiteOutput* p, uint32_t seed, uint32_t run, const std::string& table)
61{
62 bool ret;
63 sqlite3_stmt* stmt;
64 ret = p->SpinPrepare(&stmt, "DELETE FROM \"" + table + "\" WHERE SEED = ? AND RUN = ?;");
65 NS_ABORT_IF(ret == false);
66 ret = p->Bind(stmt, 1, seed);
67 NS_ABORT_IF(ret == false);
68 ret = p->Bind(stmt, 2, run);
69
70 ret = p->SpinExec(stmt);
71 NS_ABORT_IF(ret == false);
72}
73
74void
75SinrOutputStats::WriteCache()
76{
77 bool ret = m_db->SpinExec("BEGIN TRANSACTION;");
78 for (const auto& v : m_sinrCache)
79 {
80 sqlite3_stmt* stmt;
81 ret = m_db->SpinPrepare(&stmt, "INSERT INTO " + m_tableName + " VALUES (?,?,?,?,?,?);");
82 NS_ASSERT(ret);
83 ret = m_db->Bind(stmt, 1, v.cellId);
84 NS_ASSERT(ret);
85 ret = m_db->Bind(stmt, 2, v.bwpId);
86 NS_ASSERT(ret);
87 ret = m_db->Bind(stmt, 3, v.rnti);
88 NS_ASSERT(ret);
89 ret = m_db->Bind(stmt, 4, v.avgSinr);
90 NS_ASSERT(ret);
91 ret = m_db->Bind(stmt, 5, RngSeedManager::GetSeed());
92 NS_ASSERT(ret);
93 ret = m_db->Bind(stmt, 6, static_cast<uint32_t>(RngSeedManager::GetRun()));
94 NS_ASSERT(ret);
95
96 ret = m_db->SpinExec(stmt);
97 NS_ASSERT(ret);
98 }
99 m_sinrCache.clear();
100 ret = m_db->SpinExec("END TRANSACTION;");
101 NS_ASSERT(ret);
102}
103
104} // namespace ns3
void SaveSinr(uint16_t cellId, uint16_t rnti, double avgSinr, uint16_t bwpId)
Store the SINR values.
void SetDb(SQLiteOutput *db, const std::string &tableName="sinr")
Install the output database.
void EmptyCache()
Force the cache write to disk, emptying the cache itself.