Add a few new examples for multithreading evaluation.
authorGuillaume Seguin <guillaume@segu.in>
Wed, 01 Jul 2009 12:43:23 +0200
changeset 4617 35c8da2a4594
parent 4616 3f3a575034a7
child 4618 300a763efe38
Add a few new examples for multithreading evaluation.
examples/multithreaded-star.cc
examples/multithreaded-udp-echo.cc
examples/point-to-point-udp-echo.cc
examples/star.cc
examples/wscript
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/examples/multithreaded-star.cc	Wed Jul 01 12:43:23 2009 +0200
@@ -0,0 +1,158 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/*
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation;
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ */
+
+#include <fstream>
+#include "ns3/core-module.h"
+#include "ns3/simulator-module.h"
+#include "ns3/node-module.h"
+#include "ns3/helper-module.h"
+#include "ns3/global-route-manager.h"
+
+// Network topology (default)
+//
+//        n2 n3 n4              .
+//         \ | /                .
+//          \|/                 .
+//     n1--- n0---n5            .
+//          /|\                 .
+//         / | \                .
+//        n8 n7 n6              .
+//
+
+
+using namespace ns3;
+
+NS_LOG_COMPONENT_DEFINE ("MultiThreadedStar");
+
+int 
+main (int argc, char *argv[])
+{
+
+  //
+  // Set up some default values for the simulation.
+  //
+  Config::SetDefault ("ns3::OnOffApplication::PacketSize", UintegerValue (137));
+
+  // ??? try and stick 15kb/s into the data rate
+  Config::SetDefault ("ns3::OnOffApplication::DataRate", StringValue ("14kb/s"));
+
+  //
+  // Default number of nodes in the star.  Overridable by command line argument.
+  //
+  uint32_t nNodes = 9;
+
+  CommandLine cmd;
+  cmd.AddValue("nNodes", "Number of nodes to place in the star", nNodes);
+  cmd.Parse (argc, argv);
+  MultiThreadingHelper multiThreadingHelper;
+  multiThreadingHelper.Enable ();
+
+  NS_LOG_INFO ("Create nodes.");
+  NodeContainer hubNode;
+  NodeContainer spokeNodes;
+  hubNode.Create (1);
+  Ptr<Node> hub = hubNode.Get (0);
+  spokeNodes.Create (nNodes - 1);
+
+  NS_LOG_INFO ("Install internet stack on all nodes.");
+  InternetStackHelper internet;
+  internet.Install (NodeContainer (hubNode, spokeNodes));
+
+  PointToPointHelper pointToPoint;
+  pointToPoint.SetDeviceAttribute ("DataRate", StringValue ("5Mbps"));
+  pointToPoint.SetChannelAttribute ("Delay", StringValue ("2ms"));
+
+  NS_LOG_INFO ("Build star topology.");
+  NetDeviceContainer hubDevices, spokeDevices;
+  pointToPoint.InstallStar (hubNode.Get (0), spokeNodes, hubDevices, spokeDevices);
+
+  NS_LOG_INFO ("Assign IP Addresses.");
+  Ipv4AddressHelper address;
+
+  //
+  // Assign IPv4 interfaces and IP addresses to the devices we previously
+  // created.  Keep track of the resulting addresses, one for the addresses
+  // of the hub node, and one for addresses on the spoke nodes.  Despite the
+  // name of the class, what is visible to clients is really the address.
+  //
+  Ipv4InterfaceContainer hubAddresses;
+  Ipv4InterfaceContainer spokeAddresses;
+
+  for(uint32_t i = 0; i < spokeNodes.GetN (); ++i)
+  {
+    std::ostringstream subnet;
+    subnet << "10.1.1." << (i << 2);
+    NS_LOG_INFO ("Assign IP Addresses for point-to-point subnet " << subnet.str ());
+    address.SetBase (subnet.str ().c_str (), "255.255.255.252");
+    hubAddresses.Add (address.Assign (hubDevices.Get (i)));
+    spokeAddresses.Add (address.Assign (spokeDevices.Get (i)));
+  }
+
+  NS_LOG_INFO ("Create applications.");
+  //
+  // Create a packet sink on the star "hub" to receive packets.  
+  // 
+  uint16_t port = 50000;
+  Address hubLocalAddress (InetSocketAddress (Ipv4Address::GetAny (), port));
+  PacketSinkHelper packetSinkHelper ("ns3::TcpSocketFactory", hubLocalAddress);
+  ApplicationContainer hubApp = packetSinkHelper.Install (hubNode);
+  hubApp.Start (Seconds (1.0));
+  hubApp.Stop (Seconds (10.0));
+
+  //
+  // Create OnOff applications to send TCP to the hub, one on each spoke node.
+  //
+  OnOffHelper onOffHelper ("ns3::TcpSocketFactory", Address ());
+  onOffHelper.SetAttribute ("OnTime", RandomVariableValue (ConstantVariable (1)));
+  onOffHelper.SetAttribute ("OffTime", RandomVariableValue (ConstantVariable (0)));
+
+  ApplicationContainer spokeApps;
+
+  for (uint32_t i = 0; i < spokeNodes.GetN (); ++i)
+    {
+      AddressValue remoteAddress (InetSocketAddress (hubAddresses.GetAddress (i), port));
+      onOffHelper.SetAttribute ("Remote", remoteAddress);
+      spokeApps.Add (onOffHelper.Install (spokeNodes.Get (i)));
+  }
+  spokeApps.Start (Seconds (1.0));
+  spokeApps.Stop (Seconds (10.0));
+
+  NS_LOG_INFO ("Enable static global routing.");
+  //
+  // Turn on global static routing so we can actually be routed across the star.
+  //
+  GlobalRouteManager::PopulateRoutingTables ();
+
+  NS_LOG_INFO ("Enable pcap tracing.");
+  //
+  // Do pcap tracing on all point-to-point devices on all nodes.
+  //
+  PointToPointHelper::EnablePcapAll ("multithreaded-star");
+
+  std::ofstream ascii;
+  ascii.open ("multithreaded-star.tr");
+  PointToPointHelper::EnableAsciiAll (ascii);
+
+  // Install the multithreading partitions
+  multiThreadingHelper.Install ();
+  NS_LOG_INFO ("Run Simulation.");
+  Simulator::Run ();
+  Simulator::Destroy ();
+  NS_LOG_INFO ("Done.");
+
+  return 0;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/examples/multithreaded-udp-echo.cc	Wed Jul 01 12:43:23 2009 +0200
@@ -0,0 +1,115 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/*
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation;
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ */
+
+//
+// Network topology
+//
+//           10Mb/s, 10ms
+//       n0-----------------n1
+//
+// - UDP flows from n0 to n1 and back
+// - DropTail queues 
+// - Tracing of queues and packet receptions to file "multithreaded-udp-echo.tr"
+
+#include <fstream>
+#include "ns3/core-module.h"
+#include "ns3/simulator-module.h"
+#include "ns3/helper-module.h"
+
+using namespace ns3;
+
+NS_LOG_COMPONENT_DEFINE ("MultiThreadedUdpEchoExample");
+
+int 
+main (int argc, char *argv[])
+{
+  //
+  // Allow the user to override any of the defaults and the above Bind() at
+  // run-time, via command-line arguments
+  //
+  CommandLine cmd;
+  cmd.Parse (argc, argv);
+  MultiThreadingHelper multiThreadingHelper;
+  multiThreadingHelper.Enable ();
+
+  //
+  // Explicitly create the nodes required by the topology (shown above).
+  //
+  NS_LOG_INFO ("Create nodes.");
+  NodeContainer n;
+  n.Create (2);
+
+  InternetStackHelper internet;
+  internet.Install (n);
+
+  //
+  // Explicitly create the channels required by the topology (shown above).
+  //
+  NS_LOG_INFO ("Create channels.");
+  PointToPointHelper p2p;
+  p2p.SetDeviceAttribute ("DataRate", DataRateValue (DataRate(10000000)));
+  p2p.SetChannelAttribute ("Delay", TimeValue (MilliSeconds(10)));
+  NetDeviceContainer d = p2p.Install (n);
+
+  //
+  // We've got the "hardware" in place.  Now we need to add IP addresses.
+  //
+  NS_LOG_INFO ("Assign IP Addresses.");
+  Ipv4AddressHelper ipv4;
+  ipv4.SetBase ("10.1.1.0", "255.255.255.0");
+  Ipv4InterfaceContainer i = ipv4.Assign (d);
+
+  NS_LOG_INFO ("Create Applications.");
+
+  //
+  // Create a UdpEchoServer application on node one.
+  //
+  uint16_t port = 9;  // well-known echo port number
+  UdpEchoServerHelper server (port);
+  ApplicationContainer apps = server.Install (n.Get(1));
+  apps.Start (Seconds (1.0));
+  apps.Stop (Seconds (10.0));
+
+  //
+  // Create a UdpEchoClient application to send UDP datagrams from node zero to
+  // node one.
+  //
+  uint32_t packetSize = 1024;
+  uint32_t maxPacketCount = 1;
+  Time interPacketInterval = Seconds (1.);
+  UdpEchoClientHelper client (i.GetAddress (1), port);
+  client.SetAttribute ("MaxPackets", UintegerValue (maxPacketCount));
+  client.SetAttribute ("Interval", TimeValue (interPacketInterval));
+  client.SetAttribute ("PacketSize", UintegerValue (packetSize));
+  apps = client.Install (n.Get (0));
+  apps.Start (Seconds (2.0));
+  apps.Stop (Seconds (10.0));
+
+  std::ofstream ascii;
+  ascii.open ("multithreaded-udp-echo.tr");
+  PointToPointHelper::EnableAsciiAll (ascii);
+  PointToPointHelper::EnablePcapAll ("multithreaded-udp-echo");
+
+  // Install the multithreading partitions
+  multiThreadingHelper.Install ();
+  //
+  // Now, do the actual simulation.
+  //
+  NS_LOG_INFO ("Run Simulation.");
+  Simulator::Run ();
+  Simulator::Destroy ();
+  NS_LOG_INFO ("Done.");
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/examples/point-to-point-udp-echo.cc	Wed Jul 01 12:43:23 2009 +0200
@@ -0,0 +1,111 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/*
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation;
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ */
+
+//
+// Network topology
+//
+//           10Mb/s, 10ms
+//       n0-----------------n1
+//
+// - UDP flows from n0 to n1 and back
+// - DropTail queues 
+// - Tracing of queues and packet receptions to file "point-to-point-udp-echo.tr"
+
+#include <fstream>
+#include "ns3/core-module.h"
+#include "ns3/simulator-module.h"
+#include "ns3/helper-module.h"
+
+using namespace ns3;
+
+NS_LOG_COMPONENT_DEFINE ("PointToPointUdpEchoExample");
+
+int 
+main (int argc, char *argv[])
+{
+  //
+  // Allow the user to override any of the defaults and the above Bind() at
+  // run-time, via command-line arguments
+  //
+  CommandLine cmd;
+  cmd.Parse (argc, argv);
+
+  //
+  // Explicitly create the nodes required by the topology (shown above).
+  //
+  NS_LOG_INFO ("Create nodes.");
+  NodeContainer n;
+  n.Create (2);
+
+  InternetStackHelper internet;
+  internet.Install (n);
+
+  //
+  // Explicitly create the channels required by the topology (shown above).
+  //
+  NS_LOG_INFO ("Create channels.");
+  PointToPointHelper p2p;
+  p2p.SetDeviceAttribute ("DataRate", DataRateValue (DataRate(10000000)));
+  p2p.SetChannelAttribute ("Delay", TimeValue (MilliSeconds(10)));
+  NetDeviceContainer d = p2p.Install (n);
+
+  //
+  // We've got the "hardware" in place.  Now we need to add IP addresses.
+  //
+  NS_LOG_INFO ("Assign IP Addresses.");
+  Ipv4AddressHelper ipv4;
+  ipv4.SetBase ("10.1.1.0", "255.255.255.0");
+  Ipv4InterfaceContainer i = ipv4.Assign (d);
+
+  NS_LOG_INFO ("Create Applications.");
+
+  //
+  // Create a UdpEchoServer application on node one.
+  //
+  uint16_t port = 9;  // well-known echo port number
+  UdpEchoServerHelper server (port);
+  ApplicationContainer apps = server.Install (n.Get(1));
+  apps.Start (Seconds (1.0));
+  apps.Stop (Seconds (10.0));
+
+  //
+  // Create a UdpEchoClient application to send UDP datagrams from node zero to
+  // node one.
+  //
+  uint32_t packetSize = 1024;
+  uint32_t maxPacketCount = 1;
+  Time interPacketInterval = Seconds (1.);
+  UdpEchoClientHelper client (i.GetAddress (1), port);
+  client.SetAttribute ("MaxPackets", UintegerValue (maxPacketCount));
+  client.SetAttribute ("Interval", TimeValue (interPacketInterval));
+  client.SetAttribute ("PacketSize", UintegerValue (packetSize));
+  apps = client.Install (n.Get (0));
+  apps.Start (Seconds (2.0));
+  apps.Stop (Seconds (10.0));
+
+  std::ofstream ascii;
+  ascii.open ("point-to-point-udp-echo.tr");
+  PointToPointHelper::EnableAsciiAll (ascii);
+  PointToPointHelper::EnablePcapAll ("point-to-point-udp-echo");
+
+  //
+  // Now, do the actual simulation.
+  //
+  NS_LOG_INFO ("Run Simulation.");
+  Simulator::Run ();
+  Simulator::Destroy ();
+  NS_LOG_INFO ("Done.");
+}
--- a/examples/star.cc	Wed Jul 01 12:42:55 2009 +0200
+++ b/examples/star.cc	Wed Jul 01 12:43:23 2009 +0200
@@ -15,6 +15,7 @@
  *
  */
 
+#include <fstream>
 #include "ns3/core-module.h"
 #include "ns3/simulator-module.h"
 #include "ns3/node-module.h"
@@ -140,6 +141,10 @@
   //
   PointToPointHelper::EnablePcapAll ("star");
 
+  std::ofstream ascii;
+  ascii.open ("star.tr");
+  PointToPointHelper::EnableAsciiAll (ascii);
+
   NS_LOG_INFO ("Run Simulation.");
   Simulator::Run ();
   Simulator::Destroy ();
--- a/examples/wscript	Wed Jul 01 12:42:55 2009 +0200
+++ b/examples/wscript	Wed Jul 01 12:43:23 2009 +0200
@@ -163,3 +163,20 @@
     obj = bld.create_ns3_program('simple-wifi-frame-aggregation',
                                  ['core', 'simulator', 'mobility', 'wifi'])
     obj.source = 'simple-wifi-frame-aggregation.cc'
+
+    obj = bld.create_ns3_program('point-to-point-udp-echo',
+                                 ['point-to-point', 'internet-stack'])
+    obj.source = 'point-to-point-udp-echo.cc'
+
+    if env['ENABLE_THREADING']:
+      obj = bld.create_ns3_program('multithreaded-sync-test',
+                                   ['point-to-point', 'internet-stack'])
+      obj.source = 'multithreaded-sync-test.cc'
+
+      obj = bld.create_ns3_program('multithreaded-udp-echo',
+                                   ['point-to-point', 'internet-stack'])
+      obj.source = 'multithreaded-udp-echo.cc'
+
+      obj = bld.create_ns3_program('multithreaded-star',
+                                   ['point-to-point', 'internet-stack'])
+      obj.source = 'multithreaded-star.cc'