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

Last change on this file since 5866 was 5861, checked in by cameron, 19 months ago

Using DirectCC builder updates; speedup wc -l

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