summaryrefslogtreecommitdiff
path: root/src/mongo/db/query/plan_cache_debug_info.h
blob: 4501e8a10e1f062563142bd9cfd80eb53e381595 (plain)
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
137
138
139
140
141
142
143
144
145
146
147
148
149
/**
 *    Copyright (C) 2021-present MongoDB, Inc.
 *
 *    This program is free software: you can redistribute it and/or modify
 *    it under the terms of the Server Side Public License, version 1,
 *    as published by MongoDB, Inc.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    Server Side Public License for more details.
 *
 *    You should have received a copy of the Server Side Public License
 *    along with this program. If not, see
 *    <http://www.mongodb.com/licensing/server-side-public-license>.
 *
 *    As a special exception, the copyright holders give permission to link the
 *    code of portions of this program with the OpenSSL library under certain
 *    conditions as described in each individual source file and distribute
 *    linked combinations including the program with the OpenSSL library. You
 *    must comply with the Server Side Public License in all respects for
 *    all of the code used other than as permitted herein. If you modify file(s)
 *    with this exception, you may extend this exception to your version of the
 *    file(s), but you are not obligated to do so. If you do not wish to do so,
 *    delete this exception statement from your version. If you delete this
 *    exception statement from all source files in the program, then also delete
 *    it in the license file.
 */

#pragma once

#include "mongo/db/query/canonical_query.h"
#include "mongo/db/query/plan_ranking_decision.h"
#include "mongo/util/container_size_helper.h"

namespace mongo::plan_cache_debug_info {
/**
 * A description of the query from which a paln cache entry was created.
 */
struct CreatedFromQuery {
    /**
     * Returns an estimate of the size of this object, including the memory allocated elsewhere
     * that it owns, in bytes.
     */
    uint64_t estimateObjectSizeInBytes() const {
        uint64_t size = 0;
        size += filter.objsize();
        size += sort.objsize();
        size += projection.objsize();
        size += collation.objsize();
        return size;
    }

    std::string debugString() const {
        return str::stream() << "query: " << filter.toString() << "; sort: " << sort.toString()
                             << "; projection: " << projection.toString()
                             << "; collation: " << collation.toString();
    }

    BSONObj filter;
    BSONObj sort;
    BSONObj projection;
    BSONObj collation;
};

/**
 * Per-plan cache entry information that is used strictly as debug information (e.g. is intended
 * for display by the $planCacheStats aggregation source). In order to save memory, this
 * information is sometimes discarded instead of kept in the plan cache entry. Therefore, this
 * information may not be used for any purpose outside displaying debug info, such as recovering
 * a plan from the cache or determining whether or not the cache entry is active.
 */
struct DebugInfo {
    DebugInfo(CreatedFromQuery createdFromQuery,
              std::unique_ptr<const plan_ranker::PlanRankingDecision> decision)
        : createdFromQuery(std::move(createdFromQuery)), decision(std::move(decision)) {
        invariant(this->decision);
    }

    /**
     * 'DebugInfo' is copy-constructible, copy-assignable, move-constructible, and
     * move-assignable.
     */
    DebugInfo(const DebugInfo& other)
        : createdFromQuery(other.createdFromQuery), decision(other.decision->clone()) {}

    DebugInfo& operator=(const DebugInfo& other) {
        createdFromQuery = other.createdFromQuery;
        decision = other.decision->clone();
        return *this;
    }

    DebugInfo(DebugInfo&&) = default;
    DebugInfo& operator=(DebugInfo&&) = default;

    ~DebugInfo() = default;

    /**
     * Returns an estimate of the size of this object, including the memory allocated elsewhere
     * that it owns, in bytes.
     */
    uint64_t estimateObjectSizeInBytes() const {
        uint64_t size = 0;
        size += createdFromQuery.estimateObjectSizeInBytes();
        size += decision->estimateObjectSizeInBytes();
        return size;
    }

    std::string debugString() const {
        return createdFromQuery.debugString();
    }

    CreatedFromQuery createdFromQuery;

    // Information that went into picking the winning plan and also why the other plans lost.
    // Never nullptr.
    std::unique_ptr<const plan_ranker::PlanRankingDecision> decision;
};

struct CollectionDebugInfoSBE {
    long long collectionScans = 0;
    long long collectionScansNonTailable = 0;
    std::vector<std::string> indexesUsed;
};

/*
 * Similar to "DebugInfo" above. This debug info struct is only for SBE plan cache.
 */
struct DebugInfoSBE {
    uint64_t estimateObjectSizeInBytes() const {
        uint64_t size = sizeof(DebugInfoSBE) + planSummary.capacity();
        size += container_size_helper::estimateObjectSizeInBytes(
            mainStats.indexesUsed, [](std::string str) { return str.capacity(); }, true);
        for (auto& [_, stats] : secondaryStats) {
            size += container_size_helper::estimateObjectSizeInBytes(
                stats.indexesUsed, [](std::string str) { return str.capacity(); }, true);
        }
        return size;
    }

    std::string debugString() const {
        return planSummary;
    }

    CollectionDebugInfoSBE mainStats;
    StringMap<CollectionDebugInfoSBE> secondaryStats;
    std::string planSummary;
};
}  // namespace mongo::plan_cache_debug_info