2 * Copyright (C) 2008 The Android Open Source Project
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 * Mutex-free cache. Each entry has two 32-bit keys, one 32-bit value,
18 * and a 32-bit version.
25 * I think modern C mandates that the results of a boolean expression are
26 * 0 or 1. If not, or we suddenly turn into C++ and bool != int, use this.
28 #define BOOL_TO_INT(x) (x)
29 //#define BOOL_TO_INT(x) ((x) ? 1 : 0)
31 #define CPU_CACHE_WIDTH 32
32 #define CPU_CACHE_WIDTH_1 (CPU_CACHE_WIDTH-1)
34 #define ATOMIC_LOCK_FLAG (1 << 31)
39 AtomicCache* dvmAllocAtomicCache(int numEntries)
41 AtomicCache* newCache;
43 newCache = (AtomicCache*) calloc(1, sizeof(AtomicCache));
47 newCache->numEntries = numEntries;
49 newCache->entryAlloc = calloc(1,
50 sizeof(AtomicCacheEntry) * numEntries + CPU_CACHE_WIDTH);
51 if (newCache->entryAlloc == NULL)
55 * Adjust storage to align on a 32-byte boundary. Each entry is 16 bytes
56 * wide. This ensures that each cache entry sits on a single CPU cache
59 assert(sizeof(AtomicCacheEntry) == 16);
60 newCache->entries = (AtomicCacheEntry*)
61 (((int) newCache->entryAlloc + CPU_CACHE_WIDTH_1) & ~CPU_CACHE_WIDTH_1);
69 void dvmFreeAtomicCache(AtomicCache* cache)
72 free(cache->entryAlloc);
80 * Update a cache entry.
82 * In the event of a collision with another thread, the update may be skipped.
84 * We only need "pCache" for stats.
86 void dvmUpdateAtomicCache(u4 key1, u4 key2, u4 value, AtomicCacheEntry* pEntry,
88 #if CALC_CACHE_STATS > 0
94 * The fields don't match, so we need to update them. There is a
95 * risk that another thread is also trying to update them, so we
96 * grab an ownership flag to lock out other threads.
98 * If the lock flag was already set in "firstVersion", somebody else
99 * was in mid-update. (This means that using "firstVersion" as the
100 * "before" argument to the CAS would succeed when it shouldn't and
101 * vice-versa -- we could also just pass in
102 * (firstVersion & ~ATOMIC_LOCK_FLAG) as the first argument.)
104 * NOTE: we don't really deal with the situation where we overflow
105 * the version counter (at 2^31). Probably not a real concern.
107 if ((firstVersion & ATOMIC_LOCK_FLAG) != 0 ||
108 !ATOMIC_CMP_SWAP((volatile s4*) &pEntry->version,
109 firstVersion, firstVersion | ATOMIC_LOCK_FLAG))
112 * We couldn't get the write lock. Return without updating the table.
114 #if CALC_CACHE_STATS > 0
120 /* must be even-valued on entry */
121 assert((firstVersion & 0x01) == 0);
123 #if CALC_CACHE_STATS > 0
124 /* for stats, assume a key value of zero indicates an empty entry */
125 if (pEntry->key1 == 0)
137 pEntry->value = value;
144 * Clear the lock flag. Nobody else should have been able to modify
145 * pEntry->version, so if this fails the world is broken.
148 if (!ATOMIC_CMP_SWAP((volatile s4*) &pEntry->version,
149 firstVersion | ATOMIC_LOCK_FLAG, firstVersion))
151 //LOGE("unable to reset the instanceof cache ownership\n");
158 * Dump the "instanceof" cache stats.
160 void dvmDumpAtomicCacheStats(const AtomicCache* pCache)
165 "Cache stats: trv=%d fai=%d hit=%d mis=%d fil=%d %d%% (size=%d)\n",
166 pCache->trivial, pCache->fail, pCache->hits,
167 pCache->misses, pCache->fills,
168 (pCache->hits == 0) ? 0 :
170 (pCache->fail + pCache->hits + pCache->misses + pCache->fills),