• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright © 2013 Intel Corporation
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21  * IN THE SOFTWARE.
22  *
23  */
24 
25 #include <sys/stat.h>
26 #include <unistd.h>
27 #include <fcntl.h>
28 #include <errno.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <ctype.h>
33 
34 #include "igt_perf.h"
35 
36 #include "gem-interrupts.h"
37 #include "debugfs.h"
38 
debugfs_read(void)39 static long long debugfs_read(void)
40 {
41 	char buf[8192], *b;
42 	int fd, len;
43 
44 	sprintf(buf, "%s/i915_gem_interrupt", debugfs_dri_path);
45 	fd = open(buf, 0);
46 	if (fd < 0)
47 		return -1;
48 
49 	len = read(fd, buf, sizeof(buf)-1);
50 	close(fd);
51 
52 	if (len < 0)
53 		return -1;
54 
55 	buf[len] = '\0';
56 
57 	b = strstr(buf, "Interrupts received:");
58 	if (b == NULL)
59 		return -1;
60 
61 	return strtoull(b + sizeof("Interrupts received:"), 0, 0);
62 }
63 
procfs_read(void)64 static long long procfs_read(void)
65 {
66 	char buf[8192], *b;
67 	int fd, len;
68 	unsigned long long val;
69 
70 /* 44:         51      42446          0          0   PCI-MSI-edge      i915*/
71 	fd = open("/proc/interrupts", 0);
72 	if (fd < 0)
73 		return -1;
74 
75 	len = read(fd, buf, sizeof(buf)-1);
76 	close(fd);
77 
78 	if (len < 0)
79 		return -1;
80 
81 	buf[len] = '\0';
82 
83 	b = strstr(buf, "i915");
84 	if (b == NULL)
85 		return -1;
86 	while (*--b != ':')
87 		;
88 
89 	val = 0;
90 	do {
91 		while (isspace(*++b))
92 			;
93 		if (!isdigit(*b))
94 			break;
95 
96 		val += strtoull(b, &b, 0);
97 	} while(1);
98 
99 	return val;
100 }
101 
interrupts_read(void)102 static long long interrupts_read(void)
103 {
104 	long long val;
105 
106 	val = debugfs_read();
107 	if (val < 0)
108 		val = procfs_read();
109 	return val;
110 }
111 
gem_interrupts_init(struct gem_interrupts * irqs)112 int gem_interrupts_init(struct gem_interrupts *irqs)
113 {
114 	memset(irqs, 0, sizeof(*irqs));
115 
116 	irqs->fd = perf_i915_open(I915_PMU_INTERRUPTS);
117 	if (irqs->fd < 0 && interrupts_read() < 0)
118 		irqs->error = ENODEV;
119 
120 	return irqs->error;
121 }
122 
gem_interrupts_update(struct gem_interrupts * irqs)123 int gem_interrupts_update(struct gem_interrupts *irqs)
124 {
125 	uint64_t val;
126 	int update;
127 
128 	if (irqs->error)
129 		return irqs->error;
130 
131 	if (irqs->fd < 0) {
132 		long long ret;
133 		ret = interrupts_read();
134 		if (ret < 0)
135 			return irqs->error = ENODEV;
136 		else
137 			val = ret;
138 	} else {
139 		uint64_t data[2];
140 
141 		if (read(irqs->fd, data, sizeof(data)) < 0)
142 			return irqs->error = errno;
143 
144 		val = data[0];
145 	}
146 
147 	update = irqs->last_count == 0;
148 	irqs->last_count = irqs->count;
149 	irqs->count = val;
150 	irqs->delta = irqs->count - irqs->last_count;
151 	return update ? EAGAIN : 0;
152 }
153