public override async Task<int> CollectRegisterCounterValuesAsync()
        {
            lock (_syncRoot)
            {
                try
                {
                    StopTimer();

                    var swMain = new StopwatchHighPrecision();
                    var timeInfo = new Dictionary<string, long>();

                    if (_timestamp == null)
                    {
                        _timestamp = DateTime.UtcNow;
                        var nw = Floor(_timestamp.Value, new TimeSpan(0, 0, 0, 1));
                        _timestamp = nw;
                    }

                    var timestamp = DateTime.UtcNow;

                    var precision = TimeUnit.Seconds;
                    //No synchronization when running the safe collector engine
                    //timeInfo.Add(TimerConstants.Synchronize, swMain.ElapsedSegment);

                    //TODO: Create a mutex lock here (So that two counters canno read the same signature at the same time, since the content of the _performanceCounterGroup might change during this process.

                    //Prepare read
                    var performanceCounterInfos = PrepareCounters();
                    timeInfo.Add(TimerConstants.Prepare, swMain.ElapsedSegment);

                    //Perform Read (This should be as fast and short as possible)
                    var values = ReadValues(performanceCounterInfos);
                    timeInfo.Add(TimerConstants.Read, swMain.ElapsedSegment);

                    //Prepare result                
                    var points = FormatResult(performanceCounterInfos, values, precision, timestamp).ToArray();
                    timeInfo.Add(TimerConstants.Format, swMain.ElapsedSegment);

                    //Queue result
                    Enqueue(points);
                    timeInfo.Add(TimerConstants.Enque, swMain.ElapsedSegment);

                    //Cleanup
                    RemoveObsoleteCounters(values, performanceCounterInfos);
                    timeInfo.Add(TimerConstants.Cleanup, swMain.ElapsedSegment);

                    if (_metadata)
                    {
                        Enqueue(MetaDataBusiness.GetCollectorPoint(EngineName, Name, points.Length, timeInfo, null).ToArray());
                    }

                    OnCollectRegisterCounterValuesEvent(new CollectRegisterCounterValuesEventArgs(Name, points.Length, timeInfo, 0, OutputLevel.Default));

                    //TODO: Release mutex

                    //TOOD: Send metadata about the read to influx, (this should be configurable)

                    return points.Length;
                }
                catch (Exception exception)
                {
                    OnCollectRegisterCounterValuesEvent(new CollectRegisterCounterValuesEventArgs(Name, exception));
                    return -1;
                }
                finally
                {
                    ResumeTimer();
                }
            }
        }
        public override async Task <int> CollectRegisterCounterValuesAsync()
        {
            lock (_syncRoot)
            {
                try
                {
                    StopTimer();

                    var swMain   = new StopwatchHighPrecision();
                    var timeInfo = new Dictionary <string, long>();

                    if (_timestamp == null)
                    {
                        _timestamp = DateTime.UtcNow;
                        var nw = Floor(_timestamp.Value, new TimeSpan(0, 0, 0, 1));
                        _timestamp = nw;
                    }

                    var timestamp = DateTime.UtcNow;

                    var precision = TimeUnit.Seconds;
                    //No synchronization when running the safe collector engine
                    //timeInfo.Add(TimerConstants.Synchronize, swMain.ElapsedSegment);

                    //TODO: Create a mutex lock here (So that two counters canno read the same signature at the same time, since the content of the _performanceCounterGroup might change during this process.

                    //Prepare read
                    var performanceCounterInfos = PrepareCounters();
                    timeInfo.Add(TimerConstants.Prepare, swMain.ElapsedSegment);

                    //Perform Read (This should be as fast and short as possible)
                    var values = ReadValues(performanceCounterInfos);
                    timeInfo.Add(TimerConstants.Read, swMain.ElapsedSegment);

                    //Prepare result
                    var points = FormatResult(performanceCounterInfos, values, precision, timestamp).ToArray();
                    timeInfo.Add(TimerConstants.Format, swMain.ElapsedSegment);

                    //Queue result
                    Enqueue(points);
                    timeInfo.Add(TimerConstants.Enque, swMain.ElapsedSegment);

                    //Cleanup
                    RemoveObsoleteCounters(values, performanceCounterInfos);
                    timeInfo.Add(TimerConstants.Cleanup, swMain.ElapsedSegment);

                    if (_metadata)
                    {
                        Enqueue(MetaDataBusiness.GetCollectorPoint(EngineName, Name, points.Length, timeInfo, null).ToArray());
                    }

                    OnCollectRegisterCounterValuesEvent(new CollectRegisterCounterValuesEventArgs(Name, points.Length, timeInfo, 0, OutputLevel.Default));

                    //TODO: Release mutex

                    //TOOD: Send metadata about the read to influx, (this should be configurable)

                    return(points.Length);
                }
                catch (Exception exception)
                {
                    OnCollectRegisterCounterValuesEvent(new CollectRegisterCounterValuesEventArgs(Name, exception));
                    return(-1);
                }
                finally
                {
                    ResumeTimer();
                }
            }
        }
