GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — Npgsql/src/Npgsql/NpgsqlEventSource.cs at main · roji/Npgsql Expand file tree/ Copy pathNpgsqlEventSource.csMore file actions 226 lines (189 loc) · 9.03 KB / Copy pathNpgsqlEventSource.csFile metadata and controls226 lines (189 loc) · 9.03 KB 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 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 using System; using System.Collections.Generic; using System.Diagnostics; using System.Threading; using System.Diagnostics.Tracing; using System.Runtime.CompilerServices; namespace Npgsql { sealed class NpgsqlEventSource : EventSource { public static readonly NpgsqlEventSource Log = new(); const string EventSourceName = "Npgsql"; internal const int CommandStartId = 3; internal const int CommandStopId = 4; #if !NETSTANDARD2_0 IncrementingPollingCounter? _bytesWrittenPerSecondCounter; IncrementingPollingCounter? _bytesReadPerSecondCounter; IncrementingPollingCounter? _commandsPerSecondCounter; PollingCounter? _totalCommandsCounter; PollingCounter? _failedCommandsCounter; PollingCounter? _currentCommandsCounter; PollingCounter? _preparedCommandsRatioCounter; PollingCounter? _poolsCounter; PollingCounter? _idleConnectionsCounter; PollingCounter? _busyConnectionsCounter; PollingCounter? _multiplexingAverageCommandsPerBatchCounter; PollingCounter? _multiplexingAverageWaitsPerBatchCounter; PollingCounter? _multiplexingAverageWriteTimePerBatchCounter; #endif long _bytesWritten; long _bytesRead; long _totalCommands; long _totalPreparedCommands; long _currentCommands; long _failedCommands; readonly object _poolsLock = new(); readonly HashSet<ConnectorPool> _pools = new(); long _multiplexingBatchesSent; long _multiplexingCommandsSent; long _multiplexingWaits; long _multiplexingTicksWritten; internal NpgsqlEventSource() : base(EventSourceName) {} // NOTE // - The 'Start' and 'Stop' suffixes on the following event names have special meaning in EventSource. They // enable creating 'activities'. // For more information, take a look at the following blog post: // https://blogs.msdn.microsoft.com/vancem/2015/09/14/exploring-eventsource-activity-correlation-and-causation-features/ // - A stop event's event id must be next one after its start event. internal void BytesWritten(long bytesWritten) => Interlocked.Add(ref _bytesWritten, bytesWritten); internal void BytesRead(long bytesRead) => Interlocked.Add(ref _bytesRead, bytesRead); public void CommandStart(string sql) { Interlocked.Increment(ref _totalCommands); Interlocked.Increment(ref _currentCommands); NpgsqlSqlEventSource.Log.CommandStart(sql); } [MethodImpl(MethodImplOptions.NoInlining)] public void CommandStop() { Interlocked.Decrement(ref _currentCommands); NpgsqlSqlEventSource.Log.CommandStop(); } internal void CommandStartPrepared() => Interlocked.Increment(ref _totalPreparedCommands); internal void CommandFailed() => Interlocked.Increment(ref _failedCommands); internal void PoolCreated(ConnectorPool pool) { lock (_poolsLock) { _pools.Add(pool); } } internal void MultiplexingBatchSent(int numCommands, int waits, Stopwatch stopwatch) { // TODO: CAS loop instead of 4 separate interlocked operations? Interlocked.Increment(ref _multiplexingBatchesSent); Interlocked.Add(ref _multiplexingCommandsSent, numCommands); Interlocked.Add(ref _multiplexingWaits, waits); Interlocked.Add(ref _multiplexingTicksWritten, stopwatch.ElapsedTicks); } #if !NETSTANDARD2_0 int GetIdleConnections() { // Note: there's no attempt here to be coherent in terms of race conditions, especially not with regards // to different counters. So idle and busy and be unsynchronized, as they're not polled together. lock (_poolsLock) { var sum = 0; foreach (var pool in _pools) { sum += pool.Statistics.Idle; } return sum; } } int GetBusyConnections() { // Note: there's no attempt here to be coherent in terms of race conditions, especially not with regards // to different counters. So idle and busy and be unsynchronized, as they're not polled together. lock (_poolsLock) { var sum = 0; foreach (var pool in _pools) { sum += pool.Statistics.Busy; } return sum; } } int GetPoolsCount() { lock (_poolsLock) { return _pools.Count; } } protected override void OnEventCommand(EventCommandEventArgs command) { if (command.Command == EventCommand.Enable) { // Comment taken from RuntimeEventSource in CoreCLR // NOTE: These counters will NOT be disposed on disable command because we may be introducing // a race condition by doing that. We still want to create these lazily so that we aren't adding // overhead by at all times even when counters aren't enabled. // On disable, PollingCounters will stop polling for values so it should be fine to leave them around. _bytesWrittenPerSecondCounter = new IncrementingPollingCounter("bytes-written-per-second", this, () => Interlocked.Read(ref _bytesWritten)) { DisplayName = "Bytes Written", DisplayRateTimeScale = TimeSpan.FromSeconds(1) }; _bytesReadPerSecondCounter = new IncrementingPollingCounter("bytes-read-per-second", this, () => Interlocked.Read(ref _bytesRead)) { DisplayName = "Bytes Read", DisplayRateTimeScale = TimeSpan.FromSeconds(1) }; _commandsPerSecondCounter = new IncrementingPollingCounter("commands-per-second", this, () => Interlocked.Read(ref _totalCommands)) { DisplayName = "Command Rate", DisplayRateTimeScale = TimeSpan.FromSeconds(1) }; _totalCommandsCounter = new PollingCounter("total-commands", this, () => Interlocked.Read(ref _totalCommands)) { DisplayName = "Total Commands", }; _currentCommandsCounter = new PollingCounter("current-commands", this, () => Interlocked.Read(ref _currentCommands)) { DisplayName = "Current Commands" }; _failedCommandsCounter = new PollingCounter("failed-commands", this, () => Interlocked.Read(ref _failedCommands)) { DisplayName = "Failed Commands" }; _preparedCommandsRatioCounter = new PollingCounter( "prepared-commands-ratio", this, () => (double)Interlocked.Read(ref _totalPreparedCommands) / Interlocked.Read(ref _totalCommands)) { DisplayName = "Prepared Commands Ratio", DisplayUnits = "%" }; _poolsCounter = new PollingCounter("connection-pools", this, () => GetPoolsCount()) { DisplayName = "Connection Pools" }; _idleConnectionsCounter = new PollingCounter("idle-connections", this, () => GetIdleConnections()) { DisplayName = "Idle Connections" }; _busyConnectionsCounter = new PollingCounter("busy-connections", this, () => GetBusyConnections()) { DisplayName = "Busy Connections" }; _multiplexingAverageCommandsPerBatchCounter = new PollingCounter("multiplexing-average-commands-per-batch", this, () => (double)Interlocked.Read(ref _multiplexingCommandsSent) / Interlocked.Read(ref _multiplexingBatchesSent)) { DisplayName = "Average commands per multiplexing batch" }; _multiplexingAverageWaitsPerBatchCounter = new PollingCounter("multiplexing-average-waits-per-batch", this, () => (double)Interlocked.Read(ref _multiplexingWaits) / Interlocked.Read(ref _multiplexingBatchesSent)) { DisplayName = "Average waits per multiplexing batch" }; _multiplexingAverageWriteTimePerBatchCounter = new PollingCounter("multiplexing-average-write-time-per-batch", this, () => (double)Interlocked.Read(ref _multiplexingTicksWritten) / Interlocked.Read(ref _multiplexingBatchesSent) / 1000) { DisplayName = "Average write time per multiplexing batch (us)", DisplayUnits = "us" }; } } #endif } } |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |