Skip to content

Commit

Permalink
[task] Create kMaxDelayedStarvationTasks feature param
Browse files Browse the repository at this point in the history
Bug: 1153139
Change-Id: I90c8a1d1c42d005337833cf327f0e852e5cdc24e
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4327433
Commit-Queue: Etienne Pierre-Doray <etiennep@chromium.org>
Reviewed-by: Francois Pierre Doray <fdoray@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1117734}
  • Loading branch information
Etienne Pierre-doray authored and Chromium LUCI CQ committed Mar 15, 2023
1 parent 1150229 commit e9957c9
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 2 deletions.
1 change: 1 addition & 0 deletions base/task/sequence_manager/sequence_manager_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,7 @@ void SequenceManagerImpl::InitializeFeatures() {
g_explicit_high_resolution_timer_win =
FeatureList::IsEnabled(kExplicitHighResolutionTimerWin);
#endif // BUILDFLAG(IS_WIN)
TaskQueueSelector::InitializeFeatures();
}

// static
Expand Down
10 changes: 10 additions & 0 deletions base/task/sequence_manager/task_queue_selector.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "base/task/sequence_manager/associated_thread_id.h"
#include "base/task/sequence_manager/task_queue_impl.h"
#include "base/task/sequence_manager/work_queue.h"
#include "base/task/task_features.h"
#include "base/threading/thread_checker.h"
#include "base/trace_event/base_tracing.h"
#include "third_party/abseil-cpp/absl/types/optional.h"
Expand All @@ -19,6 +20,9 @@ namespace base {
namespace sequence_manager {
namespace internal {

std::atomic_int TaskQueueSelector::g_max_delayed_starvation_tasks =
TaskQueueSelector::kDefaultMaxDelayedStarvationTasks;

TaskQueueSelector::TaskQueueSelector(
scoped_refptr<const AssociatedThreadId> associated_thread,
const SequenceManager::Settings& settings)
Expand All @@ -34,6 +38,12 @@ TaskQueueSelector::TaskQueueSelector(

TaskQueueSelector::~TaskQueueSelector() = default;

// static
void TaskQueueSelector::InitializeFeatures() {
g_max_delayed_starvation_tasks.store(kMaxDelayedStarvationTasksParam.Get(),
std::memory_order_relaxed);
}

void TaskQueueSelector::AddQueue(internal::TaskQueueImpl* queue,
TaskQueue::QueuePriority priority) {
DCHECK_CALLED_ON_VALID_THREAD(associated_thread_->thread_checker);
Expand Down
10 changes: 8 additions & 2 deletions base/task/sequence_manager/task_queue_selector.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#include <stddef.h>

#include <atomic>
#include <vector>

#include "base/base_export.h"
Expand Down Expand Up @@ -39,6 +40,8 @@ class BASE_EXPORT TaskQueueSelector : public WorkQueueSets::Observer {
TaskQueueSelector& operator=(const TaskQueueSelector&) = delete;
~TaskQueueSelector() override;

static void InitializeFeatures();

// Called to register a queue that can be selected. This function is called
// on the main thread.
void AddQueue(internal::TaskQueueImpl* queue,
Expand Down Expand Up @@ -108,7 +111,7 @@ class BASE_EXPORT TaskQueueSelector : public WorkQueueSets::Observer {

// Maximum number of delayed tasks tasks which can be run while there's a
// waiting non-delayed task.
static const int kMaxDelayedStarvationTasks = 3;
static const int kDefaultMaxDelayedStarvationTasks = 3;

// Tracks which priorities are currently active, meaning there are pending
// runnable tasks with that priority. Because there are only a handful of
Expand Down Expand Up @@ -167,7 +170,7 @@ class BASE_EXPORT TaskQueueSelector : public WorkQueueSets::Observer {
template <typename SetOperation>
WorkQueue* ChooseWithPriority(TaskQueue::QueuePriority priority) const {
// Select an immediate work queue if we are starving immediate tasks.
if (immediate_starvation_count_ >= kMaxDelayedStarvationTasks) {
if (immediate_starvation_count_ >= g_max_delayed_starvation_tasks) {
WorkQueue* queue =
ChooseImmediateOnlyWithPriority<SetOperation>(priority);
if (queue)
Expand Down Expand Up @@ -240,6 +243,9 @@ class BASE_EXPORT TaskQueueSelector : public WorkQueueSets::Observer {
std::vector<int> non_empty_set_counts_;

static constexpr const int kMaxNonEmptySetCount = 2;
// An atomic is used here because InitializeFeatures() can race with
// SequenceManager reading this.
static std::atomic_int g_max_delayed_starvation_tasks;

// List of active priorities, which is used to work out which priority to run
// next.
Expand Down
7 changes: 7 additions & 0 deletions base/task/task_features.cc
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,11 @@ BASE_EXPORT TimeDelta GetDefaultTaskLeeway() {
return g_task_leeway.load(std::memory_order_relaxed);
}

BASE_FEATURE(kMaxDelayedStarvationTasks,
"MaxDelayedStarvationTasks",
base::FEATURE_ENABLED_BY_DEFAULT);

const base::FeatureParam<int> kMaxDelayedStarvationTasksParam{
&kMaxDelayedStarvationTasks, "count", 3};

} // namespace base
6 changes: 6 additions & 0 deletions base/task/task_features.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@ BASE_EXPORT void InitializeTaskLeeway();
BASE_EXPORT TimeDelta GetTaskLeewayForCurrentThread();
BASE_EXPORT TimeDelta GetDefaultTaskLeeway();

// Controls the max number of delayed tasks that can run before selecting an
// immediate task in sequence manager.
BASE_EXPORT BASE_DECLARE_FEATURE(kMaxDelayedStarvationTasks);
extern const BASE_EXPORT base::FeatureParam<int>
kMaxDelayedStarvationTasksParam;

} // namespace base

#endif // BASE_TASK_TASK_FEATURES_H_

0 comments on commit e9957c9

Please sign in to comment.