Ejemplo n.º 1
0
        static NetStatistics()
        {
            var statReader = new Utf8FileReader(NetDevPath, 512);

            try
            {
                statReader.SkipLine();
                statReader.SkipFragment('|');
                statReader.SkipWhiteSpaces();
                var receiveColumnCount = 0;
                while (true)
                {
                    var column = statReader.ReadWord();
                    if (column.IndexOf('|') >= 0)
                    {
                        if (column.Length != 0)
                        {
                            ++receiveColumnCount;
                        }
                        break;
                    }
                    ++receiveColumnCount;
                }
                ReceiveColumnCount = receiveColumnCount;
            }
            finally
            {
                statReader.Dispose();
            }
        }
Ejemplo n.º 2
0
        internal static IEnumerable <CpuStatistics> GetAll()
        {
            var statReader = new Utf8FileReader(StatPath, 4096);

            try
            {
                while (!statReader.EndOfStream)
                {
                    var cpuStr = statReader.ReadWord();
                    if (!cpuStr.StartsWith(CpuNumberStart.Span))
                    {
                        yield break;
                    }

                    var   cpuNumberStr = cpuStr.Slice(CpuNumberStart.Length);
                    short?cpuNumber;
                    if (cpuNumberStr.Length == 0)
                    {
                        cpuNumber = null;
                    }
                    else
                    {
                        Utf8Parser.TryParse(cpuNumberStr, out short num, out _);
                        cpuNumber = num;
                    }

                    var ticksPerSecond = (double)ProcFs.TicksPerSecond;
                    var userTime       = statReader.ReadInt64() / ticksPerSecond;
                    var niceTime       = statReader.ReadInt64() / ticksPerSecond;
                    var kernelTime     = statReader.ReadInt64() / ticksPerSecond;
                    var idleTime       = statReader.ReadInt64() / ticksPerSecond;
                    statReader.SkipWord();
                    var irqTime     = statReader.ReadInt64() / ticksPerSecond;
                    var softIrqTime = statReader.ReadInt64() / ticksPerSecond;

                    statReader.SkipLine();

                    yield return(new CpuStatistics
                    {
                        CpuNumber = cpuNumber,
                        UserTime = userTime,
                        NiceTime = niceTime,
                        KernelTime = kernelTime,
                        IdleTime = idleTime,
                        IrqTime = irqTime,
                        SoftIrqTime = softIrqTime
                    });
                }
            }
            finally
            {
                statReader.Dispose();
            }
        }
Ejemplo n.º 3
0
        internal static unsafe MemoryStatistics Get()
        {
            var statReader = new Utf8FileReader(StatPath, 2048);

            try
            {
                var sections = stackalloc long[(int)Section.Max];
                for (var i = 0; i < (int)Section.Max; ++i)
                {
                    sections[i] = -1;
                }

                var sectionsRead = 0;
                while (sectionsRead < (int)Section.Max)
                {
                    var section = statReader.ReadLine();

                    var nameEnd = section.IndexOf(':');
                    var name    = section.Slice(0, nameEnd);


                    var valueStart = nameEnd + 1;
                    while (section[valueStart] == ' ')
                    {
                        ++valueStart;
                    }
                    var valueEnd = section.IndexOf(' ', valueStart);
                    if (valueEnd < 0)
                    {
                        valueEnd = section.Length;
                    }
                    Utf8Parser.TryParse(section.Slice(valueStart, valueEnd - valueStart), out long value, out _);
                    value *= 0x400;

                    for (Section sectionType = default; sectionType < Section.Max; ++sectionType)
                    {
                        if (Names[(int)sectionType].Span.SequenceEqual(name))
                        {
                            sections[(int)sectionType] = value;
                            break;
                        }
                    }

                    sectionsRead = 0;
                    for (var i = 0; i < (int)Section.Max; ++i)
                    {
                        if (sections[i] >= 0)
                        {
                            ++sectionsRead;
                        }
                    }
                }

                return(new MemoryStatistics
                {
                    Total = sections[(int)Section.MemTotal],
                    Free = sections[(int)Section.MemFree],
                    Available = sections[(int)Section.MemAvailable],
                    SwapTotal = sections[(int)Section.SwapTotal],
                    SwapFree = sections[(int)Section.SwapFree]
                });
            }
            finally
            {
                statReader.Dispose();
            }
        }
Ejemplo n.º 4
0
        public void Refresh()
        {
            _initialized  = false;
            _commandLine  = null;
            _startTimeUtc = null;
            var statReader = new Utf8FileReader($"{ProcFs.RootPath}/{Pid}/stat", 512);

            try
            {
                // See http://man7.org/linux/man-pages/man5/proc.5.html /proc/[pid]/stat section

                // (1) pid
                statReader.SkipWord();

                // (2) name
                var name = statReader.ReadWord();
                _name = name.Slice(1, name.Length - 2).ToUtf8String();

                // (3) state
                _state = GetProcessState((char)statReader.ReadWord()[0]);

                // (4) ppid
                _parentPid = statReader.ReadInt32();

                // (5) pgrp
                _groupId = statReader.ReadInt32();

                // (6) session
                _sessionId = statReader.ReadInt32();

                // (7) tty_nr
                statReader.SkipWord();

                // (8) tpgid
                statReader.SkipWord();

                // (9) flags
                statReader.SkipWord();

                // (10) minflt
                _minorFaults = statReader.ReadInt64();

                // (11) cminflt
                statReader.SkipWord();

                // (12) majflt
                _majorFaults = statReader.ReadInt64();

                // (13) cmajflt
                statReader.SkipWord();

                // (14) utime
                _userProcessorTime = statReader.ReadInt64() / (double)ProcFs.TicksPerSecond;

                // (15) stime
                _kernelProcessorTime = statReader.ReadInt64() / (double)ProcFs.TicksPerSecond;

                // (16) cutime
                statReader.SkipWord();

                // (17) cstime
                statReader.SkipWord();

                // (18) priority
                _priority = statReader.ReadInt16();

                // (19) nice
                _nice = statReader.ReadInt16();

                // (20) num_threads
                _threadCount = statReader.ReadInt32();

                // (21) itrealvalue
                statReader.SkipWord();

                // (22) starttime
                _startTimeTicks = statReader.ReadInt64();

                // (23) vsize
                _virtualMemorySize = statReader.ReadInt64();

                // (24) rss
                _residentSetSize = statReader.ReadInt64() * Environment.SystemPageSize;
            }
            finally
            {
                statReader.Dispose();
            }

            _initialized = true;
        }