public static void HandleCpuUsage(float usageInPercents) { var stats = new CpuUsageCallsRecord { StartedAt = SystemTime.UtcNow, }; var previousWriteIndex = nextWriteIndex; LastUsages[previousWriteIndex] = usageInPercents; nextWriteIndex = (nextWriteIndex + 1) % NumberOfItemsInQueue; if (previousWriteIndex < NumberOfItemsInQueue - 1) // waiting for queue to fill up { return; } nextWriteIndex = 0; var average = Average = LastUsages.Average(); if (average < 0) { return; // there was an error in getting the CPU stats, ignoring } CpuUsageCallsRecord cpuUsage; CpuUsageCallsRecordsQueue.TryPeek(out cpuUsage); if (average >= HighNotificationThreshold) { if (cpuUsage?.Reason != CpuUsageLevel.HighCpuUsage) { stats.Reason = CpuUsageLevel.HighCpuUsage; CpuUsageCallsRecordsQueue.Enqueue(stats); } RunCpuUsageHandlers(handler => handler.HandleHighCpuUsage()); } else if (average < LowNotificationThreshold) { if (cpuUsage?.Reason != CpuUsageLevel.LowCpuUsage) { stats.Reason = CpuUsageLevel.LowCpuUsage; CpuUsageCallsRecordsQueue.Enqueue(stats); } RunCpuUsageHandlers(handler => handler.HandleLowCpuUsage()); } //Normal CPU usage else if (cpuUsage?.Reason != CpuUsageLevel.NormalCpuUsage) { stats.Reason = CpuUsageLevel.NormalCpuUsage; CpuUsageCallsRecordsQueue.Enqueue(stats); } }
public static void RunLowMemoryHandlers(string reason) { var inactiveHandlers = new List <WeakReference <ILowMemoryHandler> >(); var sp = Stopwatch.StartNew(); var stats = new LowMemoryCalledRecord { StartedAt = SystemTime.UtcNow, Reason = reason }; foreach (var lowMemoryHandler in LowMemoryHandlers) { ILowMemoryHandler handler; if (lowMemoryHandler.TryGetTarget(out handler)) { try { var res = handler.HandleLowMemory(); if (!string.IsNullOrEmpty(res.Summary)) { stats.Operations.Add(res); } } catch (Exception e) { Log.Error("Failure to process low memory notification (low memory handler - " + handler + ")", e); } } } stats.Duration = sp.Elapsed; LowMemoryCallRecords.Enqueue(stats); inactiveHandlers.ForEach(x => LowMemoryHandlers.TryRemove(x)); }
public async Task InternalPush(ServerStatisticsDataItem item) { _monitoringData.Enqueue(new InMemoryTimeValue(item.CurrentValue, DateTime.UtcNow)); Order = item.Order; Display = item.CurrentValueDisplay; await Task.CompletedTask; }
private void OnIncomingData(object sender, SerialDataReceivedEventArgs e) { SerialPort sp = (SerialPort)sender; string data; try { data = sp.ReadLine(); DataPoint p = Parse(data); if (p != null) { if (p.GetType() == typeof(IRDataPoint)) { irDataPointQueue.Enqueue(p as IRDataPoint); EmitDataPoint(this, p); } } } catch (Exception) { } }