R:如何检查可用的核心/CPU 使用率

问题描述 投票:0回答:7

R 是单线程的。

  1. 使用R,如何检查Windows和Linux中有多少个核心/线程正在运行R? (或者正在运行多少卢比)
  2. 使用R,如何查看Windows和Linux中运行R的每个核心的使用情况? (或者每个 R 使用的 CPU 百分比)

例如,如果我有两个 R 打开正在运行的项目。我期望有 2 个线程运行 R,每个线程占用一定百分比的 CPU。然后我打开另一个 R。如何使用第三个 R 检查线程数(本例中为 2)以及 R 使用的 CPU 百分比?

r cpu cpu-usage
7个回答
36
投票

如果您打开多个 R 窗口,每个窗口将在不同的内核上运行,最多可达您拥有的最大内核数。这在 Windows 和 Mac 计算机上自动实现。如果你想知道你有多少个核心,你可以运行:

library(parallel)
detectCores()

18
投票

在 Linux 上,您可以向系统发送 ps 命令:它会为您提供名为 rsession 的程序的平均 cpu 使用率和内存使用率:

splitted <- strsplit(system("ps -C rsession -o %cpu,%mem,pid,cmd", intern = TRUE), " ")
df <- do.call(rbind, lapply(splitted[-1], 
                            function(x) data.frame(
                                cpu = as.numeric(x[2]),
                                mem = as.numeric(x[4]),
                                pid = as.numeric(x[5]),
                                cmd = paste(x[-c(1:5)], collapse = " "))))
df
#  cpu mem   pid   cmd
#1 0.8 0.7 11001  /usr/lib/rstudio/bin/rsession 
#2 0.0 0.2 12397  /usr/lib/rstudio/bin/rsession
#3 0.1 0.7 14960  /usr/lib/rstudio/bin/rsession
#4 0.4 0.2 26122  /usr/lib/rstudio-server/bin/rsession 
#5 0.3 8.3 35782  /usr/lib/rstudio/bin/rsession

您可以改进它,以获取父 ID 和瞬时 CPU 使用率,并将其他选项传递给 ps 或 top,并推断每个会话使用的核心数量。

在 Windows 上您可以尝试以下操作:

a <- system("wmic path Win32_PerfFormattedData_PerfProc_Process get Name,PercentProcessorTime", intern = TRUE)
df <- do.call(rbind, lapply(strsplit(a, " "), function(x) {x <- x[x != ""];data.frame(process = x[1], cpu = x[2])}))
df[grepl("Rgui|rstudio", df$process),]
#     process cpu
# 105    Rgui   0
# 108 rstudio   0

4
投票

使用R,如何检查Windows中有多少个核心/线程正在运行R 和Linux? (或者正在运行多少卢比)

我在这里还没有读到的一个有效答案是简单地使用 ps R 包和函数

ps()
然后你可以对名为“rsession”的进程返回的表进行子集化:

ps::ps()[ps::ps()$name == "rsession",]

行数将为您提供计算机/服务器上现有会话的数量:

nrow(ps::ps()[ps::ps()$name == "rsession",])

我不完全确定

function ps_num_threads()
的作用,但检查结果是否有意义也可能很有趣:

ps::ps_num_threads(ps::ps_handle())

不幸的是,我在psR包中没有找到任何关于%CPU使用率的信息,但你可以尝试一下我在其他答案中引用的函数,它应该在Linux下工作。


3
投票

对于那些想了解 1) 机器中可用的核心/CPU 数量和/或工作线程/计算节点数量或 2) 由运行当前 R 程序的 HPC 集群分配的数量的人,请尝试以下操作(使用并行和future包中的函数):

library(parallel) # for using parallel::mclapply() and checking #totalCores on compute nodes / workstation: detectCores()
library(future) # for checking #availble cores / workers on compute nodes / workstation: availableWorkers() / availableCores() 

workers <- availableWorkers()
cat(sprintf("#workders/#availableCores/#totalCores: %d/%d/%d, workers:\n", length(workers), availableCores(), detectCores()))
print( workers )

1
投票

有一个更简单的方法,使用 benchmarkme 包。

library(benchmarkme)
get_cpu()$no_of_cores

0
投票

对于那些感兴趣的人,我和一个朋友创建了一个 Github 存储库,其功能可以从 R 控制台探测计算机/服务器上正在进行的进程。
这是链接:https://github.com/mathosi/cluster_check

要回答这个问题,你可以使用我制作的功能

ps.to.df()

devtools::source_url("https://github.com/mathosi/cluster_check/blob/master/ps_to_df.R?raw=TRUE")
ps.to.df() #List All processes sorted by %CPU usage
ps.to.df(bylist.selection = "C rsession") #List All processes named 'rsession' sorted by %CPU usage

输出是一个 data.frame,因此您可以按照您希望在 R 中查找的方式对其子集进行排序,以查找您想查看的任何内容!
我还没有尝试所有可能的查询

ps.to.df()
支持,但我想它应该支持其他查询。
输出的灵活性和可读性还有提高的空间,也许还可以创建附加功能。任何有兴趣的人都可以加入并贡献。


0
投票

也许我找到了一种在 Windows 系统上使用 powershell 时获取核心工作负载百分比的方法:

library(tidyverse)

command = 'Get-WmiObject -Query \'select Name, PercentProcessorTime from Win32_PerfFormattedData_PerfOS_Processor\' | foreach-object { write-host "$($_.Name): $($_.PercentProcessorTime)" }; '
output = system2('powershell', c('-Command', command), stdout = T)

output_lst = strsplit(output, split = " : ") 
cores = data.frame(t(sapply(output_lst,c))) %>% 
  rename( "Core" = X1 , `CPU_%` = X2)

为您提供核心列表及其工作负载:

> cores
    Core CPU_%
1       0    62
2       1    19
3      10     0
4      11    12
5      12     6
6      13     0
7      14    12
8      15     0
9      16    56
10     17    31
11     18    12
12     19    12
13      2    50
14     20     0
15     21     6
16     22    12
17     23     6
18     24    12
19     25    19
20     26    19
21     27    43
22     28    31
23     29    12
24      3    12
25     30     6
26     31    25
27      4    43
28      5    37
29      6     0
30      7     6
31      8     0
32      9     0
33 _Total    18

过滤卸载的核心:

free_cores = cores %>% filter(`CPU_%` == 0) %>% nrow()
> free_cores
[1] 7
© www.soinside.com 2019 - 2024. All rights reserved.