Problem statement: Get information about the computer’s CPU on a *nix (both Linux && Unix) machine.
Use case scenario:
- Compile the module
- Use it:
[["processor","0"],
["vendor_id","GenuineIntel"],
["cpu family","6"],
["model","23"],
["model name","Intel(R) Core(TM)2 Duo CPU T8100@ 2.10GHz"],
["stepping","6"],
["cpu MHz","800.000"],
["cache size","3072 KB"],
["physical id","0"],
["siblings","2"],
["core id","0"],
["cpu cores","2"],
["apicid","0"],
["initial apicid","0"],
["fpu","yes"],
["fpu_exception","yes"],
["cpuid level","10"],
["wp","yes"],
["flags",
"fpu vme de pse tsc msr pae mce cx8 apic mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx lm constant_tsc arch_perfmon pebs bts rep_good pni dtes64 monitor ds_cpl vmx est tm2 ssse3 cx16 xtpr pdcm sse4_1 lahf_lm ida tpr_shadow vnmi flexpriority"],
["bogomips","4189.71"],
["clflush size","64"],
["cache_alignment","64"],
["address sizes","36 bits physical, 48 bits virtual"],
["power management","\n"],
["\n"],
["processor","1"],
["vendor_id",[...]],
[[...]|…],
[...]|…]
- Source code:
-
-module(cpuinfo).
-
-export([details/0,removeUnwantedCharsFromList/2,frontSpace/2]).
-
-
frontSpace([H,HH|T],R) ->
-
if
-
(H=:=32 andalso HH=:=32) -> frontSpace(T,R); %space
-
(H=:=32 andalso HH=/=32) -> frontSpace([HH|T],[H]++R); %space
-
H=:=9 -> frontSpace([HH|T],R); %\t
-
H=:=10 -> frontSpace([HH|T],R); %\n
-
true -> frontSpace([HH|T],[H]++R)
-
end;
-
frontSpace(H,R) ->
-
if
-
H=:=[] -> lists:reverse(R);
-
(H=/=9 andalso H=/=10)-> lists:reverse(H++R)
-
end.
-
removeChar([H|Word])->
-
if
-
H=:=32 -> frontSpace(Word,[]);
-
true -> frontSpace([H|Word],[])
-
end.
-
-
removeUnwantedCharsFromList([Word|Words],Result)->
-
L1 = removeChar(Word),
-
L2 = removeChar(lists:reverse(L1)),
-
R = lists:reverse(L2),
-
removeUnwantedCharsFromList(Words, [R] ++ Result);
-
removeUnwantedCharsFromList([],Result)->lists:reverse(Result).
-
extractDataFromLine(Line) ->
-
{ok,Values} = regexp:split(Line,"[:]+")
-
%,L1 = frontSpace(Values,[])
-
,[removeUnwantedCharsFromList(Values,[])].
-
extractData(File,List)->
-
Line = io:get_line(File,''),
-
if
-
Line == eof -> lists:reverse(tl(List));
-
true -> extractData(File,
-
extractDataFromLine(Line) ++ List)
-
end.
-
details() ->
-
{ok, File} = file:open(paths:cpuinfo(),read),
-
extractData(File,[]).