mempool + ring test

#include <stdio.h>
#include <unistd.h>
#include <rte_memory.h>
#include <rte_ring.h>
#include <rte_mempool.h>

#define RING_SIZE            64
static const char *_MSG_POOL = "MSG_POOL";
struct rte_mempool *message_pool;
struct lcore_params {
    struct rte_ring *send_ring, *recv_ring;
};

struct data {
    uint32_t value;
};
struct private_data {
    uint32_t value;
};

static int
lcore_recv(struct lcore_params *p)
{
    unsigned lcore_id = rte_lcore_id();
    printf("Starting core %u
", lcore_id);
    void * vp;
    while (rte_ring_dequeue(p->send_ring, &vp) != 0){
        usleep(5);
    }
    struct data * d = (struct data *) vp;
        struct private_data * priv = (struct private_data *)rte_mempool_get_priv(message_pool);
    printf("core %u: Received %d and private data %u
", lcore_id, d->value, priv->value);
    d->value ++;
    rte_ring_enqueue(p->recv_ring, (void *)d);

    return 0;
}

static void
enqueue_sample_data(struct rte_ring * ring)
{
    int i;
    uint32_t values[4] = {1, 3, 5, 8};
    for (i = 0; i < 4; ++i) {
        void *p  = NULL;
        printf("mempool count before get: %u
", rte_mempool_avail_count(message_pool));
        if (rte_mempool_get(message_pool, &p) < 0)
                rte_panic("Failed to get message buffer
");
        printf("mempool count after get: %u
", rte_mempool_avail_count(message_pool));
        struct data * msg = (struct data *)p;
        msg->value = values[i];
        if (rte_ring_enqueue(ring, msg) < 0) {
                printf("Failed to send message - message discarded
");
                rte_mempool_put(message_pool, p);
        }
    }
    struct private_data * priv = (struct private_data *)rte_mempool_get_priv(message_pool);
    priv->value =1000;

}



static void
print_ring(struct rte_ring * ring)
{
    struct data * d;
    while (rte_ring_dequeue(ring, (void *)&d) == 0) {
        printf("DEQ-DATA:%d
", d->value);
        //free(d);
    }
}

int
main(int argc, char **argv)
{
    int ret;
    unsigned lcore_id;
        //const unsigned pool_size = sizeof(struct data) * 4;
        const unsigned pool_size = 64;
        const unsigned pool_cache = 32;
        const unsigned priv_data_sz = sizeof(struct private_data);
    ret = rte_eal_init(argc, argv);
    if (ret < 0)
        rte_exit(EXIT_FAILURE, "Cannot init EAL
");

    struct lcore_params params;

    params.send_ring = rte_ring_create("R1", RING_SIZE, SOCKET_ID_ANY, 0/*RING_F_SP_ENQ*/);
    if (!params.send_ring) {
        rte_exit(EXIT_FAILURE, "Problem getting sending ring
");
    }


    params.recv_ring = rte_ring_create("R2", RING_SIZE, SOCKET_ID_ANY, 0/*RING_F_SC_DEQ*/);
    if (!params.recv_ring) {
        rte_exit(EXIT_FAILURE, "Problem getting receiving ring
");
    }

    message_pool = rte_mempool_create(_MSG_POOL, pool_size,
                                sizeof(struct data), pool_cache, priv_data_sz,
                                NULL, NULL, NULL, NULL,
                                rte_socket_id(), 0);
         if (message_pool == NULL)
                rte_exit(EXIT_FAILURE, "Problem getting message pool
");
    printf("mempool count : %u
", rte_mempool_avail_count(message_pool));
    enqueue_sample_data(params.send_ring);


    printf("Starting lcores.
");
    RTE_LCORE_FOREACH_SLAVE(lcore_id) {
        rte_eal_remote_launch((lcore_function_t*)lcore_recv, &params, lcore_id);
    }

    printf("Waiting for lcores to finish.
");
    rte_eal_mp_wait_lcore();

    print_ring(params.recv_ring);
        getchar();
    return 0;
}
[root@localhost mbuf]# build/app/helloworld -c 0xf -n 4
EAL: Detected 128 lcore(s)
EAL: Detected 4 NUMA nodes
EAL: Multi-process socket /var/run/dpdk/rte/mp_socket
EAL: Selected IOVA mode 'PA'
EAL: No available hugepages reported in hugepages-2048kB
EAL: Probing VFIO support...
EAL: VFIO support initialized
EAL: PCI device 0000:05:00.0 on NUMA socket 0
EAL:   probe driver: 19e5:200 net_hinic
EAL:   using IOMMU type 1 (Type 1)
net_hinic: Initializing pf hinic-0000:05:00.0 in primary process
net_hinic: Device 0000:05:00.0 hwif attribute:
net_hinic: func_idx:0, p2p_idx:0, pciintf_idx:0, vf_in_pf:0, ppf_idx:0, global_vf_id:15, func_type:2
net_hinic: num_aeqs:4, num_ceqs:4, num_irqs:32, dma_attr:2
net_hinic: Get public resource capability:
net_hinic: host_id: 0x0, ep_id: 0x0, intr_type: 0x0, max_cos_id: 0x7, er_id: 0x0, port_id: 0x0
net_hinic: host_total_function: 0xf2, host_oq_id_mask_val: 0x8, max_vf: 0x78
net_hinic: pf_num: 0x2, pf_id_start: 0x0, vf_num: 0xf0, vf_id_start: 0x10
net_hinic: Get share resource capability:
net_hinic: host_pctxs: 0x0, host_cctxs: 0x0, host_scqs: 0x0, host_srqs: 0x0, host_mpts: 0x0
net_hinic: Get l2nic resource capability:
net_hinic: max_sqs: 0x10, max_rqs: 0x10, vf_max_sqs: 0x4, vf_max_rqs: 0x4
net_hinic: Initialize 0000:05:00.0 in primary successfully
EAL: PCI device 0000:06:00.0 on NUMA socket 0
EAL:   probe driver: 19e5:200 net_hinic
EAL: PCI device 0000:7d:00.0 on NUMA socket 0
EAL:   probe driver: 19e5:a222 net_hns3
EAL: PCI device 0000:7d:00.1 on NUMA socket 0
EAL:   probe driver: 19e5:a221 net_hns3
EAL: PCI device 0000:7d:00.2 on NUMA socket 0
EAL:   probe driver: 19e5:a222 net_hns3
EAL: PCI device 0000:7d:00.3 on NUMA socket 0
EAL:   probe driver: 19e5:a221 net_hns3
mempool count : 64
mempool count before get: 64
mempool count after get: 63
mempool count before get: 63
mempool count after get: 62
mempool count before get: 62
mempool count after get: 61
mempool count before get: 61
mempool count after get: 60
Starting lcores.
Starting core 1
core 1: Received 1 and private data 1000
Starting core 2
core 2: Received 3 and private data 1000
Starting core 3
Waiting for lcores to finish.
core 3: Received 5 and private data 1000
DEQ-DATA:2
DEQ-DATA:4
DEQ-DATA:6
原文地址:https://www.cnblogs.com/dream397/p/13669022.html