Skip to content

Commit

Permalink
sched/tcb: reduce kthread overheads
Browse files Browse the repository at this point in the history
This patch uses shared group for kthreads to reduce overhead.
see pull/12320 for more info.

Signed-off-by: Yanfeng Liu <[email protected]>
  • Loading branch information
yf13 committed May 12, 2024
1 parent b47a361 commit 7270ad8
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 10 deletions.
20 changes: 18 additions & 2 deletions sched/group/group_create.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,11 @@
#include "tls/tls.h"

/****************************************************************************
* Public Data
* Private Data
****************************************************************************/

static struct task_group_s g_kthread_group; /* Shared among kthreads */

/****************************************************************************
* Private Functions
****************************************************************************/
Expand Down Expand Up @@ -119,9 +121,23 @@ int group_initialize(FAR struct task_tcb_s *tcb, uint8_t ttype)

DEBUGASSERT(tcb && !tcb->cmn.group);

ttype &= TCB_FLAG_TTYPE_MASK;

if (ttype == TCB_FLAG_TTYPE_KERNEL && g_kthread_group.tg_info)
{
tcb->cmn.group = &g_kthread_group;
return OK;
}

if (NULL == g_kthread_group.tg_info)
{
sinfo("group=%zu task=%zu kthread=%zu\n", sizeof(*group),
sizeof(struct task_tcb_s), sizeof(struct tcb_s));
}

/* Allocate the group structure and assign it to the TCB */

group = &tcb->group;
group = (ttype == TCB_FLAG_TTYPE_KERNEL) ? &g_kthread_group : &tcb->group;

#if defined(CONFIG_MM_KERNEL_HEAP)
/* If this group is being created for a privileged thread, then all
Expand Down
18 changes: 12 additions & 6 deletions sched/init/nx_start.c
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ uint8_t g_nx_initstate; /* See enum nx_initstate_e */
* bringing up the rest of the system.
*/

static struct task_tcb_s g_idletcb[CONFIG_SMP_NCPUS];
static struct tcb_s g_idletcb[CONFIG_SMP_NCPUS];

/* This is the name of the idle task */

Expand Down Expand Up @@ -352,11 +352,14 @@ static void idle_task_initialize(void)
{
FAR struct task_tcb_s *tcb;
FAR dq_queue_t *tasklist;
volatile uintptr_t p;
int i;

memset(g_idletcb, 0, sizeof(g_idletcb));
for (i = 0; i < CONFIG_SMP_NCPUS; i++)
{
tcb = &g_idletcb[i];
p = (uintptr_t)&g_idletcb[i];
tcb = (FAR struct task_tcb_s *)p;

/* Initialize a TCB for this thread of execution. NOTE: The default
* value for most components of the g_idletcb are zero. The entire
Expand All @@ -365,7 +368,6 @@ static void idle_task_initialize(void)
* that has pid == 0 and sched_priority == 0.
*/

memset(tcb, 0, sizeof(struct task_tcb_s));
tcb->cmn.pid = i;
tcb->cmn.task_state = TSTATE_TASK_RUNNING;

Expand Down Expand Up @@ -460,14 +462,16 @@ static void idle_task_initialize(void)
static void idle_group_initialize(void)
{
FAR struct task_tcb_s *tcb;
volatile uintptr_t p;
int hashndx;
int i;

/* Assign the process ID(s) of ZERO to the idle task(s) */

for (i = 0; i < CONFIG_SMP_NCPUS; i++)
{
tcb = &g_idletcb[i];
p = (uintptr_t)&g_idletcb[i];
tcb = (FAR struct task_tcb_s *)p;

hashndx = PIDHASH(i);
nxsched_pidhash()[hashndx] = &tcb->cmn;
Expand Down Expand Up @@ -533,6 +537,7 @@ static void idle_group_initialize(void)

void nx_start(void)
{
FAR struct task_tcb_s *ttcb;
int i;

sinfo("Entry\n");
Expand Down Expand Up @@ -727,17 +732,18 @@ void nx_start(void)

/* Announce that the CPU0 IDLE task has started */

sched_note_start(&g_idletcb[0].cmn);
sched_note_start(&g_idletcb[0]);

/* Initialize stdio for the IDLE task of each CPU */

for (i = 0; i < CONFIG_SMP_NCPUS; i++)
{
ttcb = (FAR struct task_tcb_s *)&g_idletcb[i];
if (i > 0)
{
/* Clone stdout, stderr, stdin from the CPU0 IDLE task. */

DEBUGVERIFY(group_setuptaskfiles(&g_idletcb[i], NULL, true));
DEBUGVERIFY(group_setuptaskfiles(ttcb, NULL, true));
}
else
{
Expand Down
6 changes: 4 additions & 2 deletions sched/task/task_create.c
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,13 @@ int nxthread_create(FAR const char *name, uint8_t ttype, int priority,
{
FAR struct task_tcb_s *tcb;
pid_t pid;
int ret;
volatile int ret;

/* Allocate a TCB for the new task. */

tcb = kmm_zalloc(sizeof(struct task_tcb_s));
ret = (ttype == TCB_FLAG_TTYPE_KERNEL) ? sizeof(struct tcb_s) :
sizeof(*tcb);
tcb = kmm_zalloc(ret);
if (!tcb)
{
serr("ERROR: Failed to allocate TCB\n");
Expand Down

0 comments on commit 7270ad8

Please sign in to comment.