Skip to content

add RtcState object #65

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 5 commits into from
Oct 5, 2021
Merged

add RtcState object #65

merged 5 commits into from
Oct 5, 2021

Conversation

lauralt
Copy link
Collaborator

@lauralt lauralt commented Sep 13, 2021

This PR is adding support for saving and restoring the state of the RTC device. We are starting to add this support in rust-vmm to enable snapshot use cases, such as live migration, and to unblock the consumption of components (e.g. virtio devices, legacy devices).

This is also converting vm-superio to a workspace, in which the base vm-superio provides the functionality required to get/set the state of the device, and vm-superio-ser defines the RtcStateSer object (and in the future the ...StateSer objects for the other devices). RtcStateSer mirrors the state structure from vm-superio, and derives the required (de)serialization/versioning traits (i.e. Serialize, Deserialize, Versionize). If we later change the state of the component, this one will have to be updated as well (by also adding the required version constraints on it).

alexandruag
alexandruag previously approved these changes Sep 20, 2021
src/rtc_pl031.rs Outdated
Comment on lines 197 to 198
/// Creates a new `AMBA PL031 RTC` instance from a given `state` and invokes the `rtc_events`
/// implementation of `RtcEvents` during operation.
Copy link
Member

Choose a reason for hiding this comment

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

I think we should also add a comment about wanting to start from a clean state, in which case people should use with_events.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Done.

src/rtc_pl031.rs Outdated
Comment on lines 217 to 218
/// Creates a new `AMBA PL031 RTC` instance and invokes the `rtc_events`
/// implementation of `RtcEvents` during operation.
Copy link
Member

Choose a reason for hiding this comment

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

The part about "invokes the rtc_events" sounds like a implementation detail, and it shouldn't be part of the interface documentation. How about: "Creates a new AMBA PL031 RTC instance that is able to track events during operation using the passed rtc_events object." We should also add a paragraph about the state being the default one or something like that.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Updated the documentation for with_events and new.

// Check that the restored `Rtc` doesn't keep the state of the old `metrics` object.
assert_eq!(rtc_from_state.events.invalid_write_count.count(), 0);
Copy link
Member

Choose a reason for hiding this comment

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

nit: For the sake of the example, maybe it would be a good idea to have a "test as an example" where the metrics are saved as well.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Added an example, it's a bit weird though :-?.

@@ -210,7 +210,7 @@ impl<EV: RtcEvents> Rtc<EV> {
ris: state.ris,
// A struct implementing RtcEvents for tracking the occurrence of
// significant events.
events,
events
Copy link
Member

Choose a reason for hiding this comment

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

nit: this should not be part of the commit.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

removed it.

@@ -134,6 +134,20 @@ pub struct Rtc<EV: RtcEvents> {
events: EV,
}

/// The state of the Rtc device.
pub struct RtcState {
Copy link
Member

Choose a reason for hiding this comment

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

Can we derive Debug, Clone, and PartialEq on this structure? It would help with testing.

Copy link
Member

Choose a reason for hiding this comment

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

I think it might also help to have a derive Default, but I am not sure what should be the implementation of that. Maybe we can implement default ourselves and have everything as 0s expect the LR which we can initialize as the current time on host.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Default is already implemented for RtcState. I was using derive initially, but switched to a manual implementation to be on the safe side, and it also helped to have a clearer documentation.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Added derives for Debug, Clone and PartialEq. I also realized now that RtcStateSer was deriving Default. I removed the derive, we can add a manual implementation if we think it's useful, but it doesn't seem so, at least for now.

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 also added a Default impl, it would look a bit awkward to convert with From from an RtcState::default() in the consumer code in case a default RtcStateSer is needed.

impl Rtc<NoEvents> {
/// Creates a new `AMBA PL031 RTC` instance without any metric capabilities. The instance is
/// created from the default state.
pub fn new() -> Self {
Copy link
Member

Choose a reason for hiding this comment

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

Is it possible to also add a from state constructor here?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

As discussed offline, we can postpone this a bit until we have a nice idea to avoid weird naming.

@lauralt lauralt force-pushed the state_no_ev branch 2 times, most recently from 531b958 to 2e8dbb0 Compare October 4, 2021 17:14
src/rtc_pl031.rs Outdated
assert_eq!(rtc_from_state.events.invalid_write_count.count(), 1);

let state2 = rtc_from_state.state();
let metrics2 = metrics;
Copy link
Member

Choose a reason for hiding this comment

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

Maybe we can rename this to saved_metrics and add a comment here that we are mimicking saving the metrics for the sake of the example so that it is a bit less weird 😆

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

done.

}
}

impl RtcStateSer {
Copy link
Member

Choose a reason for hiding this comment

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

Is there a reason for implementing new here, and from for the reversed operation?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Hmm, not really, I think I was under the impression I can't implement From here. I replaced with From.

RtcState represents the state of the Rtc device, and
is storing the state needed for restoring the device
(no implementation detailis). The generic `events`
object is not stored due to complexity reasons.
More details in the design proposal:
rust-vmm/community#118.

Signed-off-by: Laura Loghin <lauralg@amazon.com>
This crate provides state objects that add serialization
and versionize capabilities to the ones from vm-superio.

Signed-off-by: Laura Loghin <lauralg@amazon.com>
This provides serialization capabilities to RTCState,
and is defined in the vm-superio-ser crate.
RtcStateSer mirrors the state structure from vm-superio and
derives the required (de)serialization/versioning traits
(i.e. Serialize, Deserialize, Versionize).

Signed-off-by: Laura Loghin <lauralg@amazon.com>
Signed-off-by: Laura Loghin <lauralg@amazon.com>
Since now the offset & LR can be set from outside of the Rtc
implementation, we need to add a checked add when computing the value of
the RTC.

Added a test & fix for the overflow.

Signed-off-by: Andreea Florescu <fandree@amazon.com>
@lauralt lauralt merged commit 9b98030 into rust-vmm:main Oct 5, 2021
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.

3 participants