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> |
| 27 | #include "ns3/ndnSIM/utils/mem-usage.h" |
| 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 | * |
| 39 | * This scenario demonstrates how to use content store that responds to Freshness parameter set in ContentObjects. |
| 40 | * 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 | |
| 46 | void PrintCsMemStatsHeader (std::ostream &os) |
| 47 | { |
| 48 | os << "SimulationTime" << "\t" |
| 49 | << "RealTime" << "\t" |
| 50 | << "NumberOfProcessedData" << "\t" |
| 51 | << "NumberOfProcessedInterests" << "\t" |
| 52 | << "NumberPitEntries" << "\t" |
| 53 | << "NumberCsEntries" << "\t" |
| 54 | << "MemUsage" << "\n"; |
| 55 | } |
| 56 | |
| 57 | void |
| 58 | PrintCsMemStats (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 | |
| 67 | os << ndn::L3Protocol::GetDataCounter () << "\t"; |
| 68 | os << ndn::L3Protocol::GetInterestCounter () << "\t"; |
| 69 | |
| 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 | |
| 93 | int |
| 94 | main (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 Afanasyev | 41684ab | 2013-02-19 11:02:37 -0800 | [diff] [blame] | 117 | |
Alexander Afanasyev | 8ade0be | 2013-06-04 10:06:06 -0700 | [diff] [blame] | 118 | // 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 Afanasyev | 41684ab | 2013-02-19 11:02:37 -0800 | [diff] [blame] | 129 | 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 | } |