Skip to content

Commit

Permalink
schedule: zephyr_domain: register threads in domain init
Browse files Browse the repository at this point in the history
This will stop creating threads in every domain register
and keep single thread per core

Signed-off-by: Adrian Bonislawski <adrian.bonislawski@intel.com>
  • Loading branch information
abonislawski committed Oct 16, 2024
1 parent a8cfb70 commit e0568e1
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 27 deletions.
55 changes: 29 additions & 26 deletions src/schedule/zephyr_domain.c
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@ static void zephyr_domain_thread_fn(void *p1, void *p2, void *p3)
k_mutex_unlock(&zephyr_domain->block_mutex);
}
#endif

dt->handler(dt->arg);
cycles1 = k_cycle_get_32();

Expand Down Expand Up @@ -156,8 +155,7 @@ static int zephyr_domain_register(struct ll_schedule_domain *domain,
struct zephyr_domain *zephyr_domain = ll_sch_domain_get_pdata(domain);
int core = cpu_get_id();
struct zephyr_domain_thread *dt = zephyr_domain->domain_thread + core;
char thread_name[] = "ll_thread0";
k_tid_t thread;

k_spinlock_key_t key;

tr_dbg(&ll_tr, "zephyr_domain_register()");
Expand All @@ -169,22 +167,8 @@ static int zephyr_domain_register(struct ll_schedule_domain *domain,
dt->handler = handler;
dt->arg = arg;

/* 10 is rather random, we better not accumulate 10 missed timer interrupts */
k_sem_init(&dt->sem, 0, 10);

thread_name[sizeof(thread_name) - 2] = '0' + core;

thread = k_thread_create(&dt->ll_thread,
ll_sched_stack[core],
ZEPHYR_LL_STACK_SIZE,
zephyr_domain_thread_fn, zephyr_domain, NULL, NULL,
CONFIG_LL_THREAD_PRIORITY, 0, K_FOREVER);

k_thread_cpu_mask_clear(thread);
k_thread_cpu_mask_enable(thread, core);
k_thread_name_set(thread, thread_name);

k_thread_start(thread);
k_sem_reset(&dt->sem);
k_thread_start(&dt->ll_thread);

key = k_spin_lock(&domain->lock);

Expand Down Expand Up @@ -218,8 +202,10 @@ static int zephyr_domain_unregister(struct ll_schedule_domain *domain,
tr_dbg(&ll_tr, "zephyr_domain_unregister()");

/* tasks still registered on this core */
if (num_tasks)
if (num_tasks) {
tr_info(&ll_tr, "num_tasks %d", num_tasks);
return 0;
}

key = k_spin_lock(&domain->lock);

Expand All @@ -230,19 +216,14 @@ static int zephyr_domain_unregister(struct ll_schedule_domain *domain,
k_timer_stop(&zephyr_domain->timer);
k_timer_user_data_set(&zephyr_domain->timer, NULL);
}

zephyr_domain->domain_thread[core].handler = NULL;

k_spin_unlock(&domain->lock, key);

tr_info(&ll_tr, "zephyr_domain_unregister domain->type %d domain->clk %d",
domain->type, domain->clk);

/*
* If running in the context of the domain thread, k_thread_abort() will
* not return
*/
k_thread_abort(&zephyr_domain->domain_thread[core].ll_thread);
k_sem_reset(&zephyr_domain->domain_thread[core].sem);

return 0;
}
Expand Down Expand Up @@ -285,6 +266,10 @@ struct ll_schedule_domain *zephyr_domain_init(int clk)
{
struct ll_schedule_domain *domain;
struct zephyr_domain *zephyr_domain;
struct zephyr_domain_thread *dt;
k_tid_t thread;
int core;
char thread_name[] = "ll_thread0";

domain = domain_init(SOF_SCHEDULE_LL_TIMER, clk, false,
&zephyr_domain_ops);
Expand All @@ -302,5 +287,23 @@ struct ll_schedule_domain *zephyr_domain_init(int clk)

ll_sch_domain_set_pdata(domain, zephyr_domain);

for (core = 0; core < CONFIG_CORE_COUNT; core++) {
dt = zephyr_domain->domain_thread + core;
thread_name[sizeof(thread_name) - 2] = '0' + core;

/* 10 is rather random, we better not accumulate 10 missed timer interrupts */
k_sem_init(&dt->sem, 0, 10);

thread = k_thread_create(&dt->ll_thread,
ll_sched_stack[core],
ZEPHYR_LL_STACK_SIZE,
zephyr_domain_thread_fn, zephyr_domain, NULL, NULL,
CONFIG_LL_THREAD_PRIORITY, 0, K_FOREVER);

k_thread_cpu_mask_clear(thread);
k_thread_cpu_mask_enable(thread, core);
k_thread_name_set(thread, thread_name);
}

return domain;
}
3 changes: 2 additions & 1 deletion src/schedule/zephyr_twb_schedule.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@
LOG_MODULE_REGISTER(twb_schedule, CONFIG_SOF_LOG_LEVEL);

/* e93326d8-0d14-4bf0-bcb9-e063d3d80136 */
DECLARE_SOF_UUID("twb-schedule", twb_sched_uuid, 0xe93326d8, 0x0d14, 0x4bf0,
SOF_DEFINE_UUID("twb-schedule", twb_sched_uuid, 0xe93326d8, 0x0d14, 0x4bf0,
0xbc, 0xb9, 0xe0, 0x63, 0xd3, 0xd8, 0x01, 0x36);
//SOF_DEFINE_REG_UUID(twb_sched);

DECLARE_TR_CTX(twb_tr, SOF_UUID(twb_sched_uuid), LOG_LEVEL_INFO);

Expand Down

0 comments on commit e0568e1

Please sign in to comment.