CLEANUP: sched: remove duplicate code in run_tasks_from_list()

Now that ->wake_date is common to tasks and tasklets, we don't need
anymore to carry a duplicate control block to read and update it for
tasks and tasklets. And given that this code was present early in the
if/else fork between tasks and tasklets, taking it out of the block
allows to move the task part into a more visible "else" branch that
also allows to factor the epilogue that resets th_ctx->current and
updates profile_entry->cpu_time, which also used to be duplicated.

Overall, doing just that saved 253 bytes in the function, or ~1/6,
which is not bad considering that it's on a hot path. And the code
got much ore readable.
This commit is contained in:
Willy Tarreau 2022-09-07 17:06:16 +02:00
parent e0e6d81460
commit a9a2384612

View File

@ -559,28 +559,30 @@ unsigned int run_tasks_from_lists(unsigned int budgets[])
ctx = t->context;
process = t->process;
t->calls++;
th_ctx->current = t;
_HA_ATOMIC_AND(&th_ctx->flags, ~TH_FL_STUCK); // this thread is still running
_HA_ATOMIC_DEC(&th_ctx->rq_total);
if (t->state & TASK_F_TASKLET) {
LIST_DEL_INIT(&((struct tasklet *)t)->list);
__ha_barrier_store();
th_ctx->sched_wake_date = ((struct tasklet *)t)->wake_date;
th_ctx->sched_wake_date = t->wake_date;
if (th_ctx->sched_wake_date) {
uint32_t now_ns = now_mono_time();
uint32_t lat = now_ns - th_ctx->sched_wake_date;
((struct tasklet *)t)->wake_date = 0;
t->wake_date = 0;
th_ctx->sched_call_date = now_ns;
profile_entry = sched_activity_entry(sched_activity, t->process);
th_ctx->sched_profile_entry = profile_entry;
HA_ATOMIC_ADD(&profile_entry->lat_time, lat);
HA_ATOMIC_INC(&profile_entry->calls);
}
__ha_barrier_store();
th_ctx->current = t;
_HA_ATOMIC_AND(&th_ctx->flags, ~TH_FL_STUCK); // this thread is still running
_HA_ATOMIC_DEC(&th_ctx->rq_total);
LIST_DEL_INIT(&((struct tasklet *)t)->list);
__ha_barrier_store();
if (t->state & TASK_F_TASKLET) {
/* this is a tasklet */
state = _HA_ATOMIC_FETCH_AND(&t->state, TASK_PERSISTENT);
__ha_barrier_atomic_store();
@ -594,33 +596,8 @@ unsigned int run_tasks_from_lists(unsigned int budgets[])
__ha_barrier_store();
continue;
}
if (th_ctx->sched_wake_date)
HA_ATOMIC_ADD(&profile_entry->cpu_time, (uint32_t)(now_mono_time() - th_ctx->sched_call_date));
done++;
th_ctx->current = NULL;
__ha_barrier_store();
continue;
}
LIST_DEL_INIT(&((struct tasklet *)t)->list);
__ha_barrier_store();
th_ctx->sched_wake_date = t->wake_date;
if (unlikely(t->wake_date)) {
uint32_t now_ns = now_mono_time();
uint32_t lat = now_ns - t->wake_date;
t->wake_date = 0;
th_ctx->sched_call_date = now_ns;
profile_entry = sched_activity_entry(sched_activity, t->process);
th_ctx->sched_profile_entry = profile_entry;
HA_ATOMIC_ADD(&profile_entry->lat_time, lat);
HA_ATOMIC_INC(&profile_entry->calls);
}
__ha_barrier_store();
} else {
/* This is a regular task */
/* We must be the exclusive owner of the TASK_RUNNING bit, and
* have to be careful that the task is not being manipulated on
@ -639,8 +616,6 @@ unsigned int run_tasks_from_lists(unsigned int budgets[])
__ha_barrier_atomic_store();
/* OK then this is a regular task */
_HA_ATOMIC_DEC(&ha_thread_ctx[tid].tasks_in_list);
/* Note for below: if TASK_KILLED arrived before we've read the state, we
@ -662,15 +637,6 @@ unsigned int run_tasks_from_lists(unsigned int budgets[])
*/
continue;
}
th_ctx->current = NULL;
__ha_barrier_store();
/* stats are only registered for non-zero wake dates */
if (unlikely(th_ctx->sched_wake_date)) {
uint32_t cpu = (uint32_t)now_mono_time() - th_ctx->sched_call_date;
HA_ATOMIC_ADD(&profile_entry->cpu_time, cpu);
}
/* If there is a pending state we have to wake up the task
* immediately, else we defer it into wait queue
@ -686,6 +652,14 @@ unsigned int run_tasks_from_lists(unsigned int budgets[])
task_drop_running(t, 0);
}
}
}
th_ctx->current = NULL;
__ha_barrier_store();
/* stats are only registered for non-zero wake dates */
if (unlikely(th_ctx->sched_wake_date))
HA_ATOMIC_ADD(&profile_entry->cpu_time, (uint32_t)(now_mono_time() - th_ctx->sched_call_date));
done++;
}
th_ctx->current_queue = -1;