Skip to content

Commit

Permalink
Iyää
Browse files Browse the repository at this point in the history
  • Loading branch information
yxhuvud committed Jan 7, 2020
0 parents commit 928e2e0
Show file tree
Hide file tree
Showing 14 changed files with 389 additions and 0 deletions.
9 changes: 9 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
root = true

[*.cr]
charset = utf-8
end_of_line = lf
insert_final_newline = true
indent_style = space
indent_size = 2
trim_trailing_whitespace = true
9 changes: 9 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/docs/
/lib/
/bin/
/.shards/
*.dwarf

# Libraries don't need dependency lock
# Dependencies will be locked in applications that use them
/shard.lock
6 changes: 6 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
language: crystal

# Uncomment the following if you'd like Travis to run specs and check code formatting
# script:
# - crystal spec
# - crystal tool format --check
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2019 Linus Sellberg

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
8 changes: 8 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
all:
cc -march=native -g -c -o build/shim.o -Wall -O3 src/c/shim.c
crystal run spec/ior_spec.cr

clean:
rm build/*
rm ior
rm ior_spec
43 changes: 43 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# ior
Bindings for liburing. These bindings are not intended to be safe,
or to replace the built-in ways of doing IO in Crystal. Rather this
is intended to be the base for other libraries and applications use
so that they can accomplish that.



## Installation

1. Add the dependency to your `shard.yml`:

```yaml
dependencies:
ior:
github: your-github-user/ior
```
2. Run `shards install`

## Usage

```crystal
require "ior"
```

TODO: Write usage instructions here

## Development

TODO: Write development instructions here

## Contributing

1. Fork it (<https://github.com/your-github-user/ior/fork>)
2. Create your feature branch (`git checkout -b my-new-feature`)
3. Commit your changes (`git commit -am 'Add some feature'`)
4. Push to the branch (`git push origin my-new-feature`)
5. Create a new Pull Request

## Contributors

- [Linus Sellberg](https://github.com/your-github-user) - creator and maintainer
9 changes: 9 additions & 0 deletions shard.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
name: ior
version: 0.1.0

authors:
- Linus Sellberg <[email protected]>

crystal: 0.31.1

license: MIT
9 changes: 9 additions & 0 deletions spec/ior_spec.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
require "./spec_helper"

describe IOR do
# TODO: Write tests

it "works" do
false.should eq(true)
end
end
2 changes: 2 additions & 0 deletions spec/spec_helper.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
require "spec"
require "../src/ior"
19 changes: 19 additions & 0 deletions src/c/shim.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include "liburing.h"

extern void _io_uring_cqe_seen(struct io_uring *ring, struct io_uring_cqe *cqe) {
io_uring_cqe_seen(ring, cqe);
}

/* io_uring_smp_load_acquire */
/* io_uring_cq_advance */

/* io_uring_sq_ready */
/* io_uring_sq_space_left */
/* io_uring_cq_ready */
/* io_uring_peek_cqe */
extern inline int _io_uring_wait_cqe(struct io_uring *ring, struct io_uring_cqe **cqe_ptr) {
return io_uring_wait_cqe(ring, cqe_ptr);
}
extern int _io_uring_peek_cqe(struct io_uring *ring, struct io_uring_cqe **cqe) {
return io_uring_peek_cqe(ring, cqe);
}
8 changes: 8 additions & 0 deletions src/ior.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
require "./lib/liburing_shim"
require "./ior/uring"
module IOR
VERSION = "0.1.0"

end

p LibUring::IOUring.new
75 changes: 75 additions & 0 deletions src/ior/uring.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
require "../lib/liburing_shim"

module IOR
class IOUring
# Not inheriting file logic from the various file descriptor classes
# in Crystal stdlib as they have a lot of irrelevant functionality,
# as well as lots of methods that emit blocking syscalls.
private property closed : Bool
private property ring : Pointer(LibUring::IOUring)

def initialize(size = 32)
@closed = false

flags = 0
# TODO: Support
# SETUP_SQPOLL: !
# SETUP_CQSIZE
LibUring.io_uring_queue_init(size, out @ring, flags)
end

def close
LibUring.io_uring_queue_exit(ring)
@closed = true
end

def closed?
@closed
end

def finalize
return if closed?

close rescue nil
end

private def get_sqe
LibUring.io_uring_get_sqe(ring)
end

def readv(fd, iovecs, offset, user_data = 0)
prep_rw(LibUring::Op::READV, fd, iovecs.to_unsafe, iovecs.size, offset, user_data)
end

def writev(fd, iovecs, offset, user_data = 0)
prep_rw(LibUring::Op::WRITEV, fd, iovecs.to_unsafe, iovecs.size, offset, user_data)
end

def sendmsg(fd, msg : MsgHeader*, flags, user_data = 0)
prep_rw(LibUring::Op::SENDMSG, fd, msg, 1, 0, user_data).tap do |sqe|
sqe.flags = flags
end
end

