简体   繁体   English

跟踪每个进程的 CPU 和内存使用情况

[英]Tracking CPU and Memory usage per process

I suspect that one of my applications eats more CPU cycles than I want it to.我怀疑我的一个应用程序占用了比我想要的更多的 CPU 周期。 The problem is - it happens in bursts, and just looking at the task manager doesn't help me as it shows immediate usage only.问题是 - 它是突然发生的,只是查看任务管理器对我没有帮助,因为它只显示即时使用情况。

Is there a way (on Windows) to track the history of CPU & Memory usage for some process.有没有办法(在 Windows 上)跟踪某个进程的 CPU 和内存使用历史。 Eg I will start tracking "firefox", and after an hour or so will see a graph of its CPU & memory usage during that hour.例如,我将开始跟踪“firefox”,大约一小时后将看到该小时内其 CPU 和内存使用情况的图表。

I'm looking for either a ready-made tool or a programmatic way to achieve this.我正在寻找现成的工具或编程方式来实现这一目标。

Just type perfmon into Start > Run and press enter.只需在“ Start > Run键入perfmon ,然后按 Enter。 When the Performance window is open, click on the + sign to add new counters to the graph.当 Performance 窗口打开时,单击 + 号将新计数器添加到图表中。 The counters are different aspects of how your PC works and are grouped by similarity into groups called "Performance Object".计数器是 PC 工作方式的不同方面,并按相似性分组到称为“性能对象”的组中。

For your questions, you can choose the "Process", "Memory" and "Processor" performance objects.对于您的问题,您可以选择“进程”、“内存”和“处理器”性能对象。 You then can see these counters in real time然后你可以实时看到这些计数器

