Skip to content

Commit

Permalink
sched: add up_this_task and up_change_task macro stub
Browse files Browse the repository at this point in the history
reason:
We can utilize percpu storage to hold information about the
current running task. If we intend to implement this feature, we would
need to define two macros that help us manage this percpu information
effectively.

up_this_task: This macro is designed to read the contents of the percpu
              register to retrieve information about the current
              running task.This allows us to quickly access
              task-specific data without having to disable interrupts,
              access global variables and obtain the current cpu index.

up_update_task: This macro is responsible for updating the contents of
                the percpu register.It is typically called during
                initialization or when a context switch occurs to ensure
                that the percpu register reflects the information of the
                newly running task.

Configuring NuttX and compile:
$ ./tools/configure.sh -l qemu-armv8a:nsh_smp
$ make
Running with qemu
$ qemu-system-aarch64 -cpu cortex-a53 -smp 4 -nographic \
   -machine virt,virtualization=on,gic-version=3 \
   -net none -chardev stdio,id=con,mux=on -serial chardev:con \
   -mon chardev=con,mode=readline -kernel ./nuttx

Signed-off-by: hujun5 <[email protected]>
  • Loading branch information
hujun260 committed Sep 29, 2024
1 parent 07061d8 commit ed0a7e8
Show file tree
Hide file tree
Showing 11 changed files with 67 additions and 3 deletions.
2 changes: 2 additions & 0 deletions fs/procfs/fs_procfstcbinfo.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@
#include <nuttx/fs/fs.h>
#include <nuttx/fs/procfs.h>

#include "sched/sched.h"

#if !defined(CONFIG_DISABLE_MOUNTPOINT) && defined(CONFIG_FS_PROCFS) && \
defined(CONFIG_ARCH_HAVE_TCBINFO) && !defined(CONFIG_FS_PROCFS_EXCLUDE_TCBINFO)

Expand Down
37 changes: 37 additions & 0 deletions include/nuttx/arch.h
Original file line number Diff line number Diff line change
Expand Up @@ -930,6 +930,43 @@ int up_copy_section(FAR void *dest, FAR const void *src, size_t n);
# define up_getpicbase(ppicbase)
#endif

/****************************************************************************
* Percpu support
****************************************************************************/

/****************************************************************************
* Name: up_update_task
*
* Description:
* We can utilize percpu storage to hold information about the
* current running task. If we intend to implement this feature, we would
* need to define two macros that help us manage this percpu information
* effectively.
*
* up_this_task: This macro is designed to read the contents of the percpu
* register to retrieve information about the current
* running task.This allows us to quickly access
* task-specific data without having to disable interrupts,
* access global variables and obtain the current cpu index.
*
* up_update_task: This macro is responsible for updating the contents of
* the percpu register.It is typically called during
* initialization or when a context switch occurs to ensure
* that the percpu register reflects the information of the
* newly running task.
*
* Input Parameters:
* current tcb
*
* Returned Value:
* current tcb
*
****************************************************************************/

#ifndef up_update_task
# define up_update_task(t)
#endif

/****************************************************************************
* Address Environment Interfaces
*
Expand Down
1 change: 1 addition & 0 deletions libs/libc/time/lib_localtime.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@

#include <sys/param.h>

#include <nuttx/arch.h>
#include <nuttx/clock.h>
#include <nuttx/init.h>
#include <nuttx/fs/fs.h>
Expand Down
1 change: 1 addition & 0 deletions net/utils/net_snoop.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@

#include <sys/param.h>

#include <nuttx/arch.h>
#include <nuttx/net/snoop.h>

/****************************************************************************
Expand Down
5 changes: 5 additions & 0 deletions sched/init/nx_start.c
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,11 @@ static void idle_task_initialize(void)
/* Mark the idle task as the running task */

g_running_tasks[i] = tcb;

if (i == 0)
{
up_update_task(&g_idletcb[0]); /* Init idle task to percpu reg */
}
}
}

Expand Down
3 changes: 3 additions & 0 deletions sched/irq/irq_csection.c
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,9 @@ irqstate_t enter_critical_section(void)
DEBUGVERIFY(up_cpu_paused(cpu));
paused = true;

/* After resume current_task may change */

up_update_task(current_task(cpu));
DEBUGASSERT((g_cpu_irqset & (1 << cpu)) == 0);

