A Discrete-Event Network Simulator
API
mac-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 "mac-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 ("MacStatsCalculator");
29 
30 NS_OBJECT_ENSURE_REGISTERED (MacStatsCalculator);
31 
33  : m_dlFirstWrite (true),
34  m_ulFirstWrite (true)
35 {
36  NS_LOG_FUNCTION (this);
37 
38 }
39 
41 {
42  NS_LOG_FUNCTION (this);
43 }
44 
45 TypeId
47 {
48  static TypeId tid = TypeId ("ns3::MacStatsCalculator")
50  .SetGroupName("Lte")
51  .AddConstructor<MacStatsCalculator> ()
52  .AddAttribute ("DlOutputFilename",
53  "Name of the file where the downlink results will be saved.",
54  StringValue ("DlMacStats.txt"),
57  .AddAttribute ("UlOutputFilename",
58  "Name of the file where the uplink results will be saved.",
59  StringValue ("UlMacStats.txt"),
62  ;
63  return tid;
64 }
65 
66 void
67 MacStatsCalculator::SetUlOutputFilename (std::string outputFilename)
68 {
70 }
71 
72 std::string
74 {
76 }
77 
78 void
79 MacStatsCalculator::SetDlOutputFilename (std::string outputFilename)
80 {
82 }
83 
84 std::string
86 {
88 }
89 
90 void
91 MacStatsCalculator::DlScheduling (uint16_t cellId, uint64_t imsi, uint32_t frameNo, uint32_t subframeNo,
92  uint16_t rnti, uint8_t mcsTb1, uint16_t sizeTb1, uint8_t mcsTb2, uint16_t sizeTb2)
93 {
94  NS_LOG_FUNCTION (this << cellId << imsi << frameNo << subframeNo << rnti << (uint32_t) mcsTb1 << sizeTb1 << (uint32_t) mcsTb2 << sizeTb2);
95  NS_LOG_INFO ("Write DL Mac Stats in " << GetDlOutputFilename ().c_str ());
96 
97  std::ofstream outFile;
98  if ( m_dlFirstWrite == true )
99  {
100  outFile.open (GetDlOutputFilename ().c_str ());
101  if (!outFile.is_open ())
102  {
103  NS_LOG_ERROR ("Can't open file " << GetDlOutputFilename ().c_str ());
104  return;
105  }
106  m_dlFirstWrite = false;
107  outFile << "% time\tcellId\tIMSI\tframe\tsframe\tRNTI\tmcsTb1\tsizeTb1\tmcsTb2\tsizeTb2";
108  outFile << std::endl;
109  }
110  else
111  {
112  outFile.open (GetDlOutputFilename ().c_str (), std::ios_base::app);
113  if (!outFile.is_open ())
114  {
115  NS_LOG_ERROR ("Can't open file " << GetDlOutputFilename ().c_str ());
116  return;
117  }
118  }
119 
120  outFile << Simulator::Now ().GetNanoSeconds () / (double) 1e9 << "\t";
121  outFile << (uint32_t) cellId << "\t";
122  outFile << imsi << "\t";
123  outFile << frameNo << "\t";
124  outFile << subframeNo << "\t";
125  outFile << rnti << "\t";
126  outFile << (uint32_t) mcsTb1 << "\t";
127  outFile << sizeTb1 << "\t";
128  outFile << (uint32_t) mcsTb2 << "\t";
129  outFile << sizeTb2 << std::endl;
130  outFile.close ();
131 }
132 
133 void
134 MacStatsCalculator::UlScheduling (uint16_t cellId, uint64_t imsi, uint32_t frameNo,
135  uint32_t subframeNo, uint16_t rnti,uint8_t mcsTb, uint16_t size)
136 {
137  NS_LOG_FUNCTION (this << cellId << imsi << frameNo << subframeNo << rnti << (uint32_t) mcsTb << size);
138  NS_LOG_INFO ("Write UL Mac Stats in " << GetUlOutputFilename ().c_str ());
139 
140  std::ofstream outFile;
141  if ( m_ulFirstWrite == true )
142  {
143  outFile.open (GetUlOutputFilename ().c_str ());
144  if (!outFile.is_open ())
145  {
146  NS_LOG_ERROR ("Can't open file " << GetUlOutputFilename ().c_str ());
147  return;
148  }
149  m_ulFirstWrite = false;
150  outFile << "% time\tcellId\tIMSI\tframe\tsframe\tRNTI\tmcs\tsize";
151  outFile << std::endl;
152  }
153  else
154  {
155  outFile.open (GetUlOutputFilename ().c_str (), std::ios_base::app);
156  if (!outFile.is_open ())
157  {
158  NS_LOG_ERROR ("Can't open file " << GetUlOutputFilename ().c_str ());
159  return;
160  }
161  }
162 
163  outFile << Simulator::Now ().GetNanoSeconds () / (double) 1e9 << "\t";
164  outFile << (uint32_t) cellId << "\t";
165  outFile << imsi << "\t";
166  outFile << frameNo << "\t";
167  outFile << subframeNo << "\t";
168  outFile << rnti << "\t";
169  outFile << (uint32_t) mcsTb << "\t";
170  outFile << size << std::endl;
171  outFile.close ();
172 }
173 
174 void
176  std::string path, uint32_t frameNo, uint32_t subframeNo,
177  uint16_t rnti, uint8_t mcsTb1, uint16_t sizeTb1,
178  uint8_t mcsTb2, uint16_t sizeTb2)
179 {
180  NS_LOG_FUNCTION (macStats << path);
181  uint64_t imsi = 0;
182  std::ostringstream pathAndRnti;
183  pathAndRnti << path << "/" << rnti;
184  if (macStats->ExistsImsiPath (pathAndRnti.str ()) == true)
185  {
186  imsi = macStats->GetImsiPath (pathAndRnti.str ());
187  }
188  else
189  {
190  imsi = FindImsiFromEnbMac (path, rnti);
191  macStats->SetImsiPath (pathAndRnti.str (), imsi);
192  }
193 
194  uint16_t cellId = 0;
195  if (macStats->ExistsCellIdPath (pathAndRnti.str ()) == true)
196  {
197  cellId = macStats->GetCellIdPath (pathAndRnti.str ());
198  }
199  else
200  {
201  cellId = FindCellIdFromEnbMac (path, rnti);
202  macStats->SetCellIdPath (pathAndRnti.str (), cellId);
203  }
204 
205  macStats->DlScheduling (cellId, imsi, frameNo, subframeNo, rnti, mcsTb1, sizeTb1, mcsTb2, sizeTb2);
206 }
207 
208 void
210  uint32_t frameNo, uint32_t subframeNo, uint16_t rnti,
211  uint8_t mcs, uint16_t size)
212 {
213  NS_LOG_FUNCTION (macStats << path);
214 
215  uint64_t imsi = 0;
216  std::ostringstream pathAndRnti;
217  pathAndRnti << path << "/" << rnti;
218  if (macStats->ExistsImsiPath (pathAndRnti.str ()) == true)
219  {
220  imsi = macStats->GetImsiPath (pathAndRnti.str ());
221  }
222  else
223  {
224  imsi = FindImsiFromEnbMac (path, rnti);
225  macStats->SetImsiPath (pathAndRnti.str (), imsi);
226  }
227  uint16_t cellId = 0;
228  if (macStats->ExistsCellIdPath (pathAndRnti.str ()) == true)
229  {
230  cellId = macStats->GetCellIdPath (pathAndRnti.str ());
231  }
232  else
233  {
234  cellId = FindCellIdFromEnbMac (path, rnti);
235  macStats->SetCellIdPath (pathAndRnti.str (), cellId);
236  }
237 
238  macStats->UlScheduling (cellId, imsi, frameNo, subframeNo, rnti, mcs, size);
239 }
240 
241 
242 } // namespace ns3
Ptr< const AttributeChecker > MakeStringChecker(void)
Definition: string.cc:30
Base class for ***StatsCalculator classes.
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:73
#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
static void DlSchedulingCallback(Ptr< MacStatsCalculator > macStats, std::string path, uint32_t frameNo, uint32_t subframeNo, uint16_t rnti, uint8_t mcsTb1, uint16_t sizeTb1, uint8_t mcsTb2, uint16_t sizeTb2)
Trace sink for the ns3::LteEnbMac::DlScheduling trace source.
#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
Takes care of storing the information generated at MAC layer.
static uint16_t FindCellIdFromEnbMac(std::string path, uint16_t rnti)
Retrieves CellId from Enb MAC path in the attribute system.
bool m_ulFirstWrite
When writing UL MAC statistics first time to file, columns description is added.
void SetUlOutputFilename(std::string outputFilename)
Set the name of the file where the uplink statistics will be stored.
void SetDlOutputFilename(std::string outputFilename)
Set the name of the file where the downlink statistics will be stored.
static TypeId GetTypeId(void)
Register this type.
std::string GetUlOutputFilename(void)
Get the name of the file where the uplink statistics will be stored.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
void DlScheduling(uint16_t cellId, uint64_t imsi, uint32_t frameNo, uint32_t subframeNo, uint16_t rnti, uint8_t mcsTb1, uint16_t sizeTb1, uint8_t mcsTb2, uint16_t sizeTb2)
Notifies the stats calculator that an downlink scheduling has occurred.
void SetDlOutputFilename(std::string outputFilename)
Set the name of the file where the downlink statistics will be stored.
std::string GetDlOutputFilename(void)
Get the name of the file where the downlink statistics will be stored.
bool m_dlFirstWrite
When writing DL MAC statistics first time to file, columns description is added.
static Time Now(void)
Return the current simulation virtual time.
Definition: simulator.cc:224
static void UlSchedulingCallback(Ptr< MacStatsCalculator > macStats, std::string path, uint32_t frameNo, uint32_t subframeNo, uint16_t rnti, uint8_t mcs, uint16_t size)
Trace sink for the ns3::LteEnbMac::UlScheduling trace source.
int64_t GetNanoSeconds(void) const
Get an approximation of the time stored in this instance in the indicated unit.
Definition: nstime.h:353
std::string GetUlOutputFilename(void)
Get the name of the file where the uplink statistics will be stored.
void UlScheduling(uint16_t cellId, uint64_t imsi, uint32_t frameNo, uint32_t subframeNo, uint16_t rnti, uint8_t mcsTb, uint16_t sizeTb)
Notifies the stats calculator that an uplink scheduling has occurred.
#define NS_LOG_ERROR(msg)
Use NS_LOG to output a message of level LOG_ERROR.
Definition: log.h:220
virtual ~MacStatsCalculator()
Destructor.
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:58
void SetUlOutputFilename(std::string outputFilename)
Set the name of the file where the uplink statistics will be stored.
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:904
std::string GetDlOutputFilename(void)
Get the name of the file where the downlink statistics will be stored.