Split JIT image from oBCC.
This change is the first step to merge CacheReader and
MCCacheReader (CacheWriter and MCCacheWriter.) In this
commit, we split the executable generated by OLD JIT
into a separated file with ".jit-image" as the file extension.
Change-Id: Icd40623e1247ffdfa93558b8e9fc4378a78b4ea1
diff --git a/lib/ExecutionEngine/CacheReader.cpp b/lib/ExecutionEngine/CacheReader.cpp
index 136cccd..c11a9e7 100644
--- a/lib/ExecutionEngine/CacheReader.cpp
+++ b/lib/ExecutionEngine/CacheReader.cpp
@@ -49,13 +49,17 @@
if (mpFuncTable) { free(mpFuncTable); }
}
-ScriptCached *CacheReader::readCacheFile(FileHandle *file, Script *S) {
+ScriptCached *CacheReader::readCacheFile(FileHandle *objFile,
+ FileHandle *infoFile,
+ Script *S) {
// Check file handle
- if (!file || file->getFD() < 0) {
+ if (!objFile || objFile->getFD() < 0 ||
+ !infoFile || infoFile->getFD() < 0) {
return NULL;
}
- mFile = file;
+ mObjFile = objFile;
+ mInfoFile = infoFile;
// Allocate ScriptCached object
mpResult.reset(new (nothrow) ScriptCached(S));
@@ -91,16 +95,26 @@
bool CacheReader::checkFileSize() {
struct stat stfile;
- if (fstat(mFile->getFD(), &stfile) < 0) {
- LOGE("Unable to stat cache file.\n");
+
+ if (fstat(mInfoFile->getFD(), &stfile) < 0) {
+ LOGE("Unable to stat metadata information file.\n");
return false;
}
- mFileSize = stfile.st_size;
+ mInfoFileSize = stfile.st_size;
- if (mFileSize < (off_t)sizeof(OBCC_Header) ||
- mFileSize < (off_t)ContextManager::ContextSize) {
- LOGE("Cache file is too small to be correct.\n");
+ if (mInfoFileSize < (off_t)sizeof(OBCC_Header)) {
+ LOGE("Metadata information file is too small to be correct.\n");
+ return false;
+ }
+
+ if (fstat(mObjFile->getFD(), &stfile) < 0) {
+ LOGE("Unable to stat executable file.\n");
+ return false;
+ }
+
+ if (stfile.st_size < (off_t)ContextManager::ContextSize) {
+ LOGE("Executable file is too small to be correct.\n");
return false;
}
@@ -109,7 +123,7 @@
bool CacheReader::readHeader() {
- if (mFile->seek(0, SEEK_SET) != 0) {
+ if (mInfoFile->seek(0, SEEK_SET) != 0) {
LOGE("Unable to seek to 0. (reason: %s)\n", strerror(errno));
return false;
}
@@ -120,7 +134,7 @@
return false;
}
- if (mFile->read(reinterpret_cast<char *>(mpHeader), sizeof(OBCC_Header)) !=
+ if (mInfoFile->read((char *)mpHeader, sizeof(OBCC_Header)) !=
(ssize_t)sizeof(OBCC_Header)) {
LOGE("Unable to read cache header.\n");
return false;
@@ -179,7 +193,7 @@
off_t offset = mpHeader-> NAME##_offset; \
off_t size = (off_t)mpHeader-> NAME##_size; \
\
- if (mFileSize < offset || mFileSize < offset + size) { \
+ if (mInfoFileSize < offset || mInfoFileSize < offset + size) { \
LOGE(#NAME " section overflow.\n"); \
return false; \
} \
@@ -204,20 +218,8 @@
#undef CHECK_SECTION_OFFSET
- if (mFileSize < mpHeader->context_offset ||
- mFileSize < (off_t)mpHeader->context_offset +
- (off_t)ContextManager::ContextSize) {
- LOGE("context section overflow.\n");
- return false;
- }
-
- long pagesize = sysconf(_SC_PAGESIZE);
- if (mpHeader->context_offset % pagesize != 0) {
- LOGE("context offset must aligned to pagesize.\n");
- return false;
- }
-
// TODO(logan): Move this to some where else.
+ long pagesize = sysconf(_SC_PAGESIZE);
if ((uintptr_t)mpHeader->context_cached_addr % pagesize != 0) {
LOGE("cached address is not aligned to pagesize.\n");
return false;
@@ -238,12 +240,12 @@
/* We have to ensure that some one will deallocate NAME##_raw */ \
AUTO_MANAGED_HOLDER = NAME##_raw; \
\
- if (mFile->seek(mpHeader->NAME##_offset, SEEK_SET) == -1) { \
+ if (mInfoFile->seek(mpHeader->NAME##_offset, SEEK_SET) == -1) { \
LOGE("Unable to seek to " #NAME " section\n"); \
return false; \
} \
\
- if (mFile->read(reinterpret_cast<char *>(NAME##_raw), \
+ if (mInfoFile->read(reinterpret_cast<char *>(NAME##_raw), \
mpHeader->NAME##_size) != (ssize_t)mpHeader->NAME##_size) \
{ \
LOGE("Unable to read " #NAME ".\n"); \
@@ -403,8 +405,7 @@
bool CacheReader::readContext() {
mpResult->mContext =
ContextManager::get().allocateContext(mpHeader->context_cached_addr,
- mFile->getFD(),
- mpHeader->context_offset);
+ mObjFile->getFD(), 0);
if (!mpResult->mContext) {
// Unable to allocate at cached address. Give up.
diff --git a/lib/ExecutionEngine/CacheReader.h b/lib/ExecutionEngine/CacheReader.h
index fd3516d..a208ed6 100644
--- a/lib/ExecutionEngine/CacheReader.h
+++ b/lib/ExecutionEngine/CacheReader.h
@@ -36,8 +36,9 @@
class CacheReader {
private:
- FileHandle *mFile;
- off_t mFileSize;
+ FileHandle *mObjFile;
+ FileHandle *mInfoFile;
+ off_t mInfoFileSize;
OBCC_Header *mpHeader;
OBCC_DependencyTable *mpCachedDependTable;
@@ -53,7 +54,7 @@
public:
CacheReader()
- : mFile(NULL), mFileSize(0), mpHeader(NULL),
+ : mObjFile(NULL), mInfoFile(NULL), mInfoFileSize(0), mpHeader(NULL),
mpCachedDependTable(NULL), mpPragmaList(NULL), mpFuncTable(NULL),
mIsContextSlotNotAvail(false) {
}
@@ -67,7 +68,9 @@
std::make_pair((uint32_t)resType, sha1)));
}
- ScriptCached *readCacheFile(FileHandle *file, Script *s);
+ ScriptCached *readCacheFile(FileHandle *objFile,
+ FileHandle *infoFile,
+ Script *s);
bool isContextSlotNotAvail() const {
return mIsContextSlotNotAvail;
diff --git a/lib/ExecutionEngine/CacheWriter.cpp b/lib/ExecutionEngine/CacheWriter.cpp
index 86775a7..4b53617 100644
--- a/lib/ExecutionEngine/CacheWriter.cpp
+++ b/lib/ExecutionEngine/CacheWriter.cpp
@@ -50,13 +50,17 @@
#undef CHECK_AND_FREE
}
-bool CacheWriter::writeCacheFile(FileHandle *file, Script *S,
+bool CacheWriter::writeCacheFile(FileHandle *objFile,
+ FileHandle *infoFile,
+ Script *S,
uint32_t libRS_threadable) {
- if (!file || file->getFD() < 0) {
+ if (!objFile || objFile->getFD() < 0 ||
+ !infoFile || infoFile->getFD() < 0) {
return false;
}
- mFile = file;
+ mObjFile = objFile;
+ mInfoFile = infoFile;
mpOwner = S;
bool result = prepareHeader(libRS_threadable)
@@ -355,15 +359,6 @@
OFFSET_INCREASE(object_slot_list);
#undef OFFSET_INCREASE
-
- // Context
- long pagesize = sysconf(_SC_PAGESIZE);
- size_t context_offset_rem = offset % pagesize;
- if (context_offset_rem) {
- offset += pagesize - context_offset_rem;
- }
-
- mpHeaderSection->context_offset = offset;
return true;
}
@@ -384,12 +379,12 @@
bool CacheWriter::writeAll() {
#define WRITE_SECTION(NAME, OFFSET, SIZE, SECTION) \
do { \
- if (mFile->seek(OFFSET, SEEK_SET) == -1) { \
+ if (mInfoFile->seek(OFFSET, SEEK_SET) == -1) { \
LOGE("Unable to seek to " #NAME " section for writing.\n"); \
return false; \
} \
\
- if (mFile->write(reinterpret_cast<char *>(SECTION), (SIZE)) != \
+ if (mInfoFile->write(reinterpret_cast<char *>(SECTION), (SIZE)) != \
static_cast<ssize_t>(SIZE)) { \
LOGE("Unable to write " #NAME " section to cache file.\n"); \
return false; \
@@ -413,13 +408,18 @@
WRITE_SECTION_SIMPLE(func_table, mpFuncTableSection);
WRITE_SECTION_SIMPLE(object_slot_list, mpObjectSlotSection);
- WRITE_SECTION(context, mpHeaderSection->context_offset,
- ContextManager::ContextSize,
- mpOwner->getContext());
-
#undef WRITE_SECTION_SIMPLE
#undef WRITE_SECTION
+
+ // Write Context to Executable File
+ char const *context = (char const *)mpOwner->getContext();
+ size_t context_size = ContextManager::ContextSize;
+ if (mObjFile->write(context, context_size) != (ssize_t)context_size) {
+ LOGE("Unable to write context image to executable file\n");
+ return false;
+ }
+
return true;
}
diff --git a/lib/ExecutionEngine/CacheWriter.h b/lib/ExecutionEngine/CacheWriter.h
index 3e3f0af..d492d4a 100644
--- a/lib/ExecutionEngine/CacheWriter.h
+++ b/lib/ExecutionEngine/CacheWriter.h
@@ -33,7 +33,8 @@
private:
Script *mpOwner;
- FileHandle *mFile;
+ FileHandle *mObjFile;
+ FileHandle *mInfoFile;
std::vector<std::pair<char const *, size_t> > mStringPool;
@@ -60,7 +61,9 @@
~CacheWriter();
- bool writeCacheFile(FileHandle *file, Script *S,
+ bool writeCacheFile(FileHandle *objFile,
+ FileHandle *infoFile,
+ Script *S,
uint32_t libRS_threadable);
void addDependency(OBCC_ResourceType resType,
diff --git a/lib/ExecutionEngine/Script.cpp b/lib/ExecutionEngine/Script.cpp
index ec52b60..ee18f47 100644
--- a/lib/ExecutionEngine/Script.cpp
+++ b/lib/ExecutionEngine/Script.cpp
@@ -222,7 +222,8 @@
}
#if USE_OLD_JIT
- std::string objPath(mCacheDir + mCacheName + ".oBCC");
+ std::string objPath(mCacheDir + mCacheName + ".jit-image");
+ std::string infoPath(mCacheDir + mCacheName + ".oBCC"); // TODO: .info instead
#elif USE_MCJIT
std::string objPath(mCacheDir + mCacheName + ".o");
std::string infoPath(mCacheDir + mCacheName + ".info");
@@ -230,17 +231,15 @@
FileHandle objFile;
if (objFile.open(objPath.c_str(), OpenMode::Read) < 0) {
- // Unable to open the cache file in read mode.
+ // Unable to open the executable file in read mode.
return 1;
}
-#if !USE_OLD_JIT && USE_MCJIT
FileHandle infoFile;
if (infoFile.open(infoPath.c_str(), OpenMode::Read) < 0) {
- // Unable to open the cache file in read mode.
+ // Unable to open the metadata information file in read mode.
return 1;
}
-#endif
#if USE_OLD_JIT
CacheReader reader;
@@ -265,12 +264,7 @@
}
// Read cache file
-#if USE_OLD_JIT
- ScriptCached *cached = reader.readCacheFile(&objFile, this);
-#elif USE_MCJIT
ScriptCached *cached = reader.readCacheFile(&objFile, &infoFile, this);
-#endif
-
if (!cached) {
mIsContextSlotNotAvail = reader.isContextSlotNotAvail();
@@ -356,12 +350,10 @@
#endif
!getBooleanProp("debug.bcc.nocache")) {
- FileHandle objFile;
-
#if USE_OLD_JIT
- std::string objPath(mCacheDir + mCacheName + ".oBCC");
+ std::string objPath(mCacheDir + mCacheName + ".jit-image");
+ std::string infoPath(mCacheDir + mCacheName + ".oBCC");
#elif USE_MCJIT
- FileHandle infoFile;
std::string objPath(mCacheDir + mCacheName + ".o");
std::string infoPath(mCacheDir + mCacheName + ".info");
#endif
@@ -376,11 +368,11 @@
::unlink(infoPath.c_str());
#endif
- if (objFile.open(objPath.c_str(), OpenMode::Write) >= 0
-#if !USE_OLD_JIT && USE_MCJIT
- && infoFile.open(infoPath.c_str(), OpenMode::Write) >= 0
-#endif
- ) {
+ FileHandle objFile;
+ FileHandle infoFile;
+
+ if (objFile.open(objPath.c_str(), OpenMode::Write) >= 0 &&
+ infoFile.open(infoPath.c_str(), OpenMode::Write) >= 0) {
#if USE_OLD_JIT
CacheWriter writer;
@@ -409,11 +401,7 @@
"__isThreadable");
}
-#if USE_OLD_JIT
- if (!writer.writeCacheFile(&objFile, this, libRS_threadable)) {
-#elif USE_MCJIT
if (!writer.writeCacheFile(&objFile, &infoFile, this, libRS_threadable)) {
-#endif
objFile.truncate();
objFile.close();
@@ -422,7 +410,6 @@
objPath.c_str(), strerror(errno));
}
-#if !USE_OLD_JIT && USE_MCJIT
infoFile.truncate();
infoFile.close();
@@ -430,7 +417,6 @@
LOGE("Unable to remove the invalid cache file: %s. (reason: %s)\n",
infoPath.c_str(), strerror(errno));
}
-#endif
}
}
}