LCOV - code coverage report
Current view: top level - app/test - test_latencystats.c (source / functions) Hit Total Coverage
Test: Code coverage Lines: 74 77 96.1 %
Date: 2025-08-01 17:49:26 Functions: 10 10 100.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 31 56 55.4 %

           Branch data     Line data    Source code
       1                 :            : /* SPDX-License-Identifier: BSD-3-Clause
       2                 :            :  * Copyright(c) 2018 Intel Corporation
       3                 :            :  */
       4                 :            : 
       5                 :            : #include <stdio.h>
       6                 :            : #include <stdint.h>
       7                 :            : #include <string.h>
       8                 :            : 
       9                 :            : #include <rte_ethdev.h>
      10                 :            : #include <rte_latencystats.h>
      11                 :            : #include "rte_lcore.h"
      12                 :            : #include "rte_metrics.h"
      13                 :            : 
      14                 :            : #include "sample_packet_forward.h"
      15                 :            : #include "test.h"
      16                 :            : 
      17                 :            : #define NUM_STATS 5
      18                 :            : #define LATENCY_NUM_PACKETS 10
      19                 :            : #define QUEUE_ID 0
      20                 :            : 
      21                 :            : static uint16_t portid;
      22                 :            : static struct rte_ring *ring;
      23                 :            : 
      24                 :            : static struct rte_metric_name lat_stats_strings[NUM_STATS] = {
      25                 :            :         {"min_latency_ns"},
      26                 :            :         {"avg_latency_ns"},
      27                 :            :         {"max_latency_ns"},
      28                 :            :         {"jitter_ns"},
      29                 :            :         {"samples"},
      30                 :            : };
      31                 :            : 
      32                 :            : /* Test case for latency init with metrics init */
      33                 :          1 : static int test_latency_init(void)
      34                 :            : {
      35                 :            :         int ret = 0;
      36                 :            : 
      37                 :            :         /* Metrics Initialization */
      38                 :          1 :         rte_metrics_init(rte_socket_id());
      39                 :            : 
      40                 :          1 :         ret = rte_latencystats_init(1, NULL);
      41         [ -  + ]:          1 :         TEST_ASSERT(ret >= 0, "Test Failed: rte_latencystats_init failed");
      42                 :            : 
      43                 :            :         return TEST_SUCCESS;
      44                 :            : }
      45                 :            : 
      46                 :            : /* Test case to update the latency stats */
      47                 :          1 : static int test_latency_update(void)
      48                 :            : {
      49                 :            :         int ret = 0;
      50                 :            : 
      51                 :          1 :         ret = rte_latencystats_update();
      52         [ -  + ]:          1 :         TEST_ASSERT(ret >= 0, "Test Failed: rte_latencystats_update failed");
      53                 :            : 
      54                 :            :         return TEST_SUCCESS;
      55                 :            : }
      56                 :            : 
      57                 :            : /* Test case to uninit latency stats */
      58                 :          1 : static int test_latency_uninit(void)
      59                 :            : {
      60                 :            :         int ret = 0;
      61                 :            : 
      62                 :          1 :         ret = rte_latencystats_uninit();
      63         [ -  + ]:          1 :         TEST_ASSERT(ret >= 0, "Test Failed: rte_latencystats_uninit failed");
      64                 :            : 
      65                 :          1 :         ret = rte_metrics_deinit();
      66         [ -  + ]:          1 :         TEST_ASSERT(ret >= 0, "Test Failed: rte_metrics_deinit failed");
      67                 :            : 
      68                 :            :         return TEST_SUCCESS;
      69                 :            : }
      70                 :            : 
      71                 :            : /* Test case to get names of latency stats */
      72                 :          1 : static int test_latencystats_get_names(void)
      73                 :            : {
      74                 :            :         int ret, i;
      75                 :            :         uint16_t size;
      76                 :          1 :         struct rte_metric_name names[NUM_STATS] = { };
      77                 :            : 
      78                 :            :         /* Success Test: Valid names and size */
      79                 :            :         size = NUM_STATS;
      80                 :          1 :         ret = rte_latencystats_get_names(names, size);
      81         [ +  + ]:          6 :         for (i = 0; i < NUM_STATS; i++) {
      82         [ +  - ]:          5 :                 if (strcmp(lat_stats_strings[i].name, names[i].name) == 0)
      83                 :            :                         printf(" %s\n", names[i].name);
      84                 :            :                 else
      85                 :            :                         printf("Failed: Names are not matched\n");
      86                 :            :         }
      87         [ -  + ]:          1 :         TEST_ASSERT((ret == NUM_STATS), "Test Failed to get metrics names");
      88                 :            : 
      89                 :            :         /* Failure Test: Invalid names and valid size */
      90                 :          1 :         ret = rte_latencystats_get_names(NULL, size);
      91         [ -  + ]:          1 :         TEST_ASSERT((ret == NUM_STATS), "Test Failed to get the metrics count,"
      92                 :            :                     "Actual: %d Expected: %d", ret, NUM_STATS);
      93                 :            : 
      94                 :            :         /* Failure Test: Valid names and invalid size */
      95                 :            :         size = 0;
      96                 :          1 :         ret = rte_latencystats_get_names(names, size);
      97         [ -  + ]:          1 :         TEST_ASSERT((ret == NUM_STATS), "Test Failed to get the metrics count,"
      98                 :            :                     "Actual: %d Expected: %d", ret, NUM_STATS);
      99                 :            : 
     100                 :            :         return TEST_SUCCESS;
     101                 :            : }
     102                 :            : 
     103                 :            : /* Test case to get latency stats values */
     104                 :          1 : static int test_latencystats_get(void)
     105                 :            : {
     106                 :            :         int ret;
     107                 :            :         uint16_t size;
     108                 :          1 :         struct rte_metric_value values[NUM_STATS] = { };
     109                 :            : 
     110                 :            :         /* Success Test: Valid values and valid size */
     111                 :            :         size = NUM_STATS;
     112                 :          1 :         ret = rte_latencystats_get(values, size);
     113         [ -  + ]:          1 :         TEST_ASSERT((ret == NUM_STATS), "Test Failed to get latency metrics"
     114                 :            :                         " values");
     115                 :            : 
     116                 :            :         /* Failure Test: Invalid values and valid size */
     117                 :          1 :         ret = rte_latencystats_get(NULL, size);
     118         [ -  + ]:          1 :         TEST_ASSERT((ret == NUM_STATS), "Test Failed to get the stats count,"
     119                 :            :                     "Actual: %d Expected: %d", ret, NUM_STATS);
     120                 :            : 
     121                 :            :         /* Failure Test: Valid values and invalid size */
     122                 :            :         size = 0;
     123                 :          1 :         ret = rte_latencystats_get(values, size);
     124         [ -  + ]:          1 :         TEST_ASSERT((ret == NUM_STATS), "Test Failed to get the stats count,"
     125                 :            :                     "Actual: %d Expected: %d", ret, NUM_STATS);
     126                 :            : 
     127                 :            :         return TEST_SUCCESS;
     128                 :            : }
     129                 :            : 
     130                 :          1 : static int test_latency_ring_setup(void)
     131                 :            : {
     132                 :          1 :         test_ring_setup(&ring, &portid);
     133                 :            : 
     134                 :          1 :         return TEST_SUCCESS;
     135                 :            : }
     136                 :            : 
     137                 :          1 : static void test_latency_ring_free(void)
     138                 :            : {
     139                 :          1 :         test_ring_free(ring);
     140                 :          1 :         test_vdev_uninit("net_ring_net_ringa");
     141                 :          1 : }
     142                 :            : 
     143                 :          1 : static int test_latency_packet_forward(void)
     144                 :            : {
     145                 :            :         unsigned int i;
     146                 :            :         int ret;
     147                 :          1 :         struct rte_mbuf *pbuf[LATENCY_NUM_PACKETS] = { };
     148                 :            :         struct rte_mempool *mp;
     149                 :          1 :         char poolname[] = "mbuf_pool";
     150                 :            :         uint64_t end_cycles;
     151                 :          1 :         struct rte_metric_value values[NUM_STATS] = { };
     152                 :          1 :         struct rte_metric_name names[NUM_STATS] = { };
     153                 :            : 
     154                 :          1 :         ret = test_get_mbuf_from_pool(&mp, pbuf, poolname);
     155         [ -  + ]:          1 :         if (ret < 0) {
     156                 :            :                 printf("allocate mbuf pool Failed\n");
     157                 :          0 :                 return TEST_FAILED;
     158                 :            :         }
     159                 :          1 :         ret = test_dev_start(portid, mp);
     160         [ -  + ]:          1 :         if (ret < 0) {
     161                 :          0 :                 printf("test_dev_start(%hu, %p) failed, error code: %d\n",
     162                 :            :                         portid, mp, ret);
     163                 :          0 :                 return TEST_FAILED;
     164                 :            :         }
     165                 :            : 
     166                 :          1 :         ret = rte_latencystats_get_names(names, NUM_STATS);
     167         [ -  + ]:          1 :         TEST_ASSERT((ret == NUM_STATS), "Test Failed to get metrics names");
     168                 :            : 
     169                 :          1 :         ret = rte_latencystats_get(values, NUM_STATS);
     170         [ -  + ]:          1 :         TEST_ASSERT(ret == NUM_STATS, "Test failed to get results before forwarding");
     171         [ -  + ]:          1 :         TEST_ASSERT(values[4].value == 0, "Samples not zero at start of test");
     172                 :            : 
     173                 :            :         /*
     174                 :            :          * Want test to run long enough to collect sufficient samples
     175                 :            :          * but not so long that scheduler decides to reschedule it (1000 hz).
     176                 :            :          */
     177                 :          1 :         end_cycles = rte_rdtsc() + rte_get_tsc_hz() / 2000;
     178                 :            :         do {
     179                 :       4024 :                 ret = test_packet_forward(pbuf, portid, QUEUE_ID);
     180         [ -  + ]:       4024 :                 if (ret < 0)
     181                 :            :                         printf("send pkts Failed\n");
     182         [ +  + ]:       4024 :         } while (rte_rdtsc() < end_cycles);
     183                 :            : 
     184                 :          1 :         ret = rte_latencystats_get(values, NUM_STATS);
     185         [ -  + ]:          1 :         TEST_ASSERT(ret == NUM_STATS, "Test failed to get results after forwarding");
     186                 :            : 
     187         [ +  + ]:          6 :         for (i = 0; i < NUM_STATS; i++) {
     188                 :          5 :                 uint16_t k = values[i].key;
     189                 :            : 
     190                 :          5 :                 printf("%s = %"PRIu64"\n",
     191                 :          5 :                        names[k].name, values[i].value);
     192                 :            :         }
     193                 :            : 
     194         [ -  + ]:          1 :         TEST_ASSERT(values[4].value > 0, "No samples taken");
     195         [ -  + ]:          1 :         TEST_ASSERT(values[0].value > 0, "Min latency should not be zero");
     196         [ -  + ]:          1 :         TEST_ASSERT(values[1].value > 0, "Avg latency should not be zero");
     197         [ -  + ]:          1 :         TEST_ASSERT(values[2].value > 0, "Max latency should not be zero");
     198         [ -  + ]:          1 :         TEST_ASSERT(values[0].value < values[1].value, "Min latency > Avg latency");
     199         [ -  + ]:          1 :         TEST_ASSERT(values[0].value < values[2].value, "Min latency > Max latency");
     200         [ -  + ]:          1 :         TEST_ASSERT(values[1].value < values[2].value, "Avg latency > Max latency");
     201                 :            : 
     202                 :          1 :         rte_eth_dev_stop(portid);
     203                 :          1 :         test_put_mbuf_to_pool(mp, pbuf);
     204                 :            : 
     205                 :          1 :         return (ret >= 0) ? TEST_SUCCESS : TEST_FAILED;
     206                 :            : }
     207                 :            : 
     208                 :            : static struct
     209                 :            : unit_test_suite latencystats_testsuite = {
     210                 :            :         .suite_name = "Latency Stats Unit Test Suite",
     211                 :            :         .setup = test_latency_ring_setup,
     212                 :            :         .teardown = test_latency_ring_free,
     213                 :            :         .unit_test_cases = {
     214                 :            : 
     215                 :            :                 /* Test Case 1: To check latency init with
     216                 :            :                  * metrics init
     217                 :            :                  */
     218                 :            :                 TEST_CASE_ST(NULL, NULL, test_latency_init),
     219                 :            : 
     220                 :            :                 /* Test Case 2: To check whether latency stats names
     221                 :            :                  * are retrieved
     222                 :            :                  */
     223                 :            :                 TEST_CASE_ST(NULL, NULL, test_latencystats_get_names),
     224                 :            : 
     225                 :            :                 /* Test Case 3: To check whether latency stats
     226                 :            :                  * values are retrieved
     227                 :            :                  */
     228                 :            :                 TEST_CASE_ST(NULL, NULL, test_latencystats_get),
     229                 :            : 
     230                 :            :                 /* Test Case 4: Do packet forwarding for metrics
     231                 :            :                  * calculation and check the latency metrics values
     232                 :            :                  * are updated
     233                 :            :                  */
     234                 :            :                 TEST_CASE_ST(test_latency_packet_forward, NULL,
     235                 :            :                                 test_latency_update),
     236                 :            : 
     237                 :            :                 /* Test Case 5: To check uninit of latency test */
     238                 :            :                 TEST_CASE_ST(NULL, NULL, test_latency_uninit),
     239                 :            : 
     240                 :            :                 TEST_CASES_END()
     241                 :            :         }
     242                 :            : };
     243                 :            : 
     244                 :          1 : static int test_latencystats(void)
     245                 :            : {
     246                 :          1 :         return unit_test_suite_runner(&latencystats_testsuite);
     247                 :            : }
     248                 :            : 
     249                 :        253 : REGISTER_FAST_TEST(latencystats_autotest, true, true, test_latencystats);

Generated by: LCOV version 1.14