# Get CPU usage

**URL:** <https://discourse.julialang.org/t/get-cpu-usage/24468>\
**Category:** General Usage\
**Tags:** question\
**Created:** [May 22, 2019, 10:35am UTC](https://discourse.julialang.org/t/get-cpu-usage/24468 "2019-05-22T10:35:14Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Caesar\_KG](https://avatars.discourse-cdn.com/v4/letter/c/7cd45c/32.png) [@Caesar\_KG](https://discourse.julialang.org/u/Caesar_KG)\
**Post date:** [May 22, 2019, 10:35am UTC](https://discourse.julialang.org/t/get-cpu-usage/24468/1 "2019-05-22T10:35:14Z")

</div>

Is there any built-in function that I can use to get the CPU usage of PCs? I’m writing a load balancer that orchestrates multiple PCs to perform a computation task based on CPU and memory usage. I know other languages like Python can do this but I’m not sure if Julia can?

---

<div class="post-metadata">

**Author:** ![davidbp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/davidbp/32/463_2.png) [@davidbp](https://discourse.julialang.org/u/davidbp)\
**Post date:** [May 22, 2019, 11:56am UTC](https://discourse.julialang.org/t/get-cpu-usage/24468/2 "2019-05-22T11:56:14Z")

</div>

I don’t know about a julia library to do it.

I’ve done it with the `psutil` python library.

You can use the Conda package (or PyCall I guess) to do it. Example

```julia
julia> using Conda
julia> Conda.add("psutil")

julia> aux = py"""import psutil
       def get_cpu():
           return psutil.cpu_percent()
       """

julia> py"get_cpu()"
11.3
julia> x = py"get_cpu()"
8.1
julia> typeof(x)
Float64

```

---

<div class="post-metadata">

**Author:** ![Caesar\_KG](https://avatars.discourse-cdn.com/v4/letter/c/7cd45c/32.png) [@Caesar\_KG](https://discourse.julialang.org/u/Caesar_KG)\
**Post date:** [May 23, 2019, 6:25am UTC](https://discourse.julialang.org/t/get-cpu-usage/24468/3 "2019-05-23T06:25:14Z")

</div>

Thank you David! It helps!

Hope someone can give a Julia way of this!

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [May 23, 2019, 7:52am UTC](https://discourse.julialang.org/t/get-cpu-usage/24468/4 "2019-05-23T07:52:59Z")

</div>

If you are using Linux, you can query and parse `/proc/stat`, eg

```julia
function get_cpu_stats()
    st = read("/proc/stat", String)
    m = match(r"^cpu *(.*)", st)
    parse.(Int, split(m.captures[1], ' '))
end

```

Then take the difference between two calls with a bit of time between them, and use the relevant fields to calculate the stats you want to focus on (this depends on your architecture and application, eg whether you consider IO wait times relevant, etc). See [proc(5)](http://man7.org/linux/man-pages/man5/proc.5.html) for the format.

---

<div class="post-metadata">

**Author:** ![johnh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/johnh/32/3615_2.png) [@johnh](https://discourse.julialang.org/u/johnh)\
**Post date:** [May 23, 2019, 9:55am UTC](https://discourse.julialang.org/t/get-cpu-usage/24468/5 "2019-05-23T09:55:10Z")

</div>

I note that on the psutil GitHub page there are links to ports in Go, Rust etc.  
Surely we cannot let these languages have something we do not…  
To the barricades mes amis!

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [May 23, 2019, 10:18am UTC](https://discourse.julialang.org/t/get-cpu-usage/24468/6 "2019-05-23T10:18:09Z")

</div>

> [@johnh](#):
>
> Surely we cannot let these languages have something we do not…  
> To the barricades mes amis!

I appreciate your enthusiasm, but replicating psutil is a _lot_ of tedious work, that requires ongoing fixes and updates, and access to a diverse set of OS environments (both for development and CI).

I am totally fine with this effort being centralized, and just calling eg the original python library via a foreign function interface. External dependencies are not ideal, but in this case they may be the easiest solution. These kind of queries are rarely performance-critical anyway.

---

<div class="post-metadata">

**Author:** ![fonsp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fonsp/32/222349_2.png) [@fonsp](https://discourse.julialang.org/u/fonsp)\
**Post date:** [November 23, 2022, 7:11pm UTC](https://discourse.julialang.org/t/get-cpu-usage/24468/7 "2022-11-23T19:11:08Z")

</div>

The psutil code was not too hard to port to Julia:

```julia
idle_time(info::Sys.CPUinfo) = Int64(info.cpu_times!idle)

busy_time(info::Sys.CPUinfo) = 
	Int64(
		info.cpu_times!user +
		info.cpu_times!nice +
		info.cpu_times!sys +
		info.cpu_times!irq
	)

period = 3 # seconds

usages = let
	
	info = Sys.cpu_info()
	busies = busy_time.(info)
	idles = idle_time.(info)

	sleep(period)

	info = Sys.cpu_info()
	busies = busy_time.(info) .- busies
	idles = idle_time.(info) .- idles

	busies ./ (idles .+ busies)
end

# This returns a Vector{Float64} with the CPU usage between 0.0 and 1.0

```

---

<div class="post-metadata">

**Author:** ![terasakisatoshi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/terasakisatoshi/32/9187_2.png) [@terasakisatoshi](https://discourse.julialang.org/u/terasakisatoshi)\
**Post date:** [December 3, 2022, 1:06pm UTC](https://discourse.julialang.org/t/get-cpu-usage/24468/8 "2022-12-03T13:06:04Z")

</div>

Your advice was very helpful.  
I could implement an easy application using MiniFB.jl and Plots.jl

 ![image](https://global.discourse-cdn.com/julialang/original/3X/8/9/89fe76fd031587723c2b9342853ccd15ef37bbbd.png)

> <https://gist.github.com/terasakisatoshi/80fcddfe37eeb8263e465a814c2cc624>
