-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Bug: sporadic scheduling does not work for multiple threads #2935
Comments
It think that the sporadic scheduler is overly complex and I had always planned to redesign that scheduler. If someone is interested in that redesign, I would be happy to share my thoughts. |
I think in this case, to observe the full range of behaviors, you would need a 3 threads. The two that you are using now:
And also:
Without this high priority thread, it might be the case the sporadic scheduler is just broken and has nothing to do with two sporadic threads. When I tested this many years ago, I added GPIO outputs from the OS task scheduler hooks. Then I could see the task behavior on a logic analyzer. I don't think anyone has used the sporadic scheduler since then and the case with it could suffer from bit rot or is not properly verified. |
There are two known issues with the sporadic scheduler in the top-level TODO list:
|
You don't mention what the failure behavior is. You say "budget enforcement does not work". It might be helpful to know what you mean by that. |
Thanks for your quick feedback. I ran a couple of experiments with this test program Some links to the source code:
The results show, that NuttX does not schedule two sporadic threads according to the specified budgets:
Discussion:
|
Will this bug be fixed? |
Apache projects do not have the kind of project organization that can answer that question. The bug will be fixed if some individual in the community decides to work on it as a contribution. No one is offering that now. As a starting point, I will clean up your test example and incorporate it into the OS test. It found an important bug so it is of value and should be a part of the test. I'll also create some sporadic configuration to exercise the test and replicate your bug. |
I have incorporated a modified version of your test case into the OS test. The is #apache/incubator-nuttx/3097 and #apache/incubator-nuttx-apps/620 Here is some sample output (using your priorities):
NOTE:
Each test case is 100,000 MS total. Expected results:
I believe that this may be largely an artifact of the identical priorities for the two sporadic threads. Consider this priority change:
Expected results:
The fact that this priority change eliminates the problem still suggests to me that that there is some issue but that just is more subtle than it originally appeared. Some of this is misleading too: By raising thread 2's lower priority to 30, it always runs for most of the replenishment interval. It would be better to have a CPU hog FIFO thread at a priority of about 100. Then neither sporadic thread could run in its lower priority state and we should then see the counts only for the sporadic threads when they are in the higher priority state. |
@patacongo thanks for including it in the os-tests. Yes, I agree, there should be a third thread scheduled with FIFO (like in my test setup) that eats up the remaining cycles. Proposed setup:
Then, a sporadic thread with a budget of e.g. 30 % shall also result in about 30% processing time, and not any value above 30%. I think with this setup you can properly verify the correctness of the sporadic server scheduling algorithm. |
I did this in a different way: I added two counts, one when the priority is high and one when the priority is low. The high priority count should be equal to the budget. Low priority counts will occur when the CPU is IDLE and has nothing else to do. Now, I can see the problem more clearly. I will edit this comment and report the results in a few minutes. user_main: Dual sporadic thread test
No you can see that the behavior is the same as your original report: The higher priority budget interval is does not occur after thread 1 budget equals or exceeds the thread 2 budget. The modified test is incubator-nuttx-apps PR 623 |
PR #3111 corrects some of the problems, but not all:
It certainly does narrow the problem down to the case where both thread's budget times complete at approximately the same time. |
I believe that I understand the problem. It is complex to explain.
That is consistent with the condition we see that causes the failure (i.e., with both budget intervals the same) and with the counting that we see in collected data (no high priority counts). But without any data, it is just a fantasy. A solution would require additional state information to detect the case that thread 2 was not initially running. There is already a sporadic->suspended that is set to true when the thread is started. However, it is reset to false when thread 2 resumes (actually runs for the first time) so that information is lost. Here is an improved description of the failure scenario:
I am not quite sure how to fix this. |
Today, I planned to add some instrumentation in the form of debug output to a RAM log to analyze this problem. The RAM log is very fast so I did not expect any issues. However, I found that generating a lot of debug output would eliminate the problem. Even generating a small amount debug output caused only some losses in budget. This is bad in that in that it means there is no simple way to debug the issue. It is good, however, in that it supports the idea that it is a race condition that causes the problem. The primary effect of using the RAM log is very small timing delays. |
I have a hunch that this would eliminate the problem seen in the case where both budgets are 30 MS because I think it would eliminate the condition that leads to the race condition. However, that problem is a real issue so it is good for the time being that this test reveals the problem. |
We published a paper using the sporadic scheduler of NuttX in the context of micro-ROS: |
Oh, i got a question about this, too. If a sporadic thread is blocked during its high-priority budget, then wake up during its low-priority, the sporadic thread will just execute at low-priority. But the budget is never consumed during one replenishment interval. But i think we should let the sporadic continues to run at high priority, if its budget is not really consumed and replenishment time is not yet arrived. I think the problem might be the watchdog. I come up with an idea that might be useful: Let`s just set the replenishment watch dog. When the replenishment time comes, thread`s budget is replenished no matter how much it is left. Once the sporadic is running, let the tcb->timeslice indicates the budget. For example, replenishment = 5, budget = 2. tcb->timeslice is set to 2 initially. If the 2 budget is consumed, set tcb->timeslice to 0. In this case, if the thread is blocked during high-priority, it still could be rescheduled at a high priority, untill its budget consumed. This would need some modification to the scheduler. I`m not sure if the modified scheduling can still be called `sporadic schduling`. I`ve also read some other papers, while there is some difference.Here is the list: 1. Scheduling Aperiodic Tasks in Dynamic Priority Systems. This paper described `Dynamic Sporadic Server`, which is a bit different from nuttx. @patacongo Thanks. |
Hi,
I am using NuttX for micro-ROS on STM32 microcontroller on Olimex board. Link to example
which is a very simple extension of the NuttX example for sporadic scheduling in testing/ostest
Test setup:
Observation:
Problem description
I came to the conclusion, that budget enforcement of the NuttX sporadic scheduling only works for one sporadic thread.
For real applications, I would like to use multiple threads with sporadic scheduling.
Could you please check the implementation and give support?
The text was updated successfully, but these errors were encountered: