blob: d22a549d0843c78594c7479d595a402cfded16d6 [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>
Alexander Afanasyev0c395372014-12-20 15:54:02 -080027#include "ns3/ndnSIM/utils/mem-usage.hpp"
Alexander Afanasyev41684ab2013-02-19 11:02:37 -080028
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 Afanasyevbe55cf62014-12-20 17:51:09 -080039 * This scenario demonstrates how to use content store that responds to Freshness parameter set in
40 *Datas.
41 * That is, if producer set Freshness field to 2 seconds, the corresponding content object will not
42 *be cached
Alexander Afanasyev41684ab2013-02-19 11:02:37 -080043 * more than 2 seconds (can be cached for a shorter time, if entry is evicted earlier)
44 *
45 * NS_LOG=ndn.Consumer ./waf --run ndn-simple-with-cs-lfu
46 */
47
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080048void
49PrintCsMemStatsHeader(std::ostream& os)
Alexander Afanasyev41684ab2013-02-19 11:02:37 -080050{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080051 os << "SimulationTime"
52 << "\t"
53 << "RealTime"
54 << "\t"
Alexander Afanasyev5bee19e2013-07-10 14:33:57 -070055 // << "NumberOfProcessedData" << "\t"
56 // << "NumberOfProcessedInterests" << "\t"
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080057 << "NumberPitEntries"
58 << "\t"
59 << "NumberCsEntries"
60 << "\t"
61 << "MemUsage"
62 << "\n";
Alexander Afanasyev41684ab2013-02-19 11:02:37 -080063}
64
65void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080066PrintCsMemStats(std::ostream& os, Time nextPrintTime, double beginRealTime)
Alexander Afanasyev41684ab2013-02-19 11:02:37 -080067{
68 ::timeval t;
69 gettimeofday(&t, NULL);
70 double realTime = t.tv_sec + (0.000001 * (unsigned)t.tv_usec) - beginRealTime;
71
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080072 os << Simulator::Now().ToDouble(Time::S) << "\t";
Alexander Afanasyev41684ab2013-02-19 11:02:37 -080073 os << realTime << "\t";
74
Alexander Afanasyev5bee19e2013-07-10 14:33:57 -070075 // os << ndn::L3Protocol::GetDataCounter () << "\t";
76 // os << ndn::L3Protocol::GetInterestCounter () << "\t";
Alexander Afanasyev41684ab2013-02-19 11:02:37 -080077
78 uint64_t pitCount = 0;
79 uint64_t csCount = 0;
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080080 for (NodeList::Iterator node = NodeList::Begin(); node != NodeList::End(); node++) {
81 Ptr<ndn::Pit> pit = (*node)->GetObject<ndn::Pit>();
82 Ptr<ndn::ContentStore> cs = (*node)->GetObject<ndn::ContentStore>();
Alexander Afanasyev41684ab2013-02-19 11:02:37 -080083
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080084 if (pit != 0)
85 pitCount += pit->GetSize();
Alexander Afanasyev41684ab2013-02-19 11:02:37 -080086
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080087 if (cs != 0)
88 csCount += cs->GetSize();
89 }
Alexander Afanasyev41684ab2013-02-19 11:02:37 -080090
91 os << pitCount << "\t";
92 os << csCount << "\t";
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080093 os << MemUsage::Get() / 1024.0 / 1024.0 << "\n";
Alexander Afanasyev41684ab2013-02-19 11:02:37 -080094
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080095 Simulator::Schedule(nextPrintTime, PrintCsMemStats, boost::ref(os), nextPrintTime, beginRealTime);
Alexander Afanasyev41684ab2013-02-19 11:02:37 -080096}
97
98int
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080099main(int argc, char* argv[])
Alexander Afanasyev41684ab2013-02-19 11:02:37 -0800100{
101 // setting default parameters for PointToPoint links and channels
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800102 Config::SetDefault("ns3::PointToPointNetDevice::DataRate", StringValue("1Mbps"));
103 Config::SetDefault("ns3::PointToPointChannel::Delay", StringValue("10ms"));
104 Config::SetDefault("ns3::DropTailQueue::MaxPackets", StringValue("20"));
Alexander Afanasyev41684ab2013-02-19 11:02:37 -0800105
106 // Read optional command-line parameters (e.g., enable visualizer with ./waf --run=<> --visualize
107 CommandLine cmd;
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800108 cmd.Parse(argc, argv);
Alexander Afanasyev41684ab2013-02-19 11:02:37 -0800109
110 // Creating nodes
111 NodeContainer nodes;
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800112 nodes.Create(3);
Alexander Afanasyev41684ab2013-02-19 11:02:37 -0800113
114 // Connecting nodes using two links
115 PointToPointHelper p2p;
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800116 p2p.Install(nodes.Get(0), nodes.Get(1));
117 p2p.Install(nodes.Get(1), nodes.Get(2));
Alexander Afanasyev41684ab2013-02-19 11:02:37 -0800118
119 // Install CCNx stack on all nodes
120 ndn::StackHelper ccnxHelper;
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800121 ccnxHelper.SetDefaultRoutes(true);
Alexander Afanasyev41684ab2013-02-19 11:02:37 -0800122
Alexander Afanasyev8ade0be2013-06-04 10:06:06 -0700123 // node 0: disable cache completely
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800124 ccnxHelper.SetContentStore("ns3::ndn::cs::Nocache"); // disable cache
125 ccnxHelper.Install(nodes.Get(0));
Alexander Afanasyev8ade0be2013-06-04 10:06:06 -0700126
127 // node 1 and 2: set cache with Lfu policy
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800128 ccnxHelper.SetContentStore("ns3::ndn::cs::Freshness::Lfu", "MaxSize",
129 "2"); // can set cache size this way
130 ccnxHelper.Install(nodes.Get(1));
131 ccnxHelper.Install(nodes.Get(2));
Alexander Afanasyev8ade0be2013-06-04 10:06:06 -0700132
133 // alternative way to configure cache size
134 // [number after nodeList is global ID of the node (= node->GetId ())]
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800135 Config::Set("/NodeList/2/$ns3::ndn::ContentStore/MaxSize", UintegerValue(100000));
Alexander Afanasyev41684ab2013-02-19 11:02:37 -0800136
137 // Installing applications
138
139 // Consumer
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800140 ndn::AppHelper consumerHelper("ns3::ndn::ConsumerCbr");
Alexander Afanasyev41684ab2013-02-19 11:02:37 -0800141 // Consumer will request /prefix/0, /prefix/1, ...
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800142 consumerHelper.SetPrefix("/prefix");
143 consumerHelper.SetAttribute("Frequency", StringValue("10")); // 10 interests a second
144 consumerHelper.Install(nodes.Get(0)); // first node
Alexander Afanasyev41684ab2013-02-19 11:02:37 -0800145
146 // Producer
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800147 ndn::AppHelper producerHelper("ns3::ndn::Producer");
Alexander Afanasyev41684ab2013-02-19 11:02:37 -0800148 // Producer will reply to all requests starting with /prefix
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800149 producerHelper.SetPrefix("/prefix");
150 producerHelper.SetAttribute("PayloadSize", StringValue("1024"));
151 producerHelper.Install(nodes.Get(2)); // last node
Alexander Afanasyev41684ab2013-02-19 11:02:37 -0800152
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800153 Simulator::Stop(Seconds(200000.0));
Alexander Afanasyev41684ab2013-02-19 11:02:37 -0800154
155 struct ::timeval t;
156 gettimeofday(&t, NULL);
157 double beginRealTime = t.tv_sec + (0.000001 * (unsigned)t.tv_usec);
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800158 Simulator::Schedule(Seconds(0), PrintCsMemStatsHeader, boost::ref(std::cout));
159 Simulator::Schedule(Seconds(100), PrintCsMemStats, boost::ref(std::cout), Seconds(100),
160 beginRealTime);
Alexander Afanasyev41684ab2013-02-19 11:02:37 -0800161
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800162 Simulator::Run();
163 Simulator::Destroy();
Alexander Afanasyev41684ab2013-02-19 11:02:37 -0800164
165 return 0;
166}