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

Last change on this file since 5881 was 5881, checked in by cameron, 17 months ago

Grapheme Cluster Break kernel

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