예제 #1
0
int aio_read_random(char* file_name)
{
	int ret = 0;
	
	int fd = -1;
	fd = ::open(file_name, O_RDONLY);
	fprintf(stdout, "open %s, fd %d\n", file_name, fd);

	int64_t file_length = ::lseek(fd, 0, SEEK_END);
	int piece_num = (file_length + PIECE_LEN - 1)/ PIECE_LEN;
	int bitfield_size = (piece_num + 8 - 1)/8;
	u_int8_t* bitfield = (u_int8_t*)malloc(bitfield_size);
	memset(bitfield, 0, bitfield_size);

	fprintf(stdout, "file_length=%ld, piece_num=%d, bitfield_size=%d\n", file_length, piece_num, bitfield_size);

	io_context_t myctx;
	memset(&myctx, 0, sizeof(myctx));
    io_queue_init(AIO_MAXIO, &myctx);

	while(1)
	{
		// read piece by random
		int piece_index = rand_index(piece_num);
		int piece_pos = bitfield_find_unset(bitfield, piece_num, piece_index);
		if(piece_pos == -1)
		{
			break;
		}
		//printf("read piece=%d\n", piece_pos);
		struct iocb* io2 = (struct iocb*)malloc(sizeof(struct iocb));
		memset(io2, 0, sizeof(struct iocb));

		u_int8_t* buff = NULL;
		posix_memalign((void **)&buff, getpagesize(), PIECE_LEN);
		io_prep_pread(io2, fd, buff, PIECE_LEN, PIECE_LEN*piece_pos);
		io2->data = (void*)piece_pos;
		io_submit(myctx, 1, &io2);

		struct io_event events[AIO_MAXIO];
		int num = io_getevents(myctx, 0, AIO_MAXIO, events, NULL);
	    //printf("io_request completed %d\n", num);
		for(int i=0;i<num;i++)
		{
			struct iocb *objp = events[i].obj;
			int finish_piece_pos = (int)(long)objp->data;
			//printf("done_piece=%d, res=%ld, res2=%ld\n", finish_piece_pos, events[i].res, events[i].res2);
			//cb(myctx, io2, events[i].res, events[i].res2);
			bitfield_set_one(bitfield, piece_num, finish_piece_pos);
			free(objp->u.c.buf);
			free(objp);
		}
		
	}
	
	close(fd);
	fd = -1;
	
	return 0;
}
예제 #2
0
static int64_t
blockdev_aio_writev(struct file_disk *fdisk, struct spdk_io_channel *ch,
		    struct blockdev_aio_task *aio_task,
		    struct iovec *iov, int iovcnt, size_t len, uint64_t offset)
{
	struct iocb *iocb = &aio_task->iocb;
	struct blockdev_aio_io_channel *aio_ch = spdk_io_channel_get_ctx(ch);
	int rc;

	io_prep_pwritev(iocb, fdisk->fd, iov, iovcnt, offset);
	iocb->data = aio_task;
	aio_task->len = len;

	SPDK_TRACELOG(SPDK_TRACE_AIO, "write %d iovs size %lu from off: %#lx\n",
		      iovcnt, len, offset);

	rc = io_submit(aio_ch->io_ctx, 1, &iocb);
	if (rc < 0) {
		spdk_bdev_io_complete(spdk_bdev_io_from_ctx(aio_task), SPDK_BDEV_IO_STATUS_FAILED);
		SPDK_ERRLOG("%s: io_submit returned %d\n", __func__, rc);
		return -1;
	}