/* NOTE: Here, this CPU does not hold g_cpu_irqlock,
Expand Down
11 changes: 8 additions & 3 deletions sched/sched/sched.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@
# define current_task(cpu) ((FAR struct tcb_s *)list_assignedtasks(cpu)->head)
#else
# define current_task(cpu) ((FAR struct tcb_s *)list_readytorun()->head)
# define this_task() (current_task(this_cpu()))
#endif

#define is_idle_task(t) ((t)->pid < CONFIG_SMP_NCPUS)
Expand Down Expand Up @@ -379,7 +378,11 @@ void nxsched_sporadic_lowpriority(FAR struct tcb_s *tcb);
void nxsched_suspend(FAR struct tcb_s *tcb);
#endif

#ifdef CONFIG_SMP
#if defined(up_this_task)
# define this_task() up_this_task()
#elif !defined(CONFIG_SMP)
# define this_task() ((FAR struct tcb_s *)g_readytorun.head)
#else
noinstrument_function
static inline_function FAR struct tcb_s *this_task(void)
{
Expand All @@ -393,7 +396,7 @@ static inline_function FAR struct tcb_s *this_task(void)

flags = up_irq_save();

/* Obtain the TCB which is currently running on this CPU */
/* Obtain the TCB which is current running on this CPU */

tcb = current_task(this_cpu());

Expand All @@ -402,7 +405,9 @@ static inline_function FAR struct tcb_s *this_task(void)
up_irq_restore(flags);
return tcb;
}
#endif

#ifdef CONFIG_SMP
int nxsched_select_cpu(cpu_set_t affinity);
int nxsched_pause_cpu(FAR struct tcb_s *tcb);
void nxsched_process_delivered(int cpu);
Expand Down
2 changes: 2 additions & 0 deletions sched/sched/sched_addreadytorun.c
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ bool nxsched_add_readytorun(FAR struct tcb_s *btcb)

btcb->task_state = TSTATE_TASK_RUNNING;
btcb->flink->task_state = TSTATE_TASK_READYTORUN;
up_update_task(btcb);
ret = true;
}
else
Expand Down Expand Up @@ -269,6 +270,7 @@ bool nxsched_add_readytorun(FAR struct tcb_s *btcb)
*/

dq_addfirst_nonempty((FAR dq_entry_t *)btcb, tasklist);
up_update_task(btcb);

DEBUGASSERT(task_state == TSTATE_TASK_RUNNING);
btcb->cpu = cpu;
Expand Down
1 change: 1 addition & 0 deletions sched/sched/sched_mergepending.c
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ bool nxsched_merge_pending(void)
= (FAR dq_entry_t *)ptcb;
rtcb->task_state = TSTATE_TASK_READYTORUN;
ptcb->task_state = TSTATE_TASK_RUNNING;
up_update_task(ptcb);
ret = true;
}
else
Expand Down
5 changes: 5 additions & 0 deletions sched/sched/sched_process_delivered.c
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ void nxsched_process_delivered(int cpu)
if (up_cpu_pausereq(cpu))
{
up_cpu_paused(cpu);

/* After resume current_task may change */

up_update_task(current_task(cpu));
}
}

Expand Down Expand Up @@ -125,6 +129,7 @@ void nxsched_process_delivered(int cpu)
dq_addfirst_nonempty((FAR dq_entry_t *)btcb, tasklist);
btcb->cpu = cpu;
btcb->task_state = TSTATE_TASK_RUNNING;
up_update_task(btcb);

DEBUGASSERT(btcb->flink != NULL);
DEBUGASSERT(next == btcb->flink);
Expand Down
2 changes: 2 additions & 0 deletions sched/sched/sched_removereadytorun.c
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ bool nxsched_remove_readytorun(FAR struct tcb_s *rtcb, bool merge)
DEBUGASSERT(nxttcb != NULL);

nxttcb->task_state = TSTATE_TASK_RUNNING;
up_update_task(nxttcb);
doswitch = true;
}

Expand Down Expand Up @@ -295,6 +296,7 @@ void nxsched_remove_running(FAR struct tcb_s *tcb)
void nxsched_remove_self(FAR struct tcb_s *tcb)
{
nxsched_remove_running(tcb);
up_update_task(current_task(tcb->cpu));
if (g_pendingtasks.head)
{
nxsched_merge_pending();
Expand Down

0 comments on commit ed0a7e8

Please sign in to comment.