aboutsummaryrefslogtreecommitdiff
path: root/core/sys/linux/uring/doc.odin
blob: 8b9ae5ee89f1463b581bcb76e66e003d3fc92ef3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/*
Wrapper/convenience package over the raw io_uring syscalls, providing help with setup, creation, and operating the ring.

The following example shows a simple `cat` program implementation using the package.

Example:
	package main

	import "base:runtime"

	import "core:fmt"
	import "core:os"
	import "core:sys/linux"
	import "core:sys/linux/uring"

	Request :: struct {
		path:       cstring,
		buffer:     []byte,
		completion: linux.IO_Uring_CQE,
	}

	main :: proc() {
		if len(os.args) < 2 {
			fmt.eprintfln("Usage: %s [file name] <[file name] ...>", os.args[0])
			os.exit(1)
		}

		requests := make_soa(#soa []Request, len(os.args)-1)
		defer delete(requests)

		ring: uring.Ring
		params := uring.DEFAULT_PARAMS
		err := uring.init(&ring, &params)
		fmt.assertf(err == nil, "uring.init: %v", err)
		defer uring.destroy(&ring)

		for &request, i in requests {
			request.path = runtime.args__[i+1]
			// sets up a read requests and adds it to the ring buffer.
			submit_read_request(request.path, &request.buffer, &ring)
		}

		ulen := u32(len(requests))

		// submit the requests and wait for them to complete right away.
		n, serr := uring.submit(&ring, ulen)
		fmt.assertf(serr == nil, "uring.submit: %v", serr)
		assert(n == ulen)

		// copy the completed requests out of the ring buffer.
		cn := uring.copy_cqes_ready(&ring, requests.completion[:ulen])
		assert(cn == ulen)

		for request in requests {
			// check result of the requests.
			fmt.assertf(request.completion.res >= 0, "read %q failed: %v", request.path, linux.Errno(-request.completion.res))
			// print out.
			fmt.print(string(request.buffer))

			delete(request.buffer)
		}
	}

	submit_read_request :: proc(path: cstring, buffer: ^[]byte, ring: ^uring.Ring) {
		fd, err := linux.open(path, {})
		fmt.assertf(err == nil, "open(%q): %v", path, err)

		file_sz := get_file_size(fd)

		buffer^ = make([]byte, file_sz)

		_, ok := uring.read(ring, 0, fd, buffer^, 0)
		assert(ok, "could not get read sqe")
	}

	get_file_size :: proc(fd: linux.Fd) -> uint {
		st: linux.Stat
		err := linux.fstat(fd, &st)
		fmt.assertf(err == nil, "fstat: %v", err)

		if linux.S_ISREG(st.mode) {
			return uint(st.size)
		}

		panic("not a regular file")
	}
*/
package uring