Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /* Counter - example userspace application
0003  *
0004  * The userspace application opens /dev/counter0, configures the
0005  * COUNTER_EVENT_INDEX event channel 0 to gather Count 0 count and Count
0006  * 1 count, and prints out the data as it becomes available on the
0007  * character device node.
0008  *
0009  * Copyright (C) 2021 William Breathitt Gray
0010  */
0011 #include <errno.h>
0012 #include <fcntl.h>
0013 #include <linux/counter.h>
0014 #include <stdio.h>
0015 #include <string.h>
0016 #include <sys/ioctl.h>
0017 #include <unistd.h>
0018 
0019 static struct counter_watch watches[2] = {
0020     {
0021         /* Component data: Count 0 count */
0022         .component.type = COUNTER_COMPONENT_COUNT,
0023         .component.scope = COUNTER_SCOPE_COUNT,
0024         .component.parent = 0,
0025         /* Event type: Index */
0026         .event = COUNTER_EVENT_INDEX,
0027         /* Device event channel 0 */
0028         .channel = 0,
0029     },
0030     {
0031         /* Component data: Count 1 count */
0032         .component.type = COUNTER_COMPONENT_COUNT,
0033         .component.scope = COUNTER_SCOPE_COUNT,
0034         .component.parent = 1,
0035         /* Event type: Index */
0036         .event = COUNTER_EVENT_INDEX,
0037         /* Device event channel 0 */
0038         .channel = 0,
0039     },
0040 };
0041 
0042 int main(void)
0043 {
0044     int fd;
0045     int ret;
0046     int i;
0047     struct counter_event event_data[2];
0048 
0049     fd = open("/dev/counter0", O_RDWR);
0050     if (fd == -1) {
0051         perror("Unable to open /dev/counter0");
0052         return 1;
0053     }
0054 
0055     for (i = 0; i < 2; i++) {
0056         ret = ioctl(fd, COUNTER_ADD_WATCH_IOCTL, watches + i);
0057         if (ret == -1) {
0058             fprintf(stderr, "Error adding watches[%d]: %s\n", i,
0059                 strerror(errno));
0060             return 1;
0061         }
0062     }
0063     ret = ioctl(fd, COUNTER_ENABLE_EVENTS_IOCTL);
0064     if (ret == -1) {
0065         perror("Error enabling events");
0066         return 1;
0067     }
0068 
0069     for (;;) {
0070         ret = read(fd, event_data, sizeof(event_data));
0071         if (ret == -1) {
0072             perror("Failed to read event data");
0073             return 1;
0074         }
0075 
0076         if (ret != sizeof(event_data)) {
0077             fprintf(stderr, "Failed to read event data\n");
0078             return -EIO;
0079         }
0080 
0081         printf("Timestamp 0: %llu\tCount 0: %llu\n"
0082                "Error Message 0: %s\n"
0083                "Timestamp 1: %llu\tCount 1: %llu\n"
0084                "Error Message 1: %s\n",
0085                event_data[0].timestamp, event_data[0].value,
0086                strerror(event_data[0].status),
0087                event_data[1].timestamp, event_data[1].value,
0088                strerror(event_data[1].status));
0089     }
0090 
0091     return 0;
0092 }