Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Fix smpi_execute_benched.
[simgrid.git] / src / surf / ns3 / ns3_simulator.cc
1 /* Copyright (c) 2007-2017. The SimGrid Team. All rights reserved.          */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 #include "src/surf/ns3/ns3_simulator.h"
7 #include "xbt/dict.h"
8 #include "xbt/log.h"
9 #include "xbt/sysdep.h"
10
11 #include <algorithm>
12
13 xbt_dict_t flowFromSock = xbt_dict_new_homogeneous([](void *p) {
14   delete (SgFlow*)p;
15 }); // ns3::sock -> SgFlow
16
17 static void receive_callback(ns3::Ptr<ns3::Socket> socket);
18 static void datasent_callback(ns3::Ptr<ns3::Socket> socket, uint32_t dataSent);
19
20 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(ns3);
21
22 SgFlow::SgFlow(uint32_t totalBytes, simgrid::surf::NetworkNS3Action* action)
23 {
24   totalBytes_ = totalBytes;
25   remaining_  = totalBytes;
26   action_     = action;
27 }
28
29 static SgFlow* getFlowFromSocket(ns3::Ptr<ns3::Socket> socket)
30 {
31   return (SgFlow*)xbt_dict_get_or_null(flowFromSock, transformSocketPtr(socket));
32 }
33
34 static void receive_callback(ns3::Ptr<ns3::Socket> socket)
35 {
36   SgFlow* flow = getFlowFromSocket(socket);
37
38   if (flow->finished_ == false) {
39     flow->finished_ = true;
40     XBT_DEBUG("recv_cb of F[%p, %p, %d]", flow, flow->action_, flow->totalBytes_);
41     XBT_DEBUG("Stop simulator at %f seconds", ns3::Simulator::Now().GetSeconds());
42     ns3::Simulator::Stop(ns3::Seconds(0.0));
43     ns3::Simulator::Run();
44   }
45 }
46
47 static void WriteUntilBufferFull(ns3::Ptr<ns3::Socket> sock, uint32_t txSpace)
48 {
49   SgFlow* flow = getFlowFromSocket(sock);
50
51   if (flow->remaining_ == 0) // all data was already buffered (and socket was already closed)
52     return;
53
54   /* While not all is buffered and there remain space in the buffers */
55   while (flow->bufferedBytes_ < flow->totalBytes_ && sock->GetTxAvailable() > 0) {
56
57     uint32_t toWrite = std::min({flow->remaining_, sock->GetTxAvailable()});
58     if (toWrite == 0) // buffer full
59       return;
60     int amountSent   = sock->Send(0, toWrite, 0);
61
62     xbt_assert(amountSent > 0, "Since TxAvailable>0, amountSent should also >0");
63     flow->bufferedBytes_ += amountSent;
64     flow->remaining_ -= amountSent;
65
66     XBT_DEBUG("%f: sent %d bytes over flow %p (still %d to go)",
67         ns3::Simulator::Now().GetSeconds(), amountSent, flow, flow->remaining_);
68   }
69
70   if (flow->bufferedBytes_ >= flow->totalBytes_)
71     sock->Close();
72 }
73
74 static void datasent_callback(ns3::Ptr<ns3::Socket> socket, uint32_t dataSent)
75 {
76   /* The tracing wants to know */
77   SgFlow* flow = getFlowFromSocket(socket);
78   flow->sentBytes_ += dataSent;
79   XBT_DEBUG("datasent_cb of F[%p, %p, %d] %d sent (%d total)",
80       flow, flow->action_, flow->totalBytes_, dataSent, flow->sentBytes_);
81 }
82
83 static void normalClose_callback(ns3::Ptr<ns3::Socket> socket)
84 {
85   SgFlow* flow = getFlowFromSocket(socket);
86   XBT_DEBUG("normalClose_cb of F[%p, %p, %d]", flow, flow->action_, flow->totalBytes_);
87   receive_callback(socket);
88 }
89
90 static void errorClose_callback(ns3::Ptr<ns3::Socket> socket)
91 {
92   SgFlow* flow = getFlowFromSocket(socket);
93   XBT_DEBUG("errorClose_cb of F[%p, %p, %d]", flow, flow->action_, flow->totalBytes_);
94   xbt_die("NS3: a socket was closed anormally");
95 }
96
97 static void succeededConnect_callback(ns3::Ptr<ns3::Socket> socket)
98 {
99   SgFlow* flow = getFlowFromSocket(socket);
100   XBT_DEBUG("succeededConnect_cb of F[%p, %p, %d]", flow, flow->action_, flow->totalBytes_);
101 }
102
103 static void failedConnect_callback(ns3::Ptr<ns3::Socket> socket)
104 {
105   SgFlow* mysocket = getFlowFromSocket(socket);
106   XBT_DEBUG("failedConnect_cb of F[%p, %p, %d]", mysocket, mysocket->action_, mysocket->totalBytes_);
107   xbt_die("NS3: a socket failed to connect");
108 }
109
110 void StartFlow(ns3::Ptr<ns3::Socket> sock, const char* to, uint16_t port_number)
111 {
112   SgFlow* flow = getFlowFromSocket(sock);
113   ns3::InetSocketAddress serverAddr(to, port_number);
114
115   sock->Connect(serverAddr);
116   // tell the tcp implementation to call WriteUntilBufferFull again
117   // if we blocked and new tx buffer space becomes available
118   sock->SetSendCallback (MakeCallback(&WriteUntilBufferFull));
119   // Note when the send is over
120   sock->SetRecvCallback(MakeCallback(&receive_callback));
121   // Keep track of what was used (for the TRACING module)
122   sock->SetDataSentCallback(MakeCallback(&datasent_callback));
123   XBT_DEBUG("startFlow of F[%p, %p, %d] dest=%s port=%d", flow, flow->action_, flow->totalBytes_, to, port_number);
124
125   //WriteUntilBufferFull (sock, sock->GetTxAvailable ());
126   /*
127   sock->SetSendCallback(MakeCallback(&send_callback));
128   sock->SetConnectCallback(MakeCallback(&succeededConnect_callback), MakeCallback(&failedConnect_callback));
129   sock->SetCloseCallbacks(MakeCallback(&normalClose_callback), MakeCallback(&errorClose_callback));
130   send_callback(sock, sock->GetTxAvailable ());
131    */
132
133 }