<commit_before>#define LOG_MEMORY_PERF_COUNTERS

using System;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Orleans.Runtime;

namespace Orleans.Statistics
{
    internal class LinuxEnvironmentStatistics : IHostEnvironmentStatistics, ILifecycleParticipant<ISiloLifecycle>, ILifecycleParticipant<IClusterClientLifecycle>, ILifecycleObserver, IDisposable
    {
        private readonly ILogger _logger;

        private const float KB = 1024f;

        /// <inheritdoc />
        public long? TotalPhysicalMemory { get; private set; }

        /// <inheritdoc />
        public float? CpuUsage { get; private set; }

        /// <inheritdoc />
        public long? AvailableMemory { get; private set; }

        /// <inheritdoc />
        private long MemoryUsage => GC.GetTotalMemory(false);

        private readonly TimeSpan MONITOR_PERIOD = TimeSpan.FromSeconds(5);

        private CancellationTokenSource _cts;
        private Task _monitorTask;

        private const string MEMINFO_FILEPATH = "/proc/meminfo";
        private const string CPUSTAT_FILEPATH = "/proc/stat";

        internal static readonly string[] RequiredFiles = new[]
        {
            MEMINFO_FILEPATH,
            CPUSTAT_FILEPATH
        };

        public LinuxEnvironmentStatistics(ILoggerFactory loggerFactory)
        {
            _logger = loggerFactory.CreateLogger<LinuxEnvironmentStatistics>();
        }

        public void Dispose()
        {
            _cts?.Dispose();
            _monitorTask?.Dispose();
        }

        public void Participate(ISiloLifecycle lifecycle)
        {
            lifecycle.Subscribe(ServiceLifecycleStage.RuntimeInitialize, this);
        }

        public void Participate(IClusterClientLifecycle lifecycle)
        {
            lifecycle.Subscribe(ServiceLifecycleStage.RuntimeInitialize, this);
        }

        public async Task OnStart(CancellationToken ct)
        {
            _logger.LogTrace($"Starting {nameof(LinuxEnvironmentStatistics)}");

            _cts = new CancellationTokenSource();
            ct.Register(() => _cts.Cancel());<commit_msg>Avoid disposing uncompleted task in LinuxEnvironmentStatistics (#6842)<commit_after>#define LOG_MEMORY_PERF_COUNTERS

using System;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Orleans.Runtime;

namespace Orleans.Statistics
{
    internal class LinuxEnvironmentStatistics : IHostEnvironmentStatistics, ILifecycleParticipant<ISiloLifecycle>, ILifecycleParticipant<IClusterClientLifecycle>, ILifecycleObserver, IDisposable
    {
        private readonly ILogger _logger;

        private const float KB = 1024f;

        /// <inheritdoc />
        public long? TotalPhysicalMemory { get; private set; }

        /// <inheritdoc />
        public float? CpuUsage { get; private set; }

        /// <inheritdoc />
        public long? AvailableMemory { get; private set; }

        /// <inheritdoc />
        private long MemoryUsage => GC.GetTotalMemory(false);

        private readonly TimeSpan MONITOR_PERIOD = TimeSpan.FromSeconds(5);

        private CancellationTokenSource _cts;
        private Task _monitorTask;

        private const string MEMINFO_FILEPATH = "/proc/meminfo";
        private const string CPUSTAT_FILEPATH = "/proc/stat";

        internal static readonly string[] RequiredFiles = new[]
        {
            MEMINFO_FILEPATH,
            CPUSTAT_FILEPATH
        };

        public LinuxEnvironmentStatistics(ILoggerFactory loggerFactory)
        {
            _logger = loggerFactory.CreateLogger<LinuxEnvironmentStatistics>();
        }

        public void Dispose()
        {
            if (_cts != null && !_cts.IsCancellationRequested)
            {
                _cts.Cancel();
            }
        }

        public void Participate(ISiloLifecycle lifecycle)
        {
            lifecycle.Subscribe(ServiceLifecycleStage.RuntimeInitialize, this);
        }

        public void Participate(IClusterClientLifecycle lifecycle)
        {
            lifecycle.Subscribe(ServiceLifecycleStage.RuntimeInitialize, this);
        }

        public async Task OnStart(CancellationToken ct)
        {
            _logger.LogTrace($"Starting {nameof(LinuxEnvironmentStatistics)}");

            _cts = new CancellationTokenSource();
            ct.Register(() => _cts.Cancel());
{'fraction_non_alphanumeric': 0.08265012187015289, 'fraction_numerical': 0.0031021493463328164, 'mean_word_length': 2.6054313099041533, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4336733', 'n_tokens_mistral': 1183, 'n_tokens_neox': 1140, 'n_words': 260}