Earl Ou has uploaded this change for review. ( https://gem5-review.googlesource.com/c/public/gem5/+/34356 )

Change subject: systemc: use setjmp to speed up fiber
......................................................................

systemc: use setjmp to speed up fiber

ucontext is an order of magnitude slower compared to most of the fiber
implementation, mainly due to the additional signal mask operation.

This change applies the trick provided in
http://www.1024cores.net/home/lock-free-algorithms/tricks/fibers,
which uses _setjmp/_longjmp to switch between contexts created by
ucontext.

Combine with NodeList improvement, we see 81% speed improvement with the
example provided by  Jung Matthias:
https://gist.github.com/myzinsky/557200aa04556de44a317e0a10f51840

Compared with Accellera's SystemC, gem5 SystemC was originally 10x
slower, and with this change it's about 1.8x.

Change-Id: I0ffb6978e83dc8be049b750dc1baebb3d251601c
---
M src/base/fiber.cc
M src/base/fiber.hh
2 files changed, 16 insertions(+), 5 deletions(-)



diff --git a/src/base/fiber.cc b/src/base/fiber.cc
index 3d2e2e9..023c091 100644
--- a/src/base/fiber.cc
+++ b/src/base/fiber.cc
@@ -39,6 +39,7 @@
 #define _DARWIN_C_SOURCE
 #endif

+#include <setjmp.h>
 #include <sys/mman.h>
 #include <unistd.h>

@@ -145,10 +146,13 @@

     setStarted();

-    // Swap back to the parent context which is still considered "current",
-    // now that we're ready to go.
-    int ret M5_VAR_USED = swapcontext(&ctx, &_currentFiber->ctx);
-    panic_if(ret == -1, strerror(errno));
+    if (_setjmp(jmp) == 0)
+    {
+ // Swap back to the parent context which is still considered "current",
+        // now that we're ready to go.
+        int ret M5_VAR_USED = swapcontext(&ctx, &_currentFiber->ctx);
+        panic_if(ret == -1, strerror(errno));
+    }

     // Call main() when we're been reactivated for the first time.
     main();
@@ -175,7 +179,8 @@
     Fiber *prev = _currentFiber;
     Fiber *next = this;
     _currentFiber = next;
-    swapcontext(&prev->ctx, &next->ctx);
+    if (_setjmp(prev->jmp) == 0)
+        _longjmp(next->jmp, 1);
 }

 Fiber *Fiber::currentFiber() { return _currentFiber; }
diff --git a/src/base/fiber.hh b/src/base/fiber.hh
index dc7ef01..b3c9964 100644
--- a/src/base/fiber.hh
+++ b/src/base/fiber.hh
@@ -39,6 +39,8 @@
 #include <ucontext.h>
 #endif

+#include <setjmp.h>
+
 #include <cstddef>
 #include <cstdint>

@@ -137,6 +139,10 @@
     void start();

     ucontext_t ctx;
+ // ucontext is slow in swapcontext. Here we use _setjmp/_longjmp to avoid
+    // the additional signals for speed up.
+    jmp_buf jmp;
+
     Fiber *link;

     // The stack for this context, or a nullptr if allocated elsewhere.

--
To view, visit https://gem5-review.googlesource.com/c/public/gem5/+/34356
To unsubscribe, or for help writing mail filters, visit https://gem5-review.googlesource.com/settings

Gerrit-Project: public/gem5
Gerrit-Branch: develop
Gerrit-Change-Id: I0ffb6978e83dc8be049b750dc1baebb3d251601c
Gerrit-Change-Number: 34356
Gerrit-PatchSet: 1
Gerrit-Owner: Earl Ou <shunhsin...@google.com>
Gerrit-MessageType: newchange
_______________________________________________
gem5-dev mailing list -- gem5-dev@gem5.org
To unsubscribe send an email to gem5-dev-le...@gem5.org
%(web_page_url)slistinfo%(cgiext)s/%(_internal_name)s

Reply via email to