Giter Club home page Giter Club logo

liburing4cpp's Introduction

liburing4cpp

Modern C++ binding for liburing that uses C++20 Coroutines ( but still compiles for clang at C++17 mode with -fcoroutines-ts )

Originally named liburing-http-demo ( this project was originally started for demo )

Requirements

Requires the latest kernel ( currently 5.8 ). Since io_uring is in active development, we will drop old kernel support when every new linux kernel version is released ( before the next LTS version is released, maybe ).

Tested: Ubuntu 5.9.0-050900rc6daily20200923-generic #202009222208 SMP Wed Sep 23 02:24:13 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux with clang version 10.0.0-4ubuntu1

First glance

#include "io_service.hpp"

int main() {
    // You first need an io_service instance
    io_service service;

    // In order to `co_await`, you must be in a coroutine.
    // We use IIFE here for simplification
    auto work = [&] () -> task<> {
        // Use Linux syscalls just as what you did before (except a little changes)
        const auto str = "Hello world\n"sv;
        co_await service.write(STDOUT_FILENO, str.data(), str.size(), 0);
    }();

    // At last, you need a loop to dispatch finished IO events
    // It's usually called Event Loop (https://en.wikipedia.org/wiki/Event_loop)
    service.run(work);
}

Benchmarks

  • Ubuntu 20.04.1 LTS
  • Linux Ubuntu 5.9.0-050900rc6daily20200923-generic #202009222208 SMP Wed Sep 23 02:24:13 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux
  • Intel(R) Xeon(R) CPU E5-2620 v4 @ 2.10GHz
  • Compiler: clang version 10.0.0-4ubuntu1

demo/bench

service.yield:        6527085832
plain IORING_OP_NOP:  5884262348
this_thread::yield:   4486533904
pause:                  41502717

demo/echo_server

with rust_echo_bench: https://github.com/haraldh/rust_echo_bench unit: request/sec

Also see benchmarks for different opcodes

command: cargo run --release

LANG USE_LINK USE_SPLICE USE_POLL operations 1st 2nd 3rd mid rate
C - - 0 RECV-SEND 114461 116797 112112 114461 100.00%
C - - 1 POLL-RECV-SEND 109037 114893 117629 114893 100.38%
C++ 0 0 0 RECV-SEND 117519 121139 120239 120239 105.05%
C++ 0 1 0 SPLICE-SPLICE 90577 91912 92301 91912 80.30%
C++ 1 1 0 SPLICE-SPLICE 93440 92619 94201 93440 81.63%
C++ 0 0 1 POLL-RECV-SEND 107454 111525 111210 111210 97.16%
C++ 0 1 1 POLL-SPLICE-SPLICE 89469 90663 89315 89469 78.17%
C++ 1 1 1 POLL-SPLICE-SPLICE 87628 89099 88708 89099 77.84%

Project Structure

task.hpp

An awaitable class for C++2a coroutine functions. Originally modified from gor_task.h

NOTE: task is not lazily executed, which is easy to use of course, but also can be easily misused. The simplest code to crash your memory is:

{
    char c;
    service.read(STDIN_FILENO, &c, sizeof (c), 0);
}

The task instance returned by service.read is destructed, but the kernel task itself is NOT canceled. The memory of variable c will be written sometime. In this case, out-of-scope stack memory access will happen.

promise.hpp

An awaitable class. It's different from task that it can't used for return type, but can be created directly without calling an async function.

Its design is highly inspired by Promise of JavaScript

when.hpp

Provide helper functions that working with an array of tasks

io_service.hpp

Main liburing binding. Also provides some helper functions for working with posix interfaces easier.

demo

Some examples

file_server.cpp

A simple http file server that returns file's content requested by clients

link_cp.cpp

A cp command inspired by original liburing link-cp demo

http_client.cpp

A simple http client that sends GET http request

threading.cpp

A simple async_invoke implementation

test.cpp

Various simple tests

bench.cpp

Benchmarks

echo_server.cpp

Echo server, features IOSQE_IO_LINK and IOSQE_FIXED_FILE

See also https://github.com/frevib/io_uring-echo-server#benchmarks for benchmarking

Build

This library is header only. It provides some demos for testing

  1. Build liburing and install, the latest version required
  2. sudo apt install clang libc++-dev libc++abi-dev. Make sure clang version >= 9
  3. git clone --recurse-submodules https://github.com/CarterLi/liburing4cpp.git
  4. cd liburing4cpp/demo && make

Note: When benchmarking, you may want to build it with optimization: make MODE=RELEASE.

License

MIT

liburing4cpp's People

Contributors

carterli avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.