Skip to content

Commit

Permalink
Revised Python wrapper and misc changes.
Browse files Browse the repository at this point in the history
  • Loading branch information
Deyoung Hong committed Dec 29, 2017
1 parent d8a4de2 commit d4b66a4
Show file tree
Hide file tree
Showing 16 changed files with 185 additions and 145 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
*.so
*.pyc
*.*~
src/.model
src/unvmed
test/unvme/*_test
test/unvme/unvme_info
Expand Down
7 changes: 2 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,8 @@ uninstall:
$(INSTALLDIR)/lib/libunvme* \
$(INSTALLDIR)/bin/unvme*

lint:
@(for d in $(SUBDIRS); do $(MAKE) -C $$d lint; done)

clean:
@(for d in $(SUBDIRS); do $(MAKE) -C $$d clean; done)
clean lint:
@(for d in $(SUBDIRS); do $(MAKE) -C $$d $@; done)

.PHONY: all install uninstall lint clean $(SUBDIRS)

4 changes: 2 additions & 2 deletions ioengine/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,15 @@ UNVME_SRC = ../src

CPPFLAGS += -I$(UNVME_SRC) -I$(FIODIR)
LDFLAGS += -shared -rdynamic
LDLIBS += -lpthread -lrt
LDLIBS += -lrt

OBJS = $(addsuffix .o, $(TARGET))

all: $(TARGET)

$(TARGET): $(UNVME_SRC)/libunvme.a

lint: CFLAGS = -Wall -D_FORTIFY_SOURCE=2 -DUNVME_DEBUG -O3
lint: CFLAGS = -Wall -O3 -D_FORTIFY_SOURCE=2 -DUNVME_DEBUG
lint: clean $(OBJS)
@$(RM) *.o

Expand Down
48 changes: 27 additions & 21 deletions ioengine/unvme_fio.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@
#include "fio.h"
#include "optgroup.h" // since fio 2.4


#define TDEBUG(fmt, arg...) //fprintf(stderr, "#%s.%d " fmt "\n", __func__, td->thread_number, ##arg)
#define FATAL(fmt, arg...) do { warnx(fmt, ##arg); abort(); } while (0)


/// Context used for thread initialization
typedef struct {
pthread_mutex_t mutex;
Expand Down Expand Up @@ -42,7 +44,7 @@ static void do_unvme_cleanup(void)
/*
* Initialize UNVMe once.
*/
static int do_unvme_init(struct thread_data *td)
static void do_unvme_init(struct thread_data *td)
{
TDEBUG("device=%s numjobs=%d iodepth=%d", td->o.filename, td->o.numjobs, td->o.iodepth);

Expand Down Expand Up @@ -74,25 +76,6 @@ static int do_unvme_init(struct thread_data *td)
}

pthread_mutex_unlock(&unvme.mutex);
return 0;
}

/*
* The ->get_file_size() is called once for every job (i.e. numjobs)
* before all other functions. This is called after ->setup() but
* is simpler to initialize here since we only care about the device name
* (given as file_name) and just have to specify the device size.
*/
static int fio_unvme_get_file_size(struct thread_data *td, struct fio_file *f)
{
TDEBUG("file=%s", f->file_name);
if (!fio_file_size_known(f)) {
do_unvme_init(td);
f->filetype = FIO_TYPE_CHAR;
f->real_file_size = unvme.ns->blockcount * unvme.ns->blocksize;
fio_file_set_size_known(f);
}
return 0;
}