def recvmsg(fd, msg : MsgHeader*, flags, user_data = 0)
prep_rw(LibUring::Op::RECVMSG, fd, msg, 1, 0, user_data).tap do |sqe|
sqe.flags = flags
end
end

private def prep_rw(op : LibUring::Op, file : File, addr, length, offset, user_data)
sqe = get_sqe
sqe.value.opcode = op
sqe.value.flags = 0
sqe.value.ioprio = 0
sqe.value.fd = file.fd
sqe.value.off_or_addr2.off = offset
sqe.value.addr = addr.address
sqe.value.len = length
sqe.value.event_flags.rw_flags = 0
sqe.value.user_data = user_data
sqe.value.buf_or_pad.pad2[0] = sqe.value.buf_or_pad.pad2[1] = sqe.value.buf_or_pad.pad2[2] = 0
sqe
end
end
end
163 changes: 163 additions & 0 deletions src/lib/liburing.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
@[Link("liburing")]
lib LibUring
# Not really certain this is correct:
alias KernelRWFT = Int32

enum Op : UInt8
NOP
READV
WRITEV
FSYNC
READ_FIXED
WRITE_FIXED
POLL_ADD
POLL_REMOVE
SYNC_FILE_RANGE
SENDMSG
RECVMSG # ^Linux 5.1
TIMEOUT
TIMEOUT_REMOVE
ACCEPT
ASYNC_CANCEL
LINK_TIMEOUT # ^Linux 5.2
CONNECT
FALLOCATE
OPENAT
CLOSE
FILES_UPDATE
STATX
READ
WRITE # ^Linux 5.3
end

struct IOUringSQ
khead : LibC::UInt*
ktail : LibC::UInt*
kring_mask : LibC::UInt*
kring_entries : LibC::UInt*
kflags : LibC::UInt*
kdropped : LibC::UInt*
array : LibC::UInt*
io_uring_sqe : IOUringSQE*
sqe_head : LibC::UInt
sql_tail : LibC::UInt

ring_sz : LibC::SizeT
ring_ptr : Void*
end

union OffOrAddr2
off : UInt64
addr2 : UInt64
end

union EventFlags
rw_flags : KernelRWFT
fsync_flags : UInt32
poll_events : UInt16
sync_range_flags : UInt32
msg_flags : UInt32
timeout_flags : UInt32
accept_flags : UInt32
end

union BufOrPad
buf_index : UInt16
pad2 : UInt64[3]
end

struct IOUringSQE
opcode : Op
flags : UInt8
ioprio : UInt16
fd : Int32
off_or_addr2 : OffOrAddr2
addr : UInt64
len : UInt32
event_flags : EventFlags
user_data : UInt64
buf_or_pad : BufOrPad
end

struct IOUringCQE
user_data : UInt64
res : Int32
flags : UInt32
end

struct IOUringCQ
khead : LibC::UInt*
ktail : LibC::UInt*
kring_mask : LibC::UInt*
kring_entries : LibC::UInt*
koverflow : LibC::UInt*
cqes : IOUringCQE*

ring_sz : LibC::SizeT
ring_ptr : Void*
end

struct IOUring
sq : IOUringSQ
cq : IOUringCQ
flags : LibC::UInt
ring_fd : LibC::Int
end

struct IOSQRingOffsets
head : UInt32
tail : UInt32
ring_mask : UInt32
ring_entries : UInt32
flags : UInt32
dropped : UInt32
array : UInt32
resv1 : UInt32
resv2 : UInt64
end

struct IOCQRingOffsets
head : UInt32
tail : UInt32
ring_mask : UInt32
ring_entries : UInt32
overflow : UInt32
cqes : UInt32
resv : UInt64[2]
end

struct IOUringParams
sq_entries : UInt32
cq_entries : UInt32
flags : UInt32
sq_thread_cpu : UInt32
sq_thread_idle : UInt32
features : UInt32
resv : UInt32[4]
sq_off : IOSQRingOffsets
cq_off : IOCQRingOffsets
end

struct IOVec
# Workaround to crystal issue #4599
# iov_base : Void*
iov_base : Int64
iov_len : LibC::SizeT
end

struct MsgHeader
name : LibC::Sockaddr*
namelen : LibC::Int
iov : IOVec*
iovlen : LibC::SizeT
control : Void*
controllen : LibC::SizeT
flags : LibC::UInt
end

fun io_uring_queue_init_params(entries : LibC::UInt, ring : IOUring*, p : IOUringParams*) : LibC::Int
fun io_uring_queue_init(entries : LibC::UInt, ring : IOUring*, flags : LibC::UInt) : LibC::Int
fun io_uring_get_sqe(ring : IOUring*) : IOUringSQE*
fun io_uring_submit(IOUring*) : LibC::Int
fun io_uring_queue_exit(IOUring*) : Void
end
Loading

0 comments on commit 928e2e0

Please sign in to comment.