Colin Cross | 6a530a3 | 2013-09-06 18:03:02 -0700 | [diff] [blame] | 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 | |
| 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)); |
Colin Cross | 6a530a3 | 2013-09-06 18:03:02 -0700 | [diff] [blame] | 48 | FILE *fp; |
Colin Cross | 6a530a3 | 2013-09-06 18:03:02 -0700 | [diff] [blame] | 49 | char line[1024]; |
| 50 | char tmp[128]; |
Fred Fettinger | a8bfefb | 2014-08-12 11:15:21 -0500 | [diff] [blame] | 51 | bool is_surfaceflinger = false; |
Colin Cross | 6a530a3 | 2013-09-06 18:03:02 -0700 | [diff] [blame] | 52 | size_t accounted_size = 0; |
| 53 | size_t unaccounted_size = 0; |
Colin Cross | 6a530a3 | 2013-09-06 18:03:02 -0700 | [diff] [blame] | 54 | |
| 55 | *num_records = ARRAY_SIZE(record_templates); |
| 56 | |
| 57 | /* fastpath to return the necessary number of records */ |
| 58 | if (allocated_records == 0) { |
| 59 | return 0; |
| 60 | } |
| 61 | |
Fred Fettinger | a8bfefb | 2014-08-12 11:15:21 -0500 | [diff] [blame] | 62 | snprintf(tmp, sizeof(tmp), "/proc/%d/cmdline", pid); |
| 63 | fp = fopen(tmp, "r"); |
| 64 | if (fp != NULL) { |
| 65 | if (fgets(line, sizeof(line), fp)) { |
| 66 | if (strcmp(line, "/system/bin/surfaceflinger") == 0) |
| 67 | is_surfaceflinger = true; |
| 68 | } |
| 69 | fclose(fp); |
| 70 | } |
| 71 | |
Colin Cross | 6a530a3 | 2013-09-06 18:03:02 -0700 | [diff] [blame] | 72 | memcpy(records, record_templates, |
| 73 | sizeof(struct memtrack_record) * allocated_records); |
| 74 | |
Arun Kumar K.R | 4bc21ce | 2013-11-11 18:45:42 -0800 | [diff] [blame] | 75 | snprintf(tmp, sizeof(tmp), "/d/kgsl/proc/%d/mem", pid); |
Colin Cross | 6a530a3 | 2013-09-06 18:03:02 -0700 | [diff] [blame] | 76 | fp = fopen(tmp, "r"); |
| 77 | if (fp == NULL) { |
| 78 | return -errno; |
| 79 | } |
| 80 | |
Harsh Vardhan Dwivedi | 0a0cd2a | 2014-05-05 22:30:02 -0600 | [diff] [blame] | 81 | /* Go through each line of <pid>/mem file and for every entry of type "gpumem" |
| 82 | * check if the gpubuffer entry is usermapped or not. If the entry is usermapped |
| 83 | * count the entry as accounted else count the entry as unaccounted. |
| 84 | */ |
Colin Cross | 6a530a3 | 2013-09-06 18:03:02 -0700 | [diff] [blame] | 85 | while (1) { |
Harshdeep Dhatt | 149b8bc | 2016-01-29 11:24:31 -0700 | [diff] [blame] | 86 | unsigned long size, mapsize; |
Colin Cross | 6a530a3 | 2013-09-06 18:03:02 -0700 | [diff] [blame] | 87 | char line_type[7]; |
Harshdeep Dhatt | 4e48da2 | 2016-04-26 13:08:49 -0600 | [diff] [blame^] | 88 | char flags[9]; |
Fred Fettinger | a8bfefb | 2014-08-12 11:15:21 -0500 | [diff] [blame] | 89 | char line_usage[19]; |
Colin Cross | 6a530a3 | 2013-09-06 18:03:02 -0700 | [diff] [blame] | 90 | int ret; |
| 91 | |
| 92 | if (fgets(line, sizeof(line), fp) == NULL) { |
| 93 | break; |
| 94 | } |
| 95 | |
| 96 | /* Format: |
Harshdeep Dhatt | 149b8bc | 2016-01-29 11:24:31 -0700 | [diff] [blame] | 97 | * gpuaddr useraddr size id flags type usage sglen mapsize |
| 98 | * 545ba000 545ba000 4096 1 -----pY gpumem arraybuffer 1 4096 |
Colin Cross | 6a530a3 | 2013-09-06 18:03:02 -0700 | [diff] [blame] | 99 | */ |
Harshdeep Dhatt | 4e48da2 | 2016-04-26 13:08:49 -0600 | [diff] [blame^] | 100 | ret = sscanf(line, "%*x %*x %lu %*d %8s %6s %18s %*d %lu\n", |
Harshdeep Dhatt | 149b8bc | 2016-01-29 11:24:31 -0700 | [diff] [blame] | 101 | &size, flags, line_type, line_usage, &mapsize); |
| 102 | if (ret != 5) { |
Colin Cross | 6a530a3 | 2013-09-06 18:03:02 -0700 | [diff] [blame] | 103 | continue; |
| 104 | } |
| 105 | |
| 106 | if (type == MEMTRACK_TYPE_GL && strcmp(line_type, "gpumem") == 0) { |
Colin Cross | 6a530a3 | 2013-09-06 18:03:02 -0700 | [diff] [blame] | 107 | |
Harshdeep Dhatt | 149b8bc | 2016-01-29 11:24:31 -0700 | [diff] [blame] | 108 | if (flags[6] == 'Y') { |
| 109 | accounted_size += mapsize; |
| 110 | unaccounted_size += size - mapsize; |
| 111 | } else |
Colin Cross | 6a530a3 | 2013-09-06 18:03:02 -0700 | [diff] [blame] | 112 | unaccounted_size += size; |
Harsh Vardhan Dwivedi | 0a0cd2a | 2014-05-05 22:30:02 -0600 | [diff] [blame] | 113 | |
Colin Cross | 6a530a3 | 2013-09-06 18:03:02 -0700 | [diff] [blame] | 114 | } else if (type == MEMTRACK_TYPE_GRAPHICS && strcmp(line_type, "ion") == 0) { |
Rama Vaddula | eb2121a | 2014-11-25 14:20:16 -0800 | [diff] [blame] | 115 | if ( !(is_surfaceflinger == false && strcmp(line_usage, "egl_surface") == 0)) { |
Fred Fettinger | a8bfefb | 2014-08-12 11:15:21 -0500 | [diff] [blame] | 116 | unaccounted_size += size; |
| 117 | } |
Colin Cross | 6a530a3 | 2013-09-06 18:03:02 -0700 | [diff] [blame] | 118 | } |
| 119 | } |
| 120 | |
| 121 | if (allocated_records > 0) { |
| 122 | records[0].size_in_bytes = accounted_size; |
| 123 | } |
| 124 | if (allocated_records > 1) { |
| 125 | records[1].size_in_bytes = unaccounted_size; |
| 126 | } |
| 127 | |
Colin Cross | 6a530a3 | 2013-09-06 18:03:02 -0700 | [diff] [blame] | 128 | fclose(fp); |
| 129 | |
| 130 | return 0; |
| 131 | } |