dring.c revision a633eec7
1/*
2 * Copyright (c) 2016  Intel Corporation.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16#include <tle_dring.h>
17
18static const char *
19str_drb_dummy(const struct tle_dring *dr, const struct tle_drb *db)
20{
21	return (db == &dr->dummy) ? "<dummy>" : "";
22}
23
24static const char *
25str_obj_state(const struct tle_dring *dr, const struct tle_drb *db,
26	uint32_t idx)
27{
28	if (db->start + idx < dr->cons.tail)
29		return "<stale>";
30	else if (db->start + idx >= dr->prod.tail)
31		return "<free>";
32	else
33		return NULL;
34}
35
36static void
37drb_obj_dump(FILE *f, int32_t verb, const struct tle_dring *dr,
38	const struct tle_drb *db, uint32_t idx)
39{
40	const char *st;
41
42	st = str_obj_state(dr, db, idx);
43
44	/* pointer to object is valid, dump it. */
45	if (st == NULL)
46		fprintf(f, "\t\t\t\t%u:%p\n", db->start + idx, db->objs[idx]);
47
48	/* dump in verbose mode only. */
49	else if (verb > 0)
50		fprintf(f, "\t\t\t\t%u:%p%s\n",
51				db->start + idx, db->objs[idx], st);
52}
53
54static void
55drb_dump(FILE *f, int32_t verb, const struct tle_dring *dr,
56	const struct tle_drb *db)
57{
58	uint32_t i;
59
60	fprintf(f, "\t\t@%p%s={\n", db, str_drb_dummy(dr, db));
61	fprintf(f, "\t\t\tnext=%p,\n", db->next);
62	fprintf(f, "\t\t\tsize=%u,\n", db->size);
63	fprintf(f, "\t\t\tstart=%u,\n", db->start);
64
65	fprintf(f, "\t\t\tobjs[]={\n");
66	for (i = 0; i != db->size; i++)
67		drb_obj_dump(f, verb, dr, db, i);
68	fprintf(f, "\t\t\t},\n");
69
70	fprintf(f, "\t\t},\n");
71}
72
73void
74tle_dring_dump(FILE *f, int32_t verb, const struct tle_dring *dr)
75{
76	struct tle_drb *db;
77
78	fprintf(f, "tle_dring@%p={\n", dr);
79	fprintf(f, "\tflags=%#x,\n", dr->flags);
80
81	fprintf(f, "\tprod={,\n");
82	fprintf(f, "\t\thead=%u,\n", dr->prod.head);
83	fprintf(f, "\t\ttail=%u,\n", dr->prod.tail);
84	fprintf(f, "\t\tcrb=%p%s,\n", dr->prod.crb,
85		str_drb_dummy(dr, dr->prod.crb));
86	fprintf(f, "\t},\n");
87
88	fprintf(f, "\tcons={,\n");
89	fprintf(f, "\t\thead=%u,\n", dr->cons.head);
90	fprintf(f, "\t\ttail=%u,\n", dr->cons.tail);
91	fprintf(f, "\t\tcrb=%p%s,\n", dr->cons.crb,
92		str_drb_dummy(dr, dr->cons.crb));
93	fprintf(f, "\t},\n");
94
95	fprintf(f, "\tdrbs[] = {\n");
96	for (db = dr->prod.crb; db != NULL; db = db->next)
97		drb_dump(f, verb, dr, db);
98	fprintf(f, "\t},\n");
99
100	fprintf(f, "};\n");
101}
102