	return len;
}
예제 #3
0
파일: aio.c 프로젝트: sameerapadhye/act
static int read_async_from_device(as_async_info_t *info, aio_context_t aio_ctx) 
{
	int fd = fd_get(info->p_readreq.p_device);
	info->fd = fd;
	if (fd == -1) 
	{
		return -1;
	}

	struct iocb *aio_cb = &info->aio_cb;
	aio_read_setup(aio_cb, fd, info);
	int ret = io_submit(aio_ctx, 1 , &aio_cb);
	if(ret != 1)
	{
		fd_put(info->p_readreq.p_device, fd);
		if(ret < 0)
		{
			fprintf(stdout, "Error: io_submit failed \n");
		}
		else 
		{
			fprintf(stdout,"Error: Failed to submit IO \n");
		}
		return -1;
	} 
	return 0;
}
예제 #4
0
//return eventfd
int AIORead(std::string path,void *buf,int epfd=-1)
{
    m_filefd = openFile(path);
    if (-1 == m_filefd) {
        return -1;
    }
    
    m_ctx = 0;
    if (io_setup(8192, &m_ctx)) {
        perror("io_setup");
        return -1;
    }

    if (posix_memalign(&buf, ALIGN_SIZE, RD_WR_SIZE)) {
        perror("posix_memalign");
        return 5;
    }
    printf("buf: %p\n", buf);

    for (i = 0, iocbp = iocbs; i < NUM_EVENTS; ++i, ++iocbp) {
        iocbps[i] = &iocbp->iocb;
        io_prep_pread(&iocbp->iocb, fd, buf, RD_WR_SIZE, i * RD_WR_SIZE);
        io_set_eventfd(&iocbp->iocb, efd);
        io_set_callback(&iocbp->iocb, aio_callback);
        iocbp->nth_request = i + 1;
    }

    if (io_submit(ctx, NUM_EVENTS, iocbps) != NUM_EVENTS) {
        perror("io_submit");
        return 6;
    }
}
예제 #5
0
static int aio_submit(struct iothread *t, http_conn_t h)
{
	struct iocb *iocb;
	size_t data_len;
	off_t data_off;
	int ret, fd;

	data_len = http_conn_data(h, &fd, &data_off);
	assert(data_len);

	iocb = hgang_alloc(aio_iocbs);
	if ( NULL == iocb )
		return 0;

	io_prep_sendfile(iocb, fd, data_len, data_off, http_conn_socket(h));
	iocb->data = h;
	io_set_eventfd(iocb, efd->fd);

	ret = io_submit(aio_ctx, 1, &iocb);
	if ( ret <= 0 ) {
		errno = -ret;
		fprintf(stderr, "io_submit: %s\n", os_err());
		return 0;
	}

	dprintf("io_submit: sendfile: %zu bytes\n", data_len);
	in_flight++;
	return 1;
}
예제 #6
0
파일: usb.c 프로젝트: helaibai/usbip
static void
out_complete (io_context_t ctx, struct iocb *iocb, long res, long res2)
{
	int	status;

	if (verbose > 2)
		fprintf(stderr, "%s uiocb %p status %ld %ld\n",
				__FUNCTION__, iocb,
				res, res2);

	/* fail on all errors we can see.  (short reads MAY mask faults.) */
	if (res < 0)
		goto fail;
	res = empty_out_buf (iocb->u.c.buf, res);
	if (res < 0)
		goto fail;

	/* resubmit */
	status = io_submit (ctx, 1, &iocb);
	if (status == 1)
		return;
	fprintf (stderr, "aio read %p resubmit fail, %d (%s)\n", iocb,
		errno, strerror (errno));
	goto clean;

fail:
	errno = -res;
	fprintf (stderr, "aio read %p fail, %d (%s)\n", iocb,
		errno, strerror (errno));
clean:
	aio_out_pending--;
	return;
}
예제 #7
0
static int swAioLinux_write(int fd, void *inbuf, size_t size, off_t offset)
{
    struct iocb *iocbps[1];
    struct iocb *iocbp = sw_malloc(sizeof(struct iocb));
    if (iocbp == NULL)
    {
        swWarn("malloc failed.");
        return SW_ERR;
    }
    bzero(iocbp, sizeof(struct iocb));

    iocbp->aio_fildes = fd;
    iocbp->aio_lio_opcode = IOCB_CMD_PWRITE;
    iocbp->aio_buf = (__u64 ) inbuf;
    iocbp->aio_offset = offset;
    iocbp->aio_nbytes = size;
    iocbp->aio_flags = IOCB_FLAG_RESFD;
    iocbp->aio_resfd = swoole_aio_eventfd;
    iocbp->aio_reqprio = SwooleAIO.current_id++;
    //iocbp->aio_data = (__u64) aio_callback;
    iocbps[0] = iocbp;

    if (io_submit(swoole_aio_context, 1, iocbps) == 1)
    {
        SwooleAIO.task_num++;
        return iocbp->aio_reqprio;
    }
    swWarn("io_submit failed. Error: %s[%d]", strerror(errno), errno);
    return SW_ERR;
}
예제 #8
0
int
mb_aiom_submit(mb_aiom_t *aiom)
{
    int ret;

    if (aiom->nr_pending == 0) {
        return 0;
    }

    ret = io_submit(aiom->context, aiom->nr_pending, (struct iocb **) aiom->pending);

    if (aio_tracefile != NULL) {
        fprintf(aio_tracefile,
                "[%d] submit: %d req\n",
                tid, ret);
    }

    if (ret != aiom->nr_pending) {
        fprintf(stderr, "fatal error\n");
        exit(EXIT_FAILURE);
    }

    aiom->nr_inflight += aiom->nr_pending;
    aiom->nr_pending = 0;

    return ret;
}
예제 #9
0
int file_submit_or_wait(struct iocb *iocb, sb_file_op_t type, ssize_t len,
                        int thread_id)
{
  sb_aio_oper_t   *oper;
  struct iocb     *iocbp;

  oper = (sb_aio_oper_t *)malloc(sizeof(sb_aio_oper_t));
  if (oper == NULL)
  {
    log_text(LOG_FATAL, "Failed to allocate AIO operation!");
    return 1;
  }

  memcpy(&oper->iocb, iocb, sizeof(*iocb));
  oper->type = type;
  oper->len = len;
  iocbp = &oper->iocb;

  if (io_submit(aio_ctxts[thread_id].io_ctxt, 1, &iocbp) < 1)
  {
    log_errno(LOG_FATAL, "io_submit() failed!");
    return 1;
  }
  
  aio_ctxts[thread_id].nrequests++;
  if (aio_ctxts[thread_id].nrequests < file_async_backlog)
    return 0;
  
  return file_wait(thread_id, 1);
}
예제 #10
0
파일: aio.c 프로젝트: goldwynr/ocfs2-test
int o2test_aio_pread(struct o2test_aio *o2a, int fd, void *buf, size_t count,
		     off_t offset)
{
	int ret = 0;
	struct iocb *iocbs[] = { o2a->o2a_iocbs[o2a->o2a_cr_event] };

	if (o2a->o2a_cr_event >= o2a->o2a_nr_events) {
		fprintf(stderr, "current aio context didn't support %d "
			"requests\n", o2a->o2a_cr_event);
		return -1;
	}

	io_prep_pread(o2a->o2a_iocbs[o2a->o2a_cr_event], fd, buf,
		      count, offset);
	ret = io_submit(o2a->o2a_ctx, 1, iocbs);
	if (ret != 1) {
		ret = errno;
		fprintf(stderr, "error %s during %s\n", strerror(errno),
			"io_submit");
		ret = -1;
	}

	o2a->o2a_cr_event++;

	return ret;
}
예제 #11
0
파일: aiocp.c 프로젝트: noahdesu/dbqos
/*
 * Read complete callback.
 * Change read iocb into a write iocb and start it.
 */
