source: icGREP/icgrep-devel/icgrep/grep_engine.cpp @ 5819

Last change on this file since 5819 was 5819, checked in by cameron, 14 months ago

Bug fix for multiplexed alphabet mode

File size: 20.1 KB
RevLine 
[4324]1/*
[5476]2 *  Copyright (c) 2017 International Characters.
[4324]3 *  This software is licensed to the public under the Open Software License 3.0.
4 *  icgrep is a trademark of International Characters.
5 */
6
[5234]7#include "grep_engine.h"
[5476]8#include "grep_interface.h"
[5267]9#include <llvm/IR/Module.h>
[5234]10#include <boost/filesystem.hpp>
[5206]11#include <UCD/resolve_properties.h>
[5585]12#include <kernels/charclasses.h>
[5142]13#include <kernels/cc_kernel.h>
[5404]14#include <kernels/grep_kernel.h>
[5357]15#include <kernels/linebreak_kernel.h>
[5338]16#include <kernels/streams_merge.h>
[5429]17#include <kernels/source_kernel.h>
[5234]18#include <kernels/s2p_kernel.h>
19#include <kernels/scanmatchgen.h>
20#include <kernels/streamset.h>
[5450]21#include <kernels/until_n.h>
[5436]22#include <kernels/kernel_builder.h>
[5087]23#include <pablo/pablo_kernel.h>
[5234]24#include <re/re_cc.h>
[5769]25#include <re/casing.h>
[5779]26#include <re/exclude_CC.h>
[5234]27#include <re/re_toolchain.h>
[5425]28#include <toolchain/toolchain.h>
[5770]29#include <re/re_name_resolve.h>
[5585]30#include <re/re_collect_unicodesets.h>
31#include <re/re_multiplex.h>
[5772]32#include <re/grapheme_clusters.h>
[5801]33#include <re/printer_re.h>
[5700]34#include <toolchain/toolchain.h>
[5464]35#include <toolchain/cpudriver.h>
[5234]36#include <iostream>
[5369]37#include <cc/multiplex_CCs.h>
[5377]38#include <llvm/Support/raw_ostream.h>
[5418]39#include <util/aligned_allocator.h>
[5386]40#include <sys/stat.h>
[5418]41#include <fcntl.h>
[5484]42#include <errno.h>
[5696]43#include <llvm/ADT/STLExtras.h> // for make_unique
[5700]44#include <llvm/Support/CommandLine.h>
[5735]45#include <llvm/Support/Debug.h>
[5762]46#include <sched.h>
[5377]47
[5241]48using namespace parabix;
[5267]49using namespace llvm;
[5795]50using namespace cc;
51
[5703]52static cl::opt<int> Threads("t", cl::desc("Total number of threads."), cl::init(2));
[5241]53
[5473]54namespace grep {
55
[5704]56// Grep Engine construction and initialization.
[5770]57
[5704]58GrepEngine::GrepEngine() :
59    mGrepDriver(nullptr),
[5735]60    mNextFileToGrep(0),
61    mNextFileToPrint(0),
[5704]62    grepMatchFound(false),
[5735]63    mMoveMatchesToEOL(true),
64    mEngineThread(pthread_self()) {}
[5770]65
[5704]66GrepEngine::~GrepEngine() {
67    delete mGrepDriver;
68}
[5770]69
[5704]70QuietModeEngine::QuietModeEngine() : GrepEngine() {
71    mMoveMatchesToEOL = false;
72}
[5473]73
[5704]74MatchOnlyEngine::MatchOnlyEngine(bool showFilesWithoutMatch) :
75    GrepEngine(), mRequiredCount(showFilesWithoutMatch) {
76    mFileSuffix = NullFlag ? std::string("\0", 1) : "\n";
77    mMoveMatchesToEOL = false;
78}
[5484]79
[5704]80CountOnlyEngine::CountOnlyEngine() : GrepEngine() {
81    mFileSuffix = ":";
82}
[5484]83
[5704]84EmitMatchesEngine::EmitMatchesEngine() : GrepEngine() {
85    mFileSuffix = InitialTabFlag ? "\t:" : ":";
86    if (LineRegexpFlag) mMoveMatchesToEOL = false;
[5484]87}
[5704]88
89void GrepEngine::initFileResult(std::vector<std::string> & filenames) {
[5732]90    const unsigned n = filenames.size();
[5704]91    mResultStrs.resize(n);
[5771]92    mFileStatus.resize(n, FileStatus::Pending);
[5704]93    inputFiles = filenames;
94}
95
96// Code Generation
97//
98// All engines share a common pipeline to compute a stream of Matches from a given input Bytestream.
99
100std::pair<StreamSetBuffer *, StreamSetBuffer *> GrepEngine::grepPipeline(std::vector<re::RE *> & REs, StreamSetBuffer * ByteStream) {
101    auto & idb = mGrepDriver->getBuilder();
102    const unsigned segmentSize = codegen::SegmentSize;
103    const unsigned bufferSegments = codegen::BufferSegments * codegen::ThreadNum;
104    const unsigned encodingBits = 8;
[5770]105
[5793]106    StreamSetBuffer * BasisBits = mGrepDriver->addBuffer<CircularBuffer>(idb, idb->getStreamSetTy(encodingBits, 1), segmentSize * bufferSegments);
[5755]107    kernel::Kernel * s2pk = mGrepDriver->addKernelInstance<kernel::S2PKernel>(idb);
[5704]108    mGrepDriver->makeKernelCall(s2pk, {ByteStream}, {BasisBits});
[5770]109
[5793]110    StreamSetBuffer * LineFeedStream = mGrepDriver->addBuffer<CircularBuffer>(idb, idb->getStreamSetTy(1, 1), segmentSize * bufferSegments);
[5782]111    kernel::Kernel * linefeedK = mGrepDriver->addKernelInstance<kernel::LineFeedKernelBuilder>(idb, encodingBits);
112    mGrepDriver->makeKernelCall(linefeedK, {BasisBits}, {LineFeedStream});
113
[5755]114    StreamSetBuffer * LineBreakStream = mGrepDriver->addBuffer<CircularBuffer>(idb, idb->getStreamSetTy(1, 1), segmentSize * bufferSegments);
[5782]115    StreamSetBuffer * CRLFStream = mGrepDriver->addBuffer<CircularBuffer>(idb, idb->getStreamSetTy(1, 1), segmentSize * bufferSegments);
[5755]116    kernel::Kernel * linebreakK = mGrepDriver->addKernelInstance<kernel::LineBreakKernelBuilder>(idb, encodingBits);
[5782]117    mGrepDriver->makeKernelCall(linebreakK, {BasisBits, LineFeedStream}, {LineBreakStream, CRLFStream});
[5770]118
[5755]119    kernel::Kernel * requiredStreamsK = mGrepDriver->addKernelInstance<kernel::RequiredStreams_UTF8>(idb);
[5782]120    StreamSetBuffer * RequiredStreams = mGrepDriver->addBuffer<CircularBuffer>(idb, idb->getStreamSetTy(3, 1), segmentSize * bufferSegments);
[5704]121    mGrepDriver->makeKernelCall(requiredStreamsK, {BasisBits}, {RequiredStreams});
[5770]122
[5704]123    const auto n = REs.size();
[5748]124    std::vector<std::vector<re::CC *>> charclasses(n);
[5704]125    std::vector<StreamSetBuffer *> MatchResultsBufs(n);
[5770]126
[5803]127    for(unsigned i = 0; i < n; ++i) {
128        REs[i] = resolveModesAndExternalSymbols(REs[i]);
129        REs[i] = excludeUnicodeLineBreak(REs[i]);
[5819]130#define USE_MULTIPLEX_CC
[5787]131#ifdef USE_MULTIPLEX_CC
[5817]132        REs[i] = regular_expression_passes(REs[i]);
[5795]133        const std::vector<const re::CC *> UnicodeSets = re::collectUnicodeSets(REs[i]);
[5816]134        if (UnicodeSets.size() <= 1) {
135            StreamSetBuffer * MatchResults = mGrepDriver->addBuffer<CircularBuffer>(idb, idb->getStreamSetTy(1, 1), segmentSize * bufferSegments);
136            kernel::Kernel * icgrepK = mGrepDriver->addKernelInstance<kernel::ICGrepKernel>(idb, REs[i]);
137            mGrepDriver->makeKernelCall(icgrepK, {BasisBits, LineBreakStream, CRLFStream, RequiredStreams}, {MatchResults});
138            MatchResultsBufs[i] = MatchResults;
139        }
140        else {
141            mpx = make_unique<MultiplexedAlphabet>("mpx", UnicodeSets);
142            REs[i] = transformCCs(mpx.get(), REs[i]);
143            std::vector<re::CC *> mpx_basis = mpx->getMultiplexedCCs();
144            auto numOfCharacterClasses = mpx_basis.size();
145            StreamSetBuffer * CharClasses = mGrepDriver->addBuffer<CircularBuffer>(idb, idb->getStreamSetTy(numOfCharacterClasses), segmentSize * bufferSegments);
146            kernel::Kernel * ccK = mGrepDriver->addKernelInstance<kernel::CharClassesKernel>(idb, std::move(mpx_basis));
147            mGrepDriver->makeKernelCall(ccK, {BasisBits}, {CharClasses});
148            StreamSetBuffer * MatchResults = mGrepDriver->addBuffer<CircularBuffer>(idb, idb->getStreamSetTy(1, 1), segmentSize * bufferSegments);
149            kernel::Kernel * icgrepK = mGrepDriver->addKernelInstance<kernel::ICGrepKernel>(idb, REs[i], std::vector<cc::Alphabet *>{mpx.get()});
150            mGrepDriver->makeKernelCall(icgrepK, {BasisBits, LineBreakStream, CRLFStream, RequiredStreams, CharClasses}, {MatchResults});
151            MatchResultsBufs[i] = MatchResults;
152        }
[5787]153#else
154        REs[i] = regular_expression_passes(REs[i]);
155        StreamSetBuffer * MatchResults = mGrepDriver->addBuffer<CircularBuffer>(idb, idb->getStreamSetTy(1, 1), segmentSize * bufferSegments);
156        kernel::Kernel * icgrepK = mGrepDriver->addKernelInstance<kernel::ICGrepKernel>(idb, REs[i]);
157        mGrepDriver->makeKernelCall(icgrepK, {BasisBits, LineBreakStream, CRLFStream, RequiredStreams}, {MatchResults});
[5816]158        MatchResultsBufs[i] = MatchResults;
[5787]159#endif
[5704]160    }
161    StreamSetBuffer * MergedResults = MatchResultsBufs[0];
162    if (REs.size() > 1) {
[5755]163        MergedResults = mGrepDriver->addBuffer<CircularBuffer>(idb, idb->getStreamSetTy(1, 1), segmentSize * bufferSegments);
164        kernel::Kernel * streamsMergeK = mGrepDriver->addKernelInstance<kernel::StreamsMerge>(idb, 1, REs.size());
[5704]165        mGrepDriver->makeKernelCall(streamsMergeK, MatchResultsBufs, {MergedResults});
166    }
167    StreamSetBuffer * Matches = MergedResults;
[5770]168
[5704]169    if (mMoveMatchesToEOL) {
170        StreamSetBuffer * OriginalMatches = Matches;
[5755]171        kernel::Kernel * matchedLinesK = mGrepDriver->addKernelInstance<kernel::MatchedLinesKernel>(idb);
172        Matches = mGrepDriver->addBuffer<CircularBuffer>(idb, idb->getStreamSetTy(1, 1), segmentSize * bufferSegments);
[5704]173        mGrepDriver->makeKernelCall(matchedLinesK, {OriginalMatches, LineBreakStream}, {Matches});
174    }
[5770]175
[5704]176    if (InvertMatchFlag) {
[5755]177        kernel::Kernel * invertK = mGrepDriver->addKernelInstance<kernel::InvertMatchesKernel>(idb);
[5704]178        StreamSetBuffer * OriginalMatches = Matches;
[5755]179        Matches = mGrepDriver->addBuffer<CircularBuffer>(idb, idb->getStreamSetTy(1, 1), segmentSize * bufferSegments);
[5704]180        mGrepDriver->makeKernelCall(invertK, {OriginalMatches, LineBreakStream}, {Matches});
181    }
182    if (MaxCountFlag > 0) {
[5755]183        kernel::Kernel * untilK = mGrepDriver->addKernelInstance<kernel::UntilNkernel>(idb);
[5704]184        untilK->setInitialArguments({idb->getSize(MaxCountFlag)});
185        StreamSetBuffer * AllMatches = Matches;
[5755]186        Matches = mGrepDriver->addBuffer<CircularBuffer>(idb, idb->getStreamSetTy(1, 1), segmentSize * bufferSegments);
[5704]187        mGrepDriver->makeKernelCall(untilK, {AllMatches}, {Matches});
188    }
189    return std::pair<StreamSetBuffer *, StreamSetBuffer *>(LineBreakStream, Matches);
[5700]190}
[5704]191
192// The QuietMode, MatchOnly and CountOnly engines share a common code generation main function,
193// which returns a count of the matches found (possibly subject to a MaxCount).
[5700]194//
195
[5704]196void GrepEngine::grepCodeGen(std::vector<re::RE *> REs) {
[5770]197
[5704]198    assert (mGrepDriver == nullptr);
199    mGrepDriver = new ParabixDriver("engine");
200    auto & idb = mGrepDriver->getBuilder();
201    Module * M = idb->getModule();
[5770]202
[5757]203    const auto segmentSize = codegen::SegmentSize;
204    const auto bufferSegments = codegen::BufferSegments * codegen::ThreadNum;
205
[5704]206    const unsigned encodingBits = 8;
[5770]207
[5704]208    Function * mainFunc = cast<Function>(M->getOrInsertFunction("Main", idb->getInt64Ty(), idb->getInt32Ty(), nullptr));
209    mainFunc->setCallingConv(CallingConv::C);
210    idb->SetInsertPoint(BasicBlock::Create(M->getContext(), "entry", mainFunc, 0));
211    auto args = mainFunc->arg_begin();
[5770]212
[5704]213    Value * const fileDescriptor = &*(args++);
214    fileDescriptor->setName("fileDescriptor");
[5770]215
[5755]216    StreamSetBuffer * ByteStream = mGrepDriver->addBuffer<SourceBuffer>(idb, idb->getStreamSetTy(1, encodingBits));
[5757]217    kernel::Kernel * sourceK = mGrepDriver->addKernelInstance<kernel::FDSourceKernel>(idb, segmentSize * bufferSegments);
[5704]218    sourceK->setInitialArguments({fileDescriptor});
219    mGrepDriver->makeKernelCall(sourceK, {}, {ByteStream});
[5770]220
[5704]221    StreamSetBuffer * LineBreakStream;
222    StreamSetBuffer * Matches;
223    std::tie(LineBreakStream, Matches) = grepPipeline(REs, ByteStream);
[5770]224
[5755]225    kernel::Kernel * matchCountK = mGrepDriver->addKernelInstance<kernel::PopcountKernel>(idb);
[5704]226    mGrepDriver->makeKernelCall(matchCountK, {Matches}, {});
227    mGrepDriver->generatePipelineIR();
228    idb->setKernel(matchCountK);
229    Value * matchedLineCount = idb->getAccumulator("countResult");
230    matchedLineCount = idb->CreateZExt(matchedLineCount, idb->getInt64Ty());
231    mGrepDriver->deallocateBuffers();
232    idb->CreateRet(matchedLineCount);
233    mGrepDriver->finalizeObject();
234}
235
236//
237// The EmitMatches engine uses an EmitMatchesAccumulator object to concatenate together
238// matched lines.
239
[5700]240class EmitMatch : public MatchAccumulator {
[5703]241    friend class EmitMatchesEngine;
[5700]242public:
[5771]243    EmitMatch(std::string linePrefix, std::ostringstream & strm) : mLinePrefix(linePrefix), mLineCount(0), mTerminated(true), mResultStr(strm) {}
[5700]244    void accumulate_match(const size_t lineNum, char * line_start, char * line_end) override;
245    void finalize_match(char * buffer_end) override;
[5703]246protected:
[5700]247    std::string mLinePrefix;
248    size_t mLineCount;
[5758]249    bool mTerminated;
[5771]250    std::ostringstream & mResultStr;
[5700]251};
252
[5704]253//
254//  Default Report Match:  lines are emitted with whatever line terminators are found in the
255//  input.  However, if the final line is not terminated, a new line is appended.
[5726]256//
[5700]257void EmitMatch::accumulate_match (const size_t lineNum, char * line_start, char * line_end) {
[5758]258    if (WithFilenameFlag) {
[5771]259        mResultStr << mLinePrefix;
[5700]260    }
[5758]261    if (LineNumberFlag) {
262        // Internally line numbers are counted from 0.  For display, adjust
263        // the line number so that lines are numbered from 1.
264        if (InitialTabFlag) {
[5771]265            mResultStr << lineNum+1 << "\t:";
[5695]266        }
[5758]267        else {
[5771]268            mResultStr << lineNum+1 << ":";
[5700]269        }
[5695]270    }
[5758]271    size_t bytes = line_end - line_start + 1;
[5771]272    mResultStr.write(line_start, bytes);
[5700]273    mLineCount++;
[5758]274    unsigned last_byte = *line_end;
275    mTerminated = (last_byte >= 0x0A) && (last_byte <= 0x0D);
276    if (LLVM_UNLIKELY(!mTerminated)) {
277        if (last_byte == 0x85) {  //  Possible NEL terminator.
278            mTerminated = (bytes >= 2) && (static_cast<unsigned>(line_end[-1]) == 0xC2);
[5726]279        }
280        else {
[5758]281            // Possible LS or PS terminators.
282            mTerminated = (bytes >= 3) && (static_cast<unsigned>(line_end[-2]) == 0xE2)
283                                       && (static_cast<unsigned>(line_end[-1]) == 0x80)
284                                       && ((last_byte == 0xA8) || (last_byte == 0xA9));
[5726]285        }
[5700]286    }
287}
288
[5758]289void EmitMatch::finalize_match(char * buffer_end) {
[5771]290    if (!mTerminated) mResultStr << "\n";
[5758]291}
292
[5704]293void EmitMatchesEngine::grepCodeGen(std::vector<re::RE *> REs) {
294    assert (mGrepDriver == nullptr);
295    mGrepDriver = new ParabixDriver("engine");
296    auto & idb = mGrepDriver->getBuilder();
297    Module * M = idb->getModule();
[5770]298
[5757]299    const auto segmentSize = codegen::SegmentSize;
300    const auto bufferSegments = codegen::BufferSegments * codegen::ThreadNum;
[5704]301    const unsigned encodingBits = 8;
[5770]302
[5704]303    Function * mainFunc = cast<Function>(M->getOrInsertFunction("Main", idb->getInt64Ty(), idb->getInt32Ty(), idb->getIntAddrTy(), nullptr));
304    mainFunc->setCallingConv(CallingConv::C);
305    idb->SetInsertPoint(BasicBlock::Create(M->getContext(), "entry", mainFunc, 0));
306    auto args = mainFunc->arg_begin();
[5770]307
[5704]308    Value * const fileDescriptor = &*(args++);
309    fileDescriptor->setName("fileDescriptor");
310    Value * match_accumulator = &*(args++);
311    match_accumulator->setName("match_accumulator");
[5770]312
[5755]313    StreamSetBuffer * ByteStream = mGrepDriver->addBuffer<SourceBuffer>(idb, idb->getStreamSetTy(1, encodingBits));
[5757]314    kernel::Kernel * sourceK = mGrepDriver->addKernelInstance<kernel::FDSourceKernel>(idb, segmentSize * bufferSegments);
[5704]315    sourceK->setInitialArguments({fileDescriptor});
316    mGrepDriver->makeKernelCall(sourceK, {}, {ByteStream});
[5770]317
[5704]318    StreamSetBuffer * LineBreakStream;
319    StreamSetBuffer * Matches;
320    std::tie(LineBreakStream, Matches) = grepPipeline(REs, ByteStream);
[5770]321
[5755]322    kernel::Kernel * scanMatchK = mGrepDriver->addKernelInstance<kernel::ScanMatchKernel>(idb);
[5704]323    scanMatchK->setInitialArguments({match_accumulator});
324    mGrepDriver->makeKernelCall(scanMatchK, {Matches, LineBreakStream, ByteStream}, {});
325    mGrepDriver->LinkFunction(*scanMatchK, "accumulate_match_wrapper", &accumulate_match_wrapper);
326    mGrepDriver->LinkFunction(*scanMatchK, "finalize_match_wrapper", &finalize_match_wrapper);
[5770]327
[5704]328    mGrepDriver->generatePipelineIR();
329    mGrepDriver->deallocateBuffers();
330    idb->CreateRet(idb->getInt64(0));
331    mGrepDriver->finalizeObject();
332}
[5700]333
334
[5704]335//
336//  The doGrep methods apply a GrepEngine to a single file, processing the results
337//  differently based on the engine type.
[5770]338
[5704]339uint64_t GrepEngine::doGrep(const std::string & fileName, const uint32_t fileIdx) {
340    typedef uint64_t (*GrepFunctionType)(int32_t fileDescriptor);
341    auto f = reinterpret_cast<GrepFunctionType>(mGrepDriver->getMain());
[5770]342
[5771]343    int32_t fileDescriptor = openFile(fileName, mResultStrs[fileIdx]);
[5704]344    if (fileDescriptor == -1) return 0;
[5770]345
[5704]346    uint64_t grepResult = f(fileDescriptor);
347    close(fileDescriptor);
348    return grepResult;
349}
350
351uint64_t CountOnlyEngine::doGrep(const std::string & fileName, const uint32_t fileIdx) {
352    uint64_t grepResult = GrepEngine::doGrep(fileName, fileIdx);
[5771]353    if (WithFilenameFlag) mResultStrs[fileIdx] << linePrefix(fileName);
354    mResultStrs[fileIdx] << grepResult << "\n";
[5704]355    return grepResult;
356}
357
358std::string GrepEngine::linePrefix(std::string fileName) {
359    if (fileName == "-") {
360        return LabelFlag + mFileSuffix;
[5548]361    }
[5704]362    else {
363        return fileName + mFileSuffix;
[5548]364    }
365}
[5770]366
[5704]367uint64_t MatchOnlyEngine::doGrep(const std::string & fileName, const uint32_t fileIdx) {
368    uint64_t grepResult = GrepEngine::doGrep(fileName, fileIdx);
369    if (grepResult == mRequiredCount) {
[5771]370       mResultStrs[fileIdx] << linePrefix(fileName);
[5704]371    }
372    return grepResult;
373}
[5700]374
[5704]375uint64_t EmitMatchesEngine::doGrep(const std::string & fileName, const uint32_t fileIdx) {
376    typedef uint64_t (*GrepFunctionType)(int32_t fileDescriptor, intptr_t accum_addr);
377    auto f = reinterpret_cast<GrepFunctionType>(mGrepDriver->getMain());
[5770]378
[5771]379    int32_t fileDescriptor = openFile(fileName, mResultStrs[fileIdx]);
[5704]380    if (fileDescriptor == -1) return 0;
[5771]381    EmitMatch accum(linePrefix(fileName), mResultStrs[fileIdx]);
[5740]382    f(fileDescriptor, reinterpret_cast<intptr_t>(&accum));
[5704]383    close(fileDescriptor);
384    if (accum.mLineCount > 0) grepMatchFound = true;
385    return accum.mLineCount;
386}
387
[5703]388// Open a file and return its file desciptor.
[5771]389int32_t GrepEngine::openFile(const std::string & fileName, std::ostringstream & msgstrm) {
[5693]390    if (fileName == "-") {
[5700]391        return STDIN_FILENO;
[5693]392    }
[5700]393    else {
394        struct stat sb;
395        int32_t fileDescriptor = open(fileName.c_str(), O_RDONLY);
396        if (LLVM_UNLIKELY(fileDescriptor == -1)) {
397            if (!NoMessagesFlag) {
398                if (errno == EACCES) {
[5771]399                    msgstrm << "icgrep: " << fileName << ": Permission denied.\n";
[5700]400                }
401                else if (errno == ENOENT) {
[5771]402                    msgstrm << "icgrep: " << fileName << ": No such file.\n";
[5700]403                }
404                else {
[5771]405                    msgstrm << "icgrep: " << fileName << ": Failed.\n";
[5700]406                }
[5484]407            }
[5700]408            return fileDescriptor;
409        }
410        if (stat(fileName.c_str(), &sb) == 0 && S_ISDIR(sb.st_mode)) {
411            if (!NoMessagesFlag) {
[5771]412                msgstrm << "icgrep: " << fileName << ": Is a directory.\n";
[5484]413            }
[5700]414            close(fileDescriptor);
[5704]415            return -1;
[5484]416        }
[5700]417        return fileDescriptor;
[4788]418    }
[5700]419}
420
[5704]421// The process of searching a group of files may use a sequential or a task
422// parallel approach.
[5770]423
[5735]424void * DoGrepThreadFunction(void *args) {
[5740]425    return reinterpret_cast<GrepEngine *>(args)->DoGrepThreadMethod();
[5735]426}
[4949]427
[5704]428bool GrepEngine::searchAllFiles() {
[5795]429    const unsigned numOfThreads = std::min(static_cast<unsigned>(Threads), static_cast<unsigned>(inputFiles.size())); 
430    std::vector<pthread_t> threads(numOfThreads);
[5770]431
[5735]432    for(unsigned long i = 1; i < numOfThreads; ++i) {
433        const int rc = pthread_create(&threads[i], nullptr, DoGrepThreadFunction, (void *)this);
434        if (rc) {
435            llvm::report_fatal_error("Failed to create thread: code " + std::to_string(rc));
[5484]436        }
[5735]437    }
438    // Main thread also does the work;
[5770]439
[5735]440    DoGrepThreadMethod();
441    for(unsigned i = 1; i < numOfThreads; ++i) {
442        void * status = nullptr;
443        const int rc = pthread_join(threads[i], &status);
444        if (rc) {
445            llvm::report_fatal_error("Failed to join thread: code " + std::to_string(rc));
[5704]446        }
[5484]447    }
[5704]448    return grepMatchFound;
[5377]449}
[5314]450
[5338]451
[5704]452// DoGrep thread function.
[5735]453void * GrepEngine::DoGrepThreadMethod() {
[5748]454
[5771]455    unsigned fileIdx = mNextFileToGrep++;
[5735]456    while (fileIdx < inputFiles.size()) {
[5771]457        const auto grepResult = doGrep(inputFiles[fileIdx], fileIdx);
[5735]458        mFileStatus[fileIdx] = FileStatus::GrepComplete;
[5761]459        if (grepResult > 0) {
460            grepMatchFound = true;
[5735]461        }
462        if (QuietMode && grepMatchFound) {
[5761]463            if (pthread_self() != mEngineThread) {
464                pthread_exit(nullptr);
465            }
[5735]466            return nullptr;
467        }
[5761]468        fileIdx = mNextFileToGrep++;
[5574]469    }
[5740]470
[5771]471    unsigned printIdx = mNextFileToPrint++;
[5761]472    while (printIdx < inputFiles.size()) {
473        const bool readyToPrint = ((printIdx == 0) || (mFileStatus[printIdx - 1] == FileStatus::PrintComplete)) && (mFileStatus[printIdx] == FileStatus::GrepComplete);
[5735]474        if (readyToPrint) {
[5771]475            const auto output = mResultStrs[printIdx].str();
[5761]476            if (!output.empty()) {
[5771]477                llvm::outs() << output;
[5761]478            }
479            mFileStatus[printIdx] = FileStatus::PrintComplete;
480            printIdx = mNextFileToPrint++;
481        } else {
[5735]482            mGrepDriver->performIncrementalCacheCleanupStep();
483        }
[5762]484        sched_yield();
[5735]485    }
[5761]486
[5735]487    if (pthread_self() != mEngineThread) {
488        pthread_exit(nullptr);
[5761]489    } else {
[5795]490        // Always perform one final cache cleanup step.
491        mGrepDriver->performIncrementalCacheCleanupStep();
[5735]492    }
[5812]493    return nullptr;
[5703]494}
[5740]495
[5481]496}
Note: See TracBrowser for help on using the repository browser.