summaryrefslogtreecommitdiffstats
path: root/tokio/src/coop.rs
blob: 1d624591667b5b88ea7511a7fff6970344679018 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
//! Opt-in yield points for improved cooperative scheduling.
//!
//! A single call to [`poll`] on a top-level task may potentially do a lot of work before it
//! returns `Poll::Pending`. If a task runs for a long period of time without yielding back to the
//! executor, it can starve other tasks waiting on that executor to execute them, or drive
//! underlying resources. Since Rust does not have a runtime, it is difficult to forcibly preempt a
//! long-running task. Instead, this module provides an opt-in mechanism for futures to collaborate
//! with the executor to avoid starvation.
//!
//! Consider a future like this one:
//!
//! ```
//! # use tokio::stream::{Stream, StreamExt};
//! async fn drop_all<I: Stream + Unpin>(mut input: I) {
//!     while let Some(_) = input.next().await {}
//! }
//! ```
//!
//! It may look harmless, but consider what happens under heavy load if the input stream is
//! _always_ ready. If we spawn `drop_all`, the task will never yield, and will starve other tasks
//! and resources on the same executor. With opt-in yield points, this problem is alleviated:
//!
//! ```ignore
//! # use tokio::stream::{Stream, StreamExt};
//! async fn drop_all<I: Stream + Unpin>(mut input: I) {
//!     while let Some(_) = input.next().await {
//!         tokio::coop::proceed().await;
//!     }
//! }
//! ```
//!
//! The `proceed` future will coordinate with the executor to make sure that every so often control
//! is yielded back to the executor so it can run other tasks.
//!
//! # Placing yield points
//!
//! Voluntary yield points should be placed _after_ at least some work has been done. If they are
//! not, a future sufficiently deep in the task hierarchy may end up _never_ getting to run because
//! of the number of yield points that inevitably appear before it is reached. In general, you will
//! want yield points to only appear in "leaf" futures -- those that do not themselves poll other
//! futures. By doing this, you avoid double-counting each iteration of the outer future against
//! the cooperating budget.
//!
//!   [`poll`]: https://doc.rust-lang.org/std/future/trait.Future.html#tymethod.poll

// NOTE: The doctests in this module are ignored since the whole module is (currently) private.

use std::cell::Cell;
use std::future::Future;
use std::pin::Pin;
use std::task::{Context, Poll};

/// Constant used to determine how much "work" a task is allowed to do without yielding.
///
/// The value itself is chosen somewhat arbitrarily. It needs to be high enough to amortize wakeup
/// and scheduling costs, but low enough that we do not starve other tasks for too long. The value
/// also needs to be high enough that particularly deep tasks are able to do at least some useful
/// work at all.
///
/// Note that as more yield points are added in the ecosystem, this value will probably also have
/// to be raised.
const BUDGET: usize = 128;

/// Constant used to determine if budgeting has been disabled.
const UNCONSTRAINED: usize = usize::max_value();

thread_local! {
    static HITS: Cell<usize> = Cell::new(UNCONSTRAINED);
}

/// Run the given closure with a cooperative task budget.
///
/// Enabling budgeting when it is already enabled is a no-op.
#[inline(always)]
pub(crate) fn budget<F, R>(f: F) -> R
where
    F: FnOnce() -> R,
{
    HITS.with(move |hits| {
        if hits.get() != UNCONSTRAINED {
            // We are already being budgeted.
            //
            // Arguably this should be an error, but it can happen "correctly"
            // such as with block_on + LocalSet, so we make it a no-op.
            return f();
        }

        struct Guard<'a>(&'a Cell<usize>);
        impl<'a> Drop for Guard<'a> {
            fn drop(&mut self) {
                self.0.set(UNCONSTRAINED);
            }
        }

        hits.set(BUDGET);
        let _guard = Guard(hits);
        f()
    })
}

cfg_rt_threaded! {
    #[inline(always)]
    pub(crate) fn has_budget_remaining() -> bool {
        HITS.with(|hits| hits.get() > 0)
    }
}

cfg_blocking_impl! {
    /// Forcibly remove the budgeting constraints early.
    pub(crate) fn stop() {
        HITS.with(|hits| {
            hits.set(UNCONSTRAINED);
        });
    }
}