Example #3
0
        public override async Task <int> CollectRegisterCounterValuesAsync()
        {
            lock (_syncRoot)
            {
                try
                {
                    var swMain   = new StopwatchHighPrecision();
                    var timeInfo = new Dictionary <string, long>();

                    double elapseOffsetSeconds = 0;
                    if (_timestamp == null)
                    {
                        _sw        = new StopwatchHighPrecision();
                        _timestamp = DateTime.UtcNow;
                        var nw = Floor(_timestamp.Value, new TimeSpan(0, 0, 0, 1));
                        _timestamp = nw;
                    }
                    else
                    {
                        var elapsedTotal = _sw.ElapsedTotal;

                        elapseOffsetSeconds = new TimeSpan(elapsedTotal).TotalSeconds - SecondsInterval * _counter;

                        if (_missCounter >= 6)
                        {
                            //Reset everything and start over.
                            OnCollectRegisterCounterValuesEvent(new CollectRegisterCounterValuesEventArgs(Name, string.Format("Missed {0} cycles. Resetting and starting over.", _missCounter), OutputLevel.Warning));

                            _timestamp   = null;
                            _counter     = 0;
                            _missCounter = 0;
                            return(-4);
                        }

                        if (elapseOffsetSeconds > SecondsInterval)
                        {
                            _missCounter++;
                            _counter = _counter + 1 + (int)(elapseOffsetSeconds / SecondsInterval);
                            OnCollectRegisterCounterValuesEvent(new CollectRegisterCounterValuesEventArgs(Name, string.Format("Dropping {0} steps.", (int)elapseOffsetSeconds), OutputLevel.Warning));
                            return(-2);
                        }

                        if (elapseOffsetSeconds < SecondsInterval * -1)
                        {
                            _missCounter++;
                            OnCollectRegisterCounterValuesEvent(new CollectRegisterCounterValuesEventArgs(Name, string.Format("Jumping 1 step. ({0} = new TimeSpan({1}).TotalSeconds - {2} * {3})", (int)elapseOffsetSeconds, elapsedTotal, SecondsInterval, _counter), OutputLevel.Warning));
                            return(-3);
                        }

                        _missCounter = 0;

                        //Adjust interval
                        var next = 1000 * (SecondsInterval - elapseOffsetSeconds);
                        if (next > 0)
                        {
                            SetTimerInterval(next);
                        }
                    }

                    var timestamp = _timestamp.Value.AddSeconds(SecondsInterval * _counter);
                    _counter++;

                    var precision = TimeUnit.Seconds;
                    timeInfo.Add(TimerConstants.Synchronize, swMain.ElapsedSegment);

                    //TODO: Create a mutex lock here (So that two counters canno read the same signature at the same time, since the content of the _performanceCounterGroup might change during this process.

                    //Prepare read
                    var performanceCounterInfos = PrepareCounters();
                    timeInfo.Add(TimerConstants.Prepare, swMain.ElapsedSegment);

                    //Perform Read (This should be as fast and short as possible)
                    var values = ReadValues(performanceCounterInfos);
                    timeInfo.Add(TimerConstants.Read, swMain.ElapsedSegment);

                    //Prepare result
                    var points = FormatResult(performanceCounterInfos, values, precision, timestamp).ToArray();
                    timeInfo.Add(TimerConstants.Format, swMain.ElapsedSegment);

                    //Queue result
                    Enqueue(points);
                    timeInfo.Add(TimerConstants.Enque, swMain.ElapsedSegment);

                    //Cleanup
                    RemoveObsoleteCounters(values, performanceCounterInfos);
                    timeInfo.Add(TimerConstants.Cleanup, swMain.ElapsedSegment);

                    if (_metadata)
                    {
                        Enqueue(MetaDataBusiness.GetCollectorPoint(EngineName, Name, points.Length, timeInfo, elapseOffsetSeconds).ToArray());
                    }

                    OnCollectRegisterCounterValuesEvent(new CollectRegisterCounterValuesEventArgs(Name, points.Count(), timeInfo, elapseOffsetSeconds, OutputLevel.Default));

                    //TODO: Release mutex

                    //TOOD: Send metadata about the read to influx, (this should be configurable)

                    return(points.Length);
                }
                catch (Exception exception)
                {
                    OnCollectRegisterCounterValuesEvent(new CollectRegisterCounterValuesEventArgs(Name, exception));
                    return(-1);
                }
            }
        }
        public override async Task<int> CollectRegisterCounterValuesAsync()
        {
            lock (_syncRoot)
            {
                try
                {
                    var swMain = new StopwatchHighPrecision();
                    var timeInfo = new Dictionary<string, long>();

                    double elapseOffsetSeconds = 0;
                    if (_timestamp == null)
                    {
                        _sw = new StopwatchHighPrecision();
                        _timestamp = DateTime.UtcNow;
                        var nw = Floor(_timestamp.Value, new TimeSpan(0, 0, 0, 1));
                        _timestamp = nw;
                    }
                    else
                    {
                        var elapsedTotal = _sw.ElapsedTotal;

                        elapseOffsetSeconds = new TimeSpan(elapsedTotal).TotalSeconds - SecondsInterval * _counter;

                        if (_missCounter >= 6)
                        {
                            //Reset everything and start over.
                            OnCollectRegisterCounterValuesEvent(new CollectRegisterCounterValuesEventArgs(Name, string.Format("Missed {0} cycles. Resetting and starting over.", _missCounter), OutputLevel.Warning));

                            _timestamp = null;
                            _counter = 0;
                            _missCounter = 0;
                            return -4;
                        }

                        if (elapseOffsetSeconds > SecondsInterval)
                        {
                            _missCounter++;
                            _counter = _counter + 1 + (int)(elapseOffsetSeconds / SecondsInterval);
                            OnCollectRegisterCounterValuesEvent(new CollectRegisterCounterValuesEventArgs(Name, string.Format("Dropping {0} steps.", (int)elapseOffsetSeconds), OutputLevel.Warning));
                            return -2;
                        }

                        if (elapseOffsetSeconds < SecondsInterval * -1)
                        {
                            _missCounter++;
                            OnCollectRegisterCounterValuesEvent(new CollectRegisterCounterValuesEventArgs(Name, string.Format("Jumping 1 step. ({0} = new TimeSpan({1}).TotalSeconds - {2} * {3})", (int)elapseOffsetSeconds, elapsedTotal, SecondsInterval, _counter), OutputLevel.Warning));
                            return -3;
                        }

                        _missCounter = 0;

                        //Adjust interval
                        var next = 1000 * (SecondsInterval - elapseOffsetSeconds);
                        if (next > 0)
                        {
                            SetTimerInterval(next);
                        }
                    }

                    var timestamp = _timestamp.Value.AddSeconds(SecondsInterval * _counter);
                    _counter++;

                    var precision = TimeUnit.Seconds;
                    timeInfo.Add(TimerConstants.Synchronize, swMain.ElapsedSegment);

                    //TODO: Create a mutex lock here (So that two counters canno read the same signature at the same time, since the content of the _performanceCounterGroup might change during this process.

                    //Prepare read
                    var performanceCounterInfos = PrepareCounters();
                    timeInfo.Add(TimerConstants.Prepare, swMain.ElapsedSegment);

                    //Perform Read (This should be as fast and short as possible)
                    var values = ReadValues(performanceCounterInfos);
                    timeInfo.Add(TimerConstants.Read, swMain.ElapsedSegment);

                    //Prepare result                
                    var points = FormatResult(performanceCounterInfos, values, precision, timestamp).ToArray();
                    timeInfo.Add(TimerConstants.Format, swMain.ElapsedSegment);

                    //Queue result
                    Enqueue(points);
                    timeInfo.Add(TimerConstants.Enque, swMain.ElapsedSegment);

                    //Cleanup
                    RemoveObsoleteCounters(values, performanceCounterInfos);
                    timeInfo.Add(TimerConstants.Cleanup, swMain.ElapsedSegment);

                    if (_metadata)
                    {
                        Enqueue(MetaDataBusiness.GetCollectorPoint(EngineName, Name, points.Length, timeInfo, elapseOffsetSeconds).ToArray());
                    }

                    OnCollectRegisterCounterValuesEvent(new CollectRegisterCounterValuesEventArgs(Name, points.Count(), timeInfo, elapseOffsetSeconds, OutputLevel.Default));

                    //TODO: Release mutex

                    //TOOD: Send metadata about the read to influx, (this should be configurable)

                    return points.Length;
                }
                catch (Exception exception)
                {
                    OnCollectRegisterCounterValuesEvent(new CollectRegisterCounterValuesEventArgs(Name, exception));
                    return -1;
                }
            }
        }
        public async Task<int> CollectRegisterCounterValuesAsync()
        {
            try
            {
                var swMain = new StopwatchHighPrecision();
                var timeInfo = new Dictionary<string, long>();

                double elapseOffset = 0;
                if (_timestamp == null)
                {
                    _sw = new StopwatchHighPrecision();
                    _timestamp = DateTime.UtcNow;
                    var nw = Floor(_timestamp.Value, new TimeSpan(0, 0, 0, 1));
                    _timestamp = nw;
                }
                else
                {
                    var elapsedTotal = _sw.ElapsedTotal;

                    elapseOffset = new TimeSpan(elapsedTotal).TotalSeconds - _performanceCounterGroup.SecondsInterval * _counter;

                    if (_missCounter >= 6)
                    {
                        //Reset everything and start over.
                        OnCollectRegisterCounterValuesEvent(new CollectRegisterCounterValuesEventArgs(_name, string.Format("Missed {0} counts. Resetting and start over.", _missCounter), OutputLevel.Warning));

                        _timestamp = null;
                        _counter = 0;
                        _missCounter = 0;
                        return -4;
                    }

                    if (elapseOffset > 1)
                    {
                        _missCounter++;
                        _counter = _counter + 1 + (int)elapseOffset;
                        OnCollectRegisterCounterValuesEvent(new CollectRegisterCounterValuesEventArgs(_name, string.Format("Dropping {0} steps.", (int)elapseOffset), OutputLevel.Warning));
                        return -2;
                    }

                    if (elapseOffset < -1)
                    {
                        _missCounter++;
                        OnCollectRegisterCounterValuesEvent(new CollectRegisterCounterValuesEventArgs(_name, string.Format("Jumping 1 step. ({0} = new TimeSpan({1}).TotalSeconds - {2} * {3})", (int)elapseOffset, elapsedTotal, _performanceCounterGroup.SecondsInterval, _counter), OutputLevel.Warning));
                        return -3;
                    }

                    _missCounter = 0;

                    //Adjust interval
                    var next = 1000 * (_performanceCounterGroup.SecondsInterval - elapseOffset);
                    if (next > 0)
                    {
                        _timer.Interval = next;
                    }
                }

                var timestamp = _timestamp.Value.AddSeconds(_performanceCounterGroup.SecondsInterval * _counter);
                _counter++;

                var precision = TimeUnit.Seconds; //TimeUnit.Microseconds;
                timeInfo.Add("Synchronize", swMain.ElapsedSegment);

                //Prepare read
                var performanceCounterInfos = _performanceCounterGroup.PerformanceCounterInfos.Where(x => x.PerformanceCounter != null).ToArray();
                timeInfo.Add("Prepare",swMain.ElapsedSegment);

                //Perform Read (This should be as fast and short as possible)
                var values = ReadValues(performanceCounterInfos);
                timeInfo.Add("Read", swMain.ElapsedSegment);

                //Prepare result                
                var points = FormatResult(performanceCounterInfos, values, precision, timestamp);
                timeInfo.Add("Format", swMain.ElapsedSegment);

                //Queue result
                _sendBusiness.Enqueue(points);
                timeInfo.Add("Enque", swMain.ElapsedSegment);

                OnCollectRegisterCounterValuesEvent(new CollectRegisterCounterValuesEventArgs(_name, points.Count(), timeInfo, elapseOffset, OutputLevel.Default));

                return points.Length;
            }
            catch (Exception exception)
            {
                OnCollectRegisterCounterValuesEvent(new CollectRegisterCounterValuesEventArgs(_name, exception));
                return -1;
            }
        }