blob: 3f36b8dc02db087db87daaab77c05f43e97571f9 [file] [log] [blame]
Colin Cross6a530a32013-09-06 18:03:02 -07001/*
Hareesh Gundu766ae862016-12-12 13:04:37 +05302 * Copyright (C) 2013 The Android Open Source Project
Colin Cross6a530a32013-09-06 18:03:02 -07003 *
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
30struct 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
43int 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));
Colin Cross6a530a32013-09-06 18:03:02 -070048 FILE *fp;
Colin Cross6a530a32013-09-06 18:03:02 -070049 char line[1024];
50 char tmp[128];
51 size_t accounted_size = 0;
52 size_t unaccounted_size = 0;
Colin Cross6a530a32013-09-06 18:03:02 -070053
54 *num_records = ARRAY_SIZE(record_templates);
55
56 /* fastpath to return the necessary number of records */
57 if (allocated_records == 0) {
58 return 0;
59 }
60
61 memcpy(records, record_templates,
62 sizeof(struct memtrack_record) * allocated_records);
63
Arun Kumar K.R4bc21ce2013-11-11 18:45:42 -080064 snprintf(tmp, sizeof(tmp), "/d/kgsl/proc/%d/mem", pid);
Colin Cross6a530a32013-09-06 18:03:02 -070065 fp = fopen(tmp, "r");
66 if (fp == NULL) {
67 return -errno;
68 }
69
Harsh Vardhan Dwivedi0a0cd2a2014-05-05 22:30:02 -060070 /* Go through each line of <pid>/mem file and for every entry of type "gpumem"
71 * check if the gpubuffer entry is usermapped or not. If the entry is usermapped
72 * count the entry as accounted else count the entry as unaccounted.
73 */
Colin Cross6a530a32013-09-06 18:03:02 -070074 while (1) {
Harshdeep Dhatt149b8bc2016-01-29 11:24:31 -070075 unsigned long size, mapsize;
Colin Cross6a530a32013-09-06 18:03:02 -070076 char line_type[7];
Archana Sriram0f26d842017-02-03 14:01:59 +053077 char flags[10];
Fred Fettingera8bfefb2014-08-12 11:15:21 -050078 char line_usage[19];
Santhosh Punugu68155922016-10-28 19:03:03 +053079 int ret, egl_surface_count = 0, egl_image_count = 0;
Colin Cross6a530a32013-09-06 18:03:02 -070080
81 if (fgets(line, sizeof(line), fp) == NULL) {
82 break;
83 }
84
85 /* Format:
Santhosh Punugu68155922016-10-28 19:03:03 +053086 * gpuaddr useraddr size id flags type usage sglen mapsize eglsrf eglimg
87 * 545ba000 545ba000 4096 1 -----pY gpumem arraybuffer 1 4096 0 0
Colin Cross6a530a32013-09-06 18:03:02 -070088 */
Archana Sriram0f26d842017-02-03 14:01:59 +053089 ret = sscanf(line, "%*x %*x %lu %*d %9s %6s %18s %*d %lu %6d %6d\n",
90 &size, flags, line_type, line_usage, &mapsize,
91 &egl_surface_count, &egl_image_count);
Santhosh Punugu68155922016-10-28 19:03:03 +053092 if (ret != 7) {
Colin Cross6a530a32013-09-06 18:03:02 -070093 continue;
94 }
95
Archana Sriramec4cee02017-01-06 17:34:57 +053096 if (size == 0)
97 return -EINVAL;
98
99 if (unaccounted_size + size < size)
100 return -ERANGE;
101
Colin Cross6a530a32013-09-06 18:03:02 -0700102 if (type == MEMTRACK_TYPE_GL && strcmp(line_type, "gpumem") == 0) {
Colin Cross6a530a32013-09-06 18:03:02 -0700103
Harshdeep Dhatt149b8bc2016-01-29 11:24:31 -0700104 if (flags[6] == 'Y') {
Archana Sriramec4cee02017-01-06 17:34:57 +0530105 if (accounted_size + mapsize < accounted_size)
106 return -ERANGE;
Harsh Vardhan Dwivedi0a0cd2a2014-05-05 22:30:02 -0600107
Archana Sriramec4cee02017-01-06 17:34:57 +0530108 accounted_size += mapsize;
109
110 if (mapsize > size)
111 return -EINVAL;
112
113 unaccounted_size += size - mapsize;
114 } else
115 unaccounted_size += size;
Colin Cross6a530a32013-09-06 18:03:02 -0700116 } else if (type == MEMTRACK_TYPE_GRAPHICS && strcmp(line_type, "ion") == 0) {
Santhosh Punugu68155922016-10-28 19:03:03 +0530117 if (strcmp(line_usage, "egl_surface") == 0)
Fred Fettingera8bfefb2014-08-12 11:15:21 -0500118 unaccounted_size += size;
Santhosh Punugu68155922016-10-28 19:03:03 +0530119 else if (egl_surface_count == 0)
Archana Sriramec4cee02017-01-06 17:34:57 +0530120 unaccounted_size += size / (egl_image_count ? egl_image_count : 1);
Colin Cross6a530a32013-09-06 18:03:02 -0700121 }
122 }
123
124 if (allocated_records > 0) {
125 records[0].size_in_bytes = accounted_size;
126 }
127 if (allocated_records > 1) {
128 records[1].size_in_bytes = unaccounted_size;
129 }
130
Colin Cross6a530a32013-09-06 18:03:02 -0700131 fclose(fp);
132
133 return 0;
134}