aboutsummaryrefslogtreecommitdiff
path: root/tools/counter/counter_watch_events.c
blob: 107631e0f2e315ae66f2150c0e7c1e1bc9a026fc (plain) (blame)
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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
// SPDX-License-Identifier: GPL-2.0-only
/*
 * Counter Watch Events - Test various counter watch events in a userspace application
 *
 * Copyright (C) STMicroelectronics 2023 - All Rights Reserved
 * Author: Fabrice Gasnier <fabrice.gasnier@foss.st.com>.
 */

#include <errno.h>
#include <fcntl.h>
#include <getopt.h>
#include <linux/counter.h>
#include <linux/kernel.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/ioctl.h>
#include <unistd.h>

static struct counter_watch simple_watch[] = {
	{
		/* Component data: Count 0 count */
		.component.type = COUNTER_COMPONENT_COUNT,
		.component.scope = COUNTER_SCOPE_COUNT,
		.component.parent = 0,
		/* Event type: overflow or underflow */
		.event = COUNTER_EVENT_OVERFLOW_UNDERFLOW,
		/* Device event channel 0 */
		.channel = 0,
	},
};

static const char * const counter_event_type_name[] = {
	"COUNTER_EVENT_OVERFLOW",
	"COUNTER_EVENT_UNDERFLOW",
	"COUNTER_EVENT_OVERFLOW_UNDERFLOW",
	"COUNTER_EVENT_THRESHOLD",
	"COUNTER_EVENT_INDEX",
	"COUNTER_EVENT_CHANGE_OF_STATE",
	"COUNTER_EVENT_CAPTURE",
};

static const char * const counter_component_type_name[] = {
	"COUNTER_COMPONENT_NONE",
	"COUNTER_COMPONENT_SIGNAL",
	"COUNTER_COMPONENT_COUNT",
	"COUNTER_COMPONENT_FUNCTION",
	"COUNTER_COMPONENT_SYNAPSE_ACTION",
	"COUNTER_COMPONENT_EXTENSION",
};

static const char * const counter_scope_name[] = {
	"COUNTER_SCOPE_DEVICE",
	"COUNTER_SCOPE_SIGNAL",
	"COUNTER_SCOPE_COUNT",
};

static void print_watch(struct counter_watch *watch, int nwatch)
{
	int i;

	/* prints the watch array in C-like structure */
	printf("watch[%d] = {\n", nwatch);
	for (i = 0; i < nwatch; i++) {
		printf(" [%d] =\t{\n"
		       "\t\t.component.type = %s\n"
		       "\t\t.component.scope = %s\n"
		       "\t\t.component.parent = %d\n"
		       "\t\t.component.id = %d\n"
		       "\t\t.event = %s\n"
		       "\t\t.channel = %d\n"
		       "\t},\n",
		       i,
		       counter_component_type_name[watch[i].component.type],
		       counter_scope_name[watch[i].component.scope],
		       watch[i].component.parent,
		       watch[i].component.id,
		       counter_event_type_name[watch[i].event],
		       watch[i].channel);
	}
	printf("};\n");
}

