Colin Cross | 6a530a3 | 2013-09-06 18:03:02 -0700 | [diff] [blame] | 1 | /* |
Hareesh Gundu | 766ae86 | 2016-12-12 13:04:37 +0530 | [diff] [blame] | 2 | * Copyright (C) 2013 The Android Open Source Project |
Colin Cross | 6a530a3 | 2013-09-06 18:03:02 -0700 | [diff] [blame] | 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)); |
Amit Kushwaha | 749db86 | 2018-04-10 20:47:10 +0530 | [diff] [blame] | 48 | char syspath[128]; |
Colin Cross | 6a530a3 | 2013-09-06 18:03:02 -0700 | [diff] [blame] | 49 | size_t accounted_size = 0; |
| 50 | size_t unaccounted_size = 0; |
Amit Kushwaha | 749db86 | 2018-04-10 20:47:10 +0530 | [diff] [blame] | 51 | FILE *fp; |
| 52 | int ret; |
Colin Cross | 6a530a3 | 2013-09-06 18:03:02 -0700 | [diff] [blame] | 53 | |
| 54 | *num_records = ARRAY_SIZE(record_templates); |
| 55 | |
| 56 | /* fastpath to return the necessary number of records */ |
Amit Kushwaha | 749db86 | 2018-04-10 20:47:10 +0530 | [diff] [blame] | 57 | if (allocated_records == 0) |
Colin Cross | 6a530a3 | 2013-09-06 18:03:02 -0700 | [diff] [blame] | 58 | return 0; |
Colin Cross | 6a530a3 | 2013-09-06 18:03:02 -0700 | [diff] [blame] | 59 | |
| 60 | memcpy(records, record_templates, |
Amit Kushwaha | 749db86 | 2018-04-10 20:47:10 +0530 | [diff] [blame] | 61 | sizeof(struct memtrack_record) * allocated_records); |
Colin Cross | 6a530a3 | 2013-09-06 18:03:02 -0700 | [diff] [blame] | 62 | |
Amit Kushwaha | 749db86 | 2018-04-10 20:47:10 +0530 | [diff] [blame] | 63 | if (type == MEMTRACK_TYPE_GL) { |
Colin Cross | 6a530a3 | 2013-09-06 18:03:02 -0700 | [diff] [blame] | 64 | |
Amit Kushwaha | 749db86 | 2018-04-10 20:47:10 +0530 | [diff] [blame] | 65 | snprintf(syspath, sizeof(syspath), |
| 66 | "/sys/class/kgsl/kgsl/proc/%d/gpumem_mapped", pid); |
Colin Cross | 6a530a3 | 2013-09-06 18:03:02 -0700 | [diff] [blame] | 67 | |
Amit Kushwaha | 749db86 | 2018-04-10 20:47:10 +0530 | [diff] [blame] | 68 | fp = fopen(syspath, "r"); |
| 69 | if (fp == NULL) |
| 70 | return -errno; |
Colin Cross | 6a530a3 | 2013-09-06 18:03:02 -0700 | [diff] [blame] | 71 | |
Amit Kushwaha | 749db86 | 2018-04-10 20:47:10 +0530 | [diff] [blame] | 72 | ret = fscanf(fp, "%zu", &accounted_size); |
| 73 | if (ret != 1) { |
Archana Sriram | 8f88815 | 2017-04-27 12:49:42 +0530 | [diff] [blame] | 74 | fclose(fp); |
Archana Sriram | ec4cee0 | 2017-01-06 17:34:57 +0530 | [diff] [blame] | 75 | return -EINVAL; |
Archana Sriram | 8f88815 | 2017-04-27 12:49:42 +0530 | [diff] [blame] | 76 | } |
Amit Kushwaha | 749db86 | 2018-04-10 20:47:10 +0530 | [diff] [blame] | 77 | fclose(fp); |
Archana Sriram | ec4cee0 | 2017-01-06 17:34:57 +0530 | [diff] [blame] | 78 | |
Amit Kushwaha | 749db86 | 2018-04-10 20:47:10 +0530 | [diff] [blame] | 79 | snprintf(syspath, sizeof(syspath), |
| 80 | "/sys/class/kgsl/kgsl/proc/%d/gpumem_unmapped", pid); |
| 81 | |
| 82 | fp = fopen(syspath, "r"); |
| 83 | if (fp == NULL) { |
Amit Kushwaha | 749db86 | 2018-04-10 20:47:10 +0530 | [diff] [blame] | 84 | return -errno; |
Archana Sriram | 8f88815 | 2017-04-27 12:49:42 +0530 | [diff] [blame] | 85 | } |
Archana Sriram | ec4cee0 | 2017-01-06 17:34:57 +0530 | [diff] [blame] | 86 | |
Amit Kushwaha | 749db86 | 2018-04-10 20:47:10 +0530 | [diff] [blame] | 87 | ret = fscanf(fp, "%zu", &unaccounted_size); |
| 88 | if (ret != 1) { |
| 89 | fclose(fp); |
| 90 | return -EINVAL; |
Colin Cross | 6a530a3 | 2013-09-06 18:03:02 -0700 | [diff] [blame] | 91 | } |
Amit Kushwaha | 749db86 | 2018-04-10 20:47:10 +0530 | [diff] [blame] | 92 | fclose(fp); |
| 93 | |
| 94 | } else if (type == MEMTRACK_TYPE_GRAPHICS) { |
| 95 | |
| 96 | snprintf(syspath, sizeof(syspath), |
| 97 | "/sys/class/kgsl/kgsl/proc/%d/imported_mem", pid); |
| 98 | |
| 99 | fp = fopen(syspath, "r"); |
| 100 | if (fp == NULL) |
| 101 | return -errno; |
| 102 | |
| 103 | ret = fscanf(fp, "%zu", &unaccounted_size); |
| 104 | if (ret != 1) { |
| 105 | fclose(fp); |
| 106 | return -EINVAL; |
| 107 | } |
| 108 | fclose(fp); |
Colin Cross | 6a530a3 | 2013-09-06 18:03:02 -0700 | [diff] [blame] | 109 | } |
| 110 | |
Amit Kushwaha | 749db86 | 2018-04-10 20:47:10 +0530 | [diff] [blame] | 111 | if (allocated_records > 0) |
| 112 | records[0].size_in_bytes = accounted_size; |
Colin Cross | 6a530a3 | 2013-09-06 18:03:02 -0700 | [diff] [blame] | 113 | |
Amit Kushwaha | 749db86 | 2018-04-10 20:47:10 +0530 | [diff] [blame] | 114 | if (allocated_records > 1) |
| 115 | records[1].size_in_bytes = unaccounted_size; |
Colin Cross | 6a530a3 | 2013-09-06 18:03:02 -0700 | [diff] [blame] | 116 | |
| 117 | return 0; |
| 118 | } |