CodeCache.cpp revision 2bc2b792782b304b15d8c48b54916a9b3fa3a7ac
1/* 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>
22#include <unistd.h>
23#include <sys/mman.h>
24
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
39#if defined(__mips__)
40#include <asm/cachectl.h>
41#include <errno.h>
42#endif
43
44// ----------------------------------------------------------------------------
45// ----------------------------------------------------------------------------
46
47Assembly::Assembly(size_t size)
48    : mCount(1), mSize(0)
49{
50    mBase = (uint32_t*)mspace_malloc(getMspace(), size);
51    mSize = size;
52    ensureMbaseExecutable();
53}
54
55Assembly::~Assembly()
56{
57    mspace_free(getMspace(), mBase);
58}
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{
85    mBase = (uint32_t*)mspace_realloc(getMspace(), mBase, newSize);
86    mSize = newSize;
87    ensureMbaseExecutable();
88    return size();
89}
90
91mspace 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);
103    size_t adjustedLength = (mBase - pageStart) * sizeof(uint32_t) + mSize;
104
105    if (mBase && mprotect(pageStart, adjustedLength, PROT_READ | PROT_WRITE | PROT_EXEC) != 0) {
106        mspace_free(getMspace(), mBase);
107        mBase = NULL;
108    }
109}
110
111// ----------------------------------------------------------------------------
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...
164#if defined(__arm__) || defined(__mips__)
165        const long base = long(assembly->base());
166        const long curr = base + long(assembly->size());
167        err = cacheflush(base, curr, 0);
168        ALOGE_IF(err, "cacheflush error %s\n",
169                 strerror(errno));
170#endif
171    }
172
173    pthread_mutex_unlock(&mLock);
174    return err;
175}
176
177// ----------------------------------------------------------------------------
178
179}; // namespace android
180