FlashGraph-ng
A new frontier in large-scale graph analysis and data mining
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Pages
wpaio.h
1 #ifndef _WPAIO_H_
2 #define _WPAIO_H_
3 
4 /*
5  * Copyright 2014 Open Connectome Project (http://openconnecto.me)
6  * Written by Da Zheng (zhengda1936@gmail.com)
7  *
8  * This file is part of SAFSlib.
9  *
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  *
14  * http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  */
22 
23 # ifndef _GNU_SOURCE
24 #define _GNU_SOURCE
25 #endif
26 
27 #include <unistd.h>
28 #include <stdio.h>
29 #include <sys/types.h>
30 #include <sys/param.h>
31 #include <fcntl.h>
32 #include <stdlib.h>
33 #include <libaio.h>
34 
35 #include "slab_allocator.h"
36 
37 #define A_READ 0
38 #define A_WRITE 1
39 
40 namespace safs
41 {
42 
43 class aio_ctx
44 {
45  obj_allocator<struct iocb> iocb_allocator;
46 public:
47  aio_ctx(int node_id, int max_aio);
48  virtual ~aio_ctx() {
49  }
50 
51  struct iocb* make_io_request(int fd, size_t iosize, long long offset,
52  void* buffer, int io_type, struct io_callback_s *cb);
53  struct iocb *make_iovec_request(int fd, const struct iovec iov[],
54  int count, long long offset, int io_type, struct io_callback_s *cb);
55  void destroy_io_requests(struct iocb **iocbs, int num) {
56  iocb_allocator.free(iocbs, num);
57  }
58 
59  virtual void submit_io_request(struct iocb* ioq[], int num) = 0;
60  virtual int io_wait(struct timespec* to, int num) = 0;
61  virtual int max_io_slot() = 0;
62  virtual void print_stat() {
63  }
64 };
65 
66 class aio_ctx_impl: public aio_ctx
67 {
68  int max_aio;
69  int busy_aio;
70  io_context_t ctx;
71 
72 public:
73  aio_ctx_impl(int node_id, int max_aio): aio_ctx(node_id, max_aio) {
74  this->max_aio = max_aio;
75  busy_aio = 0;
76  memset(&ctx, 0, sizeof(ctx));
77 
78  int ret = io_queue_init(max_aio, &ctx);
79  if (ret < 0) {
80  fprintf(stderr, "io_queue_init fails: %s\n", strerror(-ret));
81  exit (1);
82  }
83  }
84 
85  virtual void submit_io_request(struct iocb* ioq[], int num);
86  virtual int io_wait(struct timespec* to, int num);
87  virtual int max_io_slot();
88 };
89 
90 typedef void (*callback_t) (io_context_t, struct iocb*[],
91  void *[], long *, long *, int);
92 
93 struct io_callback_s
94 {
95  callback_t func;
96 };
97 
98 }
99 
100 #endif