LCOV - code coverage report
Current view: top level - app/test - test_metrics.c (source / functions) Hit Total Coverage
Test: Code coverage Lines: 86 86 100.0 %
Date: 2025-03-01 20:23:48 Functions: 11 11 100.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 35 68 51.5 %

           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 <string.h>
       7                 :            : #include <stdint.h>
       8                 :            : #include <errno.h>
       9                 :            : 
      10                 :            : #include <rte_lcore.h>
      11                 :            : #include <rte_metrics.h>
      12                 :            : 
      13                 :            : #include "test.h"
      14                 :            : 
      15                 :            : #define REG_METRIC_COUNT        6
      16                 :            : #define METRIC_LESSER_COUNT     3
      17                 :            : #define KEY     1
      18                 :            : #define VALUE   1
      19                 :            : 
      20                 :            : /* Initializes metric module. This function must be called
      21                 :            :  * from a primary process before metrics are used
      22                 :            :  */
      23                 :            : static int
      24                 :          1 : test_metrics_init(void)
      25                 :            : {
      26                 :          1 :         rte_metrics_init(rte_socket_id());
      27                 :          1 :         return TEST_SUCCESS;
      28                 :            : }
      29                 :            : 
      30                 :            : /* Deinitialize metric module. This function must be called
      31                 :            :  * from a primary process after metrics usage is over
      32                 :            :  */
      33                 :            : static int
      34                 :          1 : test_metrics_deinitialize(void)
      35                 :            : {
      36                 :            :         int err = 0;
      37                 :          1 :         err = rte_metrics_deinit();
      38         [ -  + ]:          1 :         TEST_ASSERT(err == 0, "%s, %d", __func__, __LINE__);
      39                 :            : 
      40                 :            :         return TEST_SUCCESS;
      41                 :            : }
      42                 :            : 
      43                 :            :  /* Test Case to check failures when memzone init is not done */
      44                 :            : static int
      45                 :          1 : test_metrics_without_init(void)
      46                 :            : {
      47                 :            :         int err = 0;
      48                 :          1 :         const uint64_t  value[REG_METRIC_COUNT] = {0};
      49                 :          1 :         const char * const mnames[] = {
      50                 :            :                 "mean_bits_in", "mean_bits_out",
      51                 :            :                 "peak_bits_in", "peak_bits_out",
      52                 :            :         };
      53                 :            : 
      54                 :            :         /* Failure Test: Checking for memzone initialization */
      55                 :          1 :         err = rte_metrics_reg_name("peak_bits_in");
      56         [ -  + ]:          1 :         TEST_ASSERT(err == -EIO, "%s, %d", __func__, __LINE__);
      57                 :            : 
      58                 :          1 :         err = rte_metrics_reg_names(&mnames[0], 1);
      59         [ -  + ]:          1 :         TEST_ASSERT(err == -EIO, "%s, %d", __func__, __LINE__);
      60                 :            : 
      61                 :          1 :         err = rte_metrics_update_value(RTE_METRICS_GLOBAL, KEY, VALUE);
      62         [ -  + ]:          1 :         TEST_ASSERT(err == -EIO, "%s, %d", __func__, __LINE__);
      63                 :            : 
      64                 :          1 :         err = rte_metrics_update_values(RTE_METRICS_GLOBAL, KEY, &value[0], 4);
      65         [ -  + ]:          1 :         TEST_ASSERT(err == -EIO, "%s, %d", __func__, __LINE__);
      66                 :            : 
      67                 :          1 :         err = rte_metrics_get_names(NULL, 0);
      68         [ -  + ]:          1 :         TEST_ASSERT(err == -EIO, "%s, %d", __func__, __LINE__);
      69                 :            : 
      70                 :          1 :         err = rte_metrics_get_values(RTE_METRICS_GLOBAL, NULL, 0);
      71         [ -  + ]:          1 :         TEST_ASSERT(err == -EIO, "%s, %d", __func__, __LINE__);
      72                 :            : 
      73                 :            :         return TEST_SUCCESS;
      74                 :            : }
      75                 :            : 
      76                 :            : /* Test Case to validate registering a single metric */
      77                 :            : static int
      78                 :          1 : test_metrics_reg_name_with_validname(void)
      79                 :            : {
      80                 :            :         int err = 0;
      81                 :            : 
      82                 :            :         /* Test to register the new metric name */
      83                 :          1 :         err = rte_metrics_reg_name("peak_bits_out");
      84         [ -  + ]:          1 :         TEST_ASSERT(err >= 0, "%s, %d", __func__, __LINE__);
      85                 :            : 
      86                 :            :         /* Test to register the same metric name */
      87                 :          1 :         err = rte_metrics_reg_name("peak_bits_out");
      88         [ -  + ]:          1 :         TEST_ASSERT(err >= 0, "%s, %d", __func__, __LINE__);
      89                 :            : 
      90                 :            :         /* Test case to validate registering a invalid metric */
      91                 :          1 :         err = rte_metrics_reg_name(NULL);
      92         [ -  + ]:          1 :         TEST_ASSERT(err == -EINVAL, "%s, %d", __func__, __LINE__);
      93                 :            : 
      94                 :            :         return TEST_SUCCESS;
      95                 :            : }
      96                 :            : 
      97                 :            : /* Test case to validate registering a list of valid  metric names */
      98                 :            : static int
      99                 :          1 : test_metrics_reg_names(void)
     100                 :            : {
     101                 :            :         int err = 0;
     102                 :          1 :         const char * const mnames[] = {
     103                 :            :                 "mean_bits_in", "mean_bits_out",
     104                 :            :                 "peak_bits_in", "peak_bits_out",
     105                 :            :                 };
     106                 :            : 
     107                 :            :         /* Success Test: valid array and count size */
     108                 :          1 :         err = rte_metrics_reg_names(&mnames[0], RTE_DIM(mnames));
     109         [ -  + ]:          1 :         TEST_ASSERT(err >= 0, "%s, %d", __func__, __LINE__);
     110                 :            : 
     111                 :            :         return TEST_SUCCESS;
     112                 :            : }
     113                 :            : 
     114                 :            : /* Test case to validate update a metric */
     115                 :            : static int
     116                 :          1 : test_metrics_update_value(void)
     117                 :            : {
     118                 :            :         int err = 0;
     119                 :            : 
     120                 :            :         /* Successful Test: Valid port_id, key and value */
     121                 :          1 :         err = rte_metrics_update_value(RTE_METRICS_GLOBAL, KEY, VALUE);
     122         [ -  + ]:          1 :         TEST_ASSERT(err >= 0, "%s, %d", __func__, __LINE__);
     123                 :            : 
     124                 :            :         /* Successful Test: Valid port_id other than RTE_METRICS_GLOBAL, key
     125                 :            :          * and value
     126                 :            :          */
     127                 :          1 :         err = rte_metrics_update_value(9, KEY, VALUE);
     128         [ -  + ]:          1 :         TEST_ASSERT(err >= 0, "%s, %d", __func__, __LINE__);
     129                 :            : 
     130                 :            :         /* Failed Test: Invalid port_id with lower value */
     131                 :          1 :         err = rte_metrics_update_value(-2, KEY, VALUE);
     132         [ -  + ]:          1 :         TEST_ASSERT(err == -EINVAL, "%s, %d", __func__, __LINE__);
     133                 :            : 
     134                 :            :         /* Failed Test: Invalid port_id with higher value */
     135                 :          1 :         err = rte_metrics_update_value(39, KEY, VALUE);
     136         [ -  + ]:          1 :         TEST_ASSERT(err == -EINVAL, "%s, %d", __func__, __LINE__);
     137                 :            : 
     138                 :            :         /* Failed Test: valid port id, value with invalid key */
     139                 :          1 :         err = rte_metrics_update_value(RTE_METRICS_GLOBAL, KEY+12, VALUE);
     140         [ -  + ]:          1 :         TEST_ASSERT(err < 0, "%s, %d", __func__, __LINE__);
     141                 :            : 
     142                 :            :         return TEST_SUCCESS;
     143                 :            : }
     144                 :            : 
     145                 :            : /* Test case to validate update a list of  metrics  */
     146                 :            : static int
     147                 :          1 : test_metrics_update_values(void)
     148                 :            : {
     149                 :            :         int err = 0;
     150                 :          1 :         const uint64_t  value[REG_METRIC_COUNT] = {1, 2, 3, 4, 5, 6};
     151                 :            : 
     152                 :            :         /* Successful Test: update metrics with first set */
     153                 :          1 :         err = rte_metrics_update_values(RTE_METRICS_GLOBAL, 0,
     154                 :            :                         &value[0], 1);
     155         [ -  + ]:          1 :         TEST_ASSERT(err >= 0, "%s, %d", __func__, __LINE__);
     156                 :            : 
     157                 :            :         /* Successful Test: update metrics with second set */
     158                 :          1 :         err = rte_metrics_update_values(RTE_METRICS_GLOBAL, 1,
     159                 :            :                         &value[1], 1);
     160         [ -  + ]:          1 :         TEST_ASSERT(err >= 0, "%s, %d", __func__, __LINE__);
     161                 :            : 
     162                 :            :         /* Successful Test: update metrics with third set */
     163                 :          1 :         err = rte_metrics_update_values(RTE_METRICS_GLOBAL, 2,
     164                 :            :                         &value[2], 4);
     165         [ -  + ]:          1 :         TEST_ASSERT(err >= 0, "%s, %d", __func__, __LINE__);
     166                 :            : 
     167                 :            :         /* Failed Test: Invalid count size */
     168                 :          1 :         err = rte_metrics_update_values(RTE_METRICS_GLOBAL,
     169                 :            :                          KEY, &value[0], RTE_DIM(value));
     170         [ -  + ]:          1 :         TEST_ASSERT(err < 0, "%s, %d", __func__, __LINE__);
     171                 :            : 
     172                 :            :         /* Failed Test: Invalid port_id(lower value) and valid data */
     173                 :          1 :         err = rte_metrics_update_values(-2, KEY, &value[0], RTE_DIM(value));
     174         [ -  + ]:          1 :         TEST_ASSERT(err == -EINVAL, "%s, %d", __func__, __LINE__);
     175                 :            : 
     176                 :            :         /* Failed Test: Invalid port_id(higher value) and valid data */
     177                 :          1 :         err = rte_metrics_update_values(39, 1, &value[0], RTE_DIM(value));
     178         [ -  + ]:          1 :         TEST_ASSERT(err == -EINVAL, "%s, %d", __func__, __LINE__);
     179                 :            : 
     180                 :            :         /* Failed Test: Invalid array */
     181                 :          1 :         err = rte_metrics_update_values(RTE_METRICS_GLOBAL,
     182                 :            :                          KEY, NULL, RTE_DIM(value));
     183         [ -  + ]:          1 :         TEST_ASSERT(err == -EINVAL, "%s, %d", __func__, __LINE__);
     184                 :            : 
     185                 :            :         return TEST_SUCCESS;
     186                 :            : }
     187                 :            : 
     188                 :            : /* Test to validate get metric name-key lookup table */
     189                 :            : static int
     190                 :          1 : test_metrics_get_names(void)
     191                 :            : {
     192                 :            :         int err = 0;
     193                 :            :         struct rte_metric_name metrics[METRIC_LESSER_COUNT];
     194                 :            :         struct rte_metric_name success_metrics[REG_METRIC_COUNT];
     195                 :            : 
     196                 :            :         /* Successful Test: Invalid array list */
     197                 :          1 :         err = rte_metrics_get_names(NULL, 0);
     198         [ -  + ]:          1 :         TEST_ASSERT(err >= 0, "%s, %d", __func__, __LINE__);
     199                 :            : 
     200                 :            :         /* Successful Test: Valid array list, Correct Count Stats same
     201                 :            :          * as memzone stats
     202                 :            :          */
     203                 :          1 :         err = rte_metrics_get_names(success_metrics, REG_METRIC_COUNT);
     204         [ -  + ]:          1 :         TEST_ASSERT(err >= 0, "%s, %d", __func__, __LINE__);
     205                 :            : 
     206                 :            :         /* Successful Test: Valid array list, Increase Count Stats than
     207                 :            :          * memzone stats
     208                 :            :          */
     209                 :          1 :         err = rte_metrics_get_names(success_metrics, REG_METRIC_COUNT+5);
     210         [ -  + ]:          1 :         TEST_ASSERT(err >= 0, "%s, %d", __func__, __LINE__);
     211                 :            : 
     212                 :            :         /* Successful Test, Not update results:
     213                 :            :          * Invalid array list, Lesser Count Stats than allocated stats
     214                 :            :          */
     215                 :          1 :         err = rte_metrics_get_names(metrics, METRIC_LESSER_COUNT);
     216         [ -  + ]:          1 :         TEST_ASSERT(err >= 0, "%s, %d", __func__, __LINE__);
     217                 :            : 
     218                 :            :         return TEST_SUCCESS;
     219                 :            : }
     220                 :            : 
     221                 :            : /* Test to validate get list of metric values  */
     222                 :            : static int
     223                 :          1 : test_metrics_get_values(void)
     224                 :            : {
     225                 :            :         int i = 0;
     226                 :            :         int err = 0;
     227                 :            :         struct rte_metric_value getvalues[REG_METRIC_COUNT];
     228                 :            : 
     229                 :            :         size_t m_size = sizeof(struct rte_metric_value);
     230         [ +  + ]:          7 :         for (i = 0; i < REG_METRIC_COUNT; i++)
     231                 :          6 :                 memset(&getvalues[i], 0, m_size);
     232                 :            : 
     233                 :            :         /* Successful Test, Not update results: valid arguments
     234                 :            :          * count lessthan the memzone stats
     235                 :            :          */
     236                 :          1 :         err = rte_metrics_get_values(RTE_METRICS_GLOBAL, getvalues,
     237                 :            :                          METRIC_LESSER_COUNT);
     238         [ -  + ]:          1 :         TEST_ASSERT(err >= 0, "%s, %d", __func__, __LINE__);
     239                 :            : 
     240                 :            :         /* Successful Test, update results: valid arguments */
     241                 :          1 :         err = rte_metrics_get_values(RTE_METRICS_GLOBAL, getvalues,
     242                 :            :                          REG_METRIC_COUNT);
     243         [ -  + ]:          1 :         TEST_ASSERT(err >= 0, "%s, %d", __func__, __LINE__);
     244                 :            : 
     245                 :            :         /* Successful Test : valid arguments count greaterthan the
     246                 :            :          * memzone stats
     247                 :            :          */
     248                 :          1 :         err = rte_metrics_get_values(RTE_METRICS_GLOBAL, getvalues,
     249                 :            :                         REG_METRIC_COUNT+2);
     250         [ -  + ]:          1 :         TEST_ASSERT(err >= 0, "%s, %d", __func__, __LINE__);
     251                 :            : 
     252                 :            :         /* Failure Test: Invalid port_id(lower value) with correct values
     253                 :            :          * and  Capacity
     254                 :            :          */
     255                 :          1 :         err = rte_metrics_get_values(-2, getvalues, REG_METRIC_COUNT);
     256         [ -  + ]:          1 :         TEST_ASSERT(err == -EINVAL, "%s, %d", __func__, __LINE__);
     257                 :            : 
     258                 :            :         /* Failure Test: Invalid port_id(higher value) with correct values
     259                 :            :          * and  Capacity
     260                 :            :          */
     261                 :          1 :         err = rte_metrics_get_values(33, getvalues, REG_METRIC_COUNT);
     262         [ -  + ]:          1 :         TEST_ASSERT(err == -EINVAL, "%s, %d", __func__, __LINE__);
     263                 :            : 
     264                 :            :         /* Successful Test: valid port_id with incorrect values and  valid
     265                 :            :          * capacity
     266                 :            :          */
     267                 :          1 :         err = rte_metrics_get_values(RTE_METRICS_GLOBAL, NULL,
     268                 :            :                          REG_METRIC_COUNT);
     269         [ -  + ]:          1 :         TEST_ASSERT(err >= 0, "%s, %d", __func__, __LINE__);
     270                 :            : 
     271                 :            :         return TEST_SUCCESS;
     272                 :            : }
     273                 :            : 
     274                 :            : static struct unit_test_suite metrics_testsuite  = {
     275                 :            :         .suite_name = "Metrics Unit Test Suite",
     276                 :            :         .setup = NULL,
     277                 :            :         .teardown = NULL,
     278                 :            :         .unit_test_cases = {
     279                 :            :                 /* Test Case 1: Test to check all metric APIs without
     280                 :            :                  * metrics init
     281                 :            :                  */
     282                 :            :                 TEST_CASE(test_metrics_without_init),
     283                 :            : 
     284                 :            :                 /* TEST CASE 2: Test to register valid metrics*/
     285                 :            :                 TEST_CASE_ST(test_metrics_init, NULL,
     286                 :            :                                 test_metrics_reg_name_with_validname),
     287                 :            : 
     288                 :            :                 /* TEST CASE 3: Test to register list of metrics with valid
     289                 :            :                  * names and valid count size, invalid names and invalid
     290                 :            :                  * count size
     291                 :            :                  */
     292                 :            :                 TEST_CASE(test_metrics_reg_names),
     293                 :            : 
     294                 :            :                 /* TEST CASE 4: Test to register a update value with valid port
     295                 :            :                  * id and invalid port id
     296                 :            :                  */
     297                 :            :                 TEST_CASE(test_metrics_update_value),
     298                 :            : 
     299                 :            :                 /* TEST CASE 5: Test to register update list of values with
     300                 :            :                  * valid port id, key, value, count size and invalid port id,
     301                 :            :                  * key, value, count size
     302                 :            :                  */
     303                 :            :                 TEST_CASE(test_metrics_update_values),
     304                 :            : 
     305                 :            :                 /* TEST CASE 6: Test to get metric names-key with valid
     306                 :            :                  * array list, count size and invalid array list, count size
     307                 :            :                  */
     308                 :            :                 TEST_CASE(test_metrics_get_names),
     309                 :            : 
     310                 :            :                 /* TEST CASE 7: Test to get list of metric values with valid
     311                 :            :                  * port id, array list, count size and invalid port id,
     312                 :            :                  * arraylist, count size
     313                 :            :                  */
     314                 :            :                 TEST_CASE(test_metrics_get_values),
     315                 :            : 
     316                 :            :                 /* TEST CASE 8: Test to unregister metrics*/
     317                 :            :                 TEST_CASE(test_metrics_deinitialize),
     318                 :            : 
     319                 :            :                 TEST_CASES_END()
     320                 :            :         }
     321                 :            : };
     322                 :            : 
     323                 :            : static int
     324                 :          1 : test_metrics(void)
     325                 :            : {
     326                 :          1 :         return unit_test_suite_runner(&metrics_testsuite);
     327                 :            : }
     328                 :            : 
     329                 :        252 : REGISTER_FAST_TEST(metrics_autotest, true, true, test_metrics);

Generated by: LCOV version 1.14