static void print_usage(void)
{
	fprintf(stderr, "Usage:\n\n"
		"counter_watch_events [options] [-w <watchoptions>]\n"
		"counter_watch_events [options] [-w <watch1 options>] [-w <watch2 options>]...\n"
		"\n"
		"When no --watch option has been provided, simple watch example is used:\n"
		"counter_watch_events [options] -w comp_count,scope_count,evt_ovf_udf\n"
		"\n"
		"Test various watch events for given counter device.\n"
		"\n"
		"Options:\n"
		"  -d, --debug                Prints debug information\n"
		"  -h, --help                 Prints usage\n"
		"  -n, --device-num <n>       Use /dev/counter<n> [default: /dev/counter0]\n"
		"  -l, --loop <n>             Loop for <n> events [default: 0 (forever)]\n"
		"  -w, --watch <watchoptions> comma-separated list of watch options\n"
		"\n"
		"Watch options:\n"
		"  scope_device               (COUNTER_SCOPE_DEVICE) [default: scope_device]\n"
		"  scope_signal               (COUNTER_SCOPE_SIGNAL)\n"
		"  scope_count                (COUNTER_SCOPE_COUNT)\n"
		"\n"
		"  comp_none                  (COUNTER_COMPONENT_NONE) [default: comp_none]\n"
		"  comp_signal                (COUNTER_COMPONENT_SIGNAL)\n"
		"  comp_count                 (COUNTER_COMPONENT_COUNT)\n"
		"  comp_function              (COUNTER_COMPONENT_FUNCTION)\n"
		"  comp_synapse_action        (COUNTER_COMPONENT_SYNAPSE_ACTION)\n"
		"  comp_extension             (COUNTER_COMPONENT_EXTENSION)\n"
		"\n"
		"  evt_ovf                    (COUNTER_EVENT_OVERFLOW) [default: evt_ovf]\n"
		"  evt_udf                    (COUNTER_EVENT_UNDERFLOW)\n"
		"  evt_ovf_udf                (COUNTER_EVENT_OVERFLOW_UNDERFLOW)\n"
		"  evt_threshold              (COUNTER_EVENT_THRESHOLD)\n"
		"  evt_index                  (COUNTER_EVENT_INDEX)\n"
		"  evt_change_of_state        (COUNTER_EVENT_CHANGE_OF_STATE)\n"
		"  evt_capture                (COUNTER_EVENT_CAPTURE)\n"
		"\n"
		"  chan=<n>                   channel <n> for this watch [default: 0]\n"
		"  id=<n>                     component id <n> for this watch [default: 0]\n"
		"  parent=<n>                 component parent <n> for this watch [default: 0]\n"
		"\n"
		"Example with two watched events:\n\n"
		"counter_watch_events -d \\\n"
		"\t-w comp_count,scope_count,evt_ovf_udf \\\n"
		"\t-w comp_extension,scope_count,evt_capture,id=7,chan=3\n"
		);
}

static const struct option longopts[] = {
	{ "debug",		no_argument,       0, 'd' },
	{ "help",		no_argument,       0, 'h' },
	{ "device-num",		required_argument, 0, 'n' },
	{ "loop",		required_argument, 0, 'l' },
	{ "watch",		required_argument, 0, 'w' },
	{ },
};

/* counter watch subopts */
enum {
	WATCH_SCOPE_DEVICE,
	WATCH_SCOPE_SIGNAL,
	WATCH_SCOPE_COUNT,
	WATCH_COMPONENT_NONE,
	WATCH_COMPONENT_SIGNAL,
	WATCH_COMPONENT_COUNT,
	WATCH_COMPONENT_FUNCTION,
	WATCH_COMPONENT_SYNAPSE_ACTION,
	WATCH_COMPONENT_EXTENSION,
	WATCH_EVENT_OVERFLOW,
	WATCH_EVENT_UNDERFLOW,
	WATCH_EVENT_OVERFLOW_UNDERFLOW,
	WATCH_EVENT_THRESHOLD,
	WATCH_EVENT_INDEX,
	WATCH_EVENT_CHANGE_OF_STATE,
	WATCH_EVENT_CAPTURE,
	WATCH_CHANNEL,
	WATCH_ID,
	WATCH_PARENT,
	WATCH_SUBOPTS_MAX,
};