/*
Expand Down Expand Up @@ -208,6 +191,9 @@ static int fio_unvme_getevents(struct thread_data *td, unsigned int min,
return 0;
}


#ifndef _UNVME_MMCC_H

/*
* The ->queue() hook is responsible for initiating io on the io_u
* being passed in. If the io engine is a synchronous one, io may complete
Expand All @@ -220,10 +206,10 @@ static int fio_unvme_getevents(struct thread_data *td, unsigned int min,
*/
static int fio_unvme_queue(struct thread_data *td, struct io_u *io_u)
{
int q = td->thread_number - 1;
void* buf = io_u->buf;
u64 slba = io_u->offset >> unvme.ns->blockshift;
int nlb = io_u->xfer_buflen >> unvme.ns->blockshift;
int q = td->thread_number - 1;

switch (io_u->ddir) {
case DDIR_READ:
Expand All @@ -247,6 +233,24 @@ static int fio_unvme_queue(struct thread_data *td, struct io_u *io_u)
return FIO_Q_COMPLETED;
}

/*
* The ->get_file_size() is called once for every job (i.e. numjobs)
* before all other functions. This is called after ->setup() but
* is simpler to initialize here since we only care about the device name
* (given as file_name) and just have to specify the device size.
*/
static int fio_unvme_get_file_size(struct thread_data *td, struct fio_file *f)
{
TDEBUG("file=%s", f->file_name);
if (!fio_file_size_known(f)) {
do_unvme_init(td);
f->filetype = FIO_TYPE_CHAR;
f->real_file_size = unvme.ns->blockcount * unvme.ns->blocksize;
fio_file_set_size_known(f);
}
return 0;
}


// Note that the structure is exported, so that fio can get it via
// dlsym(..., "ioengine");
Expand All @@ -266,3 +270,5 @@ struct ioengine_ops ioengine = {
.flags = FIO_NOEXTEND | FIO_RAWIO,
};

#endif // _UNVME_MMCC_H

12 changes: 6 additions & 6 deletions src/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -31,28 +31,28 @@

include ../Makefile.def

TARGET = libunvme.a
TARGET_SO = libunvme.so
TARGET_LIB = libunvme.a
TARGET_LIBSO = libunvme.so

INCS = $(wildcard *.h)
SRCS = $(wildcard *.c)
OBJS = $(SRCS:.c=.o)
LDLIBS = -lrt

all: $(TARGET) $(TARGET_SO)
all: $(TARGET_LIB) $(TARGET_LIBSO)

$(OBJS): $(INCS)

$(TARGET): $(OBJS)
$(TARGET_LIB): $(OBJS)
$(AR) crs $@ $^

$(TARGET_SO): $(OBJS)
$(TARGET_LIBSO): $(OBJS)
$(CC) -shared -rdynamic -o $@ $^ -lrt

%.i: %.c
$(CPP) $(CPPFLAGS) -o $@ $<

lint: CFLAGS = -Wall -D_FORTIFY_SOURCE=2 -DUNVME_DEBUG -O3
lint: CFLAGS = -Wall -O3 -D_FORTIFY_SOURCE=2 -DUNVME_DEBUG
lint: clean $(OBJS)
@$(RM) *.o

Expand Down
13 changes: 9 additions & 4 deletions src/rdtsc.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ static inline uint64_t rdtsc(void)
*/
static inline uint64_t rdtsc_elapse(uint64_t tsc) {
int64_t et;

do {
et = rdtsc() - tsc;
} while (et <= 0);
Expand All @@ -79,9 +78,15 @@ static inline uint64_t rdtsc_second()
{
static uint64_t tsc_ps = 0;
if (!tsc_ps) {
uint64_t tsc = rdtsc();
usleep(10000);
tsc_ps = (rdtsc() - tsc) * 100;
uint64_t t0 = rdtsc();
usleep(1000);
uint64_t t1 = rdtsc();
usleep(1000);
uint64_t t2 = rdtsc();
t2 -= t1;
t1 -= t0;
if (t2 > t1) t2 = t1;
tsc_ps = t2 * 1000;
}
return tsc_ps;
}
Expand Down
34 changes: 19 additions & 15 deletions src/unvme_nvme.c
Original file line number Diff line number Diff line change
Expand Up @@ -174,10 +174,10 @@ int nvme_check_completion(nvme_queue_t* q, int* stat, u32* cqe_cs)
#endif

if (*stat == 0) {
DEBUG_FN("q=%d h=%d cid=%#x (C)", q->id, q->cq_head, cqe->cid);
DEBUG_FN("q=%d cq=%d sq=%d-%d cid=%#x (C)", q->id, q->cq_head, q->sq_head, q->sq_tail, cqe->cid);
} else {
ERROR("q=%d cid=%#x stat=%#x (dnr=%d m=%d sct=%d sc=%#x) (C)",
q->id, cqe->cid, *stat, cqe->dnr, cqe->m, cqe->sct, cqe->sc);
ERROR("q=%d cq=%d sq=%d-%d cid=%#x stat=%#x (dnr=%d m=%d sct=%d sc=%#x) (C)",
q->id, q->cq_head, q->sq_head, q->sq_tail, cqe->cid, *stat, cqe->dnr, cqe->m, cqe->sct, cqe->sc);
}

return cqe->cid;
Expand Down Expand Up @@ -236,7 +236,7 @@ int nvme_acmd_identify(nvme_device_t* dev, int nsid, u64 prp1, u64 prp2)
cmd->common.prp2 = prp2;
cmd->cns = nsid == 0 ? 1 : 0;

DEBUG_FN("t=%d h=%d cid=%#x nsid=%d", adminq->sq_tail, adminq->sq_head, cid, nsid);
DEBUG_FN("sq=%d-%d cid=%#x nsid=%d", adminq->sq_head, adminq->sq_tail, cid, nsid);
int err = nvme_submit_cmd(adminq);
if (!err) err = nvme_wait_completion(adminq, cid, 30);
return err;
Expand Down Expand Up @@ -269,7 +269,7 @@ int nvme_acmd_get_log_page(nvme_device_t* dev, int nsid,
cmd->lid = lid;
cmd->numd = numd;

DEBUG_FN("t=%d h=%d cid=%#x lid=%d", adminq->sq_tail, adminq->sq_head, cid, lid);
DEBUG_FN("sq=%d-%d cid=%#x lid=%d", adminq->sq_head, adminq->sq_tail, cid, lid);
int err = nvme_submit_cmd(adminq);
if (!err) err = nvme_wait_completion(adminq, cid, 30);
return err;
Expand Down Expand Up @@ -302,7 +302,7 @@ int nvme_acmd_get_features(nvme_device_t* dev, int nsid,
cmd->fid = fid;
*res = -1;

DEBUG_FN("t=%d h=%d cid=%#x fid=%d", adminq->sq_tail, adminq->sq_head, cid, fid);
DEBUG_FN("sq=%d-%d cid=%#x fid=%d", adminq->sq_head, adminq->sq_tail, cid, fid);
int err = nvme_submit_cmd(adminq);
if (!err) err = nvme_wait_completion(adminq, cid, 30);
if (!err) *res = adminq->cq[cid].cs;
Expand Down Expand Up @@ -365,7 +365,7 @@ int nvme_acmd_create_cq(nvme_queue_t* ioq, u64 prp)
cmd->qid = ioq->id;
cmd->qsize = ioq->size - 1;

DEBUG_FN("t=%d h=%d cid=%#x cq=%d qs=%d", adminq->sq_tail, adminq->sq_head, cid, ioq->id, ioq->size);
DEBUG_FN("sq=%d-%d cid=%#x cq=%d qs=%d", adminq->sq_head, adminq->sq_tail, cid, ioq->id, ioq->size);
int err = nvme_submit_cmd(adminq);
if (!err) err = nvme_wait_completion(adminq, cid, 30);
return err;
Expand Down Expand Up @@ -394,7 +394,7 @@ int nvme_acmd_create_sq(nvme_queue_t* ioq, u64 prp)
cmd->cqid = ioq->id;
cmd->qsize = ioq->size - 1;

DEBUG_FN("t=%d h=%d cid=%#x sq=%d qs=%d", adminq->sq_tail, adminq->sq_head, cid, ioq->id, ioq->size);
DEBUG_FN("sq=%d-%d cid=%#x cq=%d qs=%d", adminq->sq_head, adminq->sq_tail, cid, ioq->id, ioq->size);
int err = nvme_submit_cmd(adminq);
if (!err) err = nvme_wait_completion(adminq, cid, 30);
return err;
Expand All @@ -418,7 +418,7 @@ static inline int nvme_acmd_delete_ioq(nvme_queue_t* ioq, int opc)
cmd->common.cid = cid;
cmd->qid = ioq->id;

DEBUG_FN("t=%d h=%d cid=%#x %cq=%d", adminq->sq_tail, adminq->sq_head, cid,
DEBUG_FN("sq=%d-%d cid=%#x %cq=%d", adminq->sq_head, adminq->sq_tail, cid,
opc == NVME_ACMD_DELETE_CQ ? 'c' : 's', ioq->id);
int err = nvme_submit_cmd(adminq);
if (!err) err = nvme_wait_completion(adminq, cid, 30);
Expand Down Expand Up @@ -470,8 +470,8 @@ int nvme_cmd_vs(nvme_queue_t* q, int opc, u16 cid, int nsid,
cmd->common.prp1 = prp1;
cmd->common.prp2 = prp2;
if (cdw10_15) memcpy(cmd->cdw10_15, cdw10_15, sizeof(cmd->cdw10_15));
DEBUG_FN("q=%d t=%d h=%d cid=%#x nsid=%d opc=%#x",
q->id, q->sq_tail, q->sq_head, cid, nsid, opc);
DEBUG_FN("q=%d sq=%d-%d cid=%#x nsid=%d opc=%#x",
q->id, q->sq_head, q->sq_tail, cid, nsid, opc);
return nvme_submit_cmd(q);
}

Expand Down Expand Up @@ -500,8 +500,8 @@ int nvme_cmd_rw(nvme_queue_t* ioq, int opc, u16 cid, int nsid,
cmd->common.prp2 = prp2;
cmd->slba = slba;
cmd->nlb = nlb - 1;
DEBUG_FN("q=%d t=%d h=%d cid=%#x nsid=%d lba=%#lx nb=%#x prp=%#lx.%#lx (%c)",
ioq->id, ioq->sq_tail, ioq->sq_head, cid, nsid, slba, nlb, prp1, prp2,
DEBUG_FN("q=%d sq=%d-%d cid=%#x nsid=%d lba=%#lx nb=%#x prp=%#lx.%#lx (%c)",
ioq->id, ioq->sq_head, ioq->sq_tail, cid, nsid, slba, nlb, prp1, prp2,
opc == NVME_CMD_READ? 'R' : 'W');
return nvme_submit_cmd(ioq);
}
Expand Down Expand Up @@ -662,8 +662,12 @@ nvme_device_t* nvme_create(nvme_device_t* dev, int mapfd)
dev->maxqsize = cap.mqes + 1;
dev->dbstride = 1 << cap.dstrd; // in u32 size offset

DEBUG_FN("cap=%#lx mps=%u-%u to=%u maxqs=%u dbs=%u", cap.val,
cap.mpsmin, cap.mpsmax, cap.to, dev->maxqsize, dev->dbstride);
dev->cmbloc.val = r32(dev, &dev->reg->cmbloc.val);
dev->cmbsz.val = r32(dev, &dev->reg->cmbsz.val);

DEBUG_FN("cap=%#lx mps=%u-%u to=%u maxqs=%u dbs=%u cmbloc=%#x cmbsz=%#x",
cap.val, cap.mpsmin, cap.mpsmax, cap.to, dev->maxqsize,
dev->dbstride, dev->cmbloc.val, dev->cmbsz.val);

return dev;
}
Expand Down
32 changes: 30 additions & 2 deletions src/unvme_nvme.h
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,31 @@ typedef union _nvme_controller_status {
};
} nvme_controller_status_t;

/// Controller memory buffer location register
typedef union _nvme_cmbloc {
u32 val; ///< whole value
struct {
u32 bir : 3; ///< base indicator register
u32 rsvd : 9; ///< reserved
u32 ofst : 20; ///< offset (in cmbsz units)
};
} nvme_cmbloc_t;

/// Controller memory buffer size register
typedef union _nvme_cmbsz {
u32 val; ///< whole value
struct {
u32 sqs : 1; ///< submission queue support
u32 cqs : 1; ///< completion queue support
u32 lists : 1; ///< PRP SGL list support
u32 rds : 1; ///< read data support
u32 wds : 1; ///< write data support
u32 rsvd : 3; ///< reserved
u32 szu : 4; ///< size units (0=4K,1=64K,2=1M,3=16M,4=256M,5=4G,6=64G)
u32 sz : 20; ///< size (in cmbsz units)
};
} nvme_cmbsz_t;

/// Controller register (bar 0)
typedef struct _nvme_controller_reg {
nvme_controller_cap_t cap; ///< controller capabilities
Expand All @@ -177,8 +202,8 @@ typedef struct _nvme_controller_reg {
nvme_adminq_attr_t aqa; ///< admin queue attributes
u64 asq; ///< admin submission queue base address
u64 acq; ///< admin completion queue base address
u32 cmbloc; ///< controller memory buffer location
u32 cmbsz; ///< controller memory buffer size
nvme_cmbloc_t cmbloc; ///< controller memory buffer location
nvme_cmbsz_t cmbsz; ///< controller memory buffer size
u32 rcss[1008]; ///< reserved and command set specific
u32 sq0tdbl[1024]; ///< sq0 tail doorbell at 0x1000
} nvme_controller_reg_t;
Expand Down Expand Up @@ -545,6 +570,8 @@ typedef struct _nvme_queue {
typedef struct _nvme_device {
nvme_controller_reg_t* reg; ///< register address map
nvme_queue_t adminq; ///< admin queue reference
nvme_cmbloc_t cmbloc; ///< CMB location
nvme_cmbsz_t cmbsz; ///< CMB size
u64 rdtsec; ///< rdtsc per second
u32 maxqcount; ///< max queue count
u32 maxqsize; ///< max queue size
Expand Down Expand Up @@ -583,3 +610,4 @@ int nvme_check_completion(nvme_queue_t* q, int* stat, u32* cqe_cs);
int nvme_wait_completion(nvme_queue_t* q, int cid, int timeout);

#endif // _UNVME_NVME_H

4 changes: 2 additions & 2 deletions test/nvme/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,15 @@ TARGETS = nvme_identify nvme_get_log_page nvme_get_features nvme_set_features
UNVME_SRC = ../../src

CPPFLAGS += -I$(UNVME_SRC)
LDLIBS += $(UNVME_SRC)/libunvme.a -lrt -lpthread
LDLIBS += -lrt

OBJS = $(addsuffix .o, $(TARGETS))

all: $(TARGETS)

$(TARGETS): $(UNVME_SRC)/libunvme.a

lint: CFLAGS = -Wall -D_FORTIFY_SOURCE=2 -DUNVME_DEBUG -O3
lint: CFLAGS = -Wall -O3 -D_FORTIFY_SOURCE=2 -DUNVME_DEBUG
lint: clean $(OBJS)
@$(RM) *.o

Expand Down
Loading

1 comment on commit d4b66a4

@1010fox
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello, RTSYork.
I am very depressed that I cannot successfully use your app.Can you provide a detailed tutorial about petalinux and unvme-zynq?My mailbox is 421527213@qq.com.Waiting for your reply, V...thank you!

Please sign in to comment.