2

How can I get the current percent CPU usage in R? Ideally, it would work for both Unix and Windows platforms.

In Windows platform, I used following code:

a <- system("wmic cpu get loadpercentage", intern = TRUE)
as.numeric(gsub("\\D", "", a[2]))

Is there a better way(or a function in a package) to get the current CPU usage, such that works with both Unix and Windows platforms?

According to how to get current cpu and ram usage in python? and the "reticulate" package:

library(reticulate)
aa<-reticulate::import("psutil")
aa$cpu_percent()

The function return the current percent usage of CPU as shown in below(6% currently used)

enter image description here

But this way needs Python to be installed on the platform.

The question Is there an R function to retrieve CPU and RAM information? ask for hardware information (not current percent usage CPU)as follows(This is not even close to My question!!!):

> system("lscpu | grep 'Model name:'")
Model name:          Intel(R) Core(TM) i7-8700 CPU @ 3.20GHz
> system("lsmem | grep 'Total online memory'")
Total online memory:      16G

> library(benchmarkme)
> get_cpu() 
$vendor_id
[1] "GenuineIntel"
$model_name
[1] "Intel(R) Core(TM) i5-7400 CPU @ 3.00GHz"
$no_of_cores
[1] 4
> get_ram()
34.3 GB

So, the answer to the question, two function get_ram() ,get_cpu() , return total available RAM and CPU! not current percent usage of RAM and CPU. That is, get_ram() function return 32GB, not 6 percent that used now!

I think, accepted answer in question R: how to check how many cores/CPU usage available, does not calculated the current percent of CPU:

Windows platform(the accepted answer R: how to check how many cores/CPU usage available):

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

And the data.frame 'df' is:

enter image description here

I can not find any way to calculate current percent of CPU usage based on the answer. Perhaps I misunderstood something, So based that answer, R: how to check how many cores/CPU usage available, ,give me the current percent of CPU usage on comment.

I tried to extract the current percent CPU usage base on R: how to check how many cores/CPU usage available, When I look at the result of

 df <- do.call(rbind, lapply(strsplit(a, " "), function(x) {x <- x[x != ""];data.frame(process = x[1], cpu = x[2])}))

I find two rows, Idle and _Total as follows:

df1<-df %>% filter(process %in% c("Idle","_Total"))
df1

enter image description here

So 1-Idle/_Total should be the percent current CPU usage. I calculate this as follows:

for(i in 1:1000){
  a <- system("wmic path Win32_PerfFormattedData_PerfProc_Process get Name,PercentProcessorTime", intern = TRUE)
  df1 <- do.call(rbind, lapply(strsplit(a, " "), function(x) {x <- x[x != ""];data.frame(process = x[1], cpu = x[2])}))
  df1<-df1 %>% filter(process %in% c("Idle","_Total"))
  df1<-df1 %>% mutate(cpu=as.numeric(df1$cpu))
  Idle<-df1 %>% filter(process=="Idle")
  Total<-df1 %>% filter(process=="_Total")
  message(1-Idle$cpu/Total$cpu)
}

and the result is:

enter image description here

that make no sense!!

When I look to the Python code, and an answer should be like it, it easily calculate the current CPU usage:

First with PowerShell install the psutil module as follows:

pip install psutil

and then use it in R as follows:

> library(reticulate)
> aa<-reticulate::import("psutil")
> aa$cpu_percent()
[1] 9.2
Masoud
  • 535
  • 3
  • 19
  • Does this answer your question? https://stackoverflow.com/questions/47318401/r-how-to-check-how-many-cores-cpu-usage-available – medium-dimensional Nov 15 '22 at 07:10
  • Does this answer your question? [Is there an R function to retrieve CPU and RAM information?](https://stackoverflow.com/questions/60775631/is-there-an-r-function-to-retrieve-cpu-and-ram-information) – GordonShumway Nov 15 '22 at 09:14
  • @GordonShumway, https://stackoverflow.com/questions/60775631/is-there-an-r-function-to-retrieve-cpu-and-ram-information ask for hardware information (not current percent usage CPU)!!!!! – Masoud Nov 19 '22 at 13:42
  • The accepted answer to the question linked by GordonShumway appears to answer your request. – IRTFM Nov 21 '22 at 22:06
  • @IRTFM, The answer to the question, by GordonShumway, two function get_ram() ,get_cpu() , return total available RAM and CPU! not current percent usage of RAM and CPU. That is, get_ram() function return 32GB, not 6 percent that used now! There is no accepted answer to the question linked by GordonShumway . There is an answer!!! Not accepted! – Masoud Nov 22 '22 at 05:19
  • Sorry. It was https://stackoverflow.com/questions/47318401/r-how-to-check-how-many-cores-cpu-usage-available – IRTFM Nov 22 '22 at 05:48

0 Answers0