blob: e97ba18143d3c18c2e3874e6c396afaf4bb79c8c [file] [log] [blame]
Alexander Afanasyev7e71c752012-01-25 21:40:39 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2/*
3 * Copyright (c) 2011 UCLA
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: Alexander Afanasyev <alexander.afanasyev@ucla.edu>
19 */
20
21#include "ipv4-rate-l3-tracer.h"
22#include "ns3/node.h"
23#include "ns3/packet.h"
24#include "ns3/config.h"
25#include "ns3/callback.h"
26#include "ns3/simulator.h"
27
28#include "ns3/ipv4-header.h"
29
30namespace ns3 {
31
32Ipv4RateL3Tracer::Ipv4RateL3Tracer (std::ostream &os, Ptr<Node> node)
33 : Ipv4L3Tracer (node)
34 , m_os (os)
35{
36 SetAveragingPeriod (Seconds (1.0));
37}
38
39Ipv4RateL3Tracer::~Ipv4RateL3Tracer ()
40{
41 m_printEvent.Cancel ();
42}
43
44void
45Ipv4RateL3Tracer::SetAveragingPeriod (const Time &period)
46{
47 m_period = period;
48 m_printEvent.Cancel ();
49 m_printEvent = Simulator::Schedule (m_period, &Ipv4RateL3Tracer::PeriodicPrinter, this);
50}
51
52void
53Ipv4RateL3Tracer::PeriodicPrinter ()
54{
55 Print (m_os);
56 Reset ();
57
58 m_printEvent = Simulator::Schedule (m_period, &Ipv4RateL3Tracer::PeriodicPrinter, this);
59}
60
61void
62Ipv4RateL3Tracer::PrintHeader (std::ostream &os) const
63{
64 os << "Time" << "\t"
65
66 << "Node" << "\t"
67 << "Interface" << "\t"
68
69 << "Type" << "\t"
70 << "Packets" << "\t"
71 << "Kilobytes";
72}
73
74void
75Ipv4RateL3Tracer::Reset ()
76{
77 for (std::map<uint32_t, boost::tuple<Stats, Stats, Stats, Stats> >::iterator stats = m_stats.begin ();
78 stats != m_stats.end ();
79 stats++)
80 {
81 stats->second.get<0> ().Reset ();
82 stats->second.get<1> ().Reset ();
83 }
84}
85
86const double alpha = 0.8;
87
88#define STATS(INDEX) stats->second.get<INDEX> ()
89#define RATE(INDEX, fieldName) STATS(INDEX).fieldName / m_period.ToDouble (Time::S)
90
91#define PRINTER(printName, fieldName) \
92STATS(2).fieldName = /*new value*/alpha * RATE(0, fieldName) + /*old value*/(1-alpha) * STATS(2).fieldName; \
93STATS(3).fieldName = /*new value*/alpha * RATE(1, fieldName) / 1024.0 + /*old value*/(1-alpha) * STATS(3).fieldName; \
94 \
95os << time.ToDouble (Time::S) << "\t" \
96 << m_node << "\t" \
97 << stats->first << "\t" \
98 << printName << "\t" \
99 << STATS(2).fieldName << "\t" \
100 << STATS(3).fieldName << "\n";
101
102void
103Ipv4RateL3Tracer::Print (std::ostream &os) const
104{
105 for (std::map<uint32_t, boost::tuple<Stats, Stats, Stats, Stats> >::iterator stats = m_stats.begin ();
106 stats != m_stats.end ();
107 stats++)
108 {
109 Time time = Simulator::Now ();
110
111 PRINTER ("In", m_in);
112 PRINTER ("Out", m_out);
113 PRINTER ("Drop", m_drop);
114 }
115}
116
117void
118Ipv4RateL3Tracer::Rx (std::string context,
119 Ptr<const Packet> packet, Ptr<Ipv4> ipv4, uint32_t iface)
120{
121 m_stats[iface].get<0> ().m_in ++;
122 m_stats[iface].get<1> ().m_in += packet->GetSize ();
123}
124
125void
126Ipv4RateL3Tracer::Tx (std::string context,
127 Ptr<const Packet> packet, Ptr<Ipv4> ipv4, uint32_t iface)
128{
129 m_stats[iface].get<0> ().m_out ++;
130 m_stats[iface].get<1> ().m_out += packet->GetSize ();
131}
132
133void
134Ipv4RateL3Tracer::Drop (std::string context,
135 const Ipv4Header &header, Ptr<const Packet> packet, Ipv4L3Protocol::DropReason, Ptr<Ipv4> ipv4, uint32_t iface)
136{
137 m_stats[iface].get<0> ().m_drop ++;
138 m_stats[iface].get<1> ().m_drop += packet->GetSize ();
139}
140
141
142} // namespace ns3