Skip to content

Commit

Permalink
refactor: make libflux library
Browse files Browse the repository at this point in the history
  • Loading branch information
g-tejas committed Jul 13, 2024
1 parent fbe945a commit b245682
Show file tree
Hide file tree
Showing 18 changed files with 89 additions and 62 deletions.
6 changes: 3 additions & 3 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[submodule "external/quill"]
path = external/quill
url = https://github.com/odygrd/quill
[submodule "external/tracy"]
path = external/tracy
url = https://github.com/wolfpld/tracy
20 changes: 5 additions & 15 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,14 @@ set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

# pass cmake configs to build tests or not by default
option(BUILD_BINARY "Build binary" ON)
option(BUILD_TESTS "Build tests" ON)
option(BUILD_EXAMPLES "Build examples" ON)

add_subdirectory(external)
add_subdirectory(libflux)

set(SOURCES
src/main.cpp
src/thread.cpp
src/reactor.cpp
src/backends/darwin.cpp)

if (BUILD_BINARY)
message(STATUS "Building binary")
include_directories(include)
add_executable(${PROJECT_NAME} ${SOURCES})
target_include_directories(${PROJECT_NAME} PUBLIC include)
target_link_libraries(${PROJECT_NAME} PUBLIC external)
if (BUILD_EXAMPLES)
message(STATUS "Building examples")
add_subdirectory(examples)
endif ()

if (BUILD_TESTS)
Expand Down
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,7 @@ https://webflow.com/made-in-webflow/website/Apple-Style-Grid Can make this as th
and `kqueue`,
we just make the edge triggered the default.

## Desgi
## Ideas

- [ ] Inject tracy profiling before functions during build step using AST transformations
- [ ] Use `eventfd` for shared mem OB?
11 changes: 10 additions & 1 deletion docs/ARCHITECTURE.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,13 @@ can be done in two ways,

1. Call `kevent` for each change to actively monitored fd list
2. Build up a list of descriptor changes and pass it to the kernel the next event loop epoch. Reduces number of
sys-calls made
sys-calls made

```cpp
struct kevent {
uintptr_t ident; // Most of the time refers to a FD, but its meaning can change based on the filter. Essentially a value to identify the event
filter; // Determines the kernel filter to process this event, e.g `EVFILT_TIMER`

};
```
2 changes: 2 additions & 0 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
add_executable(fiber fiber.cpp)
target_link_libraries(fiber PRIVATE libflux external)
31 changes: 6 additions & 25 deletions src/main.cpp → examples/fiber.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
#include <fcntl.h>
#include <iostream>

#include "tracy/Tracy.hpp"
#include "tracy/TracyC.h"

#include "backends/darwin.hpp"
#include "thread.hpp"

Expand Down Expand Up @@ -36,6 +39,7 @@ struct Worker : flux::Thread {
thing = (thing * 7 + count) / 8;
printf("count: %d\n", count);
this->wait();
FrameMark;
}
}
};
Expand All @@ -49,33 +53,10 @@ int main() {
flux::KqueueReactor reactor;
reactor.set_timer(1010, 1500, &worker);
// reactor.subscribe(1010, &worker);
tracy::SetThreadName("hello");

while (reactor.active()) {
reactor.work();
FrameMark;
}

return 0;

// CHECK_EX(false, "This is a test");
// exit(1);
// kq = kqueue();
// assert(kq != -1);
//
// struct context obj = {};
// obj.rhandler = [](struct context *obj) {
// printf("Received socket READ event via kqueue\n");
// int csock = accept(obj->sk, NULL, 0);
// assert(csock != -1);
// close(csock);
// };
//
// // creet and prepare a socket
// obj.sk = socket(AF_INET, SOCK_STREAM | SOCK_NONBLOCK, 0);
// assert(obj.sk != -1);
// int val = 1;
// setsockopt(obj.sk, SOL_SOCKET, SO_REUSEADDR, &val, 4);
//
// struct sockaddr_in addr = {};
// addr.sin_family = AF_INET;
// addr.sin_port = ntohs(64000);
}
8 changes: 0 additions & 8 deletions external/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,8 +0,0 @@
find_package(Boost COMPONENTS context REQUIRED)
message(STATUS "Boost include dir: ${Boost_INCLUDE_DIRS}")

add_subdirectory(quill)

add_library(external INTERFACE)
target_link_libraries(external INTERFACE ${Boost_LIBRARIES} quill::quill)
target_include_directories(external INTERFACE ${Boost_INCLUDE_DIRS})
1 change: 0 additions & 1 deletion external/quill
Submodule quill deleted from ea0e0e
1 change: 1 addition & 0 deletions external/tracy
Submodule tracy added at 521e37
44 changes: 44 additions & 0 deletions libflux/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
cmake_minimum_required(VERSION 3.9)
project(libflux)
include(FetchContent)

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# Boost
find_package(Boost COMPONENTS context REQUIRED)
message(STATUS "Boost include dir: ${Boost_INCLUDE_DIRS}")

