Alexander Afanasyev | 41684ab | 2013-02-19 11:02:37 -0800 | [diff] [blame] | 1 | /* -*- 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 Afanasyev | 0c39537 | 2014-12-20 15:54:02 -0800 | [diff] [blame] | 27 | #include "ns3/ndnSIM/utils/mem-usage.hpp" |
Alexander Afanasyev | 41684ab | 2013-02-19 11:02:37 -0800 | [diff] [blame] | 28 | |
| 29 | using 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 Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 39 | * 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 Afanasyev | 41684ab | 2013-02-19 11:02:37 -0800 | [diff] [blame] | 43 | * 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 Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 48 | void |
| 49 | PrintCsMemStatsHeader(std::ostream& os) |
Alexander Afanasyev | 41684ab | 2013-02-19 11:02:37 -0800 | [diff] [blame] | 50 | { |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 51 | os << "SimulationTime" |
| 52 | << "\t" |
| 53 | << "RealTime" |
| 54 | << "\t" |
Alexander Afanasyev | 5bee19e | 2013-07-10 14:33:57 -0700 | [diff] [blame] | 55 | // << "NumberOfProcessedData" << "\t" |
| 56 | // << "NumberOfProcessedInterests" << "\t" |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 57 | << "NumberPitEntries" |
| 58 | << "\t" |
| 59 | << "NumberCsEntries" |
| 60 | << "\t" |
| 61 | << "MemUsage" |
| 62 | << "\n"; |
Alexander Afanasyev | 41684ab | 2013-02-19 11:02:37 -0800 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | void |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 66 | PrintCsMemStats(std::ostream& os, Time nextPrintTime, double beginRealTime) |
Alexander Afanasyev | 41684ab | 2013-02-19 11:02:37 -0800 | [diff] [blame] | 67 | { |
| 68 | ::timeval t; |
| 69 | gettimeofday(&t, NULL); |
| 70 | double realTime = t.tv_sec + (0.000001 * (unsigned)t.tv_usec) - beginRealTime; |
| 71 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 72 | os << Simulator::Now().ToDouble(Time::S) << "\t"; |
Alexander Afanasyev | 41684ab | 2013-02-19 11:02:37 -0800 | [diff] [blame] | 73 | os << realTime << "\t"; |
| 74 | |
Alexander Afanasyev | 5bee19e | 2013-07-10 14:33:57 -0700 | [diff] [blame] | 75 | // os << ndn::L3Protocol::GetDataCounter () << "\t"; |
| 76 | // os << ndn::L3Protocol::GetInterestCounter () << "\t"; |
Alexander Afanasyev | 41684ab | 2013-02-19 11:02:37 -0800 | [diff] [blame] | 77 | |
| 78 | uint64_t pitCount = 0; |
| 79 | uint64_t csCount = 0; |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 80 | 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 Afanasyev | 41684ab | 2013-02-19 11:02:37 -0800 | [diff] [blame] | 83 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 84 | if (pit != 0) |
| 85 | pitCount += pit->GetSize(); |
Alexander Afanasyev | 41684ab | 2013-02-19 11:02:37 -0800 | [diff] [blame] | 86 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 87 | if (cs != 0) |
| 88 | csCount += cs->GetSize(); |
| 89 | } |
Alexander Afanasyev | 41684ab | 2013-02-19 11:02:37 -0800 | [diff] [blame] | 90 | |
| 91 | os << pitCount << "\t"; |
| 92 | os << csCount << "\t"; |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 93 | os << MemUsage::Get() / 1024.0 / 1024.0 << "\n"; |
Alexander Afanasyev | 41684ab | 2013-02-19 11:02:37 -0800 | [diff] [blame] | 94 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 95 | Simulator::Schedule(nextPrintTime, PrintCsMemStats, boost::ref(os), nextPrintTime, beginRealTime); |
Alexander Afanasyev | 41684ab | 2013-02-19 11:02:37 -0800 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | int |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 99 | main(int argc, char* argv[]) |
Alexander Afanasyev | 41684ab | 2013-02-19 11:02:37 -0800 | [diff] [blame] | 100 | { |
| 101 | // setting default parameters for PointToPoint links and channels |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 102 | Config::SetDefault("ns3::PointToPointNetDevice::DataRate", StringValue("1Mbps")); |
| 103 | Config::SetDefault("ns3::PointToPointChannel::Delay", StringValue("10ms")); |
| 104 | Config::SetDefault("ns3::DropTailQueue::MaxPackets", StringValue("20")); |
Alexander Afanasyev | 41684ab | 2013-02-19 11:02:37 -0800 | [diff] [blame] | 105 | |
| 106 | // Read optional command-line parameters (e.g., enable visualizer with ./waf --run=<> --visualize |
| 107 | CommandLine cmd; |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 108 | cmd.Parse(argc, argv); |
Alexander Afanasyev | 41684ab | 2013-02-19 11:02:37 -0800 | [diff] [blame] | 109 | |
| 110 | // Creating nodes |
| 111 | NodeContainer nodes; |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 112 | nodes.Create(3); |
Alexander Afanasyev | 41684ab | 2013-02-19 11:02:37 -0800 | [diff] [blame] | 113 | |
| 114 | // Connecting nodes using two links |
| 115 | PointToPointHelper p2p; |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 116 | p2p.Install(nodes.Get(0), nodes.Get(1)); |
| 117 | p2p.Install(nodes.Get(1), nodes.Get(2)); |
Alexander Afanasyev | 41684ab | 2013-02-19 11:02:37 -0800 | [diff] [blame] | 118 | |
| 119 | // Install CCNx stack on all nodes |
| 120 | ndn::StackHelper ccnxHelper; |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 121 | ccnxHelper.SetDefaultRoutes(true); |
Alexander Afanasyev | 41684ab | 2013-02-19 11:02:37 -0800 | [diff] [blame] | 122 | |
Alexander Afanasyev | 8ade0be | 2013-06-04 10:06:06 -0700 | [diff] [blame] | 123 | // node 0: disable cache completely |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 124 | ccnxHelper.SetContentStore("ns3::ndn::cs::Nocache"); // disable cache |
| 125 | ccnxHelper.Install(nodes.Get(0)); |
Alexander Afanasyev | 8ade0be | 2013-06-04 10:06:06 -0700 | [diff] [blame] | 126 | |
| 127 | // node 1 and 2: set cache with Lfu policy |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 128 | 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 Afanasyev | 8ade0be | 2013-06-04 10:06:06 -0700 | [diff] [blame] | 132 | |
| 133 | // alternative way to configure cache size |
| 134 | // [number after nodeList is global ID of the node (= node->GetId ())] |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 135 | Config::Set("/NodeList/2/$ns3::ndn::ContentStore/MaxSize", UintegerValue(100000)); |
Alexander Afanasyev | 41684ab | 2013-02-19 11:02:37 -0800 | [diff] [blame] | 136 | |
| 137 | // Installing applications |
| 138 | |
| 139 | // Consumer |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 140 | ndn::AppHelper consumerHelper("ns3::ndn::ConsumerCbr"); |
Alexander Afanasyev | 41684ab | 2013-02-19 11:02:37 -0800 | [diff] [blame] | 141 | // Consumer will request /prefix/0, /prefix/1, ... |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 142 | consumerHelper.SetPrefix("/prefix"); |
| 143 | consumerHelper.SetAttribute("Frequency", StringValue("10")); // 10 interests a second |
| 144 | consumerHelper.Install(nodes.Get(0)); // first node |
Alexander Afanasyev | 41684ab | 2013-02-19 11:02:37 -0800 | [diff] [blame] | 145 | |
| 146 | // Producer |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 147 | ndn::AppHelper producerHelper("ns3::ndn::Producer"); |
Alexander Afanasyev | 41684ab | 2013-02-19 11:02:37 -0800 | [diff] [blame] | 148 | // Producer will reply to all requests starting with /prefix |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 149 | producerHelper.SetPrefix("/prefix"); |
| 150 | producerHelper.SetAttribute("PayloadSize", StringValue("1024")); |
| 151 | producerHelper.Install(nodes.Get(2)); // last node |
Alexander Afanasyev | 41684ab | 2013-02-19 11:02:37 -0800 | [diff] [blame] | 152 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 153 | Simulator::Stop(Seconds(200000.0)); |
Alexander Afanasyev | 41684ab | 2013-02-19 11:02:37 -0800 | [diff] [blame] | 154 | |
| 155 | struct ::timeval t; |
| 156 | gettimeofday(&t, NULL); |
| 157 | double beginRealTime = t.tv_sec + (0.000001 * (unsigned)t.tv_usec); |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 158 | Simulator::Schedule(Seconds(0), PrintCsMemStatsHeader, boost::ref(std::cout)); |
| 159 | Simulator::Schedule(Seconds(100), PrintCsMemStats, boost::ref(std::cout), Seconds(100), |
| 160 | beginRealTime); |
Alexander Afanasyev | 41684ab | 2013-02-19 11:02:37 -0800 | [diff] [blame] | 161 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 162 | Simulator::Run(); |
| 163 | Simulator::Destroy(); |
Alexander Afanasyev | 41684ab | 2013-02-19 11:02:37 -0800 | [diff] [blame] | 164 | |
| 165 | return 0; |
| 166 | } |