Skip to content

Commit

Permalink
Merge pull request #3 from zeam-vm/zacky1972_v0.0.2
Browse files Browse the repository at this point in the history
for Nerves
  • Loading branch information
zacky1972 authored Nov 6, 2019
2 parents e10351b + a4800a2 commit 21a00e5
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 44 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ by adding `cpu_info` to your list of dependencies in `mix.exs`:
```elixir
def deps do
[
{:cpu_info, "~> 0.0.1"}
{:cpu_info, "~> 0.0.2"}
]
end
```
Expand Down
76 changes: 34 additions & 42 deletions lib/cpu_info.ex
Original file line number Diff line number Diff line change
Expand Up @@ -69,67 +69,59 @@ defmodule CpuInfo do
end

defp cpu_type_sub(:linux) do
confirm_executable("cat")
confirm_executable("grep")
confirm_executable("sort")
confirm_executable("wc")
confirm_executable("uname")

kernel_release =
case System.cmd("uname", ["-r"]) do
{result, 0} -> result |> String.trim()
_ -> raise RuntimeError, message: "uname don't work."
_ -> nil
end

system_version =
case System.cmd("cat", ["/etc/issue"]) do
{result, 0} -> result |> String.trim()
_ -> ""
end
system_version = File.read!("/etc/issue") |> String.trim()

kernel_version =
case System.cmd("uname", ["-v"]) do
{result, 0} -> result |> String.trim()
_ -> raise RuntimeError, message: "uname don't work."
_ -> nil
end

cpu_type =
case System.cmd("uname", ["-m"]) do
{result, 0} -> result |> String.trim()
_ -> raise RuntimeError, message: "uname don't work."
end

cpu_models =
:os.cmd('grep model.name /proc/cpuinfo | sort -u')
|> List.to_string()
|> String.split("\n")
|> Enum.map(&String.trim(&1))
|> Enum.reject(&(String.length(&1) == 0))
|> Enum.map(&String.split(&1))
|> Enum.map(&Enum.slice(&1, 3..-1))
|> Enum.map(&Enum.join(&1, " "))
:erlang.system_info(:system_architecture) |> List.to_string() |> String.split("-") |> hd

info =
File.read!("/proc/cpuinfo")
|> String.split("\n\n")
# drop last (emtpy) item
|> Enum.reverse()
|> tl()
|> Enum.reverse()
|> Enum.map(fn cpuinfo ->
String.split(cpuinfo, "\n")
|> Enum.map(fn item ->
[k | v] = String.split(item, ~r"\t+: ")
{k, v}
end)
|> Map.new()
end)

cpu_models = Enum.map(info, &Map.get(&1, "model name")) |> List.flatten()

cpu_model = hd(cpu_models)

num_of_processors =
:os.cmd('grep physical.id /proc/cpuinfo | sort -u | wc -l')
|> List.to_string()
|> String.trim()
|> String.to_integer()

num_of_cores_of_a_processor =
:os.cmd('grep cpu.cores /proc/cpuinfo | sort -u')
|> List.to_string()
|> String.trim()
|> match_to_integer()
Enum.map(info, &Map.get(&1, "physical id"))
|> Enum.uniq()
|> Enum.count()

total_num_of_cores = num_of_cores_of_a_processor * num_of_processors
total_num_of_cores =
Enum.map(info, &Map.get(&1, "cpu cores"))
|> Enum.uniq()
|> Enum.map(&(&1 |> hd |> String.to_integer()))
|> Enum.sum()

num_of_cores_of_a_processor = div(total_num_of_cores, num_of_processors)

total_num_of_threads =
:os.cmd('grep processor /proc/cpuinfo | wc -l')
|> List.to_string()
|> String.trim()
|> String.to_integer()
Enum.map(info, &Map.get(&1, "processor"))
|> Enum.count()

num_of_threads_of_a_processor = div(total_num_of_threads, num_of_processors)

Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ defmodule CpuInfo.MixProject do
def project do
[
app: :cpu_info,
version: "0.0.1",
version: "0.0.2",
elixir: "~> 1.9",
start_permanent: Mix.env() == :prod,
description: description(),
Expand Down

0 comments on commit 21a00e5

Please sign in to comment.