1 module mecca.platform.cpu; 2 3 // Licensed under the Boost license. Full copyright information in the AUTHORS file 4 5 import std..string; 6 import std.conv; 7 8 version(linux): 9 10 struct CPUInfo { 11 string[string] members; 12 13 bool opBinaryRight(string op: "in")(string name) const { 14 return (name in members) !is null; 15 } 16 string opIndex(string name) const { 17 return members[name]; 18 } 19 T getAs(T)(string name) const { 20 return to!T(members[name]); 21 } 22 string toString() const { 23 return to!string(members); 24 } 25 26 @property int coreId() const { 27 return getAs!int("core id"); 28 } 29 @property int physicalId() const { 30 return getAs!int("physical id"); 31 } 32 @property string[] flags() const { 33 return members["flags"].split(); 34 } 35 } 36 37 private __gshared CPUInfo[int] _cpus; 38 39 private void loadCPUInfos() { 40 import std.file: readText; 41 auto lines = readText("/proc/cpuinfo").splitLines(); 42 43 CPUInfo processor; 44 45 while(lines.length > 0) { 46 auto line = lines[0].strip(); 47 lines = lines[1 .. $]; 48 if (line.length == 0) { 49 _cpus[processor.getAs!int("processor")] = processor; 50 processor = CPUInfo.init; 51 continue; 52 } 53 54 auto colon = line.indexOf(":"); 55 auto k = line[0 .. colon].strip(); 56 auto v = line[colon+1 .. $].strip(); 57 processor.members[k] = v; 58 } 59 } 60 61 @property const(CPUInfo[int]) cpuInfos() { 62 if (_cpus.length == 0) { 63 loadCPUInfos(); 64 } 65 return _cpus; 66 } 67 68 unittest { 69 import std.stdio; 70 import std.algorithm; 71 72 assert (cpuInfos.length > 0); 73 assert (cpuInfos[0].flags.canFind("sse")); 74 } 75