blob: da86f31daec8e9734fb91c5a1be43c7bdddc7848 [file] [log] [blame]
Spyridon Mastorakisb4bd4b72015-01-05 17:41:12 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2011-2015 Regents of the University of California.
4 *
5 * This file is part of ndnSIM. See AUTHORS for complete list of ndnSIM authors and
6 * contributors.
7 *
8 * ndnSIM is free software: you can redistribute it and/or modify it under the terms
9 * of the GNU General Public License as published by the Free Software Foundation,
10 * either version 3 of the License, or (at your option) any later version.
11 *
12 * ndnSIM is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
13 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along with
17 * ndnSIM, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
18 **/
19
20// ndn-test.cpp
21
22#include "ns3/core-module.h"
23#include "ns3/network-module.h"
24#include "ns3/point-to-point-module.h"
25#include "ns3/ndnSIM-module.h"
26
27#include <sys/time.h>
28#include "ns3/ndnSIM/utils/mem-usage.hpp"
Spyridon Mastorakisb4bd4b72015-01-05 17:41:12 -080029#include "ns3/ndnSIM/utils/mem-usage.hpp"
30
31namespace ns3 {
32
33/**
34 * This scenario simulates a very simple network topology:
35 *
36 *
37 * +----------+ 10000Mbps +----------+
38 * | consumer | <------------> | producer |
39 * +----------+ 10ms +----------+
40 *
41 *
42 * NS_LOG=ndn.Consumer ./waf --run ndn-test
43 */
44
45class Tester {
46public:
47 Tester()
48 : m_csSize(100)
49 , m_interestRate(1000)
50 , m_shouldEvaluatePit(false)
51 , m_simulationTime(Seconds(2000) / m_interestRate)
52 {
53 }
54
55 int
56 run(int argc, char* argv[]);
57
58 void
59 printHeader(std::ostream& os);
60
61 void
62 printStats(std::ostream& os, Time nextPrintTime, double beginRealTime);
63
64private:
Spyridon Mastorakisb4bd4b72015-01-05 17:41:12 -080065 size_t m_csSize;
66 double m_interestRate;
67 bool m_shouldEvaluatePit;
68 std::string m_strategy;
69 double m_initialOverhead;
70 Time m_simulationTime;
71};
72
73void
74Tester::printHeader(std::ostream& os)
75{
76 m_initialOverhead = MemUsage::Get() / 1024.0 / 1024.0;
77 os << "SimulationTime"
78 << "\t"
79 << "RealTime"
80 << "\t"
81 << "NumberOfInterests (total)"
82 << "\t"
83 << "NumberOfInterests (per real time)"
84 << "\n";
85}
86
87void
88Tester::printStats(std::ostream& os, Time nextPrintTime, double beginRealTime)
89{
90 ::timeval t;
91 gettimeofday(&t, NULL);
92 double realTime = t.tv_sec + (0.000001 * (unsigned)t.tv_usec) - beginRealTime;
93 Time simTime = Simulator::Now();
94
95 os << simTime << "\t";
96 os << realTime << "\t";
97
98 double interestCount = m_interestRate * simTime.ToDouble(Time::S);
99 double nInterestsPerSec = interestCount / realTime;
100
101 os << interestCount << "\t" << nInterestsPerSec << "\t";
102
103 uint64_t pitCount = 0;
104 uint64_t csCount = 0;
105 for (NodeList::Iterator node = NodeList::Begin(); node != NodeList::End(); node++) {
106
107 auto pitSize = (*node)->GetObject<ndn::L3Protocol>()->getForwarder()->getPit().size();
108 if (pitSize != 0)
109 pitCount += pitSize;
110
Alexander Afanasyev25e4d8f2019-07-29 13:44:04 -0400111 auto csSize = (*node)->GetObject<ndn::L3Protocol>()->getForwarder()->getCs().size();
112 if (csSize != 0)
113 csCount += csSize;
Spyridon Mastorakisb4bd4b72015-01-05 17:41:12 -0800114 }
115
116 os << "pit:" << pitCount << "\t";
117 os << "cs:" << csCount << "\t";
118
119 os << MemUsage::Get() / 1024.0 / 1024.0 << "MiB\n";
120
121 if ((simTime + nextPrintTime) >= m_simulationTime) {
122 double finalOverhead = MemUsage::Get() / 1024.0 / 1024.0;
123 if (m_shouldEvaluatePit) {
124 if (pitCount != 0) {
125 os << "Approximate memory overhead per PIT entry:"
126 << 1000 * (finalOverhead - m_initialOverhead) / pitCount << "KiB\n";
127 }
128 else {
129 os << "`The number of PIT entries is equal to zero\n";
130 }
131 }
132 else {
133 if (csCount != 0) {
134 os << "Approximate memory overhead per CS entry:"
135 << 1000 * (finalOverhead - m_initialOverhead) / csCount << "KiB\n";
136 }
137 else {
138 os << "The number of CS entries is equal to zero\n";
139 }
140 }
141 }
142
143 Simulator::Schedule(nextPrintTime, &Tester::printStats, this, ref(os), nextPrintTime,
144 beginRealTime);
145}
146
147int
148Tester::run(int argc, char* argv[])
149{
150 // setting default parameters for PointToPoint links and channels
151 Config::SetDefault("ns3::PointToPointNetDevice::DataRate", StringValue("10000Mbps"));
152 Config::SetDefault("ns3::PointToPointChannel::Delay", StringValue("10ms"));
Alexander Afanasyev77f84c62018-10-08 13:37:42 -0400153 Config::SetDefault("ns3::DropTailQueue::MaxSize", StringValue("20p"));
Spyridon Mastorakisb4bd4b72015-01-05 17:41:12 -0800154
155 // Read optional command-line parameters (e.g., enable visualizer with ./waf --run=<> --visualize
156 CommandLine cmd;
Spyridon Mastorakisb4bd4b72015-01-05 17:41:12 -0800157 cmd.AddValue("cs-size", "Maximum number of cached packets per node", m_csSize);
158 cmd.AddValue("rate", "Interest rate", m_interestRate);
159 cmd.AddValue("pit", "Perform PIT evaluation if this parameter is true",
160 m_shouldEvaluatePit);
161 cmd.AddValue("strategy", "Choose forwarding strategy "
Alexander Afanasyevc3c7f042015-08-21 11:38:00 -0700162 "(e.g., /localhost/nfd/strategy/multicast, "
Spyridon Mastorakisb4bd4b72015-01-05 17:41:12 -0800163 "/localhost/nfd/strategy/best-route, ...) ",
164 m_strategy);
165 cmd.AddValue("sim-time", "Simulation time", m_simulationTime);
166 cmd.Parse(argc, argv);
167
168 // Creating nodes
169 NodeContainer nodes;
170 nodes.Create(2);
171
172 // Connecting nodes using two links
173 PointToPointHelper p2p;
174 p2p.Install(nodes.Get(0), nodes.Get(1));
175
176 // Install NDN stack on all nodes
177 ndn::StackHelper ndnHelper;
178 ndnHelper.setCsSize(m_csSize);
179
Spyridon Mastorakisb4bd4b72015-01-05 17:41:12 -0800180 ndnHelper.InstallAll();
181
182 ndn::FibHelper::AddRoute(nodes.Get(0), "/", nodes.Get(1), 10);
183 if (!m_strategy.empty()) {
184 ndn::StrategyChoiceHelper::InstallAll("/", m_strategy);
185 }
186
187 // Installing applications
188
189 // Consumer
190 ndn::AppHelper consumerHelper("ns3::ndn::ConsumerCbr");
191 // Consumer will request /prefix/0, /prefix/1, ...
192 consumerHelper.SetPrefix("/prefix");
193 consumerHelper.SetAttribute("Frequency", DoubleValue(m_interestRate));
194 consumerHelper.Install(nodes.Get(0)); // first node
195
196 if (!m_shouldEvaluatePit) {
197 // Producer
198 ndn::AppHelper producerHelper("ns3::ndn::Producer");
199 // Producer will reply to all requests starting with /prefix
200 producerHelper.SetPrefix("/prefix");
201 producerHelper.SetAttribute("PayloadSize", StringValue("1024"));
202 producerHelper.Install(nodes.Get(1)); // last node
203 }
204
205 Simulator::Stop(m_simulationTime);
206
207 struct ::timeval t;
208 gettimeofday(&t, NULL);
209 double beginRealTime = t.tv_sec + (0.000001 * (unsigned)t.tv_usec);
210 Simulator::Schedule(Seconds(0), &Tester::printHeader, this, ref(std::cout));
211 Simulator::Schedule(m_simulationTime / 200, &Tester::printStats, this, ref(std::cout),
212 m_simulationTime / 200, beginRealTime);
213
214 L2RateTracer::InstallAll("drop-trace2.txt", Seconds(0.5));
215 Simulator::Run();
216 Simulator::Destroy();
217
218 return 0;
219}
220
221} // namespace ns3
222
223int
224main(int argc, char* argv[])
225{
226 ns3::Tester tester;
227 return tester.run(argc, argv);
228}