dotnet / runtime

.NET is a cross-platform runtime for cloud, mobile, desktop, and IoT apps.
https://docs.microsoft.com/dotnet/core/
MIT License
14.98k stars 4.66k forks source link

How to get Common System Hardware information across platforms #22948

Open AlexGhiondea opened 7 years ago

AlexGhiondea commented 7 years ago

@heartlocker commented on Mon Jul 24 2017

As the .NET Core project could run on Windows, Linux and Mac OS. Is it possible for me to get the PhysicalMemory size of the running machine on these three OS. Just like in .NET framework, by using Microsoft.VisualBasic.Devices.ComputerInfo to get the Physical Memory information of the windows machine


@Petermarcu commented on Wed Jul 26 2017

@AlexGhiondea , should this issue be moved to CoreFX to make an API proposal?


@AlexGhiondea commented on Wed Jul 26 2017

@Petermarcu yes -- I will move it.

ygoe commented 4 years ago

@danmosemsft Ehm, I'm talking about the system total and system free memory, not the used memory by a single process. AFAIK this information is not available up to .NET Core 2.2, probably also not in 3.1. I could not find suitable Windows API functions. There's the Linux free command to determine those. I'm especially interested in separate numbers for buffers/cache memory usage to determine the actual memory need, not including caches that fill up the free memory. Basically everything from here:

$ free -h
              total        used        free      shared  buff/cache   available
Mem:            15G        5.4G        726M        1.3G        9.2G        8.3G
Swap:          4.0G        100M        3.9G

CPU counters could be used to determine how much the CPU is currently used. In Linux there are different counters, available with iostat:

avg-cpu:  %user   %nice %system %iowait  %steal   %idle
           5.00    0.00    0.69    7.25    0.00   87.06
danmoseley commented 4 years ago

@ygoe oh of course - my bad.

OK, to make this issue concrete -- I think we need a concrete API proposal

  1. not too large in extent (better to use specialized package)
  2. well defined - unambiguous how to implement, even in containers, etc.
  3. meaningful across platforms
  4. broadly useful for real scenarios

@richlander you have spent a lot of time thinking about the memory and CPU counters that the runtime itself should read (for example, to properly respect limits on a container). Any thoughts here?

jkotas commented 4 years ago

GC.GetGCMemoryInfo is the (container-aware) API added in 3.0 that returns memory details about memory used and available.

ygoe commented 4 years ago

GetGCMemoryInfo doesn't sound like it would return system-wide data.

jkotas commented 4 years ago

Returning non-virtualized system-wide data does not make sense in containers.

ygoe commented 4 years ago

Yeah, but I'm not using containers. 😉 I'm fully aware that inside containers you have a very limited view on the host system.

damageboy commented 4 years ago

@jkotas: that is really on a case-by-case basis, no? CPU manufacturer and cache layout, for example, make a lot of sense and remain constant for all cases (unless the virtualization engine is lying to you by emulating CPUID), while the more often virtualized properties have to be the virtualized/containerized versions too.

Specifically, CPU topology and memory would have to be container aware. Virtualization is already good at exposing the virtual values through the normal "APIs". It would take herculean efforts to read/access the physical/host properties in a virtualized system.

jasonliaocn commented 4 years ago

Some of my projects are planning to be hosted in Linux but I can't get the hardware info. Is there any update on this? or anybody will be working on this? I find java they have oshi but all net core are talking about the "RuntimeInformation"

KamranShahid commented 4 years ago

Some of my projects are planning to be hosted in Linux but I can't get the hardware info. Is there any update on this? or anybody will be working on this? I find java they have oshi but all net core are talking about the "RuntimeInformation"

At the current time you have to be specific about what hardware information currently you requires. There are limited support and some workaround as listed above which can be used scenario by scenario

damageboy commented 4 years ago

@KamranShahid

I think you will find partial overlap between what you want and what WILL be provided in: https://github.com/dotnet/runtime/issues/785

jasonliaocn commented 4 years ago

@KamranShahid the specific hardwares are cpu, disk, network, board, memory, the information include the name, unique id, manufacture or some other infomations wihch like the WMI can provided before. some of the product require a license based on the hardware and now we need to rewrite this in C++ insead of .net core.

jasonliaocn commented 4 years ago

find this MatthewKing/DeviceId

KamranShahid commented 4 years ago

@KamranShahid the specific hardwares are cpu, disk, network, board, memory, the information include the name, unique id, manufacture or some other infomations wihch like the WMI can provided before. some of the product require a license based on the hardware and now we need to rewrite this in C++ insead of .net core.

It s up-to you. Mine problem was solved as per above hack that time

juliusfriedman commented 4 years ago

I find the following names ambiguous, is this indicating the cache exists? it's size and if so what metric. (Potentially only comments but a better name would also help)

    public int FirstLevelCacheSize { get; protected set; }
    public int SecondLevelCache { get; protected set; }
    public int ThirdLevelCache { get; protected set; }

What I am mainly looking for out of this is an easy way to determine when tis best to ATTEMPT to go parallel, see https://github.com/dotnet/runtime/issues/37602 for further info.

Thank you for your time!

startewho commented 3 years ago

It 's a long time for looking forwart this function.

Jinjinov commented 3 years ago

Even if somebody point me to third party nuget package then it will also be helpful

@KamranShahid My attempt at cross platform hardware info: https://github.com/Jinjinov/Hardware.Info

danmoseley commented 3 years ago

As noted above the next step here is for someone passionate to reach consensus among those interested on a formal proposal following the template. My suggestion is to do this in increments, starting with a proposal for the most key part - maybe that's cache size. It may be clearer to create that as a new issue, following that template.

Jakar510 commented 3 years ago

Even if somebody point me to third party nuget package then it will also be helpful

@KamranShahid My attempt at cross platform hardware info: https://github.com/Jinjinov/Hardware.Info

Thank you