blob: 768829030a43a8fb4caa0b8f7b521de457a08220 [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
25#include <cutils/log.h>
Ian Rogers2d137912012-08-17 17:08:48 -070026#include <cutils/ashmem.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080027#include <cutils/atomic.h>
28
29#include "codeflinger/CodeCache.h"
30
Ian Rogers2d137912012-08-17 17:08:48 -070031#define LOG_TAG "CodeCache"
32
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080033namespace android {
34
35// ----------------------------------------------------------------------------
36
37#if defined(__arm__)
38#include <unistd.h>
39#include <errno.h>
40#endif
41
Paul Lind2bc2b792012-02-01 10:54:19 -080042#if defined(__mips__)
43#include <asm/cachectl.h>
44#include <errno.h>
45#endif
46
47// ----------------------------------------------------------------------------
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080048// ----------------------------------------------------------------------------
49
Ian Rogers2d137912012-08-17 17:08:48 -070050// A dlmalloc mspace is used to manage the code cache over a mmaped region.
51#define HAVE_MMAP 0
52#define HAVE_MREMAP 0
53#define HAVE_MORECORE 0
54#define MALLOC_ALIGNMENT 16
55#define MSPACES 1
56#define NO_MALLINFO 1
57#define ONLY_MSPACES 1
58// Custom heap error handling.
59#define PROCEED_ON_ERROR 0
60static void heap_error(const char* msg, const char* function, void* p);
61#define CORRUPTION_ERROR_ACTION(m) \
62 heap_error("HEAP MEMORY CORRUPTION", __FUNCTION__, NULL)
63#define USAGE_ERROR_ACTION(m,p) \
64 heap_error("ARGUMENT IS INVALID HEAP ADDRESS", __FUNCTION__, p)
65
66
67#pragma GCC diagnostic ignored "-Wstrict-aliasing"
68#pragma GCC diagnostic ignored "-Wempty-body"
69#include "../../../../bionic/libc/upstream-dlmalloc/malloc.c"
70#pragma GCC diagnostic warning "-Wstrict-aliasing"
71#pragma GCC diagnostic warning "-Wempty-body"
72
73static void heap_error(const char* msg, const char* function, void* p) {
74 ALOG(LOG_FATAL, LOG_TAG, "@@@ ABORTING: CODE FLINGER: %s IN %s addr=%p",
75 msg, function, p);
76 /* So that we can get a memory dump around p */
77 *((int **) 0xdeadbaad) = (int *) p;
78}
79
80// ----------------------------------------------------------------------------
81
82static void* gExecutableStore = NULL;
83static mspace gMspace = NULL;
84const size_t kMaxCodeCacheCapacity = 1024 * 1024;
85
86static mspace getMspace()
87{
88 if (gExecutableStore == NULL) {
89 int fd = ashmem_create_region("CodeFlinger code cache",
90 kMaxCodeCacheCapacity);
91 LOG_ALWAYS_FATAL_IF(fd < 0,
92 "Creating code cache, ashmem_create_region "
93 "failed with error '%s'", strerror(errno));
94 gExecutableStore = mmap(NULL, kMaxCodeCacheCapacity,
95 PROT_READ | PROT_WRITE | PROT_EXEC,
96 MAP_PRIVATE, fd, 0);
97 LOG_ALWAYS_FATAL_IF(gExecutableStore == NULL,
98 "Creating code cache, mmap failed with error "
99 "'%s'", strerror(errno));
100 close(fd);
101 gMspace = create_mspace_with_base(gExecutableStore, kMaxCodeCacheCapacity,
102 /*locked=*/ false);
103 mspace_set_footprint_limit(gMspace, kMaxCodeCacheCapacity);
104 }
105 return gMspace;
106}
107
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800108Assembly::Assembly(size_t size)
109 : mCount(1), mSize(0)
110{
Nick Kralevichbeeeee72010-05-06 15:05:52 -0700111 mBase = (uint32_t*)mspace_malloc(getMspace(), size);
Ian Rogers2d137912012-08-17 17:08:48 -0700112 LOG_ALWAYS_FATAL_IF(mBase == NULL,
113 "Failed to create Assembly of size %zd in executable "
114 "store of size %zd", size, kMaxCodeCacheCapacity);
Nick Kralevichbeeeee72010-05-06 15:05:52 -0700115 mSize = size;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800116}
117
118Assembly::~Assembly()
119{
Nick Kralevichbeeeee72010-05-06 15:05:52 -0700120 mspace_free(getMspace(), mBase);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800121}
122
123void Assembly::incStrong(const void*) const
124{
125 android_atomic_inc(&mCount);
126}
127
128void Assembly::decStrong(const void*) const
129{
130 if (android_atomic_dec(&mCount) == 1) {
131 delete this;
132 }
133}
134
135ssize_t Assembly::size() const
136{
137 if (!mBase) return NO_MEMORY;
138 return mSize;
139}
140
141uint32_t* Assembly::base() const
142{
143 return mBase;
144}
145
146ssize_t Assembly::resize(size_t newSize)
147{
Nick Kralevichbeeeee72010-05-06 15:05:52 -0700148 mBase = (uint32_t*)mspace_realloc(getMspace(), mBase, newSize);
Ian Rogers2d137912012-08-17 17:08:48 -0700149 LOG_ALWAYS_FATAL_IF(mBase == NULL,
150 "Failed to resize Assembly to %zd in code cache "
151 "of size %zd", newSize, kMaxCodeCacheCapacity);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800152 mSize = newSize;
153 return size();
154}
155
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800156// ----------------------------------------------------------------------------
157
158CodeCache::CodeCache(size_t size)
159 : mCacheSize(size), mCacheInUse(0)
160{
161 pthread_mutex_init(&mLock, 0);
162}
163
164CodeCache::~CodeCache()
165{
166 pthread_mutex_destroy(&mLock);
167}
168
169sp<Assembly> CodeCache::lookup(const AssemblyKeyBase& keyBase) const
170{
171 pthread_mutex_lock(&mLock);
172 sp<Assembly> r;
173 ssize_t index = mCacheData.indexOfKey(key_t(keyBase));
174 if (index >= 0) {
175 const cache_entry_t& e = mCacheData.valueAt(index);
176 e.when = mWhen++;
177 r = e.entry;
178 }
179 pthread_mutex_unlock(&mLock);
180 return r;
181}
182
183int CodeCache::cache( const AssemblyKeyBase& keyBase,
184 const sp<Assembly>& assembly)
185{
186 pthread_mutex_lock(&mLock);
187
188 const ssize_t assemblySize = assembly->size();
189 while (mCacheInUse + assemblySize > mCacheSize) {
190 // evict the LRU
191 size_t lru = 0;
192 size_t count = mCacheData.size();
193 for (size_t i=0 ; i<count ; i++) {
194 const cache_entry_t& e = mCacheData.valueAt(i);
195 if (e.when < mCacheData.valueAt(lru).when) {
196 lru = i;
197 }
198 }
199 const cache_entry_t& e = mCacheData.valueAt(lru);
200 mCacheInUse -= e.entry->size();
201 mCacheData.removeItemsAt(lru);
202 }
203
204 ssize_t err = mCacheData.add(key_t(keyBase), cache_entry_t(assembly, mWhen));
205 if (err >= 0) {
206 mCacheInUse += assemblySize;
207 mWhen++;
208 // synchronize caches...
Paul Lind2bc2b792012-02-01 10:54:19 -0800209#if defined(__arm__) || defined(__mips__)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800210 const long base = long(assembly->base());
211 const long curr = base + long(assembly->size());
212 err = cacheflush(base, curr, 0);
Paul Lind2bc2b792012-02-01 10:54:19 -0800213 ALOGE_IF(err, "cacheflush error %s\n",
214 strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800215#endif
216 }
217
218 pthread_mutex_unlock(&mLock);
219 return err;
220}
221
222// ----------------------------------------------------------------------------
223
224}; // namespace android