HackerTube
HomeLongsLikedHistoryWatch laterCategoriesPlaylistsSettings

User-level threads....... with threads. - Paul Turner - Google

Linux Plumbers Conference6 HN recommenders · recommended in 6 months, 2013-2024
YouTube
41K views · 13 years ago · talk · evergreen

Paul Turner of Google presents kernel interfaces for user-level scheduling of real threads, aiming at a uniform synchronous programming model across C++, Java, Go and Python. It introduces the switchto-style system calls behind Google's fibers.

HN cites it as the key explanation of cooperative, user-scheduled kernel threads and the prior art behind later async and coroutine debates.

Operating systems & kernelsLinux & open-source ecosystems#linuxkernel#fibers#scheduling#concurrency#switchto#google

20 comments from Hacker News

@jsnell2024 · in “Async hazard: MMAP is blocking IO”

The term to search for prior art is user-mode scheduler / scheduling. Basically you add additional kernel features that allow making some scheduling decisions in the application, it's not something you'd just build on a vanilla pthreads implementation. Examples: Windows 7 UMS: link google3 fibers / switchto: ▶ this video

CitesView on HN
@eklitzke2023 · in “Coroutines for Go”

I think it's also worth mentioning that for certain specific use cases coroutines are much more efficient than a full goroutine, because switching to/from a coroutine doesn't require context switching or rescheduling anything. If you have two cooperating tasks that are logically synchronous anyway (e.g. an iterator) it's much more efficient to just run everything on the same CPU because the kernel doesn't have to reschedule anything or power down/wake up CPU cores, and the data is in the right CPU caches, so you'll get better cache latency and hit rates. With goroutines this may happen anyway, but it's not guaranteed and at the minimum you have the overhead of going through the Go runtime goroutine scheduler which is fast, but not as fast as just executing a different code context in the same goroutine. Coroutines offer more predictable scheduling behavior because you know that task A is switching directly to task B, whereas otherwise the goroutine scheduler could switch to another available task that wants to run. The last section of the blog post goes into this, where Russ shows that an optimized runtime implementation of coroutines is 10x faster than emulating them with goroutines. Google has internal kernel patches that implement cooperating multithreading this way (internally they're called fibers), and the patches exist for precisely this reason: better latency and more predictable scheduling behavior. Paul Turner gave a presentation about this at LPC ~10 years ago that

CitesView on HN
@m0zg2021 · in “C++ Coroutines Do Not Spark Joy”

Google needs to get off its ass and open source Fibers (not to be confused with Google Fiber). Fibers sparked plenty of joy in me back when I was at Google. It's just a more dignified way of doing things with threads, IMO. Here's the talk which, unfortunately, does not show the higher level APIs I'm talking about: ▶ this video . If this were released today everyone would slap their foreheads and say "of course that's how it should be done". Google folks have already removed the obstacle in that direction by adding userspace switching to the kernel in recent kernel releases. Just go all the way now, and release the kraken. Last update I've seen on this is this article from June of this year: link Imagine how excruciating it must have been to all the Google people on the C++ Standard committee to work on this stuff that comes nowhere close to what they've been using internally for years, and not be able to talk about it in any kind of detail.

RecommendsView on HN
@MichaelMoser1232021 · in “Moving Google toward the mainline”

Thanks for the link, it is explained in the linked video: ▶ this video They explain it around 15:01 - google added it's own syscall switchto_thread - that puts the current thread to sleep and is switching to the argument thread id. (and some other calls too). That one helps with cutting down latency in inter thread calls for m:n threading. The real effort is to make latency for individual application requests predictable, while keeping it low.

CitesView on HN
@kccqzy2021 · in “Moving Google toward the mainline”

Here: ▶ this video Jump to the 15-minute mark for the three new system calls, switchto_wait, switchto_resume, switchto_switch.

CitesView on HN
@Qerub2020 · in “Fibers, Oh My”

Oh, yes. I've been looking forward to this since I saw ▶ this video (User-level threads....... with threads. - Paul Turner - Google) in 2013.

CitesView on HN
@nwlieb2020 · in “A hybrid thread / fiber task scheduler written in C++ 11”

Could you describe what makes the Google Fibers so nice? I'm also really curious why they require modifications to the Linux kernel. My first guess would be stronger integration with the IO model at the syscall boundary (similar to io_uring). Edit: is this the talk your referring to? ▶ this video

CitesView on HN
@scottlamb2020 · in “A hybrid thread / fiber task scheduler written in C++ 11”

I don't think "fiber" has a universally agreed definition, which makes these things super confusing to read without clarification. It's hard to come up with a good definition and stick to it because people keep coming up with new variations. An interesting case is a different Google fiber library, not open-sourced but described in a public talk. [1] Real kernel threads but (mostly) scheduled in userspace via new syscalls [2] that suspend the current thread and unsuspend a userspace-chosen other thread. The idea is that most of the overhead of kernel threads is the scheduling and userspace has the ability to make a quick good choice. [3] Thus you can get most of the CPU performance of an async implementation but the relatively easy-to-read callback-free code of a threaded implementation. Anyway, people can disagree on what to call such a combination. [1] ▶ this video [2] the rseq stuff described in that talk has been upstreamed [4] but the switchto stuff still hasn't. [3] I'm not sure if the performance argument is as strong since the Meltdown/Spectre mitigations added to the performance cost of any syscall. [4] link

CitesView on HN
@Veserv2020 · in “Ways of Implementing a System Call”

Although you are technically correct in that the cost of system calls on most systems is high, your post also implies to a casual reader that the cost is fundamental. This is not true. As demonstrated in the benchmark, the hardware cost of systems calls/trapping is quite low. Even accounting for hardware workarounds (which were not even known about when the common consensus decided system calls were slow), excepting the case of hardware virtualization which has much more expensive mitigations, the fundamental cost is a fraction of the actual cost. The majority of the cost (~90+%) is actually due to software. In the system call case this is the implementation of the system call. In the context switch case this is usually the cost of the scheduling decision. These operations take material amounts of time and access memory which evicts user caches. It is in speeding up the software component that real gains can be made. This is not to say that system calls could ever be made "free" since function calls/loads/stores will still be ~100x faster, but the cost would probably be ~10% of the current cost if they software component could magically be made instant. As an example of what I am talking about, there is this talk by Google from 2013 [1] which describes a "fast context switch" system call (benchmark at 18:40). This "context switch" knows what it is switching to which lets it bypass the scheduling decision in normal context switches. As a result it takes ~170 ns (which is prett

CitesView on HN
@pron2019 · in “Why async fn in traits are hard”

> Can we identify why exactly 1:1 threading is uncompetitive with async/await schemes, and fix that? I think we can identify it, but fixing it is not easy, at least at the kernel level. The easy part is the cost of scheduling. The kernel must schedule threads with very different behaviors -- say, encoding a video or serving requests over a socket -- so it uses a scheduling algorithm that's a compromise for all uses. But we can let the language's runtime take the scheduling of kernel threads with something like this: ▶ this video The harder part is managing the memory required for the stack. Even on 64-bit systems, the OS can't shrink and grow stacks finely enough. For example, I don't think there's a hard requirement that, say, memory below the sp is never accessed, so the OS can't even be sure about how much of the stack is used and can't uncommit pages. But even if there were such a requirement, or, that the language could tell the OS it follows such a requirement, still the OS can only manage memory at a page granularity, which is too much for lightweight concurrency. Any finer than that requires knowing about all pointers into the stack. We can do it in languages that track the location of all pointers in all frames, though, which is what we're attempting to do in Java, and this allows us to move stacks around, grow them and shrink them as necessary, even at a word granularity. > All this, by the way, is not to say that Rust made the wrong decision in focusing on async/await.

CitesView on HN
@lomnakkus2018 · in “Fast, Safe, and Complete(ish) Web Service in Rust”

Allow me to third that. It's one of dangers assuming that what you learned at university 15-20 years ago still applies. Another interesting talk: [0] (and even that is 5 years old) [0] ▶ this video

RecommendsView on HN
@asb2016 · in “Haskell in the Datacentre”

Related to this discussion, this talk on user-scheduled OS threads is really interesting. It's a shame no proposed implementation or experimental patches have appeared ▶ this video

RecommendsView on HN
@acconsta2015 · in “Asynchronous IO in Rust”

Oh OK, I definitely could have written that better. From what I can tell, M:N threading has been thoroughly abandoned by Linux, but it might be worth revisiting given the horrendous I/O scaling of Linux kernel threads. I would imagine scheduling user threads cooperatively simplifies things a lot. Google has a very interesting talk about cooperative native thread scheduling, but they haven't upstreamed their code: ▶ this video And here's what linux devs are actually doing (it's underwhelming, something like 50% of what the hardware is capable of): link In the meantime, people who actually need the performance (HPC and even some enterprise servers) have to bypass the kernel entirely. It looks like that's going to be possible even in virtualized environments, thanks to hardware support: link

RecommendsView on HN
@mpweiher2015 · in “An introduction to reactive programming”

I am fully aware of the approach, especially in languages/systems like Erlang, and the freedom that very cheap threads give you. My first point was that you actually have much more of this freedom than most people are aware of, even with (comparatively) heavy kernel threads. For example, see the "Replace user threads with ... threads" by Paul Turned (Plumber's Conference): ▶ this video More on that point, I see a lot of user-threading/async craziness on clients such as iOS that would have been easily been handled by less than a dozen kernel threads, most of which would be sleeping most of the time anyhow. That's a number of kernel threads that is easily manageable and not particularly resource intensive. My second point is that there is one thread that this blocking-happy approach mostly doesn't apply to, and that is the UI thread. You really don't want that UI waiting for (network) I/O and therefore must employ some sort of asynchronous mechanism for data flow and/or notifications.

CitesView on HN
@nostrademons2014 · in “An Opinionated Guide to Modern Java, Part 3: Web Development”

Relevant HN thread: link BTW, I wonder if Paul Turner's work on fibers will change this equation if/when it makes it into the production kernel. ▶ this video Google has had very promising initial results with it.

CitesView on HN
@russell_h2013 · in “The Rust standard library no longer has any scheduling baked into it”

Paul Turner explained this really well at this year's Linux Plumbers Conference. The whole talk is fantastic, but the explanation of what pron is describing in particular (and how it could be improved) starts around 8:39: ▶ this video

Strongly recommendsView on HN
@bascule2013 · in “This Week in Rust – Static linking, Win64”

They're actually talking about pursuing a "N:1" concurrency model (i.e. userland-managed coroutines in the kernel). See: ▶ this video

CitesView on HN
@onedognight2013 · in “The future of M:N threading”

There is no User-Mode scheduling in the Linux Kernel to my knowledge, but the video[1] mentioned the post describes Linux patches that add support for it. [1] ▶ this video

CitesView on HN
@dbaupp2013 · in “The future of M:N threading”

It's hinted at in the video linked in the email[1]: > Over the past year we've been looking at how a uniform, performant, synchronous programming model can be implemented across all of Google's core languages (C++, Java, Go, Python), while maintaining compatibility with existing code. ▶ this video

CitesView on HN
@skybrian2013 · in “The future of M:N threading”

If you're interested in this area you might like this talk from the Linux plumbers' conference: "Over the past year we've been looking at how a uniform, performant, synchronous programming model can be implemented across all of Google's core languages (C++, Java, Go, Python), while maintaining compatibility with existing code." ▶ this video

RecommendsView on HN
Raymond Hettinger, Keynote on Concurrency, PyBay 20171:13:53

Raymond Hettinger, Keynote on Concurrency, PyBay 2017

SF Python
161K views • 9 years ago
Google I/O 2012 - Go Concurrency Patterns51:27

Google I/O 2012 - Go Concurrency Patterns

Google for Developers
894K views • 14 years ago
Differential Synchronization54:44

Differential Synchronization

Google TechTalks
52K views • 17 years ago
Dmitry Vyukov — Go scheduler: Implementing language with lightweight concurrency1:00:40

Dmitry Vyukov — Go scheduler: Implementing language with lightweight concurrency

Hydra
47K views • 7 years ago
"Liberating the Smalltalk lurking in C and Unix" by Stephen Kell40:56

"Liberating the Smalltalk lurking in C and Unix" by Stephen Kell

Strange Loop Conference
25K views • 12 years ago
"Write a single library to handle all input devices, it'll be easy" they said...46:54

"Write a single library to handle all input devices, it'll be easy" they said...

linux.conf.au
21K views • 6 years ago
MINIX 3: a Modular, Self-Healing POSIX-compatible Operating System56:19

MINIX 3: a Modular, Self-Healing POSIX-compatible Operating System

FOSDEM
90K views • 16 years ago
Stanford Seminar - The Soul of a New Machine: Rethinking the Computer1:26:02

Stanford Seminar - The Soul of a New Machine: Rethinking the Computer

Stanford Online
76K views • 6 years ago
Advanced Topics in Programming Languages: A Lock-Free Hash Table58:36

Advanced Topics in Programming Languages: A Lock-Free Hash Table

GoogleTalksArchive
29K views • 14 years ago
Crust of Rust: async/await2:34:01

Crust of Rust: async/await

Jon Gjengset
246K views • 5 years ago
Shared memory parallelism in Julia | Kiran Pamnany | Cambridge Julia Meetup (May 2018)26:34

Shared memory parallelism in Julia | Kiran Pamnany | Cambridge Julia Meetup (May 2018)

The Julia Programming Language
7K views • 8 years ago
The Tragedy of systemd47:18

The Tragedy of systemd

linux.conf.au
1.3M views • 7 years ago
"Unison: a new distributed programming language" by Paul Chiusano41:06

"Unison: a new distributed programming language" by Paul Chiusano

Strange Loop Conference
45K views • 7 years ago
Benno Rice: The Tragedy of systemd -- BSDCan 201832:30

Benno Rice: The Tragedy of systemd -- BSDCan 2018

BSDCan
25K views • 8 years ago
Nathaniel J. Smith - Trio: Async concurrency for mere mortals - PyCon 201832:17

Nathaniel J. Smith - Trio: Async concurrency for mere mortals - PyCon 2018

PyCon 2018
30K views • 8 years ago
Run containers on bare metal already!41:12

Run containers on bare metal already!

Bryan Cantrill
40K views • 10 years ago
"Stop Rate Limiting! Capacity Management Done Right" by Jon Moore42:20

"Stop Rate Limiting! Capacity Management Done Right" by Jon Moore

Strange Loop Conference
41K views • 9 years ago
Raft lecture (Raft user study)58:17

Raft lecture (Raft user study)

Diego Ongaro
87K views • 13 years ago
Paxos lecture (Raft user study)1:06:34

Paxos lecture (Raft user study)

Diego Ongaro
114K views • 13 years ago
The past, present, and future of local-first - Martin Kleppmann (Local-First Conf)29:46

The past, present, and future of local-first - Martin Kleppmann (Local-First Conf)

Local-First Conf
17K views • 2 years ago