-
Notifications
You must be signed in to change notification settings - Fork 7
Backend: Add BackendWithFallback to retry IO #215
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
base: develop
Are you sure you want to change the base?
Changes from all commits
299a72b
c77c747
4c8aa69
722d723
8d2cf82
b3a3330
62db64a
c8a987d
71e168f
3ddd9b0
89a9a9d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,89 @@ | ||||||||||||||||||||||||||||||||
| /* Copyright (c) Advanced Micro Devices, Inc. All rights reserved. | ||||||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||||||
| * SPDX-License-Identifier: MIT | ||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| #include "backend.h" | ||||||||||||||||||||||||||||||||
| #include "buffer.h" | ||||||||||||||||||||||||||||||||
| #include "file.h" | ||||||||||||||||||||||||||||||||
| #include "io.h" | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| #include <cstddef> | ||||||||||||||||||||||||||||||||
| #include <exception> | ||||||||||||||||||||||||||||||||
| #include <memory> | ||||||||||||||||||||||||||||||||
| #include <sys/types.h> | ||||||||||||||||||||||||||||||||
| #include <system_error> | ||||||||||||||||||||||||||||||||
| #include <unistd.h> | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| using namespace hipFile; | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| ssize_t | ||||||||||||||||||||||||||||||||
| Backend::io(IoType type, std::shared_ptr<IFile> file, std::shared_ptr<IBuffer> buffer, size_t size, | ||||||||||||||||||||||||||||||||
| hoff_t file_offset, hoff_t buffer_offset) | ||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||
| ssize_t nbytes = _io_impl(type, file, buffer, size, file_offset, buffer_offset); | ||||||||||||||||||||||||||||||||
| switch (type) { | ||||||||||||||||||||||||||||||||
| case (IoType::Read): | ||||||||||||||||||||||||||||||||
| update_read_stats(nbytes); | ||||||||||||||||||||||||||||||||
| break; | ||||||||||||||||||||||||||||||||
| case (IoType::Write): | ||||||||||||||||||||||||||||||||
| update_write_stats(nbytes); | ||||||||||||||||||||||||||||||||
| break; | ||||||||||||||||||||||||||||||||
| default: | ||||||||||||||||||||||||||||||||
| break; | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
| return nbytes; | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| ssize_t | ||||||||||||||||||||||||||||||||
| BackendWithFallback::io(IoType type, std::shared_ptr<IFile> file, std::shared_ptr<IBuffer> buffer, | ||||||||||||||||||||||||||||||||
| size_t size, hoff_t file_offset, hoff_t buffer_offset) | ||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||
| ssize_t nbytes{0}; | ||||||||||||||||||||||||||||||||
| try { | ||||||||||||||||||||||||||||||||
| nbytes = _io_impl(type, file, buffer, size, file_offset, buffer_offset); | ||||||||||||||||||||||||||||||||
| if (nbytes < 0) { | ||||||||||||||||||||||||||||||||
| // Typically we should not reach this point. But in case we do, throw | ||||||||||||||||||||||||||||||||
| // an exception to use the fallback backend. | ||||||||||||||||||||||||||||||||
| throw std::system_error(-static_cast<int>(nbytes), std::generic_category()); | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
| catch (...) { | ||||||||||||||||||||||||||||||||
| std::exception_ptr e_ptr = std::current_exception(); | ||||||||||||||||||||||||||||||||
| if (is_fallback_eligible(e_ptr, nbytes, file, buffer, size, file_offset, buffer_offset)) { | ||||||||||||||||||||||||||||||||
| nbytes = fallback_backend->io(type, file, buffer, size, file_offset, buffer_offset); | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
| else { | ||||||||||||||||||||||||||||||||
| throw; | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
| return nbytes; | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
riley-dixon marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||||||||||||
| switch (type) { | ||||||||||||||||||||||||||||||||
| case (IoType::Read): | ||||||||||||||||||||||||||||||||
| update_read_stats(nbytes); | ||||||||||||||||||||||||||||||||
| break; | ||||||||||||||||||||||||||||||||
| case (IoType::Write): | ||||||||||||||||||||||||||||||||
| update_write_stats(nbytes); | ||||||||||||||||||||||||||||||||
| break; | ||||||||||||||||||||||||||||||||
| default: | ||||||||||||||||||||||||||||||||
| break; | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
| return nbytes; | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| bool | ||||||||||||||||||||||||||||||||
| BackendWithFallback::is_fallback_eligible(std::exception_ptr e_ptr, ssize_t nbytes, | ||||||||||||||||||||||||||||||||
| std::shared_ptr<IFile> file, std::shared_ptr<IBuffer> buffer, | ||||||||||||||||||||||||||||||||
| size_t size, hoff_t file_offset, hoff_t buffer_offset) const | ||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||
| (void)e_ptr; | ||||||||||||||||||||||||||||||||
| (void)nbytes; | ||||||||||||||||||||||||||||||||
| return static_cast<bool>(fallback_backend) && | ||||||||||||||||||||||||||||||||
| fallback_backend->score(file, buffer, size, file_offset, buffer_offset) >= 0; | ||||||||||||||||||||||||||||||||
riley-dixon marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| void | ||||||||||||||||||||||||||||||||
| BackendWithFallback::register_fallback_backend(std::shared_ptr<Backend> backend) noexcept | ||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
| { | |
| { | |
| // Prevent self-registration, which can cause unbounded recursion on fallback. | |
| if (backend.get() == this) { | |
| return; | |
| } | |
| // Optionally prevent a simple two-node cycle: this -> backend -> this. | |
| if (backend) { | |
| if (auto backend_with_fallback = std::dynamic_pointer_cast<BackendWithFallback>(backend)) { | |
| if (backend_with_fallback->fallback_backend.get() == this) { | |
| return; | |
| } | |
| } | |
| } |
Uh oh!
There was an error while loading. Please reload this page.