static void rd_done(io_context_t ctx, struct iocb *iocb, long res, long res2)
{
	/* library needs accessors to look at iocb? */
	int iosize = iocb->u.c.nbytes;
	char *buf = iocb->u.c.buf;
	off_t offset = iocb->u.c.offset;

	if (res2 != 0)
		io_error("aio read", res2);
	if (res != iosize) {
		fprintf(stderr, "read missing bytes expect %lu got %ld\n",
			iocb->u.c.nbytes, res);
		exit(1);
	}


	/* turn read into write */
	if (no_write) {
		--tocopy;
		--busy;
		free_iocb(iocb);
	} else {
		io_prep_pwrite(iocb, dstfd, buf, iosize, offset);
		io_set_callback(iocb, wr_done);
		if (1 != (res = io_submit(ctx, 1, &iocb)))
			io_error("io_submit write", res);
	}
	if (debug)
		write(2, "r", 1);
	if (debug > 1)
		printf("%d", iosize);
}
예제 #12
0
static int ioq_submit(struct qemu_laio_state *s)
{
    int ret, i = 0;
    int len = s->io_q.idx;

    do {
        ret = io_submit(s->ctx, len, s->io_q.iocbs);
    } while (i++ < 3 && ret == -EAGAIN);

    /* empty io queue */
    s->io_q.idx = 0;

    if (ret < 0) {
        i = 0;
    } else {
        i = ret;
    }

    for (; i < len; i++) {
        struct qemu_laiocb *laiocb =
            container_of(s->io_q.iocbs[i], struct qemu_laiocb, iocb);

        laiocb->ret = (ret < 0) ? ret : -EIO;
        qemu_laio_process_completion(s, laiocb);
    }
    return ret;
}
예제 #13
0
파일: unix_io.c 프로젝트: djs55/ocfs2-tools
static errcode_t unix_vec_read_blocks(io_channel *channel,
				      struct io_vec_unit *ivus, int count)
{
	int i;
	int ret;
	io_context_t io_ctx;
	struct iocb *iocb = NULL, **iocbs = NULL;
	struct io_event *events = NULL;
	int64_t offset;
	int submitted, completed = 0;

