source: examples/io/filereader.c@ 8ff586c

ADT arm-eh ast-experimental enum forall-pointer-decay jacob/cs343-translation new-ast new-ast-unique-expr pthread-emulation qualifiedEnum
Last change on this file since 8ff586c was 2a35f14, checked in by Thierry Delisle <tdelisle@…>, 5 years ago

added simple file reading n times example

  • Property mode set to 100644
File size: 1.9 KB
Line 
1#include <errno.h>
2#include <fcntl.h>
3#include <liburing.h>
4#include <stdbool.h>
5#include <stdio.h>
6#include <stdlib.h>
7#include <string.h>
8#include <unistd.h>
9
10int main(int argc, char * argv[]) {
11 if ( argc != 3 ) {
12 printf( "usage\n" );
13 exit( EXIT_FAILURE );
14 }
15
16 if(argc != 3) {
17 printf("usage: %s FILE TIMES - read FILE from disk TIMES times\n", argv[0]);
18 return EXIT_FAILURE;
19 }
20
21
22 int times = atoi( argv[2] );
23 if(times <= 0) {
24 printf("Invalid number of times %d (from %s).\n", times, argv[2]);
25 return EXIT_FAILURE;
26 }
27
28 int fd = open(argv[1], 0);
29 if(fd < 0) {
30 printf("Could not open file %s.\n", argv[1]);
31 return EXIT_FAILURE;
32 }
33
34 /* prep the array */
35 char data[100];
36 struct iovec iov = { data, 100 };
37
38 /* init liburing */
39 struct io_uring ring;
40 io_uring_queue_init(256, &ring, 0);
41
42 /* declare required structs */
43 printf("Reading %s(%d) %d times\n", argv[1], fd, times);
44 size_t counter = 0;
45 for(int i = 0; i < times; i++) {
46 /* get an sqe and fill in a READV operation */
47 struct io_uring_sqe * sqe = io_uring_get_sqe(&ring);
48 io_uring_prep_readv(sqe, fd, &iov, 1, 0);
49
50 /* tell the kernel we have an sqe ready for consumption */
51 io_uring_submit(&ring);
52
53 /* poll the cq and count how much polling we did */
54 while(true) {
55 struct io_uring_cqe * cqe = NULL;
56 /* wait for the sqe to complete */
57 int ret = io_uring_wait_cqe_nr(&ring, &cqe, 0);
58
59 /* read and process cqe event */
60 switch(ret) {
61 case 0:
62 if( cqe->res < 0 ) {
63 printf("Completion Error : %s\n", strerror( -cqe->res ));
64 return EXIT_FAILURE;
65 }
66 io_uring_cqe_seen(&ring, cqe);
67 goto LOOP;
68 case -EAGAIN:
69 counter++;
70 break;
71 default:
72 printf("Wait Error : %s\n", strerror( -ret ));
73 return EXIT_FAILURE;
74 }
75 }
76
77 LOOP:;
78 }
79
80 printf("%zu\n", counter);
81
82 io_uring_queue_exit(&ring);
83
84 close(fd);
85}
Note: See TracBrowser for help on using the repository browser.