Alexander Afanasyev | 141d131 | 2011-12-18 14:58:35 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /* |
| 3 | * Copyright (c) 2011 UCLA |
| 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 | |
| 21 | #include "ccnx-trace-helper.h" |
| 22 | |
Alexander Afanasyev | 141d131 | 2011-12-18 14:58:35 -0800 | [diff] [blame] | 23 | #include "ns3/config.h" |
| 24 | #include "ns3/callback.h" |
| 25 | #include "ns3/ccnx-app.h" |
| 26 | #include "ns3/ccnx-face.h" |
| 27 | #include "ns3/packet.h" |
| 28 | #include "ns3/log.h" |
Alexander Afanasyev | c86c283 | 2011-12-23 02:56:22 -0800 | [diff] [blame] | 29 | #include "ns3/assert.h" |
Alexander Afanasyev | 141d131 | 2011-12-18 14:58:35 -0800 | [diff] [blame] | 30 | #include "ns3/node-list.h" |
| 31 | #include "ns3/object-vector.h" |
Alexander Afanasyev | c86c283 | 2011-12-23 02:56:22 -0800 | [diff] [blame] | 32 | #include "ns3/simulator.h" |
| 33 | #include "ns3/names.h" |
| 34 | |
| 35 | #include <boost/ref.hpp> |
| 36 | #include <boost/lexical_cast.hpp> |
Alexander Afanasyev | b3e4b85 | 2011-12-23 15:58:20 -0800 | [diff] [blame] | 37 | |
| 38 | #include "tracers/ccnx-aggregate-app-tracer.h" |
| 39 | #include "tracers/ccnx-aggregate-l3-tracer.h" |
| 40 | #include "tracers/ccnx-rate-l3-tracer.h" |
Alexander Afanasyev | 3183b5a | 2011-12-23 20:48:20 -0800 | [diff] [blame] | 41 | #include "tracers/ccnx-seqs-app-tracer.h" |
Alexander Afanasyev | c4f8828 | 2012-01-03 11:27:20 -0800 | [diff] [blame^] | 42 | #include "tracers/ipv4-seqs-app-tracer.h" |
Alexander Afanasyev | b3e4b85 | 2011-12-23 15:58:20 -0800 | [diff] [blame] | 43 | |
| 44 | #include "ns3/ccnx-interest-header.h" |
| 45 | #include "ns3/ccnx-content-object-header.h" |
Alexander Afanasyev | 141d131 | 2011-12-18 14:58:35 -0800 | [diff] [blame] | 46 | |
| 47 | #include <fstream> |
Alexander Afanasyev | 141d131 | 2011-12-18 14:58:35 -0800 | [diff] [blame] | 48 | |
| 49 | using namespace std; |
| 50 | using namespace boost; |
| 51 | |
| 52 | NS_LOG_COMPONENT_DEFINE ("CcnxTraceHelper"); |
| 53 | |
| 54 | namespace ns3 { |
Alexander Afanasyev | c86c283 | 2011-12-23 02:56:22 -0800 | [diff] [blame] | 55 | |
| 56 | CcnxTraceHelper::CcnxTraceHelper () |
| 57 | : m_l3RateTrace (0) |
Alexander Afanasyev | 3183b5a | 2011-12-23 20:48:20 -0800 | [diff] [blame] | 58 | , m_appSeqsTrace (0) |
Alexander Afanasyev | c4f8828 | 2012-01-03 11:27:20 -0800 | [diff] [blame^] | 59 | , m_ipv4AppSeqsTrace (0) |
Alexander Afanasyev | c86c283 | 2011-12-23 02:56:22 -0800 | [diff] [blame] | 60 | { |
| 61 | } |
| 62 | |
Alexander Afanasyev | 141d131 | 2011-12-18 14:58:35 -0800 | [diff] [blame] | 63 | CcnxTraceHelper::~CcnxTraceHelper () |
| 64 | { |
| 65 | NS_LOG_FUNCTION (this); |
Alexander Afanasyev | c86c283 | 2011-12-23 02:56:22 -0800 | [diff] [blame] | 66 | if (m_l3RateTrace != 0) delete m_l3RateTrace; |
Alexander Afanasyev | 3183b5a | 2011-12-23 20:48:20 -0800 | [diff] [blame] | 67 | if (m_appSeqsTrace != 0) delete m_appSeqsTrace; |
Alexander Afanasyev | c4f8828 | 2012-01-03 11:27:20 -0800 | [diff] [blame^] | 68 | if (m_ipv4AppSeqsTrace != 0) delete m_ipv4AppSeqsTrace; |
Alexander Afanasyev | 141d131 | 2011-12-18 14:58:35 -0800 | [diff] [blame] | 69 | |
| 70 | if (m_apps.size () > 0) |
| 71 | { |
| 72 | ofstream of; |
| 73 | if (!m_appTrace.empty ()) |
| 74 | { |
| 75 | of.open (m_appTrace.c_str (), ios_base::trunc | ios_base::out); |
| 76 | of << "# "; |
| 77 | m_apps.front ()->PrintHeader (of); |
| 78 | of << "\n"; |
| 79 | } |
| 80 | |
Alexander Afanasyev | b3e4b85 | 2011-12-23 15:58:20 -0800 | [diff] [blame] | 81 | for (std::list<Ptr<CcnxAppTracer> >::iterator app = m_apps.begin (); |
Alexander Afanasyev | 141d131 | 2011-12-18 14:58:35 -0800 | [diff] [blame] | 82 | app != m_apps.end (); |
| 83 | app++) |
| 84 | { |
| 85 | if (!m_appTrace.empty ()) |
| 86 | { |
| 87 | (*app)->Print (of); |
| 88 | of << "\n"; |
| 89 | } |
| 90 | else |
| 91 | { |
| 92 | NS_LOG_INFO (*(*app)); |
| 93 | } |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | if (m_l3s.size () > 0) |
| 98 | { |
| 99 | ofstream of; |
| 100 | if (!m_l3Trace.empty ()) |
| 101 | { |
| 102 | of.open (m_l3Trace.c_str (), ios_base::trunc | ios_base::out); |
| 103 | of << "# "; |
| 104 | m_l3s.front ()->PrintHeader (of); |
| 105 | of << "\n"; |
| 106 | } |
| 107 | |
Alexander Afanasyev | b3e4b85 | 2011-12-23 15:58:20 -0800 | [diff] [blame] | 108 | for (std::list<Ptr<CcnxL3Tracer> >::iterator l3 = m_l3s.begin (); |
Alexander Afanasyev | 141d131 | 2011-12-18 14:58:35 -0800 | [diff] [blame] | 109 | l3 != m_l3s.end (); |
| 110 | l3++) |
| 111 | { |
| 112 | if (!m_l3Trace.empty ()) |
| 113 | { |
| 114 | (*l3)->Print (of); |
| 115 | of << "\n"; |
| 116 | } |
| 117 | else |
| 118 | { |
| 119 | NS_LOG_INFO (*(*l3)); |
| 120 | } |
| 121 | } |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | void |
| 126 | CcnxTraceHelper::SetAppTraceFile (const std::string &appTrace) |
| 127 | { |
| 128 | NS_LOG_FUNCTION (this << appTrace); |
| 129 | m_appTrace = appTrace; |
| 130 | } |
| 131 | |
| 132 | void |
| 133 | CcnxTraceHelper::SetL3TraceFile (const std::string &l3Trace) |
| 134 | { |
| 135 | NS_LOG_FUNCTION (this << l3Trace); |
| 136 | m_l3Trace = l3Trace; |
| 137 | } |
| 138 | |
| 139 | void |
| 140 | CcnxTraceHelper::EnableAggregateAppAll (const std::string &appName) |
| 141 | { |
| 142 | NS_LOG_FUNCTION (this << appName); |
| 143 | for (NodeList::Iterator node = NodeList::Begin (); |
| 144 | node != NodeList::End (); |
| 145 | node++) |
| 146 | { |
| 147 | ObjectVectorValue apps; |
| 148 | (*node)->GetAttribute ("ApplicationList", apps); |
| 149 | |
| 150 | NS_LOG_DEBUG ("Node: " << lexical_cast<string> ((*node)->GetId ())); |
| 151 | |
| 152 | uint32_t appId = 0; |
| 153 | for (ObjectVectorValue::Iterator app = apps.Begin (); |
| 154 | app != apps.End (); |
| 155 | app++, appId++) |
| 156 | { |
| 157 | NS_LOG_DEBUG ("App: " << lexical_cast<string> (appId) << ", typeId: " << (*app)->GetInstanceTypeId ().GetName ()); |
| 158 | if ((*app)->GetInstanceTypeId ().GetName () == appName) |
| 159 | { |
| 160 | m_apps.push_back (Create<CcnxAggregateAppTracer> (appName, |
Alexander Afanasyev | c86c283 | 2011-12-23 02:56:22 -0800 | [diff] [blame] | 161 | *node, |
Alexander Afanasyev | 141d131 | 2011-12-18 14:58:35 -0800 | [diff] [blame] | 162 | lexical_cast<string> (appId))); |
| 163 | } |
| 164 | } |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | void |
| 169 | CcnxTraceHelper::EnableAggregateL3All () |
| 170 | { |
| 171 | NS_LOG_FUNCTION (this); |
| 172 | |
| 173 | for (NodeList::Iterator node = NodeList::Begin (); |
| 174 | node != NodeList::End (); |
| 175 | node++) |
| 176 | { |
| 177 | NS_LOG_DEBUG ("Node: " << lexical_cast<string> ((*node)->GetId ())); |
| 178 | |
Alexander Afanasyev | c86c283 | 2011-12-23 02:56:22 -0800 | [diff] [blame] | 179 | m_l3s.push_back (Create<CcnxAggregateL3Tracer> (*node)); |
Alexander Afanasyev | 141d131 | 2011-12-18 14:58:35 -0800 | [diff] [blame] | 180 | } |
| 181 | } |
| 182 | |
Alexander Afanasyev | c86c283 | 2011-12-23 02:56:22 -0800 | [diff] [blame] | 183 | void |
| 184 | CcnxTraceHelper::EnableRateL3All (const std::string &l3RateTrace) |
| 185 | { |
| 186 | NS_LOG_FUNCTION (this); |
| 187 | m_l3RateTrace = new ofstream (l3RateTrace.c_str (), ios::trunc); |
| 188 | |
| 189 | for (NodeList::Iterator node = NodeList::Begin (); |
| 190 | node != NodeList::End (); |
| 191 | node++) |
| 192 | { |
| 193 | NS_LOG_DEBUG ("Node: " << lexical_cast<string> ((*node)->GetId ())); |
| 194 | |
| 195 | Ptr<CcnxRateL3Tracer> trace = Create<CcnxRateL3Tracer> (boost::ref(*m_l3RateTrace), *node); |
| 196 | trace->SetAveragingPeriod (Seconds (0.5)); |
| 197 | m_l3Rates.push_back (trace); |
| 198 | } |
| 199 | |
| 200 | if (m_l3Rates.size () > 0) |
| 201 | { |
| 202 | // *m_l3RateTrace << "# "; // not necessary for R's read.table |
| 203 | m_l3Rates.front ()->PrintHeader (*m_l3RateTrace); |
| 204 | *m_l3RateTrace << "\n"; |
| 205 | } |
| 206 | } |
Alexander Afanasyev | 141d131 | 2011-12-18 14:58:35 -0800 | [diff] [blame] | 207 | |
Alexander Afanasyev | 3183b5a | 2011-12-23 20:48:20 -0800 | [diff] [blame] | 208 | void |
| 209 | CcnxTraceHelper::EnableSeqsAppAll (const std::string &appName, const std::string &trace) |
| 210 | { |
| 211 | NS_LOG_FUNCTION (this); |
| 212 | m_appSeqsTrace = new ofstream (trace.c_str (), ios::trunc); |
| 213 | |
| 214 | for (NodeList::Iterator node = NodeList::Begin (); |
| 215 | node != NodeList::End (); |
| 216 | node++) |
| 217 | { |
| 218 | ObjectVectorValue apps; |
| 219 | (*node)->GetAttribute ("ApplicationList", apps); |
| 220 | |
| 221 | NS_LOG_DEBUG ("Node: " << lexical_cast<string> ((*node)->GetId ())); |
| 222 | |
| 223 | uint32_t appId = 0; |
| 224 | for (ObjectVectorValue::Iterator app = apps.Begin (); |
| 225 | app != apps.End (); |
| 226 | app++, appId++) |
| 227 | { |
| 228 | NS_LOG_DEBUG ("App: " << lexical_cast<string> (appId) << ", typeId: " << (*app)->GetInstanceTypeId ().GetName ()); |
| 229 | if ((*app)->GetInstanceTypeId ().GetName () == appName) |
| 230 | { |
| 231 | Ptr<CcnxSeqsAppTracer> trace = Create<CcnxSeqsAppTracer> (boost::ref(*m_appSeqsTrace), |
| 232 | appName, |
| 233 | *node, |
| 234 | lexical_cast<string> (appId)); |
| 235 | m_appSeqs.push_back (trace); |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | } |
| 240 | |
| 241 | if (m_appSeqs.size () > 0) |
| 242 | { |
| 243 | // *m_l3RateTrace << "# "; // not necessary for R's read.table |
| 244 | m_appSeqs.front ()->PrintHeader (*m_appSeqsTrace); |
| 245 | *m_appSeqsTrace << "\n"; |
| 246 | } |
| 247 | } |
| 248 | |
Alexander Afanasyev | c4f8828 | 2012-01-03 11:27:20 -0800 | [diff] [blame^] | 249 | void |
| 250 | CcnxTraceHelper::EnableIpv4SeqsAppAll (const std::string &trace) |
| 251 | { |
| 252 | NS_LOG_FUNCTION (this); |
| 253 | m_ipv4AppSeqsTrace = new ofstream (trace.c_str (), ios::trunc); |
| 254 | |
| 255 | for (NodeList::Iterator node = NodeList::Begin (); |
| 256 | node != NodeList::End (); |
| 257 | node++) |
| 258 | { |
| 259 | ObjectVectorValue apps; |
| 260 | (*node)->GetAttribute ("ApplicationList", apps); |
| 261 | |
| 262 | NS_LOG_DEBUG ("Node: " << lexical_cast<string> ((*node)->GetId ())); |
| 263 | |
| 264 | uint32_t appId = 0; |
| 265 | for (ObjectVectorValue::Iterator app = apps.Begin (); |
| 266 | app != apps.End (); |
| 267 | app++, appId++) |
| 268 | { |
| 269 | NS_LOG_DEBUG ("App: " << lexical_cast<string> (appId) << ", typeId: " << (*app)->GetInstanceTypeId ().GetName ()); |
| 270 | if ((*app)->GetInstanceTypeId ().GetName () == "ns3::PacketSink" || |
| 271 | (*app)->GetInstanceTypeId ().GetName () == "ns3::BulkSendApplication") |
| 272 | { |
| 273 | Ptr<Ipv4SeqsAppTracer> trace = Create<Ipv4SeqsAppTracer> (boost::ref(*m_ipv4AppSeqsTrace), |
| 274 | *node, |
| 275 | lexical_cast<string> (appId)); |
| 276 | m_ipv4AppSeqs.push_back (trace); |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | } |
| 281 | |
| 282 | if (m_ipv4AppSeqs.size () > 0) |
| 283 | { |
| 284 | m_ipv4AppSeqs.front ()->PrintHeader (*m_ipv4AppSeqsTrace); |
| 285 | *m_ipv4AppSeqsTrace << "\n"; |
| 286 | } |
| 287 | } |
| 288 | |
Alexander Afanasyev | 3183b5a | 2011-12-23 20:48:20 -0800 | [diff] [blame] | 289 | |
Alexander Afanasyev | 141d131 | 2011-12-18 14:58:35 -0800 | [diff] [blame] | 290 | } // namespace ns3 |