| counters.h | | counters.h | |
|
| // counters.h | | // Copyright 2012 the V8 project authors. All rights reserved. | |
| /* | | // Redistribution and use in source and binary forms, with or without | |
| * Copyright (C) 2010 10gen Inc. | | // modification, are permitted provided that the following conditions are | |
| * | | // met: | |
| * This program is free software: you can redistribute it and/or modify | | // | |
| * it under the terms of the GNU Affero General Public License, version | | // * Redistributions of source code must retain the above copyright | |
| 3, | | // notice, this list of conditions and the following disclaimer. | |
| * as published by the Free Software Foundation. | | // * Redistributions in binary form must reproduce the above | |
| * | | // copyright notice, this list of conditions and the following | |
| * This program is distributed in the hope that it will be useful, | | // disclaimer in the documentation and/or other materials provided | |
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | | // with the distribution. | |
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | | // * Neither the name of Google Inc. nor the names of its | |
| * GNU Affero General Public License for more details. | | // contributors may be used to endorse or promote products derived | |
| * | | // from this software without specific prior written permission. | |
| * You should have received a copy of the GNU Affero General Public Lice | | // | |
| nse | | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
| * along with this program. If not, see <http://www.gnu.org/licenses/>. | | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
| */ | | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
| | | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
| #pragma once | | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
| | | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
| #include "mongo/pch.h" | | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
| #include "../jsobj.h" | | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
| #include "../../util/net/message.h" | | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
| #include "../../util/processinfo.h" | | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
| #include "../../util/concurrency/spin_lock.h" | | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
| #include "mongo/db/pdfile.h" | | | |
| | | #ifndef V8_COUNTERS_H_ | |
| namespace mongo { | | #define V8_COUNTERS_H_ | |
| | | | |
| /** | | #include "../include/v8.h" | |
| * for storing operation counters | | #include "allocation.h" | |
| * note: not thread safe. ok with that for speed | | | |
| */ | | namespace v8 { | |
| class OpCounters { | | namespace internal { | |
| public: | | | |
| | | // StatsCounters is an interface for plugging into external | |
| OpCounters(); | | // counters for monitoring. Counters can be looked up and | |
| void incInsertInWriteLock(int n) { _insert.x += n; } | | // manipulated by name. | |
| void gotInsert() { _insert++; } | | | |
| void gotQuery() { _query++; } | | class StatsTable { | |
| void gotUpdate() { _update++; } | | public: | |
| void gotDelete() { _delete++; } | | // Register an application-defined function where | |
| void gotGetMore() { _getmore++; } | | // counters can be looked up. | |
| void gotCommand() { _command++; } | | void SetCounterFunction(CounterLookupCallback f) { | |
| | | lookup_function_ = f; | |
| void gotOp( int op , bool isCommand ); | | } | |
| | | | |
| BSONObj getObj() const; | | // Register an application-defined function to create | |
| | | // a histogram for passing to the AddHistogramSample function | |
| // thse are used by snmp, and other things, do not remove | | void SetCreateHistogramFunction(CreateHistogramCallback f) { | |
| const AtomicUInt * getInsert() const { return &_insert; } | | create_histogram_function_ = f; | |
| const AtomicUInt * getQuery() const { return &_query; } | | } | |
| const AtomicUInt * getUpdate() const { return &_update; } | | | |
| const AtomicUInt * getDelete() const { return &_delete; } | | // Register an application-defined function to add a sample | |
| const AtomicUInt * getGetMore() const { return &_getmore; } | | // to a histogram created with CreateHistogram function | |
| const AtomicUInt * getCommand() const { return &_command; } | | void SetAddHistogramSampleFunction(AddHistogramSampleCallback f) { | |
| | | add_histogram_sample_function_ = f; | |
| private: | | } | |
| void _checkWrap(); | | | |
| | | bool HasCounterFunction() const { | |
| // todo: there will be a lot of cache line contention on these. ne | | return lookup_function_ != NULL; | |
| ed to do something | | } | |
| // else eventually. | | | |
| AtomicUInt _insert; | | // Lookup the location of a counter by name. If the lookup | |
| AtomicUInt _query; | | // is successful, returns a non-NULL pointer for writing the | |
| AtomicUInt _update; | | // value of the counter. Each thread calling this function | |
| AtomicUInt _delete; | | // may receive a different location to store it's counter. | |
| AtomicUInt _getmore; | | // The return value must not be cached and re-used across | |
| AtomicUInt _command; | | // threads, although a single thread is free to cache it. | |
| }; | | int* FindLocation(const char* name) { | |
| | | if (!lookup_function_) return NULL; | |
| extern OpCounters globalOpCounters; | | return lookup_function_(name); | |
| extern OpCounters replOpCounters; | | } | |
| | | | |
| class NetworkCounter { | | // Create a histogram by name. If the create is successful, | |
| public: | | // returns a non-NULL pointer for use with AddHistogramSample | |
| NetworkCounter() : _bytesIn(0), _bytesOut(0), _requests(0), _overfl | | // function. min and max define the expected minimum and maximum | |
| ows(0) {} | | // sample values. buckets is the maximum number of buckets | |
| void hit( long long bytesIn , long long bytesOut ); | | // that the samples will be grouped into. | |
| void append( BSONObjBuilder& b ); | | void* CreateHistogram(const char* name, | |
| private: | | int min, | |
| long long _bytesIn; | | int max, | |
| long long _bytesOut; | | size_t buckets) { | |
| long long _requests; | | if (!create_histogram_function_) return NULL; | |
| | | return create_histogram_function_(name, min, max, buckets); | |
| | | } | |
| | | | |
| | | // Add a sample to a histogram created with the CreateHistogram | |
| | | // function. | |
| | | void AddHistogramSample(void* histogram, int sample) { | |
| | | if (!add_histogram_sample_function_) return; | |
| | | return add_histogram_sample_function_(histogram, sample); | |
| | | } | |
| | | | |
| | | private: | |
| | | StatsTable(); | |
| | | | |
| | | CounterLookupCallback lookup_function_; | |
| | | CreateHistogramCallback create_histogram_function_; | |
| | | AddHistogramSampleCallback add_histogram_sample_function_; | |
| | | | |
| | | friend class Isolate; | |
| | | | |
| | | DISALLOW_COPY_AND_ASSIGN(StatsTable); | |
| | | }; | |
| | | | |
| | | // StatsCounters are dynamically created values which can be tracked in | |
| | | // the StatsTable. They are designed to be lightweight to create and | |
| | | // easy to use. | |
| | | // | |
| | | // Internally, a counter represents a value in a row of a StatsTable. | |
| | | // The row has a 32bit value for each process/thread in the table and also | |
| | | // a name (stored in the table metadata). Since the storage location can b | |
| | | e | |
| | | // thread-specific, this class cannot be shared across threads. | |
| | | // | |
| | | // This class is designed to be POD initialized. It will be registered wit | |
| | | h | |
| | | // the counter system on first use. For example: | |
| | | // StatsCounter c = { "c:myctr", NULL, false }; | |
| | | struct StatsCounter { | |
| | | const char* name_; | |
| | | int* ptr_; | |
| | | bool lookup_done_; | |
| | | | |
| | | // Sets the counter to a specific value. | |
| | | void Set(int value) { | |
| | | int* loc = GetPtr(); | |
| | | if (loc) *loc = value; | |
| | | } | |
| | | | |
| | | // Increments the counter. | |
| | | void Increment() { | |
| | | int* loc = GetPtr(); | |
| | | if (loc) (*loc)++; | |
| | | } | |
| | | | |
| | | void Increment(int value) { | |
| | | int* loc = GetPtr(); | |
| | | if (loc) | |
| | | (*loc) += value; | |
| | | } | |
| | | | |
| | | // Decrements the counter. | |
| | | void Decrement() { | |
| | | int* loc = GetPtr(); | |
| | | if (loc) (*loc)--; | |
| | | } | |
| | | | |
| | | void Decrement(int value) { | |
| | | int* loc = GetPtr(); | |
| | | if (loc) (*loc) -= value; | |
| | | } | |
| | | | |
| | | // Is this counter enabled? | |
| | | // Returns false if table is full. | |
| | | bool Enabled() { | |
| | | return GetPtr() != NULL; | |
| | | } | |
| | | | |
| | | // Get the internal pointer to the counter. This is used | |
| | | // by the code generator to emit code that manipulates a | |
| | | // given counter without calling the runtime system. | |
| | | int* GetInternalPointer() { | |
| | | int* loc = GetPtr(); | |
| | | ASSERT(loc != NULL); | |
| | | return loc; | |
| | | } | |
| | | | |
| | | protected: | |
| | | // Returns the cached address of this counter location. | |
| | | int* GetPtr() { | |
| | | if (lookup_done_) return ptr_; | |
| | | lookup_done_ = true; | |
| | | ptr_ = FindLocationInStatsTable(); | |
| | | return ptr_; | |
| | | } | |
| | | | |
| | | private: | |
| | | int* FindLocationInStatsTable() const; | |
| | | }; | |
| | | | |
| | | // StatsCounterTimer t = { { L"t:foo", NULL, false }, 0, 0 }; | |
| | | struct StatsCounterTimer { | |
| | | StatsCounter counter_; | |
| | | | |
| | | int64_t start_time_; | |
| | | int64_t stop_time_; | |
| | | | |
| | | // Start the timer. | |
| | | void Start(); | |
| | | | |
| | | // Stop the timer and record the results. | |
| | | void Stop(); | |
| | | | |
| | | // Returns true if the timer is running. | |
| | | bool Running() { | |
| | | return counter_.Enabled() && start_time_ != 0 && stop_time_ == 0; | |
| | | } | |
| | | }; | |
| | | | |
| | | // A Histogram represents a dynamically created histogram in the StatsTable | |
| | | . | |
| | | // | |
| | | // This class is designed to be POD initialized. It will be registered wit | |
| | | h | |
| | | // the histogram system on first use. For example: | |
| | | // Histogram h = { "myhist", 0, 10000, 50, NULL, false }; | |
| | | struct Histogram { | |
| | | const char* name_; | |
| | | int min_; | |
| | | int max_; | |
| | | int num_buckets_; | |
| | | void* histogram_; | |
| | | bool lookup_done_; | |
| | | | |
| | | // Add a single sample to this histogram. | |
| | | void AddSample(int sample); | |
| | | | |
| | | // Returns true if this histogram is enabled. | |
| | | bool Enabled() { | |
| | | return GetHistogram() != NULL; | |
| | | } | |
| | | | |
| | | protected: | |
| | | // Returns the handle to the histogram. | |
| | | void* GetHistogram() { | |
| | | if (!lookup_done_) { | |
| | | lookup_done_ = true; | |
| | | histogram_ = CreateHistogram(); | |
| | | } | |
| | | return histogram_; | |
| | | } | |
| | | | |
| | | private: | |
| | | void* CreateHistogram() const; | |
| | | }; | |
| | | | |
| | | // A HistogramTimer allows distributions of results to be created | |
| | | // HistogramTimer t = { {L"foo", 0, 10000, 50, NULL, false}, 0, 0 }; | |
| | | struct HistogramTimer { | |
| | | Histogram histogram_; | |
| | | | |
| | | int64_t start_time_; | |
| | | int64_t stop_time_; | |
| | | | |
| | | // Start the timer. | |
| | | void Start(); | |
| | | | |
| | | // Stop the timer and record the results. | |
| | | void Stop(); | |
| | | | |
| | | // Returns true if the timer is running. | |
| | | bool Running() { | |
| | | return histogram_.Enabled() && (start_time_ != 0) && (stop_time_ == 0); | |
| | | } | |
| | | }; | |
| | | | |
| | | // Helper class for scoping a HistogramTimer. | |
| | | class HistogramTimerScope BASE_EMBEDDED { | |
| | | public: | |
| | | explicit HistogramTimerScope(HistogramTimer* timer) : | |
| | | timer_(timer) { | |
| | | timer_->Start(); | |
| | | } | |
| | | ~HistogramTimerScope() { | |
| | | timer_->Stop(); | |
| | | } | |
| | | private: | |
| | | HistogramTimer* timer_; | |
| | | }; | |
| | | | |
|
| long long _overflows; | | } } // namespace v8::internal | |
| | | | |
|
| SpinLock _lock; | | #endif // V8_COUNTERS_H_ | |
| }; | | | |
| | | | |
| extern NetworkCounter networkCounter; | | | |
| } | | | |
| | | | |
End of changes. 3 change blocks. |
| 86 lines changed or deleted | | 273 lines changed or added | |
|
| distlock.h | | distlock.h | |
| | | | |
| skipping to change at line 60 | | skipping to change at line 60 | |
| * Indicates an error in retrieving time values from remote servers. | | * Indicates an error in retrieving time values from remote servers. | |
| */ | | */ | |
| class TimeNotFoundException : public LockException { | | class TimeNotFoundException : public LockException { | |
| public: | | public: | |
| TimeNotFoundException( const char * msg , int code ) : LockExceptio
n( msg, code ) {} | | TimeNotFoundException( const char * msg , int code ) : LockExceptio
n( msg, code ) {} | |
| TimeNotFoundException( const string& msg, int code ) : LockExceptio
n( msg, code ) {} | | TimeNotFoundException( const string& msg, int code ) : LockExceptio
n( msg, code ) {} | |
| virtual ~TimeNotFoundException() throw() { } | | virtual ~TimeNotFoundException() throw() { } | |
| }; | | }; | |
| | | | |
| /** | | /** | |
|
| * The distributed lock is a configdb backed way of synchronizing syste | | * The distributed lock is a configdb backed way of synchronizing syste | |
| m-wide tasks. A task must be identified by a | | m-wide tasks. A task | |
| * unique name across the system (e.g., "balancer"). A lock is taken by | | * must be identified by a unique name across the system (e.g., "balanc | |
| writing a document in the configdb's locks | | er"). A lock is taken | |
| * collection with that name. | | * by writing a document in the configdb's locks collection with that n | |
| | | ame. | |
| * | | * | |
|
| * To be maintained, each taken lock needs to be revalidated ("pinged") | | * To be maintained, each taken lock needs to be revalidated ("pinged") | |
| within a pre-established amount of time. This | | within a | |
| * class does this maintenance automatically once a DistributedLock obj | | * pre-established amount of time. This class does this maintenance aut | |
| ect was constructed. | | omatically once a | |
| | | * DistributedLock object was constructed. The ping procedure records t | |
| | | he local time to | |
| | | * the ping document, but that time is untrusted and is only used as a | |
| | | point of reference | |
| | | * of whether the ping was refreshed or not. Ultimately, the clock a co | |
| | | nfigdb is the source | |
| | | * of truth when determining whether a ping is still fresh or not. This | |
| | | is achieved by | |
| | | * (1) remembering the ping document time along with config server time | |
| | | when unable to | |
| | | * take a lock, and (2) ensuring all config servers report similar time | |
| | | s and have similar | |
| | | * time rates (the difference in times must start and stay small). | |
| */ | | */ | |
| class DistributedLock { | | class DistributedLock { | |
| public: | | public: | |
| | | | |
| static LabeledLevel logLvl; | | static LabeledLevel logLvl; | |
| | | | |
| struct PingData { | | struct PingData { | |
| | | | |
| PingData( const string& _id , Date_t _lastPing , Date_t _remote
, OID _ts ) | | PingData( const string& _id , Date_t _lastPing , Date_t _remote
, OID _ts ) | |
| : id(_id), lastPing(_lastPing), remote(_remote), ts(_ts){ | | : id(_id), lastPing(_lastPing), remote(_remote), ts(_ts){ | |
| | | | |
| skipping to change at line 150 | | skipping to change at line 157 | |
| | | | |
| Date_t getRemoteTime(); | | Date_t getRemoteTime(); | |
| | | | |
| bool isRemoteTimeSkewed(); | | bool isRemoteTimeSkewed(); | |
| | | | |
| const string& getProcessId(); | | const string& getProcessId(); | |
| | | | |
| const ConnectionString& getRemoteConnection(); | | const ConnectionString& getRemoteConnection(); | |
| | | | |
| /** | | /** | |
|
| * Check the skew between a cluster of servers | | * Checks the skew among a cluster of servers and returns true if t | |
| | | he min and max clock | |
| | | * times among the servers are within maxClockSkew. | |
| */ | | */ | |
|
| static bool checkSkew( const ConnectionString& cluster, unsigned sk | | static bool checkSkew( const ConnectionString& cluster, | |
| ewChecks = NUM_LOCK_SKEW_CHECKS, unsigned long long maxClockSkew = MAX_LOCK | | unsigned skewChecks = NUM_LOCK_SKEW_CHECKS, | |
| _CLOCK_SKEW, unsigned long long maxNetSkew = MAX_LOCK_NET_SKEW ); | | unsigned long long maxClockSkew = MAX_LOCK_C | |
| | | LOCK_SKEW, | |
| | | unsigned long long maxNetSkew = MAX_LOCK_NET | |
| | | _SKEW ); | |
| | | | |
| /** | | /** | |
| * Get the remote time from a server or cluster | | * Get the remote time from a server or cluster | |
| */ | | */ | |
| static Date_t remoteTime( const ConnectionString& cluster, unsigned
long long maxNetSkew = MAX_LOCK_NET_SKEW ); | | static Date_t remoteTime( const ConnectionString& cluster, unsigned
long long maxNetSkew = MAX_LOCK_NET_SKEW ); | |
| | | | |
| static bool killPinger( DistributedLock& lock ); | | static bool killPinger( DistributedLock& lock ); | |
| | | | |
| /** | | /** | |
| * Namespace for lock pings | | * Namespace for lock pings | |
| | | | |
End of changes. 4 change blocks. |
| 13 lines changed or deleted | | 32 lines changed or added | |
|