mastodontech.de ist einer von vielen unabhängigen Mastodon-Servern, mit dem du dich im Fediverse beteiligen kannst.
Offen für alle (über 16) und bereitgestellt von Markus'Blog

Serverstatistik:

1,5 Tsd.
aktive Profile

#threadpool

0 Beiträge0 Beteiligte0 Beiträge heute
Felix Palmen :freebsd: :c64:<p>Now that <a href="https://mastodon.bsd.cafe/tags/swad" class="mention hashtag" rel="nofollow noopener" target="_blank">#<span>swad</span></a> 0.7 is released, it's time to prepare a new release of <a href="https://mastodon.bsd.cafe/tags/poser" class="mention hashtag" rel="nofollow noopener" target="_blank">#<span>poser</span></a>, my own lib supporting <a href="https://mastodon.bsd.cafe/tags/services" class="mention hashtag" rel="nofollow noopener" target="_blank">#<span>services</span></a> on <a href="https://mastodon.bsd.cafe/tags/POSIX" class="mention hashtag" rel="nofollow noopener" target="_blank">#<span>POSIX</span></a> systems, following a <a href="https://mastodon.bsd.cafe/tags/reactor" class="mention hashtag" rel="nofollow noopener" target="_blank">#<span>reactor</span></a> with <a href="https://mastodon.bsd.cafe/tags/threadpool" class="mention hashtag" rel="nofollow noopener" target="_blank">#<span>threadpool</span></a> design.</p><p>During development of swad, I moved poser from using strictly only POSIX APIs (with the scalability limits of e.g. <a href="https://mastodon.bsd.cafe/tags/select" class="mention hashtag" rel="nofollow noopener" target="_blank">#<span>select</span></a>) to auto-detected support for <a href="https://mastodon.bsd.cafe/tags/kqueue" class="mention hashtag" rel="nofollow noopener" target="_blank">#<span>kqueue</span></a>, <a href="https://mastodon.bsd.cafe/tags/epoll" class="mention hashtag" rel="nofollow noopener" target="_blank">#<span>epoll</span></a>, <a href="https://mastodon.bsd.cafe/tags/eventports" class="mention hashtag" rel="nofollow noopener" target="_blank">#<span>eventports</span></a>, <a href="https://mastodon.bsd.cafe/tags/signalfd" class="mention hashtag" rel="nofollow noopener" target="_blank">#<span>signalfd</span></a> and <a href="https://mastodon.bsd.cafe/tags/timerfd" class="mention hashtag" rel="nofollow noopener" target="_blank">#<span>timerfd</span></a> (so now it could, in theory(!), "compete" with e.g. libevent). I also fixed quite some hidden bugs, and added more base functionality, like a <a href="https://mastodon.bsd.cafe/tags/dictionary" class="mention hashtag" rel="nofollow noopener" target="_blank">#<span>dictionary</span></a> using nested hashtables internally, or <a href="https://mastodon.bsd.cafe/tags/async" class="mention hashtag" rel="nofollow noopener" target="_blank">#<span>async</span></a> tasks mimicking the async/await pattern known from e.g, <a href="https://mastodon.bsd.cafe/tags/csharp" class="mention hashtag" rel="nofollow noopener" target="_blank">#<span>csharp</span></a>. I also deprecated two features, the periodic and global "service tick" (superseded by individual timers) and the "resolve hosts" property of a "connection" (superseded by a separate resolve class).</p><p>I'll have to decide on a few things, e.g. whether I'll remove the deprecated stuff immediately and bump the major version of the "posercore" lib. I guess I'll do just that. I'd also like to add all the web-specific stuff (http 1.0/1.1 server) that's currently part of the swad code as a "poserweb" lib. This would get a major version of 0, indicating a generally unstable API/ABI as of now....</p><p>And then, I'd have to decide where certain utility classes belong to. The rate limiter is probably useful for things other than web, so it should probably go to core. What about url encoding/decoding, for example? 🤔</p><p>Stay tuned, something will come here, maybe helping you to write a nice service in plain <a href="https://mastodon.bsd.cafe/tags/C" class="mention hashtag" rel="nofollow noopener" target="_blank">#<span>C</span></a> 😎:</p><p><a href="https://github.com/Zirias/poser" rel="nofollow noopener" translate="no" target="_blank"><span class="invisible">https://</span><span class="">github.com/Zirias/poser</span><span class="invisible"></span></a></p>
Felix Palmen :freebsd: :c64:<p>Revisiting <a href="https://mastodon.bsd.cafe/tags/async" class="mention hashtag" rel="nofollow noopener" target="_blank">#<span>async</span></a> / <a href="https://mastodon.bsd.cafe/tags/await" class="mention hashtag" rel="nofollow noopener" target="_blank">#<span>await</span></a> in <a href="https://mastodon.bsd.cafe/tags/POSIX" class="mention hashtag" rel="nofollow noopener" target="_blank">#<span>POSIX</span></a> C, trying to "add some <a href="https://mastodon.bsd.cafe/tags/security" class="mention hashtag" rel="nofollow noopener" target="_blank">#<span>security</span></a>" 🙈 </p><p>Recap: Consider a classic <a href="https://mastodon.bsd.cafe/tags/reactor" class="mention hashtag" rel="nofollow noopener" target="_blank">#<span>reactor</span></a>-style service in C with a <a href="https://mastodon.bsd.cafe/tags/threadpool" class="mention hashtag" rel="nofollow noopener" target="_blank">#<span>threadpool</span></a> attached to run the individual request handlers. When such a handler needs to do some I/O, it'll have to wait for its completion, and doing so is kind of straight forward by just blocking the worker thread executing the job until whatever I/O was needed completes.</p><p>Now, blocking a thread is never a great thing to do and I recently tooted about an interesting alternative I found: Make use of the (unfortunately deprecated) POSIX user context switching to enable releasing the worker thread while waiting. In a nutshell, you create a context with <a href="https://mastodon.bsd.cafe/tags/makecontext" class="mention hashtag" rel="nofollow noopener" target="_blank">#<span>makecontext</span></a> that has its own private <a href="https://mastodon.bsd.cafe/tags/stack" class="mention hashtag" rel="nofollow noopener" target="_blank">#<span>stack</span></a>, and then you can use <a href="https://mastodon.bsd.cafe/tags/swapcontext" class="mention hashtag" rel="nofollow noopener" target="_blank">#<span>swapcontext</span></a> to get off the thread, and later again to get back on the thread. A minor issue is: It must be the *same* thread ... so you might have to wait until it completes something else before you can resume your job. But then, that's probably okayish, you can make sure in your job scheduling to only use worker threads with awaited tasks attached when no other thread is available.</p><p>In my first implementation, I just used <a href="https://mastodon.bsd.cafe/tags/malloc" class="mention hashtag" rel="nofollow noopener" target="_blank">#<span>malloc</span></a> to create a 64kiB private stack for each thread job. That's perfectly fine if you can guarantee your job will never consume more stack space, AND it won't have any vulnerabilities allowing some attacker to mess with the stack. But in practice, especially for a library offering this async/await implementation, it's nothing but a wild <a href="https://mastodon.bsd.cafe/tags/CVE" class="mention hashtag" rel="nofollow noopener" target="_blank">#<span>CVE</span></a> generator.</p><p>So, I now improved on that:</p><p>* Allocate a much larger stack of now 2MiB. That alone makes issues at least less likely. And on a sane modern OS, we can still assume pages will only be mapped "on demand".<br>* Only allocate the stack directly before running the thread job, and delegate allocation to some internal "stack manager" that keeps track of all allocated stacks and reuses them, only freeing them on exit. This should avoid most of the allocation overhead.<br>* If MAP_ANON / MAP_ANONYMOUS is available, use <a href="https://mastodon.bsd.cafe/tags/mmap" class="mention hashtag" rel="nofollow noopener" target="_blank">#<span>mmap</span></a> for allocating the stack. That at least gives a chance to stay away from other allocations ....<br>* But finally, if MAP_STACK is available, use this flag! From my research, <a href="https://mastodon.bsd.cafe/tags/FreeBSD" class="mention hashtag" rel="nofollow noopener" target="_blank">#<span>FreeBSD</span></a>, <a href="https://mastodon.bsd.cafe/tags/OpenBSD" class="mention hashtag" rel="nofollow noopener" target="_blank">#<span>OpenBSD</span></a> and <a href="https://mastodon.bsd.cafe/tags/NetBSD" class="mention hashtag" rel="nofollow noopener" target="_blank">#<span>NetBSD</span></a> will for example make sure there's at least one "guard page" below a stack mapped with this flag, so a stack overflow consistently takes the SIGSEGV emergency exit 😆. <a href="https://mastodon.bsd.cafe/tags/Linux" class="mention hashtag" rel="nofollow noopener" target="_blank">#<span>Linux</span></a> knows this flag as well, but doesn't seem to implement such protection at this time ... 🤔 </p><p><a href="https://mastodon.bsd.cafe/tags/C" class="mention hashtag" rel="nofollow noopener" target="_blank">#<span>C</span></a> <a href="https://mastodon.bsd.cafe/tags/coding" class="mention hashtag" rel="nofollow noopener" target="_blank">#<span>coding</span></a></p>
Felix Palmen :freebsd: :c64:<p>Nice, <a href="https://mastodon.bsd.cafe/tags/threadpool" class="mention hashtag" rel="nofollow noopener" target="_blank">#<span>threadpool</span></a> overhaul done. Removed two locks (<a href="https://mastodon.bsd.cafe/tags/mutex" class="mention hashtag" rel="nofollow noopener" target="_blank">#<span>mutex</span></a>) and two condition variables, replaced by a single lock and a single <a href="https://mastodon.bsd.cafe/tags/semaphore" class="mention hashtag" rel="nofollow noopener" target="_blank">#<span>semaphore</span></a>. 😎 Simplifies the overall structure a lot, and it's probably safe to assume slightly better performance in contended situations as well. And so far, <a href="https://mastodon.bsd.cafe/tags/valgrind" class="mention hashtag" rel="nofollow noopener" target="_blank">#<span>valgrind</span></a>'s helgrind tool doesn't find anything to complain about. 🙃</p><p>Looking at the screenshot, I should probably make <a href="https://mastodon.bsd.cafe/tags/swad" class="mention hashtag" rel="nofollow noopener" target="_blank">#<span>swad</span></a> default to *two* threads per CPU and expose the setting in the configuration file. When some thread jobs are expected to block, having more threads than CPUs is probably better.</p><p><a href="https://github.com/Zirias/poser/commit/995c27352615a65723fbd1833b2d36781cbeff4d" rel="nofollow noopener" translate="no" target="_blank"><span class="invisible">https://</span><span class="ellipsis">github.com/Zirias/poser/commit</span><span class="invisible">/995c27352615a65723fbd1833b2d36781cbeff4d</span></a></p>
Felix Palmen :freebsd: :c64:<p>Still working on <a href="https://mastodon.bsd.cafe/tags/swad" class="mention hashtag" rel="nofollow noopener" target="_blank">#<span>swad</span></a>, and currently very busy with improving quality, most of the actual work done inside my <a href="https://mastodon.bsd.cafe/tags/poser" class="mention hashtag" rel="nofollow noopener" target="_blank">#<span>poser</span></a> library.</p><p>After finally supporting <a href="https://mastodon.bsd.cafe/tags/kqueue" class="mention hashtag" rel="nofollow noopener" target="_blank">#<span>kqueue</span></a> and <a href="https://mastodon.bsd.cafe/tags/epoll" class="mention hashtag" rel="nofollow noopener" target="_blank">#<span>epoll</span></a>, I now integrated <a href="https://mastodon.bsd.cafe/tags/xxhash" class="mention hashtag" rel="nofollow noopener" target="_blank">#<span>xxhash</span></a> to completely replace my previous stupid and naive hashing. I also added a more involved <a href="https://mastodon.bsd.cafe/tags/dictionary" class="mention hashtag" rel="nofollow noopener" target="_blank">#<span>dictionary</span></a> class as an alternative to the already existing <a href="https://mastodon.bsd.cafe/tags/hashtable" class="mention hashtag" rel="nofollow noopener" target="_blank">#<span>hashtable</span></a>. While the hashtable's size must be pre-configured and collissions are only ever resolved by storing linked lists, the new dictionary dynamically nests multiple hashtables (using different bits of a single hash value). I hope to achieve acceptable scaling while maintaining also acceptable memory overhead that way ...</p><p><a href="https://mastodon.bsd.cafe/tags/swad" class="mention hashtag" rel="nofollow noopener" target="_blank">#<span>swad</span></a> already uses both container classes as appropriate.</p><p>Next I'll probably revisit poser's <a href="https://mastodon.bsd.cafe/tags/threadpool" class="mention hashtag" rel="nofollow noopener" target="_blank">#<span>threadpool</span></a>. I think I could replace <a href="https://mastodon.bsd.cafe/tags/pthread" class="mention hashtag" rel="nofollow noopener" target="_blank">#<span>pthread</span></a> condition variables by "simple" <a href="https://mastodon.bsd.cafe/tags/semaphores" class="mention hashtag" rel="nofollow noopener" target="_blank">#<span>semaphores</span></a>, which should also reduce overhead ... </p><p><a href="https://github.com/Zirias/swad" rel="nofollow noopener" translate="no" target="_blank"><span class="invisible">https://</span><span class="">github.com/Zirias/swad</span><span class="invisible"></span></a></p><p><a href="https://mastodon.bsd.cafe/tags/c" class="mention hashtag" rel="nofollow noopener" target="_blank">#<span>c</span></a> <a href="https://mastodon.bsd.cafe/tags/coding" class="mention hashtag" rel="nofollow noopener" target="_blank">#<span>coding</span></a></p>
Dave Tabb<p><span class="h-card" translate="no"><a href="https://mastodon.social/@khalidabuhakmeh" class="u-url mention" rel="nofollow noopener" target="_blank">@<span>khalidabuhakmeh</span></a></span> Yeah, the business of ensuring that all the threads had finished running was a little awkward!<br><a href="https://mastodon.africa/tags/ThreadPool" class="mention hashtag" rel="nofollow noopener" target="_blank">#<span>ThreadPool</span></a>.GetAvailableThreads<br><a href="https://mastodon.africa/tags/ThreadPool" class="mention hashtag" rel="nofollow noopener" target="_blank">#<span>ThreadPool</span></a>.GetMaxThreads</p>
Dave Tabb<p>Today, I multi-threaded some data analysis code in the C# language, and I DID NOT DIE!<br><a href="https://mastodon.africa/tags/DotNet" class="mention hashtag" rel="nofollow noopener" target="_blank">#<span>DotNet</span></a> <a href="https://mastodon.africa/tags/ThreadPool" class="mention hashtag" rel="nofollow noopener" target="_blank">#<span>ThreadPool</span></a></p>