Skip to content

Commit

Permalink
Improve CPU brand detection
Browse files Browse the repository at this point in the history
  • Loading branch information
jart committed Jun 5, 2024
1 parent 3b7b1e3 commit e973fa2
Showing 1 changed file with 30 additions and 6 deletions.
36 changes: 30 additions & 6 deletions llama.cpp/llama-bench/llama-bench.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,16 +97,40 @@ static std::string replaceAll(std::string str, const std::string& from, const st
return str;
}


#ifdef __x86_64__
static void cpuid(unsigned leaf, unsigned subleaf, unsigned *info) {
asm("movq\t%%rbx,%%rsi\n\t"
"cpuid\n\t"
"xchgq\t%%rbx,%%rsi"
: "=a"(info[0]), "=S"(info[1]), "=c"(info[2]), "=d"(info[3])
: "0"(leaf), "2"(subleaf));
}
#endif // __x86_64__

static std::string get_cpu_info() { // [jart]
std::string id;

#ifdef __x86_64__
union { // [jart]
char str[64];
unsigned reg[16];
} u = {0};
cpuid(0x80000002, 0, u.reg + 0*4);
cpuid(0x80000003, 0, u.reg + 1*4);
cpuid(0x80000004, 0, u.reg + 2*4);
int len = strlen(u.str);
while (len > 0 && u.str[len - 1] == ' ')
u.str[--len] = 0;
id = u.str;
#else
if (IsLinux()) {
FILE * f = fopen("/proc/cpuinfo", "r");
if (f) {
char buf[1024];
while (fgets(buf, sizeof(buf), f)) {
if (strncmp(buf, "model name", 10) == 0 ||
startswith(buf, "Model :")) {
if (!strncmp(buf, "model name", 10) ||
startswith(buf, "Model\t\t:")) { // e.g. raspi
char * p = strchr(buf, ':');
if (p) {
p++;
Expand All @@ -117,24 +141,24 @@ static std::string get_cpu_info() { // [jart]
p[strlen(p) - 1] = '\0';
}
id = p;
id = replaceAll(id, " 96-Cores", "");
id = replaceAll(id, "(TM)", "");
id = replaceAll(id, "(R)", "");
break;
}
}
}
fclose(f);
}
}

if (IsXnu()) {
char cpu_name[128] = {0};
size_t size = sizeof(cpu_name);
if (sysctlbyname("machdep.cpu.brand_string", cpu_name, &size, NULL, 0) != -1) {
id = cpu_name;
}
}
#endif
id = replaceAll(id, " 96-Cores", "");
id = replaceAll(id, "(TM)", "");
id = replaceAll(id, "(R)", "");

std::string march;
#ifdef __x86_64__
Expand Down

0 comments on commit e973fa2

Please sign in to comment.