	ret = OCFS2_ET_NO_MEMORY;
	iocb = malloc((sizeof(struct iocb) * count));
	iocbs = malloc((sizeof(struct iocb *) * count));
	events = malloc((sizeof(struct io_event) * count));
	if (!iocb || !iocbs || !events)
		goto out;

	memset(&io_ctx, 0, sizeof(io_ctx));
	ret = io_queue_init(count, &io_ctx);
	if (ret)
		return ret;

	for (i = 0; i < count; ++i) {
		offset = ivus[i].ivu_blkno * channel->io_blksize;
		io_prep_pread(&(iocb[i]), channel->io_fd, ivus[i].ivu_buf,
			      ivus[i].ivu_buflen, offset);
		iocbs[i] = &iocb[i];
	}

resubmit:
	ret = io_submit(io_ctx, count - completed, &iocbs[completed]);
	if (!ret && (count - completed))
		ret = OCFS2_ET_SHORT_READ;
	if (ret < 0)
		goto out;
	submitted = ret;

	ret = io_getevents(io_ctx, submitted, submitted, events, NULL);
	if (ret < 0)
		goto out;

	completed += submitted;
	if (completed < count)
		goto resubmit;

out:
	if (ret >= 0)
		ret = 0;
	if (!ret)
		channel->io_bytes_read += (count * channel->io_blksize);
	free(iocb);
	free(iocbs);
	free(events);
	io_queue_release(io_ctx);