# Tracy
set(TRACY_ENABLE ON)
set(TRACY_FIBERS ON) # pdf page 38
FetchContent_Declare(
tracy
GIT_REPOSITORY https://github.com/wolfpld/tracy.git
GIT_TAG master
GIT_SHALLOW TRUE
GIT_PROGRESS TRUE
)
FetchContent_MakeAvailable(tracy)

# Quill
FetchContent_Declare(
quill
GIT_REPOSITORY https://github.com/odygrd/quill.git
GIT_TAG master
GIT_SHALLOW TRUE
GIT_PROGRESS TRUE
)
FetchContent_MakeAvailable(quill)


# specify the source files
set(SOURCES
src/thread.cpp
src/reactor.cpp
src/backends/darwin.cpp)

# add the library
add_library(${PROJECT_NAME} STATIC ${SOURCES})
target_include_directories(${PROJECT_NAME} PUBLIC include ${Boost_INCLUDE_DIRS})
target_link_libraries(${PROJECT_NAME} PUBLIC ${Boost_LIBRARIES} Tracy::TracyClient quill:quill)
File renamed without changes.
File renamed without changes.
File renamed without changes.
13 changes: 7 additions & 6 deletions include/thread.hpp → libflux/include/thread.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <boost/context/detail/fcontext.hpp>
#include <boost/context/protected_fixedsize_stack.hpp>
#include <boost/context/stack_context.hpp>
#include <tracy/Tracy.hpp>

#include <cstddef>
#include <vector>
Expand Down Expand Up @@ -41,27 +42,27 @@ class Thread {

~Thread();

//! Called from within thread's context
//! Called from within thread's context.
//! Passes control back to the caller (e.g Reactor), and this thread will be resumed
//! when the events are ready.
auto wait() -> Event *;

//! Resumes the thread with the given event. Returns true if resumable
[[nodiscard]] auto resume(Event *event) -> bool;

//! Where you place your business logic
//! Where you place your business logic.
virtual void run() = 0;

//! Starts executing the `run()` method
//! Starts executing the `run()` method.
void start();

//! Subscribe to a particular file descriptor
//! Subscribe to a particular file descriptor.
auto subscribe(int fd) -> bool;

//! Unsubscribe to a particular file descriptor
//! Unsubscribe to a particular file descriptor.
auto unsubscribe(int fd) -> bool;

//! List of fds that this thread is interested in
//! List of fds that this thread is interested in.
std::vector<int> m_fds;

private:
Expand Down
File renamed without changes.
2 changes: 2 additions & 0 deletions src/backends/darwin.cpp → libflux/src/backends/darwin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ KqueueReactor::KqueueReactor() : m_kqueue_fd{kqueue()}, m_timeout{nullptr} {
KqueueReactor::~KqueueReactor() { close(m_kqueue_fd); }

bool KqueueReactor::work() {
ZoneScoped;
// TODO: 16 can be adjusted based on busy-ness of the event loop. Can even be dynamic
std::array<struct kevent, 16> events{};

Expand Down Expand Up @@ -61,6 +62,7 @@ void KqueueReactor::set_timer(int id, int timer_period, Thread *thread) {
}

void KqueueReactor::handle_sock_op(int fd, Operation op) {
ZoneScoped;
// #ifndef NDEBUG
FLUX_ASSERT(m_kqueue_fd >= 0, "Kqueue file descriptor is invalid");
// #endif
Expand Down
File renamed without changes.
7 changes: 5 additions & 2 deletions src/thread.cpp → libflux/src/thread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

using namespace flux;

const char *flux_thread = "flux_thread";

Thread::Thread(size_t _stack_size) {
StackAllocator stack_allocator(_stack_size);
m_stack = stack_allocator.allocate();
Expand All @@ -13,12 +15,13 @@ Thread::~Thread() {
}

auto Thread::wait() -> Event * {
TracyFiberLeave;
m_return_context = jump_fcontext(m_return_context.fctx, this);
TracyFiberEnter(flux_thread);
return reinterpret_cast<Event *>(m_return_context.data);
}

auto Thread::resume(Event *event) -> bool {
printf("in resume\n");
m_return_context = jump_fcontext(m_return_context.fctx, (void *)event);
return m_return_context.data != THREAD_STATUS_COMPLETE;
}
Expand All @@ -37,7 +40,7 @@ void Thread::enter(ReturnContext ctx) {
auto *thread = reinterpret_cast<Thread *>(ctx.data);

thread->m_return_context = ctx;

TracyFiberEnter(flux_thread);
thread->run();

while (true) {
Expand Down

0 comments on commit b245682

Please sign in to comment.