/// Invoke `f` with a subset of the remaining budget.
///
/// This is useful if you have sub-futures that you need to poll, but that you want to restrict
/// from using up your entire budget. For example, imagine the following future:
///
/// ```rust
/// # use std::{future::Future, pin::Pin, task::{Context, Poll}};
/// use futures::stream::FuturesUnordered;
/// struct MyFuture<F1, F2> {
///     big: FuturesUnordered<F1>,
///     small: F2,
/// }
///
/// use tokio::stream::Stream;
/// impl<F1, F2> Future for MyFuture<F1, F2>
///   where F1: Future, F2: Future
/// # , F1: Unpin, F2: Unpin
/// {
///     type Output = F2::Output;
///
///     // fn poll(...)
/// # fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<F2::Output> {
/// #   let this = &mut *self;
///     let mut big = // something to pin self.big
/// #                 Pin::new(&mut this.big);
///     let small = // something to pin self.small
/// #             Pin::new(&mut this.small);
///
///     // see if any of the big futures have finished
///     while let Some(e) = futures::ready!(big.as_mut().poll_next(cx)) {
///         // do something with e
/// #       let _ = e;
///     }
///
///     // see if the small future has finished
///     small.poll(cx)
/// }
/// # }
/// ```
///
/// It could be that every time `poll` gets called, `big` ends up spending the entire budget, and
/// `small` never gets polled. That would be sad. If you want to stick up for the little future,
/// that's what `limit` is for. It lets you portion out a smaller part of the yield budget to a
/// particular segment of your code. In the code above, you would write
///
/// ```rust,ignore
/// # use std::{future::Future, pin::Pin, task::{Context, Poll}};
/// # use futures::stream::FuturesUnordered;
/// # struct MyFuture<F1, F2> {
/// #     big: FuturesUnordered<F1>,
/// #     small: F2,
/// # }
/// #
/// # use tokio::stream::Stream;
/// # impl<F1, F2> Future for MyFuture<F1, F2>
/// #   where F1: Future, F2: Future
/// # , F1: Unpin, F2: Unpin
/// # {
/// # type Output = F2::Output;
/// # fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<F2::Output> {
/// #   let this = &mut *self;
/// #   let mut big = Pin::new(&mut this.big);
/// #   let small = Pin::new(&mut this.small);
/// #
///     // see if any of the big futures have finished
///     while let Some(e) = futures::ready!(tokio::coop::limit(64, || big.as_mut().poll_next(cx))) {
/// #       // do something with e
/// #       let _ = e;
/// #   }
/// #   small.poll(cx)
/// # }
/// # }
/// ```
///
/// Now, even if `big` spends its entire budget, `small` will likely be left with some budget left
/// to also do useful work. In particular, if the remaining budget was `N` at the start of `poll`,
/// `small` will have at least a budget of `N - 64`. It may be more if `big` did not spend its
/// entire budget.
///
/// Note that you cannot _increase_ your budget by calling `limit`. The budget provided to the code
/// inside the buget is the _minimum_ of the _current_ budget and the bound.
///
#[allow(unreachable_pub, dead_code)]
pub fn limit<R>(bound: usize, f: impl FnOnce() -> R) -> R {
    HITS.with(|hits| {
        let budget = hits.get();
        // with_bound cannot _increase_ the remaining budget
        let bound = std::cmp::min(budget, bound);
        // When f() exits, how much should we add to what is left?
        let floor = budget.saturating_sub(bound);
        // Make sure we restore the remaining budget even on panic
        struct RestoreBudget<'a>(&'a Cell<usize>, usize);
        impl<'a> Drop for RestoreBudget<'a> {
            fn drop(&mut self) {
                let left = self.0.get();
                self.0.set(self.1 + left);
            }
        }
        // Time to restrict!
        hits.set(bound);
        let _restore = RestoreBudget(&hits, floor);
        f()
    })
}

/// Returns `Poll::Pending` if the current task has exceeded its budget and should yield.
#[allow(unreachable_pub, dead_code)]
#[inline]
pub fn poll_proceed(cx: &mut Context<'_>) -> Poll<()> {
    HITS.with(|hits| {
        let n = hits.get();
        if n == UNCONSTRAINED {
            // opted out of budgeting
            Poll::Ready(())
        } else if n == 0 {
            cx.waker().wake_by_ref();
            Poll::Pending
        } else {
            hits.set(n.saturating_sub(1));
            Poll::Ready(())
        }
    })
}

