aboutsummaryrefslogtreecommitdiffstats
path: root/searchlib/src/vespa/searchlib/queryeval/profiled_iterator.cpp
blob: e30fcd6f9f58ba342f64b86fc7ac7709192182ac (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "profiled_iterator.h"
#include "sourceblendersearch.h"
#include <vespa/searchlib/common/bitvector.h>
#include <vespa/vespalib/objects/visit.hpp>
#include <vespa/vespalib/util/classname.h>
#include <vespa/vespalib/util/stringfmt.h>

#include <typeindex>

using vespalib::make_string_short::fmt;

namespace search::queryeval {

namespace {

using Profiler = vespalib::ExecutionProfiler;

struct TaskGuard {
    Profiler &profiler;
    TaskGuard(Profiler &profiler_in, Profiler::TaskId task) noexcept
      : profiler(profiler_in) { profiler.start(task); }
    ~TaskGuard() { profiler.complete(); }
};

vespalib::string name_of(const auto &obj) {
    auto name = vespalib::getClassName(obj);
    auto end = name.find("<");
    auto ns = name.rfind("::", end);
    size_t begin = (ns > name.size()) ? 0 : ns + 2;
    return name.substr(begin, end - begin);
}

std::unique_ptr<SearchIterator> create(Profiler &profiler,
                                       const vespalib::string &path,
                                       std::unique_ptr<SearchIterator> search,
                                       auto ctor_token)
{
    vespalib::string prefix = fmt("%s%s/", path.c_str(), name_of(*search).c_str());
    return std::make_unique<ProfiledIterator>(profiler, std::move(search),
                                              profiler.resolve(prefix + "init"),
                                              profiler.resolve(prefix + "seek"),
                                              profiler.resolve(prefix + "unpack"),
                                              profiler.resolve(prefix + "termwise"),
                                              ctor_token);    
}

void handle_leaf_node(Profiler &profiler, SearchIterator &leaf, const vespalib::string &path) {
    if (leaf.isSourceBlender()) {
        auto &source_blender = static_cast<SourceBlenderSearch&>(leaf);
        for (size_t i = 0; i < source_blender.getNumChildren(); ++i) {
            auto child = source_blender.steal(i);
            child = ProfiledIterator::profile(profiler, std::move(child), fmt("%s%zu/", path.c_str(), i));
            source_blender.setChild(i, std::move(child));
        }
    }
}

}

void
ProfiledIterator::initRange(uint32_t begin_id, uint32_t end_id)
{
    TaskGuard guard(_profiler, _init_tag);
    SearchIterator::initRange(begin_id, end_id);
    _search->initRange(begin_id, end_id);
    setDocId(_search->getDocId());
}

void
ProfiledIterator::doSeek(uint32_t docid)
{
    TaskGuard guard(_profiler, _seek_tag);
    _search->doSeek(docid);
    setDocId(_search->getDocId());
}

void
ProfiledIterator::doUnpack(uint32_t docid)
{
    TaskGuard guard(_profiler, _unpack_tag);
    _search->doUnpack(docid);
}

std::unique_ptr<BitVector>
ProfiledIterator::get_hits(uint32_t begin_id)
{
    TaskGuard guard(_profiler, _termwise_tag);
    return _search->get_hits(begin_id);
}

void
ProfiledIterator::or_hits_into(BitVector &result, uint32_t begin_id)
{
    TaskGuard guard(_profiler, _termwise_tag);
    _search->or_hits_into(result, begin_id);
}

void
ProfiledIterator::and_hits_into(BitVector &result, uint32_t begin_id)
{
    TaskGuard guard(_profiler, _termwise_tag);
    _search->and_hits_into(result, begin_id);
}

void
ProfiledIterator::visitMembers(vespalib::ObjectVisitor &visitor) const
{
    visit(visitor, "search", _search);
}

std::unique_ptr<SearchIterator>
ProfiledIterator::profile(Profiler &profiler, std::unique_ptr<SearchIterator> root, const vespalib::string &root_path)
{
    std::vector<UP*> links({&root});
    std::vector<vespalib::string> paths({root_path});
    for (size_t offset = 0; offset < links.size(); ++offset) {
        UP &link = *(links[offset]);
        vespalib::string path = paths[offset];
        size_t first_child = links.size();
        link->disclose_children(links);
        size_t num_children = links.size() - first_child;
        if (num_children == 0) {
            handle_leaf_node(profiler, *link, path);
        } else {
            for (size_t i = 0; i < num_children; ++i) {
                paths.push_back(fmt("%s%zu/", path.c_str(), i));
            }
        }
        link = create(profiler, path, std::move(link), ctor_tag{});
    }
    return root;
}

}