From cd354e11fa134f18ee5f32bc75185eaf0d2c5128 Mon Sep 17 00:00:00 2001 From: Susumu Yamazaki Date: Wed, 6 Nov 2019 09:30:11 +0900 Subject: [PATCH 1/4] for Nerves --- lib/cpu_info.ex | 73 ++++++++++++++++++++----------------------------- 1 file changed, 29 insertions(+), 44 deletions(-) diff --git a/lib/cpu_info.ex b/lib/cpu_info.ex index 656aae7..9a5dd46 100644 --- a/lib/cpu_info.ex +++ b/lib/cpu_info.ex @@ -69,67 +69,52 @@ 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_type = :erlang.system_info(:system_architecture) |> List.to_string |> String.split("-") |> hd + + info = File.read!("/proc/cpuinfo") + |> String.split("\n\n") + |> Enum.reverse() |> tl() |> Enum.reverse() # drop last (emtpy) item + |> 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 = - :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, " ")) + cpu_models = Enum.map(info, & Map.get(&1, "model name")) 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() + num_of_processors = 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_threads = - :os.cmd('grep processor /proc/cpuinfo | wc -l') - |> List.to_string() - |> String.trim() - |> String.to_integer() + 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 = Enum.map(info, &Map.get(&1, "processor")) + |> Enum.count num_of_threads_of_a_processor = div(total_num_of_threads, num_of_processors) From 6fe45834185861c74ed1c9fbbc3d54be5641ab13 Mon Sep 17 00:00:00 2001 From: Susumu Yamazaki Date: Wed, 6 Nov 2019 09:32:30 +0900 Subject: [PATCH 2/4] apply List.flatten --- lib/cpu_info.ex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/cpu_info.ex b/lib/cpu_info.ex index 9a5dd46..97eab44 100644 --- a/lib/cpu_info.ex +++ b/lib/cpu_info.ex @@ -97,7 +97,7 @@ defmodule CpuInfo do |> Map.new() end) - cpu_models = Enum.map(info, & Map.get(&1, "model name")) + cpu_models = Enum.map(info, & Map.get(&1, "model name")) |> List.flatten cpu_model = hd(cpu_models) From 976ba54a1176d01e1cf5031efb98b8dd0bf16912 Mon Sep 17 00:00:00 2001 From: Susumu Yamazaki Date: Wed, 6 Nov 2019 09:32:42 +0900 Subject: [PATCH 3/4] apply mix format --- lib/cpu_info.ex | 59 +++++++++++++++++++++++++++---------------------- 1 file changed, 33 insertions(+), 26 deletions(-) diff --git a/lib/cpu_info.ex b/lib/cpu_info.ex index 97eab44..a3acc87 100644 --- a/lib/cpu_info.ex +++ b/lib/cpu_info.ex @@ -75,7 +75,7 @@ defmodule CpuInfo do _ -> nil end - system_version = File.read!("/etc/issue") |> String.trim + system_version = File.read!("/etc/issue") |> String.trim() kernel_version = case System.cmd("uname", ["-v"]) do @@ -83,38 +83,45 @@ defmodule CpuInfo do _ -> nil end - cpu_type = :erlang.system_info(:system_architecture) |> List.to_string |> String.split("-") |> hd - - info = File.read!("/proc/cpuinfo") - |> String.split("\n\n") - |> Enum.reverse() |> tl() |> Enum.reverse() # drop last (emtpy) item - |> 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_type = + :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 = Enum.map(info, &Map.get(&1, "physical id")) - |> Enum.uniq - |> Enum.count - + num_of_processors = + Enum.map(info, &Map.get(&1, "physical id")) + |> Enum.uniq() + |> Enum.count() - total_num_of_cores = Enum.map(info, &Map.get(&1, "cpu cores")) - |> Enum.uniq - |> Enum.map(& &1 |> hd |> String.to_integer) - |> Enum.sum + 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 = Enum.map(info, &Map.get(&1, "processor")) - |> Enum.count + total_num_of_threads = + Enum.map(info, &Map.get(&1, "processor")) + |> Enum.count() num_of_threads_of_a_processor = div(total_num_of_threads, num_of_processors) From a4800a2d2456101c6ffb8edab4117ef23075961f Mon Sep 17 00:00:00 2001 From: Susumu Yamazaki Date: Wed, 6 Nov 2019 09:34:46 +0900 Subject: [PATCH 4/4] release v0.0.2 --- README.md | 2 +- mix.exs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index a8a0ffc..878c641 100644 --- a/README.md +++ b/README.md @@ -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 ``` diff --git a/mix.exs b/mix.exs index 943c213..2d78898 100644 --- a/mix.exs +++ b/mix.exs @@ -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(),