Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
Skyward Boardcore
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Analyze
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Avionics
Software Development
Skyward Boardcore
Commits
24ceb7e2
Commit
24ceb7e2
authored
2 years ago
by
Niccolò Betto
Browse files
Options
Downloads
Patches
Plain Diff
[TaskScheduler] Replace `Task` pointer with task ID in `Event`
parent
a093559d
No related branches found
No related tags found
1 merge request
!91
[TaskScheduler] Improved task scheduler efficiency and minor bugs fix
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/shared/scheduler/TaskScheduler.cpp
+22
-22
22 additions, 22 deletions
src/shared/scheduler/TaskScheduler.cpp
src/shared/scheduler/TaskScheduler.h
+6
-1
6 additions, 1 deletion
src/shared/scheduler/TaskScheduler.h
with
28 additions
and
23 deletions
src/shared/scheduler/TaskScheduler.cpp
+
22
−
22
View file @
24ceb7e2
...
...
@@ -81,9 +81,8 @@ size_t TaskScheduler::addTask(function_t function, uint32_t period,
startTick
+=
period
;
}
// Add the task first event in the agenda
Event
event
=
{
&
(
*
tasks
)[
id
],
startTick
};
agenda
.
push
(
event
);
// Add the task first event in the agenda, performs in-place construction
agenda
.
emplace
(
id
,
startTick
);
condvar
.
broadcast
();
// Signals the run thread
return
id
;
...
...
@@ -161,9 +160,10 @@ void TaskScheduler::normalizeTasks()
if
(
event
.
nextTick
<
currentTick
)
{
Task
&
task
=
(
*
tasks
)[
event
.
taskId
];
event
.
nextTick
+=
((
currentTick
-
event
.
nextTick
)
/
event
.
task
->
period
+
1
)
*
event
.
task
->
period
;
((
currentTick
-
event
.
nextTick
)
/
task
.
period
+
1
)
*
task
.
period
;
}
newAgenda
.
push
(
event
);
...
...
@@ -188,11 +188,11 @@ void TaskScheduler::run()
int64_t
startTick
=
getTick
();
Event
nextEvent
=
agenda
.
top
();
Task
&
nextTask
=
(
*
tasks
)[
nextEvent
.
taskId
];
// If the task has the SKIP policy and its execution was missed, we need
// to move it forward to match the period
if
(
nextEvent
.
nextTick
<
startTick
&&
nextEvent
.
task
->
policy
==
Policy
::
SKIP
)
if
(
nextEvent
.
nextTick
<
startTick
&&
nextTask
.
policy
==
Policy
::
SKIP
)
{
agenda
.
pop
();
enqueue
(
nextEvent
,
startTick
);
...
...
@@ -202,19 +202,19 @@ void TaskScheduler::run()
agenda
.
pop
();
// Execute the task function
if
(
next
Event
.
t
ask
->
valid
)
if
(
next
T
ask
.
valid
)
{
{
Unlock
<
FastMutex
>
unlock
(
lock
);
try
{
next
Event
.
t
ask
->
function
();
next
T
ask
.
function
();
}
catch
(...)
{
// Update the failed statistic
next
Event
.
t
ask
->
failedEvents
++
;
next
T
ask
.
failedEvents
++
;
}
}
...
...
@@ -236,39 +236,40 @@ void TaskScheduler::updateStats(const Event& event, int64_t startTick,
int64_t
endTick
)
{
constexpr
float
tickToMs
=
1000.
f
/
TICK_FREQ
;
Task
&
task
=
(
*
tasks
)[
event
.
taskId
];
// Activation stats
float
activationError
=
startTick
-
event
.
nextTick
;
event
.
task
->
activationStats
.
add
(
activationError
*
tickToMs
);
task
.
activationStats
.
add
(
activationError
*
tickToMs
);
// Period stats
int64_t
lastCall
=
event
.
task
->
lastCall
;
int64_t
lastCall
=
task
.
lastCall
;
if
(
lastCall
>=
0
)
event
.
task
->
periodStats
.
add
((
startTick
-
lastCall
)
*
tickToMs
);
task
.
periodStats
.
add
((
startTick
-
lastCall
)
*
tickToMs
);
// Update the last call tick to the current start tick for the next
// iteration
event
.
task
->
lastCall
=
startTick
;
task
.
lastCall
=
startTick
;
// Workload stats
event
.
task
->
workloadStats
.
add
(
endTick
-
startTick
);
task
.
workloadStats
.
add
(
endTick
-
startTick
);
}
void
TaskScheduler
::
enqueue
(
Event
event
,
int64_t
startTick
)
{
constexpr
float
msToTick
=
TICK_FREQ
/
1000.
f
;
switch
(
event
.
task
->
policy
)
Task
&
task
=
(
*
tasks
)[
event
.
taskId
];
switch
(
task
.
policy
)
{
case
Policy
::
ONE_SHOT
:
// If the task is one shot we won't push it to the agenda and we'll
// remove it from the tasks map.
(
*
task
s
)[
event
.
task
->
id
]
.
valid
=
false
;
task
.
valid
=
false
;
return
;
case
Policy
::
SKIP
:
// Updated the missed events count
event
.
task
->
missedEvents
+=
(
startTick
-
event
.
nextTick
)
/
event
.
task
->
period
;
task
.
missedEvents
+=
(
startTick
-
event
.
nextTick
)
/
task
.
period
;
// Compute the number of periods between the tick the event should
// have been run and the tick it actually run. Than adds 1 and
...
...
@@ -278,11 +279,10 @@ void TaskScheduler::enqueue(Event event, int64_t startTick)
// but for whatever reason the first execution is at tick 3, then
// the next execution will be at tick 4.
event
.
nextTick
+=
((
startTick
-
event
.
nextTick
)
/
event
.
task
->
period
+
1
)
*
event
.
task
->
period
;
((
startTick
-
event
.
nextTick
)
/
task
.
period
+
1
)
*
task
.
period
;
break
;
case
Policy
::
RECOVER
:
event
.
nextTick
+=
event
.
task
->
period
*
msToTick
;
event
.
nextTick
+=
task
.
period
*
msToTick
;
break
;
}
...
...
This diff is collapsed.
Click to expand it.
src/shared/scheduler/TaskScheduler.h
+
6
−
1
View file @
24ceb7e2
...
...
@@ -157,9 +157,14 @@ private:
struct
Event
{
Task
*
task
;
///< The task to execute.
size_t
task
Id
;
///< The task to execute.
int64_t
nextTick
;
///< Tick of next activation.
Event
(
size_t
taskId
,
int64_t
nextTick
)
:
taskId
(
taskId
),
nextTick
(
nextTick
)
{
}
bool
operator
<
(
const
Event
&
e
)
const
{
// Note: operator < is reversed, so that the priority_queue will
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment