Changeset 6209


Ignore:
Timestamp:
Nov 26, 2018, 12:06:06 PM (5 months ago)
Author:
nmedfort
Message:

Initial cache janitor daemon test

Location:
icGREP/icgrep-devel/icgrep
Files:
2 added
2 deleted
11 edited

Legend:

Unmodified
Added
Removed
  • icGREP/icgrep-devel/icgrep/CMakeLists.txt

    r6207 r6209  
    6060SET(OBJECT_CACHE_SRC toolchain/object_cache.cpp)
    6161
     62SET(OBJECT_CACHE_DAEMON_SRC toolchain/object_cache_daemon.cpp)
     63
    6264SET(GREP_CORE_SRC kernels/linebreak_kernel.cpp kernels/grep_kernel.cpp kernels/scanmatchgen.cpp )
    6365
    64 SET(TOOLCHAIN_SRC toolchain/toolchain.cpp toolchain/object_cache_manager.cpp)
     66SET(TOOLCHAIN_SRC toolchain/toolchain.cpp)
    6567
    6668SET(DRIVER_SRC toolchain/driver.cpp toolchain/cpudriver.cpp)
     
    134136add_executable(lz4_grep grep_interface.cpp lz4_grep.cpp lz4/grep/lz4_grep_base_generator.cpp lz4/grep/lz4_grep_bytestream_generator.cpp lz4/grep/lz4_grep_bytestream_generator.h lz4/grep/lz4_grep_bitstream_generator.cpp lz4/grep/lz4_grep_bitstream_generator.h lz4/grep/lz4_grep_swizzle_generator.cpp lz4/grep/lz4_grep_swizzle_generator.h)
    135137add_executable(idisa_test idisa_test.cpp)
    136 
     138add_executable(cachejanitord ${OBJECT_CACHE_DAEMON_SRC})
    137139
    138140#Exclude CoRE from default build.
     
    150152target_link_libraries (lz4_grep LZ4_Lib UCDlib PabloADT RegExpCompiler CodeGen CCADT CodeGen FileSelect ${REQ_LLVM_LIBRARIES} ${Boost_LIBRARIES} ${CUDA_LIB})
    151153target_link_libraries (idisa_test CodeGen PabloADT ${REQ_LLVM_LIBRARIES} ${Boost_LIBRARIES} ${CUDA_LIB})
     154target_link_libraries (cachejanitord ${Boost_LIBRARIES})
    152155
    153156
     
    188191  SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /GR-")
    189192ELSE() # using Clang, GCC, Intel C++, etc
    190   SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-rtti") 
     193  SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-rtti")
    191194ENDIF()
    192195
     
    351354  WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}/../QA
    352355  COMMAND python abc_testgen.py)
    353  
     356
    354357add_custom_target (abc_test
    355358  WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}/../QA
     
    373376  WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}/../performance)
    374377
    375 
  • icGREP/icgrep-devel/icgrep/base64.cpp

    r6184 r6209  
    2626#include <mutex>
    2727#include <kernels/pipeline_builder.h>
     28
    2829
    2930using namespace llvm;
     
    7576    }
    7677    fn_ptr(fd);
    77     close(fd);   
     78    close(fd);
    7879}
    7980
  • icGREP/icgrep-devel/icgrep/grep/grep_engine.cpp

    r6207 r6209  
    7878
    7979using Alphabets = ICGrepKernel::Alphabets;
    80    
     80
    8181void GrepCallBackObject::handle_signal(unsigned s) {
    8282    if (static_cast<GrepSignal>(s) == GrepSignal::BinaryFile) {
     
    9494    reinterpret_cast<MatchAccumulator *>(accum_addr)->finalize_match(buffer_end);
    9595}
    96    
     96
    9797
    9898inline static size_t ceil_log2(const size_t v) {
     
    184184}
    185185
    186    
     186
    187187bool GrepEngine::hasComponent(Component compon_set, Component c) {
    188188    return (static_cast<component_t>(compon_set) & static_cast<component_t>(c)) != 0;
     
    196196    mGrepRecordBreak = b;
    197197}
    198    
     198
    199199void GrepEngine::initFileResult(std::vector<boost::filesystem::path> & paths) {
    200200    const unsigned n = paths.size();
     
    218218        anchorRE = anchorName;
    219219    }
    220    
     220
    221221    mREs = REs;
    222222    bool allAnchored = true;
     
    283283
    284284    bool requiresComplexTest = true;
    285    
     285
    286286
    287287
     
    579579void GrepEngine::showResult(uint64_t grepResult, const std::string & fileName, std::ostringstream & strm) {
    580580}
    581    
     581
    582582void CountOnlyEngine::showResult(uint64_t grepResult, const std::string & fileName, std::ostringstream & strm) {
    583583    if (mShowFileNames) strm << linePrefix(fileName);
    584584    strm << grepResult << "\n";
    585585}
    586    
     586
    587587void MatchOnlyEngine::showResult(uint64_t grepResult, const std::string & fileName, std::ostringstream & strm) {
    588588    if (grepResult == mRequiredCount) {
     
    704704            mFileStatus[printIdx] = FileStatus::PrintComplete;
    705705            printIdx = mNextFileToPrint++;
    706         } else {
    707             ObjectCacheManager::performIncrementalCacheCleanupStep();
    708         }
    709         sched_yield();
     706        }
    710707    }
    711708
     
    714711    } else {
    715712        // Always perform one final cache cleanup step.
    716         ObjectCacheManager::performIncrementalCacheCleanupStep();
    717713        if (mGrepStdIn) {
    718714            std::ostringstream s;
     
    725721}
    726722
    727    
    728    
     723
     724
    729725InternalSearchEngine::InternalSearchEngine(BaseDriver &driver) :
    730726    mGrepRecordBreak(GrepRecordBreakKind::LF),
     
    732728    mGrepDriver(driver),
    733729    mMainMethod(nullptr) {}
    734    
     730
    735731void InternalSearchEngine::grepCodeGen(re::RE * matchingRE, re::RE * excludedRE) {
    736732    auto & idb = mGrepDriver.getBuilder();
    737733    mSaveSegmentPipelineParallel = codegen::SegmentPipelineParallel;
    738734    codegen::SegmentPipelineParallel = false;
    739    
     735
    740736    re::CC * breakCC = nullptr;
    741737    if (mGrepRecordBreak == GrepRecordBreakKind::Null) {
     
    774770    const auto RBname = (mGrepRecordBreak == GrepRecordBreakKind::Null) ? "Null" : "LF";
    775771
    776    
     772
    777773    StreamSet * BasisBits = nullptr;
    778    
     774
    779775    if (matchAllLines && excludeNothing) {
    780776        E->CreateKernelCall<DirectCharacterClassKernelBuilder>(RBname, std::vector<re::CC *>{breakCC}, ByteStream, RecordBreakStream);
     
    784780        E->CreateKernelCall<ParabixCharacterClassKernelBuilder>(RBname, std::vector<re::CC *>{breakCC}, BasisBits, RecordBreakStream);
    785781    }
    786    
     782
    787783    StreamSet * MatchingRecords = nullptr;
    788784    if (matchAllLines) {
  • icGREP/icgrep-devel/icgrep/icgrep.files

    r6193 r6209  
    22kernels/directorysearch.cpp
    33kernels/directorysearch.h
     4toolchain/object_cache.cpp
     5toolchain/object_cache.h
     6toolchain/object_cache_daemon.cpp
     7toolchain/object_cache_util.hpp
    48wc.cpp
    59CMakeLists.txt
     
    7781toolchain/toolchain.h
    7882toolchain/toolchain.cpp
    79 toolchain/object_cache_manager.h
    80 toolchain/object_cache_manager.cpp
    8183toolchain/object_cache.h
    8284toolchain/object_cache.cpp
  • icGREP/icgrep-devel/icgrep/kernels/kernel.cpp

    r6187 r6209  
    2929#include <sstream>
    3030
    31 
    3231using namespace llvm;
    3332using namespace boost;
    34 using boost::container::flat_set;
    3533
    3634namespace kernel {
     
    403401    // Calculate and/or load the accessible and writable item counts. If they are unneeded,
    404402    // LLVM ought to recognize them as dead code and remove them.
    405     generateKernelMethod(b); // must be overridden by the Kernel subtype   
     403    generateKernelMethod(b); // must be overridden by the Kernel subtype
    406404    if (LLVM_UNLIKELY(codegen::DebugOptionIsSet(codegen::EnableMProtect))) {
    407405        b->CreateMProtect(mHandle, CBuilder::Protect::READ);
     
    601599            fields.push_back(scalar.getType());
    602600        }
    603         mKernelStateType = StructType::create(b->getContext(), fields, getName());       
     601        mKernelStateType = StructType::create(b->getContext(), fields, getName());
    604602    }
    605603
     
    644642    if (LLVM_UNLIKELY(mKernelStateType != nullptr)) {
    645643        report_fatal_error(getName() + ": cannot prepare kernel after kernel state finalized");
    646     } 
     644    }
    647645    addBaseKernelProperties(b);
    648646    mKernelStateType = getModule()->getTypeByName(getName());
  • icGREP/icgrep-devel/icgrep/toolchain/cpudriver.cpp

    r6189 r6209  
    7474//    InitializeNativeTargetAsmParser();
    7575    llvm::sys::DynamicLibrary::LoadLibraryPermanently(nullptr);
    76    
     76
    7777
    7878    #ifdef ORCJIT
     
    101101
    102102    mTarget = builder.selectTarget();
    103    
     103
    104104    if (mTarget == nullptr) {
    105105        throw std::runtime_error("Could not selectTarget");
     
    113113    }
    114114    #endif
    115     auto cache = ObjectCacheManager::getObjectCache();
     115    auto cache = ParabixObjectCache::getInstance();
    116116    if (cache) {
    117117        #ifdef ORCJIT
     
    152152std::string CPUDriver::getMangledName(std::string s) {
    153153    #if LLVM_VERSION_INTEGER >= LLVM_VERSION_CODE(3, 9, 0)
    154     DataLayout DL(mTarget->createDataLayout());   
     154    DataLayout DL(mTarget->createDataLayout());
    155155    std::string MangledName;
    156156    raw_string_ostream MangledNameStream(MangledName);
     
    170170    initializeCodeGen(*Registry);
    171171    initializeLowerIntrinsicsPass(*Registry);
    172    
     172
    173173    if (LLVM_UNLIKELY(codegen::ShowUnoptimizedIROption != codegen::OmittedOption)) {
    174174        if (LLVM_LIKELY(mIROutputStream == nullptr)) {
  • icGREP/icgrep-devel/icgrep/toolchain/cpudriver.h

    r6189 r6209  
    3333#endif
    3434
    35 class ObjectCacheManager;
    36 
    3735class CPUDriver final : public BaseDriver {
    3836public:
     
    5149
    5250    std::string getMangledName(std::string s);
    53    
     51
    5452    llvm::legacy::PassManager preparePassManager();
    5553
  • icGREP/icgrep-devel/icgrep/toolchain/driver.cpp

    r6184 r6209  
    5353 ** ------------------------------------------------------------------------------------------------------------- */
    5454void BaseDriver::addKernel(Kernel * const kernel) {
    55     if (ObjectCacheManager::checkForCachedKernel(iBuilder, kernel)) {
     55    if (ParabixObjectCache::checkForCachedKernel(iBuilder, kernel)) {
    5656        assert (kernel->getModule());
    5757        mCachedKernel.emplace_back(kernel);
     
    7171, mMainModule(new llvm::Module(moduleName, *mContext))
    7272, iBuilder(nullptr) {
    73     ObjectCacheManager::initializeCacheSystems();
     73    ParabixObjectCache::initializeCacheSystems();
    7474}
    7575
  • icGREP/icgrep-devel/icgrep/toolchain/driver.h

    r6184 r6209  
    77#include <kernels/streamset.h>
    88#include <kernels/relationship.h>
    9 #include <toolchain/object_cache_manager.h>
    109#include <util/slab_allocator.h>
    1110#include <string>
     
    1716namespace kernel { class PipelineBuilder; }
    1817class CBuilder;
    19 
    20 class ObjectCacheManager;
    2118
    2219class BaseDriver {
     
    5451
    5552    virtual void * finalizeObject(llvm::Function * mainMethod) = 0;
    56    
     53
    5754    virtual ~BaseDriver();
    5855
  • icGREP/icgrep-devel/icgrep/toolchain/object_cache.cpp

    r6193 r6209  
    1 #include "toolchain.h"
    2 #include "object_cache.h"
     1#include "object_cache.h"
     2#include "object_cache_util.hpp"
    33#include <kernels/kernel.h>
    44#include <kernels/kernel_builder.h>
     
    1010#include <llvm/Support/Debug.h>
    1111#include <llvm/IR/Module.h>
    12 #include <sys/file.h>
    13 #include <sys/stat.h>
    14 #include <fcntl.h>
    15 #include <boost/filesystem.hpp>
    16 #include <boost/range/iterator_range.hpp>
    17 #include <boost/container/flat_set.hpp>
     12#include <toolchain/toolchain.h>
    1813#if LLVM_VERSION_INTEGER < LLVM_VERSION_CODE(4, 0, 0)
    1914#include <llvm/Bitcode/ReaderWriter.h>
     
    2318#endif
    2419#include <llvm/IR/Verifier.h>
    25 #include <ctime>
     20#include <boost/lexical_cast.hpp>
    2621
    2722using namespace llvm;
    28 namespace fs = boost::filesystem;
    29 
    30 #ifdef NDEBUG
    31 #define CACHE_ENTRY_MAX_HOURS (24 * codegen::CacheDaysLimit)
    32 #else
    33 #define CACHE_ENTRY_MAX_HOURS (1)
    34 #endif
    35 
    36 #define SECONDS_PER_HOUR (3600)
     23using namespace boost;
     24
     25using Path = ParabixObjectCache::Path;
     26
     27std::unique_ptr<ParabixObjectCache> ParabixObjectCache::mInstance;
     28
    3729//===----------------------------------------------------------------------===//
    3830// Object cache (based on tools/lli/lli.cpp, LLVM 3.6.1)
     
    7769const static auto SIGNATURE = "signature";
    7870
     71/** ------------------------------------------------------------------------------------------------------------- *
     72 * @brief getSignature
     73 ** ------------------------------------------------------------------------------------------------------------- */
    7974const MDString * getSignature(const llvm::Module * const M) {
    8075    NamedMDNode * const sig = M->getNamedMetadata(SIGNATURE);
     
    8782}
    8883
     84/** ------------------------------------------------------------------------------------------------------------- *
     85 * @brief loadCachedObjectFile
     86 ** ------------------------------------------------------------------------------------------------------------- */
    8987bool ParabixObjectCache::loadCachedObjectFile(const std::unique_ptr<kernel::KernelBuilder> & idb, kernel::Kernel * const kernel) {
    9088    if (LLVM_LIKELY(kernel->isCachable())) {
     
    108106        sys::path::append(fileName, CACHE_PREFIX);
    109107        fileName.append(moduleId);
    110         fileName.append(".kernel");
    111 
    112         auto kernelBuffer = MemoryBuffer::getFile(fileName.c_str(), -1, false);
     108        fileName.append("." KERNEL_FILE_EXTENSION);
     109
     110        auto kernelBuffer = MemoryBuffer::getFile(fileName, -1, false);
    113111        if (kernelBuffer) {
    114112            #if LLVM_VERSION_INTEGER < LLVM_VERSION_CODE(4, 0, 0)
     
    127125                    }
    128126                }
    129                 sys::path::replace_extension(fileName, ".o");
     127                sys::path::replace_extension(fileName, "." OBJECT_FILE_EXTENSION);
    130128                auto objectBuffer = MemoryBuffer::getFile(fileName.c_str(), -1, false);
    131129                if (LLVM_LIKELY(objectBuffer)) {
     
    137135                    mCachedObject.emplace(moduleId, std::make_pair(m, std::move(objectBuffer.get())));
    138136                    // update the modified time of the .kernel, .o and .sig files
    139                     time_t access_time = time(0);
     137                    const auto access_time = currentTime();
    140138                    fs::last_write_time(fileName.c_str(), access_time);
    141                     sys::path::replace_extension(fileName, ".kernel");
     139                    sys::path::replace_extension(fileName, "." KERNEL_FILE_EXTENSION);
    142140                    fs::last_write_time(fileName.c_str(), access_time);
    143                     mNewlyCached++;
    144141                    return true;
    145142                }
     
    163160}
    164161
    165 // A new module has been compiled. If it is cacheable and no conflicting module
    166 // exists, write it out.
     162/** ------------------------------------------------------------------------------------------------------------- *
     163 * @brief notifyObjectCompiled
     164 *
     165 * A new module has been compiled. If it is cacheable and no conflicting module exists, write it out.
     166 ** ------------------------------------------------------------------------------------------------------------- */
    167167void ParabixObjectCache::notifyObjectCompiled(const Module * M, MemoryBufferRef Obj) {
    168168    if (LLVM_LIKELY(M->getNamedMetadata(CACHEABLE))) {
     
    172172        sys::path::append(objectName, CACHE_PREFIX);
    173173        objectName.append(moduleId);
    174         objectName.append(".o");
     174        objectName.append("." OBJECT_FILE_EXTENSION);
    175175
    176176        // Write the object code
     
    197197        }
    198198
    199         sys::path::replace_extension(objectName, ".kernel");
     199        sys::path::replace_extension(objectName, "." KERNEL_FILE_EXTENSION);
    200200        raw_fd_ostream kernelFile(objectName.str(), EC, sys::fs::F_None);
    201201        WriteBitcodeToFile(H.get(), kernelFile);
    202202        kernelFile.close();
    203         mCacheRetrievals++;
    204     }
    205 }
    206 
    207 void ParabixObjectCache::performIncrementalCacheCleanupStep() {
    208     //if (LLVM_LIKELY(mCleanupMutex.try_lock())) {
    209         try {
    210 
    211             // Simple clean-up policy: files that haven't been touched by the
    212             // driver in MaxCacheEntryHours are deleted.
    213             // Each cleanup step removes at monst mNewlyCached + 1 files
    214             // and examines at most mNewlyCached + mCacheRetrievals files.
    215 
    216             const auto now = std::time(nullptr);
    217             unsigned removed = 0;
    218             unsigned examined = 0;
    219             unsigned removalLimit = mNewlyCached + 1;
    220             unsigned examineLimit = mNewlyCached + mCacheRetrievals;
    221             while (LLVM_LIKELY(mCleanupIterator != fs::directory_iterator())) {
    222                 const auto i = mCleanupIterator;
    223                 ++mCleanupIterator;
    224                 const auto & e = i->path();
    225                 if (LLVM_LIKELY(fs::is_regular_file(e))) {
    226                     const auto expiry = fs::last_write_time(e) + (CACHE_ENTRY_MAX_HOURS * SECONDS_PER_HOUR);
    227                     if (now > expiry) {
    228                         fs::remove(e);
    229                         removed++;
    230                         if (removed >= removalLimit) break;
    231                     }
    232                     examined++;
    233                     if (examined > examineLimit) break;
    234                 }
    235                 unsigned skip = rand() % 23;
    236                 while ((mCleanupIterator != fs::directory_iterator()) && (skip > 0)) {
    237                     ++mCleanupIterator;
    238                     skip--;
    239                 }
    240             }
    241         } catch (...) {
    242             fs::path p(mCachePath.str());
    243             mCleanupIterator = fs::directory_iterator(p);
    244         }
    245 //        mCleanupMutex.unlock();
    246 //    }
    247 }
    248 
     203    }
     204}
     205
     206/** ------------------------------------------------------------------------------------------------------------- *
     207 * @brief getObject
     208 ** ------------------------------------------------------------------------------------------------------------- */
    249209std::unique_ptr<MemoryBuffer> ParabixObjectCache::getObject(const Module * module) {
    250210    const auto f = mCachedObject.find(module->getModuleIdentifier());
     
    256216}
    257217
    258 ParabixObjectCache::ParabixObjectCache(const StringRef dir)
    259 : mCacheRetrievals(0)
    260 , mNewlyCached(0)
    261 , mCachePath(dir) {
    262     fs::path p(mCachePath.str());
    263     if (LLVM_LIKELY(!mCachePath.empty())) {
    264         sys::fs::create_directories(mCachePath);
    265     }
    266     mCleanupIterator = fs::directory_iterator(p);
    267 }
    268 
    269 inline ParabixObjectCache::Path getDefaultPath() {
    270     // $HOME/.cache/parabix/
    271     ParabixObjectCache::Path cachePath;
    272 #if LLVM_VERSION_INTEGER < LLVM_VERSION_CODE(3, 7, 0)
    273     sys::path::user_cache_directory(cachePath, "parabix");
    274 #else
    275     sys::path::home_directory(cachePath);
    276     sys::path::append(cachePath, ".cache", "parabix");
     218/** ------------------------------------------------------------------------------------------------------------- *
     219 * @brief checkForCachedKernel
     220 ** ------------------------------------------------------------------------------------------------------------- */
     221bool ParabixObjectCache::checkForCachedKernel(const std::unique_ptr<kernel::KernelBuilder> & b, kernel::Kernel * const kernel) noexcept {
     222    return mInstance.get() && mInstance->loadCachedObjectFile(b, kernel);
     223}
     224
     225/** ------------------------------------------------------------------------------------------------------------- *
     226 * @brief requiresCacheCleanUp
     227 ** ------------------------------------------------------------------------------------------------------------- */
     228inline bool ParabixObjectCache::requiresCacheCleanUp() noexcept {
     229    return FileLock(fs::path{mCachePath.str()}).locked();
     230}
     231
     232/** ------------------------------------------------------------------------------------------------------------- *
     233 * @brief initiateCacheCleanUp
     234 ** ------------------------------------------------------------------------------------------------------------- */
     235void ParabixObjectCache::initiateCacheCleanUp() noexcept {
     236    if (LLVM_UNLIKELY(requiresCacheCleanUp())) {
     237        // syslog?
     238        if (fork() == 0) {
     239            char * const cachePath = const_cast<char *>(mCachePath.c_str());
     240            char * args[3] = {const_cast<char *>(CACHE_JANITOR_FILE_NAME), cachePath, nullptr};
     241            fs::path path(codegen::ProgramName);
     242            path.remove_filename().append(CACHE_JANITOR_FILE_NAME);
     243            if (execvp(const_cast<char *>(path.c_str()), args) < 0) {
     244                // syslog?
     245            }
     246        }
     247    }
     248}
     249
     250/** ------------------------------------------------------------------------------------------------------------- *
     251 * @brief getDefaultCachePath
     252 ** ------------------------------------------------------------------------------------------------------------- */
     253inline void getDefaultCachePath(Path & configPath) {
     254    // default: $HOME/.cache/parabix/
     255    sys::path::home_directory(configPath);
     256    sys::path::append(configPath, ".cache", "parabix");
     257}
     258
     259#if 0
     260
     261/** ------------------------------------------------------------------------------------------------------------- *
     262 * @brief getConfigPath
     263 ** ------------------------------------------------------------------------------------------------------------- */
     264inline Path getConfigPath() {
     265    // $HOME/.config/parabix/cache.cfg
     266    Path configPath;
     267    sys::path::home_directory(configPath);
     268    sys::path::append(configPath, ".config", "parabix");
     269    sys::fs::create_directories(configPath);
     270    sys::path::append(configPath, "cache.cfg");
     271    return configPath;
     272}
     273
     274/** ------------------------------------------------------------------------------------------------------------- *
     275 * @brief loadCacheSettings
     276 ** ------------------------------------------------------------------------------------------------------------- */
     277inline size_t parseInt(const StringRef & str, const StringRef & label) {
     278    try {
     279        return lexical_cast<size_t>(str.data(), str.size());
     280    } catch(const bad_lexical_cast &) {
     281        errs() << "configuration for " << label << " must be an integer";
     282        exit(-1);
     283    }
     284}
     285
    277286#endif
    278     return cachePath;
    279 }
    280 
    281 ParabixObjectCache::ParabixObjectCache()
    282 : ParabixObjectCache(getDefaultPath()) {
    283 
    284 }
     287
     288/** ------------------------------------------------------------------------------------------------------------- *
     289 * @brief loadCacheSettings
     290 ** ------------------------------------------------------------------------------------------------------------- */
     291inline void ParabixObjectCache::loadCacheSettings() noexcept {
     292    getDefaultCachePath(mCachePath);
     293    #if 0
     294
     295    const auto configPath = getConfigPath();
     296    auto configFile = MemoryBuffer::getFile(configPath);
     297
     298    // default: $HOME/.cache/parabix/
     299    sys::path::home_directory(mCachePath);
     300    sys::path::append(mCachePath, ".cache", "parabix");
     301    // default: 1 week
     302    mCacheExpirationDelay = CACHE_ENTRY_EXPIRY_PERIOD;
     303
     304    if (LLVM_UNLIKELY(!!configFile)) {
     305        const StringRef config = (*configFile)->getBuffer();
     306        #define ASCII_WHITESPACE " \f\n\r\t\v"
     307        #define ASCII_WHITESPACE_OR_EQUALS (ASCII_WHITESPACE "+")
     308        size_t nameStart = 0;
     309        for (;;) {
     310
     311            const auto nameEnd = config.find_first_of(ASCII_WHITESPACE_OR_EQUALS, nameStart);
     312            if (nameEnd == StringRef::npos) break;
     313            const auto afterEquals = config.find_first_of('=', nameEnd) + 1;
     314            if (LLVM_UNLIKELY(afterEquals == StringRef::npos)) break;
     315            const auto valueStart = config.find_first_not_of(ASCII_WHITESPACE, afterEquals);
     316            if (LLVM_UNLIKELY(valueStart == StringRef::npos)) break;
     317            const auto valueEnd = config.find_first_of(ASCII_WHITESPACE, valueStart);
     318            if (LLVM_UNLIKELY(valueEnd == StringRef::npos)) break;
     319            const auto name = config.slice(nameStart, nameEnd);
     320            const auto value = config.slice(valueStart, valueEnd);
     321
     322            if (name.equals_lower("cachepath")) {
     323                mCachePath.assign(value);
     324            } else if (name.equals_lower("cachedayslimit")) {
     325                mCacheExpirationDelay = parseInt(value, "cachedayslimit");
     326            }
     327            // get the next name start
     328            nameStart = config.find_first_not_of(ASCII_WHITESPACE, valueEnd + 1);
     329        }
     330    }
     331    #endif
     332    sys::fs::create_directories(mCachePath);
     333}
     334
     335/** ------------------------------------------------------------------------------------------------------------- *
     336 * @brief saveCachePath
     337 ** ------------------------------------------------------------------------------------------------------------- */
     338inline void ParabixObjectCache::saveCacheSettings() noexcept {
     339
     340
     341}
     342
     343/** ------------------------------------------------------------------------------------------------------------- *
     344 * @brief initializeCacheSystems
     345 ** ------------------------------------------------------------------------------------------------------------- */
     346void ParabixObjectCache::initializeCacheSystems() noexcept {
     347    if (LLVM_LIKELY(mInstance.get() == nullptr && codegen::EnableObjectCache)) {
     348        mInstance.reset(new ParabixObjectCache());
     349    }
     350}
     351
     352ParabixObjectCache::ParabixObjectCache() {
     353    loadCacheSettings();
     354    initiateCacheCleanUp();
     355}
  • icGREP/icgrep-devel/icgrep/toolchain/object_cache.h

    r6193 r6209  
    1212#include <llvm/ADT/StringRef.h>
    1313#include <boost/container/flat_map.hpp>
    14 #include <boost/filesystem.hpp>
    15 #include <vector>
    1614#include <string>
    17 //#include <mutex>
    1815
    1916namespace llvm { class Module; }
     
    3633//
    3734
    38 class ParabixObjectCache final : public llvm::ObjectCache {   
     35class ParabixObjectCache final : public llvm::ObjectCache {
    3936    template <typename K, typename V>
    4037    using Map = boost::container::flat_map<K, V>;
    41     using ModuleCache = Map<std::string, std::pair<llvm::Module *, std::unique_ptr<llvm::MemoryBuffer>>>;   
     38    using ModuleCache = Map<std::string, std::pair<llvm::Module *, std::unique_ptr<llvm::MemoryBuffer>>>;
     39    using Instance = std::unique_ptr<ParabixObjectCache>;
    4240public:
    4341    using Path = llvm::SmallString<128>;
    44     ParabixObjectCache(const llvm::StringRef dir);
     42
     43    static bool checkForCachedKernel(const std::unique_ptr<kernel::KernelBuilder> & b, kernel::Kernel * const kernel) noexcept;
     44
     45    static void initializeCacheSystems() noexcept;
     46
     47    static ParabixObjectCache * getInstance() {
     48        return mInstance.get();
     49    }
     50
     51    bool loadCachedObjectFile(const std::unique_ptr<kernel::KernelBuilder> & idb, kernel::Kernel * const kernel);
     52
     53    void notifyObjectCompiled(const llvm::Module * M, llvm::MemoryBufferRef Obj) override;
     54
     55    std::unique_ptr<llvm::MemoryBuffer> getObject(const llvm::Module * M) override;
     56
     57protected:
     58
    4559    ParabixObjectCache();
    46     bool loadCachedObjectFile(const std::unique_ptr<kernel::KernelBuilder> & idb, kernel::Kernel * const kernel);
    47     void notifyObjectCompiled(const llvm::Module * M, llvm::MemoryBufferRef Obj) override;
    48     std::unique_ptr<llvm::MemoryBuffer> getObject(const llvm::Module * M) override;
    49     void performIncrementalCacheCleanupStep();
     60    void loadCacheSettings() noexcept;
     61    void saveCacheSettings() noexcept;
     62
    5063private:
    51 //    std::mutex mCleanupMutex;
    52     unsigned mCacheRetrievals;
    53     unsigned mNewlyCached;
    54     boost::filesystem::directory_iterator mCleanupIterator;
    55     ModuleCache mCachedObject;
    56     const Path mCachePath;
    57    
     64    void initiateCacheCleanUp() noexcept;
     65    bool requiresCacheCleanUp() noexcept;
     66private:
     67    static Instance     mInstance;
     68    ModuleCache         mCachedObject;
     69    Path                mCachePath;
    5870};
    5971
Note: See TracChangeset for help on using the changeset viewer.