Skip to content

Commit

Permalink
O1 heap, executor and main loop (#22)
Browse files Browse the repository at this point in the history
- added `platform::O1HeapMemoryResource`
- added platform executor and main loop
  • Loading branch information
serges147 authored Oct 17, 2024
1 parent 8e49db2 commit cd315f7
Show file tree
Hide file tree
Showing 12 changed files with 598 additions and 7 deletions.
8 changes: 6 additions & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@ name: build

on:
push:
branches: [ "sshirokov/libcyphal" ]
branches:
- main
- 'issue/*'
pull_request:
branches: [ "sshirokov/libcyphal" ]
branches:
- main
- 'issue/*'

env:
# Customize the CMake build type here (Release, Debug, RelWithDebInfo, etc.)
Expand Down
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"cSpell.words": [
"baremetal",
"Cyphal",
"libcyphal",
"POSIX"
],
"cmake.sourceDirectory": "/home/sergei/Develop/git/OpenCyphal-Garage/demos/libcyphal_demo"
Expand Down
1 change: 0 additions & 1 deletion libcyphal_demo/.clang-tidy
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,4 @@ CheckOptions:
- key: readability-magic-numbers.IgnoredIntegerValues
value: '1;2;3;4;5;8;10;16;20;32;60;64;100;128;256;500;512;1000'
WarningsAsErrors: '*'
HeaderFilterRegex: 'include/libcyphal/.*\.hpp'
FormatStyle: file
3 changes: 2 additions & 1 deletion libcyphal_demo/src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@ cmake_minimum_required(VERSION 3.20)
# Define the demo application build target and link it with the library.
add_executable(
demo
${CMAKE_SOURCE_DIR}/src/application.cpp
${CMAKE_SOURCE_DIR}/src/main.cpp
${CMAKE_SOURCE_DIR}/src/no_cpp_heap.cpp
)
target_link_libraries(demo PRIVATE udpard)
target_link_libraries(demo PRIVATE canard)
target_link_libraries(demo PRIVATE o1heap)
target_link_libraries(demo PRIVATE udpard)
target_include_directories(demo PRIVATE ${submodules}/cetl/include)
target_include_directories(demo PRIVATE ${submodules}/libcyphal/include)
add_dependencies(demo dsdl_uavcan)
Expand Down
38 changes: 38 additions & 0 deletions libcyphal_demo/src/application.cpp
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";
}
48 changes: 48 additions & 0 deletions libcyphal_demo/src/application.hpp
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
46 changes: 44 additions & 2 deletions libcyphal_demo/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Loading

0 comments on commit cd315f7

Please sign in to comment.