zoukankan      html  css  js  c++  java
  • rte_mempool_get_priv

    /**
     * Calculate the size of the mempool header.
     *
     * @param mp
     *   Pointer to the memory pool.
     * @param cs
     *   Size of the per-lcore cache.
     */
    #define MEMPOOL_HEADER_SIZE(mp, cs) 
            (sizeof(*(mp)) + (((cs) == 0) ? 0 : 
            (sizeof(struct rte_mempool_cache) * RTE_MAX_LCORE)))
    static inline void *rte_mempool_get_priv(struct rte_mempool *mp)
    {
            return (char *)mp +
                    MEMPOOL_HEADER_SIZE(mp, mp->cache_size);
    }
    #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;
            if (rte_mempool_get(message_pool, &p) < 0)
                    rte_panic("Failed to get message buffer
    ");
            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
    ");
        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;
    }

     private data 共享

    EAL:   probe driver: 19e5:a221 net_hns3
    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 4
    core 4: Received 5 and private data 1000
    Waiting for lcores to finish.
    Starting core 3
    core 3: Received 8 and private data 1000
    DEQ-DATA:2
    DEQ-DATA:4
    DEQ-DATA:6
    DEQ-DATA:9
    q
  • 相关阅读:
    php数组根据某一个键值,把相同键值的合并生成一个新的二维数组
    首次备案图文引导
    阿里云域名备案之如何填写真实性核验单
    国际域名和境外域名能否提交备案
    sublime如何实现函数折叠
    怎样实现给DEDE的栏目增加栏目图片(2)
    深入浅出Hadoop实战开发(HDFS实战图片、MapReduce、HBase实战微博、Hive应用)
    HBase零基础高阶应用实战(CDH5、二级索引、实践、DBA)
    大数据就是这么任性第一季数据结构和算法(一线经验、权威资料、知识新鲜、实践性强、全程源码)
    Cloudera Hadoop 5& Hadoop高阶管理及调优课程(CDH5,Hadoop2.0,HA,安全,管理,调优)
  • 原文地址:https://www.cnblogs.com/dream397/p/13671618.html
Copyright © 2011-2022 走看看