Skip to content

Commit c5052ba

Browse files
committed
MINOR: sched: add TASK_F_WANTS_TIME to make the scheduler update the call date
Currently tasks being profiled have th_ctx->sched_call_date set to the current nanosecond in monotonic time. But there's no other way to have this, despite the scheduler being capable of it. Let's just declare a new task flag, TASK_F_WANTS_TIME, that makes the scheduler take the time just before calling the handler. This way, a task that needs nanosecond resolution on the call date will be able to be called with an up-to-date date without having to abuse now_mono_time() if not needed. In addition, if CLOCK_MONOTONIC is not supported (now_mono_time() always returns 0), the date is set to the most recently known now_ns, which is guaranteed to be atomic and is only updated once per poll loop. This date can be more conveniently retrieved using task_mono_time(). This can be useful, e.g. for pacing. The code was slightly adjusted so as to merge the common parts between the profiling case and this one.
1 parent 12969c1 commit c5052ba

File tree

3 files changed

+22
-7
lines changed

3 files changed

+22
-7
lines changed

include/haproxy/task-t.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,13 @@
6060
#define TASK_F_USR1 0x00010000 /* preserved user flag 1, application-specific, def:0 */
6161
#define TASK_F_UEVT1 0x00020000 /* one-shot user event type 1, application specific, def:0 */
6262
#define TASK_F_UEVT2 0x00040000 /* one-shot user event type 2, application specific, def:0 */
63-
/* unused: 0x80000..0x80000000 */
63+
#define TASK_F_WANTS_TIME 0x00080000 /* task/tasklet wants th_ctx->sched_call_date to be set */
64+
/* unused: 0x100000..0x80000000 */
6465

6566
/* These flags are persistent across scheduler calls */
6667
#define TASK_PERSISTENT (TASK_SELF_WAKING | TASK_KILLED | \
67-
TASK_HEAVY | TASK_F_TASKLET | TASK_F_USR1)
68+
TASK_HEAVY | TASK_F_TASKLET | TASK_F_USR1 | \
69+
TASK_F_WANTS_TIME)
6870

6971
/* This function is used to report state in debugging tools. Please reflect
7072
* below any single-bit flag addition above in the same order via the

include/haproxy/task.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,12 @@ static inline int thread_has_tasks(void)
193193
(int)!MT_LIST_ISEMPTY(&th_ctx->shared_tasklet_list));
194194
}
195195

196+
/* returns the most recent known date of the task's call from the scheduler */
197+
static inline uint64_t task_mono_time(void)
198+
{
199+
return th_ctx->sched_call_date;
200+
}
201+
196202
/* puts the task <t> in run queue with reason flags <f>, and returns <t> */
197203
/* This will put the task in the local runqueue if the task is only runnable
198204
* by the current thread, in the global runqueue otherwies. With DEBUG_TASK,

src/task.c

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -567,17 +567,24 @@ unsigned int run_tasks_from_lists(unsigned int budgets[])
567567
t->calls++;
568568

569569
th_ctx->sched_wake_date = t->wake_date;
570-
if (th_ctx->sched_wake_date) {
571-
uint32_t now_ns = now_mono_time();
572-
uint32_t lat = now_ns - th_ctx->sched_wake_date;
570+
if (th_ctx->sched_wake_date || (t->state & TASK_F_WANTS_TIME)) {
571+
/* take the most accurate clock we have, either
572+
* mono_time() or last now_ns (monotonic but only
573+
* incremented once per poll loop).
574+
*/
575+
th_ctx->sched_call_date = now_mono_time();
576+
if (unlikely(!th_ctx->sched_call_date))
577+
th_ctx->sched_call_date = now_ns;
578+
}
573579

580+
if (th_ctx->sched_wake_date) {
574581
t->wake_date = 0;
575-
th_ctx->sched_call_date = now_ns;
576582
profile_entry = sched_activity_entry(sched_activity, t->process, t->caller);
577583
th_ctx->sched_profile_entry = profile_entry;
578-
HA_ATOMIC_ADD(&profile_entry->lat_time, lat);
584+
HA_ATOMIC_ADD(&profile_entry->lat_time, (uint32_t)(th_ctx->sched_call_date - th_ctx->sched_wake_date));
579585
HA_ATOMIC_INC(&profile_entry->calls);
580586
}
587+
581588
__ha_barrier_store();
582589

583590
th_ctx->current = t;

0 commit comments

Comments
 (0)