Caleb James DeLisle 9 anos atrás
pai
commit
ab3a0f4452
5 arquivos alterados com 41 adições e 15 exclusões
  1. 0 1
      interface/Iface.h
  2. 6 1
      memory/Allocator.c
  3. 4 4
      net/InterfaceController.c
  4. 30 8
      net/PeerLink.c
  5. 1 1
      node_build/make.js

+ 0 - 1
interface/Iface.h

@@ -58,7 +58,6 @@ static inline void Iface_send(struct Iface* iface, struct Message* msg)
         struct Iface* conn = iface->connectedIf;
 
         #ifdef PARANOIA
-            Assert_true(!msg->currentIface);
             Assert_true(conn);
             Assert_true(conn->send);
             Assert_true(msg);

+ 6 - 1
memory/Allocator.c

@@ -668,6 +668,11 @@ void Allocator__disown(struct Allocator* parentAlloc,
         return;
     }
 
+    if (isAncestorOf(child, parent)) {
+        // Rare but possible way that the child would never have been adopted.
+        return;
+    }
+
     disconnectAdopted(parent, child);
 }
 
@@ -698,7 +703,7 @@ void Allocator__adopt(struct Allocator* adoptedParent,
     }
 
     struct Allocator_List* pl =
-        Allocator__calloc(adoptedParent, sizeof(struct Allocator_List), 1, file, line);
+        Allocator__calloc(childToAdopt, sizeof(struct Allocator_List), 1, file, line);
     pl->alloc = child;
     pl->next = parent->adoptions->children;
     parent->adoptions->children = pl;

+ 4 - 4
net/InterfaceController.c

@@ -455,10 +455,10 @@ static Iface_DEFUN sendFromSwitch(struct Message* msg, struct Iface* switchIf)
 
     ep->bytesOut += msg->length;
 
-    //int msgs = PeerLink_send(msg, ep->peerLink);
+    int msgs = PeerLink_send(msg, ep->peerLink);
 
-    //for (int i = 0; i < msgs; i++) {
-        //msg = PeerLink_poll(ep->peerLink);
+    for (int i = 0; i < msgs; i++) {
+        msg = PeerLink_poll(ep->peerLink);
         Assert_true(!CryptoAuth_encrypt(ep->caSession, msg));
 
         Assert_true(!(((uintptr_t)msg->bytes) % 4) && "alignment fault");
@@ -474,7 +474,7 @@ static Iface_DEFUN sendFromSwitch(struct Message* msg, struct Iface* switchIf)
         }
 
         Iface_send(&ep->ici->pub.addrIf, msg);
-    //}
+    }
     return NULL;
 }
 

+ 30 - 8
net/PeerLink.c

@@ -21,22 +21,46 @@
 #define ArrayList_NAME Messages
 #include "util/ArrayList.h"
 
+struct Bandwidth {
+    uint32_t lastMessageTime;
+    uint32_t bandwidth;
+};
+
 struct PeerLink_pvt
 {
     struct PeerLink pub;
     struct Allocator* alloc;
     struct EventBase* base;
     struct ArrayList_Messages* queue;
+    struct Bandwidth sendBw;
+    struct Bandwidth recvBw;
     Identity
 };
 
+/**
+ * Calculate the number of bytes per second which was sent, using only the size of
+ * the current message amortized over the timewindow since the previous message was sent.
+ */
+static int instantaniousBandwidth(int messageLength, int millisecondsSinceLast)
+{
+    if (millisecondsSinceLast >= 1024) { return messageLength; }
+    return ( messageLength * ((1024 - millisecondsSinceLast) << 4) ) >> 4;
+}
+
+static void calcNextBandwidth(struct Bandwidth* bw, int messageLength, struct PeerLink_pvt* pl)
+{
+    uint64_t now = Time_currentTimeMilliseconds(pl->base);
+    if (now <= bw->lastMessageTime) { now = bw->lastMessageTime; }
+    int ibw = instantaniousBandwidth(messageLength, now - bw->lastMessageTime);
+    
+}
 
 struct Message* PeerLink_poll(struct PeerLink* peerLink)
 {
     struct PeerLink_pvt* pl = Identity_check((struct PeerLink_pvt*) peerLink);
-    struct Message* out = ArrayList_Messages_get(pl->queue, 0); //ArrayList_Messages_shift(pl->queue);
+    struct Message* out = ArrayList_Messages_shift(pl->queue);
     if (out) {
-        //Allocator_disown(pl->alloc, out->alloc);
+        Allocator_disown(pl->alloc, out->alloc);
     }
     return out;
 }
@@ -44,7 +68,7 @@ struct Message* PeerLink_poll(struct PeerLink* peerLink)
 int PeerLink_send(struct Message* msg, struct PeerLink* peerLink)
 {
     struct PeerLink_pvt* pl = Identity_check((struct PeerLink_pvt*) peerLink);
-    //Allocator_adopt(pl->alloc, msg->alloc);
+    Allocator_adopt(pl->alloc, msg->alloc);
     ArrayList_Messages_add(pl->queue, msg);
     return pl->queue->length;
 }
@@ -57,12 +81,10 @@ void PeerLink_recv(struct Message* msg, struct PeerLink* pl)
 
 struct PeerLink* PeerLink_new(struct EventBase* base, struct Allocator* alloc)
 {
-    struct PeerLink_pvt* pl = Allocator_calloc(alloc, sizeof(struct PeerLink_pvt*), 1);
+    struct PeerLink_pvt* pl = Allocator_calloc(alloc, sizeof(struct PeerLink_pvt), 1);
     Identity_set(pl);
-    //pl->base = base;
+    pl->base = base;
     pl->alloc = alloc;
-Allocator_realloc(alloc, pl, 0);
-    return NULL;
-    //pl->queue = ArrayList_Messages_new(alloc);
+    pl->queue = ArrayList_Messages_new(alloc);
     return &pl->pub;
 }

+ 1 - 1
node_build/make.js

@@ -44,7 +44,7 @@ Builder.configure({
     crossCompiling: process.env['CROSS'] !== undefined,
     gcc:            GCC,
     tempDir:        '/tmp',
-    optimizeLevel:  '-O0',
+    optimizeLevel:  '-O3',
     logLevel:       process.env['Log_LEVEL'] || 'DEBUG'
 }, function (builder, waitFor) {
     builder.config.cflags.push(