1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Counter - example userspace application
4 * The userspace application opens /dev/counter0, configures the
5 * COUNTER_EVENT_INDEX event channel 0 to gather Count 0 count and Count
6 * 1 count, and prints out the data as it becomes available on the
7 * character device node.
9 * Copyright (C) 2021 William Breathitt Gray
13 #include <linux/counter.h>
16 #include <sys/ioctl.h>
19 static struct counter_watch watches[2] = {
21 /* Component data: Count 0 count */
22 .component.type = COUNTER_COMPONENT_COUNT,
23 .component.scope = COUNTER_SCOPE_COUNT,
24 .component.parent = 0,
25 /* Event type: Index */
26 .event = COUNTER_EVENT_INDEX,
27 /* Device event channel 0 */
31 /* Component data: Count 1 count */
32 .component.type = COUNTER_COMPONENT_COUNT,
33 .component.scope = COUNTER_SCOPE_COUNT,
34 .component.parent = 1,
35 /* Event type: Index */
36 .event = COUNTER_EVENT_INDEX,
37 /* Device event channel 0 */
47 struct counter_event event_data[2];
49 fd = open("/dev/counter0", O_RDWR);
51 perror("Unable to open /dev/counter0");
55 for (i = 0; i < 2; i++) {
56 ret = ioctl(fd, COUNTER_ADD_WATCH_IOCTL, watches + i);
58 fprintf(stderr, "Error adding watches[%d]: %s\n", i,
63 ret = ioctl(fd, COUNTER_ENABLE_EVENTS_IOCTL);
65 perror("Error enabling events");
70 ret = read(fd, event_data, sizeof(event_data));
72 perror("Failed to read event data");
76 if (ret != sizeof(event_data)) {
77 fprintf(stderr, "Failed to read event data\n");
81 printf("Timestamp 0: %llu\tCount 0: %llu\n"
82 "Error Message 0: %s\n"
83 "Timestamp 1: %llu\tCount 1: %llu\n"
84 "Error Message 1: %s\n",
85 event_data[0].timestamp, event_data[0].value,
86 strerror(event_data[0].status),
87 event_data[1].timestamp, event_data[1].value,
88 strerror(event_data[1].status));