blob: 14427a737e9c35ef0395e89a09c15ef93aa02f8a [file] [log] [blame]
Chris Wilsonb98bade2013-08-20 21:39:27 +01001/*
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
Chris Wilsonf9a50de2013-08-17 11:12:07 +010025#include <unistd.h>
26#include <fcntl.h>
Chris Wilsone1ed5602013-08-18 18:17:05 +010027#include <errno.h>
28#include <stdio.h>
29#include <stdlib.h>
30#include <string.h>
Chris Wilsonf9a50de2013-08-17 11:12:07 +010031
32#include "gem-objects.h"
Chris Wilson9574cb12013-08-23 15:51:21 +010033#include "debugfs.h"
Chris Wilsonf9a50de2013-08-17 11:12:07 +010034
Chris Wilsone1ed5602013-08-18 18:17:05 +010035/* /sys/kernel/debug/dri/0/i915_gem_objects:
36 * 46 objects, 20107264 bytes
37 * 42 [42] objects, 15863808 [15863808] bytes in gtt
38 * 0 [0] active objects, 0 [0] bytes
39 * 42 [42] inactive objects, 15863808 [15863808] bytes
40 * 0 unbound objects, 0 bytes
41 * 3 purgeable objects, 4456448 bytes
42 * 30 pinned mappable objects, 3821568 bytes
43 * 1 fault mappable objects, 3145728 bytes
44 * 2145386496 [536870912] gtt total
45 *
46 * Xorg: 35 objects, 16347136 bytes (0 active, 12103680 inactive, 0 unbound)
47 */
48
49int gem_objects_init(struct gem_objects *obj)
Chris Wilsonf9a50de2013-08-17 11:12:07 +010050{
Chris Wilsone1ed5602013-08-18 18:17:05 +010051 char buf[8192], *b;
52 int fd, len;
53
54 memset(obj, 0, sizeof(*obj));
Chris Wilsonf9a50de2013-08-17 11:12:07 +010055
Chris Wilson9574cb12013-08-23 15:51:21 +010056 sprintf(buf, "%s/i915_gem_objects", debugfs_path);
57 fd = open(buf, 0);
Chris Wilsone1ed5602013-08-18 18:17:05 +010058 if (fd < 0)
59 return errno;
60 len = read(fd, buf, sizeof(buf)-1);
61 close(fd);
62
63 if (len < 0)
64 return EIO;
65
66 b = strstr(buf, "gtt total");
67 if (b == NULL)
68 return EIO;
69
70 while (*b != '\n')
71 b--;
72
73 sscanf(b, "%ld [%ld]",
74 &obj->max_gtt, &obj->max_aperture);
75
76 return 0;
77}
78
79static void insert_sorted(struct gem_objects *obj,
80 struct gem_objects_comm *comm)
81{
82 struct gem_objects_comm *next, **prev;
83
84 for (prev = &obj->comm; (next = *prev) != NULL; prev = &next->next)
85 if (comm->bytes > next->bytes)
86 break;
87
88 comm->next = *prev;
89 *prev = comm;
90}
91
92int gem_objects_update(struct gem_objects *obj)
93{
94 char buf[8192], *b;
95 struct gem_objects_comm *comm;
96 struct gem_objects_comm *freed;
97 int fd, len, ret;
98
99 freed = obj->comm;
100 obj->comm = NULL;
101
Chris Wilson9574cb12013-08-23 15:51:21 +0100102 sprintf(buf, "%s/i915_gem_objects", debugfs_path);
103 fd = open(buf, 0);
Chris Wilsone1ed5602013-08-18 18:17:05 +0100104 if (fd < 0) {
105 ret = errno;
106 goto done;
107 }
108 len = read(fd, buf, sizeof(buf)-1);
109 close(fd);
110
111 if (len < 0) {
112 ret = EIO;
113 goto done;
Chris Wilsonf9a50de2013-08-17 11:12:07 +0100114 }
115
Chris Wilsone1ed5602013-08-18 18:17:05 +0100116 buf[len] = '\0';
117 while (buf[--len] == '\n')
118 buf[len] = '\0';
119
120 b = buf;
121
122 sscanf(b, "%d objects, %ld bytes",
123 &obj->total_count, &obj->total_bytes);
124
125 b = strchr(b, '\n');
126 sscanf(b, "%*d [%*d] objects, %ld [%ld] bytes in gtt",
127 &obj->total_gtt, &obj->total_aperture);
128
129 ret = 0;
130 b = strchr(b, ':');
131 if (b == NULL)
132 goto done;
133
134 while (*b != '\n')
135 b--;
136
137 do {
138 comm = freed;
139 if (comm)
140 freed = comm->next;
141 else
142 comm = malloc(sizeof(*comm));
143 if (comm == NULL)
144 break;
145
146 /* Xorg: 35 objects, 16347136 bytes (0 active, 12103680 inactive, 0 unbound) */
147 sscanf(++b, "%256s %u objects, %lu bytes",
148 comm->name, &comm->count, &comm->bytes);
149
150 insert_sorted(obj, comm);
151 } while ((b = strchr(b, '\n')) != NULL);
152
153done:
154 while (freed) {
155 comm = freed;
156 freed = comm->next;
157 free(comm);
158 }
159
160 return ret;
Chris Wilsonf9a50de2013-08-17 11:12:07 +0100161}