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  *         Nicola Baldo <nbaldo@cttc.es>
20  */
21 
22 #ifndef RADIO_BEARER_STATS_CALCULATOR_H_
23 #define RADIO_BEARER_STATS_CALCULATOR_H_
24 
25 #include "ns3/lte-stats-calculator.h"
26 #include "ns3/lte-common.h"
27 #include "ns3/uinteger.h"
28 #include "ns3/object.h"
29 #include "ns3/basic-data-calculators.h"
30 #include "ns3/lte-common.h"
31 #include <string>
32 #include <map>
33 #include <fstream>
34 
35 namespace ns3
36 {
37 /// Container: (IMSI, LCID) pair, uint32_t
38 typedef std::map<ImsiLcidPair_t, uint32_t> Uint32Map;
39 /// Container: (IMSI, LCID) pair, uint64_t
40 typedef std::map<ImsiLcidPair_t, uint64_t> Uint64Map;
41 /// Container: (IMSI, LCID) pair, uint32_t calculator
42 typedef std::map<ImsiLcidPair_t, Ptr<MinMaxAvgTotalCalculator<uint32_t> > > Uint32StatsMap;
43 /// Container: (IMSI, LCID) pair, uint64_t calculator
44 typedef std::map<ImsiLcidPair_t, Ptr<MinMaxAvgTotalCalculator<uint64_t> > > Uint64StatsMap;
45 /// Container: (IMSI, LCID) pair, double
46 typedef std::map<ImsiLcidPair_t, double> DoubleMap;
47 /// Container: (IMSI, LCID) pair, LteFlowId_t
48 typedef std::map<ImsiLcidPair_t, LteFlowId_t> FlowIdMap;
49 
50 /**
51  * \ingroup lte
52  *
53  * This class is an ns-3 trace sink that performs the calculation of
54  * PDU statistics for uplink and downlink. Statistics are generated
55  * on a per radio bearer basis. This class can be used for
56  * RLC PDU stats or PDCP PDU stats by connecting to the appropriate
57  * trace sources at the RLC or PDCP layer.
58  *
59  * The statistics are calculated at consecutive time windows and
60  * periodically written to a file. The calculated statistics are:
61  *
62  *   - Number of transmitted PDUs
63  *   - Number of received PDUs
64  *   - Number of transmitted bytes
65  *   - Number of received bytes
66  *   - Average, min, max and standard deviation of PDU delay (delay is
67  *     calculated from the generation of the PDU to its reception)
68  *   - Average, min, max and standard deviation of PDU size
69  */
70 class RadioBearerStatsCalculator : public LteStatsCalculator
71 {
72 public:
73   /**
74    * Class constructor
75    */
76   RadioBearerStatsCalculator ();
77 
78   /**
79     * Class constructor
80     * \param protocolType the name of the protocol type
81     */
82   RadioBearerStatsCalculator (std::string protocolType);
83 
84   /**
85    * Class destructor
86    */
87   virtual
88   ~RadioBearerStatsCalculator ();
89 
90   // Inherited from ns3::Object
91   /**
92    *  Register this type.
93    *  \return The object TypeId.
94    */
95   static TypeId GetTypeId (void);
96   void DoDispose ();
97 
98   /**
99    * Get the name of the file where the uplink statistics will be stored.
100    * @return the name of the file where the uplink statistics will be stored
101    */
102   std::string GetUlOutputFilename (void);
103 
104   /**
105    * Get the name of the file where the downlink statistics will be stored.
106    * @return the name of the file where the downlink statistics will be stored
107    */
108   std::string GetDlOutputFilename (void);
109 
110   /**
111    * Set the name of the file where the uplink PDCP statistics will be stored.
112    *
113    * @param outputFilename string with the name of the file
114    */
115   void SetUlPdcpOutputFilename (std::string outputFilename);
116 
117   /**
118    * Get the name of the file where the uplink PDCP statistics will be stored.
119    * @return the name of the file where the uplink PDCP statistics will be stored
120    */
121   std::string GetUlPdcpOutputFilename (void);
122 
123   /**
124    * Set the name of the file where the downlink PDCP statistics will be stored.
125    *
126    * @param outputFilename string with the name of the file
127    */
128   void SetDlPdcpOutputFilename (std::string outputFilename);
129 
130   /**
131    * Get the name of the file where the downlink PDCP statistics will be stored.
132    * @return the name of the file where the downlink PDCP statistics will be stored
133    */
134   std::string GetDlPdcpOutputFilename (void);
135 
136 
137   /**
138    *
139    * \param t the value of the StartTime attribute
140    */
141   void SetStartTime (Time t);
142 
143   /**
144    *
145    * \return the value of the StartTime attribute
146    */
147   Time GetStartTime () const;
148 
149   /**
150    *
151    * \param e the epoch duration
152    */
153   void SetEpoch (Time e);
154 
155   /**
156    *
157    * \return the epoch duration
158    */
159   Time GetEpoch () const;
160 
161   /**
162    * Notifies the stats calculator that an uplink transmission has occurred.
163    * @param cellId CellId of the attached Enb
164    * @param imsi IMSI of the UE who transmitted the PDU
165    * @param rnti C-RNTI of the UE who transmitted the PDU
166    * @param lcid LCID through which the PDU has been transmitted
167    * @param packetSize size of the PDU in bytes
168    */
169   void
170   UlTxPdu (uint16_t cellId, uint64_t imsi, uint16_t rnti, uint8_t lcid, uint32_t packetSize);
171 
172   /**
173    * Notifies the stats calculator that an uplink reception has occurred.
174    * @param cellId CellId of the attached Enb
175    * @param imsi IMSI of the UE who received the PDU
176    * @param rnti C-RNTI of the UE who received the PDU
177    * @param lcid LCID through which the PDU has been received
178    * @param packetSize size of the PDU in bytes
179    * @param delay RLC to RLC delay in nanoseconds
180    */
181   void
182   UlRxPdu (uint16_t cellId, uint64_t imsi, uint16_t rnti, uint8_t lcid, uint32_t packetSize, uint64_t delay);
183 
184   /**
185    * Notifies the stats calculator that an downlink transmission has occurred.
186    * @param cellId CellId of the attached Enb
187    * @param imsi IMSI of the UE who is receiving the PDU
188    * @param rnti C-RNTI of the UE who is receiving the PDU
189    * @param lcid LCID through which the PDU has been transmitted
190    * @param packetSize size of the PDU in bytes
191    */
192   void
193   DlTxPdu (uint16_t cellId, uint64_t imsi, uint16_t rnti, uint8_t lcid, uint32_t packetSize);
194 
195   /**
196    * Notifies the stats calculator that an downlink reception has occurred.
197    * @param cellId CellId of the attached Enb
198    * @param imsi IMSI of the UE who received the PDU
199    * @param rnti C-RNTI of the UE who received the PDU
200    * @param lcid LCID through which the PDU has been transmitted
201    * @param packetSize size of the PDU in bytes
202    * @param delay RLC to RLC delay in nanoseconds
203    */
204   void
205   DlRxPdu (uint16_t cellId, uint64_t imsi, uint16_t rnti, uint8_t lcid, uint32_t packetSize, uint64_t delay);
206 
207   /**
208    * Gets the number of transmitted uplink packets.
209    * @param imsi IMSI of the UE
210    * @param lcid LCID
211    * @return number of transmitted uplink packets
212    */
213   uint32_t
214   GetUlTxPackets (uint64_t imsi, uint8_t lcid);
215 
216   /**
217    * Gets the number of received uplink packets.
218    * @param imsi IMSI of the UE
219    * @param lcid LCID
220    * @return number of received uplink packets
221    */
222   uint32_t
223   GetUlRxPackets (uint64_t imsi, uint8_t lcid);
224 
225   /**
226    * Gets the number of transmitted uplink data bytes.
227    * @param imsi IMSI of the UE
228    * @param lcid LCID
229    * @return number of transmitted data bytes
230    */
231   uint64_t
232   GetUlTxData (uint64_t imsi, uint8_t lcid);
233 
234   /**
235    * Gets the number of received uplink data bytes.
236    * @param imsi IMSI of the UE
237    * @param lcid LCID
238    * @return number of received data bytes
239    */
240   uint64_t
241   GetUlRxData (uint64_t imsi, uint8_t lcid);
242 
243   /**
244    * Gets the attached Enb cellId.
245    * @param imsi IMSI of the UE
246    * @param lcid LCID
247    * @return Enb cellId
248    */
249   uint32_t
250   GetUlCellId (uint64_t imsi, uint8_t lcid);
251 
252   /**
253    * Gets the uplink RLC to RLC delay
254    * @param imsi IMSI of the UE
255    * @param lcid LCID
256    * @return RLC to RLC delay in seconds
257    */
258   double
259   GetUlDelay (uint64_t imsi, uint8_t lcid);
260 
261   /**
262    * Gets the uplink RLC to RLC statistics: average, min, max and standard deviation.
263    * @param imsi IMSI of the UE
264    * @param lcid LCID
265    * @return RLC to RLC delay statistics average, min, max and standard deviation in seconds
266    */
267   std::vector<double>
268   GetUlDelayStats (uint64_t imsi, uint8_t lcid);
269 
270   /**
271    * Gets the uplink PDU size statistics: average, min, max and standard deviation.
272    * @param imsi IMSI of the UE
273    * @param lcid LCID
274    * @return PDU size statistics average, min, max and standard deviation in seconds
275    */
276   std::vector<double>
277   GetUlPduSizeStats (uint64_t imsi, uint8_t lcid);
278 
279   /**
280    * Gets the number of transmitted downlink data bytes.
281    * @param imsi IMSI of the UE
282    * @param lcid LCID
283    * @return number of transmitted data bytes
284    */
285   uint32_t
286   GetDlTxPackets (uint64_t imsi, uint8_t lcid);
287 
288   /**
289    * Gets the number of received downlink data bytes.
290    * @param imsi IMSI of the UE
291    * @param lcid LCID
292    * @return number of received data bytes
293    */
294   uint32_t
295   GetDlRxPackets (uint64_t imsi, uint8_t lcid);
296 
297   /**
298    * Gets the number of transmitted downlink data bytes.
299    * @param imsi IMSI of the UE
300    * @param lcid LCID
301    * @return number of transmitted data bytes
302    */
303   uint64_t
304   GetDlTxData (uint64_t imsi, uint8_t lcid);
305 
306   /**
307    * Gets the number of received downlink data bytes.
308    * @param imsi IMSI of the UE
309    * @param lcid LCID
310    * @return number of received data bytes
311    */
312   uint64_t
313   GetDlRxData (uint64_t imsi, uint8_t lcid);
314 
315   /**
316    * Gets the attached Enb cellId.
317    * @param imsi IMSI of the UE
318    * @param lcid LCID
319    * @return Enb cellId
320    */
321   uint32_t
322   GetDlCellId (uint64_t imsi, uint8_t lcid);
323 
324   /**
325    * Gets the downlink RLC to RLC delay
326    * @param imsi IMSI of the UE
327    * @param lcid LCID
328    * @return RLC to RLC delay in seconds
329    */
330   double
331   GetDlDelay (uint64_t imsi, uint8_t lcid);
332 
333   /**
334    * Gets the downlink RLC to RLC statistics: average, min, max and standard deviation.
335    * @param imsi IMSI of the UE
336    * @param lcid LCID
337    * @return RLC to RLC delay statistics average, min, max and standard deviation in seconds
338    */
339   std::vector<double>
340   GetDlDelayStats (uint64_t imsi, uint8_t lcid);
341 
342   /**
343    * Gets the downlink PDU size statistics: average, min, max and standard deviation.
344    * @param imsi IMSI of the UE
345    * @param lcid LCID
346    * @return PDU size statistics average, min, max and standard deviation in seconds
347    */
348   std::vector<double>
349   GetDlPduSizeStats (uint64_t imsi, uint8_t lcid);
350 
351 private:
352   /**
353    * Called after each epoch to write collected
354    * statistics to output files. During first call
355    * it opens output files and write columns descriptions.
356    * During next calls it opens output files in append mode.
357    */
358   void
359   ShowResults (void);
360 
361   /**
362    * Writes collected statistics to UL output file and
363    * closes UL output file.
364    * @param outFile ofstream for UL statistics
365    */
366   void
367   WriteUlResults (std::ofstream& outFile);
368 
369   /**
370    * Writes collected statistics to DL output file and
371    * closes DL output file.
372    * @param outFile ofstream for DL statistics
373    */
374   void
375   WriteDlResults (std::ofstream& outFile);
376 
377   /**
378    * Erases collected statistics
379    */
380   void
381   ResetResults (void);
382 
383   /**
384    * Reschedules EndEpoch event. Usually used after
385    * execution of SetStartTime() or SetEpoch()
386    */
387   void RescheduleEndEpoch ();
388 
389   /**
390    * Function called in every endEpochEvent. It calls
391    * ShowResults() to write statistics to output files
392    * and ResetResults() to clear collected statistics.
393    */
394   void EndEpoch (void);
395 
396   EventId m_endEpochEvent; //!< Event id for next end epoch event
397 
398   FlowIdMap m_flowId; //!< List of FlowIds, ie. (RNTI, LCID) by (IMSI, LCID) pair
399 
400   Uint32Map m_dlCellId; //!< List of DL CellIds by (IMSI, LCID) pair
401   Uint32Map m_dlTxPackets; //!< Number of DL TX Packets by (IMSI, LCID) pair
402   Uint32Map m_dlRxPackets; //!< Number of DL RX Packets by (IMSI, LCID) pair
403   Uint64Map m_dlTxData; //!< Amount of DL TX Data by (IMSI, LCID) pair
404   Uint64Map m_dlRxData; //!< Amount of DL RX Data by (IMSI, LCID) pair
405   Uint64StatsMap m_dlDelay; //!< DL delay by (IMSI, LCID) pair
406   Uint32StatsMap m_dlPduSize; //!< DL PDU Size by (IMSI, LCID) pair
407 
408   Uint32Map m_ulCellId; //!< List of UL CellIds by (IMSI, LCID) pair
409   Uint32Map m_ulTxPackets; //!< Number of UL TX Packets by (IMSI, LCID) pair
410   Uint32Map m_ulRxPackets; //!< Number of UL RX Packets by (IMSI, LCID) pair
411   Uint64Map m_ulTxData; //!< Amount of UL TX Data by (IMSI, LCID) pair
412   Uint64Map m_ulRxData; //!< Amount of UL RX Data by (IMSI, LCID) pair
413   Uint64StatsMap m_ulDelay; //!< UL delay by (IMSI, LCID) pair
414   Uint32StatsMap m_ulPduSize; //!< UL PDU Size by (IMSI, LCID) pair
415 
416   /**
417    * Start time of the on going epoch
418    */
419   Time m_startTime;
420 
421   /**
422    * Epoch duration
423    */
424   Time m_epochDuration;
425 
426   /**
427    * true if output files have not been opened yet
428    */
429   bool m_firstWrite;
430 
431   /**
432    * true if any output is pending
433    */
434   bool m_pendingOutput;
435 
436   /**
437    * Protocol type, by default RLC
438    */
439   std::string m_protocolType;
440 
441   /**
442    * Name of the file where the downlink PDCP statistics will be saved
443    */
444   std::string m_dlPdcpOutputFilename;
445 
446   /**
447    * Name of the file where the uplink PDCP statistics will be saved
448    */
449   std::string m_ulPdcpOutputFilename;
450 
451 };
452 
453 } // namespace ns3
454 
455 #endif /* RADIO_BEARER_STATS_CALCULATOR_H_ */
456