LCOV - code coverage report
Current view: top level - app/test - test_barrier.c (source / functions) Hit Total Coverage
Test: Code coverage Lines: 1 76 1.3 %
Date: 2026-11-01 19:11:22 Functions: 1 6 16.7 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 0 38 0.0 %

           Branch data     Line data    Source code
       1                 :            : /* SPDX-License-Identifier: BSD-3-Clause
       2                 :            :  * Copyright(c) 2010-2018 Intel Corporation
       3                 :            :  */
       4                 :            : 
       5                 :            : /*
       6                 :            :  * Functional test for the full barriers rte_mb() and
       7                 :            :  * rte_atomic_thread_fence(rte_memory_order_seq_cst).
       8                 :            :  * I.E. make sure that LOAD and STORE operations that precede the barrier
       9                 :            :  * are globally visible across the lcores before the LOAD and STORE
      10                 :            :  * operations that follow it.
      11                 :            :  * The test uses a simple implementation of Peterson's lock algorithm
      12                 :            :  * (https://en.wikipedia.org/wiki/Peterson%27s_algorithm)
      13                 :            :  * for two execution units, since that algorithm only works if
      14                 :            :  * store-load reordering is prevented.
      15                 :            :  * Also when executed on a single lcore it can be used as an approximate
      16                 :            :  * estimate of the number of cycles a particular barrier takes.
      17                 :            :  */
      18                 :            : 
      19                 :            : #include <errno.h>
      20                 :            : #include <stdio.h>
      21                 :            : #include <stdint.h>
      22                 :            : #include <stdlib.h>
      23                 :            : #include <string.h>
      24                 :            : #include <inttypes.h>
      25                 :            : 
      26                 :            : #include <rte_launch.h>
      27                 :            : #include <rte_eal.h>
      28                 :            : #include <rte_lcore.h>
      29                 :            : #include <rte_memory.h>
      30                 :            : #include <rte_pause.h>
      31                 :            : #include <rte_random.h>
      32                 :            : #include <rte_cycles.h>
      33                 :            : #include <rte_stdatomic.h>
      34                 :            : 
      35                 :            : #include "test.h"
      36                 :            : 
      37                 :            : #define ADD_MAX         8
      38                 :            : #define ITER_MAX        0x1000000
      39                 :            : 
      40                 :            : enum plock_use_type {
      41                 :            :         USE_MB,
      42                 :            :         USE_FENCE,
      43                 :            :         USE_NUM
      44                 :            : };
      45                 :            : 
      46                 :            : struct plock {
      47                 :            :         RTE_ATOMIC(uint32_t) flag[2];
      48                 :            :         RTE_ATOMIC(uint32_t) victim;
      49                 :            :         enum plock_use_type utype;
      50                 :            : };
      51                 :            : 
      52                 :            : /*
      53                 :            :  * Lock plus protected by it two counters.
      54                 :            :  */
      55                 :            : struct plock_test {
      56                 :            :         struct plock lock;
      57                 :            :         uint64_t val;
      58                 :            :         uint64_t iter;
      59                 :            : };
      60                 :            : 
      61                 :            : /*
      62                 :            :  * Each active lcore shares plock_test struct with it's left and right
      63                 :            :  * neighbours.
      64                 :            :  */
      65                 :            : struct lcore_plock_test {
      66                 :            :         struct plock_test *pt[2]; /* shared, lock-protected data */
      67                 :            :         uint64_t sum[2];          /* local copy of the shared data */
      68                 :            :         uint64_t iter;            /* number of iterations to perform */
      69                 :            :         uint32_t lc;              /* given lcore id */
      70                 :            : };
      71                 :            : 
      72                 :            : static inline void
      73                 :            : store_load_barrier(enum plock_use_type utype)
      74                 :            : {
      75                 :          0 :         if (utype == USE_MB)
      76                 :            :                 rte_mb();
      77                 :            :         else
      78                 :            :                 rte_atomic_thread_fence(rte_memory_order_seq_cst);
      79                 :            : }
      80                 :            : 
      81                 :            : /*
      82                 :            :  * Peterson lock implementation.
      83                 :            :  * The loads in the spin loop are relaxed on purpose: the barrier under
      84                 :            :  * test is the only thing keeping them after the stores above, so a
      85                 :            :  * barrier that fails to prevent store-load reordering shows up as two
      86                 :            :  * lcores in the critical section at once.
      87                 :            :  */
      88                 :            : static void
      89                 :          0 : plock_lock(struct plock *l, uint32_t self)
      90                 :            : {
      91                 :            :         uint32_t other;
      92                 :            : 
      93                 :          0 :         other = self ^ 1;
      94                 :            : 
      95                 :          0 :         rte_atomic_store_explicit(&l->flag[self], 1, rte_memory_order_relaxed);
      96                 :            : 
      97                 :            :         /* Release so that the other lcore cannot see this lcore claim to be
      98                 :            :          * the victim while its flag still reads zero, which would let both
      99                 :            :          * lcores through.
     100                 :            :          */
     101                 :          0 :         rte_atomic_store_explicit(&l->victim, self, rte_memory_order_release);
     102                 :            : 
     103         [ #  # ]:          0 :         store_load_barrier(l->utype);
     104                 :            : 
     105         [ #  # ]:          0 :         while (rte_atomic_load_explicit(&l->flag[other], rte_memory_order_relaxed) == 1 &&
     106         [ #  # ]:          0 :                         rte_atomic_load_explicit(&l->victim, rte_memory_order_relaxed) == self)
     107                 :            :                 rte_pause();
     108                 :            : 
     109                 :            :         rte_atomic_thread_fence(rte_memory_order_acquire);
     110                 :          0 : }
     111                 :            : 
     112                 :            : static void
     113                 :            : plock_unlock(struct plock *l, uint32_t self)
     114                 :            : {
     115                 :          0 :         rte_atomic_store_explicit(&l->flag[self], 0, rte_memory_order_release);
     116                 :            : }
     117                 :            : 
     118                 :            : static void
     119                 :            : plock_reset(struct plock *l, enum plock_use_type utype)
     120                 :            : {
     121                 :            :         memset(l, 0, sizeof(*l));
     122                 :          0 :         l->utype = utype;
     123                 :            : }
     124                 :            : 
     125                 :            : /*
     126                 :            :  * grab the lock, update both counters, release the lock.
     127                 :            :  */
     128                 :            : static void
     129                 :          0 : plock_add(struct plock_test *pt, uint32_t self, uint32_t n)
     130                 :            : {
     131                 :          0 :         plock_lock(&pt->lock, self);
     132                 :          0 :         pt->iter++;
     133                 :          0 :         pt->val += n;
     134                 :            :         plock_unlock(&pt->lock, self);
     135                 :          0 : }
     136                 :            : 
     137                 :            : static int
     138                 :          0 : plock_test1_lcore(void *data)
     139                 :            : {
     140                 :            :         uint64_t tm;
     141                 :            :         uint32_t lc, ln;
     142                 :            :         uint64_t i, n;
     143                 :            :         struct lcore_plock_test *lpt;
     144                 :            : 
     145                 :            :         lpt = data;
     146                 :            :         lc = rte_lcore_id();
     147                 :            : 
     148                 :            :         /* find lcore_plock_test struct for given lcore */
     149   [ #  #  #  # ]:          0 :         for (ln = rte_lcore_count(); ln != 0 && lpt->lc != lc; lpt++, ln--)
     150                 :            :                 ;
     151                 :            : 
     152         [ #  # ]:          0 :         if (ln == 0) {
     153                 :            :                 printf("%s(%u) error at init\n", __func__, lc);
     154                 :          0 :                 return -1;
     155                 :            :         }
     156                 :            : 
     157                 :          0 :         n = rte_rand() % ADD_MAX;
     158                 :            :         tm = rte_get_timer_cycles();
     159                 :            : 
     160                 :            :         /*
     161                 :            :          * for each iteration:
     162                 :            :          * - update shared, locked protected data in a safe manner
     163                 :            :          * - update local copy of the shared data
     164                 :            :          */
     165         [ #  # ]:          0 :         for (i = 0; i != lpt->iter; i++) {
     166                 :            : 
     167                 :          0 :                 plock_add(lpt->pt[0], 0, n);
     168                 :          0 :                 plock_add(lpt->pt[1], 1, n);
     169                 :            : 
     170                 :          0 :                 lpt->sum[0] += n;
     171                 :          0 :                 lpt->sum[1] += n;
     172                 :            : 
     173                 :          0 :                 n = (n + 1) % ADD_MAX;
     174                 :            :         }
     175                 :            : 
     176                 :          0 :         tm = rte_get_timer_cycles() - tm;
     177                 :            : 
     178                 :          0 :         printf("%s(%u): %" PRIu64 " iterations finished, in %" PRIu64
     179                 :            :                 " cycles, %#Lf cycles/iteration, "
     180                 :            :                 "local sum={%" PRIu64 ", %" PRIu64 "}\n",
     181                 :          0 :                 __func__, lc, i, tm, (long double)tm / i,
     182                 :            :                 lpt->sum[0], lpt->sum[1]);
     183                 :          0 :         return 0;
     184                 :            : }
     185                 :            : 
     186                 :            : /*
     187                 :            :  * For N active lcores we allocate N+1 lcore_plock_test structures.
     188                 :            :  * Each active lcore shares one lcore_plock_test structure with its
     189                 :            :  * left lcore neighbor and one lcore_plock_test structure with its
     190                 :            :  * right lcore neighbor.
     191                 :            :  * During the test each lcore updates data in both shared structures and
     192                 :            :  * its local copies. Then at validation phase we check that our shared
     193                 :            :  * and local data are the same.
     194                 :            :  */
     195                 :            : static int
     196                 :          0 : plock_test(uint64_t iter, enum plock_use_type utype)
     197                 :            : {
     198                 :            :         int32_t rc;
     199                 :            :         uint32_t i, lc, n;
     200                 :            :         uint64_t *sum;
     201                 :            :         struct plock_test *pt;
     202                 :            :         struct lcore_plock_test *lpt;
     203                 :            : 
     204                 :            :         /* init phase, allocate and initialize shared data */
     205                 :            : 
     206                 :          0 :         n = rte_lcore_count();
     207                 :          0 :         pt = calloc(n + 1, sizeof(*pt));
     208                 :          0 :         lpt = calloc(n, sizeof(*lpt));
     209                 :          0 :         sum = calloc(n + 1, sizeof(*sum));
     210                 :            : 
     211                 :            :         printf("%s(iter=%" PRIu64 ", utype=%u) started on %u lcores\n",
     212                 :            :                 __func__, iter, utype, n);
     213                 :            : 
     214   [ #  #  #  # ]:          0 :         if (pt == NULL || lpt == NULL || sum == NULL) {
     215                 :            :                 printf("%s: failed to allocate memory for %u lcores\n",
     216                 :            :                         __func__, n);
     217                 :          0 :                 free(pt);
     218                 :          0 :                 free(lpt);
     219                 :          0 :                 free(sum);
     220                 :          0 :                 return -ENOMEM;
     221                 :            :         }
     222                 :            : 
     223         [ #  # ]:          0 :         for (i = 0; i != n + 1; i++)
     224                 :          0 :                 plock_reset(&pt[i].lock, utype);
     225                 :            : 
     226                 :            :         i = 0;
     227         [ #  # ]:          0 :         RTE_LCORE_FOREACH(lc) {
     228                 :            : 
     229                 :          0 :                 lpt[i].lc = lc;
     230                 :          0 :                 lpt[i].iter = iter;
     231                 :          0 :                 lpt[i].pt[0] = pt + i;
     232                 :          0 :                 lpt[i].pt[1] = pt + i + 1;
     233                 :          0 :                 i++;
     234                 :            :         }
     235                 :            : 
     236                 :          0 :         lpt[i - 1].pt[1] = pt;
     237                 :            : 
     238         [ #  # ]:          0 :         for (i = 0; i != n; i++)
     239                 :          0 :                 printf("lpt[%u]={lc=%u, pt={%p, %p},};\n",
     240                 :          0 :                         i, lpt[i].lc, lpt[i].pt[0], lpt[i].pt[1]);
     241                 :            : 
     242                 :            : 
     243                 :            :         /* test phase - start and wait for completion on each active lcore */
     244                 :            : 
     245                 :          0 :         rte_eal_mp_remote_launch(plock_test1_lcore, lpt, CALL_MAIN);
     246                 :          0 :         rte_eal_mp_wait_lcore();
     247                 :            : 
     248                 :            :         /* validation phase - make sure that shared and local data match */
     249                 :            : 
     250         [ #  # ]:          0 :         for (i = 0; i != n; i++) {
     251                 :          0 :                 sum[i] += lpt[i].sum[0];
     252                 :          0 :                 sum[i + 1] += lpt[i].sum[1];
     253                 :            :         }
     254                 :            : 
     255                 :          0 :         sum[0] += sum[i];
     256                 :            : 
     257                 :            :         rc = 0;
     258         [ #  # ]:          0 :         for (i = 0; i != n; i++) {
     259                 :          0 :                 printf("%s: sum[%u]=%" PRIu64 ", pt[%u].val=%" PRIu64 ", pt[%u].iter=%" PRIu64 ";\n",
     260                 :          0 :                         __func__, i, sum[i], i, pt[i].val, i, pt[i].iter);
     261                 :            : 
     262                 :            :                 /* race condition occurred, lock doesn't work properly */
     263   [ #  #  #  # ]:          0 :                 if (sum[i] != pt[i].val || 2 * iter != pt[i].iter) {
     264                 :            :                         printf("error: local and shared sums don't match\n");
     265                 :            :                         rc = -1;
     266                 :            :                 }
     267                 :            :         }
     268                 :            : 
     269                 :          0 :         free(pt);
     270                 :          0 :         free(lpt);
     271                 :          0 :         free(sum);
     272                 :            : 
     273                 :            :         printf("%s(utype=%u) returns %d\n", __func__, utype, rc);
     274                 :          0 :         return rc;
     275                 :            : }
     276                 :            : 
     277                 :            : static int
     278                 :          0 : test_barrier(void)
     279                 :            : {
     280                 :            :         int32_t i, ret, rc[USE_NUM];
     281                 :            : 
     282         [ #  # ]:          0 :         for (i = 0; i != RTE_DIM(rc); i++)
     283                 :          0 :                 rc[i] = plock_test(ITER_MAX, i);
     284                 :            : 
     285                 :            :         ret = 0;
     286         [ #  # ]:          0 :         for (i = 0; i != RTE_DIM(rc); i++) {
     287                 :          0 :                 printf("%s for utype=%d %s\n",
     288         [ #  # ]:          0 :                         __func__, i, rc[i] == 0 ? "passed" : "failed");
     289                 :          0 :                 ret |= rc[i];
     290                 :            :         }
     291                 :            : 
     292                 :          0 :         return ret;
     293                 :            : }
     294                 :            : 
     295                 :        305 : REGISTER_PERF_TEST(barrier_autotest, test_barrier);

Generated by: LCOV version 1.14