LCOV - code coverage report
Current view: top level - spdk/lib/env_dpdk - env.c (source / functions) Hit Total Coverage
Test: Combined Lines: 138 177 78.0 %
Date: 2024-07-15 21:04:32 Functions: 33 42 78.6 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 46 88 52.3 %

           Branch data     Line data    Source code
       1                 :            : /*   SPDX-License-Identifier: BSD-3-Clause
       2                 :            :  *   Copyright (C) 2016 Intel Corporation.
       3                 :            :  *   Copyright (c) 2023, NVIDIA CORPORATION & AFFILIATES.
       4                 :            :  *   All rights reserved.
       5                 :            :  */
       6                 :            : 
       7                 :            : #include "spdk/stdinc.h"
       8                 :            : #include "spdk/util.h"
       9                 :            : #include "spdk/env_dpdk.h"
      10                 :            : #include "spdk/log.h"
      11                 :            : 
      12                 :            : #include "env_internal.h"
      13                 :            : 
      14                 :            : #include <rte_config.h>
      15                 :            : #include <rte_cycles.h>
      16                 :            : #include <rte_malloc.h>
      17                 :            : #include <rte_mempool.h>
      18                 :            : #include <rte_memzone.h>
      19                 :            : #include <rte_version.h>
      20                 :            : #include <rte_eal.h>
      21                 :            : 
      22                 :            : static __thread bool g_is_thread_unaffinitized;
      23                 :            : 
      24                 :            : void *
      25                 :    7772734 : spdk_malloc(size_t size, size_t align, uint64_t *unused, int socket_id, uint32_t flags)
      26                 :            : {
      27   [ +  -  -  + ]:    7772734 :         if (flags == 0 || unused != NULL) {
      28                 :          0 :                 return NULL;
      29                 :            :         }
      30                 :            : 
      31                 :    7772734 :         align = spdk_max(align, RTE_CACHE_LINE_SIZE);
      32                 :    7772734 :         return rte_malloc_socket(NULL, size, align, socket_id);
      33                 :            : }
      34                 :            : 
      35                 :            : void *
      36                 :    8870652 : spdk_zmalloc(size_t size, size_t align, uint64_t *unused, int socket_id, uint32_t flags)
      37                 :            : {
      38   [ +  -  -  + ]:    8870652 :         if (flags == 0 || unused != NULL) {
      39                 :          0 :                 return NULL;
      40                 :            :         }
      41                 :            : 
      42                 :    8870652 :         align = spdk_max(align, RTE_CACHE_LINE_SIZE);
      43                 :    8870652 :         return rte_zmalloc_socket(NULL, size, align, socket_id);
      44                 :            : }
      45                 :            : 
      46                 :            : void *
      47                 :      60530 : spdk_realloc(void *buf, size_t size, size_t align)
      48                 :            : {
      49                 :      60530 :         align = spdk_max(align, RTE_CACHE_LINE_SIZE);
      50                 :      60530 :         return rte_realloc(buf, size, align);
      51                 :            : }
      52                 :            : 
      53                 :            : void
      54                 :   17069756 : spdk_free(void *buf)
      55                 :            : {
      56                 :   17069756 :         rte_free(buf);
      57                 :   17069756 : }
      58                 :            : 
      59                 :            : void *
      60                 :      26353 : spdk_dma_malloc_socket(size_t size, size_t align, uint64_t *unused, int socket_id)
      61                 :            : {
      62                 :      26353 :         return spdk_malloc(size, align, unused, socket_id, (SPDK_MALLOC_DMA | SPDK_MALLOC_SHARE));
      63                 :            : }
      64                 :            : 
      65                 :            : void *
      66                 :     647859 : spdk_dma_zmalloc_socket(size_t size, size_t align, uint64_t *unused, int socket_id)
      67                 :            : {
      68                 :     647859 :         return spdk_zmalloc(size, align, unused, socket_id, (SPDK_MALLOC_DMA | SPDK_MALLOC_SHARE));
      69                 :            : }
      70                 :            : 
      71                 :            : void *
      72                 :      26144 : spdk_dma_malloc(size_t size, size_t align, uint64_t *unused)
      73                 :            : {
      74                 :      26144 :         return spdk_dma_malloc_socket(size, align, unused, SPDK_ENV_SOCKET_ID_ANY);
      75                 :            : }
      76                 :            : 
      77                 :            : void *
      78                 :     647859 : spdk_dma_zmalloc(size_t size, size_t align, uint64_t *unused)
      79                 :            : {
      80                 :     647859 :         return spdk_dma_zmalloc_socket(size, align, unused, SPDK_ENV_SOCKET_ID_ANY);
      81                 :            : }
      82                 :            : 
      83                 :            : void *
      84                 :          0 : spdk_dma_realloc(void *buf, size_t size, size_t align, uint64_t *unused)
      85                 :            : {
      86         [ #  # ]:          0 :         if (unused != NULL) {
      87                 :          0 :                 return NULL;
      88                 :            :         }
      89                 :          0 :         align = spdk_max(align, RTE_CACHE_LINE_SIZE);
      90                 :          0 :         return rte_realloc(buf, size, align);
      91                 :            : }
      92                 :            : 
      93                 :            : void
      94                 :     695149 : spdk_dma_free(void *buf)
      95                 :            : {
      96                 :     695149 :         spdk_free(buf);
      97                 :     695149 : }
      98                 :            : 
      99                 :            : void *
     100                 :        864 : spdk_memzone_reserve_aligned(const char *name, size_t len, int socket_id,
     101                 :            :                              unsigned flags, unsigned align)
     102                 :            : {
     103                 :            :         const struct rte_memzone *mz;
     104                 :        864 :         unsigned dpdk_flags = 0;
     105                 :            : 
     106         [ -  + ]:        864 :         if ((flags & SPDK_MEMZONE_NO_IOVA_CONTIG) == 0) {
     107                 :          0 :                 dpdk_flags |= RTE_MEMZONE_IOVA_CONTIG;
     108                 :            :         }
     109                 :            : 
     110         [ +  - ]:        864 :         if (socket_id == SPDK_ENV_SOCKET_ID_ANY) {
     111                 :        864 :                 socket_id = SOCKET_ID_ANY;
     112                 :            :         }
     113                 :            : 
     114                 :        864 :         mz = rte_memzone_reserve_aligned(name, len, socket_id, dpdk_flags, align);
     115                 :            : 
     116         [ +  - ]:        864 :         if (mz != NULL) {
     117         [ -  + ]:        864 :                 memset(mz->addr, 0, len);
     118                 :        864 :                 return mz->addr;
     119                 :            :         } else {
     120                 :          0 :                 return NULL;
     121                 :            :         }
     122                 :            : }
     123                 :            : 
     124                 :            : void *
     125                 :        864 : spdk_memzone_reserve(const char *name, size_t len, int socket_id, unsigned flags)
     126                 :            : {
     127                 :        864 :         return spdk_memzone_reserve_aligned(name, len, socket_id, flags,
     128                 :            :                                             RTE_CACHE_LINE_SIZE);
     129                 :            : }
     130                 :            : 
     131                 :            : void *
     132                 :        190 : spdk_memzone_lookup(const char *name)
     133                 :            : {
     134                 :        190 :         const struct rte_memzone *mz = rte_memzone_lookup(name);
     135                 :            : 
     136         [ +  - ]:        190 :         if (mz != NULL) {
     137                 :        190 :                 return mz->addr;
     138                 :            :         } else {
     139                 :          0 :                 return NULL;
     140                 :            :         }
     141                 :            : }
     142                 :            : 
     143                 :            : int
     144                 :          0 : spdk_memzone_free(const char *name)
     145                 :            : {
     146                 :          0 :         const struct rte_memzone *mz = rte_memzone_lookup(name);
     147                 :            : 
     148         [ #  # ]:          0 :         if (mz != NULL) {
     149                 :          0 :                 return rte_memzone_free(mz);
     150                 :            :         }
     151                 :            : 
     152                 :          0 :         return -1;
     153                 :            : }
     154                 :            : 
     155                 :            : void
     156                 :          0 : spdk_memzone_dump(FILE *f)
     157                 :            : {
     158                 :          0 :         rte_memzone_dump(f);
     159                 :          0 : }
     160                 :            : 
     161                 :            : struct spdk_mempool *
     162                 :      11989 : spdk_mempool_create_ctor(const char *name, size_t count,
     163                 :            :                          size_t ele_size, size_t cache_size, int socket_id,
     164                 :            :                          spdk_mempool_obj_cb_t *obj_init, void *obj_init_arg)
     165                 :            : {
     166                 :            :         struct rte_mempool *mp;
     167                 :            :         size_t tmp;
     168                 :            : 
     169         [ +  - ]:      11989 :         if (socket_id == SPDK_ENV_SOCKET_ID_ANY) {
     170                 :      11989 :                 socket_id = SOCKET_ID_ANY;
     171                 :            :         }
     172                 :            : 
     173                 :            :         /* No more than half of all elements can be in cache */
     174         [ -  + ]:      11989 :         tmp = (count / 2) / rte_lcore_count();
     175         [ +  + ]:      11989 :         if (cache_size > tmp) {
     176                 :       3960 :                 cache_size = tmp;
     177                 :            :         }
     178                 :            : 
     179         [ +  + ]:      11989 :         if (cache_size > RTE_MEMPOOL_CACHE_MAX_SIZE) {
     180                 :       3737 :                 cache_size = RTE_MEMPOOL_CACHE_MAX_SIZE;
     181                 :            :         }
     182                 :            : 
     183                 :      11989 :         mp = rte_mempool_create(name, count, ele_size, cache_size,
     184                 :            :                                 0, NULL, NULL, (rte_mempool_obj_cb_t *)obj_init, obj_init_arg,
     185                 :            :                                 socket_id, 0);
     186                 :            : 
     187                 :      11989 :         return (struct spdk_mempool *)mp;
     188                 :            : }
     189                 :            : 
     190                 :            : 
     191                 :            : struct spdk_mempool *
     192                 :      10108 : spdk_mempool_create(const char *name, size_t count,
     193                 :            :                     size_t ele_size, size_t cache_size, int socket_id)
     194                 :            : {
     195                 :      10108 :         return spdk_mempool_create_ctor(name, count, ele_size, cache_size, socket_id,
     196                 :            :                                         NULL, NULL);
     197                 :            : }
     198                 :            : 
     199                 :            : char *
     200                 :          0 : spdk_mempool_get_name(struct spdk_mempool *mp)
     201                 :            : {
     202                 :          0 :         return ((struct rte_mempool *)mp)->name;
     203                 :            : }
     204                 :            : 
     205                 :            : void
     206                 :      11987 : spdk_mempool_free(struct spdk_mempool *mp)
     207                 :            : {
     208                 :      11987 :         rte_mempool_free((struct rte_mempool *)mp);
     209                 :      11987 : }
     210                 :            : 
     211                 :            : void *
     212                 :  128431121 : spdk_mempool_get(struct spdk_mempool *mp)
     213                 :            : {
     214                 :  128431121 :         void *ele = NULL;
     215                 :            :         int rc;
     216                 :            : 
     217                 :  128431121 :         rc = rte_mempool_get((struct rte_mempool *)mp, &ele);
     218         [ +  + ]:  128431121 :         if (rc != 0) {
     219                 :     129946 :                 return NULL;
     220                 :            :         }
     221                 :  128301175 :         return ele;
     222                 :            : }
     223                 :            : 
     224                 :            : int
     225                 :      82580 : spdk_mempool_get_bulk(struct spdk_mempool *mp, void **ele_arr, size_t count)
     226                 :            : {
     227                 :     165160 :         return rte_mempool_get_bulk((struct rte_mempool *)mp, ele_arr, count);
     228                 :            : }
     229                 :            : 
     230                 :            : void
     231                 :  111615400 : spdk_mempool_put(struct spdk_mempool *mp, void *ele)
     232                 :            : {
     233                 :   99599539 :         rte_mempool_put((struct rte_mempool *)mp, ele);
     234                 :  111615400 : }
     235                 :            : 
     236                 :            : void
     237                 :   13909451 : spdk_mempool_put_bulk(struct spdk_mempool *mp, void **ele_arr, size_t count)
     238                 :            : {
     239                 :   13909451 :         rte_mempool_put_bulk((struct rte_mempool *)mp, ele_arr, count);
     240                 :   13909451 : }
     241                 :            : 
     242                 :            : size_t
     243                 :     531970 : spdk_mempool_count(const struct spdk_mempool *pool)
     244                 :            : {
     245                 :     531970 :         return rte_mempool_avail_count((struct rte_mempool *)pool);
     246                 :            : }
     247                 :            : 
     248                 :            : uint32_t
     249                 :          0 : spdk_mempool_obj_iter(struct spdk_mempool *mp, spdk_mempool_obj_cb_t obj_cb,
     250                 :            :                       void *obj_cb_arg)
     251                 :            : {
     252                 :          0 :         return rte_mempool_obj_iter((struct rte_mempool *)mp, (rte_mempool_obj_cb_t *)obj_cb,
     253                 :            :                                     obj_cb_arg);
     254                 :            : }
     255                 :            : 
     256                 :            : struct env_mempool_mem_iter_ctx {
     257                 :            :         spdk_mempool_mem_cb_t *user_cb;
     258                 :            :         void *user_arg;
     259                 :            : };
     260                 :            : 
     261                 :            : static void
     262                 :          0 : mempool_mem_iter_remap(struct rte_mempool *mp, void *opaque, struct rte_mempool_memhdr *memhdr,
     263                 :            :                        unsigned mem_idx)
     264                 :            : {
     265                 :          0 :         struct env_mempool_mem_iter_ctx *ctx = opaque;
     266                 :            : 
     267                 :          0 :         ctx->user_cb((struct spdk_mempool *)mp, ctx->user_arg, memhdr->addr, memhdr->iova, memhdr->len,
     268                 :            :                      mem_idx);
     269                 :          0 : }
     270                 :            : 
     271                 :            : uint32_t
     272                 :          0 : spdk_mempool_mem_iter(struct spdk_mempool *mp, spdk_mempool_mem_cb_t mem_cb,
     273                 :            :                       void *mem_cb_arg)
     274                 :            : {
     275                 :          0 :         struct env_mempool_mem_iter_ctx ctx = {
     276                 :            :                 .user_cb = mem_cb,
     277                 :            :                 .user_arg = mem_cb_arg
     278                 :            :         };
     279                 :            : 
     280                 :          0 :         return rte_mempool_mem_iter((struct rte_mempool *)mp, mempool_mem_iter_remap, &ctx);
     281                 :            : }
     282                 :            : 
     283                 :            : struct spdk_mempool *
     284                 :          0 : spdk_mempool_lookup(const char *name)
     285                 :            : {
     286                 :          0 :         return (struct spdk_mempool *)rte_mempool_lookup(name);
     287                 :            : }
     288                 :            : 
     289                 :            : bool
     290                 :  140758914 : spdk_process_is_primary(void)
     291                 :            : {
     292                 :  140758914 :         return (rte_eal_process_type() == RTE_PROC_PRIMARY);
     293                 :            : }
     294                 :            : 
     295                 :            : uint64_t
     296                 :17739322611 : spdk_get_ticks(void)
     297                 :            : {
     298                 :17739322611 :         return rte_get_timer_cycles();
     299                 :            : }
     300                 :            : 
     301                 :            : uint64_t
     302                 :   10657956 : spdk_get_ticks_hz(void)
     303                 :            : {
     304                 :   10657956 :         return rte_get_timer_hz();
     305                 :            : }
     306                 :            : 
     307                 :            : void
     308                 :    2216108 : spdk_delay_us(unsigned int us)
     309                 :            : {
     310                 :    2216108 :         rte_delay_us(us);
     311                 :    2216108 : }
     312                 :            : 
     313                 :            : void
     314                 :          0 : spdk_pause(void)
     315                 :            : {
     316                 :          0 :         rte_pause();
     317                 :          0 : }
     318                 :            : 
     319                 :            : void
     320                 :       1666 : spdk_unaffinitize_thread(void)
     321                 :            : {
     322                 :        982 :         rte_cpuset_t new_cpuset;
     323                 :            :         long num_cores, i;
     324                 :            : 
     325   [ -  +  -  + ]:       1666 :         if (g_is_thread_unaffinitized) {
     326                 :          0 :                 return;
     327                 :            :         }
     328                 :            : 
     329         [ -  + ]:       1666 :         CPU_ZERO(&new_cpuset);
     330                 :            : 
     331                 :       1666 :         num_cores = sysconf(_SC_NPROCESSORS_CONF);
     332                 :            : 
     333                 :            :         /* Create a mask containing all CPUs */
     334         [ +  + ]:      39684 :         for (i = 0; i < num_cores; i++) {
     335         [ +  - ]:      38018 :                 CPU_SET(i, &new_cpuset);
     336                 :            :         }
     337                 :            : 
     338                 :       1666 :         rte_thread_set_affinity(&new_cpuset);
     339                 :       1666 :         g_is_thread_unaffinitized = true;
     340                 :            : }
     341                 :            : 
     342                 :            : void *
     343                 :        114 : spdk_call_unaffinitized(void *cb(void *arg), void *arg)
     344                 :            : {
     345                 :          0 :         rte_cpuset_t orig_cpuset;
     346                 :            :         void *ret;
     347                 :            : 
     348         [ -  + ]:        114 :         if (cb == NULL) {
     349                 :          0 :                 return NULL;
     350                 :            :         }
     351                 :            : 
     352   [ -  +  +  + ]:        114 :         if (g_is_thread_unaffinitized) {
     353                 :          4 :                 ret = cb(arg);
     354                 :            :         } else {
     355                 :        110 :                 rte_thread_get_affinity(&orig_cpuset);
     356                 :        110 :                 spdk_unaffinitize_thread();
     357                 :            : 
     358                 :        110 :                 ret = cb(arg);
     359                 :            : 
     360                 :        110 :                 rte_thread_set_affinity(&orig_cpuset);
     361                 :        110 :                 g_is_thread_unaffinitized = false;
     362                 :            :         }
     363                 :            : 
     364                 :        114 :         return ret;
     365                 :            : }
     366                 :            : 
     367                 :            : struct spdk_ring *
     368                 :      16633 : spdk_ring_create(enum spdk_ring_type type, size_t count, int socket_id)
     369                 :            : {
     370                 :       7253 :         char ring_name[64];
     371                 :            :         static uint32_t ring_num = 0;
     372                 :      16633 :         unsigned flags = RING_F_EXACT_SZ;
     373                 :            : 
     374   [ +  +  +  - ]:      16633 :         switch (type) {
     375                 :         94 :         case SPDK_RING_TYPE_SP_SC:
     376                 :         94 :                 flags |= RING_F_SP_ENQ | RING_F_SC_DEQ;
     377                 :         94 :                 break;
     378                 :      11421 :         case SPDK_RING_TYPE_MP_SC:
     379                 :      11421 :                 flags |= RING_F_SC_DEQ;
     380                 :      11421 :                 break;
     381                 :       5118 :         case SPDK_RING_TYPE_MP_MC:
     382                 :       5118 :                 flags |= 0;
     383                 :       5118 :                 break;
     384                 :          0 :         default:
     385                 :          0 :                 return NULL;
     386                 :            :         }
     387                 :            : 
     388         [ -  + ]:      16633 :         snprintf(ring_name, sizeof(ring_name), "ring_%u_%d",
     389                 :            :                  __atomic_fetch_add(&ring_num, 1, __ATOMIC_RELAXED), getpid());
     390                 :            : 
     391                 :      16633 :         return (struct spdk_ring *)rte_ring_create(ring_name, count, socket_id, flags);
     392                 :            : }
     393                 :            : 
     394                 :            : void
     395                 :      16633 : spdk_ring_free(struct spdk_ring *ring)
     396                 :            : {
     397                 :      16633 :         rte_ring_free((struct rte_ring *)ring);
     398                 :      16633 : }
     399                 :            : 
     400                 :            : size_t
     401                 :   97142813 : spdk_ring_count(struct spdk_ring *ring)
     402                 :            : {
     403                 :   97142813 :         return rte_ring_count((struct rte_ring *)ring);
     404                 :            : }
     405                 :            : 
     406                 :            : size_t
     407                 :   85859069 : spdk_ring_enqueue(struct spdk_ring *ring, void **objs, size_t count,
     408                 :            :                   size_t *free_space)
     409                 :            : {
     410   [ +  -  -  -  :  171718138 :         return rte_ring_enqueue_bulk((struct rte_ring *)ring, objs, count,
                      - ]
     411                 :            :                                      (unsigned int *)free_space);
     412                 :            : }
     413                 :            : 
     414                 :            : size_t
     415                 :22659507003 : spdk_ring_dequeue(struct spdk_ring *ring, void **objs, size_t count)
     416                 :            : {
     417   [ +  +  -  -  :45319014006 :         return rte_ring_dequeue_burst((struct rte_ring *)ring, objs, count, NULL);
                      - ]
     418                 :            : }
     419                 :            : 
     420                 :            : void
     421                 :         20 : spdk_env_dpdk_dump_mem_stats(FILE *file)
     422                 :            : {
     423   [ -  +  -  + ]:         20 :         fprintf(file, "DPDK memory size %" PRIu64 "\n", rte_eal_get_physmem_size());
     424   [ -  +  -  + ]:         20 :         fprintf(file, "DPDK memory layout\n");
     425                 :         20 :         rte_dump_physmem_layout(file);
     426   [ -  +  -  + ]:         20 :         fprintf(file, "DPDK memzones.\n");
     427                 :         20 :         rte_memzone_dump(file);
     428   [ -  +  -  + ]:         20 :         fprintf(file, "DPDK mempools.\n");
     429                 :         20 :         rte_mempool_list_dump(file);
     430   [ -  +  -  + ]:         20 :         fprintf(file, "DPDK malloc stats.\n");
     431                 :         20 :         rte_malloc_dump_stats(file, NULL);
     432   [ -  +  -  + ]:         20 :         fprintf(file, "DPDK malloc heaps.\n");
     433                 :         20 :         rte_malloc_dump_heaps(file);
     434                 :         20 : }
     435                 :            : 
     436                 :            : int
     437                 :         96 : spdk_get_tid(void)
     438                 :            : {
     439                 :         96 :         return rte_sys_gettid();
     440                 :            : }

Generated by: LCOV version 1.14