Skip to content
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

[Feature] More precise intervals #9

Open
tomek-szczesny opened this issue Dec 1, 2021 · 7 comments
Open

[Feature] More precise intervals #9

tomek-szczesny opened this issue Dec 1, 2021 · 7 comments

Comments

@tomek-szczesny
Copy link
Contributor

This is my log from measurements in 10ms intervals, captured via wi-fi:

0001407763,19144,0046,00880,0,00000,0000,00000,0,00,00000,0000,00000,0,00
0001407777,19145,0046,00880,0,00000,0000,00000,0,00,00000,0000,00000,0,00
0001407822,19147,0049,00938,0,00000,0000,00000,0,00,00000,0000,00000,0,00
0001407834,19144,0048,00918,0,00000,0000,00000,0,00,00000,0000,00000,0,00
0001407843,19145,0047,00899,0,00000,0000,00000,0,00,00000,0000,00000,0,00
0001407855,19145,0047,00899,0,00000,0000,00000,0,00,00000,0000,00000,0,00
0001407866,19145,0047,00899,0,00000,0000,00000,0,00,00000,0000,00000,0,00
0001407877,19143,0047,00899,0,00000,0000,00000,0,00,00000,0000,00000,0,00
0001407889,19144,0047,00899,0,00000,0000,00000,0,00,00000,0000,00000,0,00
0001407900,19143,0049,00938,0,00000,0000,00000,0,00,00000,0000,00000,0,00
0001407913,19149,0048,00919,0,00000,0000,00000,0,00,00000,0000,00000,0,00
0001407925,19144,0046,00880,0,00000,0000,00000,0,00,00000,0000,00000,0,00
0001407939,19149,0046,00880,0,00000,0000,00000,0,00,00000,0000,00000,0,00
0001407950,19146,0047,00899,0,00000,0000,00000,0,00,00000,0000,00000,0,00
0001407963,19146,0047,00899,0,00000,0000,00000,0,00,00000,0000,00000,0,00
0001407974,19145,0047,00899,0,00000,0000,00000,0,00,00000,0000,00000,0,00
0001407987,19147,0049,00938,0,00000,0000,00000,0,00,00000,0000,00000,0,00
0001408005,19142,0046,00880,0,00000,0000,00000,0,00,00000,0000,00000,0,00
0001408018,19147,0046,00880,0,00000,0000,00000,0,00,00000,0000,00000,0,00
0001408030,19146,0045,00861,0,00000,0000,00000,0,00,00000,0000,00000,0,00
0001408043,19145,0045,00861,0,00000,0000,00000,0,00,00000,0000,00000,0,00
0001408061,19145,0052,00995,0,00000,0000,00000,0,00,00000,0000,00000,0,00
0001408071,19146,0050,00957,0,00000,0000,00000,0,00,00000,0000,00000,0,00
0001408081,19146,0050,00957,0,00000,0000,00000,0,00,00000,0000,00000,0,00
0001408118,19142,0049,00937,0,00000,0000,00000,0,00,00000,0000,00000,0,00
0001408130,19145,0046,00880,0,00000,0000,00000,0,00,00000,0000,00000,0,00
0001408145,19147,0046,00880,0,00000,0000,00000,0,00,00000,0000,00000,0,00
0001408161,19150,0047,00900,0,00000,0000,00000,0,00,00000,0000,00000,0,00

Barely any samples actually are spaced out by 10ms.

I think the Wi-Fi process is too heavy and clogs the normal operation of the device.
I think it is sensible to sacrifice Wi-Fi response time to restore equal interval operation.

What I suggest to do is either:

  • Decrease Wi-Fi task priority
  • Set Wi-Fi task affinity to different core than Log Task
  • Modify Wi-Fi task so it sends data less often (every 100ms or 200ms is reasonable)
@tomek-szczesny
Copy link
Contributor Author

The logTask loop also seems to poll interrupt flag with no delay whatsoever. This may hog CPU to 100% when waiting for something to happen.
Hardware timer is a sensible approach, but I think this should work as an interrupt routine, and not polled like this.
Alternatively, we could get rid of the hardware timer and just use vTaskDelay() instead.

