blob: 4f2ede3f256aebb3e728bdb27efad6a2a1357ff7 [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>
26#include <cutils/atomic.h>
27
28#include "codeflinger/CodeCache.h"
29
30namespace android {
31
32// ----------------------------------------------------------------------------
33
34#if defined(__arm__)
35#include <unistd.h>
36#include <errno.h>
37#endif
38
Paul Lind2bc2b792012-02-01 10:54:19 -080039#if defined(__mips__)
40#include <asm/cachectl.h>
41#include <errno.h>
42#endif
43
44// ----------------------------------------------------------------------------
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080045// ----------------------------------------------------------------------------
46
47Assembly::Assembly(size_t size)
48 : mCount(1), mSize(0)
49{
Nick Kralevichbeeeee72010-05-06 15:05:52 -070050 mBase = (uint32_t*)mspace_malloc(getMspace(), size);
51 mSize = size;
52 ensureMbaseExecutable();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080053}
54
55Assembly::~Assembly()
56{
Nick Kralevichbeeeee72010-05-06 15:05:52 -070057 mspace_free(getMspace(), mBase);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080058}
59
60void Assembly::incStrong(const void*) const
61{
62 android_atomic_inc(&mCount);
63}
64
65void Assembly::decStrong(const void*) const
66{
67 if (android_atomic_dec(&mCount) == 1) {
68 delete this;
69 }
70}
71
72ssize_t Assembly::size() const
73{
74 if (!mBase) return NO_MEMORY;
75 return mSize;
76}
77
78uint32_t* Assembly::base() const
79{
80 return mBase;
81}
82
83ssize_t Assembly::resize(size_t newSize)
84{
Nick Kralevichbeeeee72010-05-06 15:05:52 -070085 mBase = (uint32_t*)mspace_realloc(getMspace(), mBase, newSize);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080086 mSize = newSize;
Nick Kralevichbeeeee72010-05-06 15:05:52 -070087 ensureMbaseExecutable();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080088 return size();
89}
90
Nick Kralevichbeeeee72010-05-06 15:05:52 -070091mspace Assembly::getMspace()
92{
93 static mspace msp = create_contiguous_mspace(2 * 1024, 1024 * 1024, /*locked=*/ false);
94 return msp;
95}
96
97void Assembly::ensureMbaseExecutable()
98{
99 long pagesize = sysconf(_SC_PAGESIZE);
100 long pagemask = ~(pagesize - 1); // assumes pagesize is a power of 2
101
102 uint32_t* pageStart = (uint32_t*) (((uintptr_t) mBase) & pagemask);
Jean-Baptiste Queru8e0e3722010-10-14 14:29:00 -0700103 size_t adjustedLength = (mBase - pageStart) * sizeof(uint32_t) + mSize;
Nick Kralevichbeeeee72010-05-06 15:05:52 -0700104
105 if (mBase && mprotect(pageStart, adjustedLength, PROT_READ | PROT_WRITE | PROT_EXEC) != 0) {
106 mspace_free(getMspace(), mBase);
107 mBase = NULL;
108 }
109}
110
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800111// ----------------------------------------------------------------------------
112
113CodeCache::CodeCache(size_t size)
114 : mCacheSize(size), mCacheInUse(0)
115{
116 pthread_mutex_init(&mLock, 0);
117}
118
119CodeCache::~CodeCache()
120{
121 pthread_mutex_destroy(&mLock);
122}
123
124sp<Assembly> CodeCache::lookup(const AssemblyKeyBase& keyBase) const
125{
126 pthread_mutex_lock(&mLock);
127 sp<Assembly> r;
128 ssize_t index = mCacheData.indexOfKey(key_t(keyBase));
129 if (index >= 0) {
130 const cache_entry_t& e = mCacheData.valueAt(index);
131 e.when = mWhen++;
132 r = e.entry;
133 }
134 pthread_mutex_unlock(&mLock);
135 return r;
136}
137
138int CodeCache::cache( const AssemblyKeyBase& keyBase,
139 const sp<Assembly>& assembly)
140{
141 pthread_mutex_lock(&mLock);
142
143 const ssize_t assemblySize = assembly->size();
144 while (mCacheInUse + assemblySize > mCacheSize) {
145 // evict the LRU
146 size_t lru = 0;
147 size_t count = mCacheData.size();
148 for (size_t i=0 ; i<count ; i++) {
149 const cache_entry_t& e = mCacheData.valueAt(i);
150 if (e.when < mCacheData.valueAt(lru).when) {
151 lru = i;
152 }
153 }
154 const cache_entry_t& e = mCacheData.valueAt(lru);
155 mCacheInUse -= e.entry->size();
156 mCacheData.removeItemsAt(lru);
157 }
158
159 ssize_t err = mCacheData.add(key_t(keyBase), cache_entry_t(assembly, mWhen));
160 if (err >= 0) {
161 mCacheInUse += assemblySize;
162 mWhen++;
163 // synchronize caches...
Paul Lind2bc2b792012-02-01 10:54:19 -0800164#if defined(__arm__) || defined(__mips__)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800165 const long base = long(assembly->base());
166 const long curr = base + long(assembly->size());
167 err = cacheflush(base, curr, 0);
Paul Lind2bc2b792012-02-01 10:54:19 -0800168 ALOGE_IF(err, "cacheflush error %s\n",
169 strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800170#endif
171 }
172
173 pthread_mutex_unlock(&mLock);
174 return err;
175}
176
177// ----------------------------------------------------------------------------
178
179}; // namespace android