~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/tools/counter/counter_example.c

Version: ~ [ linux-6.11.5 ] ~ [ linux-6.10.14 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.58 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.114 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.169 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.228 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.284 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.322 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.9 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 // SPDX-License-Identifier: GPL-2.0-only
  2 /* Counter - example userspace application
  3  *
  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.
  8  *
  9  * Copyright (C) 2021 William Breathitt Gray
 10  */
 11 #include <errno.h>
 12 #include <fcntl.h>
 13 #include <linux/counter.h>
 14 #include <stdio.h>
 15 #include <string.h>
 16 #include <sys/ioctl.h>
 17 #include <unistd.h>
 18 
 19 static struct counter_watch watches[2] = {
 20         {
 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 */
 28                 .channel = 0,
 29         },
 30         {
 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 */
 38                 .channel = 0,
 39         },
 40 };
 41 
 42 int main(void)
 43 {
 44         int fd;
 45         int ret;
 46         int i;
 47         struct counter_event event_data[2];
 48 
 49         fd = open("/dev/counter0", O_RDWR);
 50         if (fd == -1) {
 51                 perror("Unable to open /dev/counter0");
 52                 return 1;
 53         }
 54 
 55         for (i = 0; i < 2; i++) {
 56                 ret = ioctl(fd, COUNTER_ADD_WATCH_IOCTL, watches + i);
 57                 if (ret == -1) {
 58                         fprintf(stderr, "Error adding watches[%d]: %s\n", i,
 59                                 strerror(errno));
 60                         return 1;
 61                 }
 62         }
 63         ret = ioctl(fd, COUNTER_ENABLE_EVENTS_IOCTL);
 64         if (ret == -1) {
 65                 perror("Error enabling events");
 66                 return 1;
 67         }
 68 
 69         for (;;) {
 70                 ret = read(fd, event_data, sizeof(event_data));
 71                 if (ret == -1) {
 72                         perror("Failed to read event data");
 73                         return 1;
 74                 }
 75 
 76                 if (ret != sizeof(event_data)) {
 77                         fprintf(stderr, "Failed to read event data\n");
 78                         return -EIO;
 79                 }
 80 
 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));
 89         }
 90 
 91         return 0;
 92 }
 93 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php