OLD | NEW |
(Empty) | |
| 1 //=- ThreadedStreamingCache.cpp - Cache for StreamingMemoryObject -*- C++ -*-=// |
| 2 // |
| 3 // The LLVM Compiler Infrastructure |
| 4 // |
| 5 // This file is distributed under the University of Illinois Open Source |
| 6 // License. See LICENSE.TXT for details. |
| 7 // |
| 8 //===----------------------------------------------------------------------===// |
| 9 |
| 10 #include "ThreadedStreamingCache.h" |
| 11 #include "llvm/Support/Compiler.h" |
| 12 #include "llvm/Support/Mutex.h" |
| 13 #include <cstring> |
| 14 |
| 15 using namespace llvm; |
| 16 using llvm::sys::ScopedLock; |
| 17 |
| 18 ThreadedStreamingCache::ThreadedStreamingCache( |
| 19 llvm::StreamingMemoryObject *S) : Streamer(S), |
| 20 Cache(kCacheSize), |
| 21 MinObjectSize(0), |
| 22 CacheBase(-1) { |
| 23 static_assert((kCacheSize & (kCacheSize - 1)) == 0, |
| 24 "kCacheSize must be a power of 2"); |
| 25 } |
| 26 |
| 27 void ThreadedStreamingCache::fetchCacheLine(uint64_t Address) const { |
| 28 uint64_t Base = Address & kCacheSizeMask; |
| 29 uint64_t BytesFetched; |
| 30 ScopedLock L(StreamerLock); |
| 31 if (Streamer->isValidAddress(Base + kCacheSize - 1)) { |
| 32 BytesFetched = Streamer->readBytes(&Cache[0], kCacheSize, Base); |
| 33 if (BytesFetched != kCacheSize) { |
| 34 llvm::report_fatal_error( |
| 35 "fetchCacheLine failed to fetch a full cache line"); |
| 36 } |
| 37 MinObjectSize = Base + kCacheSize; |
| 38 } else { |
| 39 uint64_t End = Streamer->getExtent(); |
| 40 assert(End > Address && End <= Base + kCacheSize); |
| 41 BytesFetched = Streamer->readBytes(&Cache[0], End - Base, Base); |
| 42 if (BytesFetched != (End - Base)) { |
| 43 llvm::report_fatal_error( |
| 44 "fetchCacheLine failed to fetch rest of stream"); |
| 45 } |
| 46 MinObjectSize = End; |
| 47 } |
| 48 CacheBase = Base; |
| 49 } |
| 50 |
| 51 uint64_t ThreadedStreamingCache::readBytes(uint8_t* Buf, uint64_t Size, |
| 52 uint64_t Address) const { |
| 53 // To keep the cache fetch simple, we currently require that no request cross |
| 54 // the cache line. This isn't a problem for the bitcode reader because it only |
| 55 // fetches a byte or a word (word may be 4 to 8 bytes) at a time. |
| 56 uint64_t Upper = Address + Size; |
| 57 if (Address < CacheBase || Upper > CacheBase + kCacheSize) { |
| 58 // If completely outside of a cacheline, fetch the cacheline. |
| 59 if ((Address & kCacheSizeMask) != ((Upper - 1) & kCacheSizeMask)) |
| 60 llvm::report_fatal_error("readBytes request spans cache lines"); |
| 61 // Fetch a cache line first, which may be partial. |
| 62 fetchCacheLine(Address); |
| 63 } |
| 64 // Now the start Address should at least fit in the cache line, |
| 65 // but Upper may still be beyond the Extent / MinObjectSize, so clamp. |
| 66 if (Upper > MinObjectSize) { |
| 67 // If in the cacheline but stretches beyone the MinObjectSize, |
| 68 // only read up to MinObjectSize (caller uses readBytes to check EOF, |
| 69 // and can guess / try to read more). MinObjectSize should be the same |
| 70 // as EOF in this case otherwise it would have fit in the cacheline. |
| 71 Size = MinObjectSize - Address; |
| 72 } |
| 73 memcpy(Buf, &Cache[Address - CacheBase], Size); |
| 74 return Size; |
| 75 } |
| 76 |
| 77 uint64_t ThreadedStreamingCache::getExtent() const { |
| 78 llvm::report_fatal_error( |
| 79 "getExtent should not be called for pnacl streaming bitcode"); |
| 80 return 0; |
| 81 } |
| 82 |
| 83 bool ThreadedStreamingCache::isValidAddress(uint64_t Address) const { |
| 84 if (Address < MinObjectSize) |
| 85 return true; |
| 86 ScopedLock L(StreamerLock); |
| 87 bool Valid = Streamer->isValidAddress(Address); |
| 88 if (Valid) |
| 89 MinObjectSize = Address; |
| 90 return Valid; |
| 91 } |
| 92 |
| 93 bool ThreadedStreamingCache::dropLeadingBytes(size_t S) { |
| 94 ScopedLock L(StreamerLock); |
| 95 return Streamer->dropLeadingBytes(S); |
| 96 } |
| 97 |
| 98 void ThreadedStreamingCache::setKnownObjectSize(size_t Size) { |
| 99 MinObjectSize = Size; |
| 100 ScopedLock L(StreamerLock); |
| 101 Streamer->setKnownObjectSize(Size); |
| 102 } |
| 103 |
| 104 const uint64_t ThreadedStreamingCache::kCacheSize; |
| 105 const uint64_t ThreadedStreamingCache::kCacheSizeMask; |
| 106 llvm::sys::SmartMutex<false> ThreadedStreamingCache::StreamerLock; |
OLD | NEW |