summaryrefslogtreecommitdiffstats
path: root/src/spdk/dpdk/app/test/test_metrics.c
blob: e736019ae4e7cad0174161c29a61fcc8eba5bb5d (plain)
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
/* SPDX-License-Identifier: BSD-3-Clause
 * Copyright(c) 2018 Intel Corporation
 */

#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include <errno.h>

#include <rte_lcore.h>
#include <rte_metrics.h>

#include "test.h"

#define	REG_METRIC_COUNT	6
#define	METRIC_LESSER_COUNT	3
#define	KEY	1
#define	VALUE	1

/* Initializes metric module. This function must be called
 * from a primary process before metrics are used
 */
static int
test_metrics_init(void)
{
	rte_metrics_init(rte_socket_id());
	return TEST_SUCCESS;
}

/* Deinitialize metric module. This function must be called
 * from a primary process after metrics usage is over
 */
static int
test_metrics_deinitialize(void)
{
	int err = 0;
	err = rte_metrics_deinit();
	TEST_ASSERT(err == 0, "%s, %d", __func__, __LINE__);

	return TEST_SUCCESS;
}

 /* Test Case to check failures when memzone init is not done */
static int
test_metrics_without_init(void)
{
	int err = 0;
	const uint64_t  value[REG_METRIC_COUNT] = {0};
	const char * const mnames[] = {
		"mean_bits_in", "mean_bits_out",
		"peak_bits_in", "peak_bits_out",
	};

	/* Failure Test: Checking for memzone initialization */
	err = rte_metrics_reg_name("peak_bits_in");
	TEST_ASSERT(err == -EIO, "%s, %d", __func__, __LINE__);

	err = rte_metrics_reg_names(&mnames[0], 1);
	TEST_ASSERT(err == -EIO, "%s, %d", __func__, __LINE__);

	err = rte_metrics_update_value(RTE_METRICS_GLOBAL, KEY, VALUE);
	TEST_ASSERT(err == -EIO, "%s, %d", __func__, __LINE__);

	err = rte_metrics_update_values(RTE_METRICS_GLOBAL, KEY, &value[0], 4);
	TEST_ASSERT(err == -EIO, "%s, %d", __func__, __LINE__);

	err = rte_metrics_get_names(NULL, 0);
	TEST_ASSERT(err == -EIO, "%s, %d", __func__, __LINE__);

	err = rte_metrics_get_values(RTE_METRICS_GLOBAL, NULL, 0);
	TEST_ASSERT(err == -EIO, "%s, %d", __func__, __LINE__);

	return TEST_SUCCESS;
}

/* Test Case to validate registering a single metric */
static int
test_metrics_reg_name_with_validname(void)
{
	int err = 0;

	/* Test to register the new metric name */
	err = rte_metrics_reg_name("peak_bits_out");
	TEST_ASSERT(err >= 0, "%s, %d", __func__, __LINE__);

	/* Test to register the same metric name */
	err = rte_metrics_reg_name("peak_bits_out");
	TEST_ASSERT(err >= 0, "%s, %d", __func__, __LINE__);

	/* Test case to validate registering a invalid metric */
	err = rte_metrics_reg_name(NULL);
	TEST_ASSERT(err == -EINVAL, "%s, %d", __func__, __LINE__);

	return TEST_SUCCESS;
}

/* Test case to validate registering a list of valid  metric names */
static int
test_metrics_reg_names(void)
{
	int err = 0;
	const char * const mnames[] = {
		"mean_bits_in", "mean_bits_out",
		"peak_bits_in", "peak_bits_out",
		};

	/* Success Test: valid array and count size */
	err = rte_metrics_reg_names(&mnames[0], RTE_DIM(mnames));
	TEST_ASSERT(err >= 0, "%s, %d", __func__, __LINE__);

	return TEST_SUCCESS;
}

