File 2410-snmp-test-Recalculate-Linux-bogomips.patch of Package erlang
From b7c920b97330bbc61a257ca3416224bb495fed23 Mon Sep 17 00:00:00 2001
From: Micael Karlberg <bmk@erlang.org>
Date: Thu, 16 Apr 2020 14:39:35 +0200
Subject: [PATCH 10/14] [snmp|test] Recalculate (Linux) bogomips
---
lib/snmp/test/snmp_test_lib.erl | 170 ++++++++++++++++++++++++++++++----------
1 file changed, 130 insertions(+), 40 deletions(-)
diff --git a/lib/snmp/test/snmp_test_lib.erl b/lib/snmp/test/snmp_test_lib.erl
index 8d0f98f9d8..aa3e262413 100644
--- a/lib/snmp/test/snmp_test_lib.erl
+++ b/lib/snmp/test/snmp_test_lib.erl
@@ -911,34 +911,21 @@ analyze_and_print_linux_host_info(Version) ->
{ok, {CPU, BogoMIPS}} ->
io:format("CPU: "
"~n Model: ~s"
- "~n BogoMIPS: ~s"
+ "~n BogoMIPS: ~w"
"~n", [CPU, BogoMIPS]),
- %% We first assume its a float, and if not try integer
- try list_to_float(string:trim(BogoMIPS)) of
- F when F > 5000 ->
+ if
+ (BogoMIPS > 20000) ->
1;
- F when F > 2000 ->
+ (BogoMIPS > 10000) ->
2;
- F when F > 1000 ->
+ (BogoMIPS > 5000) ->
3;
- _ ->
- 5
- catch
- _:_:_ ->
- %%
- try list_to_integer(string:trim(BogoMIPS)) of
- I when I > 5000 ->
- 1;
- I when I > 2000 ->
- 2;
- I when I > 1000 ->
- 3;
- _ ->
- 5
- catch
- _:_:_ ->
- 5
- end
+ (BogoMIPS > 2000) ->
+ 5;
+ (BogoMIPS > 1000) ->
+ 8;
+ true ->
+ 10
end;
{ok, CPU} ->
io:format("CPU: "
@@ -964,20 +951,104 @@ analyze_and_print_linux_host_info(Version) ->
linux_cpuinfo_lookup(Key) when is_list(Key) ->
linux_info_lookup(Key, "/proc/cpuinfo").
+linux_cpuinfo_cpu() ->
+ case linux_cpuinfo_lookup("cpu") of
+ [Model] ->
+ Model;
+ _ ->
+ "-"
+ end.
+
+linux_cpuinfo_motherboard() ->
+ case linux_cpuinfo_lookup("motherboard") of
+ [MB] ->
+ MB;
+ _ ->
+ "-"
+ end.
+
+linux_cpuinfo_bogomips() ->
+ case linux_cpuinfo_lookup("bogomips") of
+ BMips when is_list(BMips) ->
+ try lists:sum([bogomips_to_int(BM) || BM <- BMips])
+ catch
+ _:_:_ ->
+ "-"
+ end;
+ _ ->
+ "-"
+ end.
+
+linux_cpuinfo_total_bogomips() ->
+ case linux_cpuinfo_lookup("total bogomips") of
+ [TMB] ->
+ TMB;
+ _ ->
+ "-"
+ end.
+
+bogomips_to_int(BM) ->
+ try list_to_float(BM) of
+ F ->
+ floor(F)
+ catch
+ _:_:_ ->
+ try list_to_integer(BM) of
+ I ->
+ I
+ catch
+ _:_:_ ->
+ throw(noinfo)
+ end
+ end.
+
+linux_cpuinfo_model() ->
+ case linux_cpuinfo_lookup("model") of
+ [M] ->
+ M;
+ _ ->
+ "-"
+ end.
+
+linux_cpuinfo_platform() ->
+ case linux_cpuinfo_lookup("platform") of
+ [P] ->
+ P;
+ _ ->
+ "-"
+ end.
+
+linux_cpuinfo_model_name() ->
+ case linux_cpuinfo_lookup("model name") of
+ [P|_] ->
+ P;
+ _ ->
+ "-"
+ end.
+
+linux_cpuinfo_processor() ->
+ case linux_cpuinfo_lookup("Processor") of
+ [P] ->
+ P;
+ _ ->
+ "-"
+ end.
+
+
linux_which_cpuinfo(montavista) ->
CPU =
- case linux_cpuinfo_lookup("cpu") of
+ case linux_cpuinfo_cpu() of
"-" ->
throw(noinfo);
Model ->
- case linux_cpuinfo_lookup("motherboard") of
+ case linux_cpuinfo_motherboard() of
"-" ->
Model;
MB ->
Model ++ " (" ++ MB ++ ")"
end
end,
- case linux_cpuinfo_lookup("bogomips") of
+ case linux_cpuinfo_bogomips() of
"-" ->
{ok, CPU};
BMips ->
@@ -986,18 +1057,18 @@ linux_which_cpuinfo(montavista) ->
linux_which_cpuinfo(wind_river) ->
CPU =
- case linux_cpuinfo_lookup("model") of
+ case linux_cpuinfo_model() of
"-" ->
throw(noinfo);
Model ->
- case linux_cpuinfo_lookup("platform") of
+ case linux_cpuinfo_platform() of
"-" ->
Model;
Platform ->
Model ++ " (" ++ Platform ++ ")"
end
end,
- case linux_cpuinfo_lookup("total bogomips") of
+ case linux_cpuinfo_total_bogomips() of
"-" ->
{ok, CPU};
BMips ->
@@ -1007,10 +1078,10 @@ linux_which_cpuinfo(wind_river) ->
linux_which_cpuinfo(other) ->
%% Check for x86 (Intel or AMD)
CPU =
- case linux_cpuinfo_lookup("model name") of
+ case linux_cpuinfo_model_name() of
"-" ->
%% ARM (at least some distros...)
- case linux_cpuinfo_lookup("Processor") of
+ case linux_cpuinfo_processor() of
"-" ->
%% Ok, we give up
throw(noinfo);
@@ -1020,7 +1091,7 @@ linux_which_cpuinfo(other) ->
ModelName ->
ModelName
end,
- case linux_cpuinfo_lookup("bogomips") of
+ case linux_cpuinfo_bogomips() of
"-" ->
{ok, CPU};
BMips ->
@@ -1030,9 +1101,17 @@ linux_which_cpuinfo(other) ->
linux_meminfo_lookup(Key) when is_list(Key) ->
linux_info_lookup(Key, "/proc/meminfo").
+linux_meminfo_memtotal() ->
+ case linux_meminfo_lookup("MemTotal") of
+ [X] ->
+ X;
+ _ ->
+ "-"
+ end.
+
%% We *add* the value this return to the Factor.
linux_which_meminfo() ->
- case linux_meminfo_lookup("MemTotal") of
+ case linux_meminfo_memtotal() of
"-" ->
0;
MemTotal ->
@@ -1709,18 +1788,29 @@ str_num_schedulers() ->
_:_:_ -> "-"
end.
-
+
linux_info_lookup(Key, File) ->
try [string:trim(S) || S <- string:tokens(os:cmd("grep " ++ "\"" ++ Key ++ "\"" ++ " " ++ File), [$:,$\n])] of
- [Key, Value | _] ->
- Value;
- _ ->
- "-"
+ Info ->
+ %% io:format("linux_info_lookup -> "
+ %% "~n Key: ~p"
+ %% "~n File: ~p"
+ %% "~n=> "
+ %% "~n ~p"
+ %% "~n", [Key, File, Info]),
+ linux_info_lookup_collect(Key, Info, [])
catch
_:_:_ ->
"-"
end.
-
+
+linux_info_lookup_collect(_Key, [], Values) ->
+ lists:reverse(Values);
+linux_info_lookup_collect(Key, [Key, Value|Rest], Values) ->
+ linux_info_lookup_collect(Key, Rest, [Value|Values]);
+linux_info_lookup_collect(_, _, Values) ->
+ lists:reverse(Values).
+
%% ----------------------------------------------------------------
--
2.16.4