static char * const counter_watch_subopts[WATCH_SUBOPTS_MAX + 1] = {
	/* component.scope */
	[WATCH_SCOPE_DEVICE] = "scope_device",
	[WATCH_SCOPE_SIGNAL] = "scope_signal",
	[WATCH_SCOPE_COUNT] = "scope_count",
	/* component.type */
	[WATCH_COMPONENT_NONE] = "comp_none",
	[WATCH_COMPONENT_SIGNAL] = "comp_signal",
	[WATCH_COMPONENT_COUNT] = "comp_count",
	[WATCH_COMPONENT_FUNCTION] = "comp_function",
	[WATCH_COMPONENT_SYNAPSE_ACTION] = "comp_synapse_action",
	[WATCH_COMPONENT_EXTENSION] = "comp_extension",
	/* event */
	[WATCH_EVENT_OVERFLOW] = "evt_ovf",
	[WATCH_EVENT_UNDERFLOW] = "evt_udf",
	[WATCH_EVENT_OVERFLOW_UNDERFLOW] = "evt_ovf_udf",
	[WATCH_EVENT_THRESHOLD] = "evt_threshold",
	[WATCH_EVENT_INDEX] = "evt_index",
	[WATCH_EVENT_CHANGE_OF_STATE] = "evt_change_of_state",
	[WATCH_EVENT_CAPTURE] = "evt_capture",
	/* channel, id, parent */
	[WATCH_CHANNEL] = "chan",
	[WATCH_ID] = "id",
	[WATCH_PARENT] = "parent",
	/* Empty entry ends the opts array */
	NULL
};