/// Resolves immediately unless the current task has already exceeded its budget.
///
/// This should be placed after at least some work has been done. Otherwise a future sufficiently
/// deep in the task hierarchy may end up never getting to run because of the number of yield
/// points that inevitably appear before it is even reached. For example:
///
/// ```ignore
/// # use tokio::stream::{Stream, StreamExt};
/// async fn drop_all<I: Stream + Unpin>(mut input: I) {
///     while let Some(_) = input.next().await {
///         tokio::coop::proceed().await;
///     }
/// }
/// ```
#[allow(unreachable_pub, dead_code)]
#[inline]
pub async fn proceed() {
    use crate::future::poll_fn;
    poll_fn(|cx| poll_proceed(cx)).await;
}

pin_project_lite::pin_project! {
    /// A future that cooperatively yields to the task scheduler when polling,
    /// if the task's budget is exhausted.
    ///
    /// Internally, this is simply a future combinator which calls
    /// [`poll_proceed`] in its `poll` implementation before polling the wrapped
    /// future.
    ///
    /// # Examples
    ///
    /// ```rust,ignore
    /// # #[tokio::main]
    /// # async fn main() {
    /// use tokio::coop::CoopFutureExt;
    ///
    /// async {  /* ... */ }
    ///     .cooperate()
    ///     .await;
    /// # }
    /// ```
    ///
    /// [`poll_proceed`]: fn@poll_proceed
    #[derive(Debug)]
    #[allow(unreachable_pub, dead_code)]
    pub struct CoopFuture<F> {
        #[pin]
        future: F,
    }
}

impl<F: Future> Future for CoopFuture<F> {
    type Output = F::Output;
    fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
        ready!(poll_proceed(cx));
        self.project().future.poll(cx)
    }
}

impl<F: Future> CoopFuture<F> {
    /// Returns a new `CoopFuture` wrapping the given future.
    ///
    #[allow(unreachable_pub, dead_code)]
    pub fn new(future: F) -> Self {
        Self { future }
    }
}

// Currently only used by `tokio::sync`; and if we make this combinator public,
// it should probably be on the `FutureExt` trait instead.
cfg_sync! {
    /// Extension trait providing `Future::cooperate` extension method.
    ///
    /// Note: if/when the co-op API becomes public, this method should probably be
    /// provided by `FutureExt`, instead.
    pub(crate) trait CoopFutureExt: Future {
        /// Wrap `self` to cooperatively yield to the scheduler when polling, if the
        /// task's budget is exhausted.
        fn cooperate(self) -> CoopFuture<Self>
        where
            Self: Sized,
        {
            CoopFuture::new(self)
        }
    }

    impl<F> CoopFutureExt for F where F: Future {}
}

#[cfg(all(test, not(loom)))]
mod test {
    use super::*;

    fn get() -> usize {
        HITS.with(|hits| hits.get())
    }

    #[test]
    fn bugeting() {
        use tokio_test::*;

        assert_eq!(get(), UNCONSTRAINED);
        assert_ready!(task::spawn(()).enter(|cx, _| poll_proceed(cx)));
        assert_eq!(get(), UNCONSTRAINED);
        budget(|| {
            assert_eq!(get(), BUDGET);
            assert_ready!(task::spawn(()).enter(|cx, _| poll_proceed(cx)));
            assert_eq!(get(), BUDGET - 1);
            assert_ready!(task::spawn(()).enter(|cx, _| poll_proceed(cx)));
            assert_eq!(get(), BUDGET - 2);
        });
        assert_eq!(get(), UNCONSTRAINED);

        budget(|| {
            limit(3, || {
                assert_eq!(get(), 3);
                assert_ready!(task::spawn(()).enter(|cx, _| poll_proceed(cx)));
                assert_eq!(get(), 2);
                limit(4, || {
                    assert_eq!(get(), 2);
                    assert_ready!(task::spawn(()).enter(|cx, _| poll_proceed(cx)));
                    assert_eq!(get(), 1);
                });
                assert_eq!(get(), 1);
                assert_ready!(task::spawn(()).enter(|cx, _| poll_proceed(cx)));
                assert_eq!(get(), 0