Optimizing logTask will give Wifi Task more CPU time to do its job!

@leeseungcheol
Copy link
Collaborator

@tomek-szczesny Thank you for the feedback.
I agree with your suggestions. For more stability of WiFi logging, it needs to modify its intervals including optimizing Log task.

First, I will check that you can set log intervals both of WiFi and Serial each of them so that we can set different intervals.
I think we need to update UI as well.

@cedel1
Copy link
Contributor

cedel1 commented Apr 3, 2023

Hmm, I was thinking, in addition to the above, maybe saving the logging output to some temporary memory buffer and reading the values for WiFi and serial logging output from there could decouple the tasks (and maybe run them on different cores). Then the logging would not necessarily have to wait for Serial and WiFi to finish sending, the log sending method could be turned on/off independently.

That would also have it's drawbacks (like what if the sending part is too slow and the memory buffer grows too large or overflows, but that is also solvable).

@tomek-szczesny
Copy link
Contributor Author

Breaking down processes into asynchronous producers and consumers may also help, especially if the consumer runs at the lowest priority. Good idea. Like I was saying, I think the log data may be buffered and sent less often.

That would also have it's drawbacks (like what if the sending part is too slow and the memory buffer grows too large or overflows, but that is also solvable).

Quite unlikely to happen, and the real question is what would we do if the consumer/sender part said the buffer was full.. Stop measurements? :) Might as well discard some samples that do not fit into the buffer.

@cedel1
Copy link
Contributor

cedel1 commented Apr 3, 2023

Might as well discard some samples that do not fit into the buffer.

Just brainstorming:

  • If backwards compatibility (with users tools that depend on the logging output) wasn't an issue, I'd suggest discard some samples and replace them (let's say 3-5 samples) with one info message that "samples were dropped".

  • Or better yet, if resources would permit, remove every Xth sample, effectively reducing the output sampling frequency - that way, there still would be some output, just not with such a good resolution.

  • Breaking it down into separate processes would have another advantage/drawback - one could easily implement another sending method that wouldn't have to touch the logging (but I cannot currently imagine what that would be, maybe could be used with SCPI?) or turn the sending methods on/off independently (currently, if WiFi is configured, it still sends samples over Serial and, if I remember correctly, logging stops when WiFi command mode menu is displayed.

  • But if more then one "consumer" would be selected, there would have to be some mechanism to ensure sample was "consumed" by every consumer currently configured to send (or permit just one at a time).

@tomek-szczesny
Copy link
Contributor Author

I think you convinced me that no samples should ever be dropped - that would be a regression compared to current solution.
On the other hand, I find buffer overflows highly unlikely. We're talking about some <10KBps streams after all.

But, digging deeper, perhaps a buffer should be a class of its own. A circular buffer with no explicit outputs.

Here's how I see it: A circular buffer class, that is basically an array of bytes to be sent, and one integer variable, a "write counter". This variable will always represent an array index of the last byte written to the array. All the new data will be written after the old one, in a circular manner. The producer, writing data to a buffer, will also update the "write counter".

Consumers will have read access to this buffer, more specifically the data array and write counter. Each consumer will have their own integer variable, the "read counter", that will represent the last index the consumer has read in previous iteration.
Consumers will be free to read the data and forward it, independently. So more consumers may be added later if needed with no effort.

If we assume that producer will never be faster than consumers, then no mutex magic is even required. I think 10kB buffer would hold up to 1s worth of data, which is definitely enough.

I'm just thinking out loud, maybe not all of that makes perfect sense. I won't be able to help you implementing it, so it's up to you to select the approach you think is the best.

By the way, you've done such a great job working on this firmware, thanks for that. If you were a student that could be a solid basis for a thesis, you know. :)

@cedel1
Copy link
Contributor

cedel1 commented Apr 3, 2023

Thank you :) I'll think about the problem. To be frank It won't be the next item I am planning to implement (have something else related to SP3 in the works and time is limited) - just wanted to get my thoughts on the matter written down here just in case somebody else decides to work on this in the meantime.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants