Guest User

Untitled

a guest
May 6th, 2020
284
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.63 KB | None | 0 0
  1. #include <errno.h>
  2. #include <fcntl.h>
  3. #include <pthread.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include <unistd.h>
  8. #include <stdbool.h>
  9.  
  10. #include "liburing.h"
  11.  
  12. #define BS 4096
  13. #define QD 64
  14.  
  15. static struct io_uring ring;
  16. static struct iovec iov;
  17. static struct io_uring_params p;
  18. static int fd;
  19. static int inflight;
  20.  
  21. pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
  22. pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
  23.  
  24. static void increment_inflight(void)
  25. {
  26.     pthread_mutex_lock(&lock);
  27.  
  28.     while (inflight >= p.cq_entries) {
  29.         pthread_cond_wait(&cond, &lock);
  30.     }
  31.  
  32.     inflight++;
  33.  
  34.     pthread_mutex_unlock(&lock);
  35. }
  36.  
  37. static void decrement_inflight(void)
  38. {
  39.     pthread_mutex_lock(&lock);
  40.  
  41.     inflight--;
  42.     pthread_cond_signal(&cond);
  43.  
  44.     pthread_mutex_unlock(&lock);
  45. }
  46.  
  47. static void *submit_io(void *input)
  48. {
  49.     off_t offset = 0;
  50.     int total_ios = *((int *)input);
  51.  
  52.     while (total_ios != 0) {
  53.         increment_inflight();
  54.  
  55.         struct io_uring_sqe *sqe = io_uring_get_sqe(&ring);
  56.  
  57.         io_uring_prep_writev(sqe, fd, &iov, 1, offset);
  58.         sqe->user_data = offset;
  59.  
  60.         total_ios--;
  61.  
  62.         io_uring_submit(&ring);
  63.  
  64.         offset += BS;
  65.  
  66.     }
  67.  
  68.         return NULL;
  69. }
  70.  
  71. static void *reap_io_completions(void *input)                                                                                                                                     [29/192]
  72. {
  73.     int total_ios = *((int *)input);
  74.     int failed_ios = 0;
  75.  
  76.     while (total_ios != 0) {
  77.         struct io_uring_cqe *cqe = NULL;
  78.  
  79.         int rc = io_uring_wait_cqe(&ring, &cqe);
  80.         if (rc < 0 || cqe->res != BS) {
  81.             printf("rc=%d cqe->res=%d offset=%llu\n",
  82.                    rc, cqe->res, cqe->user_data);
  83.             failed_ios++;
  84.         }
  85.  
  86.         total_ios--;
  87.         io_uring_cqe_seen(&ring, cqe);
  88.  
  89.         decrement_inflight();
  90.     }
  91.  
  92.     printf("io_failed=%d\n", failed_ios);
  93.  
  94.         return NULL;
  95. }
  96.  
  97. static void *setup_iov_base(size_t size)
  98. {
  99.     void *buf;
  100.     int fd;
  101.  
  102.     if (posix_memalign(&buf, BS, size) != 0) {
  103.         printf("mem aligned failed\n");
  104.         return NULL;
  105.     }
  106.  
  107.     fd = open("/dev/urandom", O_RDONLY);
  108.     if (fd < 0) {
  109.         printf("Failed to open urandom. rc=%d\n", fd);
  110.         return NULL;
  111.     }
  112.  
  113.     read(fd, buf, size);
  114.     close(fd);
  115.  
  116.     return buf;
  117. }
  118.  
  119. static int init(void)
  120. {
  121.     int rc;
  122.  
  123.     rc = io_uring_queue_init_params(QD, &ring, &p);
  124.     if (rc != 0) {
  125.         printf("queue_init failed. rc=%d\n", rc);
  126.         return rc;
  127.     }
  128.  
  129.     fd = open("/dev/nvme1n1", O_RDWR | O_DIRECT | O_SYNC);
  130.     if (fd < 0) {
  131.         printf("Failed to open nvme device. rc=%d\n", fd);
  132.         return fd;
  133.     }
  134.  
  135.     iov.iov_base = setup_iov_base(BS);
  136.     iov.iov_len = BS;
  137.  
  138.     return 0;
  139. }
  140.  
  141.  
  142. int main(int argc, char *argv[])
  143. {
  144.     pthread_t submit, complete;
  145.     int total_ios, rc;
  146.  
  147.         if (argc != 2) {
  148.         printf("Correct usage: %s <total_ios> \n", argv[0]);
  149.         return -EINVAL;
  150.         }
  151.  
  152.     total_ios = atoi(argv[1]);
  153.  
  154.     rc = init();
  155.     if (rc != 0) {
  156.         return rc;
  157.     }
  158.  
  159.     rc = pthread_create(&submit, NULL, submit_io, &total_ios);
  160.     if (rc < 0) {
  161.         printf("Failed to create submit thread. rc=%d\n", rc);
  162.         return rc;
  163.     }
  164.  
  165.     rc = pthread_create(&complete, NULL, reap_io_completions, &total_ios);
  166.     if (rc < 0) {
  167.         printf("Failed to create complete thread. rc=%d\n", rc);
  168.         return rc;
  169.     }
  170.  
  171.     pthread_join(submit, NULL);
  172.     pthread_join(complete, NULL);
  173.  
  174.     io_uring_queue_exit(&ring);
  175.  
  176.     close(fd);
  177.  
  178.     return 0;
  179. }
Advertisement
Add Comment
Please, Sign In to add comment