Skip to content

Commit

Permalink
sheep: fix logic for setting the max number of threads for dynamic WQ
Browse files Browse the repository at this point in the history
This commit makes a minor change on "doubling growth". If the double
of the current number of threads for a dynamic queue exceeds the
maximum number, that workqueue grows into that maximum number.

Signed-off-by: Takashi Menjo <menjo.takashi@lab.ntt.co.jp>
(cherry picked from commit c42b9d5)
  • Loading branch information
tmenjo committed Jan 23, 2017
1 parent 0e3479f commit cc3beb9
Showing 1 changed file with 24 additions and 12 deletions.
36 changes: 24 additions & 12 deletions lib/work.c
Expand Up @@ -227,19 +227,30 @@ static inline uint64_t wq_get_roof(struct wq_info *wi)
return nr;
}

static bool wq_need_grow(struct wq_info *wi)
/*
* Return non-zero if a given workqueue need to grow.
* The return value is the new number of threads.
*
* Otherwise, return zero.
*/
static size_t wq_need_grow(struct wq_info *wi)
{
size_t roof = 0;

if (wi->tc == WQ_FIXED)
return false;
return 0;

if (wi->nr_threads < uatomic_read(&wi->nr_queued_work) &&
wi->nr_threads * 2 <= wq_get_roof(wi)) {
wi->tm_end_of_protection = get_msec_time() +
WQ_PROTECTION_PERIOD;
return true;
}
/* do not need to grow if there are enough threads */
if (wi->nr_threads >= uatomic_read(&wi->nr_queued_work))
return 0;

return false;
/* cannot grow if # threads already reaches maximum */
roof = (size_t)wq_get_roof(wi);
if (wi->nr_threads >= roof)
return 0;

wi->tm_end_of_protection = get_msec_time() + WQ_PROTECTION_PERIOD;
return min(wi->nr_threads * 2, roof);
}

/*
Expand Down Expand Up @@ -281,15 +292,16 @@ static int create_worker_threads(struct wq_info *wi, size_t nr_threads)
void queue_work(struct work_queue *q, struct work *work)
{
struct wq_info *wi = container_of(q, struct wq_info, q);
size_t new_nr_threads = 0;

tracepoint(work, queue_work, wi, work);

uatomic_inc(&wi->nr_queued_work);
sd_mutex_lock(&wi->pending_lock);

if (wq_need_grow(wi))
/* double the thread pool size */
create_worker_threads(wi, wi->nr_threads * 2);
new_nr_threads = wq_need_grow(wi);
if (new_nr_threads > 0)
create_worker_threads(wi, new_nr_threads);

list_add_tail(&work->w_list, &wi->q.pending_list);
sd_mutex_unlock(&wi->pending_lock);
Expand Down

0 comments on commit cc3beb9

Please sign in to comment.