Exemple #1
0
        public CapturedMetric[] GetMetrics()
        {
            CapturedMetadata[] profiledMethodsCopy;
            CapturedMetric[]   metrics;

            lock (registrationLock)
            {
                profiledMethodsCopy = Metadata;
                metrics             = new CapturedMetric[ProfiledMethodCount];
            }

            var treadLocalCollectorsCopy = _collectors.Values;

            var timestamp = ReferenceFrame.Now();

            for (var i = 0; i < metrics.Length; i++)
            {
                var method = profiledMethodsCopy[i];

                var attempts = 0;
                while (true)
                {
                    metrics[i].Timestamp = timestamp.Ticks;

                    foreach (var threadLocalCollector in treadLocalCollectorsCopy)
                    {
                        if (threadLocalCollector.GetSample(method, out var threadLocalData))
                        {
                            metrics[i].AddData(threadLocalData);
                        }
                    }

                    timestamp = ReferenceFrame.Now();


                    // Detect if our thread has been preempted, and retry if so.
                    if (timestamp.Ticks - metrics[i].Timestamp < timestampTolerance)
                    {
                        break;
                    }
                    else
                    {
                        metrics[i] = default;
                        attempts++;

                        if (attempts > 3)
                        {
                            Console.WriteLine("Too many non-voluntary preemptions in the publisher thread. ");
                            metrics[i].SetInvalid();
                            break;
                        }
                    }
                }
            }

            return(metrics);
        }
Exemple #2
0
        private void PublishMetrics(IReadOnlyList <CapturedMetadata> methods, CapturedMetric[] metrics)
        {
            for (var i = 0; i < metrics.Length; i++)
            {
                CapturedMetric lastSample;
                if (i < lastSamples.Length)
                {
                    lastSample = lastSamples[i];
                }
                else
                {
                    lastSample = new CapturedMetric {
                        Timestamp = ReferenceFrame.StartTime.Ticks
                    };
                }


                metrics[i].GetDifference(lastSample, out var differenceSample);

                PublishMetric(methods[i], differenceSample);
            }

            lastSamples = metrics;
        }