1 // top.h : DB usage monitor. 2 3 4 /** 5 * Copyright (C) 2018-present MongoDB, Inc. 6 * 7 * This program is free software: you can redistribute it and/or modify 8 * it under the terms of the Server Side Public License, version 1, 9 * as published by MongoDB, Inc. 10 * 11 * This program is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * Server Side Public License for more details. 15 * 16 * You should have received a copy of the Server Side Public License 17 * along with this program. If not, see 18 * <http://www.mongodb.com/licensing/server-side-public-license>. 19 * 20 * As a special exception, the copyright holders give permission to link the 21 * code of portions of this program with the OpenSSL library under certain 22 * conditions as described in each individual source file and distribute 23 * linked combinations including the program with the OpenSSL library. You 24 * must comply with the Server Side Public License in all respects for 25 * all of the code used other than as permitted herein. If you modify file(s) 26 * with this exception, you may extend this exception to your version of the 27 * file(s), but you are not obligated to do so. If you do not wish to do so, 28 * delete this exception statement from your version. If you delete this 29 * exception statement from all source files in the program, then also delete 30 * it in the license file. 31 */ 32 33 #pragma once 34 35 #include <boost/date_time/posix_time/posix_time.hpp> 36 37 #include "mongo/db/commands.h" 38 #include "mongo/db/operation_context.h" 39 #include "mongo/db/stats/operation_latency_histogram.h" 40 #include "mongo/util/concurrency/mutex.h" 41 #include "mongo/util/net/message.h" 42 #include "mongo/util/string_map.h" 43 44 namespace mongo { 45 46 class ServiceContext; 47 48 /** 49 * tracks usage by collection 50 */ 51 class Top { 52 public: 53 static Top& get(ServiceContext* service); 54 55 Top() = default; 56 57 struct UsageData { UsageDataUsageData58 UsageData() : time(0), count(0) {} 59 UsageData(const UsageData& older, const UsageData& newer); 60 long long time; 61 long long count; 62 incUsageData63 void inc(long long micros) { 64 count++; 65 time += micros; 66 } 67 }; 68 69 struct CollectionData { 70 /** 71 * constructs a diff 72 */ CollectionDataCollectionData73 CollectionData() {} 74 CollectionData(const CollectionData& older, const CollectionData& newer); 75 76 UsageData total; 77 78 UsageData readLock; 79 UsageData writeLock; 80 81 UsageData queries; 82 UsageData getmore; 83 UsageData insert; 84 UsageData update; 85 UsageData remove; 86 UsageData commands; 87 OperationLatencyHistogram opLatencyHistogram; 88 }; 89 90 enum class LockType { 91 ReadLocked, 92 WriteLocked, 93 NotLocked, 94 }; 95 96 typedef StringMap<CollectionData> UsageMap; 97 98 public: 99 void record(OperationContext* opCtx, 100 StringData ns, 101 LogicalOp logicalOp, 102 LockType lockType, 103 long long micros, 104 bool command, 105 Command::ReadWriteType readWriteType); 106 107 void append(BSONObjBuilder& b); 108 109 void cloneMap(UsageMap& out) const; 110 111 void collectionDropped(StringData ns, bool databaseDropped = false); 112 113 /** 114 * Appends the collection-level latency statistics 115 */ 116 void appendLatencyStats(StringData ns, bool includeHistograms, BSONObjBuilder* builder); 117 118 /** 119 * Increments the global histogram. 120 */ 121 void incrementGlobalLatencyStats(OperationContext* opCtx, 122 uint64_t latency, 123 Command::ReadWriteType readWriteType); 124 125 /** 126 * Appends the global latency statistics. 127 */ 128 void appendGlobalLatencyStats(bool includeHistograms, BSONObjBuilder* builder); 129 130 private: 131 void _appendToUsageMap(BSONObjBuilder& b, const UsageMap& map) const; 132 133 void _appendStatsEntry(BSONObjBuilder& b, const char* statsName, const UsageData& map) const; 134 135 void _record(OperationContext* opCtx, 136 CollectionData& c, 137 LogicalOp logicalOp, 138 LockType lockType, 139 long long micros, 140 Command::ReadWriteType readWriteType); 141 142 void _incrementHistogram(OperationContext* opCtx, 143 long long latency, 144 OperationLatencyHistogram* histogram, 145 Command::ReadWriteType readWriteType); 146 147 mutable SimpleMutex _lock; 148 OperationLatencyHistogram _globalHistogramStats; 149 UsageMap _usage; 150 std::string _lastDropped; 151 }; 152 153 } // namespace mongo 154