You can also specify the utility to save the performance data for your inspection later.您还可以指定实用程序来保存性能数据以供以后检查。 To do this, select "Performance Logs and Alerts" in the left-hand panel.为此,请在左侧面板中选择“性能日志和警报”。 (It's right under the System Monitor console which provides us with the above mentioned counters. If it is not there, click "File" > "Add/remove snap-in", click Add and select "Performance Logs and Alerts" in the list".) From the "Performance Logs and Alerts", create a new monitoring configuration under "Counter Logs". Then you can add the counters, specify the sampling rate, the log format (binary or plain text) and log location. (它就在 System Monitor 控制台下方,它为我们提供了上述计数器。如果没有,请单击“文件”>“添加/删除管理单元”,单击“添加”并在列表中选择“性能日志和警报” ".) 从“性能日志和警报”中,在“计数器日志”下创建一个新的监控配置。然后您可以添加计数器、指定采样率、日志格式(二进制或纯文本)和日志位置。

Process Explorer可以显示 进程占用的总 CPU 时间,以及每个进程的历史图表。

使用 perfmon.exe,我尝试使用“进程”计数器下的“私有字节”计数器来跟踪内存使用情况,并且效果很好。

maybe you can use this.也许你可以使用这个。 It should work for you and will report processor time for the specified process.它应该适合您,并将报告指定进程的处理器时间。

@echo off
: Rich Kreider <rjk@techish.net>
: report processor time for given process until process exits (could be expanded to use a PID to be more
: precise)
: Depends:  typeperf
: Usage:  foo.cmd <processname>

set process=%~1
echo Press CTRL-C To Stop...
:begin
for /f "tokens=2 delims=," %%c in ('typeperf "\Process(%process%)\%% Processor Time" -si 1 -sc 1 ^| find /V "\\"') do (
if %%~c==-1 (
goto :end
) else (
echo %%~c%%
goto begin
)
)

:end
echo Process seems to have terminated.

I agree, perfmon.exe allows you to add counters (right click on the right panel) for any process you want to monitor.我同意, perfmon.exe 允许您为要监视的任何进程添加计数器(右键单击右侧面板)。

Performance Object: Process Check "Select instances from list" and select firefox.性能对象:处理选中“从列表中选择实例”并选择 firefox。

WMI is Windows Management Instrumentation, and it's built into all recent versions of Windows. WMI 是 Windows Management Instrumentation,它内置于所有最新版本的 Windows 中。 It allows you to programmatically track things like CPU usage, disk I/O, and memory usage.它允许您以编程方式跟踪 CPU 使用率、磁盘 I/O 和内存使用率等内容。

Perfmon.exe is a GUI front-end to this interface, and can monitor a process, write information to a log, and allow you to analyze the log after the fact. Perfmon.exe 是该界面的GUI 前端,可以监控进程,将信息写入日志,并允许您事后分析日志。 It's not the world's most elegant program, but it does get the job done.它不是世界上最优雅的程序,但它确实完成了工作。

Process Lasso is designed more for process automation and priority class optimization, not graphs. Process Lasso 更多地设计用于流程自动化和优先级优化,而不是图形。 That said, it does offer per-process CPU utilization history (drawn as a white line on the graph) but it does NOT offer per-process memory utilization history.这就是说,它确实提供每个进程的CPU占用率的历史(画成白线图形上的),但它提供每个进程的内存使用率的历史。

DISCLAIMER: I am the author of Process Lasso, but am not actually endorsing it here - as there are better solutions (perfmon being the best).免责声明:我是 Process Lasso 的作者,但实际上并没有在这里支持它 - 因为有更好的解决方案(perfmon 是最好的)。

The best thing ever is Windows Vista+ Resource and Performance Monitor.有史以来最好的东西是 Windows Vista+ 资源和性能监视器。 It can track usage of CPU, Memory, Network, and Disk accesses by processes over time.它可以跟踪进程随时间推移对 CPU、内存、网络和磁盘访问的使用情况。 It is a great overall system information utility that should have been created long ago.它是一个很棒的整体系统信息实用程序,早就应该创建了。 Unless I am mistaken, it can track per-process CPU and memory utilization over time (amongst the other things listed).除非我弄错了,否则它可以随着时间的推移跟踪每个进程的 CPU 和内存利用率(以及列出的其他内容)。

Although I have not tried this out, ProcDump seems like a better solution.虽然我还没有尝试过,但ProcDump似乎是一个更好的解决方案。

Description from site:来自网站的描述:

ProcDump is a command-line utility whose primary purpose is monitoring an application for CPU spikes and generating crash dumps during a spike that an administrator or developer can use to determine the cause of the spike. ProcDump 是一个命令行实用程序,其主要目的是监视应用程序的 CPU 峰值并在峰值期间生成故障转储,管理员或开发人员可以使用它来确定峰值的原因。 ProcDump also includes hung window monitoring (using the same definition of a window hang that Windows and Task Manager use), unhandled exception monitoring and can generate dumps based on the values of system performance counters. ProcDump 还包括挂起的窗口监控(使用与 Windows 和任务管理器使用的窗口挂起的相同定义)、未处理的异常监控并可以根据系统性能计数器的值生成转储。 It also can serve as a general process dump utility that you can embed in other scripts.它还可以用作通用进程转储实用程序,您可以将其嵌入到其他脚本中。

You can also try using a C#/Perl/Java script get the utilization data using WMI Commands, and below is the steps for it.您也可以尝试使用 C#/Perl/Java 脚本使用 WMI 命令获取利用率数据,下面是它的步骤。

We need to execute 2 WMI Select Queries and apply CPU% utilization formula我们需要执行 2 个 WMI Select Queries 并应用 CPU% 利用率公式

1. To retrieve the total number of logical process 1.检索逻辑进程总数

select NumberOfLogicalProcessors from Win32_ComputerSystem

2. To retrieve the values of PercentProcessorTime, TimeStamp_Sys100NS ( CPU utilization formula has be applied get the actual utilization percentage)and WorkingSetPrivate ( RAM ) minimum of 2 times with a sleep interval of 1 second 2. 要检索 PercentProcessorTime、TimeStamp_Sys100NS(已应用 CPU 利用率公式获得实际利用率百分比)和 WorkingSetPrivate ( RAM ) 的值,最少 2 次,睡眠间隔为 1 秒

select * from Win32_PerfRawData_PerfProc_Process where IDProcess=1234

3. Apply CPU% utilization formula 3. 应用 CPU% 利用率公式

CPU%= ((p2-p1)/(t2-t1)*100)/NumberOfLogicalProcessors

p2 indicated PercentProcessorTime retrieved for the second time, and p1 indicateds the PercentProcessorTime retrieved for the first time, t2 and t1 is for TimeStamp_Sys100NS. p2表示第二次检索到的PercentProcessorTime,p1表示第一次检索到的PercentProcessorTime,t2和t1为TimeStamp_Sys100NS。

A sample Perl code for this can be found in the link http://www.craftedforeveryone.com/cpu-and-ram-utilization-of-an-application-using-perl-via-wmi/可以在链接http://www.craftedforeveryone.com/cpu-and-ram-utilization-of-an-application-using-perl-via-wmi/ 中找到用于此的示例 Perl 代码

This logic applies for all programming language which supports WMI queries此逻辑适用于所有支持 WMI 查询的编程语言

There was a requirement to get status and cpu / memory usage of some specific windows servers.需要获取某些特定 Windows 服务器的状态和 CPU/内存使用情况。 I used below script:我使用了以下脚本:

This is an example of Windows Search Service.这是 Windows 搜索服务的一个示例。

  $cpu = Get-WmiObject win32_processor
  $search = get-service "WSearch"
  if ($search.Status -eq 'Running')
  {
  $searchmem = Get-WmiObject Win32_Service -Filter "Name = 'WSearch'"
  $searchid = $searchmem.ProcessID
  $searchcpu1 = Get-WmiObject Win32_PerfRawData_PerfProc_Process | Where {$_.IDProcess -eq $searchid}
  Start-Sleep -Seconds 1
  $searchcpu2 = Get-WmiObject Win32_PerfRawData_PerfProc_Process | Where {$_.IDProcess -eq $searchid}
  $searchp2p1 = $searchcpu2.PercentProcessorTime - $searchcpu1.PercentProcessorTime
  $searcht2t1 = $searchcpu2.Timestamp_Sys100NS - $searchcpu1.Timestamp_Sys100NS
  $searchcpu = [Math]::Round(($searchp2p1 / $searcht2t1 * 100) /$cpu.NumberOfLogicalProcessors, 1)
  $searchmem = [Math]::Round($searchcpu1.WorkingSetPrivate / 1mb,1)
  Write-Host 'Service is' $search.Status', Memory consumed: '$searchmem' MB, CPU Usage: '$searchcpu' %'
  }

  else
  {
  Write-Host Service is $search.Status -BackgroundColor Red
  }

Hmm, I see that Process Explorer can do it, although its graphs are not too convenient.嗯,我看到Process Explorer可以做到,虽然它的图表不太方便。 Still looking for alternative / better ways to do it.仍在寻找替代/更好的方法来做到这一点。

Perfmon.exe 内置于 Windows 中。

您可能想看看Process Lasso

I use taskinfo for history graph of CPU/RAM/IO speed.我使用 taskinfo 作为 CPU/RAM/IO 速度的历史图表。 http://www.iarsn.com/taskinfo.html http://www.iarsn.com/taskinfo.html

But bursts of unresponsiveness, sounds more like interrupt time due to a falty HD/SS drive.但是突然的无响应听起来更像是由于 HD/SS 驱动器故障造成的中断时间。

Under Windows 10, the Task Manager can show you cumulative CPU hours.在 Windows 10 下,任务管理器可以显示累积的 CPU 小时数。 Just head to the "App history" tab and "Delete usage history".只需前往“应用历史记录”选项卡和“删除使用历史记录”即可。 Now leave things running for an hour or two:现在让事情运行一两个小时:

Windows 10 累积 CPU 时间

What this does NOT do is break down usage in browsers by tab.这不会按选项卡分解浏览器中的使用情况。 Quite often inactive tabs will do a tremendous amount of work, with each open tab using energy and slowing your PC.通常,不活动的标签会做大量的工作,每个打开的标签都会消耗能量并降低你的电脑速度。

How about writing a tool and integrating it with scom so that scom triggers your script when CPU usage exeeds a certain percentage. 如何编写工具并将其与scom集成,以便scom在CPU使用率达到一定百分比时触发您的脚本。 That way, you collate all the data and do an independent analysis 这样,您可以整理所有数据并进行独立分析

Downloadprocess monitor下载进程监视器

  1. Start Process Monitor启动进程监视器

  2. Set a filter if required如果需要,设置过滤器

  3. Enter menu Options > Profiling Events进入菜单选项 > 分析事件

  4. Click "Generate thread prof‌iling events", choose the frequency, and click OK.单击“生成线程分析事件”,选择频率,然后单击“确定”。

  5. To see the collected historical data at any time, enter menu Tools > Process Activity Summary要随时查看收集的历史数据,请进入菜单工具>流程活动摘要

I suspect that one of my applications eats more CPU cycles than I want it to.我怀疑我的一个应用程序消耗了比我想要的更多的CPU周期。 The problem is - it happens in bursts, and just looking at the task manager doesn't help me as it shows immediate usage only.问题是-它突然发生,仅查看任务管理器对我没有帮助,因为它仅显示立即使用情况。

Is there a way (on Windows) to track the history of CPU & Memory usage for some process.是否有办法(在Windows上)跟踪某些进程的CPU和内存使用情况的历史记录。 Eg I will start tracking "firefox", and after an hour or so will see a graph of its CPU & memory usage during that hour.例如,我将开始跟踪“ firefox”,大约一个小时后,将看到该小时内CPU和内存使用情况的图表。

I'm looking for either a ready-made tool or a programmatic way to achieve this.我正在寻找现成的工具或编程方式来实现这一目标。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM