dpdk源碼分析 rte_eal_cpu_init()函數

可能用到的函數:

1.access()函數:

        功 能: 確定文件或文件夾的訪問權限。即,檢查某個文件的存取方式,比如說是隻讀方式、只寫方式等。如果指定的存取方式有效,則函數返回0,否則函數返回-1。
  用 法: int access(const char *filenpath, int mode);
        mode參數:
               R_OK 只判斷是否有讀權限
         W_OK 只判斷是否有寫權限
         X_OK 判斷是否有執行權限
         F_OK 只判斷是否存在

2.strtoul()函數:

     strtoul() 函數源自於“string to unsigned long”,用來將字符串轉換成無符號長整型數(unsigned long)。

     原型爲: unsigned long strtoul (const char* str, char** endptr, int base);
     參數介紹:

           str :要轉換的字符串;

           endptr:第一個不能轉換的字符的指針

           base:字符串 str 所採用的進制

     函數說明:strtoul() 會將參數 str 字符串根據參數 base 來轉換成無符號的長整型數(unsigned long)。參數 base 範圍從2 至36,或0。參數 base 代表 str 採用的進制方式,如 base 值爲10 則採用10 進制,若 base 值爲16 則採用16 進制數等。

 


rte_eal_cpu_init()函數的分析:
 

函數功能:

    /* 主要填充幾個數據:cpu_config結構體

     * 1.core_index:如果對應的lcore_id可用,該值爲0,否則爲-1
     * 2.cpuset:如果對應的lcore_id 可用,將該值填入cpuset中
     * 3.detected:1   可用,0 不可用
     * 4.core_id:在socket上的序號,默認從0開始。比如每個socket上兩個core,該core是第二個,則爲1;
     * 5.socket_id:該lcore_id屬於哪一個socket
     *
     * rte_config結構體:
     * lcore_role:ROLE_OFF:沒有使用,ROLE_RTE 可以使用;

     * lcore_count:可以使用的core

     */

源代碼:
int rte_eal_cpu_init(void)
{
    /* pointer to global configuration */
    struct rte_config *config = rte_eal_get_configuration();//獲取全局變量rte_config結構體的指針;
    unsigned lcore_id;
    unsigned count = 0; //用來記錄可以使用的lcore的數量

    /*
     * Parse the maximum set of logical cores, detect the subset of running
     * ones and enable them by default.
     *
     */
    for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
        lcore_config[lcore_id].core_index = count; //初始化爲0

        /* init cpuset for per lcore config */
        CPU_ZERO(&lcore_config[lcore_id].cpuset); //初始化

        /* in 1:1 mapping, record related cpu detected state */
        lcore_config[lcore_id].detected = eal_cpu_detected(lcore_id);
        if (lcore_config[lcore_id].detected == 0) {
            config->lcore_role[lcore_id] = ROLE_OFF;
            lcore_config[lcore_id].core_index = -1;
            continue;
        }

        /* By default, lcore 1:1 map to cpu id */
        CPU_SET(lcore_id, &lcore_config[lcore_id].cpuset); //cpuset設置爲lcore_id

        /* By default, each detected core is enabled */
        config->lcore_role[lcore_id] = ROLE_RTE;
        lcore_config[lcore_id].core_id = eal_cpu_core_id(lcore_id);//該lcore_id在socket上的序號
        lcore_config[lcore_id].socket_id = eal_cpu_socket_id(lcore_id); //socket_id
        if (lcore_config[lcore_id].socket_id >= RTE_MAX_NUMA_NODES)
#ifdef RTE_EAL_ALLOW_INV_SOCKET_ID
            lcore_config[lcore_id].socket_id = 0;
#else
            rte_panic("Socket ID (%u) is greater than "
                "RTE_MAX_NUMA_NODES (%d)\n",
                lcore_config[lcore_id].socket_id,
                RTE_MAX_NUMA_NODES);
#endif

        RTE_LOG(DEBUG, EAL, "Detected lcore %u as "
                "core %u on socket %u\n",
                lcore_id, lcore_config[lcore_id].core_id,
                lcore_config[lcore_id].socket_id);
        count++;
    }
    /* Set the count of enabled logical cores of the EAL configuration */
    config->lcore_count = count; //有效的lcore數
    RTE_LOG(DEBUG, EAL,
        "Support maximum %u logical core(s) by configuration.\n",
        RTE_MAX_LCORE);
    RTE_LOG(DEBUG, EAL, "Detected %u lcore(s)\n", config->lcore_count);

    return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章