-
Notifications
You must be signed in to change notification settings - Fork 25
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
Add used() API in AddressAllocator #95
base: main
Are you sure you want to change the base?
Conversation
dup of #94 @roypat thanks for reviewing my PR. |
You can have a memory pool from 0 to 12K. You can allocate a block with ExactMatch from 4k to 8k. Now you have 8k of available memory but you can not allocate a 8k blob. I think that in this scenario "available" as it is implemented is misleading.
Maybe you could check that the memory that has been freed is the same as memory that has been allocated. |
yeah, i understand this fragmentation and
the |
8a27025
to
72e663c
Compare
maybe we can have the inverse of this API instead? Something that returns how many bytes are used from the allocator range? That should cover what you want to do, and be less misleading, API-wise. |
sg, that works too, any API that can expose the available/used memory in the address space/interval tree would be helpful. let me update the code. |
@roypat @AlexandruCihodaru can you help review the changes again and see if you have any further comments? |
Summary: follow wiki (https://www.internalfb.com/wiki/Rust/Third_Party_Libraries/Adding_or_Updating_Libraries/#maintaining-local-change) to patch the pkg w/ my PR. * rust-vmm/vm-allocator#95 * Added a `used()` API in `AddressAllocator` so in runtime we can report DRAM/SRAM usage. NOTE to myself on updating rust 3rd party lib * update https://www.internalfb.com/code/fbsource/[eae01930e4f97fad5f6f03341bbbdc0b62636187]/third-party/rust/Cargo.toml?lines=1483 * `cd ~/fbsource && fbcode/common/rust/tools/reindeer/vendor && arc autocargo` Reviewed By: diliop Differential Revision: D69826054 fbshipit-source-id: c712d57a6e74398b61e8288c6fae5d50954a967f
Summary: follow wiki (https://www.internalfb.com/wiki/Rust/Third_Party_Libraries/Adding_or_Updating_Libraries/#maintaining-local-change) to patch the pkg w/ my PR. * rust-vmm/vm-allocator#95 * Added a `used()` API in `AddressAllocator` so in runtime we can report DRAM/SRAM usage. NOTE to myself on updating rust 3rd party lib * update https://www.internalfb.com/code/fbsource/[eae01930e4f97fad5f6f03341bbbdc0b62636187]/third-party/rust/Cargo.toml?lines=1483 * `cd ~/fbsource && fbcode/common/rust/tools/reindeer/vendor && arc autocargo` Reviewed By: diliop Differential Revision: D69826054 fbshipit-source-id: c712d57a6e74398b61e8288c6fae5d50954a967f
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Overall LGTM. Please squash your commits and clean you commit messages.
ec1147d
to
441b079
Compare
Signed-off-by: ylzh10 <46687083+ylzh10@users.noreply.github.com>
ec1147d
to
598cd61
Compare
self.used = self | ||
.used | ||
.checked_add(allocated.len() as usize) | ||
.expect("Failed to calculate used memory"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am not sure that expect is the best solution here. I think it would be better to use ok_or(Error::Overflow)
and propagate the error with ?
self.used = self | ||
.used | ||
.checked_sub(key.len() as usize) | ||
.expect("Failed to calculate used memory"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same here but with ok_or(Error:Underflow)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This error conditions is impossible to hit unless the logic in the library is fundamentally broken. In that case, panicking is the correct thing to do I'd say - if we just propagate with ?
then the allocate will be left in an inconsistent state anyway, which I don't think would be recoverable
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same here but with
ok_or(Error:Underflow)
same thought as @roypat, it should be ok to panic here since the checked_add/sub()
should never fail unless allocate/free()
before these two calls went very wrong like it's capable of allocating some total >=usize:MAX
memory or free some non-existing memory calling used < 0
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In that case, panicking is the correct thing to do I'd say - if we just propagate with ? then the allocate will be left in an inconsistent state anyway, which I don't think would be recoverable
You are right that the interval tree will be likely messed up. But the reason for propagating the error is to allow the caller to clean up after themselves before exiting.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In that case, panicking is the correct thing to do I'd say - if we just propagate with ? then the allocate will be left in an inconsistent state anyway, which I don't think would be recoverable
You are right that the interval tree will be likely messed up. But the reason for propagating the error is to allow the caller to clean up after themselves before exiting.
i don't really expect caller to handle this error externally, since it's non-recoverable and the program should crash if it hits here.
How would you expect caller to handle/clean up this, like undo the allocate/free()? but the previous allocate/free on the interval tree has succeeded before the self.used.add/sub()
and callers have no way to manipulate self.used
w/o changing the interval tree again, in which case the interval tree and used()
can never be "consistent" w/ each other. IMHO, panic inside allocator would give callers a signal that this is non-recoverable, they should seek help to fix the allocator rather than remediate on their side
Summary of the PR
bit dumb here, but I thought it should be a common use case to get the available memories in
AddressAllocator
, while i don't see a way on how to get this info today.Or there is already a way to get this info from allocator and somehow i missed it? (please lmk)
So add a new private var
used
allocate/free()
used()
so we can infer how many available memories are left (We don't want to exposeavailable()
directly since it can be confusing/inaccurate due to fragmentations most times)Requirements
Before submitting your PR, please make sure you addressed the following
requirements:
git commit -s
), and the commit message has max 60 characters for thesummary and max 75 characters for each description line.
test.
Release" section of CHANGELOG.md (if no such section exists, please create one).
unsafe
code is properly documented.