| Colin Cross | fc600e4 | 2013-09-06 16:41:40 -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 |  | 
| Adam Lesinski | 481b947 | 2013-09-23 18:42:41 -0700 | [diff] [blame] | 17 | #include <memtrack/memtrack.h> | 
| Colin Cross | fc600e4 | 2013-09-06 16:41:40 -0700 | [diff] [blame] | 18 |  | 
 | 19 | #define LOG_TAG "memtrack" | 
 | 20 |  | 
 | 21 | #include <log/log.h> | 
 | 22 |  | 
| Elliott Hughes | 0badbd6 | 2014-12-29 12:24:25 -0800 | [diff] [blame^] | 23 | #include <errno.h> | 
| Colin Cross | fc600e4 | 2013-09-06 16:41:40 -0700 | [diff] [blame] | 24 | #include <hardware/memtrack.h> | 
 | 25 |  | 
 | 26 | #define ARRAY_SIZE(x) (sizeof(x)/sizeof(x[0])) | 
 | 27 |  | 
 | 28 | static const memtrack_module_t *module; | 
 | 29 |  | 
 | 30 | struct memtrack_proc { | 
 | 31 |     pid_t pid; | 
 | 32 |     struct memtrack_proc_type { | 
 | 33 |         enum memtrack_type type; | 
 | 34 |         size_t num_records; | 
 | 35 |         size_t allocated_records; | 
 | 36 |         struct memtrack_record *records; | 
 | 37 |     } types[MEMTRACK_NUM_TYPES]; | 
 | 38 | }; | 
 | 39 |  | 
 | 40 | int memtrack_init(void) | 
 | 41 | { | 
 | 42 |     int err; | 
 | 43 |  | 
 | 44 |     if (module) { | 
 | 45 |         return 0; | 
 | 46 |     } | 
 | 47 |  | 
 | 48 |     err = hw_get_module(MEMTRACK_HARDWARE_MODULE_ID, | 
 | 49 |             (hw_module_t const**)&module); | 
 | 50 |     if (err) { | 
 | 51 |         ALOGE("Couldn't load %s module (%s)", MEMTRACK_HARDWARE_MODULE_ID, | 
 | 52 |                 strerror(-err)); | 
 | 53 |         return err; | 
 | 54 |     } | 
 | 55 |  | 
 | 56 |     return module->init(module); | 
 | 57 | } | 
 | 58 |  | 
 | 59 | struct memtrack_proc *memtrack_proc_new(void) | 
 | 60 | { | 
 | 61 |     if (!module) { | 
 | 62 |         return NULL; | 
 | 63 |     } | 
 | 64 |  | 
 | 65 |     return calloc(sizeof(struct memtrack_proc), 1); | 
 | 66 | } | 
 | 67 |  | 
 | 68 | void memtrack_proc_destroy(struct memtrack_proc *p) | 
 | 69 | { | 
 | 70 |     enum memtrack_type i; | 
 | 71 |  | 
 | 72 |     if (p) { | 
 | 73 |         for (i = 0; i < MEMTRACK_NUM_TYPES; i++) { | 
 | 74 |             free(p->types[i].records); | 
 | 75 |         } | 
 | 76 |     } | 
 | 77 |     free(p); | 
 | 78 | } | 
 | 79 |  | 
 | 80 | static int memtrack_proc_get_type(struct memtrack_proc_type *t, | 
 | 81 |             pid_t pid, enum memtrack_type type) | 
 | 82 | { | 
 | 83 |     size_t num_records = t->num_records; | 
 | 84 |     int ret; | 
 | 85 |  | 
 | 86 | retry: | 
 | 87 |     ret = module->getMemory(module, pid, type, t->records, &num_records); | 
 | 88 |     if (ret) { | 
 | 89 |         t->num_records = 0; | 
 | 90 |         return ret; | 
 | 91 |     } | 
 | 92 |     if (num_records > t->allocated_records) { | 
 | 93 |         /* Need more records than allocated */ | 
 | 94 |         free(t->records); | 
 | 95 |         t->records = calloc(sizeof(*t->records), num_records); | 
 | 96 |         if (!t->records) { | 
 | 97 |             return -ENOMEM; | 
 | 98 |         } | 
 | 99 |         t->allocated_records = num_records; | 
 | 100 |         goto retry; | 
 | 101 |     } | 
 | 102 |     t->num_records = num_records; | 
 | 103 |  | 
 | 104 |     return 0; | 
 | 105 | } | 
 | 106 |  | 
 | 107 | /* TODO: sanity checks on return values from HALs: | 
 | 108 |  *   make sure no records have invalid flags set | 
 | 109 |  *    - unknown flags | 
 | 110 |  *    - too many flags of a single category | 
 | 111 |  *    - missing ACCOUNTED/UNACCOUNTED | 
 | 112 |  *   make sure there are not overlapping SHARED and SHARED_PSS records | 
 | 113 |  */ | 
 | 114 | static int memtrack_proc_sanity_check(struct memtrack_proc *p) | 
 | 115 | { | 
 | 116 |     (void)p; | 
 | 117 |     return 0; | 
 | 118 | } | 
 | 119 |  | 
 | 120 | int memtrack_proc_get(struct memtrack_proc *p, pid_t pid) | 
 | 121 | { | 
 | 122 |     enum memtrack_type i; | 
 | 123 |  | 
 | 124 |     if (!module) { | 
 | 125 |         return -EINVAL; | 
 | 126 |     } | 
 | 127 |  | 
 | 128 |     if (!p) { | 
 | 129 |         return -EINVAL; | 
 | 130 |     } | 
 | 131 |  | 
 | 132 |     p->pid = pid; | 
 | 133 |     for (i = 0; i < MEMTRACK_NUM_TYPES; i++) { | 
 | 134 |         memtrack_proc_get_type(&p->types[i], pid, i); | 
 | 135 |     } | 
 | 136 |  | 
 | 137 |     return memtrack_proc_sanity_check(p); | 
 | 138 | } | 
 | 139 |  | 
 | 140 | static ssize_t memtrack_proc_sum(struct memtrack_proc *p, | 
 | 141 |             enum memtrack_type types[], size_t num_types, | 
 | 142 |             unsigned int flags) | 
 | 143 | { | 
 | 144 |     ssize_t sum = 0; | 
 | 145 |     size_t i; | 
 | 146 |     size_t j; | 
 | 147 |  | 
 | 148 |     for (i = 0; i < num_types; i++) { | 
 | 149 |         enum memtrack_type type = types[i]; | 
 | 150 |         for (j = 0; j < p->types[type].num_records; j++) { | 
 | 151 |             if ((p->types[type].records[j].flags & flags) == flags) { | 
 | 152 |                 sum += p->types[type].records[j].size_in_bytes; | 
 | 153 |             } | 
 | 154 |         } | 
 | 155 |     } | 
 | 156 |  | 
 | 157 |     return sum; | 
 | 158 | } | 
 | 159 |  | 
 | 160 | ssize_t memtrack_proc_graphics_total(struct memtrack_proc *p) | 
 | 161 | { | 
 | 162 |     enum memtrack_type types[] = { MEMTRACK_TYPE_GRAPHICS }; | 
 | 163 |     return memtrack_proc_sum(p, types, ARRAY_SIZE(types), 0); | 
 | 164 | } | 
 | 165 |  | 
 | 166 | ssize_t memtrack_proc_graphics_pss(struct memtrack_proc *p) | 
 | 167 | { | 
 | 168 |     enum memtrack_type types[] = { MEMTRACK_TYPE_GRAPHICS }; | 
 | 169 |     return memtrack_proc_sum(p, types, ARRAY_SIZE(types), | 
 | 170 |                 MEMTRACK_FLAG_SMAPS_UNACCOUNTED); | 
 | 171 | } | 
 | 172 |  | 
 | 173 | ssize_t memtrack_proc_gl_total(struct memtrack_proc *p) | 
 | 174 | { | 
 | 175 |     enum memtrack_type types[] = { MEMTRACK_TYPE_GL }; | 
 | 176 |     return memtrack_proc_sum(p, types, ARRAY_SIZE(types), 0); | 
 | 177 | } | 
 | 178 |  | 
 | 179 | ssize_t memtrack_proc_gl_pss(struct memtrack_proc *p) | 
 | 180 | { | 
 | 181 |     enum memtrack_type types[] = { MEMTRACK_TYPE_GL }; | 
 | 182 |     return memtrack_proc_sum(p, types, ARRAY_SIZE(types), | 
 | 183 |                 MEMTRACK_FLAG_SMAPS_UNACCOUNTED); | 
 | 184 | } | 
 | 185 |  | 
 | 186 | ssize_t memtrack_proc_other_total(struct memtrack_proc *p) | 
 | 187 | { | 
 | 188 |     enum memtrack_type types[] = { MEMTRACK_TYPE_MULTIMEDIA, | 
 | 189 |                                         MEMTRACK_TYPE_CAMERA, | 
 | 190 |                                         MEMTRACK_TYPE_OTHER }; | 
 | 191 |     return memtrack_proc_sum(p, types, ARRAY_SIZE(types), 0); | 
 | 192 | } | 
 | 193 |  | 
 | 194 | ssize_t memtrack_proc_other_pss(struct memtrack_proc *p) | 
 | 195 | { | 
 | 196 |     enum memtrack_type types[] = { MEMTRACK_TYPE_MULTIMEDIA, | 
 | 197 |                                         MEMTRACK_TYPE_CAMERA, | 
 | 198 |                                         MEMTRACK_TYPE_OTHER }; | 
 | 199 |     return memtrack_proc_sum(p, types, ARRAY_SIZE(types), | 
 | 200 |                 MEMTRACK_FLAG_SMAPS_UNACCOUNTED); | 
 | 201 | } |