X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=plocate.cpp;h=0d59076e8fc99efb4829384da1894e99c4399867;hb=d5ba26d705460a7e37213eeb4954b2efed8bebf0;hp=e31345852341f2a7218a435b873ec88303888a72;hpb=75c42d06f8eb513afc1976e82033ffb893b101b8;p=plocate diff --git a/plocate.cpp b/plocate.cpp index e313458..0d59076 100644 --- a/plocate.cpp +++ b/plocate.cpp @@ -1,27 +1,28 @@ +#include "access_rx_cache.h" #include "db.h" #include "dprintf.h" #include "io_uring_engine.h" #include "parse_trigrams.h" +#include "serializer.h" #include "turbopfor.h" #include "unique_sort.h" #include -#include #include +#include #include #include +#include #include #include #include #include #include -#include #include #include -#include +#include #include #include -#include #include #include #include @@ -30,6 +31,7 @@ #include #include #include +#include #include #include #include @@ -57,83 +59,6 @@ ZSTD_DDict *ddict = nullptr; regex_t compile_regex(const string &needle); -void apply_limit() -{ - if (--limit_left > 0) { - return; - } - dprintf("Done in %.1f ms, found %" PRId64 " matches.\n", - 1e3 * duration(steady_clock::now() - start).count(), limit_matches); - if (only_count) { - printf("%" PRId64 "\n", limit_matches); - } - exit(0); -} - -class ResultReceiver { -public: - virtual ~ResultReceiver() = default; - virtual void print(uint64_t seq, uint64_t skip, const string msg) = 0; -}; - -class Serializer : public ResultReceiver { -public: - ~Serializer() { assert(limit_left <= 0 || pending.empty()); } - void print(uint64_t seq, uint64_t skip, const string msg) override; - -private: - uint64_t next_seq = 0; - struct Element { - uint64_t seq, skip; - string msg; - - bool operator<(const Element &other) const - { - return seq > other.seq; - } - }; - priority_queue pending; -}; - -void Serializer::print(uint64_t seq, uint64_t skip, const string msg) -{ - if (only_count) { - if (!msg.empty()) { - apply_limit(); - } - return; - } - - if (next_seq != seq) { - pending.push(Element{ seq, skip, move(msg) }); - return; - } - - if (!msg.empty()) { - if (print_nul) { - printf("%s%c", msg.c_str(), 0); - } else { - printf("%s\n", msg.c_str()); - } - apply_limit(); - } - next_seq += skip; - - // See if any delayed prints can now be dealt with. - while (!pending.empty() && pending.top().seq == next_seq) { - if (!pending.top().msg.empty()) { - if (print_nul) { - printf("%s%c", pending.top().msg.c_str(), 0); - } else { - printf("%s\n", pending.top().msg.c_str()); - } - apply_limit(); - } - next_seq += pending.top().skip; - pending.pop(); - } -} - struct Needle { enum { STRSTR, REGEX, @@ -155,87 +80,6 @@ bool matches(const Needle &needle, const char *haystack) } } -class AccessRXCache { -public: - AccessRXCache(IOUringEngine *engine) - : engine(engine) {} - void check_access(const char *filename, bool allow_async, function cb); - -private: - unordered_map cache; - struct PendingStat { - string filename; - function cb; - }; - map> pending_stats; - IOUringEngine *engine; - mutex mu; -}; - -void AccessRXCache::check_access(const char *filename, bool allow_async, function cb) -{ - lock_guard lock(mu); - if (engine == nullptr || !engine->get_supports_stat()) { - allow_async = false; - } - - for (const char *end = strchr(filename + 1, '/'); end != nullptr; end = strchr(end + 1, '/')) { - string parent_path(filename, end - filename); // string_view from C++20. - auto cache_it = cache.find(parent_path); - if (cache_it != cache.end()) { - // Found in the cache. - if (!cache_it->second) { - cb(false); - return; - } - continue; - } - - if (!allow_async) { - bool ok = access(parent_path.c_str(), R_OK | X_OK) == 0; - cache.emplace(parent_path, ok); - if (!ok) { - cb(false); - return; - } - continue; - } - - // We want to call access(), but it could block on I/O. io_uring doesn't support - // access(), but we can do a dummy asynchonous statx() to populate the kernel's cache, - // which nearly always makes the next access() instantaneous. - - // See if there's already a pending stat that matches this, - // or is a subdirectory. - auto it = pending_stats.lower_bound(parent_path); - if (it != pending_stats.end() && it->first.size() >= parent_path.size() && - it->first.compare(0, parent_path.size(), parent_path) == 0) { - it->second.emplace_back(PendingStat{ filename, move(cb) }); - } else { - it = pending_stats.emplace(filename, vector{}).first; - engine->submit_stat(filename, [this, it, filename{ strdup(filename) }, cb{ move(cb) }] { - // The stat returned, so now do the actual access() calls. - // All of them should be in cache, so don't fire off new statx() - // calls during that check. - check_access(filename, /*allow_async=*/false, move(cb)); - free(filename); - - // Call all others that waited for the same stat() to finish. - // They may fire off new stat() calls if needed. - vector pending = move(it->second); - pending_stats.erase(it); - for (PendingStat &ps : pending) { - check_access(ps.filename.c_str(), /*allow_async=*/true, move(ps.cb)); - } - }); - } - return; // The rest will happen in async context. - } - - // Passed all checks. - cb(true); -} - class Corpus { public: Corpus(int fd, IOUringEngine *engine); @@ -399,7 +243,7 @@ size_t scan_docids(const vector &needles, const vector &docids { Serializer docids_in_order; AccessRXCache access_rx_cache(engine); - atomic matched{0}; + atomic matched{ 0 }; for (size_t i = 0; i < docids.size(); ++i) { uint32_t docid = docids[i]; corpus.get_compressed_filename_block(docid, [i, &matched, &needles, &access_rx_cache, &docids_in_order](string_view compressed) { @@ -417,17 +261,27 @@ struct WorkerThread { // since a lock on it becomes a huge choke point if there are // lots of threads. mutex result_mu; - vector> results; + struct Result { + uint64_t seq; + uint64_t skip; + string msg; + }; + vector results; }; class WorkerThreadReceiver : public ResultReceiver { public: - WorkerThreadReceiver(WorkerThread *wt) : wt(wt) {} + WorkerThreadReceiver(WorkerThread *wt) + : wt(wt) {} void print(uint64_t seq, uint64_t skip, const string msg) override { lock_guard lock(wt->result_mu); - wt->results.emplace_back(seq, skip, move(msg)); + if (msg.empty() && !wt->results.empty() && wt->results.back().seq + wt->results.back().skip == seq) { + wt->results.back().skip += skip; + } else { + wt->results.emplace_back(WorkerThread::Result{ seq, skip, move(msg) }); + } } private: @@ -436,13 +290,13 @@ private: void deliver_results(WorkerThread *wt, Serializer *serializer) { - vector> results; + vector results; { lock_guard lock(wt->result_mu); results = move(wt->results); } - for (const auto &result : results) { - serializer->print(get<0>(result), get<1>(result), move(get<2>(result))); + for (const WorkerThread::Result &result : results) { + serializer->print(result.seq, result.skip, move(result.msg)); } } @@ -469,7 +323,7 @@ uint64_t scan_all_docids(const vector &needles, int fd, const Corpus &co uint32_t num_blocks = corpus.get_num_filename_blocks(); unique_ptr offsets(new uint64_t[num_blocks + 1]); complete_pread(fd, offsets.get(), (num_blocks + 1) * sizeof(uint64_t), corpus.offset_for_block(0)); - atomic matched{0}; + atomic matched{ 0 }; mutex mu; condition_variable queue_added, queue_removed;