/* Test case to validate update a metric */
static int
test_metrics_update_value(void)
{
	int err = 0;

	/* Successful Test: Valid port_id, key and value */
	err = rte_metrics_update_value(RTE_METRICS_GLOBAL, KEY, VALUE);
	TEST_ASSERT(err >= 0, "%s, %d", __func__, __LINE__);

	/* Successful Test: Valid port_id otherthan RTE_METRICS_GLOBAL, key
	 * and value
	 */
	err = rte_metrics_update_value(9, KEY, VALUE);
	TEST_ASSERT(err >= 0, "%s, %d", __func__, __LINE__);

	/* Failed Test: Invalid port_id with lower value */
	err = rte_metrics_update_value(-2, KEY, VALUE);
	TEST_ASSERT(err == -EINVAL, "%s, %d", __func__, __LINE__);

	/* Failed Test: Invalid port_id with higher value */
	err = rte_metrics_update_value(39, KEY, VALUE);
	TEST_ASSERT(err == -EINVAL, "%s, %d", __func__, __LINE__);

	/* Failed Test: valid port id, value with invalid key */
	err = rte_metrics_update_value(RTE_METRICS_GLOBAL, KEY+12, VALUE);
	TEST_ASSERT(err < 0, "%s, %d", __func__, __LINE__);

	return TEST_SUCCESS;
}

/* Test case to validate update a list of  metrics  */
static int
test_metrics_update_values(void)
{
	int err = 0;
	const uint64_t  value[REG_METRIC_COUNT] = {1, 2, 3, 4, 5, 6};

	/* Successful Test: update metrics with first set */
	err = rte_metrics_update_values(RTE_METRICS_GLOBAL, 0,
			&value[0], 1);
	TEST_ASSERT(err >= 0, "%s, %d", __func__, __LINE__);

	/* Successful Test: update metrics with second set */
	err = rte_metrics_update_values(RTE_METRICS_GLOBAL, 1,
			&value[1], 1);
	TEST_ASSERT(err >= 0, "%s, %d", __func__, __LINE__);

	/* Successful Test: update metrics with third set */
	err = rte_metrics_update_values(RTE_METRICS_GLOBAL, 2,
			&value[2], 4);
	TEST_ASSERT(err >= 0, "%s, %d", __func__, __LINE__);

	/* Failed Test: Invalid count size */
	err = rte_metrics_update_values(RTE_METRICS_GLOBAL,
			 KEY, &value[0], RTE_DIM(value));
	TEST_ASSERT(err < 0, "%s, %d", __func__, __LINE__);

	/* Failed Test: Invalid port_id(lower value) and valid data */
	err = rte_metrics_update_values(-2, KEY, &value[0], RTE_DIM(value));
	TEST_ASSERT(err == -EINVAL, "%s, %d", __func__, __LINE__);

	/* Failed Test: Invalid port_id(higher value) and valid data */
	err = rte_metrics_update_values(39, 1, &value[0], RTE_DIM(value));
	TEST_ASSERT(err == -EINVAL, "%s, %d", __func__, __LINE__);

	/* Failed Test: Invalid array */
	err = rte_metrics_update_values(RTE_METRICS_GLOBAL,
			 KEY, NULL, RTE_DIM(value));
	TEST_ASSERT(err == -EINVAL, "%s, %d", __func__, __LINE__);

	return TEST_SUCCESS;
}

/* Test to validate get metric name-key lookup table */
static int
test_metrics_get_names(void)
{
	int err = 0;
	struct rte_metric_name metrics[METRIC_LESSER_COUNT];
	struct rte_metric_name success_metrics[REG_METRIC_COUNT];

	/* Successful Test: Invalid array list */
	err = rte_metrics_get_names(NULL, 0);
	TEST_ASSERT(err >= 0, "%s, %d", __func__, __LINE__);

	/* Successful Test: Valid array list, Correct Count Stats same
	 * as memzone stats
	 */
	err = rte_metrics_get_names(success_metrics, REG_METRIC_COUNT);
	TEST_ASSERT(err >= 0, "%s, %d", __func__, __LINE__);

	/* Successful Test: Valid array list, Increase Count Stats than
	 * memzone stats
	 */
	err = rte_metrics_get_names(success_metrics, REG_METRIC_COUNT+5);
	TEST_ASSERT(err >= 0, "%s, %d", __func__, __LINE__);

	/* Successful Test, Not update results:
	 * Invalid array list, Lesser Count Stats than allocated stats
	 */
	err = rte_metrics_get_names(metrics, METRIC_LESSER_COUNT);
	TEST_ASSERT(err >= 0, "%s, %d", __func__, __LINE__);

	return TEST_SUCCESS;
}

