Skip to content

Split libtock into core and high level library #164

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

Merged
merged 11 commits into from
Mar 19, 2020

Conversation

torfmaster
Copy link
Collaborator

Summary

In order to address #160 we decided to do a first draft of splitting the library into parts.

The idea is that there are two layers:

  • libtock-core
  • libtock-rs

(names tbd). Libtock-core contains the panic handler, the entry point, the allocator and the syscall interfaces. libtock-rs uses libtock-core and the interface stays as usual.

Possible Extensions

We deleted the "blink" code from the panic handler. This saves more than 1kbytes in the text segment. If this feature is still wanted we could implement the following:

  • provide a feature in libtock-core to opt-out the panic handler in libtock-core
  • provide a feature for libtock-rs to opt-out the panic handler in libtock-core and using a blinking panic handler.

In the long run the (default) panic handler could exit the app and send debug information to the kernel which could print it for example. This could be implemented once the kernel functionality is in place.

Open questions

The current implementation of the panic handler more or less silently fails if there is a panic. Panics can be identified by enabling the "trace_syscall" option in the kernel. How should this be tackled?
Should there be a default panic handling or should it be customizable and if yes how should it be customized?

Copy link
Collaborator

@jrvanwhy jrvanwhy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Regarding your open question: If we can build libcore using cargo (see https://www.github.com/tock/tock/issues/1682) then we should be able to enable the panic_immediate_abort feature (see rust-lang/rust#55011), which should help us remove a lot of `panic!()'s machinery. That would remove the call to the panic handler entirely, so it becomes a moot point.

If panic handling is customizable then I think it should be customized through panic handler crates (as is already the case for other platforms). We would need to add a cargo feature to libtock-core to prevent libtock-core from providing its panic handler.

@torfmaster
Copy link
Collaborator Author

Regarding your open question: If we can build libcore using cargo (see https://www.github.com/tock/tock/issues/1682) then we should be able to enable the panic_immediate_abort feature (see rust-lang/rust#55011), which should help us remove a lot of `panic!()'s machinery. That would remove the call to the panic handler entirely, so it becomes a moot point.

Okay, but that's probably something for the remote future, right?

@torfmaster
Copy link
Collaborator Author

If panic handling is customizable then I think it should be customized through panic handler crates (as is already the case for other platforms).

Do you mean adding a crate only containing the panic handler here (and turning off the panic handler in libtock-core)?

@jrvanwhy
Copy link
Collaborator

Regarding your open question: If we can build libcore using cargo (see https://www.github.com/tock/tock/issues/1682) then we should be able to enable the panic_immediate_abort feature (see rust-lang/rust#55011), which should help us remove a lot of `panic!()'s machinery. That would remove the call to the panic handler entirely, so it becomes a moot point.

Okay, but that's probably something for the remote future, right?

Oh, I misunderstood your question as referring to the future. Yes, my comment was out of scope for this PR.

@jrvanwhy
Copy link
Collaborator

If panic handling is customizable then I think it should be customized through panic handler crates (as is already the case for other platforms).

Do you mean adding a crate only containing the panic handler here (and turning off the panic handler in libtock-core)?

That's what I meant. However, when I look around I think my assertion that panic handler crates were in use on other platforms was incorrect.

I'm not strongly opinionated on whether panic behavior is controlled by swapping crates or whether it is controlled through cargo features, other than I think we need a "no libtock-rs panic handler" feature so applications can overwrite things themselves. I would prefer to use interchangable panic handler crates rather than cargo features to control behavior.

Copy link
Collaborator

@alistair23 alistair23 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is great! Do you have any idea what the split looks like size wise?

@@ -1,35 +1,23 @@
#![feature(asm, lang_items, naked_functions)]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this mean the new libtock-rs doesn't need the nightly toolchain?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately not. Its dependencies need it, still. In libtock-core it should be in principle possible to remove the necessity for the nightly compiler (as in https://github.com/rust-embedded/cortex-m-rt), however I am not sure whether it is worth the effort, while the tock kernel needs its.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume this is fixed?

@torfmaster
Copy link
Collaborator Author

This is great! Do you have any idea what the split looks like size wise?

It is hard to answer: usinglibtock-rs is zero cost unless you use certain features. We have made quick investigations about that:

  • using `block_on´ (i.e. using futures instead of sync runtime) costs 200 bytes
  • using the parallel sleep driver costs 1kb

I think the last point has some potential for optimization, if one restricts to not delay in paralllel futures. The current workarounds for drawbacks of the timer interface are quite expensive.

However, I think we currently don't have objective criteria to judge about the size of libtock-rs applications. I started to create some basic tooling for that and have the vision of including that into the CI of libtock: https://github.com/torfmaster/libtock-size-utils (e.g. store some snapshot sizes, and fail if some threshold is hit).

@Woyten
Copy link
Contributor

Woyten commented Mar 14, 2020

  • using `block_on´ (i.e. using futures instead of sync runtime) costs 200 bytes
  • using the parallel sleep driver costs 1kb

It should be mentioned that those are the approximate values for a trivial example. We observed that the impact of futures or the parallel sleep driver on the text size is significantly lower in more complex examples. This is kind of expected due to synergetic effects.

@Woyten Woyten force-pushed the feature/libtock-core branch from 54b9f4c to b2680dd Compare March 14, 2020 22:42
@alistair23
Copy link
Collaborator

What is this PR still waiting on?

@torfmaster
Copy link
Collaborator Author

Apart from some minor fixes we still need to create a solution for the panic handler, i.e. add a feature flag or an external panic crate.

@jrvanwhy
Copy link
Collaborator

I recommend adding a cargo feature flag to libtock-core that -- when enabled -- removes the panic handler.

@torfmaster
Copy link
Collaborator Author

I recommend adding a cargo feature flag to libtock-core that -- when enabled -- removes the panic handler.

I'll do that+add the original panic handler for libtock-rs behind a feature flag.

@torfmaster
Copy link
Collaborator Author

bors try

bors bot added a commit that referenced this pull request Mar 17, 2020
@Woyten
Copy link
Contributor

Woyten commented Mar 17, 2020

I recommend adding a cargo feature flag to libtock-core that -- when enabled -- removes the panic handler.

I already prepared that. It's just that it's hard to test.

@torfmaster
Copy link
Collaborator Author

I already prepared that. It's just that it's hard to test.

Adding the original panic handler inside libtock-rs behind a feature flag may be a test.

@bors
Copy link
Contributor

bors bot commented Mar 17, 2020

try

Build succeeded

@Woyten
Copy link
Contributor

Woyten commented Mar 17, 2020

I pushed something, s.t. you can see where this is getting at but I am still trying to find a solution to include the feature flags into our build pipeline.

@torfmaster
Copy link
Collaborator Author

bors try

bors bot added a commit that referenced this pull request Mar 18, 2020
@bors
Copy link
Contributor

bors bot commented Mar 18, 2020

try

Build succeeded

@torfmaster
Copy link
Collaborator Author

I pushed a commit addressing all comments but the panic_handler issue.

@torfmaster
Copy link
Collaborator Author

I added an example including a custom error handler and build this in CI. An alternative would be to put a regular panic example into the core crate and restore the original error handler of libtock-rs as a default feature.

@Woyten
Copy link
Contributor

Woyten commented Mar 18, 2020

I found a slightly different solution which doesn't add a new example and tests both features.

@bradjc
Copy link
Contributor

bradjc commented Mar 18, 2020

I would recommend a readme in the core folder that describes what is (and what isn't) in core. Mainly for future readers to understand what the conceptual split is.

@torfmaster
Copy link
Collaborator Author

I added a (minimal) readme and remove the now duplicated example with a custom panic handler.

bors try

bors bot added a commit that referenced this pull request Mar 19, 2020
@bors
Copy link
Contributor

bors bot commented Mar 19, 2020

try

Build succeeded

@Woyten Woyten changed the title WIP: Split libtock into core and high level library Split libtock into core and high level library Mar 19, 2020
@Woyten
Copy link
Contributor

Woyten commented Mar 19, 2020

bors r+

@bors
Copy link
Contributor

bors bot commented Mar 19, 2020

Build succeeded

@bors bors bot merged commit d60e365 into tock:master Mar 19, 2020
@Woyten Woyten deleted the feature/libtock-core branch March 19, 2020 18:50
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

Successfully merging this pull request may close these issues.

5 participants