protected override async Task ExecuteAsync(CancellationToken cancellationToken) { var cpuStats = new CPUStatsRequest { UsagePercent = CpuHelpers.UsagePercent }; var gcStats = new GCStatsRequest { Gen0CollectCount = GCHelpers.Gen0CollectCount, Gen1CollectCount = GCHelpers.Gen1CollectCount, Gen2CollectCount = GCHelpers.Gen2CollectCount, HeapMemory = GCHelpers.TotalMemory }; var availableWorkerThreads = 0; var availableCompletionPortThreads = 0; var maxWorkerThreads = 0; var maxCompletionPortThreads = 0; ThreadPool.GetAvailableThreads(out availableWorkerThreads, out availableCompletionPortThreads); ThreadPool.GetMaxThreads(out maxWorkerThreads, out maxCompletionPortThreads); var threadStats = new ThreadStatsRequest { MaxCompletionPortThreads = maxCompletionPortThreads, MaxWorkerThreads = maxWorkerThreads, AvailableCompletionPortThreads = availableCompletionPortThreads, AvailableWorkerThreads = availableWorkerThreads }; var statsRequest = new CLRStatsRequest { CPU = cpuStats, GC = gcStats, Thread = threadStats }; try { await _reporter.ReportAsync(statsRequest, cancellationToken); Logger.Information( $"Report CLR Stats. CPU UsagePercent {cpuStats.UsagePercent} GenCollectCount {gcStats.Gen0CollectCount} {gcStats.Gen1CollectCount} {gcStats.Gen2CollectCount} {gcStats.HeapMemory / (1024 * 1024)}M ThreadPool {threadStats.AvailableWorkerThreads} {threadStats.MaxWorkerThreads} {threadStats.AvailableCompletionPortThreads} {threadStats.MaxCompletionPortThreads}"); } catch (Exception exception) { Logger.Error("Report CLR Stats error.", exception); } }
private void clr(Channel channel, int serviceId, int serviceInstanceId) { var availableWorkerThreads = 0; var availableCompletionPortThreads = 0; var maxWorkerThreads = 0; var maxCompletionPortThreads = 0; var cpuStats = new CPUStatsRequest { UsagePercent = CpuHelpers.UsagePercent }; var gcStats = new GCStatsRequest { Gen0CollectCount = GCHelpers.Gen0CollectCount, Gen1CollectCount = GCHelpers.Gen1CollectCount, Gen2CollectCount = GCHelpers.Gen2CollectCount, HeapMemory = GCHelpers.TotalMemory }; ThreadPool.GetAvailableThreads(out availableWorkerThreads, out availableCompletionPortThreads); ThreadPool.GetMaxThreads(out maxWorkerThreads, out maxCompletionPortThreads); var threadStats = new ThreadStatsRequest { MaxCompletionPortThreads = maxCompletionPortThreads, MaxWorkerThreads = maxWorkerThreads, AvailableCompletionPortThreads = availableCompletionPortThreads, AvailableWorkerThreads = availableWorkerThreads }; var statsRequest = new CLRStatsRequest { CPU = cpuStats, GC = gcStats, Thread = threadStats }; try { var client = new CLRMetricReportService.CLRMetricReportServiceClient(channel); var request = new CLRMetricCollection { ServiceInstanceId = serviceInstanceId }; var metric = new CLRMetric { Cpu = new CPU { UsagePercent = statsRequest.CPU.UsagePercent }, Gc = new ClrGC { Gen0CollectCount = statsRequest.GC.Gen0CollectCount, Gen1CollectCount = statsRequest.GC.Gen1CollectCount, Gen2CollectCount = statsRequest.GC.Gen2CollectCount, HeapMemory = statsRequest.GC.HeapMemory }, Thread = new ClrThread { AvailableWorkerThreads = statsRequest.Thread.MaxWorkerThreads, AvailableCompletionPortThreads = statsRequest.Thread.MaxCompletionPortThreads, MaxWorkerThreads = statsRequest.Thread.MaxWorkerThreads, MaxCompletionPortThreads = statsRequest.Thread.MaxCompletionPortThreads }, Time = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds() }; request.Metrics.Add(metric); var reply = Task.Run(async() => { return(await client.collectAsync(request)); }).Result; } catch (Exception ex) { throw ex; } }