隨著雲計算、大數據等新興技術的快速發展,存儲技術也不斷更新迭代。NVMe(Non-Volatile Memory Express)是一種在PCIe匯流排上運行的新型存儲介面協議,相比傳統的SATA、SAS等存儲介面協議,NVMe在CPU與存儲介質之間加入了更快的通訊通道,能夠提供更高的存儲性能。而Ubuntunvme,則是基於Linux內核中NVMe驅動所提供的一種針對NVMe存儲器的通用文件系統。本文將從以下幾個方面對Ubuntunvme進行詳細的闡述。
一、系統架構
Ubuntunvme採用了NVMe標準介面協議,並嚴格遵循離線編程模式,包括NVMe命令集、NVMe隊列以及門鈴模式。由於NVMe所支持的CMD和Doorbell已經向上提供了共享內存,NVMe設備直接映射共享內存來實現命令環和設備環之間的通訊。而在NVMe驅動內核上層,Ubuntunvme使用了通用文件系統,並支持兩個主要函數:inode和dentry。
static const struct address_space_operations nvme_dax_aops = {
.fault = dax_noflush_fault,
.writepage = dax_writeback_mapping_range,
.direct_io = nvme_dax_direct_access,
};
在這段代碼中,可以看到Ubuntunvme使用了通用文件系統,並定義了dax_aops用於操作address_space,同時設置了三個操作:fault、writepage和direct_io。這些操作函數都是NVMe所支持的。
二、特點和應用
Ubuntunvme與傳統文件系統最大的不同,就是由NVMe命令集指定的直接讀寫,而不需要經過系統緩存區。這種基於直接讀寫的存儲方式,可以大大提高系統I/O性能,且具備以下特點:
1、高性能
由於Ubuntunvme能夠快速完成CMD和Doorbell之間的通訊,且不需要經過系統緩存區,因此能夠大幅提升系統I/O性能。這對於需要大量數據讀寫操作的應用場景,如大數據、機器學習等領域具有重要意義。
2、支持大容量存儲設備
相比傳統的存儲協議,NVMe協議支持更大的LBA定址空間,能夠支持16TB以上的存儲設備。而Ubuntunvme也能夠完美支持這些大容量存儲設備,並保持較高的性能。
3、較低的主機CPU和內存負載
由於Ubuntunvme是直接操作存儲設備的文件系統,因此相比隨系統載入的傳統文件系統,其啟動時無需佔用大量的內存空間,大大降低了系統的CPU和內存負載。
三、示例代碼
以下是一個基於Ubuntunvme的示常式序,用於讀寫NVMe設備。示例代碼採用C語言編寫,並使用了DPDK庫函數。
#include <rte_mbuf.h>
#include <rte_ethdev.h>
#include <rte_distributor.h>
#include <rte_ring.h>
#include <rte_malloc.h>
#include <rte_eal.h>
#define NUM_MBUFS 8191
#define MBUF_CACHE_SIZE 250
#define RX_RING_SIZE 1024
#define TX_RING_SIZE 1024
#define BURST_SIZE 32
static const struct rte_eth_conf port_conf_default = {
.rxmode = {
.max_rx_pkt_len = ETHER_MAX_LEN,
},
};
static struct rte_eth_dev_tx_buffer *buffer;
struct rte_mempool *mbuf_pool;
static inline uint16_t
add_bonded_device(const char *devargs, uint8_t port_id,
uint32_t numa_node)
{
struct rte_eth_conf port_conf = port_conf_default;
struct rte_eth_dev_info dev_info;
struct rte_eth_link link;
uint16_t nb_queues = rte_eth_dev_count_avail();
if (!nb_queues) {
RTE_LOG(ERR, APP, "No ethdev ports available\n");
return 0;
}
int ret = rte_eth_dev_configure(port_id, nb_queues, nb_queues, &port_conf);
if (ret < 0) {
RTE_LOG(ERR, APP,
"Cannot configure device: "
"err=%d, port=%d\n",
ret, port_id);
return 0;
}
rte_eth_dev_info_get(port_id, &dev_info);
rte_eth_link_get(port_id, &link);
struct rte_eth_rxconf rx_conf;
struct rte_eth_txconf tx_conf;
rx_conf = dev_info.default_rxconf;
tx_conf = dev_info.default_txconf;
tx_conf.txq_flags = ETH_TXQ_FLAGS_IGNORE;
uint16_t q;
for (q = 0; q < nb_queues; q++) {
ret = rte_eth_rx_queue_setup(port_id, q, RX_RING_SIZE,
numa_node, &rx_conf, mbuf_pool);
if (ret < 0) {
RTE_LOG(ERR, APP, "Cannot allocate rx queue: err=%d,"
" port=%d\n", ret, port_id);
return 0;
}
ret = rte_eth_tx_queue_setup(port_id, q, TX_RING_SIZE,
numa_node, &tx_conf);
if (ret < 0) {
RTE_LOG(ERR, APP, "Cannot allocate tx queue: err=%d,"
" port=%d\n", ret, port_id);
return 0;
}
buffer = rte_zmalloc_socket(NULL,
RTE_ETH_TX_BUFFER_SIZE(BURST_SIZE), 0,
rte_eth_dev_socket_id(port_id));
if (buffer == NULL) {
RTE_LOG(ERR, APP,
"Cannot allocate buffer for tx on port %u\n",
port_id);
return 0;
}
ret = rte_eth_tx_buffer_init(buffer, BURST_SIZE);
if (ret = 0, "Cannot init EAL\n");
}
int main(int argc, char **argv)
{
dpdk_init();
mbuf_pool = rte_pktmbuf_pool_create("MBUF_POOL",
NUM_MBUFS * rte_eth_dev_count_avail(),
MBUF_CACHE_SIZE, 0, RTE_MBUF_DEFAULT_BUF_SIZE, rte_socket_id());
uint8_t port_id = 0;
uint32_t numa_node = rte_socket_id();
int nb_queues = add_bonded_device(NULL, port_id, numa_node);
if (!nb_queues) {
RTE_LOG(ERR, APP, "Cannot create bonded device\n");
return 0;
}
printf("Hello, Ubuntunvme!\n");
return 0;
}
這段代碼主要用於創建DPDK虛擬網路設備並進行綁定,然後使用Ubuntunvme進行NVMe設備上的數據讀寫操作。其中,rte_eal_init用於初始化DPDK環境,rte_pktmbuf_pool_create用於創建DPDK內存池,add_bonded_device用於創建虛擬網路設備。這些操作都是Ubuntunvme的API,能夠方便地對NVMe設備進行操作。
原創文章,作者:UPEMF,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/368946.html