From a8f508b1678383741af9605dc2ff0c468270a80b Mon Sep 17 00:00:00 2001
From: Martin Karsten <mkarsten@uwaterloo.ca>
Date: Thu, 5 Jul 2018 23:06:06 -0400
Subject: [PATCH] - clerical updates to webserver program (max listen backlog,
 Go concurrency) - bug fixes in debugging support - make poller pthread with
 tight polling default - change order of scheduling when using poller fibre

---
 src/libfibre/EventEngine.h      |  2 +-
 src/libfibre/Fibre.h            |  6 ++----
 src/libfibre/libfibre.so-gdb.py |  5 +----
 src/libfibre/webserver.cpp      | 16 +++++++++++++---
 src/libfibre/webserver.go       |  7 ++++---
 src/runtime/BaseProcessor.cc    |  6 ++++++
 src/testoptions.h               |  4 ++--
 7 files changed, 29 insertions(+), 17 deletions(-)

diff --git a/src/libfibre/EventEngine.h b/src/libfibre/EventEngine.h
index dcdd7f6..8a5b01e 100644
--- a/src/libfibre/EventEngine.h
+++ b/src/libfibre/EventEngine.h
@@ -152,7 +152,7 @@ public:
 
   template<bool Input, bool Yield, bool Lock, typename T, class... Args>
   T syncIO( T (*iofunc)(int, Args...), int fd, Args... a) {
-    GENASSERT(fd >= 0 && fd < fdcount);
+    GENASSERTN(fd >= 0 && fd < fdcount, fd, fdcount);
     SyncIO& sync = Input ? fdSyncVector[fd].RD : fdSyncVector[fd].WR;
     if (Yield) Fibre::yield();
     if (Lock) sync.lock.acquire();
diff --git a/src/libfibre/Fibre.h b/src/libfibre/Fibre.h
index 75b426a..0e861a8 100644
--- a/src/libfibre/Fibre.h
+++ b/src/libfibre/Fibre.h
@@ -102,15 +102,13 @@ public:
   : StackContext(sp), stackSize(0) { initDebug(); }
 
   // synchronize at object destruction
-  ~Fibre() {
-    join();
-    clearDebug();
-  }
+  ~Fibre() { join(); }
   void join() { done.wait(); }
   void detach() { done.detach(); }
 
   // callback from StackContext via Runtime after terminal context switch
   void destroy(_friend<Runtime>) {
+    clearDebug();
     stackFree();
     done.post();
   }
diff --git a/src/libfibre/libfibre.so-gdb.py b/src/libfibre/libfibre.so-gdb.py
index ec8f237..659f047 100644
--- a/src/libfibre/libfibre.so-gdb.py
+++ b/src/libfibre/libfibre.so-gdb.py
@@ -9,13 +9,10 @@ class FibreSupport():
         FibreSupport.list = []
         FibreSupport.active = {}
         FibreSupport.saved = False
-        if (gdb.lookup_symbol("_globalStackList")[0] == None):
-            print()
-            print("WARNING: no fibre debugging support - did you enable TESTING_ENABLE_DEBUGGING?")
-            print()
 
     def stop_handler(event):
         if (gdb.lookup_symbol("_globalStackList")[0] == None):
+            print("WARNING: no fibre debugging support - did you enable TESTING_ENABLE_DEBUGGING?")
             return
         # save register context for later continuation
         FibreSupport.rsp = str(gdb.parse_and_eval("$rsp")).split(None, 1)[0]
diff --git a/src/libfibre/webserver.cpp b/src/libfibre/webserver.cpp
index 04a5193..e52a93f 100644
--- a/src/libfibre/webserver.cpp
+++ b/src/libfibre/webserver.cpp
@@ -16,6 +16,7 @@
 ******************************************************************************/
 #include <iostream>
 #include <sstream>
+#include <fstream>
 #include <string>
 #include <list>
 #include <map>
@@ -93,6 +94,9 @@ static unsigned int threadCount = 1;
 static bool affinityFlag = false;
 static bool singleServerSocket = true;
 
+// system configuration, if needed (set listen backlog to maximum value)
+static int maxBacklog = -1;
+
 // define request handler
 typedef void (*UrlHandler)(void* fd, const char* path, int minor_version);
 
@@ -274,7 +278,7 @@ closeAndOut:
   
 #if defined __U_CPLUSPLUS__
 static uSocketServer* create_socket() {
-  return new uSocketServer(8800, SOCK_STREAM, 0, 65535);
+  return new uSocketServer(8800, SOCK_STREAM, 0, maxBacklog);
 }
 
 #else
@@ -302,8 +306,8 @@ static int create_socket(bool singleAccept = false) {
   sockaddr_in addr = { AF_INET, htons(8800), { INADDR_ANY }, { 0 } };
 #endif
   SYSCALL(lfBind(fd, (sockaddr*)&addr, sizeof(addr)));
-  if (singleAccept) SYSCALL(lfListen(fd, 4));
-  else SYSCALL(lfListen(fd, 16384));
+  if (singleAccept) SYSCALL(lfListen(fd, 0));
+  else SYSCALL(lfListen(fd, maxBacklog));
   return fd;
 }
 #endif
@@ -379,6 +383,12 @@ int main(int argc, char** argv) {
   SYSCALL(sigaction(SIGQUIT, &sa, 0));
   SYSCALL(sigaction(SIGTERM, &sa, 0));
 
+#if __linux__
+  // read max backlog setting
+  ifstream f("/proc/sys/net/ipv4/tcp_max_syn_backlog");
+  f >> maxBacklog;
+#endif
+
 #if defined __LIBFIBRE__ || defined __U_CPLUSPLUS__
 
   // set additional clusters and processors
diff --git a/src/libfibre/webserver.go b/src/libfibre/webserver.go
index e92f08c..f40098c 100644
--- a/src/libfibre/webserver.go
+++ b/src/libfibre/webserver.go
@@ -13,7 +13,7 @@ import (
 
 var (
 	clusterSize   = flag.Int("c", 64, "Size of clusters (ignored)")
-	listenerCount = flag.Int("l", 1, "Number of listeners (ignored)")
+	listenerCount = flag.Int("l", 1, "Number of listeners")
 	threadcount   = flag.Int("t", 1, "Number of system threads")
 	listenAddr    = flag.String("addr", ":8800", "TCP address to listen to")
 )
@@ -30,8 +30,9 @@ func main() {
 	flag.Parse()
   runtime.GOMAXPROCS(*threadcount)
 	s := &fasthttp.Server{
-		Handler: mainHandler,
-		Name:    "go",
+		Name:        "go",
+		Handler:     mainHandler,
+		Concurrency: 1048576,
 	}
 	for i := 0; i < *listenerCount; i++ {
 		ln := GetListener()
diff --git a/src/runtime/BaseProcessor.cc b/src/runtime/BaseProcessor.cc
index cb175c5..c707705 100644
--- a/src/runtime/BaseProcessor.cc
+++ b/src/runtime/BaseProcessor.cc
@@ -102,8 +102,14 @@ StackContext* BaseProcessor::schedule(_friend<StackContext>) {
   StackContext* nextStack;
   if (tryDequeue(nextStack)) return nextStack;
   if (terminate) return idleStack;
+#if TESTING_POLLER_FIBRES
+  if (tryBorrow(nextStack)) return nextStack;
+  if (tryStage(nextStack)) return nextStack;
+  if (trySteal(nextStack)) return nextStack;
+#else
   if (tryStage(nextStack)) return nextStack;
   if (trySteal(nextStack)) return nextStack;
   if (tryBorrow(nextStack)) return nextStack;
+#endif
   return idleStack;
 }
diff --git a/src/testoptions.h b/src/testoptions.h
index eaa907b..77debd7 100644
--- a/src/testoptions.h
+++ b/src/testoptions.h
@@ -30,8 +30,8 @@
 // **** libfibre options - event handling
 
 //#define TESTING_POLLER_FIBRES         1 // vs. per-cluster poller pthread
-#define TESTING_POLLER_IDLEWAIT       1 // vs. wake up poller thread anytime
-#define TESTING_POLLER_IDLETIMEDWAIT  1 // vs. wait indefinitely for idle
+//#define TESTING_POLLER_IDLEWAIT       1 // vs. wake up poller thread anytime
+//#define TESTING_POLLER_IDLETIMEDWAIT  1 // vs. wait indefinitely for idle
 #define TESTING_BULK_RESUME           1 // vs. individual resume
 #define TESTING_LAZY_FD_REGISTRATION  1 // vs. eager registration after fd creation
 
-- 
GitLab