-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
O1 heap, executor and main loop (#22)
- added `platform::O1HeapMemoryResource` - added platform executor and main loop
- Loading branch information
Showing
12 changed files
with
598 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// This software is distributed under the terms of the MIT License. | ||
// Copyright (C) OpenCyphal Development Team <opencyphal.org> | ||
// Copyright Amazon.com Inc. or its affiliates. | ||
// SPDX-License-Identifier: MIT | ||
// Author: Sergei Shirokov <[email protected]> | ||
|
||
#include "application.hpp" | ||
|
||
#include <cetl/pf17/cetlpf.hpp> | ||
#include <o1heap.h> | ||
|
||
#include <array> | ||
#include <cstddef> | ||
#include <iostream> | ||
|
||
namespace | ||
{ | ||
|
||
constexpr std::size_t HeapSize = 16ULL * 1024ULL; | ||
alignas(O1HEAP_ALIGNMENT) std::array<cetl::byte, HeapSize> s_heap_arena{}; | ||
|
||
} // namespace | ||
|
||
Application::Application() | ||
: o1_heap_mr_{s_heap_arena} | ||
{ | ||
} | ||
|
||
Application::~Application() | ||
{ | ||
const auto mr_diag = o1_heap_mr_.queryDiagnostics(); | ||
std::cout << "O(1) Heap diagnostics:" << "\n" | ||
<< " tcapacity=" << mr_diag.capacity << "\n" | ||
<< " tallocated=" << mr_diag.allocated << "\n" | ||
<< " tpeak_allocated=" << mr_diag.peak_allocated << "\n" | ||
<< " tpeak_request_size=" << mr_diag.peak_request_size << "\n" | ||
<< " toom_count=" << mr_diag.oom_count << "\n"; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// This software is distributed under the terms of the MIT License. | ||
// Copyright (C) OpenCyphal Development Team <opencyphal.org> | ||
// Copyright Amazon.com Inc. or its affiliates. | ||
// SPDX-License-Identifier: MIT | ||
// Author: Sergei Shirokov <[email protected]> | ||
|
||
#ifndef APPLICATION_HPP | ||
#define APPLICATION_HPP | ||
|
||
#include "platform/linux/epoll_single_threaded_executor.hpp" | ||
#include "platform/o1_heap_memory_resource.hpp" | ||
|
||
#include <cetl/pf17/cetlpf.hpp> | ||
|
||
/// The main application class. | ||
/// | ||
/// Expected to be a singleton. | ||
/// | ||
class Application final | ||
{ | ||
public: | ||
Application(); | ||
~Application(); | ||
|
||
Application(const Application&) = delete; | ||
Application& operator=(const Application&) = delete; | ||
Application(Application&&) = delete; | ||
Application& operator=(Application&&) = delete; | ||
|
||
CETL_NODISCARD platform::Linux::EpollSingleThreadedExecutor& executor() noexcept | ||
{ | ||
return executor_; | ||
} | ||
|
||
CETL_NODISCARD cetl::pmr::memory_resource& memory() noexcept | ||
{ | ||
return o1_heap_mr_; | ||
} | ||
|
||
private: | ||
// MARK: Data members: | ||
|
||
platform::Linux::EpollSingleThreadedExecutor executor_; | ||
platform::O1HeapMemoryResource o1_heap_mr_; | ||
|
||
}; // Application | ||
|
||
#endif // APPLICATION_HPP |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,13 +4,55 @@ | |
// SPDX-License-Identifier: MIT | ||
// Author: Sergei Shirokov <[email protected]> | ||
|
||
#include "application.hpp" | ||
|
||
#include <cetl/pf17/cetlpf.hpp> | ||
#include <libcyphal/application/node.hpp> | ||
#include <libcyphal/executor.hpp> | ||
#include <libcyphal/types.hpp> | ||
|
||
#include <algorithm> | ||
#include <chrono> | ||
#include <iostream> | ||
|
||
using namespace std::chrono_literals; | ||
|
||
using Callback = libcyphal::IExecutor::Callback; | ||
|
||
int main() | ||
{ | ||
const std::string str{"LibCyphal demo."}; | ||
std::cout << str << "\n"; | ||
Application application; | ||
auto& executor = application.executor(); | ||
|
||
std::cout << "LibCyphal demo." << "\n"; | ||
|
||
auto cb = executor.registerCallback([](auto&) { | ||
// | ||
std::cout << "Callback fired." << "\n"; | ||
}); | ||
cb.schedule(Callback::Schedule::Repeat{executor.now(), libcyphal::Duration{1s}}); | ||
|
||
// Main loop. | ||
// | ||
libcyphal::Duration worst_lateness{0}; | ||
const libcyphal::TimePoint deadline = executor.now() + 4s; | ||
std::cout << "-----------\nRunning..." << std::endl; // NOLINT | ||
// | ||
while (executor.now() < deadline) | ||
{ | ||
const auto spin_result = executor.spinOnce(); | ||
worst_lateness = std::max(worst_lateness, spin_result.worst_lateness); | ||
|
||
libcyphal::Duration timeout{1s}; // awake at least once per second | ||
if (spin_result.next_exec_time.has_value()) | ||
{ | ||
timeout = std::min(timeout, spin_result.next_exec_time.value() - executor.now()); | ||
} | ||
(void) executor.pollAwaitableResourcesFor(cetl::make_optional(timeout)); | ||
} | ||
// | ||
std::cout << "Done.\n-----------\nRun Stats:\n"; | ||
std::cout << " worst_callback_lateness=" << worst_lateness.count() << "us\n"; | ||
|
||
return 0; | ||
} |
Oops, something went wrong.