A Discrete-Event Network Simulator
API
phy-stats-calculator.cc
Go to the documentation of this file.
1 /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2011 Centre Tecnologic de Telecomunicacions de Catalunya (CTTC)
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation;
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * Author: Jaume Nin <jnin@cttc.es>
19  */
20 
21 #include "phy-stats-calculator.h"
22 #include "ns3/string.h"
23 #include <ns3/simulator.h>
24 #include <ns3/log.h>
25 
26 namespace ns3 {
27 
28 NS_LOG_COMPONENT_DEFINE ("PhyStatsCalculator");
29 
30 NS_OBJECT_ENSURE_REGISTERED (PhyStatsCalculator);
31 
33  : m_RsrpSinrFirstWrite (true),
34  m_UeSinrFirstWrite (true),
35  m_InterferenceFirstWrite (true)
36 {
37  NS_LOG_FUNCTION (this);
38 
39 }
40 
42 {
43  NS_LOG_FUNCTION (this);
44 }
45 
46 TypeId
48 {
49  static TypeId tid = TypeId ("ns3::PhyStatsCalculator")
51  .AddConstructor<PhyStatsCalculator> ()
52  .AddAttribute ("DlRsrpSinrFilename",
53  "Name of the file where the RSRP/SINR statistics will be saved.",
54  StringValue ("DlRsrpSinrStats.txt"),
57  .AddAttribute ("UlSinrFilename",
58  "Name of the file where the UE SINR statistics will be saved.",
59  StringValue ("UlSinrStats.txt"),
62  .AddAttribute ("UlInterferenceFilename",
63  "Name of the file where the interference statistics will be saved.",
64  StringValue ("UlInterferenceStats.txt"),
67  ;
68  return tid;
69 }
70 
71 void
73 {
74  m_RsrpSinrFilename = filename;
75 }
76 
77 std::string
79 {
80  return m_RsrpSinrFilename;
81 }
82 
83 void
85 {
86  m_ueSinrFilename = filename;
87 }
88 
89 std::string
91 {
92  return m_ueSinrFilename;
93 }
94 
95 void
97 {
98  m_interferenceFilename = filename;
99 }
100 
101 std::string
103 {
104  return m_interferenceFilename;
105 }
106 
107 
108 
109 void
110 PhyStatsCalculator::ReportCurrentCellRsrpSinr (uint16_t cellId, uint64_t imsi, uint16_t rnti,
111  double rsrp, double sinr)
112 {
113  NS_LOG_FUNCTION (this << cellId << imsi << rnti << rsrp << sinr);
114  NS_LOG_INFO ("Write RSRP/SINR Phy Stats in " << GetCurrentCellRsrpSinrFilename ().c_str ());
115 
116  std::ofstream outFile;
117  if ( m_RsrpSinrFirstWrite == true )
118  {
119  outFile.open (GetCurrentCellRsrpSinrFilename ().c_str ());
120  if (!outFile.is_open ())
121  {
122  NS_LOG_ERROR ("Can't open file " << GetCurrentCellRsrpSinrFilename ().c_str ());
123  return;
124  }
125  m_RsrpSinrFirstWrite = false;
126  outFile << "% time\tcellId\tIMSI\tRNTI\trsrp\tsinr";
127  outFile << std::endl;
128  }
129  else
130  {
131  outFile.open (GetCurrentCellRsrpSinrFilename ().c_str (), std::ios_base::app);
132  if (!outFile.is_open ())
133  {
134  NS_LOG_ERROR ("Can't open file " << GetCurrentCellRsrpSinrFilename ().c_str ());
135  return;
136  }
137  }
138 
139  outFile << Simulator::Now ().GetNanoSeconds () / (double) 1e9 << "\t";
140  outFile << cellId << "\t";
141  outFile << imsi << "\t";
142  outFile << rnti << "\t";
143  outFile << rsrp << "\t";
144  outFile << sinr << std::endl;
145  outFile.close ();
146 }
147 
148 void
149 PhyStatsCalculator::ReportUeSinr (uint16_t cellId, uint64_t imsi, uint16_t rnti, double sinrLinear)
150 {
151  NS_LOG_FUNCTION (this << cellId << imsi << rnti << sinrLinear);
152  NS_LOG_INFO ("Write SINR Linear Phy Stats in " << GetUeSinrFilename ().c_str ());
153 
154  std::ofstream outFile;
155  if ( m_UeSinrFirstWrite == true )
156  {
157  outFile.open (GetUeSinrFilename ().c_str ());
158  if (!outFile.is_open ())
159  {
160  NS_LOG_ERROR ("Can't open file " << GetUeSinrFilename ().c_str ());
161  return;
162  }
163  m_UeSinrFirstWrite = false;
164  outFile << "% time\tcellId\tIMSI\tRNTI\tsinrLinear";
165  outFile << std::endl;
166  }
167  else
168  {
169  outFile.open (GetUeSinrFilename ().c_str (), std::ios_base::app);
170  if (!outFile.is_open ())
171  {
172  NS_LOG_ERROR ("Can't open file " << GetUeSinrFilename ().c_str ());
173  return;
174  }
175  }
176 
177  outFile << Simulator::Now ().GetNanoSeconds () / (double) 1e9 << "\t";
178  outFile << cellId << "\t";
179  outFile << imsi << "\t";
180  outFile << rnti << "\t";
181  outFile << sinrLinear << std::endl;
182  outFile.close ();
183 }
184 
185 void
187 {
188  NS_LOG_FUNCTION (this << cellId << interference);
189  NS_LOG_INFO ("Write Interference Phy Stats in " << GetInterferenceFilename ().c_str ());
190 
191  std::ofstream outFile;
192  if ( m_InterferenceFirstWrite == true )
193  {
194  outFile.open (GetInterferenceFilename ().c_str ());
195  if (!outFile.is_open ())
196  {
197  NS_LOG_ERROR ("Can't open file " << GetInterferenceFilename ().c_str ());
198  return;
199  }
200  m_InterferenceFirstWrite = false;
201  outFile << "% time\tcellId\tInterference";
202  outFile << std::endl;
203  }
204  else
205  {
206  outFile.open (GetInterferenceFilename ().c_str (), std::ios_base::app);
207  if (!outFile.is_open ())
208  {
209  NS_LOG_ERROR ("Can't open file " << GetInterferenceFilename ().c_str ());
210  return;
211  }
212  }
213 
214  outFile << Simulator::Now ().GetNanoSeconds () / (double) 1e9 << "\t";
215  outFile << cellId << "\t";
216  outFile << *interference;
217  outFile.close ();
218 }
219 
220 
221 void
223  std::string path, uint16_t cellId, uint16_t rnti,
224  double rsrp, double sinr)
225 {
226  NS_LOG_FUNCTION (phyStats << path);
227  uint64_t imsi = 0;
228  std::string pathUePhy = path.substr (0, path.find ("/ReportCurrentCellRsrpSinr"));
229  if (phyStats->ExistsImsiPath (pathUePhy) == true)
230  {
231  imsi = phyStats->GetImsiPath (pathUePhy);
232  }
233  else
234  {
235  imsi = FindImsiFromUePhy (pathUePhy);
236  phyStats->SetImsiPath (pathUePhy, imsi);
237  }
238 
239  phyStats->ReportCurrentCellRsrpSinr (cellId, imsi, rnti, rsrp,sinr);
240 }
241 
242 void
244  uint16_t cellId, uint16_t rnti, double sinrLinear)
245 {
246  NS_LOG_FUNCTION (phyStats << path);
247 
248  uint64_t imsi = 0;
249  std::ostringstream pathAndRnti;
250  pathAndRnti << path << "/" << rnti;
251  std::string pathEnbMac = path.substr (0, path.find ("LteEnbPhy/ReportUeSinr"));
252  pathEnbMac += "LteEnbMac/DlScheduling";
253  if (phyStats->ExistsImsiPath (pathAndRnti.str ()) == true)
254  {
255  imsi = phyStats->GetImsiPath (pathAndRnti.str ());
256  }
257  else
258  {
259  imsi = FindImsiFromEnbMac (pathEnbMac, rnti);
260  phyStats->SetImsiPath (pathAndRnti.str (), imsi);
261  }
262 
263  phyStats->ReportUeSinr (cellId, imsi, rnti, sinrLinear);
264 }
265 
266 void
268  uint16_t cellId, Ptr<SpectrumValue> interference)
269 {
270  NS_LOG_FUNCTION (phyStats << path);
271  phyStats->ReportInterference (cellId, interference);
272 }
273 
274 
275 } // namespace ns3
Ptr< const AttributeChecker > MakeStringChecker(void)
Definition: string.cc:30
Base class for ***StatsCalculator classes.
static uint64_t FindImsiFromUePhy(std::string path)
Retrieves IMSI from Ue PHY path in the attribute system.
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by "...
static uint64_t FindImsiFromEnbMac(std::string path, uint16_t rnti)
Retrieves IMSI from Enb MAC path in the attribute system.
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:44
Hold variables of type string.
Definition: string.h:41
std::string GetInterferenceFilename(void)
Get the name of the file where the interference statistics will be stored.
std::string m_ueSinrFilename
Name of the file where the UE SINR statistics will be saved.
void ReportUeSinr(uint16_t cellId, uint64_t imsi, uint16_t rnti, double sinrLinear)
Notifies the stats calculator that an UE SINR report has occurred.
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:201
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition: log.h:244
void SetCurrentCellRsrpSinrFilename(std::string filename)
Set the name of the file where the RSRP/SINR statistics will be stored.
bool m_RsrpSinrFirstWrite
When writing RSRP SINR statistics first time to file, columns description is added.
static TypeId GetTypeId(void)
Register this type.
static void ReportCurrentCellRsrpSinrCallback(Ptr< PhyStatsCalculator > phyStats, std::string path, uint16_t cellId, uint16_t rnti, double rsrp, double sinr)
trace sink
std::string m_interferenceFilename
Name of the file where the interference statistics will be saved.
bool m_InterferenceFirstWrite
When writing interference statistics first time to file, columns description is added.
void SetUeSinrFilename(std::string filename)
Set the name of the file where the UE SINR statistics will be stored.
virtual ~PhyStatsCalculator()
Destructor.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
static Time Now(void)
Return the current simulation virtual time.
Definition: simulator.cc:223
int64_t GetNanoSeconds(void) const
Get an approximation of the time stored in this instance in the indicated unit.
Definition: nstime.h:339
std::string m_RsrpSinrFilename
Name of the file where the RSRP/SINR statistics will be saved.
void SetInterferenceFilename(std::string filename)
Set the name of the file where the interference statistics will be stored.
std::string GetUeSinrFilename(void)
Get the name of the file where the UE SINR statistics will be stored.
void ReportInterference(uint16_t cellId, Ptr< SpectrumValue > interference)
Notifies the stats calculator that an interference report has occurred.
#define NS_LOG_ERROR(msg)
Use NS_LOG to output a message of level LOG_ERROR.
Definition: log.h:220
Ptr< const AttributeAccessor > MakeStringAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method...
Definition: string.h:42
a unique identifier for an interface.
Definition: type-id.h:51
TypeId SetParent(TypeId tid)
Definition: type-id.cc:631
std::string GetCurrentCellRsrpSinrFilename(void)
Get the name of the file where the RSRP/SINR statistics will be stored.
bool m_UeSinrFirstWrite
When writing UE SINR statistics first time to file, columns description is added. ...
void ReportCurrentCellRsrpSinr(uint16_t cellId, uint64_t imsi, uint16_t rnti, double rsrp, double sinr)
Notifies the stats calculator that an RSRP and SINR report has occurred.