int main(int argc, char **argv)
{
	int c, fd, i, ret, rc = 0, debug = 0, loop = 0, dev_num = 0, nwatch = 0;
	struct counter_event event_data;
	char *device_name = NULL, *subopts, *value;
	struct counter_watch *watches;

	/*
	 * 1st pass:
	 * - list watch events number to allocate the watch array.
	 * - parse normal options (other than watch options)
	 */
	while ((c = getopt_long(argc, argv, "dhn:l:w:", longopts, NULL)) != -1) {
		switch (c) {
		case 'd':
			debug = 1;
			break;
		case 'h':
			print_usage();
			return EXIT_SUCCESS;
		case 'n':
			dev_num = strtoul(optarg, NULL, 10);
			if (errno) {
				perror("strtol failed: --device-num <n>\n");
				return EXIT_FAILURE;
			}
			break;
		case 'l':
			loop = strtol(optarg, NULL, 10);
			if (errno) {
				perror("strtol failed: --loop <n>\n");
				return EXIT_FAILURE;
			}
			break;
		case 'w':
			nwatch++;
			break;
		default:
			return EXIT_FAILURE;
		}
	}

	if (nwatch) {
		watches = calloc(nwatch, sizeof(*watches));
		if (!watches) {
			perror("Error allocating watches\n");
			return EXIT_FAILURE;
		}
	} else {
		/* default to simple watch example */
		watches = simple_watch;
		nwatch = ARRAY_SIZE(simple_watch);
	}

	/* 2nd pass: parse watch sub-options to fill in watch array */
	optind = 1;
	i = 0;
	while ((c = getopt_long(argc, argv, "dhn:l:w:", longopts, NULL)) != -1) {
		switch (c) {
		case 'w':
			subopts = optarg;
			while (*subopts != '\0') {
				ret = getsubopt(&subopts, counter_watch_subopts, &value);
				switch (ret) {
				case WATCH_SCOPE_DEVICE:
				case WATCH_SCOPE_SIGNAL:
				case WATCH_SCOPE_COUNT:
					/* match with counter_scope */
					watches[i].component.scope = ret;
					break;
				case WATCH_COMPONENT_NONE:
				case WATCH_COMPONENT_SIGNAL:
				case WATCH_COMPONENT_COUNT:
				case WATCH_COMPONENT_FUNCTION:
				case WATCH_COMPONENT_SYNAPSE_ACTION:
				case WATCH_COMPONENT_EXTENSION:
					/* match counter_component_type: subtract enum value */
					ret -= WATCH_COMPONENT_NONE;
					watches[i].component.type = ret;
					break;
				case WATCH_EVENT_OVERFLOW:
				case WATCH_EVENT_UNDERFLOW:
				case WATCH_EVENT_OVERFLOW_UNDERFLOW:
				case WATCH_EVENT_THRESHOLD:
				case WATCH_EVENT_INDEX:
				case WATCH_EVENT_CHANGE_OF_STATE:
				case WATCH_EVENT_CAPTURE:
					/* match counter_event_type: subtract enum value */
					ret -= WATCH_EVENT_OVERFLOW;
					watches[i].event = ret;
					break;
				case WATCH_CHANNEL:
					if (!value) {
						fprintf(stderr, "Invalid chan=<number>\n");
						rc = EXIT_FAILURE;
						goto err_free_watches;
					}
					watches[i].channel = strtoul(value, NULL, 10);
					if (errno) {
						perror("strtoul failed: chan=<number>\n");
						rc = EXIT_FAILURE;
						goto err_free_watches;
					}
					break;
				case WATCH_ID:
					if (!value) {
						fprintf(stderr, "Invalid id=<number>\n");
						rc = EXIT_FAILURE;
						goto err_free_watches;
					}
					watches[i].component.id = strtoul(value, NULL, 10);
					if (errno) {
						perror("strtoul failed: id=<number>\n");
						rc = EXIT_FAILURE;
						goto err_free_watches;
					}
					break;
				case WATCH_PARENT:
					if (!value) {
						fprintf(stderr, "Invalid parent=<number>\n");
						rc = EXIT_FAILURE;
						goto err_free_watches;
					}
					watches[i].component.parent = strtoul(value, NULL, 10);
					if (errno) {
						perror("strtoul failed: parent=<number>\n");
						rc = EXIT_FAILURE;
						goto err_free_watches;
					}
					break;
				default:
					fprintf(stderr, "Unknown suboption '%s'\n", value);
					rc = EXIT_FAILURE;
					goto err_free_watches;
				}
			}
			i++;
			break;
		}
	}

	if (debug)
		print_watch(watches, nwatch);

	ret = asprintf(&device_name, "/dev/counter%d", dev_num);
	if (ret < 0) {
		fprintf(stderr, "asprintf failed\n");
		rc = EXIT_FAILURE;
		goto err_free_watches;
	}

	if (debug)
		printf("Opening %s\n", device_name);

	fd = open(device_name, O_RDWR);
	if (fd == -1) {
		fprintf(stderr, "Unable to open %s: %s\n", device_name, strerror(errno));
		free(device_name);
		rc = EXIT_FAILURE;
		goto err_free_watches;
	}
	free(device_name);

	for (i = 0; i < nwatch; i++) {
		ret = ioctl(fd, COUNTER_ADD_WATCH_IOCTL, watches + i);
		if (ret == -1) {
			fprintf(stderr, "Error adding watches[%d]: %s\n", i,
				strerror(errno));
			rc = EXIT_FAILURE;
			goto err_close;
		}
	}

	ret = ioctl(fd, COUNTER_ENABLE_EVENTS_IOCTL);
	if (ret == -1) {
		perror("Error enabling events");
		rc = EXIT_FAILURE;
		goto err_close;
	}

	for (i = 0; loop <= 0 || i < loop; i++) {
		ret = read(fd, &event_data, sizeof(event_data));
		if (ret == -1) {
			perror("Failed to read event data");
			rc = EXIT_FAILURE;
			goto err_close;
		}

		if (ret != sizeof(event_data)) {
			fprintf(stderr, "Failed to read event data (got: %d)\n", ret);
			rc = EXIT_FAILURE;
			goto err_close;
		}

		printf("Timestamp: %llu\tData: %llu\t event: %s\tch: %d\n",
		       event_data.timestamp, event_data.value,
		       counter_event_type_name[event_data.watch.event],
		       event_data.watch.channel);

		if (event_data.status) {
			fprintf(stderr, "Error %d: %s\n", event_data.status,
				strerror(event_data.status));
		}
	}

err_close:
	close(fd);
err_free_watches:
	if (watches != simple_watch)
		free(watches);

	return rc;
}