	return ret;
}
예제 #14
0
long test_write(aio_context_t ctx, int fd, long range, int afd) {
	long i, n, r, j;
	u_int64_t eval;
	struct iocb **piocb;
	struct iocb *iocb;
	struct timespec tmo;
	static struct io_event events[NUM_EVENTS];
	static char buf[IORTX_SIZE];

	for (i = 0; i < IORTX_SIZE; i++)
		buf[i] = i & 0xff;
	n = range / IORTX_SIZE;
	iocb = malloc(n * sizeof(struct iocb));
	piocb = malloc(n * sizeof(struct iocb *));
	if (!iocb || !piocb) {
		perror("iocb alloc");
		return -1;
	}
	for (i = 0; i < n; i++) {
		piocb[i] = &iocb[i];
		asyio_prep_pwrite(&iocb[i], fd, buf, sizeof(buf),
				  (n - i - 1) * IORTX_SIZE, afd);
		iocb[i].aio_data = (u_int64_t) i + 1;
	}
	fprintf(stdout, "submitting write request ...\n");
	if (io_submit(ctx, n, piocb) <= 0) {
		perror("io_submit");
		return -1;
	}
	for (i = 0; i < n;) {
		fprintf(stdout, "waiting ... ");
		waitasync(afd, -1);
		eval = 0;
		if (read(afd, &eval, sizeof(eval)) != sizeof(eval))
			perror("read");
		fprintf(stdout, "done! %llu\n", (unsigned long long) eval);
		while (eval > 0) {
			tmo.tv_sec = 0;
			tmo.tv_nsec = 0;
			r = io_getevents(ctx, 1, eval > NUM_EVENTS ? NUM_EVENTS: (long) eval,
					 events, &tmo);
			if (r > 0) {
				for (j = 0; j < r; j++) {

				}
				i += r;
				eval -= r;
				fprintf(stdout, "test_write got %ld/%ld results so far\n",
					i, n);
			}
		}
	}
	free(iocb);
	free(piocb);

	return n;
}
void fun_write1(void *ptr)
{
	int ret;
    
	ret = io_submit(ctxp, MAX_AIO_EVENTS, iocbs);
	if (ret !=  MAX_AIO_EVENTS)
		fail("io_subit returned %d instead of %u\n", ret,
		     MAX_AIO_EVENTS);
}
void fun_writeN(void *ptr)
{
	int i;
	int ret;

	for(i = 0; i < MAX_AIO_EVENTS; ++i) {
		ret = io_submit(ctxp, 1, &(iocbs[i]));
		if (ret != 1)
			fail("io_subit returned %d instead of 1\n", ret);
	}
}
예제 #17
0
파일: aio_read.c 프로젝트: AKdroid/AIO-Test
/* Submits the read IO request */
void SubmitRead(int offset){
	int x;
	struct iocb iocbr;
	struct iocb *iocbrp=&iocbr;
    /* Init iocb structure */
	io_prep_pread(&iocbr,filedes,buffer,BUFFERSIZE,BUFFERSIZE*offset);	
	iocbr.data=(void*)buffer;
	x=io_submit(context,1,&iocbrp);					/* Submit the request */
	if(x<0)
		printf("Error while submitting read request\n %d",x);
}
예제 #18
0
int aio_preadv(io_context_t ctx, struct iocb *iocb, int fd, const struct iovec *iov, int iovcnt,
		off_t offset, int ev, void *param)
{
	struct iocb *ios[1] = { iocb };

	io_prep_preadv(iocb, fd, iov, iovcnt, offset);
	io_set_eventfd(iocb, ev);
	iocb->data = param;

	return io_submit(ctx, 1, ios);
}
예제 #19
0
파일: sbd-md.c 프로젝트: credativ/sbd
static int
sector_io(struct sbd_context *st, int sector, void *data, int rw)
{
	struct timespec	timeout;
	struct io_event event;
	struct iocb	*ios[1] = { &st->io };
	long		r;

	timeout.tv_sec  = timeout_io;
	timeout.tv_nsec = 0;

	memset(&st->io, 0, sizeof(struct iocb));
	if (rw) {
		io_prep_pwrite(&st->io, st->devfd, data, sector_size, sector_size * sector);
	} else {
		io_prep_pread(&st->io, st->devfd, data, sector_size, sector_size * sector);
	}

	if (io_submit(st->ioctx, 1, ios) != 1) {
		cl_log(LOG_ERR, "Failed to submit IO request! (rw=%d)", rw);
		return -1;
	}

	errno = 0;
	r = io_getevents(st->ioctx, 1L, 1L, &event, &timeout);

	if (r < 0 ) {
		cl_log(LOG_ERR, "Failed to retrieve IO events (rw=%d)", rw);
		return -1;
	} else if (r < 1L) {
		cl_log(LOG_INFO, "Cancelling IO request due to timeout (rw=%d)", rw);
		r = io_cancel(st->ioctx, ios[0], &event);
		if (r) {
			DBGLOG(LOG_INFO, "Could not cancel IO request (rw=%d)", rw);
			/* Doesn't really matter, debugging information.
			 */
		}
		return -1;
	} else if (r > 1L) {
		cl_log(LOG_ERR, "More than one IO was returned (r=%ld)", r);
		return -1;
	}

	
	/* IO is happy */
	if (event.res == sector_size) {
		return 0;
	} else {
		cl_log(LOG_ERR, "Short IO (rw=%d, res=%lu, sector_size=%d)",
				rw, event.res, sector_size);
		return -1;
	}
}
예제 #20
0
int aio_read(int fd, void *buffer, size_t length, off_t offset, aio_callback_t cb){
	struct iocb iocb;
        struct iocb *piocb = &iocb;
        int resp;

        io_prep_pread(&iocb, fd, buffer, length, offset);
        iocb.data = cb;
	io_set_eventfd(&iocb, aio_fd);

        resp = io_submit(aio_ctx,1, &piocb);
        return resp;
}
예제 #21
0
파일: _iomodule.c 프로젝트: sh01/gonium
static PyObject *IOManager_submit(IOManager *self, PyObject *args) {
   PyObject *req_s, *iter;
   IORequest *item;
   Py_ssize_t l;
   int rc;
   struct iocb **cb, **cb_l;
   if (!PyArg_ParseTuple(args, "O", &req_s)) return NULL;

   if ((l = PySequence_Size(req_s)) < 0) return NULL;
   if (l > (self->nr_events - self->pending_events)) {
      PyErr_SetString(PyExc_ValueError, "Queue length exceeded.");
      return NULL;
   }
   
   cb = self->cbs;
   cb_l = cb + (self->nr_events - self->pending_events);
   
   if (!(iter = PyObject_GetIter(req_s))) return NULL;
   for (; (item = (IORequest*) PyIter_Next(iter)); cb++) {
      if (!PyObject_IsInstance((PyObject*) item, (PyObject*) &IORequestType)) {
         Py_DECREF(item);
         PyErr_SetString(PyExc_TypeError, "Elements of argument 0 must be of type IORequest.");
         return IOM_iocb_cleanup(self, cb+1);
      }
      if (cb == cb_l) {
         Py_DECREF(item);
         PyErr_SetString(PyExc_ValueError, "Queue length exceeded (secondary check)");
         return IOM_iocb_cleanup(self, cb+1);
      }
      if (item->submitted) {
         Py_DECREF(item);
         PyErr_SetString(PyExc_ValueError, "Element of argument 0 had already been submitted earlier.");
         return IOM_iocb_cleanup(self, cb+1);
      }
      item->submitted = 1;
      item->iocb.u.c.resfd = self->fd;
      *cb = &item->iocb;
   }
   if (PyErr_Occurred()) return IOM_iocb_cleanup(self, cb);
   
   l = cb - self->cbs;
   rc = io_submit(self->ctx, l, self->cbs);
   if (rc < 0) {
      errno = -rc;
      PyErr_SetFromErrno(PyExc_OSError);
      return IOM_iocb_cleanup(self, cb);
   }
   /* Keep one reference to each element read from the iterable, to make sure
      they aren't deallocated while we wait for their IO requests to complete
   */
   self->pending_events += l;
   Py_RETURN_NONE;
}
예제 #22
0
void FlatFileReader::BeginRead(void* pBuffer, uint sector, uint count)
{
	u64 offset;
	offset = sector * (s64)m_blocksize + m_dataoffset;

	u32 bytesToRead = count * m_blocksize;

	struct iocb iocb;
	struct iocb* iocbs = &iocb;

	io_prep_pread(&iocb, m_fd, pBuffer, bytesToRead, offset);
	io_submit(m_aio_context, 1, &iocbs);
}
예제 #23
0
파일: aiocp.c 프로젝트: wsjtangy/loongsso
int file_aio_read(int fd, void *start, int size, long long offset)
{
	struct iocb *cb = malloc(sizeof(struct iocb)); /* remaind to free */
	if(!cb) 
	{
		printf("file_aio_write %s\n", strerror(errno));
		return -1;
	}

	io_prep_pread(cb, fd, start, size, offset);
	io_set_callback(cb, read_done);

	return io_submit(*aio_queue, 1, &cb);
}
예제 #24
0
void random_io(int fd, off_t ionum, int access_size, int num_requests)
{
  // (1) io_context_tの初期化
  io_context_t ctx;
  memset(&ctx, 0, sizeof(io_context_t));
  int r = io_setup(num_requests, &ctx);
  assert(r == 0);

  // (2) iocbs(I/O要求)の構築
  struct iocb **iocbs = new struct iocb*[num_requests];
  char **bufs = new char*[num_requests];
  for (int i = 0; i < num_requests; i++) {
    iocbs[i] = new struct iocb();
    posix_memalign((void **)&bufs[i], 512, access_size);

    off_t block_number = rand() % ionum;
    io_prep_pread(iocbs[i], fd, bufs[i], access_size, block_number * access_size);
    io_set_callback(iocbs[i], read_done);
  }

  // (3) I/O要求を投げる
  r = io_submit(ctx, num_requests, iocbs);
  assert(r == num_requests);

  // (4) 完了したI/O要求を待ち、終わったものについてはcallbackを呼び出す
  int cnt = 0;
  while (true) {
    struct io_event events[32];
    int n = io_getevents(ctx, 1, 32, events, NULL);
    if (n > 0)
      cnt += n;

    for (int i = 0; i < n; i++) {
      struct io_event *ev = events + i;
      io_callback_t callback = (io_callback_t)ev->data;
      struct iocb *iocb = ev->obj;
      callback(ctx, iocb, ev->res, ev->res2);
    }

    if (n == 0 || cnt == num_requests)
      break;
  }

  for (int i = 0; i < num_requests; i++) {
    delete iocbs[i];
    free(bufs[i]);
  }
  delete[] iocbs;
  delete[] bufs;
}
예제 #25
0
파일: aiobe.c 프로젝트: jopereira/holeycow
static void aiobe_pwrite(struct device* dev, void* buf, size_t size, off64_t offset, dev_callback_t cb, void* cookie) {
	int ret;
	struct aiobe_request* req=(struct aiobe_request*)malloc(sizeof(struct aiobe_request));
	struct iocb* iocb = &req->iocb;
	memset(req, 0, sizeof(*req));

	io_prep_pwrite(iocb, D(dev)->fd, buf, size, offset);
	req->cb=cb;
	req->cookie=cookie;
	iocb->data = req;

	ret=io_submit(D(dev)->ctx, 1, &iocb);
	if (ret!=1)
		aiobe_complete(req, -1);
}
예제 #26
0
파일: ioping.c 프로젝트: yksuh/ioping
static ssize_t aio_pwrite(int fd, void *buf, size_t nbytes, off_t offset)
{
    aio_cb.aio_lio_opcode = IOCB_CMD_PWRITE;
    aio_cb.aio_fildes = fd;
    aio_cb.aio_buf = (unsigned long) buf;
    aio_cb.aio_nbytes = nbytes;
    aio_cb.aio_offset = offset;

    if (io_submit(aio_ctx, 1, &aio_cbp) != 1)
        err(1, "aio submit failed");

    if (io_getevents(aio_ctx, 1, 1, &aio_ev, NULL) != 1)
        err(1, "aio getevents failed");

    if (aio_ev.res < 0) {
        errno = -aio_ev.res;
        return -1;
    }

    if (!cached && fdatasync(fd) < 0)
        return -1;

    return aio_ev.res;

#if 0
    aio_cb.aio_lio_opcode = IOCB_CMD_FDSYNC;
    if (io_submit(aio_ctx, 1, &aio_cbp) != 1)
        err(1, "aio fdsync submit failed");

    if (io_getevents(aio_ctx, 1, 1, &aio_ev, NULL) != 1)
        err(1, "aio getevents failed");

    if (aio_ev.res < 0)
        return aio_ev.res;
#endif
}
예제 #27
0
파일: AsyncIO.cpp 프로젝트: JasonCC/folly
void AsyncIO::submit(Op* op) {
  CHECK_EQ(op->state(), Op::State::INITIALIZED);
  CHECK_LT(pending_, capacity_) << "too many pending requests";
  initializeContext();  // on demand
  iocb* cb = &op->iocb_;
  cb->data = nullptr;  // unused
  if (pollFd_ != -1) {
    io_set_eventfd(cb, pollFd_);
  }
  int rc = io_submit(ctx_, 1, &cb);
  checkKernelError(rc, "AsyncIO: io_submit failed");
  DCHECK_EQ(rc, 1);
  op->start();
  ++pending_;
}
예제 #28
0
파일: eventfd01.c 프로젝트: Nan619/ltp
/*
 * Test whether counter overflow is detected and handled correctly.
 *
 * It is not possible to directly overflow the counter using the
 * write() syscall. Overflows occur when the counter is incremented
 * from kernel space, in an irq context, when it is not possible to
 * block the calling thread of execution.
 *
 * The AIO subsystem internally uses eventfd mechanism for
 * notification of completion of read or write requests. In this test
 * we trigger a counter overflow, by setting the counter value to the
 * max possible value initially. When the AIO subsystem notifies
 * through the eventfd counter, the counter overflows.
 *
 * NOTE: If the the counter starts from an initial value of 0, it will
 * take decades for an overflow to occur. But since we set the initial
 * value to the max possible counter value, we are able to cause it to
 * overflow with a single increment.
 *
 * When the counter overflows, the following are tested
 *   1. Check whether POLLERR event occurs in poll() for the eventfd.
 *   2. Check whether readfd_set/writefd_set is set in select() for the
        eventfd.
 *   3. The counter value is UINT64_MAX.
 */
