blob: 0ed9079bc2a153b603c1da3c7423102851c18478 [file] [log] [blame]
Alexander Afanasyev41684ab2013-02-19 11:02:37 -08001/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */
2/*
3 * Copyright (c) 2012-2013 University of California, Los Angeles
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// ndn-simple-with-cs-lfu.cc
21#include "ns3/core-module.h"
22#include "ns3/network-module.h"
23#include "ns3/point-to-point-module.h"
24#include "ns3/ndnSIM-module.h"
25
26#include <sys/time.h>
27#include "ns3/ndnSIM/utils/mem-usage.h"
28
29using namespace ns3;
30
31/**
32 * This scenario simulates a very simple network topology:
33 *
34 *
35 * +----------+ 1Mbps +--------+ 1Mbps +----------+
36 * | consumer | <------------> | router | <------------> | producer |
37 * +----------+ 10ms +--------+ 10ms +----------+
38 *
Alexander Afanasyev772f51b2013-08-01 18:53:25 -070039 * This scenario demonstrates how to use content store that responds to Freshness parameter set in Datas.
Alexander Afanasyev41684ab2013-02-19 11:02:37 -080040 * That is, if producer set Freshness field to 2 seconds, the corresponding content object will not be cached
41 * more than 2 seconds (can be cached for a shorter time, if entry is evicted earlier)
42 *
43 * NS_LOG=ndn.Consumer ./waf --run ndn-simple-with-cs-lfu
44 */
45
46void PrintCsMemStatsHeader (std::ostream &os)
47{
48 os << "SimulationTime" << "\t"
49 << "RealTime" << "\t"
Alexander Afanasyev5bee19e2013-07-10 14:33:57 -070050 // << "NumberOfProcessedData" << "\t"
51 // << "NumberOfProcessedInterests" << "\t"
Alexander Afanasyev41684ab2013-02-19 11:02:37 -080052 << "NumberPitEntries" << "\t"
53 << "NumberCsEntries" << "\t"
54 << "MemUsage" << "\n";
55}
56
57void
58PrintCsMemStats (std::ostream &os, Time nextPrintTime, double beginRealTime)
59{
60 ::timeval t;
61 gettimeofday(&t, NULL);
62 double realTime = t.tv_sec + (0.000001 * (unsigned)t.tv_usec) - beginRealTime;
63
64 os << Simulator::Now ().ToDouble (Time::S) << "\t";
65 os << realTime << "\t";
66
Alexander Afanasyev5bee19e2013-07-10 14:33:57 -070067 // os << ndn::L3Protocol::GetDataCounter () << "\t";
68 // os << ndn::L3Protocol::GetInterestCounter () << "\t";
Alexander Afanasyev41684ab2013-02-19 11:02:37 -080069
70 uint64_t pitCount = 0;
71 uint64_t csCount = 0;
72 for (NodeList::Iterator node = NodeList::Begin ();
73 node != NodeList::End ();
74 node ++)
75 {
76 Ptr<ndn::Pit> pit = (*node)->GetObject<ndn::Pit> ();
77 Ptr<ndn::ContentStore> cs = (*node)->GetObject<ndn::ContentStore> ();
78
79 if (pit != 0)
80 pitCount += pit->GetSize ();
81
82 if (cs != 0)
83 csCount += cs->GetSize ();
84 }
85
86 os << pitCount << "\t";
87 os << csCount << "\t";
88 os << MemUsage::Get () / 1024.0 / 1024.0 << "\n";
89
90 Simulator::Schedule (nextPrintTime, PrintCsMemStats, boost::ref (os), nextPrintTime, beginRealTime);
91}
92
93int
94main (int argc, char *argv[])
95{
96 // setting default parameters for PointToPoint links and channels
97 Config::SetDefault ("ns3::PointToPointNetDevice::DataRate", StringValue ("1Mbps"));
98 Config::SetDefault ("ns3::PointToPointChannel::Delay", StringValue ("10ms"));
99 Config::SetDefault ("ns3::DropTailQueue::MaxPackets", StringValue ("20"));
100
101 // Read optional command-line parameters (e.g., enable visualizer with ./waf --run=<> --visualize
102 CommandLine cmd;
103 cmd.Parse (argc, argv);
104
105 // Creating nodes
106 NodeContainer nodes;
107 nodes.Create (3);
108
109 // Connecting nodes using two links
110 PointToPointHelper p2p;
111 p2p.Install (nodes.Get (0), nodes.Get (1));
112 p2p.Install (nodes.Get (1), nodes.Get (2));
113
114 // Install CCNx stack on all nodes
115 ndn::StackHelper ccnxHelper;
116 ccnxHelper.SetDefaultRoutes (true);
Alexander Afanasyev41684ab2013-02-19 11:02:37 -0800117
Alexander Afanasyev8ade0be2013-06-04 10:06:06 -0700118 // node 0: disable cache completely
119 ccnxHelper.SetContentStore ("ns3::ndn::cs::Nocache"); // disable cache
120 ccnxHelper.Install (nodes.Get (0));
121
122 // node 1 and 2: set cache with Lfu policy
123 ccnxHelper.SetContentStore ("ns3::ndn::cs::Freshness::Lfu", "MaxSize", "2"); // can set cache size this way
124 ccnxHelper.Install (nodes.Get (1));
125 ccnxHelper.Install (nodes.Get (2));
126
127 // alternative way to configure cache size
128 // [number after nodeList is global ID of the node (= node->GetId ())]
Alexander Afanasyev41684ab2013-02-19 11:02:37 -0800129 Config::Set ("/NodeList/2/$ns3::ndn::ContentStore/MaxSize", UintegerValue (100000));
130
131 // Installing applications
132
133 // Consumer
134 ndn::AppHelper consumerHelper ("ns3::ndn::ConsumerCbr");
135 // Consumer will request /prefix/0, /prefix/1, ...
136 consumerHelper.SetPrefix ("/prefix");
137 consumerHelper.SetAttribute ("Frequency", StringValue ("10")); // 10 interests a second
138 consumerHelper.Install (nodes.Get (0)); // first node
139
140 // Producer
141 ndn::AppHelper producerHelper ("ns3::ndn::Producer");
142 // Producer will reply to all requests starting with /prefix
143 producerHelper.SetPrefix ("/prefix");
144 producerHelper.SetAttribute ("PayloadSize", StringValue("1024"));
145 producerHelper.Install (nodes.Get (2)); // last node
146
147 Simulator::Stop (Seconds (200000.0));
148
149 struct ::timeval t;
150 gettimeofday(&t, NULL);
151 double beginRealTime = t.tv_sec + (0.000001 * (unsigned)t.tv_usec);
152 Simulator::Schedule (Seconds (0), PrintCsMemStatsHeader, boost::ref (std::cout));
153 Simulator::Schedule (Seconds (100), PrintCsMemStats, boost::ref (std::cout), Seconds (100), beginRealTime);
154
155 Simulator::Run ();
156 Simulator::Destroy ();
157
158 return 0;
159}