20

R is single-threaded.

  1. Using R, how to check how many cores/threads are running R in Windows and Linux? (Or how many Rs are running)
  2. Using R, how to check the usage of each core that is running R in Windows and Linux? (Or the percentage of CPU each R is using)

For example, if I have two R opened running projects. I would expect that there are 2 threads running R with some % of CPU for each thread. Then I open another R. How to use the third R to check the number of threads (2 in this case) and percentage of CPU being used by R?

WCMC
  • 1,602
  • 3
  • 20
  • 32
  • 2
    Don't think you'll find much (though I'm frequently surprised). Your best bet is to use task manager (windows) or something like `top` or `htop` (linux) to see what each core is doing, remembering that R is single-threaded by default. There are math libraries that R might use that take advantage of multiple threads, but R doesn't report on that, only the OS does. – r2evans Nov 15 '17 at 22:34
  • If the system can do it, then I guess I can use `system()` in R to do so as well? – WCMC Nov 15 '17 at 22:35
  • Sure, depending on your OS and tools available. – r2evans Nov 15 '17 at 22:36

6 Answers6

36

If you open multiple R windows, each window will be running on a different core up to the maximum number of cores that you have. This is automatically implemented on windows and mac computers. If you want to know how many cores you have, you can run:

library(parallel)
detectCores()
mikey
  • 1,066
  • 9
  • 17
  • 2
    Thanks but this is not what I am asking. I’d like to know how to use R to check how many Rs or CPU usage of each R I am running. – WCMC Nov 15 '17 at 22:38
18

On Linux you can send ps command to the system: it gives you the average cpu usage and the memory usage of the program called rsession:

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

You can probably improve it to get the parent id and the instantaneous CPU usage with other options passed to ps or top and deduce the number of cores used by each session.

On Windows you can try this:

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
BenoitLondon
  • 845
  • 6
  • 19
4

Using R, how to check how many cores/threads are running R in Windows and Linux? (Or how many Rs are running)

One valid answer that I haven't read yet here is simply using the ps R package with the function ps() you can then subset the table returned by processes with the name "rsession":

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

Number of rows will give you the number of sessions existing on the computer/server:

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

I am not entirely sure about what the function ps_num_threads() does but it might also be interesting to check if the result make sense:

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

I unfortunately did not found anything about %CPU usage in the ps R package but you can give a try to the function I quote in my other answer, it should work under Linux.

Yoann Pageaud
  • 412
  • 5
  • 22
3

For those who would like to know the number of cores/cpus and/or number of workers/compute nodes that 1) are available in the machine or 2) are allocated by HPC clusters on which current R programm is running, try this (using functions from parallel and future packages):

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 )
Good Will
  • 1,220
  • 16
  • 10
1

There is a simpler method, using benchmarkme package.

library(benchmarkme)
get_cpu()$no_of_cores
0

For those interested, a friend and I have created a Github Repository with functions to probe ongoing processes on a computer/server from a R console.
Here is the link: https://github.com/mathosi/cluster_check

To answer the question you can use the function I made 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

The output is a data.frame so you can then sort it subset it the way you want in R to look for whatever you want to see!
I haven't try yet all the possible query ps.to.df() supports, but I guess it should support others.
There is room to improve flexibility and readability of the outputs, maybe to create additionnal functions also. Anyone interested can join and contribute.

Community
  • 1
  • 1
Yoann Pageaud
  • 412
  • 5
  • 22