LCOV - code coverage report
Current view: top level - app/test - test_stack.c (source / functions) Hit Total Coverage
Test: Code coverage Lines: 128 215 59.5 %
Date: 2026-11-01 19:11:22 Functions: 16 16 100.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 86 142 60.6 %

           Branch data     Line data    Source code
       1                 :            : /* SPDX-License-Identifier: BSD-3-Clause
       2                 :            :  * Copyright(c) 2019 Intel Corporation
       3                 :            :  */
       4                 :            : 
       5                 :            : #include <string.h>
       6                 :            : 
       7                 :            : #include <rte_lcore.h>
       8                 :            : #include <rte_malloc.h>
       9                 :            : #include <rte_random.h>
      10                 :            : #include <rte_stack.h>
      11                 :            : 
      12                 :            : #include "test.h"
      13                 :            : 
      14                 :            : #define STACK_SIZE 65536
      15                 :            : #define MAX_BULK 512
      16                 :            : 
      17                 :            : static int
      18                 :          6 : test_stack_push_pop(struct rte_stack *s, void **obj_table, unsigned int bulk_sz)
      19                 :            : {
      20                 :            :         unsigned int i, ret;
      21                 :            :         void **popped_objs;
      22                 :            : 
      23                 :          6 :         popped_objs = rte_calloc(NULL, STACK_SIZE, sizeof(void *), 0);
      24         [ -  + ]:          6 :         if (popped_objs == NULL) {
      25                 :            :                 printf("[%s():%u] failed to calloc %zu bytes\n",
      26                 :            :                        __func__, __LINE__, STACK_SIZE * sizeof(void *));
      27                 :          0 :                 return -1;
      28                 :            :         }
      29                 :            : 
      30         [ +  + ]:     196998 :         for (i = 0; i < STACK_SIZE; i += bulk_sz) {
      31         [ +  + ]:     196992 :                 ret = rte_stack_push(s, &obj_table[i], bulk_sz);
      32                 :            : 
      33         [ -  + ]:     196992 :                 if (ret != bulk_sz) {
      34                 :            :                         printf("[%s():%u] push returned: %d (expected %u)\n",
      35                 :            :                                __func__, __LINE__, ret, bulk_sz);
      36                 :          0 :                         rte_free(popped_objs);
      37                 :          0 :                         return -1;
      38                 :            :                 }
      39                 :            : 
      40         [ -  + ]:     196992 :                 if (rte_stack_count(s) != i + bulk_sz) {
      41                 :            :                         printf("[%s():%u] stack count: %u (expected %u)\n",
      42                 :            :                                __func__, __LINE__, rte_stack_count(s),
      43                 :            :                                i + bulk_sz);
      44                 :          0 :                         rte_free(popped_objs);
      45                 :          0 :                         return -1;
      46                 :            :                 }
      47                 :            : 
      48         [ -  + ]:     196992 :                 if (rte_stack_free_count(s) != STACK_SIZE - i - bulk_sz) {
      49                 :            :                         printf("[%s():%u] stack free count: %u (expected %u)\n",
      50                 :            :                                __func__, __LINE__, rte_stack_count(s),
      51                 :            :                                STACK_SIZE - i - bulk_sz);
      52                 :          0 :                         rte_free(popped_objs);
      53                 :          0 :                         return -1;
      54                 :            :                 }
      55                 :            :         }
      56                 :            : 
      57         [ +  + ]:     196998 :         for (i = 0; i < STACK_SIZE; i += bulk_sz) {
      58         [ +  + ]:     196992 :                 ret = rte_stack_pop(s, &popped_objs[i], bulk_sz);
      59                 :            : 
      60         [ -  + ]:     196992 :                 if (ret != bulk_sz) {
      61                 :            :                         printf("[%s():%u] pop returned: %d (expected %u)\n",
      62                 :            :                                __func__, __LINE__, ret, bulk_sz);
      63                 :          0 :                         rte_free(popped_objs);
      64                 :          0 :                         return -1;
      65                 :            :                 }
      66                 :            : 
      67         [ -  + ]:     196992 :                 if (rte_stack_count(s) != STACK_SIZE - i - bulk_sz) {
      68                 :            :                         printf("[%s():%u] stack count: %u (expected %u)\n",
      69                 :            :                                __func__, __LINE__, rte_stack_count(s),
      70                 :            :                                STACK_SIZE - i - bulk_sz);
      71                 :          0 :                         rte_free(popped_objs);
      72                 :          0 :                         return -1;
      73                 :            :                 }
      74                 :            : 
      75         [ -  + ]:     196992 :                 if (rte_stack_free_count(s) != i + bulk_sz) {
      76                 :            :                         printf("[%s():%u] stack free count: %u (expected %u)\n",
      77                 :            :                                __func__, __LINE__, rte_stack_count(s),
      78                 :            :                                i + bulk_sz);
      79                 :          0 :                         rte_free(popped_objs);
      80                 :          0 :                         return -1;
      81                 :            :                 }
      82                 :            :         }
      83                 :            : 
      84         [ +  + ]:          6 :         if (!(s->flags & RTE_STACK_F_PILE)) {
      85                 :            :                 /* Normal stack. */
      86                 :          5 : lifo:
      87         [ +  + ]:     327685 :                 for (i = 0; i < STACK_SIZE; i++) {
      88         [ -  + ]:     327680 :                         if (obj_table[i] != popped_objs[STACK_SIZE - i - 1]) {
      89                 :            :                                 printf("[%s():%u] Incorrect value %p at index 0x%x\n",
      90                 :            :                                        __func__, __LINE__,
      91                 :            :                                        popped_objs[STACK_SIZE - i - 1], i);
      92                 :          0 :                                 rte_free(popped_objs);
      93                 :          0 :                                 return -1;
      94                 :            :                         }
      95                 :            :                 }
      96                 :            :         } else {
      97                 :            :                 /* Pile. Ordering not strictly LIFO. */
      98         [ +  + ]:          2 :                 if (bulk_sz < RTE_STACK_PILE_BULK_SIZE)
      99                 :          1 :                         goto lifo;
     100         [ +  - ]:          1 :                 if ((bulk_sz & (RTE_STACK_PILE_BULK_SIZE - 1)) == 0) {
     101         [ +  + ]:       2049 :                         for (i = 0; i < STACK_SIZE; i += RTE_STACK_PILE_BULK_SIZE) {
     102                 :       2048 :                                 if (memcmp(&obj_table[i],
     103         [ -  + ]:       2048 :                                                 &popped_objs[STACK_SIZE - RTE_STACK_PILE_BULK_SIZE -
     104                 :            :                                                 i],
     105                 :            :                                                 sizeof(void *) * RTE_STACK_PILE_BULK_SIZE) != 0) {
     106                 :          0 :                                         printf("[%s():%u] Incorrect values %p at 0x%x, bulk %u\n",
     107                 :            :                                                __func__, __LINE__,
     108                 :            :                                                popped_objs[STACK_SIZE - RTE_STACK_PILE_BULK_SIZE -
     109                 :            :                                                i],
     110                 :            :                                                i, bulk_sz);
     111                 :          0 :                                         rte_free(popped_objs);
     112                 :          0 :                                         return -1;
     113                 :            :                                 }
     114                 :            :                         }
     115                 :            :                 }
     116                 :            :         }
     117                 :            : 
     118                 :          6 :         rte_free(popped_objs);
     119                 :            : 
     120                 :          6 :         return 0;
     121                 :            : }
     122                 :            : 
     123                 :            : static int
     124                 :          1 : test_stack_pile_fragmentation(struct rte_stack *s, void **obj_table)
     125                 :            : {
     126                 :            :         unsigned int ret;
     127                 :            :         void **popped_objs;
     128                 :            : 
     129                 :          1 :         popped_objs = rte_calloc(NULL, STACK_SIZE, sizeof(void *), 0);
     130         [ -  + ]:          1 :         if (popped_objs == NULL) {
     131                 :            :                 printf("[%s():%u] failed to calloc %zu bytes\n",
     132                 :            :                        __func__, __LINE__, STACK_SIZE * sizeof(void *));
     133                 :          0 :                 return -1;
     134                 :            :         }
     135                 :            : 
     136                 :            :         ret = rte_stack_push(s, &obj_table[0], RTE_STACK_PILE_BULK_SIZE);
     137                 :            :         if (ret != RTE_STACK_PILE_BULK_SIZE) {
     138                 :            :                 printf("[%s():%u] push returned: %d (expected %u)\n",
     139                 :            :                        __func__, __LINE__, ret, RTE_STACK_PILE_BULK_SIZE);
     140                 :          0 :                 rte_free(popped_objs);
     141                 :          0 :                 return -1;
     142                 :            :         }
     143         [ -  + ]:          1 :         if (rte_stack_count(s) != RTE_STACK_PILE_BULK_SIZE) {
     144                 :            :                 printf("[%s():%u] stack count: %u (expected %u)\n",
     145                 :            :                        __func__, __LINE__, rte_stack_count(s),
     146                 :            :                        RTE_STACK_PILE_BULK_SIZE);
     147                 :          0 :                 rte_free(popped_objs);
     148                 :          0 :                 return -1;
     149                 :            :         }
     150         [ -  + ]:          1 :         if (rte_stack_free_count(s) != STACK_SIZE - RTE_STACK_PILE_BULK_SIZE) {
     151                 :            :                 printf("[%s():%u] stack free count: %u (expected %u)\n",
     152                 :            :                        __func__, __LINE__, rte_stack_count(s),
     153                 :            :                        STACK_SIZE - RTE_STACK_PILE_BULK_SIZE);
     154                 :          0 :                 rte_free(popped_objs);
     155                 :          0 :                 return -1;
     156                 :            :         }
     157                 :            : 
     158                 :            :         ret = rte_stack_pop(s, &popped_objs[0], 3);
     159                 :            :         if (ret != 3) {
     160                 :            :                 printf("[%s():%u] pop returned: %d (expected %u)\n",
     161                 :            :                        __func__, __LINE__, ret, 3);
     162                 :          0 :                 rte_free(popped_objs);
     163                 :          0 :                 return -1;
     164                 :            :         }
     165         [ -  + ]:          1 :         if (rte_stack_count(s) != RTE_STACK_PILE_BULK_SIZE - 3) {
     166                 :            :                 printf("[%s():%u] stack count: %u (expected %u)\n",
     167                 :            :                        __func__, __LINE__, rte_stack_count(s),
     168                 :            :                        RTE_STACK_PILE_BULK_SIZE - 3);
     169                 :          0 :                 rte_free(popped_objs);
     170                 :          0 :                 return -1;
     171                 :            :         }
     172         [ -  + ]:          1 :         if (rte_stack_free_count(s) != STACK_SIZE - (RTE_STACK_PILE_BULK_SIZE - 3)) {
     173                 :            :                 printf("[%s():%u] stack free count: %u (expected %u)\n",
     174                 :            :                        __func__, __LINE__, rte_stack_count(s),
     175                 :            :                        STACK_SIZE - (RTE_STACK_PILE_BULK_SIZE - 3));
     176                 :          0 :                 rte_free(popped_objs);
     177                 :          0 :                 return -1;
     178                 :            :         }
     179                 :            : 
     180         [ -  + ]:          1 :         ret = rte_stack_pop(s, &popped_objs[3], RTE_STACK_PILE_BULK_SIZE - 3);
     181                 :            :         if (ret != RTE_STACK_PILE_BULK_SIZE - 3) {
     182                 :            :                 printf("[%s():%u] pop returned: %d (expected %u)\n",
     183                 :            :                        __func__, __LINE__, ret, RTE_STACK_PILE_BULK_SIZE - 3);
     184                 :          0 :                 rte_free(popped_objs);
     185                 :          0 :                 return -1;
     186                 :            :         }
     187         [ -  + ]:          1 :         if (rte_stack_count(s) != 0) {
     188                 :            :                 printf("[%s():%u] stack count: %u (expected %u)\n",
     189                 :            :                        __func__, __LINE__, rte_stack_count(s),
     190                 :            :                        0);
     191                 :          0 :                 rte_free(popped_objs);
     192                 :          0 :                 return -1;
     193                 :            :         }
     194         [ -  + ]:          1 :         if (rte_stack_free_count(s) != STACK_SIZE) {
     195                 :            :                 printf("[%s():%u] stack free count: %u (expected %u)\n",
     196                 :            :                        __func__, __LINE__, rte_stack_count(s),
     197                 :            :                        STACK_SIZE);
     198                 :          0 :                 rte_free(popped_objs);
     199                 :          0 :                 return -1;
     200                 :            :         }
     201                 :            : 
     202         [ -  + ]:          1 :         if (memcmp(obj_table, popped_objs, sizeof(void *) * RTE_STACK_PILE_BULK_SIZE) != 0) {
     203                 :            :                 printf("[%s():%u] Incorrect values\n",
     204                 :            :                        __func__, __LINE__);
     205                 :          0 :                 rte_free(popped_objs);
     206                 :          0 :                 return -1;
     207                 :            :         }
     208                 :            : 
     209                 :          1 :         rte_free(popped_objs);
     210                 :            : 
     211                 :          1 :         return 0;
     212                 :            : }
     213                 :            : 
     214                 :            : static int
     215                 :          1 : test_stack_pile_retry(struct rte_stack *s, void **obj_table)
     216                 :            : {
     217                 :            :         unsigned int i, ret;
     218                 :            :         void **popped_objs;
     219                 :            : 
     220                 :          1 :         popped_objs = rte_calloc(NULL, STACK_SIZE, sizeof(void *), 0);
     221         [ -  + ]:          1 :         if (popped_objs == NULL) {
     222                 :            :                 printf("[%s():%u] failed to calloc %zu bytes\n",
     223                 :            :                        __func__, __LINE__, STACK_SIZE * sizeof(void *));
     224                 :          0 :                 return -1;
     225                 :            :         }
     226                 :            : 
     227                 :            :         ret = rte_stack_push(s, &obj_table[0], 3);
     228                 :            :         if (ret != 3) {
     229                 :            :                 printf("[%s():%u] push returned: %d (expected %u)\n",
     230                 :            :                        __func__, __LINE__, ret, 3);
     231                 :          0 :                 rte_free(popped_objs);
     232                 :          0 :                 return -1;
     233                 :            :         }
     234         [ -  + ]:          1 :         if (rte_stack_count(s) != 3) {
     235                 :            :                 printf("[%s():%u] stack count: %u (expected %u)\n",
     236                 :            :                        __func__, __LINE__, rte_stack_count(s),
     237                 :            :                        3);
     238                 :          0 :                 rte_free(popped_objs);
     239                 :          0 :                 return -1;
     240                 :            :         }
     241         [ -  + ]:          1 :         if (rte_stack_free_count(s) != STACK_SIZE - 3) {
     242                 :            :                 printf("[%s():%u] stack free count: %u (expected %u)\n",
     243                 :            :                        __func__, __LINE__, rte_stack_count(s),
     244                 :            :                        STACK_SIZE - 3);
     245                 :          0 :                 rte_free(popped_objs);
     246                 :          0 :                 return -1;
     247                 :            :         }
     248                 :            : 
     249         [ -  + ]:          1 :         ret = rte_stack_push(s, &obj_table[3], RTE_STACK_PILE_BULK_SIZE - 3);
     250                 :            :         if (ret != RTE_STACK_PILE_BULK_SIZE - 3) {
     251                 :            :                 printf("[%s():%u] push returned: %d (expected %u)\n",
     252                 :            :                        __func__, __LINE__, ret, RTE_STACK_PILE_BULK_SIZE - 3);
     253                 :          0 :                 rte_free(popped_objs);
     254                 :          0 :                 return -1;
     255                 :            :         }
     256         [ -  + ]:          1 :         if (rte_stack_count(s) != RTE_STACK_PILE_BULK_SIZE) {
     257                 :            :                 printf("[%s():%u] stack count: %u (expected %u)\n",
     258                 :            :                        __func__, __LINE__, rte_stack_count(s),
     259                 :            :                        RTE_STACK_PILE_BULK_SIZE);
     260                 :          0 :                 rte_free(popped_objs);
     261                 :          0 :                 return -1;
     262                 :            :         }
     263         [ -  + ]:          1 :         if (rte_stack_free_count(s) != STACK_SIZE - RTE_STACK_PILE_BULK_SIZE) {
     264                 :            :                 printf("[%s():%u] stack free count: %u (expected %u)\n",
     265                 :            :                        __func__, __LINE__, rte_stack_count(s),
     266                 :            :                        STACK_SIZE - RTE_STACK_PILE_BULK_SIZE);
     267                 :          0 :                 rte_free(popped_objs);
     268                 :          0 :                 return -1;
     269                 :            :         }
     270                 :            : 
     271                 :            :         ret = rte_stack_pop(s, &popped_objs[0], RTE_STACK_PILE_BULK_SIZE);
     272                 :            :         if (ret != RTE_STACK_PILE_BULK_SIZE) {
     273                 :            :                 printf("[%s():%u] pop returned: %d (expected %u)\n",
     274                 :            :                        __func__, __LINE__, ret, RTE_STACK_PILE_BULK_SIZE);
     275                 :          0 :                 rte_free(popped_objs);
     276                 :          0 :                 return -1;
     277                 :            :         }
     278         [ -  + ]:          1 :         if (rte_stack_count(s) != 0) {
     279                 :            :                 printf("[%s():%u] stack count: %u (expected %u)\n",
     280                 :            :                        __func__, __LINE__, rte_stack_count(s),
     281                 :            :                        0);
     282                 :          0 :                 rte_free(popped_objs);
     283                 :          0 :                 return -1;
     284                 :            :         }
     285         [ -  + ]:          1 :         if (rte_stack_free_count(s) != STACK_SIZE) {
     286                 :            :                 printf("[%s():%u] stack free count: %u (expected %u)\n",
     287                 :            :                        __func__, __LINE__, rte_stack_count(s),
     288                 :            :                        STACK_SIZE);
     289                 :          0 :                 rte_free(popped_objs);
     290                 :          0 :                 return -1;
     291                 :            :         }
     292                 :            : 
     293         [ +  + ]:         33 :         for (i = 0; i < RTE_STACK_PILE_BULK_SIZE; i++) {
     294         [ -  + ]:         32 :                 if (obj_table[i] != popped_objs[RTE_STACK_PILE_BULK_SIZE - i - 1]) {
     295                 :            :                         printf("[%s():%u] Incorrect value %p at index 0x%x\n",
     296                 :            :                                __func__, __LINE__,
     297                 :            :                                popped_objs[RTE_STACK_PILE_BULK_SIZE - i - 1], i);
     298                 :          0 :                         rte_free(popped_objs);
     299                 :          0 :                         return -1;
     300                 :            :                 }
     301                 :            :         }
     302                 :            : 
     303                 :          1 :         rte_free(popped_objs);
     304                 :            : 
     305                 :          1 :         return 0;
     306                 :            : }
     307                 :            : 
     308                 :            : static int
     309                 :          3 : test_stack_basic(uint32_t flags)
     310                 :            : {
     311                 :            :         struct rte_stack *s = NULL;
     312                 :            :         void **obj_table = NULL;
     313                 :            :         int i, ret = -1;
     314                 :            : 
     315                 :          3 :         obj_table = rte_calloc(NULL, STACK_SIZE, sizeof(void *), 0);
     316         [ -  + ]:          3 :         if (obj_table == NULL) {
     317                 :            :                 printf("[%s():%u] failed to calloc %zu bytes\n",
     318                 :            :                        __func__, __LINE__, STACK_SIZE * sizeof(void *));
     319                 :          0 :                 goto fail_test;
     320                 :            :         }
     321                 :            : 
     322         [ +  + ]:     196611 :         for (i = 0; i < STACK_SIZE; i++)
     323                 :     196608 :                 obj_table[i] = (void *)(uintptr_t)i;
     324                 :            : 
     325                 :          3 :         s = rte_stack_create(__func__, STACK_SIZE, rte_socket_id(), flags);
     326         [ -  + ]:          3 :         if (s == NULL) {
     327                 :            :                 printf("[%s():%u] failed to create a stack\n",
     328                 :            :                        __func__, __LINE__);
     329                 :          0 :                 goto fail_test;
     330                 :            :         }
     331                 :            : 
     332         [ -  + ]:          3 :         if (rte_stack_lookup(__func__) != s) {
     333                 :            :                 printf("[%s():%u] failed to lookup a stack\n",
     334                 :            :                        __func__, __LINE__);
     335                 :          0 :                 goto fail_test;
     336                 :            :         }
     337                 :            : 
     338         [ -  + ]:          3 :         if (rte_stack_count(s) != 0) {
     339                 :            :                 printf("[%s():%u] stack count: %u (expected 0)\n",
     340                 :            :                        __func__, __LINE__, rte_stack_count(s));
     341                 :          0 :                 goto fail_test;
     342                 :            :         }
     343                 :            : 
     344         [ -  + ]:          3 :         if (rte_stack_free_count(s) != STACK_SIZE) {
     345                 :            :                 printf("[%s():%u] stack free count: %u (expected %u)\n",
     346                 :            :                        __func__, __LINE__, rte_stack_count(s), STACK_SIZE);
     347                 :          0 :                 goto fail_test;
     348                 :            :         }
     349                 :            : 
     350                 :          3 :         ret = test_stack_push_pop(s, obj_table, 1);
     351         [ -  + ]:          3 :         if (ret) {
     352                 :            :                 printf("[%s():%u] Single object push/pop failed\n",
     353                 :            :                        __func__, __LINE__);
     354                 :          0 :                 goto fail_test;
     355                 :            :         }
     356                 :            : 
     357                 :          3 :         ret = test_stack_push_pop(s, obj_table, MAX_BULK);
     358         [ -  + ]:          3 :         if (ret) {
     359                 :            :                 printf("[%s():%u] Bulk object push/pop failed\n",
     360                 :            :                        __func__, __LINE__);
     361                 :          0 :                 goto fail_test;
     362                 :            :         }
     363                 :            : 
     364         [ +  + ]:          3 :         if (s->flags & RTE_STACK_F_PILE) {
     365                 :          1 :                 ret = test_stack_pile_fragmentation(s, obj_table);
     366         [ -  + ]:          1 :                 if (ret) {
     367                 :            :                         printf("[%s():%u] Pile push/pop fragmentation failed\n",
     368                 :            :                                __func__, __LINE__);
     369                 :          0 :                         goto fail_test;
     370                 :            :                 }
     371                 :          1 :                 ret = test_stack_pile_retry(s, obj_table);
     372         [ -  + ]:          1 :                 if (ret) {
     373                 :            :                         printf("[%s():%u] Pile push/pop retry failed\n",
     374                 :            :                                __func__, __LINE__);
     375                 :          0 :                         goto fail_test;
     376                 :            :                 }
     377                 :            :         }
     378                 :            : 
     379                 :            :         ret = rte_stack_push(s, obj_table, STACK_SIZE);
     380                 :            :         if (ret == 0) {
     381                 :            :                 printf("[%s():%u] All objects push failed\n",
     382                 :            :                        __func__, __LINE__);
     383                 :          0 :                 goto fail_test;
     384                 :            :         }
     385                 :            :         ret = rte_stack_push(s, obj_table, STACK_SIZE);
     386                 :            :         if (ret != 0) {
     387                 :            :                 printf("[%s():%u] Excess objects push succeeded\n",
     388                 :            :                        __func__, __LINE__);
     389                 :          0 :                 goto fail_test;
     390                 :            :         }
     391                 :            :         ret = rte_stack_pop(s, obj_table, STACK_SIZE);
     392                 :            :         if (ret == 0) {
     393                 :            :                 printf("[%s():%u] All objects pop failed\n",
     394                 :            :                        __func__, __LINE__);
     395                 :          0 :                 goto fail_test;
     396                 :            :         }
     397                 :            : 
     398                 :            :         ret = rte_stack_pop(s, obj_table, 1);
     399                 :            :         if (ret != 0) {
     400                 :            :                 printf("[%s():%u] Empty stack pop succeeded\n",
     401                 :            :                        __func__, __LINE__);
     402                 :          0 :                 goto fail_test;
     403                 :            :         }
     404                 :            : 
     405                 :            :         ret = 0;
     406                 :          3 :         goto done;
     407                 :            : 
     408                 :            : fail_test:
     409                 :            :         ret = -1;
     410                 :            : 
     411                 :          3 : done:
     412                 :          3 :         rte_stack_free(s);
     413                 :            : 
     414                 :          3 :         rte_free(obj_table);
     415                 :            : 
     416                 :          3 :         return ret;
     417                 :            : }
     418                 :            : 
     419                 :            : static int
     420                 :          3 : test_stack_name_reuse(uint32_t flags)
     421                 :            : {
     422                 :            :         struct rte_stack *s[2];
     423                 :            : 
     424                 :          3 :         s[0] = rte_stack_create("test", STACK_SIZE, rte_socket_id(), flags);
     425         [ -  + ]:          3 :         if (s[0] == NULL) {
     426                 :            :                 printf("[%s():%u] Failed to create a stack\n",
     427                 :            :                        __func__, __LINE__);
     428                 :          0 :                 return -1;
     429                 :            :         }
     430                 :            : 
     431                 :          3 :         s[1] = rte_stack_create("test", STACK_SIZE, rte_socket_id(), flags);
     432         [ -  + ]:          3 :         if (s[1] != NULL) {
     433                 :            :                 printf("[%s():%u] Failed to detect re-used name\n",
     434                 :            :                        __func__, __LINE__);
     435                 :          0 :                 return -1;
     436                 :            :         }
     437                 :            : 
     438                 :          3 :         rte_stack_free(s[0]);
     439                 :            : 
     440                 :          3 :         return 0;
     441                 :            : }
     442                 :            : 
     443                 :            : static int
     444                 :          3 : test_stack_name_length(uint32_t flags)
     445                 :            : {
     446                 :            :         char name[RTE_STACK_NAMESIZE + 1];
     447                 :            :         struct rte_stack *s;
     448                 :            : 
     449                 :            :         memset(name, 's', sizeof(name));
     450                 :          3 :         name[RTE_STACK_NAMESIZE] = '\0';
     451                 :            : 
     452                 :          3 :         s = rte_stack_create(name, STACK_SIZE, rte_socket_id(), flags);
     453         [ -  + ]:          3 :         if (s != NULL) {
     454                 :            :                 printf("[%s():%u] Failed to prevent long name\n",
     455                 :            :                        __func__, __LINE__);
     456                 :          0 :                 return -1;
     457                 :            :         }
     458                 :            : 
     459         [ -  + ]:          3 :         if (rte_errno != ENAMETOOLONG) {
     460                 :            :                 printf("[%s():%u] rte_stack failed to set correct errno on failed lookup\n",
     461                 :            :                        __func__, __LINE__);
     462                 :          0 :                 return -1;
     463                 :            :         }
     464                 :            : 
     465                 :            :         return 0;
     466                 :            : }
     467                 :            : 
     468                 :            : static int
     469                 :          3 : test_lookup_null(void)
     470                 :            : {
     471                 :          3 :         struct rte_stack *s = rte_stack_lookup("stack_not_found");
     472                 :            : 
     473         [ -  + ]:          3 :         if (s != NULL) {
     474                 :            :                 printf("[%s():%u] rte_stack found a non-existent stack\n",
     475                 :            :                        __func__, __LINE__);
     476                 :          0 :                 return -1;
     477                 :            :         }
     478                 :            : 
     479         [ -  + ]:          3 :         if (rte_errno != ENOENT) {
     480                 :            :                 printf("[%s():%u] rte_stack failed to set correct errno on failed lookup\n",
     481                 :            :                        __func__, __LINE__);
     482                 :          0 :                 return -1;
     483                 :            :         }
     484                 :            : 
     485                 :          3 :         s = rte_stack_lookup(NULL);
     486                 :            : 
     487         [ -  + ]:          3 :         if (s != NULL) {
     488                 :            :                 printf("[%s():%u] rte_stack found a non-existent stack\n",
     489                 :            :                        __func__, __LINE__);
     490                 :          0 :                 return -1;
     491                 :            :         }
     492                 :            : 
     493         [ -  + ]:          3 :         if (rte_errno != EINVAL) {
     494                 :            :                 printf("[%s():%u] rte_stack failed to set correct errno on failed lookup\n",
     495                 :            :                        __func__, __LINE__);
     496                 :          0 :                 return -1;
     497                 :            :         }
     498                 :            : 
     499                 :            :         return 0;
     500                 :            : }
     501                 :            : 
     502                 :            : static int
     503                 :            : test_free_null(void)
     504                 :            : {
     505                 :            :         /* Check whether the library proper handles a NULL pointer */
     506                 :          3 :         rte_stack_free(NULL);
     507                 :            : 
     508                 :            :         return 0;
     509                 :            : }
     510                 :            : 
     511                 :            : #define NUM_ITERS_BASE  100000u
     512                 :            : #define NUM_ITERS_MIN   1000u
     513                 :            : 
     514                 :            : struct test_args {
     515                 :            :         struct rte_stack *s;
     516                 :            :         unsigned int num_iters;
     517                 :            : };
     518                 :            : 
     519                 :            : static struct test_args thread_test_args;
     520                 :            : 
     521                 :            : static int
     522                 :          6 : stack_thread_push_pop(__rte_unused void *args)
     523                 :            : {
     524                 :            :         void *obj_table[MAX_BULK];
     525                 :            :         unsigned int i;
     526                 :            : 
     527         [ +  + ]:     299070 :         for (i = 0; i < thread_test_args.num_iters; i++) {
     528                 :            :                 unsigned int num;
     529                 :            : 
     530                 :     299064 :                 num = rte_rand() % MAX_BULK;
     531                 :            : 
     532   [ +  +  -  + ]:     597588 :                 if (rte_stack_push(thread_test_args.s, obj_table, num) != num) {
     533                 :            :                         printf("[%s():%u] Failed to push %u pointers\n",
     534                 :            :                                __func__, __LINE__, num);
     535                 :          0 :                         return -1;
     536                 :            :                 }
     537                 :            : 
     538   [ +  +  -  + ]:     598279 :                 if (rte_stack_pop(thread_test_args.s, obj_table, num) != num) {
     539                 :            :                         printf("[%s():%u] Failed to pop %u pointers\n",
     540                 :            :                                __func__, __LINE__, num);
     541                 :          0 :                         return -1;
     542                 :            :                 }
     543                 :            :         }
     544                 :            : 
     545                 :            :         return 0;
     546                 :            : }
     547                 :            : 
     548                 :            : static int
     549                 :          3 : test_stack_multithreaded(uint32_t flags)
     550                 :            : {
     551                 :            :         unsigned int lcore_id;
     552                 :            :         struct rte_stack *s;
     553                 :            :         unsigned int iterations;
     554                 :            :         int result = 0;
     555                 :            : 
     556         [ -  + ]:          3 :         if (rte_lcore_count() < 2) {
     557                 :            :                 printf("Not enough cores for test_stack_multithreaded, expecting at least 2\n");
     558                 :          0 :                 return TEST_SKIPPED;
     559                 :            :         }
     560                 :            :         iterations = test_scale_iterations(NUM_ITERS_BASE, NUM_ITERS_MIN);
     561                 :            : 
     562                 :          3 :         printf("[%s():%u] Running with %u lcores\n",
     563                 :            :                __func__, __LINE__, rte_lcore_count());
     564                 :            : 
     565                 :          3 :         s = rte_stack_create("test", MAX_BULK * rte_lcore_count(), rte_socket_id(), flags);
     566         [ -  + ]:          3 :         if (s == NULL) {
     567                 :            :                 printf("[%s():%u] Failed to create a stack\n",
     568                 :            :                        __func__, __LINE__);
     569                 :          0 :                 return -1;
     570                 :            :         }
     571                 :            : 
     572                 :          3 :         thread_test_args.num_iters = iterations;
     573                 :          3 :         thread_test_args.s = s;
     574                 :            : 
     575         [ -  + ]:          3 :         if (rte_eal_mp_remote_launch(stack_thread_push_pop, NULL, CALL_MAIN))
     576                 :          0 :                 rte_panic("Failed to launch tests\n");
     577                 :            : 
     578         [ +  + ]:          9 :         RTE_LCORE_FOREACH(lcore_id) {
     579         [ -  + ]:          6 :                 if (rte_eal_wait_lcore(lcore_id) < 0)
     580                 :            :                         result = -1;
     581                 :            :         }
     582                 :            : 
     583                 :          3 :         rte_stack_free(s);
     584                 :          3 :         return result;
     585                 :            : }
     586                 :            : 
     587                 :            : static int
     588                 :          3 : __test_stack(uint32_t flags)
     589                 :            : {
     590         [ +  - ]:          3 :         if (test_stack_basic(flags) < 0)
     591                 :            :                 return -1;
     592                 :            : 
     593         [ +  - ]:          3 :         if (test_lookup_null() < 0)
     594                 :            :                 return -1;
     595                 :            : 
     596                 :            :         if (test_free_null() < 0)
     597                 :            :                 return -1;
     598                 :            : 
     599         [ +  - ]:          3 :         if (test_stack_name_reuse(flags) < 0)
     600                 :            :                 return -1;
     601                 :            : 
     602         [ +  - ]:          3 :         if (test_stack_name_length(flags) < 0)
     603                 :            :                 return -1;
     604                 :            : 
     605         [ -  + ]:          3 :         if (test_stack_multithreaded(flags) < 0)
     606                 :          0 :                 return -1;
     607                 :            : 
     608                 :            :         return 0;
     609                 :            : }
     610                 :            : 
     611                 :            : static int
     612                 :          1 : test_stack(void)
     613                 :            : {
     614                 :          1 :         return __test_stack(0);
     615                 :            : }
     616                 :            : 
     617                 :            : static int
     618                 :          1 : test_lf_stack(void)
     619                 :            : {
     620                 :            : #if defined(RTE_STACK_LF_SUPPORTED)
     621                 :          1 :         return __test_stack(RTE_STACK_F_LF);
     622                 :            : #else
     623                 :            :         return TEST_SKIPPED;
     624                 :            : #endif
     625                 :            : }
     626                 :            : 
     627                 :            : static int
     628                 :          1 : test_pile(void)
     629                 :            : {
     630                 :            : #if defined(RTE_STACK_PILE_SUPPORTED)
     631                 :          1 :         return __test_stack(RTE_STACK_F_PILE);
     632                 :            : #else
     633                 :            :         return TEST_SKIPPED;
     634                 :            : #endif
     635                 :            : }
     636                 :            : 
     637                 :        305 : REGISTER_FAST_TEST(stack_autotest, NOHUGE_SKIP, ASAN_OK, test_stack);
     638                 :        305 : REGISTER_FAST_TEST(stack_lf_autotest, NOHUGE_SKIP, ASAN_OK, test_lf_stack);
     639                 :        305 : REGISTER_FAST_TEST(stack_pile_autotest, NOHUGE_SKIP, ASAN_OK, test_pile);

Generated by: LCOV version 1.14