0001
0002
0003
0004
0005
0006
0007
0008
0009
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
0022 .component.type = COUNTER_COMPONENT_COUNT,
0023 .component.scope = COUNTER_SCOPE_COUNT,
0024 .component.parent = 0,
0025
0026 .event = COUNTER_EVENT_INDEX,
0027
0028 .channel = 0,
0029 },
0030 {
0031
0032 .component.type = COUNTER_COMPONENT_COUNT,
0033 .component.scope = COUNTER_SCOPE_COUNT,
0034 .component.parent = 1,
0035
0036 .event = COUNTER_EVENT_INDEX,
0037
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 }