/* Test to validate get list of metric values  */
static int
test_metrics_get_values(void)
{
	int i = 0;
	int err = 0;
	struct rte_metric_value getvalues[REG_METRIC_COUNT];

	size_t m_size = sizeof(struct rte_metric_value);
	for (i = 0; i < REG_METRIC_COUNT; i++)
		memset(&getvalues[i], 0, m_size);

	/* Successful Test, Not update results: valid arguments
	 * count lessthan the memzone stats
	 */
	err = rte_metrics_get_values(RTE_METRICS_GLOBAL, getvalues,
			 METRIC_LESSER_COUNT);
	TEST_ASSERT(err >= 0, "%s, %d", __func__, __LINE__);

	/* Successful Test, update results: valid arguments */
	err = rte_metrics_get_values(RTE_METRICS_GLOBAL, getvalues,
			 REG_METRIC_COUNT);
	TEST_ASSERT(err >= 0, "%s, %d", __func__, __LINE__);

	/* Successful Test : valid arguments count greaterthan the
	 * memzone stats
	 */
	err = rte_metrics_get_values(RTE_METRICS_GLOBAL, getvalues,
			REG_METRIC_COUNT+2);
	TEST_ASSERT(err >= 0, "%s, %d", __func__, __LINE__);

	/* Failure Test: Invalid port_id(lower value) with correct values
	 * and  Capacity
	 */
	err = rte_metrics_get_values(-2, getvalues, REG_METRIC_COUNT);
	TEST_ASSERT(err == -EINVAL, "%s, %d", __func__, __LINE__);

	/* Failure Test: Invalid port_id(higher value) with correct values
	 * and  Capacity
	 */
	err = rte_metrics_get_values(33, getvalues, REG_METRIC_COUNT);
	TEST_ASSERT(err == -EINVAL, "%s, %d", __func__, __LINE__);

	/* Successful Test: valid port_id with incorrect values and  valid
	 * capacity
	 */
	err = rte_metrics_get_values(RTE_METRICS_GLOBAL, NULL,
			 REG_METRIC_COUNT);
	TEST_ASSERT(err >= 0, "%s, %d", __func__, __LINE__);

	return TEST_SUCCESS;
}

static struct unit_test_suite metrics_testsuite  = {
	.suite_name = "Metrics Unit Test Suite",
	.setup = NULL,
	.teardown = NULL,
	.unit_test_cases = {
		/* Test Case 1: Test to check all metric APIs without
		 * metrics init
		 */
		TEST_CASE(test_metrics_without_init),

		/* TEST CASE 2: Test to register valid metrics*/
		TEST_CASE_ST(test_metrics_init, NULL,
				test_metrics_reg_name_with_validname),

		/* TEST CASE 3: Test to register list of metrics with valid
		 * names and valid count size, invalid names and invalid
		 * count size
		 */
		TEST_CASE(test_metrics_reg_names),

		/* TEST CASE 4: Test to register a update value with valid port
		 * id and invalid port id
		 */
		TEST_CASE(test_metrics_update_value),

		/* TEST CASE 5: Test to register update list of values with
		 * valid port id, key, value, count size and invalid port id,
		 * key, value, count size
		 */
		TEST_CASE(test_metrics_update_values),

		/* TEST CASE 6: Test to get metric names-key with valid
		 * array list, count size and invalid array list, count size
		 */
		TEST_CASE(test_metrics_get_names),

		/* TEST CASE 7: Test to get list of metric values with valid
		 * port id, array list, count size and invalid port id,
		 * arraylist, count size
		 */
		TEST_CASE(test_metrics_get_values),

		/* TEST CASE 8: Test to unregister metrics*/
		TEST_CASE(test_metrics_deinitialize),

		TEST_CASES_END()
	}
};

static int
test_metrics(void)
{
	return unit_test_suite_runner(&metrics_testsuite);
}

REGISTER_TEST_COMMAND(metrics_autotest, test_metrics);