blob: 60fc7717ee60aa2c886ce5c121233cb96092b096 [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
42// ----------------------------------------------------------------------------
43
Ian Rogers2d137912012-08-17 17:08:48 -070044// A dlmalloc mspace is used to manage the code cache over a mmaped region.
45#define HAVE_MMAP 0
46#define HAVE_MREMAP 0
47#define HAVE_MORECORE 0
48#define MALLOC_ALIGNMENT 16
49#define MSPACES 1
50#define NO_MALLINFO 1
51#define ONLY_MSPACES 1
52// Custom heap error handling.
53#define PROCEED_ON_ERROR 0
54static void heap_error(const char* msg, const char* function, void* p);
55#define CORRUPTION_ERROR_ACTION(m) \
56 heap_error("HEAP MEMORY CORRUPTION", __FUNCTION__, NULL)
57#define USAGE_ERROR_ACTION(m,p) \
58 heap_error("ARGUMENT IS INVALID HEAP ADDRESS", __FUNCTION__, p)
59
60
61#pragma GCC diagnostic ignored "-Wstrict-aliasing"
62#pragma GCC diagnostic ignored "-Wempty-body"
63#include "../../../../bionic/libc/upstream-dlmalloc/malloc.c"
64#pragma GCC diagnostic warning "-Wstrict-aliasing"
65#pragma GCC diagnostic warning "-Wempty-body"
66
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);
91 LOG_ALWAYS_FATAL_IF(gExecutableStore == NULL,
92 "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)
103 : mCount(1), mSize(0)
104{
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{
119 android_atomic_inc(&mCount);
120}
121
122void Assembly::decStrong(const void*) const
123{
124 if (android_atomic_dec(&mCount) == 1) {
125 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...
203#if defined(__arm__)
204 const long base = long(assembly->base());
205 const long curr = base + long(assembly->size());
206 err = cacheflush(base, curr, 0);
Steve Block01dda202012-01-06 14:13:42 +0000207 ALOGE_IF(err, "__ARM_NR_cacheflush error %s\n",
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800208 strerror(errno));
209#endif
210 }
211
212 pthread_mutex_unlock(&mLock);
213 return err;
214}
215
216// ----------------------------------------------------------------------------
217
218}; // namespace android