- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试将 CPU 总使用率的百分比获取到 label1.Caption
我搜索并找到了这些:
不起作用 - http://www.vbforums.com/showthread.php?345723-DELPHI-Get-CPU-Usage
不是我需要的 - http://delphi.cjcsoft.net/viewthread.php?tid=42837
这就是我正在做的事情:
我相信有一种简单的方法,比如我们获取 RAM 使用情况。
GlobalMemoryStatus(RamStats);
Label1.Caption := Format('RAM: %d %%', [RamStats.dwMemoryLoad]);
最佳答案
我找到了一篇文章,determine-cpu-usage-of-current-process-c-and-c ,关于如何获取当前进程的CPU使用率。
现在我们需要做更多的工作,通过将每个正在运行的进程的 CPU 使用百分比相加来计算总 CPU 使用百分比:
function GetTotalCpuUsagePct(): Double;
var
ProcessID: TProcessID;
RunningProcessIDs : TArray<TProcessID>;
begin
Result := 0.0;
RunningProcessIDs := GetRunningProcessIDs;
DeleteNonExistingProcessIDsFromCache(RunningProcessIDs);
for ProcessID in RunningProcessIDs do
Result := Result + GetProcessCpuUsagePct( ProcessID );
end;
获取正在运行的进程 ID 后,我们开始调用DeleteNonExistingProcessIDsFromCache
清理缓存,该缓存保存 GetProcessCpuUsagePct
中所需的先前 Cpu 使用时间:自上次查询以来已停止的每个进程都会从此缓存中删除。
GetProcessCpuUsagePct
是核心,它是determine-cpu-usage-of-current-process-c-and-c的翻译。 。此函数需要使用 ProcessID 从 Cpu 使用缓存 LatestProcessCpuUsageCache
(单元中的全局)检索先前的读数。请注意,不建议每隔 200 毫秒调用一次 GetToalCpuUsageCpu
,因为它可能会给出错误的结果。
function GetProcessCpuUsagePct(ProcessID: TProcessID): Double;
function SubtractFileTime(FileTime1: TFileTIme; FileTime2: TFileTIme): TFileTIme;
begin
Result := TFileTIme(Int64(FileTime1) - Int64(FileTime2));
end;
var
ProcessCpuUsage: TProcessCpuUsage;
ProcessHandle: THandle;
SystemTimes: TSystemTimesRec;
SystemDiffTimes: TSystemTimesRec;
ProcessDiffTimes: TProcessTimesRec;
ProcessTimes: TProcessTimesRec;
SystemTimesIdleTime: TFileTime;
ProcessTimesCreationTime: TFileTime;
ProcessTimesExitTime: TFileTime;
begin
Result := 0.0;
LatestProcessCpuUsageCache.TryGetValue(ProcessID, ProcessCpuUsage);
if ProcessCpuUsage = nil then
begin
ProcessCpuUsage := TProcessCpuUsage.Create;
LatestProcessCpuUsageCache.Add(ProcessID, ProcessCpuUsage);
end;
// method from:
// http://www.philosophicalgeek.com/2009/01/03/determine-cpu-usage-of-current-process-c-and-c/
ProcessHandle := OpenProcess(PROCESS_QUERY_INFORMATION or PROCESS_VM_READ, False, ProcessID);
if ProcessHandle <> 0 then
begin
try
if GetSystemTimes(SystemTimesIdleTime, SystemTimes.KernelTime, SystemTimes.UserTime) then
begin
SystemDiffTimes.KernelTime := SubtractFileTime(SystemTimes.KernelTime, ProcessCpuUsage.LastSystemTimes.KernelTime);
SystemDiffTimes.UserTime := SubtractFileTime(SystemTimes.UserTime, ProcessCpuUsage.LastSystemTimes.UserTime);
ProcessCpuUsage.LastSystemTimes := SystemTimes;
if GetProcessTimes(ProcessHandle, ProcessTimesCreationTime, ProcessTimesExitTime, ProcessTimes.KernelTime, ProcessTimes.UserTime) then
begin
ProcessDiffTimes.KernelTime := SubtractFileTime(ProcessTimes.KernelTime, ProcessCpuUsage.LastProcessTimes.KernelTime);
ProcessDiffTimes.UserTime := SubtractFileTime(ProcessTimes.UserTime, ProcessCpuUsage.LastProcessTimes.UserTime);
ProcessCpuUsage.LastProcessTimes := ProcessTimes;
if (Int64(SystemDiffTimes.KernelTime) + Int64(SystemDiffTimes.UserTime)) > 0 then
Result := (Int64(ProcessDiffTimes.KernelTime) + Int64(ProcessDiffTimes.UserTime)) / (Int64(SystemDiffTimes.KernelTime) + Int64(SystemDiffTimes.UserTime)) * 100;
end;
end;
finally
CloseHandle(ProcessHandle);
end;
end;
end;
这是 Windows 7 上结果的屏幕截图。
单位完整列表:
unit uTotalCpuUsagePct;
interface
function GetTotalCpuUsagePct : Double;
implementation
uses
SysUtils, DateUtils, Windows, PsAPI, TlHelp32, ShellAPI, Generics.Collections;
type
TProcessID = DWORD;
TSystemTimesRec = record
KernelTime: TFileTIme;
UserTime: TFileTIme;
end;
TProcessTimesRec = record
KernelTime: TFileTIme;
UserTime: TFileTIme;
end;
TProcessCpuUsage = class
LastSystemTimes: TSystemTimesRec;
LastProcessTimes: TProcessTimesRec;
ProcessCPUusagePercentage: Double;
end;
TProcessCpuUsageList = TObjectDictionary<TProcessID, TProcessCpuUsage>;
var
LatestProcessCpuUsageCache : TProcessCpuUsageList;
LastQueryTime : TDateTime;
(* -------------------------------------------------------------------------- *)
function GetRunningProcessIDs: TArray<TProcessID>;
var
SnapProcHandle: THandle;
ProcEntry: TProcessEntry32;
NextProc: Boolean;
begin
SnapProcHandle := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if SnapProcHandle <> INVALID_HANDLE_VALUE then
begin
try
ProcEntry.dwSize := SizeOf(ProcEntry);
NextProc := Process32First(SnapProcHandle, ProcEntry);
while NextProc do
begin
SetLength(Result, Length(Result) + 1);
Result[Length(Result) - 1] := ProcEntry.th32ProcessID;
NextProc := Process32Next(SnapProcHandle, ProcEntry);
end;
finally
CloseHandle(SnapProcHandle);
end;
TArray.Sort<TProcessID>(Result);
end;
end;
(* -------------------------------------------------------------------------- *)
function GetProcessCpuUsagePct(ProcessID: TProcessID): Double;
function SubtractFileTime(FileTime1: TFileTIme; FileTime2: TFileTIme): TFileTIme;
begin
Result := TFileTIme(Int64(FileTime1) - Int64(FileTime2));
end;
var
ProcessCpuUsage: TProcessCpuUsage;
ProcessHandle: THandle;
SystemTimes: TSystemTimesRec;
SystemDiffTimes: TSystemTimesRec;
ProcessDiffTimes: TProcessTimesRec;
ProcessTimes: TProcessTimesRec;
SystemTimesIdleTime: TFileTime;
ProcessTimesCreationTime: TFileTime;
ProcessTimesExitTime: TFileTime;
begin
Result := 0.0;
LatestProcessCpuUsageCache.TryGetValue(ProcessID, ProcessCpuUsage);
if ProcessCpuUsage = nil then
begin
ProcessCpuUsage := TProcessCpuUsage.Create;
LatestProcessCpuUsageCache.Add(ProcessID, ProcessCpuUsage);
end;
// method from:
// http://www.philosophicalgeek.com/2009/01/03/determine-cpu-usage-of-current-process-c-and-c/
ProcessHandle := OpenProcess(PROCESS_QUERY_INFORMATION or PROCESS_VM_READ, False, ProcessID);
if ProcessHandle <> 0 then
begin
try
if GetSystemTimes(SystemTimesIdleTime, SystemTimes.KernelTime, SystemTimes.UserTime) then
begin
SystemDiffTimes.KernelTime := SubtractFileTime(SystemTimes.KernelTime, ProcessCpuUsage.LastSystemTimes.KernelTime);
SystemDiffTimes.UserTime := SubtractFileTime(SystemTimes.UserTime, ProcessCpuUsage.LastSystemTimes.UserTime);
ProcessCpuUsage.LastSystemTimes := SystemTimes;
if GetProcessTimes(ProcessHandle, ProcessTimesCreationTime, ProcessTimesExitTime, ProcessTimes.KernelTime, ProcessTimes.UserTime) then
begin
ProcessDiffTimes.KernelTime := SubtractFileTime(ProcessTimes.KernelTime, ProcessCpuUsage.LastProcessTimes.KernelTime);
ProcessDiffTimes.UserTime := SubtractFileTime(ProcessTimes.UserTime, ProcessCpuUsage.LastProcessTimes.UserTime);
ProcessCpuUsage.LastProcessTimes := ProcessTimes;
if (Int64(SystemDiffTimes.KernelTime) + Int64(SystemDiffTimes.UserTime)) > 0 then
Result := (Int64(ProcessDiffTimes.KernelTime) + Int64(ProcessDiffTimes.UserTime)) / (Int64(SystemDiffTimes.KernelTime) + Int64(SystemDiffTimes.UserTime)) * 100;
end;
end;
finally
CloseHandle(ProcessHandle);
end;
end;
end;
(* -------------------------------------------------------------------------- *)
procedure DeleteNonExistingProcessIDsFromCache(const RunningProcessIDs : TArray<TProcessID>);
var
FoundKeyIdx: Integer;
Keys: TArray<TProcessID>;
n: Integer;
begin
Keys := LatestProcessCpuUsageCache.Keys.ToArray;
for n := Low(Keys) to High(Keys) do
begin
if not TArray.BinarySearch<TProcessID>(RunningProcessIDs, Keys[n], FoundKeyIdx) then
LatestProcessCpuUsageCache.Remove(Keys[n]);
end;
end;
(* -------------------------------------------------------------------------- *)
function GetTotalCpuUsagePct(): Double;
var
ProcessID: TProcessID;
RunningProcessIDs : TArray<TProcessID>;
begin
Result := 0.0;
RunningProcessIDs := GetRunningProcessIDs;
DeleteNonExistingProcessIDsFromCache(RunningProcessIDs);
for ProcessID in RunningProcessIDs do
Result := Result + GetProcessCpuUsagePct( ProcessID );
end;
(* -------------------------------------------------------------------------- *)
initialization
LatestProcessCpuUsageCache := TProcessCpuUsageList.Create( [ doOwnsValues ] );
// init:
GetTotalCpuUsagePct;
finalization
LatestProcessCpuUsageCache.Free;
end.
测试代码:
单元单元1;
interface
uses
Vcl.Forms, System.SysUtils, Vcl.Controls, Vcl.StdCtrls, System.Classes,
Vcl.ExtCtrls,
uTotalCpuUsagePct;
type
TForm1 = class(TForm)
Timer1: TTimer;
Label1: TLabel;
procedure Timer1Timer(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
begin
// start cpu load thread
TThread.CreateAnonymousThread(
procedure
begin
while True do
begin
end;
end).Start;
end;
procedure TForm1.Timer1Timer(Sender: TObject);
var
TotalCPUusagePercentage: Double;
begin
TotalCPUusagePercentage := GetTotalCpuUsagePct();
Label1.Caption := 'Total cpu: ' + IntToStr(Round(TotalCPUusagePercentage)) + '%';
end;
end.
关于delphi - 获取CPU总使用率百分比,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33571061/
我们有一个 SQL 服务器,其中包含大约 40 个不同的数据库(每个数据库大约 1-5GB)。该服务器是8核2.3G CPU和32Gigs RAM。 27Gig 固定到 SQL Server。 CPU
我通过创建一个简单的循环并在数组中添加元素来测试 Java 8 并行流 API 的性能。 与非并行相比,我获得了巨大的性能提升。 但是当我检查我的任务管理器时,我看到了一个不受控制的 CPU 使用率,
我在使用 JFX 应用程序时遇到了一些问题。在我的本地开发系统(Linux)上,我的应用程序的 CPU 使用率约为 0-2%。当我在客户 Windows 虚拟机系统上安装并运行我的应用程序时,CPU
我在 unix 上工作。我想知道进程当前的 cpu 使用情况。我知道 ps 给出了在进程启动之前使用的 cpu 平均值 - 这不是当前使用情况。 有没有办法从 top 命令只打印 cpu 而无需 10
我尝试对许多文件进行哈希处理,但它没有使用满 CPU 能力。它只消耗25%。我测试将繁重的进程移动到线程中。但仍然没有什么不同。我来自 nodejs 使用 sharp 库。有同样的任务。它消耗所有的C
有没有办法在 CentOS 中获取 CPU 使用率?我需要解析这些信息并将其从 Perl 脚本中绘制出来,因此它最好是一个简单的工具,可以打印出一个单一的输出。 最佳答案 更简单,看/proc/loa
早上好。 目前我正在 Ubuntu 服务器 11.10 中运行 Java Web 应用程序。对于我的 Java 应用程序,我使用的是 apache、tomcat 和 mysql。 在过去的几周里,我的
我想做的事 我有一个计算密集型 OCaml 应用程序,我希望它在后台运行而不影响正常的计算机使用。我想为用户提供两个选项: (1) 应用程序仅在 CPU 使用率几乎为 0% 时运行; (2) 应用程序
我使用Couchdb创建了一个私有(private)NPM镜像,但我发现beam.smp将我的 CPU 使用率保持在 100%,有没有办法降低它,比如 50%? 非常感谢你。 最佳答案 您不能直接限制
我正在 docker 容器内构建一个项目,在创建容器时没有任何资源限制。当我监控它时,我看到了不同的 CPU 使用率结果。 来自 ctop 来自 Grafana(全节点导出器图表) 来自 cAdvis
我需要在 Web 开发编码 session 期间收集有关 Firefox CPU 使用率的数据,我想知道是否可以监视特定 firefox 插件的 CPU 使用率。 现在我正在使用 windows 的
R 是单线程的。 使用 R,如何检查 Windows 和 Linux 中有多少内核/线程正在运行 R? (或运行了多少卢比) 使用 R,如何检查 Windows 和 Linux 中运行 R 的每个内核
我正在尝试像示例中那样测试 Kubernetes HPA here kubectl run php-apache --image=gcr.io/google_containers/hpa-exampl
在我们的办公室,我们有一个开发服务器:Win 2k8 server R2 - Coldfusion 9(.0.0) - MySQL 5 ... 几乎每天早上上类时,我都会发现服务器的 CPU 为 50
我有一组 cpu 消耗执行,每个执行都在低优先级的线程中运行。这些线程将在一个进程(如 IIS)中运行,该进程具有许多我不想减慢它们速度的其他线程。我想计算所有其他线程的 cpu 使用率,如果它大于
我是 azure 云的新手,我已经部署了我的第一个辅助角色。 在我的本地系统中需要 30 分钟才能完成的过程在 azure 辅助角色上需要 1 个多小时。 为了查找问题,我已访问辅助角色的远程桌面。我
这是我的测试 boost::tribool 示例: #include #include "boost/logic/tribool.hpp" int main() { boost::logic::tr
我正在使用 docker 远程 API 来检索正在运行的容器的统计信息。对于 CPU 使用情况,我得到的例子是: "cpu_stats": { "cpu_usage": { "to
我是 azure 云的新手,我已经部署了我的第一个辅助角色。 在我的本地系统中需要 30 分钟才能完成的过程在 azure 辅助角色上需要 1 个多小时。 为了查找问题,我已访问辅助角色的远程桌面。我
我知道意外的无限循环通常会导致 CPU 使用率较高。但是,我不太明白为什么。谁能给我解释一下吗? 最佳答案 CPU 在执行该循环(永远不会结束)时无法执行任何其他操作。即使您使用的是抢占式多任务系统(
我是一名优秀的程序员,十分优秀!