xref: /dpdk/app/test/test_cksum_perf.c (revision 99ed5e931f59ebaf48cf4ab045f75466a2c388cd)
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2022 Ericsson AB
3  */
4 
5 #include <stdio.h>
6 
7 #include <rte_common.h>
8 #include <rte_cycles.h>
9 #include <rte_ip.h>
10 #include <rte_malloc.h>
11 #include <rte_random.h>
12 
13 #include "test.h"
14 
15 #define NUM_BLOCKS 10
16 #define ITERATIONS 1000000
17 
18 static const size_t data_sizes[] = { 20, 21, 100, 101, 1500, 1501 };
19 
20 static __rte_noinline uint16_t
21 do_rte_raw_cksum(const void *buf, size_t len)
22 {
23 	return rte_raw_cksum(buf, len);
24 }
25 
26 static void
27 init_block(char *buf, size_t len)
28 {
29 	size_t i;
30 
31 	for (i = 0; i < len; i++)
32 		buf[i] = (char)rte_rand();
33 }
34 
35 static int
36 test_cksum_perf_size_alignment(size_t block_size, bool aligned)
37 {
38 	char *data[NUM_BLOCKS];
39 	char *blocks[NUM_BLOCKS];
40 	unsigned int i;
41 	uint64_t start;
42 	uint64_t end;
43 	/* Floating point to handle low (pseudo-)TSC frequencies */
44 	double block_latency;
45 	double byte_latency;
46 	volatile __rte_unused uint64_t sum = 0;
47 
48 	for (i = 0; i < NUM_BLOCKS; i++) {
49 		data[i] = rte_malloc(NULL, block_size + 1, 0);
50 
51 		if (data[i] == NULL) {
52 			printf("Failed to allocate memory for block\n");
53 			return TEST_FAILED;
54 		}
55 
56 		init_block(data[i], block_size + 1);
57 
58 		blocks[i] = aligned ? data[i] : data[i] + 1;
59 	}
60 
61 	start = rte_rdtsc();
62 
63 	for (i = 0; i < ITERATIONS; i++) {
64 		unsigned int j;
65 		for (j = 0; j < NUM_BLOCKS; j++)
66 			sum += do_rte_raw_cksum(blocks[j], block_size);
67 	}
68 
69 	end = rte_rdtsc();
70 
71 	block_latency = (end - start) / (double)(ITERATIONS * NUM_BLOCKS);
72 	byte_latency = block_latency / block_size;
73 
74 	printf("%-9s %10zd %19.1f %16.2f\n", aligned ? "Aligned" : "Unaligned",
75 	       block_size, block_latency, byte_latency);
76 
77 	for (i = 0; i < NUM_BLOCKS; i++)
78 		rte_free(data[i]);
79 
80 	return TEST_SUCCESS;
81 }
82 
83 static int
84 test_cksum_perf_size(size_t block_size)
85 {
86 	int rc;
87 
88 	rc = test_cksum_perf_size_alignment(block_size, true);
89 	if (rc != TEST_SUCCESS)
90 		return rc;
91 
92 	rc = test_cksum_perf_size_alignment(block_size, false);
93 
94 	return rc;
95 }
96 
97 static int
98 test_cksum_perf(void)
99 {
100 	uint16_t i;
101 
102 	printf("### rte_raw_cksum() performance ###\n");
103 	printf("Alignment  Block size    TSC cycles/block  TSC cycles/byte\n");
104 
105 	for (i = 0; i < RTE_DIM(data_sizes); i++) {
106 		int rc;
107 
108 		rc = test_cksum_perf_size(data_sizes[i]);
109 		if (rc != TEST_SUCCESS)
110 			return rc;
111 	}
112 
113 	return TEST_SUCCESS;
114 }
115 
116 REGISTER_PERF_TEST(cksum_perf_autotest, test_cksum_perf);
117