static int trigger_eventfd_overflow(int evfd, int *fd, io_context_t * ctx)
{
    int ret;
    struct iocb iocb;
    struct iocb *iocbap[1];
    static char buf[4 * 1024];

    *ctx = 0;
    ret = io_setup(16, ctx);
    if (ret < 0) {
        errno = -ret;
        tst_resm(TINFO | TERRNO, "io_setup error");
        return -1;
    }

    *fd = open("testfile", O_RDWR | O_CREAT, 0644);
    if (*fd == -1) {
        tst_resm(TINFO | TERRNO, "open(testfile) failed");
        goto err_io_destroy;
    }

    ret = set_counter(evfd, UINT64_MAX - 1);
    if (ret == -1) {
        tst_resm(TINFO, "error setting counter to UINT64_MAX-1");
        goto err_close_file;
    }

    io_prep_pwrite(&iocb, *fd, buf, sizeof(buf), 0);
    io_set_eventfd(&iocb, evfd);

    iocbap[0] = &iocb;
    ret = io_submit(*ctx, 1, iocbap);
    if (ret < 0) {
        errno = -ret;
        tst_resm(TINFO | TERRNO, "error submitting iocb");
        goto err_close_file;
    }

    return 0;

err_close_file:
    close(*fd);

err_io_destroy:
    io_destroy(*ctx);

    return -1;
}
static inline short submit(JNIEnv * env, struct io_control * theControl, struct iocb * iocb) {
    int result = io_submit(theControl->ioContext, 1, &iocb);

    if (result < 0) {
        // Putting the Global Ref and IOCB back in case of a failure
        if (iocb->data != NULL && iocb->data != (void *) -1) {
            (*env)->DeleteGlobalRef(env, (jobject)iocb->data);
        }
        putIOCB(theControl, iocb);

        throwIOExceptionErrorNo(env, "Error while submitting IO: ", -result);
        return 0;
    }

    return 1;
}
예제 #30
0
void submitReads(aio_context_t ctx,int fd,int blockSize,s64 fileSize,int count) {
	iocb cbs[count];
	iocb* cbs_[count];
	memset(cbs,0,sizeof(iocb)*count);
	for(int i=0;i<count;i++) {
		cbs_[i]=cbs+i;
		s64 offset=randRange(0,fileSize/blockSize)*blockSize;
		cbs[i].aio_lio_opcode=IOCB_CMD_PREAD;
		cbs[i].aio_fildes=fd;
		cbs[i].aio_buf=(u64)bufferPool[--bufferPoolLength];
		cbs[i].aio_nbytes=blockSize;
		cbs[i].aio_offset=offset;
		cbs[i].aio_data=cbs[i].aio_buf;
		//printf("fd=%i buf=%p offset=%lli nbytes=%i\n",fd,(void*)cbs[i].aio_buf,offset,blockSize);
	}
	checkError(io_submit(ctx,count,cbs_));
}