{"id":24546642,"url":"https://github.com/devizer/universe.cpuusage","last_synced_at":"2025-04-13T00:43:11.922Z","repository":{"id":40906701,"uuid":"220759279","full_name":"devizer/Universe.CpuUsage","owner":"devizer","description":"CPU Usage for .NET Core and .NET Framework for Linux, Windows and macOS with coverage of async/await scenarios.","archived":false,"fork":false,"pushed_at":"2025-04-05T14:29:18.000Z","size":825,"stargazers_count":66,"open_issues_count":2,"forks_count":3,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-04-13T00:43:07.101Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/devizer.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-11-10T08:00:10.000Z","updated_at":"2025-04-05T14:29:21.000Z","dependencies_parsed_at":"2025-01-08T10:22:37.140Z","dependency_job_id":"09d2b225-7f9a-48dd-9bf2-ad20114d5038","html_url":"https://github.com/devizer/Universe.CpuUsage","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/devizer%2FUniverse.CpuUsage","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/devizer%2FUniverse.CpuUsage/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/devizer%2FUniverse.CpuUsage/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/devizer%2FUniverse.CpuUsage/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/devizer","download_url":"https://codeload.github.com/devizer/Universe.CpuUsage/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248650418,"owners_count":21139672,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2025-01-22T22:16:19.664Z","updated_at":"2025-04-13T00:43:11.913Z","avatar_url":"https://github.com/devizer.png","language":"C#","readme":"# Universe.CpuUsage \u0026nbsp;\u0026nbsp;\u0026nbsp; [![Nuget](https://img.shields.io/nuget/v/Universe.CpuUsage?label=nuget.org)](https://www.nuget.org/packages/Universe.CpuUsage/)\nCPU Usage for .NET Core, .NET Framework and Mono on Linux, Windows and macOS\n\n## Crossplatform CPU Usage metrics\nIt receives the amount of time that the current **thread(s)/process** has executed in _**kernel**_ and _**user**_ mode.\n\nWorks everywhere: Linux, OSX and Windows.\n\nTargets everywhere: Net Framework 2.0+, Net Standard 1.3+, Net Core 1.0+\n\n## Coverage and supported OS\nMinimum OS requirements: Linux Kernel 2.6.26, Mac OS 10.9, Windows XP/2003\n\nAutotests using .NET Core and .NET Framework cover:\n- Linux x86_64 and Arm 64-bit on appveyor\n- Windows Server 2016 on appveyor\n- macOS 10.14 on travis-ci.org\n- Raspbian 10 Buster on Azure Piplines using self-hosted agent on Raspberry pi\n\nAutotests using Mono:\n- Linux x86_64, Arm 64-bit, Arm-v7 32 bit, i386 using mono on travis-ci.org\n- Mac OSX 10.10 using travis-ci.org\n\nManually tested on:\n- Windows 7 x86 (.NET Core), Windows 10 ARM64 (.NET Core)\n- FreeBSD 12 (both .NET Core and Mono).\n- Debian 8 on armv5 (Mono)\n\n\n| appveyor                   | travis-ci                                                                                 |\n|----------------------------|-------------------------------------------------------------------------------------------|\n| .NET Core: **Linux** x64, **Windows** x64. \u003cbr\u003eMono: **Linux** x64. | .NET Core: **macOS** 10.14, **Linux** Arm 64. \u003cbr\u003eMono: **Linux** Arm 64, Arm-v7, i386, **macOS** 10.10. |\n| \u003cbr\u003e\u003cp align=\"center\"\u003e[![Build status](https://ci.appveyor.com/api/projects/status/udq3dip23mqxlkjf?svg=true)](https://ci.appveyor.com/project/devizer/universe-cpuusage)\u003c/p\u003e | \u003cbr\u003e\u003cp align=\"center\"\u003e[![Build Status](https://travis-ci.org/devizer/Universe.CpuUsage.svg?branch=master)](https://travis-ci.org/devizer/Universe.CpuUsage)\u003c/p\u003e |\n\n### Integration tests on exotic platforms\nFor ***mono only*** platforms (i386, ppc64, mips, arm v5/v6, etc) here is the script for integration tests: [test-on-mono-only-platforms.sh](https://raw.githubusercontent.com/devizer/Universe.CpuUsage/master/test-on-mono-only-platforms.sh)\n```\nurl=https://raw.githubusercontent.com/devizer/Universe.CpuUsage/master/test-on-mono-only-platforms.sh;\n(wget -q -nv --no-check-certificate -O - $url 2\u003e/dev/null || curl -ksSL $url) | bash\n```\n\n## Implementation\nThe implementation utilizes platform invocation (P/Invoke) of the corresponding system libraries depending on the OS:\n\n| OS       | per thread implementation  | per process implementation   | library         |\n|----------|--------------------------|------------------------|-----------------|\n| Linux    | getrusage(RUSAGE_THREAD) | getrusage(RUSAGE_SELF) | libc.so         |\n| Windows  | GetThreadTimes()         | GetProcessTimes()      | kernel32.dll    |\n| Mac OS X | thread_info()            | getrusage(RUSAGE_SELF) | libSystem.dylib |\n\n## Precision depends on\nHere is a summary of CPU usage precision. In general, it depends on OS and version and it does not depend on CPU performance except of FreeBSD\n\n| OS                                                                         | Average Precision |\n|----------------------------------------------------------------------------|------------------:|\n| Windows Server 2025, AMD EPYC 7763                                         |       _15,625 μs_ |\n| Windows Server 2019, Xeon E5-2697 v3                                       |       _16,250 μs_ |\n| Linux, i5-10300, 4.0 GHz, kernel 6.8                                       |        _1,000 μs_ |\n| Ubuntu, x86-64 QEMU, kernel 6.8                                            |        _1,000 μs_ |\n| Debian, x86-64 QEMU, kernel 6.10                                           |        _4,000 μs_ |\n| Ubuntu, arm64 QEMU, kernel 6.8                                             |        _1,000 μs_ |\n| Debian, arm64 QEMU, kernel 6.10                                            |        _4,000 μs_ |\n| Linux, Xeon E5-2697 v3 @ 2.60GHz, kernel 5.0                               |        _4,000 μs_ |\n| Linux, ARMv7 H3 CPU, 1.50GHz, kernel 3.4                                   |       _10,000 μs_ |\n| Linux, ARMv7 H3 CPU, 1.10GHz, kernel 4.19                                  |        _4,000 μs_ |\n| Linux, ARMv7 H3 CPU, 1.30GHz, kernel 6.13                                  |        _4,000 μs_ |\n| Mac OS 10.14, Xeon E5-2697 v2 @ 2.70GHz                                    |           14.0 μs |\n| Mac OS 15, Apple M1                                                        |            3.0 μs |\n| FreeBSD 12, .NET Core 2.0, Xeon E3-1270 v2 @ 3.50GHz, pseudo kernel 2.6.32 |            3.0 μs |\n| FreeBSD 12, Mono 5.1, Xeon E3-1270 v2 @ 3.50GHz, native BSD 12             |            1.9 μs |\n\nDetailed histograms of precision are produced by PrecisionTest.cs\n\n## Low level API: class CpuUsage\n```csharp\nvar onStart = CpuUsage.GetByThread();\n... \nvar onEnd = CpuUsage.GetByThread();\nConsole.WriteLine(\"CPU Usage: \" + (onEnd-onStart));\n```\n\n## High level API: class CpuUsageAsyncWatcher\n```csharp\npublic void Configure(IApplicationBuilder app)\n{\n    // Here is a \"middleware\" that displays total CPU usage\n    // of all the Tasks executed by ASP.NET Core pipeline during each http request\n    app.Use(async (context, next) =\u003e\n    {\n        CpuUsageAsyncWatcher watcher = new CpuUsageAsyncWatcher();\n        await next.Invoke();\n        watcher.Stop();\n        Console.WriteLine($\"Cpu Usage by http request is {watcher.GetSummaryCpuUsage()}\");\n    });\n}\n```\n\n## Benchmark \nJust for illustration here is a comparison to well known DateTime.Now and Stopwatch using benchmark.net. All of them are taken using .NET Core 3.0 runtime.\n\n#### Linux x64, kernel 4.15\n|      Method |      Mean |    Error |   StdDev | Rank |  Gen 0 | Gen 1 | Gen 2 | Allocated |\n|------------ |----------:|---------:|---------:|-----:|-------:|------:|------:|----------:|\n| DateTime.Now() | 150.24 ns | 2.346 ns | 2.194 ns |    2 |      - |     - |     - |         - |\n|   Stopwatch |  77.27 ns | 0.473 ns | 0.419 ns |    1 | 0.0095 |     - |     - |      40 B |\n| Process CPU Usage | 795.18 ns | 8.000 ns | 6.681 ns |    3 |      - |     - |     - |         - |\n| Thread CPU Usage| 834.59 ns | 9.324 ns | 8.722 ns |    4 |      - |     - |     - |         - |\n\n#### Linux 32 bit @ ARM, kernel 3.4 (Orange PI, H3, 1500 MHz)\n|      Method |     Mean |     Error |    StdDev | Rank |  Gen 0 | Gen 1 | Gen 2 | Allocated |\n|------------ |---------:|----------:|----------:|-----:|-------:|------:|------:|----------:|\n| DateTime.Now | 2.788  μs | 0.0595  μs | 0.0944  μs |    2 |      - |     - |     - |         - |\n|   Stopwatch | 1.737  μs | 0.0539  μs | 0.0504  μs |    1 | 0.1945 |     - |     - |      32 B |\n| Process CPU Usage | 5.552  μs | 0.1662  μs | 0.4900  μs |    3 |      - |     - |     - |         - |\n|    Thread CPU Usage | 5.664  μs | 0.1136  μs | 0.1960  μs |    3 |      - |     - |     - |         - |\n\n#### macOS 10.14\n|      Method |        Mean |     Error |    StdDev | Rank |  Gen 0 | Gen 1 | Gen 2 | Allocated |\n|------------ |------------:|----------:|----------:|-----:|-------:|------:|------:|----------:|\n| DateTime.Now() |    73.40 ns |  1.506 ns |  2.475 ns |    1 |      - |     - |     - |         - |\n|   Stopwatch |    79.36 ns |  1.640 ns |  1.454 ns |    2 | 0.0025 |     - |     - |      40 B |\n|   Process CPU Usage | 1,979.10 ns | 29.771 ns | 26.391 ns |    4 |      - |     - |     - |         - |\n|    Thread CPU Usage | 1,921.54 ns | 35.869 ns | 33.552 ns |    3 |      - |     - |     - |         - |\n\n#### Windows Server 2016\n|      Method |      Mean |    Error |   StdDev | Rank |  Gen 0 | Gen 1 | Gen 2 | Allocated |\n|------------ |----------:|---------:|---------:|-----:|-------:|------:|------:|----------:|\n| DateTime.Now() | 217.08 ns | 1.667 ns | 1.559 ns |    4 |      - |     - |     - |         - |\n|   Stopwatch |  31.12 ns | 0.203 ns | 0.169 ns |    1 | 0.0095 |     - |     - |      40 B |\n|   Process CPU Usage | 200.49 ns | 3.743 ns | 3.501 ns |    2 |      - |     - |     - |         - |\n|    Thread CPU Usage | 205.11 ns | 3.970 ns | 4.413 ns |    3 |      - |     - |     - |         - |\n\n##### Legend\n- Stopwatch: `var sw = new Stopwatch(); var ticks = sw.ElapsedTicks;`\n- Process CPU Usage: `CpuUsageReader.GetByProcess();`\n- Thread CPU Usage: `CpuUsageReader.GetByThread();`\n- ns - nanosecond,  μs - microsecond\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdevizer%2Funiverse.cpuusage","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdevizer%2Funiverse.cpuusage","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdevizer%2Funiverse.cpuusage/lists"}