blob: b644d732b8cfa2d421913820cd442e8efa8587ed [file] [log] [blame]
Colin Cross6a530a32013-09-06 18:03:02 -07001/*
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
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));
48 int i;
49 FILE *fp;
Colin Cross6a530a32013-09-06 18:03:02 -070050 char line[1024];
51 char tmp[128];
52 size_t accounted_size = 0;
53 size_t unaccounted_size = 0;
54 unsigned long smaps_addr = 0;
55
56 *num_records = ARRAY_SIZE(record_templates);
57
58 /* fastpath to return the necessary number of records */
59 if (allocated_records == 0) {
60 return 0;
61 }
62
63 memcpy(records, record_templates,
64 sizeof(struct memtrack_record) * allocated_records);
65
Arun Kumar K.R4bc21ce2013-11-11 18:45:42 -080066 snprintf(tmp, sizeof(tmp), "/d/kgsl/proc/%d/mem", pid);
Colin Cross6a530a32013-09-06 18:03:02 -070067 fp = fopen(tmp, "r");
68 if (fp == NULL) {
69 return -errno;
70 }
71
Harsh Vardhan Dwivedi0a0cd2a2014-05-05 22:30:02 -060072 /* Go through each line of <pid>/mem file and for every entry of type "gpumem"
73 * check if the gpubuffer entry is usermapped or not. If the entry is usermapped
74 * count the entry as accounted else count the entry as unaccounted.
75 */
Colin Cross6a530a32013-09-06 18:03:02 -070076 while (1) {
Colin Cross6a530a32013-09-06 18:03:02 -070077 unsigned long size;
78 char line_type[7];
Harsh Vardhan Dwivedi0a0cd2a2014-05-05 22:30:02 -060079 char flags[7];
Colin Cross6a530a32013-09-06 18:03:02 -070080 int ret;
81
82 if (fgets(line, sizeof(line), fp) == NULL) {
83 break;
84 }
85
86 /* Format:
87 * gpuaddr useraddr size id flags type usage sglen
Harsh Vardhan Dwivedi0a0cd2a2014-05-05 22:30:02 -060088 * 545ba000 545ba000 4096 1 ----pY gpumem arraybuffer 1
Colin Cross6a530a32013-09-06 18:03:02 -070089 */
Arun Kumar K.R2bdd3d02014-06-20 10:59:46 -070090 ret = sscanf(line, "%*x %*x %lu %*d %6s %6s %*s %*d\n",
Harsh Vardhan Dwivedi0a0cd2a2014-05-05 22:30:02 -060091 &size, flags, line_type);
Colin Cross6a530a32013-09-06 18:03:02 -070092 if (ret != 3) {
93 continue;
94 }
95
96 if (type == MEMTRACK_TYPE_GL && strcmp(line_type, "gpumem") == 0) {
Colin Cross6a530a32013-09-06 18:03:02 -070097
Harsh Vardhan Dwivedi0a0cd2a2014-05-05 22:30:02 -060098 if (flags[6] == 'Y')
99 accounted_size += size;
100 else
Colin Cross6a530a32013-09-06 18:03:02 -0700101 unaccounted_size += size;
Harsh Vardhan Dwivedi0a0cd2a2014-05-05 22:30:02 -0600102
Colin Cross6a530a32013-09-06 18:03:02 -0700103 } else if (type == MEMTRACK_TYPE_GRAPHICS && strcmp(line_type, "ion") == 0) {
104 unaccounted_size += size;
105 }
106 }
107
108 if (allocated_records > 0) {
109 records[0].size_in_bytes = accounted_size;
110 }
111 if (allocated_records > 1) {
112 records[1].size_in_bytes = unaccounted_size;
113 }
114
Colin Cross6a530a32013-09-06 18:03:02 -0700115 fclose(fp);
116
117 return 0;
118}