toffaletti / state-threads Goto Github PK
View Code? Open in Web Editor NEWFork of http://state-threads.sourceforge.net/
Home Page: http://state-threads.sourceforge.net/
Fork of http://state-threads.sourceforge.net/
Home Page: http://state-threads.sourceforge.net/
This is my fork of the state-threads library from http://state-threads.sourceforge.net/ Changes include: - CMake build system - Valgrind support for stack swaps - Minor updates for modern C compilers (gcc/clang) - pthread support (using __thread gcc extension) See README.orig for more information.
Probably forgotten: results in multiple worker pthreads sharing key counters. Cured with this simple patch:
diff --git a/key.c b/key.c
index 9708c35..9133e65 100644
--- a/key.c
+++ b/key.c
@@ -47,8 +47,8 @@
/*
As a result, st_usleep(999) is equivalent to st_usleep(0).
Proposed solution:
diff --git a/event.c b/event.c
index 6ee32f7..f5ea4d9 100644
--- a/event.c
+++ b/event.c
@@ -1288,7 +1288,7 @@ ST_HIDDEN void _st_epoll_dispatch(void)
/* Check for I/O operations */
nfd = epoll_wait(_st_epoll_data->epfd, _st_epoll_data->evtlist,
- _st_epoll_data->evtlist_size, timeout);
+ _st_epoll_data->evtlist_size, timeout == -1 ? -1 : 0);
if (nfd > 0) {
for (i = 0; i < nfd; i++) {
@@ -1359,6 +1359,14 @@ ST_HIDDEN void _st_epoll_dispatch(void)
}
}
}
+ else if (nfd == 0) {
+ if (timeout >= 0) {
+ struct timespec t;
+ t.tv_sec = min_timeout / 1000000;
+ t.tv_nsec = min_timeout * 1000;
+ nanosleep(&t, NULL);
+ }
+ }
}
I came across a performance issue in epoll mode, when there were thousands concurrent connections. Profiling shows that _st_epoll_dispatch() consumed a lot of CPU.
After reviewing the function, I think I've found the reason: there's a loop that enumerates ALL threads in the I/O queue.
for (q = _ST_IOQ.next; q != &_ST_IOQ; q = q->next) {
As I'm using one thread per connection model, I believe this loop make epoll mode degraded effectively to select mode.
This causes CLOSE_WAIT-state sockets to linger. This fix cures it:
@@ -142,7 +142,7 @@ static __thread struct _st_epolldata {
#define _ST_EPOLL_EXCEP_CNT(fd) (_st_epoll_data->fd_data[fd].ex_ref_cnt)
#define _ST_EPOLL_REVENTS(fd) (_st_epoll_data->fd_data[fd].revents)
-#define _ST_EPOLL_READ_BIT(fd) (_ST_EPOLL_READ_CNT(fd) ? EPOLLIN : 0)
+#define _ST_EPOLL_READ_BIT(fd) (_ST_EPOLL_READ_CNT(fd) ? (EPOLLIN|EPOLLRDHUP) : 0)
#define _ST_EPOLL_WRITE_BIT(fd) (_ST_EPOLL_WRITE_CNT(fd) ? EPOLLOUT : 0)
#define _ST_EPOLL_EXCEP_BIT(fd) (_ST_EPOLL_EXCEP_CNT(fd) ? EPOLLPRI : 0)
#define _ST_EPOLL_EVENTS(fd) \
commit accd04f in my fork.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.