示例#1
0
 private void UploadCounters(SqlConnection conn, SqlTransaction tran, IEnumerable <Interval> intervals)
 {
     lock (counterData)
     {
         PerformanceCounterSnapshot.SaveToDatabase(conn, PerformanceCountersTargetTable, counterData, tran, intervals);
     }
 }
示例#2
0
        private void CollectCounters(string serverName, SqlConnection targetConnection, List <CounterDefinition> counters)
        {
            var currentSnapshot = PerformanceCounterSnapshot.Take(targetConnection, counters);

            // For cumulative counters I subtract the value of the initial counter value as a base
            foreach (var snap in currentSnapshot.Where(s => s.Counter.Cumulative))
            {
                var baseSnap = baseCounters.FirstOrDefault(s => s.Counter.Name == snap.Counter.Name && s.Counter.Instance == snap.Counter.Instance && s.Interval.Server.Name == serverName);
                if (baseSnap != null)
                {
                    // take a copy of this snapshot
                    var tmpSnap = (PerformanceCounterSnapshot)snap.Clone();

                    //
                    // rebase counter
                    //
                    // see https://blogs.msdn.microsoft.com/oldnewthing/20160219-00/?p=93052
                    //
                    snap.Value -= baseSnap.RawValue;
                    if (snap.Counter.Name.EndsWith("/sec"))
                    {
                        snap.Value = snap.Value / (snap.Interval.EndDate.Subtract(baseSnap.Interval.EndDate).TotalSeconds);
                    }
                    snap.Rebased = true;

                    baseCounters[baseCounters.IndexOf(baseSnap)] = tmpSnap;
                }
                else
                {
                    // base counter not found: it needs to be added to the cache
                    baseCounters.Add(snap);
                }
            }

            lock (counterData)
            {
                counterData.AddRange(currentSnapshot.Where(t => (t.Counter.Cumulative && t.Rebased) || !t.Counter.Cumulative));
            }
        }