1
0
Fork 0
mirror of https://github.com/anrieff/libcpuid synced 2025-01-23 20:06:41 +00:00

Improve msr_serialize_raw_data function

- Use cached CPUID
- Cache CPU clock
- Make all output fields parsable
This commit is contained in:
Xorg 2022-09-25 10:33:36 +02:00
parent 967f3ab80d
commit 951c42be82
No known key found for this signature in database
GPG key ID: 1E55EE2EFF18BC1A

View file

@ -1026,30 +1026,37 @@ int msr_serialize_raw_data(struct msr_driver_t* handle, const char* filename)
FILE *f;
uint64_t reg;
const uint32_t *msr;
struct cpu_raw_data_t raw;
struct cpu_id_t id;
struct internal_id_info_t internal;
struct cpu_id_t* id;
static int cpu_clock = 0;
/* Check if MSR driver is initialized */
if (handle == NULL)
return set_error(ERR_HANDLE);
if ((filename == NULL) || !strcmp(filename, ""))
f = stdout;
else
f = fopen(filename, "wt");
if (!f) return set_error(ERR_OPEN);
/* Open file descriptor */
f = ((filename == NULL) || !strcmp(filename, "")) ? stdout : fopen(filename, "wt");
if (!f)
return set_error(ERR_OPEN);
/* Get cached decoded CPUID information */
id = get_cached_cpuid();
if (id->vendor == VENDOR_UNKNOWN)
return get_error();
if (cpuid_get_raw_data(&raw) || cpu_ident_internal(&raw, &id, &internal))
return -1;
/* Get CPU stock speed */
if (cpu_clock == 0)
cpu_clock = cpu_clock_measure(250, 1);
fprintf(f, "CPU is %s %s, stock clock is %dMHz.\n", id.vendor_str, id.brand_str, cpu_clock_measure(250, 1));
switch (id.vendor) {
/* Check if CPU vendor is supported */
fprintf(f, "vendor_str=%s\nbrand_str=%s\ncpu_clock_measure=%dMHz\n", id->vendor_str, id->brand_str, cpu_clock);
switch (id->vendor) {
case VENDOR_HYGON:
case VENDOR_AMD: msr = amd_msr; break;
case VENDOR_AMD: msr = amd_msr; break;
case VENDOR_INTEL: msr = intel_msr; break;
default: return set_error(ERR_CPU_UNKN);
}
/* Print raw MSR values */
for (i = 0; msr[i] != CPU_INVALID_VALUE; i++) {
cpu_rdmsr(handle, msr[i], &reg);
fprintf(f, "msr[%#08x]=", msr[i]);
@ -1058,7 +1065,8 @@ int msr_serialize_raw_data(struct msr_driver_t* handle, const char* filename)
fprintf(f, "\n");
}
if ((filename != NULL) && strcmp(filename, ""))
/* Close file descriptor */
if (f != stdout)
fclose(f);
return set_error(ERR_OK);
}