In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Today, I will talk to you about how .NET Core uses the CZGL.SystemInfo library to obtain host running resources, which may not be well understood by many people. In order to make you understand better, the editor has summarized the following for you. I hope you can get something from this article.
Brief introduction
CZGL.SystemInfo is a resource information acquisition library that supports Windows and Linux, which is used to obtain system environment, machine resource information, and system resource usage.
Nuget search for CZGL.SystemInfo to install.
I have annotated every property and method in the class library so that I can see it when I call it.
Platform universal
Under the CZGL.SystemInfo namespace, there is an EnvironmentInfo static class that gets all kinds of information.
There are three types in the CZGL.SystemInfo.Info namespace that are used to obtain and record different types of information.
MachineRunInfo is used to obtain the resource information used by the machine to run.
SystemPlatformInfo is used to obtain system platform information.
SystemRunEvnInfo acquires system attribute information
EnvironmentInfo.GetEnvironmentVariables () is used to get all the environment variables of the system.
Get a property information
You can use it like this.
/ / new instance gets MachineRunInfo m = new MachineRunInfo (); Console.WriteLine ("memory used by current process" + m.ThisUsedMem)
Each of the above three types has a static instance, which can also be used in this way
Console.WriteLine ("memory used by current process" + MachineRunInfo.Instance.ThisUsedMem)
There are three types: MachineRunInfo, SystemPlatformInfo and SystemRunEvnInfo, which can output information by using attributes directly.
Get all attribute information
If you want to output to the console or do a statistic at one time, you can use the methods in EnvironmentInfo to quickly generate information.
If the current system is in Chinese, Chinese comments will be output.
/ / Note that the units of some resources are kb / / obtain system platform information KeyValuePair [] a = env.GetSystemPlatformInfoValue (); / / obtain system operation attribute information KeyValuePair [] b = env.GetSystemRunInfoValue (); / / obtain machine resource information KeyValuePair [] c = env.GetMachineInfoValue () / / get all environment variables of the system KeyValuePair [] d = env.GetEnvironmentVariables ()
Print exampl
Console.WriteLine ("\ nsystem platform Information:\ n"); foreach (var item in a) {Console.WriteLine ($"{item.Key}: {item.Value}");} Console.WriteLine ("\ n\ nsystem Operation attribute Information:\ n") Foreach (var item in b) {Console.WriteLine ($"{item.Key}: {item.Value}");} Console.WriteLine ("\ n\ nMachine Resource Information:\ n") Foreach (var item in c) {Console.WriteLine ($"{item.Key}: {item.Value}");} Console.WriteLine ("\ n\ nAll system environment variables:\ n") Foreach (var item in d) {Console.WriteLine ($"{item.Key}: {item.Value}");}
Output (partial display)
System platform information: running framework: .net Core 3.1.0 operating system: Microsoft Windows 10.0.17763 operating system version: Microsoft Windows NT 6.2.9200.0 platform architecture: X64 system running attribute information: machine name: aaaa-PC current associated user name: aaa user network domain name: aaa-PC system Unified run time (milliseconds): 3227500Web core framework version: 3.1.0 whether to run in interactive mode: True partition disk: d:\ E:\, F:\, G:\, H:\, X:\ system directory: X:\ windows\ system32 machine resource information: current process used physical memory: 20020 current process consumed CPU time: 328.125 memory used by all processes in the system: system .Collections.Generic.KeyValuePair`2 [System.String System.Int64] [] system used memory: 5988340 all system environment variables: VisualStudioVersion: 16.0CommonProgramFiles (x86): X:\ ProgramFiles (x86)\ Common Files
You can also use (string, KeyValuePair []) GetMachineInfo (), etc., and string returns a description of this type of information.
Linux
Nuget searches for CZGL.SystemInfo.Linux installation.
In this library, Linux resource information includes process metering, memory metering, CPU metering, virtual memory metering, and various process running information metering.
It can only be obtained by instantiating the DynamicInfo.
There are five objects for mapping the corresponding information.
Tasks: used to count the number of processes and the number of processes in different states.
CpuState:CPU usage, CPU various load information.
Mem: physical memory and cache usage.
Swap: virtual memory usage.
PidInfo: information about the running resources of a process.
They all have an IsSuccess attribute, which is used to determine whether the Linux information can be obtained normally.
Instantiate the acquisition object
DynamicInfo info = new DynamicInfo (); use directly
You can get the corresponding object through the method.
Var item = info.GetTasks (); Console.WriteLine ("Total number of processes in the system:" + item.Total); Console.WriteLine ("number of running processes:" + item.Running); batch acquisition
The following is an example of batch acquisition, where each attribute and attribute value generates a key-value pair, and the list of information can be obtained in batches.
If the current system is in Chinese, Chinese comments will be output.
/ / obtain process statistics KeyValuePair [] a = info.GetRefTasks (); / / obtain CPU resource statistics KeyValuePair [] b = info.GetRefCpuState (); / / obtain memory statistics KeyValuePair [] c = info.GetRefMem (); / / obtain virtual memory statistics KeyValuePair [] d = info.GetRefSwap () Dictionary dic = info.GetPidInfo (); Console.WriteLine ("\ nprocess statistics:\ n"); foreach (var item in a) {Console.WriteLine ($"{item.Key}: {item.Value}");} Console.WriteLine ("\ n\ nCPU resource statistics:\ n") Foreach (var item in b) {Console.WriteLine ($"{item.Key}: {item.Value}");} Console.WriteLine ("\ n\ nmemory Statistics:\ n") Foreach (var item in c) {Console.WriteLine ($"{item.Key}: {item.Value}");} Console.WriteLine ("\ n\ nget virtual memory statistics:\ n") Foreach (var item in d) {Console.WriteLine ($"{item.Key}: {item.Value}");} Console.WriteLine ("\ n\ nResources used by each process:\ n") Console.WriteLine ("process Id process name belongs to user optimized high and low priority virtual memory physical memory shared memory process state occupies system CPU (%) memory (% d)") Foreach (var item in dic) {Console.WriteLine ($"{item.Key} {item.Value.Command} {item.Value.User} {item.Value.PR}" + $"{item.Value.Nice} {item.Value.VIRT} {item.Value.RES} {item.Value.SHR}" + $"{item.Value.State} {item.Value.CPU} {item.Value.Mem}") }}
Output
Process Statistics: Total: 93Running: 1Sleeping: 59Stopped: 0Zombie: 0CPU Resource Statistics: UserSpace: 1Sysctl: 0.6NI: 0Idolt: 98.3WaitIO: 0.1HardwareIRQ: 0SoftwareInterrupts: 0 memory Statistics: Total: 1009048Used: 334040Free: 85408Buffers: 589600CanUsed : 675008 get virtual memory statistics: Total: 0Used: 0Free: 0AvailMem: 505744 finish reading the above Do you have any further understanding of how .NET Core uses the CZGL.SystemInfo library to obtain host running resources? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.