/** * Copyright (C) 2018-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 * . * * 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 #include "mongo/db/commands/server_status_metric.h" #include "mongo/db/jsobj.h" #include "mongo/platform/atomic_word.h" #include "mongo/platform/basic.h" #include "mongo/rpc/message.h" #include "mongo/util/concurrency/spin_lock.h" #include "mongo/util/processinfo.h" #include "mongo/util/string_map.h" #include "mongo/util/with_alignment.h" namespace mongo { /** * for storing operation counters * note: not thread safe. ok with that for speed */ class OpCounters { public: OpCounters() = default; void gotInserts(int n) { _checkWrap(&OpCounters::_insert, n); } void gotInsert() { _checkWrap(&OpCounters::_insert, 1); } void gotQuery() { _checkWrap(&OpCounters::_query, 1); } void gotUpdate() { _checkWrap(&OpCounters::_update, 1); } void gotDelete() { _checkWrap(&OpCounters::_delete, 1); } void gotGetMore() { _checkWrap(&OpCounters::_getmore, 1); } void gotCommand() { _checkWrap(&OpCounters::_command, 1); } void gotInsertsDeprecated(int n) { _checkWrap(&OpCounters::_insertDeprecated, n); } void gotQueryDeprecated() { _checkWrap(&OpCounters::_queryDeprecated, 1); } void gotUpdateDeprecated() { _checkWrap(&OpCounters::_updateDeprecated, 1); } void gotDeleteDeprecated() { _checkWrap(&OpCounters::_deleteDeprecated, 1); } void gotGetMoreDeprecated() { _checkWrap(&OpCounters::_getmoreDeprecated, 1); } void gotKillCursorsDeprecated() { _checkWrap(&OpCounters::_killcursorsDeprecated, 1); } BSONObj getObj() const; // These opcounters record operations that would fail if we were fully enforcing our consistency // constraints in steady-state oplog application mode. void gotInsertOnExistingDoc() { _checkWrap(&OpCounters::_insertOnExistingDoc, 1); } void gotUpdateOnMissingDoc() { _checkWrap(&OpCounters::_updateOnMissingDoc, 1); } void gotDeleteWasEmpty() { _checkWrap(&OpCounters::_deleteWasEmpty, 1); } void gotDeleteFromMissingNamespace() { _checkWrap(&OpCounters::_deleteFromMissingNamespace, 1); } void gotAcceptableErrorInCommand() { _checkWrap(&OpCounters::_acceptableErrorInCommand, 1); } // thse are used by snmp, and other things, do not remove const AtomicWord* getInsert() const { return &_insert; } const AtomicWord* getQuery() const { return &_query; } const AtomicWord* getUpdate() const { return &_update; } const AtomicWord* getDelete() const { return &_delete; } const AtomicWord* getGetMore() const { return &_getmore; } const AtomicWord* getCommand() const { return &_command; } const AtomicWord* getInsertOnExistingDoc() const { return &_insertOnExistingDoc; } const AtomicWord* getUpdateOnMissingDoc() const { return &_updateOnMissingDoc; } const AtomicWord* getDeleteWasEmpty() const { return &_deleteWasEmpty; } const AtomicWord* getDeleteFromMissingNamespace() const { return &_deleteFromMissingNamespace; } const AtomicWord* getAcceptableErrorInCommand() const { return &_acceptableErrorInCommand; } private: // Increment member `counter` by `n`, resetting all counters if it was > 2^60. void _checkWrap(CacheAligned> OpCounters::*counter, int n); CacheAligned> _insert; CacheAligned> _query; CacheAligned> _update; CacheAligned> _delete; CacheAligned> _getmore; CacheAligned> _command; CacheAligned> _insertOnExistingDoc; CacheAligned> _updateOnMissingDoc; CacheAligned> _deleteWasEmpty; CacheAligned> _deleteFromMissingNamespace; CacheAligned> _acceptableErrorInCommand; // Counters for deprecated opcodes. CacheAligned> _insertDeprecated; CacheAligned> _queryDeprecated; CacheAligned> _updateDeprecated; CacheAligned> _deleteDeprecated; CacheAligned> _getmoreDeprecated; CacheAligned> _killcursorsDeprecated; }; extern OpCounters globalOpCounters; extern OpCounters replOpCounters; class NetworkCounter { public: // Increment the counters for the number of bytes read directly off the wire void hitPhysicalIn(long long bytes); void hitPhysicalOut(long long bytes); // Increment the counters for the number of bytes passed out of the TransportLayer to the // server void hitLogicalIn(long long bytes); void hitLogicalOut(long long bytes); // Increment the counter for the number of slow dns resolution operations. void incrementNumSlowDNSOperations(); // Increment the counter for the number of slow ssl handshake operations. void incrementNumSlowSSLOperations(); // TFO Counters and Status; void acceptedTFOIngress(); void setTFOKernelSetting(std::int64_t val) { _tfo.kernelSetting = val; } void setTFOServerSupport(bool val) { _tfo.kernelSupportServer = val; } void setTFOClientSupport(bool val) { _tfo.kernelSupportClient = val; } void append(BSONObjBuilder& b); private: CacheAligned> _physicalBytesIn{0}; CacheAligned> _physicalBytesOut{0}; // These two counters are always incremented at the same time, so // we place them on the same cache line. struct Together { AtomicWord logicalBytesIn{0}; AtomicWord requests{0}; }; CacheAligned _together{}; static_assert(sizeof(decltype(_together)) <= stdx::hardware_constructive_interference_size, "cache line spill"); CacheAligned> _logicalBytesOut{0}; CacheAligned> _numSlowDNSOperations{0}; CacheAligned> _numSlowSSLOperations{0}; struct TFO { // Counter of inbound connections at runtime. AtomicWord accepted{0}; // Info determined at startup. std::int64_t kernelSetting; bool kernelSupportServer{false}; bool kernelSupportClient{false}; }; CacheAligned _tfo{}; }; extern NetworkCounter networkCounter; class AuthCounter { struct MechanismData; public: class MechanismCounterHandle { public: MechanismCounterHandle(MechanismData* data) : _data(data) {} void incSpeculativeAuthenticateReceived(); void incSpeculativeAuthenticateSuccessful(); void incAuthenticateReceived(); void incAuthenticateSuccessful(); void incClusterAuthenticateReceived(); void incClusterAuthenticateSuccessful(); private: MechanismData* _data; }; MechanismCounterHandle getMechanismCounter(StringData mechanism); void incSaslSupportedMechanismsReceived(); void append(BSONObjBuilder*); void initializeMechanismMap(const std::vector&); private: struct MechanismData { struct { AtomicWord received; AtomicWord successful; } speculativeAuthenticate; struct { AtomicWord received; AtomicWord successful; } authenticate; struct { AtomicWord received; AtomicWord successful; } clusterAuthenticate; }; using MechanismMap = std::map; AtomicWord _saslSupportedMechanismsReceived; // Mechanism maps are initialized at startup to contain all // mechanisms known to authenticationMechanisms setParam. // After that they are kept to a fixed size. MechanismMap _mechanisms; }; extern AuthCounter authCounter; class AggStageCounters { public: // Container for a stage count metric along with its corresponding counter. struct StageCounter { StageCounter(StringData name) : metric("aggStageCounters." + name, &counter) {} Counter64 counter; ServerStatusMetricField metric; }; // Map of aggregation stages to the number of occurrences. StringMap> stageCounterMap = {}; }; extern AggStageCounters aggStageCounters; class DotsAndDollarsFieldsCounters { public: DotsAndDollarsFieldsCounters() : insertMetric("dotsAndDollarsFields.inserts", &inserts), updateMetric("dotsAndDollarsFields.updates", &updates) {} void incrementForUpsert(bool didInsert) { if (didInsert) { inserts.increment(); } else { updates.increment(); } } Counter64 inserts; Counter64 updates; ServerStatusMetricField insertMetric; ServerStatusMetricField updateMetric; }; extern DotsAndDollarsFieldsCounters dotsAndDollarsFieldsCounters; class OperatorCountersAggExpressions { private: struct AggExprCounter { AggExprCounter(StringData name) : metric("operatorCounters.expressions." + name, &counter) {} Counter64 counter; ServerStatusMetricField metric; }; public: void addAggExpressionCounter(StringData name) { operatorCountersAggExpressionMap[name] = std::make_unique(name); } void mergeCounters(StringMap& toMerge) { for (auto&& [name, cnt] : toMerge) { if (auto it = operatorCountersAggExpressionMap.find(name); it != operatorCountersAggExpressionMap.end()) { it->second->counter.increment(cnt); } } } private: // Map of aggregation expressions to the number of occurrences in aggregation pipelines. StringMap> operatorCountersAggExpressionMap = {}; }; extern OperatorCountersAggExpressions operatorCountersAggExpressions; /** * Global counters for match expressions. */ class OperatorCountersMatchExpressions { private: struct MatchExprCounter { MatchExprCounter(StringData name) : metric("operatorCounters.match." + name, &counter) {} Counter64 counter; ServerStatusMetricField metric; }; public: void addMatchExprCounter(StringData name) { operatorCountersMatchExprMap[name] = std::make_unique(name); } void mergeCounters(StringMap& toMerge) { for (auto&& [name, cnt] : toMerge) { if (auto it = operatorCountersMatchExprMap.find(name); it != operatorCountersMatchExprMap.end()) { it->second->counter.increment(cnt); } } } private: // Map of match expressions to the number of occurrences in queries. StringMap> operatorCountersMatchExprMap = {}; }; extern OperatorCountersMatchExpressions operatorCountersMatchExpressions; } // namespace mongo