Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

scx_rustland: prevent starvation and improve responsiveness #60

Merged
merged 8 commits into from
Jan 2, 2024

Commits on Jan 1, 2024

  1. scx_rustland: rename variable id -> pos for better clarity

    Signed-off-by: Andrea Righi <[email protected]>
    Andrea Righi committed Jan 1, 2024
    Configuration menu
    Copy the full SHA
    2773906 View commit details
    Browse the repository at this point in the history
  2. scx_rustland: clean up old entries in the task map

    The user-space scheduler maintains an internal hash map of tasks
    information (indexed by their pid). Tasks are only added to this hash
    map and never removed. After running the scheduler for a while we may
    experience a performance degration, because the hash map keeps growing.
    
    Therefore implement a mechanism of garbage collector to remove the old
    entries from the task map (periodically removing pids that don't exist
    anymore).
    
    Signed-off-by: Andrea Righi <[email protected]>
    Andrea Righi committed Jan 1, 2024
    Configuration menu
    Copy the full SHA
    61c77b7 View commit details
    Browse the repository at this point in the history
  3. scx_rustland: never account more than slice_ns to vruntime

    In any case make sure that we never account more than the maximum
    slice_ns to a task's vruntime.
    
    This helps to prevent starving a task for too long in the user-space
    scheduler.
    
    Signed-off-by: Andrea Righi <[email protected]>
    Andrea Righi committed Jan 1, 2024
    Configuration menu
    Copy the full SHA
    8402601 View commit details
    Browse the repository at this point in the history
  4. scx_rustland: remove SCX_ENQ_LAST check in is_task_cpu_available()

    With commit 49f2e7c ("scx_rustland: enable SCX_OPS_ENQ_LAST") we have
    enabled SCX_OPS_ENQ_LAST that seems to save some unnecessary user-space
    scheduler activations when the system is mostly idle.
    
    We are also checking for the SCX_ENQ_LAST in the enqueue flags, that
    apparently it is not needed and we can achieve the same behavior
    dropping this check.
    
    Signed-off-by: Andrea Righi <[email protected]>
    Andrea Righi committed Jan 1, 2024
    Configuration menu
    Copy the full SHA
    0fc46b2 View commit details
    Browse the repository at this point in the history
  5. bpf_rustland: do not dispatch the scheduler to the global DSQ

    Never dispatch the user-space scheduler to the global DSQ, while all
    the other tasks are dispatched to the local per-CPU DSQ.
    
    Since tasks are consumed from the local DSQ first and then from the
    global DSQ, we may end up starving the scheduler if we dispatch only
    this one on the global DSQ.
    
    In fact it is really easy to trigger a stall with a workload that
    triggers many context switches in the system, for example (on a 8 cores
    system):
    
     $ stress-ng --cpu 32 --iomix 4 --vm 2 --vm-bytes 128M --fork 4 --timeout 30s
    
     ...
     09:28:11 [WARN] EXIT: scx_rustland[1455943] failed to run for 5.275s
     09:28:11 [INFO] Unregister RustLand scheduler
    
    To prevent this from happening also dispatch the user-space scheduler on
    the local DSQ, using the current CPU where .dispatch() is called, if
    possible, or the previously used CPU otherwise.
    
    Apply the same logic when the scheduler is congested: dispatch on the
    previously used CPU using the local DSQ.
    
    In this way all tasks will always get the same "dispatch priority" and
    we can prevent the scheduler starvation issue.
    
    Note that with this change in place dispatch_global() is never used and
    we can get rid of it.
    
    Signed-off-by: Andrea Righi <[email protected]>
    Andrea Righi committed Jan 1, 2024
    Configuration menu
    Copy the full SHA
    676bd88 View commit details
    Browse the repository at this point in the history
  6. scx_rustland: prevent starvation handling short-lived tasks properly

    Prevent newly created short-lived tasks from starving the other tasks
    sitting in the user-space scheduler.
    
    This can be done setting an initial vruntime of (min_vruntime + 1) to
    newly scheduled tasks, instead of min_vruntime: this ensures a
    progressing global vruntime durig each scheduler run, providing a
    priority boost to newer tasks (that is still beneficial for potential
    short-lived tasks) while also preventing excessive starvation of the
    other tasks sitting in the user-space scheduler, waiting to be
    dispatched.
    
    Without this change it is really easy to create a stall condition simply
    by forking a bunch of short-lived tasks in a busy loop, with this change
    applied the scheduler can handle properly the consistent flow of newly
    created short-lived tasks, without introducing any stall.
    
    Signed-off-by: Andrea Righi <[email protected]>
    Andrea Righi committed Jan 1, 2024
    Configuration menu
    Copy the full SHA
    90e92ac View commit details
    Browse the repository at this point in the history
  7. scx_rustland: evaluate the proper vruntime delta

    The forumla used to evaluate the weighted time delta is not correct,
    it's not considering the weight as a percentage. Fix this by using the
    proper formula.
    
    Moreover, take into account also the task weight when evaluating the
    maximum time delta to account in vruntime and make sure that we never
    charge a task more than slice_ns.
    
    This helps to prevent starvation of low priority tasks.
    
    Signed-off-by: Andrea Righi <[email protected]>
    Andrea Righi committed Jan 1, 2024
    Configuration menu
    Copy the full SHA
    2900b20 View commit details
    Browse the repository at this point in the history
  8. scx_rustland: small code refactoring

    No functional change, make the user-space scheduler code a bit more
    readable and more Rust idiomatic.
    
    Signed-off-by: Andrea Righi <[email protected]>
    Andrea Righi committed Jan 1, 2024
    Configuration menu
    Copy the full SHA
    280796c View commit details
    Browse the repository at this point in the history