blob: ae38519140580e195b4fd4cf3cc94bd19aa1baf5 [file] [log] [blame]
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001/* libs/pixelflinger/codeflinger/CodeCache.cpp
2**
3** Copyright 2006, The Android Open Source Project
4**
5** Licensed under the Apache License, Version 2.0 (the "License");
6** you may not use this file except in compliance with the License.
7** You may obtain a copy of the License at
8**
9** http://www.apache.org/licenses/LICENSE-2.0
10**
11** Unless required by applicable law or agreed to in writing, software
12** distributed under the License is distributed on an "AS IS" BASIS,
13** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14** See the License for the specific language governing permissions and
15** limitations under the License.
16*/
17
18
19#include <assert.h>
20#include <stdio.h>
21#include <stdlib.h>
Nick Kralevichbeeeee72010-05-06 15:05:52 -070022#include <unistd.h>
23#include <sys/mman.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080024
Ian Rogers2d137912012-08-17 17:08:48 -070025#include <cutils/ashmem.h>
Ian Rogers04b5ac32012-08-29 14:06:02 -070026#define LOG_TAG "CodeCache"
27#include <cutils/log.h>
28
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080029
Mathias Agopian9857d992013-04-01 15:17:55 -070030#include "CodeCache.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080031
32namespace android {
33
34// ----------------------------------------------------------------------------
35
Ashok Bhat658f89d2013-02-28 18:32:03 +000036#if defined(__arm__) || defined(__aarch64__)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080037#include <unistd.h>
38#include <errno.h>
39#endif
40
Paul Lind2bc2b792012-02-01 10:54:19 -080041#if defined(__mips__)
42#include <asm/cachectl.h>
43#include <errno.h>
44#endif
45
46// ----------------------------------------------------------------------------
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080047// ----------------------------------------------------------------------------
48
Ian Rogers2d137912012-08-17 17:08:48 -070049// A dlmalloc mspace is used to manage the code cache over a mmaped region.
50#define HAVE_MMAP 0
51#define HAVE_MREMAP 0
52#define HAVE_MORECORE 0
53#define MALLOC_ALIGNMENT 16
54#define MSPACES 1
55#define NO_MALLINFO 1
56#define ONLY_MSPACES 1
57// Custom heap error handling.
58#define PROCEED_ON_ERROR 0
59static void heap_error(const char* msg, const char* function, void* p);
60#define CORRUPTION_ERROR_ACTION(m) \
61 heap_error("HEAP MEMORY CORRUPTION", __FUNCTION__, NULL)
62#define USAGE_ERROR_ACTION(m,p) \
63 heap_error("ARGUMENT IS INVALID HEAP ADDRESS", __FUNCTION__, p)
64
Josh Gao70bde212016-01-22 11:04:35 -080065#include "../../../../external/dlmalloc/malloc.c"
Ian Rogers2d137912012-08-17 17:08:48 -070066
67static void heap_error(const char* msg, const char* function, void* p) {
68 ALOG(LOG_FATAL, LOG_TAG, "@@@ ABORTING: CODE FLINGER: %s IN %s addr=%p",
69 msg, function, p);
70 /* So that we can get a memory dump around p */
71 *((int **) 0xdeadbaad) = (int *) p;
72}
73
74// ----------------------------------------------------------------------------
75
76static void* gExecutableStore = NULL;
77static mspace gMspace = NULL;
78const size_t kMaxCodeCacheCapacity = 1024 * 1024;
79
80static mspace getMspace()
81{
82 if (gExecutableStore == NULL) {
83 int fd = ashmem_create_region("CodeFlinger code cache",
84 kMaxCodeCacheCapacity);
85 LOG_ALWAYS_FATAL_IF(fd < 0,
86 "Creating code cache, ashmem_create_region "
87 "failed with error '%s'", strerror(errno));
88 gExecutableStore = mmap(NULL, kMaxCodeCacheCapacity,
89 PROT_READ | PROT_WRITE | PROT_EXEC,
90 MAP_PRIVATE, fd, 0);
Hurri Lu473a7292014-05-22 11:38:12 +080091 LOG_ALWAYS_FATAL_IF(gExecutableStore == MAP_FAILED,
Ian Rogers2d137912012-08-17 17:08:48 -070092 "Creating code cache, mmap failed with error "
93 "'%s'", strerror(errno));
94 close(fd);
95 gMspace = create_mspace_with_base(gExecutableStore, kMaxCodeCacheCapacity,
96 /*locked=*/ false);
97 mspace_set_footprint_limit(gMspace, kMaxCodeCacheCapacity);
98 }
99 return gMspace;
100}
101
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800102Assembly::Assembly(size_t size)
Hans Boehme74dec42016-08-10 18:44:14 -0700103 : mCount(0), mSize(0)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800104{
Nick Kralevichbeeeee72010-05-06 15:05:52 -0700105 mBase = (uint32_t*)mspace_malloc(getMspace(), size);
Ian Rogers2d137912012-08-17 17:08:48 -0700106 LOG_ALWAYS_FATAL_IF(mBase == NULL,
107 "Failed to create Assembly of size %zd in executable "
108 "store of size %zd", size, kMaxCodeCacheCapacity);
Nick Kralevichbeeeee72010-05-06 15:05:52 -0700109 mSize = size;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800110}
111
112Assembly::~Assembly()
113{
Nick Kralevichbeeeee72010-05-06 15:05:52 -0700114 mspace_free(getMspace(), mBase);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800115}
116
117void Assembly::incStrong(const void*) const
118{
Hans Boehme74dec42016-08-10 18:44:14 -0700119 mCount.fetch_add(1, std::memory_order_relaxed);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800120}
121
122void Assembly::decStrong(const void*) const
123{
Hans Boehme74dec42016-08-10 18:44:14 -0700124 if (mCount.fetch_sub(1, std::memory_order_acq_rel) == 1) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800125 delete this;
126 }
127}
128
129ssize_t Assembly::size() const
130{
131 if (!mBase) return NO_MEMORY;
132 return mSize;
133}
134
135uint32_t* Assembly::base() const
136{
137 return mBase;
138}
139
140ssize_t Assembly::resize(size_t newSize)
141{
Nick Kralevichbeeeee72010-05-06 15:05:52 -0700142 mBase = (uint32_t*)mspace_realloc(getMspace(), mBase, newSize);
Ian Rogers2d137912012-08-17 17:08:48 -0700143 LOG_ALWAYS_FATAL_IF(mBase == NULL,
144 "Failed to resize Assembly to %zd in code cache "
145 "of size %zd", newSize, kMaxCodeCacheCapacity);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800146 mSize = newSize;
147 return size();
148}
149
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800150// ----------------------------------------------------------------------------
151
152CodeCache::CodeCache(size_t size)
153 : mCacheSize(size), mCacheInUse(0)
154{
155 pthread_mutex_init(&mLock, 0);
156}
157
158CodeCache::~CodeCache()
159{
160 pthread_mutex_destroy(&mLock);
161}
162
163sp<Assembly> CodeCache::lookup(const AssemblyKeyBase& keyBase) const
164{
165 pthread_mutex_lock(&mLock);
166 sp<Assembly> r;
167 ssize_t index = mCacheData.indexOfKey(key_t(keyBase));
168 if (index >= 0) {
169 const cache_entry_t& e = mCacheData.valueAt(index);
170 e.when = mWhen++;
171 r = e.entry;
172 }
173 pthread_mutex_unlock(&mLock);
174 return r;
175}
176
177int CodeCache::cache( const AssemblyKeyBase& keyBase,
178 const sp<Assembly>& assembly)
179{
180 pthread_mutex_lock(&mLock);
181
182 const ssize_t assemblySize = assembly->size();
183 while (mCacheInUse + assemblySize > mCacheSize) {
184 // evict the LRU
185 size_t lru = 0;
186 size_t count = mCacheData.size();
187 for (size_t i=0 ; i<count ; i++) {
188 const cache_entry_t& e = mCacheData.valueAt(i);
189 if (e.when < mCacheData.valueAt(lru).when) {
190 lru = i;
191 }
192 }
193 const cache_entry_t& e = mCacheData.valueAt(lru);
194 mCacheInUse -= e.entry->size();
195 mCacheData.removeItemsAt(lru);
196 }
197
198 ssize_t err = mCacheData.add(key_t(keyBase), cache_entry_t(assembly, mWhen));
199 if (err >= 0) {
200 mCacheInUse += assemblySize;
201 mWhen++;
202 // synchronize caches...
Dan Albert949aa232014-09-08 19:09:41 -0700203 char* base = reinterpret_cast<char*>(assembly->base());
204 char* curr = reinterpret_cast<char*>(base + assembly->size());
Ashok Bhat410ae2f2014-06-19 11:03:32 +0100205 __builtin___clear_cache(base, curr);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800206 }
207
208 pthread_mutex_unlock(&mLock);
209 return err;
210}
211
212// ----------------------------------------------------------------------------
213
214}; // namespace android