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