1 /*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <errno.h>
18 #include <stdbool.h>
19 #include <stdio.h>
20 #include <string.h>
21 #include <sys/mman.h>
22
23 #include <hardware/memtrack.h>
24
25 #include "memtrack_msm.h"
26
27 #define ARRAY_SIZE(x) (sizeof(x)/sizeof(x[0]))
28 #define min(x, y) ((x) < (y) ? (x) : (y))
29
30 struct memtrack_record record_templates[] = {
31 {
32 .flags = MEMTRACK_FLAG_SMAPS_ACCOUNTED |
33 MEMTRACK_FLAG_PRIVATE |
34 MEMTRACK_FLAG_NONSECURE,
35 },
36 {
37 .flags = MEMTRACK_FLAG_SMAPS_UNACCOUNTED |
38 MEMTRACK_FLAG_PRIVATE |
39 MEMTRACK_FLAG_NONSECURE,
40 },
41 };
42
kgsl_memtrack_get_memory(pid_t pid,enum memtrack_type type,struct memtrack_record * records,size_t * num_records)43 int kgsl_memtrack_get_memory(pid_t pid, enum memtrack_type type,
44 struct memtrack_record *records,
45 size_t *num_records)
46 {
47 size_t allocated_records = min(*num_records, ARRAY_SIZE(record_templates));
48 int i;
49 FILE *fp;
50 FILE *smaps_fp = NULL;
51 char line[1024];
52 char tmp[128];
53 size_t accounted_size = 0;
54 size_t unaccounted_size = 0;
55 unsigned long smaps_addr = 0;
56
57 *num_records = ARRAY_SIZE(record_templates);
58
59 /* fastpath to return the necessary number of records */
60 if (allocated_records == 0) {
61 return 0;
62 }
63
64 memcpy(records, record_templates,
65 sizeof(struct memtrack_record) * allocated_records);
66
67 sprintf(tmp, "/d/kgsl/proc/%d/mem", pid);
68 fp = fopen(tmp, "r");
69 if (fp == NULL) {
70 return -errno;
71 }
72
73 if (type == MEMTRACK_TYPE_GL) {
74 sprintf(tmp, "/proc/%d/smaps", pid);
75 smaps_fp = fopen(tmp, "r");
76 if (smaps_fp == NULL) {
77 fclose(fp);
78 return -errno;
79 }
80 }
81
82 while (1) {
83 unsigned long uaddr;
84 unsigned long size;
85 char line_type[7];
86 int ret;
87
88 if (fgets(line, sizeof(line), fp) == NULL) {
89 break;
90 }
91
92 /* Format:
93 * gpuaddr useraddr size id flags type usage sglen
94 * 545ba000 545ba000 4096 1 ----p gpumem arraybuffer 1
95 */
96 ret = sscanf(line, "%*x %lx %lu %*d %*s %6s %*s %*d\n",
97 &uaddr, &size, line_type);
98 if (ret != 3) {
99 continue;
100 }
101
102 if (type == MEMTRACK_TYPE_GL && strcmp(line_type, "gpumem") == 0) {
103 bool accounted = false;
104 /*
105 * We need to cross reference the user address against smaps,
106 * luckily both are sorted.
107 */
108 while (smaps_addr <= uaddr) {
109 unsigned long start;
110 unsigned long end;
111 unsigned long smaps_size;
112
113 if (fgets(line, sizeof(line), smaps_fp) == NULL) {
114 break;
115 }
116
117 if (sscanf(line, "%8lx-%8lx", &start, &end) == 2) {
118 smaps_addr = start;
119 continue;
120 }
121
122 if (smaps_addr != uaddr) {
123 continue;
124 }
125
126 if (sscanf(line, "Rss: %lu kB", &smaps_size) == 1) {
127 if (smaps_size) {
128 accounted = true;
129 accounted_size += size;
130 break;
131 }
132 }
133 }
134 if (!accounted) {
135 unaccounted_size += size;
136 }
137 } else if (type == MEMTRACK_TYPE_GRAPHICS && strcmp(line_type, "ion") == 0) {
138 unaccounted_size += size;
139 }
140 }
141
142 if (allocated_records > 0) {
143 records[0].size_in_bytes = accounted_size;
144 }
145 if (allocated_records > 1) {
146 records[1].size_in_bytes = unaccounted_size;
147 }
148
149 if (smaps_fp)
150 fclose(smaps_fp);
151 fclose(fp);
152
153 return 0;
154 }
155