[[!meta copyright="Copyright © 2010, 2011, 2012, 2013, 2014 Free Software Foundation, Inc."]] [[!meta license="""[[!toggle id="license" text="GFDL 1.2+"]][[!toggleable id="license" text="Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2 or any later version published by the Free Software Foundation; with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts. A copy of the license is included in the section entitled [[GNU Free Documentation License|/fdl]]."]]"""]] [[!tag open_issue_hurd]] Hurd servers / VFS libraries are multithreaded. They can even be said to be "hyperthreaded". # Implementation * well-known threading libraries * [[hurd/libthreads]] * [[hurd/libpthread]] ## IRC, freenode, #hurd, 2011-04-20 so basically, a thread should consume only a few kernel resources in GNU Mach, it doesn't even consume a kernel stack because only continuations are used [[microkernel/mach/gnumach/continuation]]. and in userspace, it consumes 2 MiB of virtual memory, a few table entries, and almost no CPU time What does "hyperthreaded" mean: Do you have a reference? in this context, it just means there are a lot of threads even back in the 90s, the expected number of threads could scale up to the thousand today, it isn't much impressive any more but at the time, most systems didn't have LWPs yet and a process was very expensive Looks like I have some catching up to do: What is "continuations" and LWP? Maybe I also need a reference to an overview on multi-threading. Lightweight process? http://en.wikipedia.org/wiki/Light-weight_process LWPs are another names for kernel threads usually most current kernels support kernel preemption though [[microkernel/mach/gnumach/preemption]]. which means their state is saved based on scheduler decisions unlike continuations where the thread voluntarily saves its state if you only have continuations, you can't have kernel preemption, but you end up with one kernel stack per processor while the other model allows kernel preemption and requires one kernel stack per thread I know resources are limited, but it looks like kernel preemption would be nice to have. Is that too much for a GSoC student? it would require a lot of changes in obscure and sensitive parts of the kernel and no, kernel preemption is something we don't actually need even current debian linux kernels are built without kernel preemption and considering mach has hard limitations on its physical memory management, increasing the amount of memory used for kernel stacks would imply less available memory for the rest of the system Are these hard limits in mach difficult to change? yes consider mach difficult to change that's actually one of the goals of my stalled project which I hope to resume by the end of the year :/ Reading Wikipedia it looks like LWP are "kernel treads" and other threads are "user threads" at least in IBM/AIX. LWP in Linux is a thread sharing resources and in SunOS they are "user threads". Which is closest for Hurd? i told you 14:09 < braunr> LWPs are another names for kernel threads usually Similar to to the IBM definition then? Sorry for not remembering what I've been reading. # Design ## Application Programs ### [[glibc/signal/signal_thread]] ## Hurd Servers See [[hurd/libports]]: roughly using one thread per incoming request. This is not the best approach: it doesn't really make sense to scale the number of worker threads with the number of incoming requests, but instead they should be scaled according to the backends' characteristics. The [[hurd/Critique]] should have some more on this. [*Event-based Concurrency Control*](http://soft.vub.ac.be/~tvcutsem/talks/presentations/T37_nobackground.pdf), Tom Van Cutsem, 2009. ### IRC, freenode, #hurd, 2012-07-08 braunr: about limiting number of threads, IIRC the problem is that for some threads, completing their work means triggering some action in the server itself, and waiting for it (with, unfortunately, some lock held), which never terminates when we can't create new threads any more youpi: the number of threads should be limited, but not globally by libports pagers should throttle their writeback requests right ### IRC, freenode, #hurd, 2012-07-16 hm interesting when many threads are creating to handle requests, they automatically create a pool of worker threads by staying around for some time this time is given in the libport call but the thread always remain they must be used in turn each time a new requet comes in ah no :(, they're maintained by the periodic sync :( hm, still not that, so weird braunr: yes, that's a known problem: unused threads should go away after some time, but that doesn't actually happen don't remember though whether it's broken for some reason, or simply not implemented at all... (this was already a known issue when thread throttling was discussed around 2005...) antrik: ok hm threads actually do finish .. libthreads retain them in a pool for faster allocations hm, it's worse than i thought i think the hurd does its job well the cthreads code never reaps threads when threads are finished, they just wait until assigned a new invocation i don't understand ports_manage_port_operations_multithread :/ i think i get it why do people write things in such a complicated way .. such code is error prone and confuses anyone i wonder how well nested functions interact with threads when sharing variables :/ the simple idea of nested functions hurts my head do you see my point ? :) variables on the stack automatically shared between threads, without the need to explicitely pass them by address braunr: I don't understand. why would variables on the stack be shared between threads?... antrik: one function declares two variables, two nested functions, and use these in separate threads are the local variables still "local" ? braunr: I would think so? why wouldn't they? threads have separate stacks, right?... I must admit though that I have no idea how accessing local variables from the parent function works at all... me neither why don't demuxers get a generic void * like every callback does :(( ? antrik: they get pointers to the input and output messages only why is this a problem? ports_manage_port_operations_multithread can be called multiple times in the same process each call must have its own context currently this is done by using nested functions also, why demuxers return booleans while mach_msg_server_timeout happily ignores them :( callbacks shouldn't return anything anyway but then you have a totally meaningless "return 1" in the middle of the code i'd advise not using a single nested function I don't understand the remark about nested function they're just horrible extensions the compiler completely hides what happens behind the scenes, and nasty bugs could come out of that i'll try to rewrite ports_manage_port_operations_multithread without them and see if it changes anything but it's not easy also, it makes debugging harder :p i suspect gdb hangs are due to that, since threads directly start on a nested function and if i'm right, they are created on the stack (which is also horrible for security concerns, but that's another story) (at least the trampolines) I seriously doubt it will change anything... but feel free to prove me wrong :-) well, i can see really weird things, but it may have nothing to do with the fact functions are nested (i still strongly believe those shouldn't be used at all) ### IRC, freenode, #hurd, 2012-08-31 and the hurd is all but scalable I thought scalability was built-in already, at least for hurd?? built in ? designed in i guess you think that because you read "aggressively multithreaded" ? well, a system that is unable to control the amount of threads it creates for no valid reason and uses global lock about everywhere isn't really scalable it's not smp nor memory scalable most modern OSes have multi-cpu support. that doesn't mean they scale bsd sucks in this area it got better in recent years but they're way behind linux linux has this magic thing called rcu and i want that in my system, from the beginning and no, the hurd was never designed to scale that's obvious a very common mistake of the early 90s ### IRC, freenode, #hurd, 2012-09-06 mel-: the problem with such a true client/server architecture is that the scheduling context of clients is not transferred to servers mel-: and the hurd creates threads on demand, so if it's too slow to process requests, more threads are spawned to prevent hurd servers from creating too many threads, they are given a higher priority and it causes increased latency for normal user applications a better way, which is what modern synchronous microkernel based systems do is to transfer the scheduling context of the client to the server the server thread behaves like the client thread from the scheduler perspective how can creating more threads ease the slowness, is that a design decision?? what would be needed to implement this? mel-: thread migration gnu_srs: is that what i wrote ? does mach support it? mel-: some versions do yes mel-: not ours 21:49:03) braunr: mel-: and the hurd creates threads on demand, so if it's too slow to process requests, more threads are spawned of course it's a design decision it doesn't "ease the slowness" it makes servers able to use multiple processors to handle requests but it's a wrong design decision as the number of threads is completely unchecked what's the idea of creating more theads then, multiple cpus is not supported? it's a very old decision taken at a time when systems and machines were very different mach used to support multiple processors it was expected gnumach would do so too mel-: but getting thread migration would also require us to adjust our threading library and our servers it's not an easy task at all and it doesn't fix everything thread migration on mach is an optimization interesting async ipc remains available, which means notifications, which are async by nature, will create messages floods anyway ### IRC, freenode, #hurd, 2013-02-23 hmm let's try something iirc, we cannot limit the max number of threads in libports but did someone try limiting the number of threads used by libpager ? (the only source of system stability problems i currently have are the unthrottled writeback requests) braunr: perhaps we can limit the amount of requests batched by the ext2fs sync? youpi: that's another approach, yes (I'm not sure to understand what threads libpager create) youpi: one for each writeback request ew but it makes its own call to ports_manage_port_operations_multithread i'll write a new ports_manage_port_operations_multithread_n function that takes a mx threads parameter and see if it helps i thought replacing spin locks with mutexes would help, but it's not enough, the true problem is simply far too much contention youpi: i still think we should increase the page dirty timeout to 30 seconds wouldn't that actually increase the amount of request done in one go? it would but other systems (including linux) do that but they group requests what linux does is scan pages every 5 seconds, and writeback those who have been dirty for more than 30 secs hum yes but that's just a performance issue i mean, a separate one a great source of fs performance degradation is due to this regular scan happenning at the same time regular I/O calls are made e.G. aptitude update so, as a first step, until the sync scan is truley optimized, we could increase that interval I'm afraid of the resulting stability regression having 6 times as much writebacks to do i see my current patch seems to work fine for now i'll stress it some more (it limits the number of paging threads to 10 currently) but iirc, you fixed a deadlock with a debian patch there i think the case was a pager thread sending a request to the kernel, and waiting for the kernel to call another RPC that would unblock the pager thread ah yes it was merged upstream which means a thread calling memory_object_lock_request with sync == 1 must wait for a memory_object_lock_completed so it can deadlock, whatever the number of threads i'll try creating two separate pools with a limited number of threads then we probably have the same deadlock issue in pager_change_attributes btw hm no, i can still bring a hurd down easily with a large i/o request :( and now it just recovered after 20 seconds without any visible cpu or i/o usage .. i'm giving up on this libpager issue it simply requires a redesign ### IRC, freenode, #hurd, 2013-02-28 so what causes the stability issues? or is that not really known yet? the basic idea is that the kernel handles the page cache and writebacks aren't correctly throttled so a huge number of threads (several hundreds, sometimes thousands) are created when this pathological state is reached, it's very hard to recover because of the various sources of (low) I/O in the system a simple line sent to syslog increases the load average the solution requires reworking the libpager library, and probably the libdiskfs one too, perhaps others, certainly also the pagers maybe the kernel too, i'm not sure i'd say so because it manages a big part of the paging policy ### IRC, freenode, #hurd, 2013-03-02 i think i have a simple-enough solution for the writeback instability [[hurd/libpager]]. ### IRC, freenode, #hurd, 2013-03-29 some day i'd like to add a system call that threads can use to terminate themselves, passing their stack as a parameter for deallocation then, we should review the timeouts used with libports management having servers go away when unneeded is a valuable and visible feature of modularity [[open_issues/libpthread/t/fix_have_kernel_resources]]. ### IRC, freenode, #hurd, 2013-04-03 youpi: i also run without the libports_stability patch and i'd like it to be removed unless you still have a good reason to keep it around well, the reason I know is mentioned in the patch i.e. the box becomes unresponsive when all these threads wake up at the same time maybe we could just introduce some randomness in the sleep time youpi: i didn't experience the problem well, I did :) or if i did, it was very short for the libports stability, I'd really say take a random value between timeout/2 and timeout and that should just nicely fix the issue ok ### IRC, freenode, #hurd, 2013-11-30 "Thread storms". if you copy a large file for example, it is loaded in memory, each page is touched and becomes dirty, and when the file system requests them to be flushed, the kernel sends one message for each page the file system spawns a thread as soon as a message arrives and there is no idle thread left if the amount of message is large and arrives very quickly, a lot of threads are created and they compete for cpu time How do you plan to work around that? first i have to merge in some work about pagein clustering then i intend to implement a specific thread pool for paging messages with a fixed size something compareable for a kernel scheduler? no the problem in the hurd is that it spawns threads as soon as it needs the thread does both the receiving and the processing But you want to queue such threads? what i want is to separate those tasks for paging and manage action queues internally in the past, it was attempted to limit the amount ot threads in servers, but since receiving is bound with processing, and some actions in libpager depend on messages not yet received, file systems would sometimes freeze that's entirely the task of the hurd? One cannot solve that in the microkernel itself? it could, but it would involve redesigning the paging interface and the less there is in the microkernel, the better #### IRC, freenode, #hurd, 2013-12-03 i think our greatest problem currently is our file system and our paging library if someone can spend some time getting to know the details and fixing the major problems they have, we would have a much more stable system braunr: The paging library because it cannot predict or keep statistics on pages to evict or not? braunr: I.e. in short - is it a stability problem or a performance problem (or both :) ) it's a scalability problem the sclability problem makes paging so slow that paging requests stack up until the system becomes almost completely unresponsive ah So one should chase defpager code then no defpager is for anonymous memory vmm? Ah ok ofc our swap has problems of its own, but we don't suffer from it as much as from ext2fs From what I have picked up from the mailing lists is the ext2fs just because no one really have put lots of love in it? While paging is because it is hard? (and I am not at that level of wizardry!) no just because it was done at a time when memory was a lot smaller, and developers didn't anticipate the huge growth of data that came during the 90s and after that's what scalability is about properly dealing with any kind of quantity braunr: are we talking about libpager ? yes and ext2fs yeah, i got that one :p :) the linear scans are in ext2fs the main drawback of libpager is that it doesn't restrict the amount of concurrent paging requests i think we talked about that recently i don't remember maybe with someone else then that doesn't sound too hard to add, is it ? what are the requirements ? and more importantly, will it make the system faster ? it's not too hard well it's not that easy to do reliably because of the async nature of the paging requests teythoon: the problem with paging on top of mach is that paging requests are asynchronous ok libpager uses the bare thread pool from libports to deal with that, i.e. a thread is spawned as soon as a message arrives and all threads are busy if a lot of messages arrive in a burst, a lot of threads are created libports implies a lot of contention (which should hopefully be lowered with your payload patch) [[community/gsoc/project_ideas/object_lookups]]. that contention is part of the scalability problem a simple solution is to use a more controlled thread pool that merely queues requests until user threads can process them i'll try to make it clearer : we can't simply limit the amout of threads in libports, because some paging requests require the reception of future paging requests in order to complete an operation why would that help with the async nature of paging requests ? it wouldn't right thaht's a solution to the scalability problem, not to reliability well, that kind of queue could also be useful for the other hurd servers, no ? i don't think so why not ? teythoon: why would it ? the only other major async messages in the hurd are the no sender and dead name notification notifications* we could cap the number of threads two problems with that solution does not solve the dos issue, but makes it less interruptive, no? 1/ it would dynamically scale and 2/ it would prevent the reception of messages that allow operations to complete why would it block the reception ? it won't be processed, but accepting it should be possilbe because all worker threads would be blocked, waiting for a future message to arrive to complete, and no thread would be available to receive that message accepting, yes that's why i was suggesting a separate pool just for that 15:35 < braunr> a simple solution is to use a more controlled thread pool that merely queues requests until user threads can process them "user threads" is a poor choice i used that to mirror what happens in current kernels, where threads are blocked until the system tells them they can continue hm but user threads don't handle their own page faults on mach so how would the threads be blocked exactly, mach_msg ? phread_locks ? probably a pthread_hurd_cond_wait_np yes that's not really the problem why not ? that's the point where we could yield the thread and steal some work from our queue this solution (a specific thread pool of a limited number of threads to receive messages) has the advantage that it solves one part of the scalability issue if you do that, you loose the current state, and you have to use something like continuations instead indeed ;) this is about the same as making threads uninterruptible when waiting for IO in unix it makes things simpler less error prone but then, the problem has just been moved instead of a large number of threads, we might have a large number of queued requests actually, it's not completely asynchronous the pageout code in mach uses some heuristics to slow down it's ugly, and is the reason why the system can get extremely slow when swap is used solving that probably requires a new paging interface with the kernel ok, we will postpone this I'll have to look at libpager for the protected payload series anyways 15:38 < braunr> 1/ it would dynamically scale + not why not ? 15:37 < teythoon> we could cap the number of threads to what value ? we could adjust the number of threads and the queue size based on some magic unicorn function :) this one deserves a smiley too ^^ ### IRC, freenode, #hurd, 2014-03-02 braunr: what is the status of the thread storm issue? do you have pending code changes for this? I was wondering whether to make ext2fs use adaptative locks, i.e. spin a bit and then block I don't remember whether anybody already did something like that youpi: no i don't youpi: i attempted switch from spin locks to mutexes once but it doesn't solve the problem switching* found another storm maker: $ dpkg-reconfigure gnome-accessibility-themes aka update-icon-caches /usr/share/icons/HighContrast braunr: ok ## Alternative approaches: * * Continuation-passing style * [[microkernel/Mach]] internally [[uses continuations|microkernel/mach/gnumach/continuation]], too. * [[Erlang-style_parallelism]] * [[!wikipedia Actor_model]]; also see overlap with {{$capability#wikipedia_object-capability_model}}. * [libtcr - Threaded Coroutine Library](http://oss.linbit.com/libtcr/) * --- See also: [[multiprocessing]].