CoreMod
From Wiki.cyring.fr
(Difference between revisions)
Cyril (Talk | contribs)
(Page créée avec « <syntaxhighlight lang="c"> // coremod.c - CyrIng 2012 #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <sys/stat.h> #include <fcntl.h> // Intel® 64 a... »)
Newer edit →
(Page créée avec « <syntaxhighlight lang="c"> // coremod.c - CyrIng 2012 #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <sys/stat.h> #include <fcntl.h> // Intel® 64 a... »)
Newer edit →
Revision as of 13:43, 25 June 2012
// coremod.c - CyrIng 2012 #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <sys/stat.h> #include <fcntl.h> // Intel® 64 and IA-32 Architectures Software Developer’s Manual // Vol. 3C § 34-1 // Table 34-2. IA-32 Architectural MSRs #define IA32_PERF_STATUS 0x198 // System Management BIOS (SMBIOS) Reference Specification // Version: 2.7.1 § 7.5 // Table 20 – Processor Information (Type 4) Structure #define SMBIOS_PROCINFO_STRUCTURE 4 #define SMBIOS_PROCINFO_INSTANCE 0 #define SMBIOS_PROCINFO_EXTCLK 0x12 int Read_MSR(int cpu, off_t offset, unsigned long long *msr) { ssize_t retval=0; char pathname[32]=""; int fd=0, rc=-1; sprintf(pathname, "/dev/cpu/%d/msr", cpu); if( (fd=open(pathname, O_RDONLY)) != -1 ) { retval=pread(fd, msr, sizeof *msr, offset); close(fd); rc=(retval != sizeof *msr) ? -1 : 0; } return(rc); } int Read_SMBIOS(int structure, int instance, off_t offset, void *buf, size_t nbyte) { ssize_t retval=0; char pathname[]="/sys/firmware/dmi/entries/999-99/raw"; int fd=0, rc=-1; sprintf(pathname, "/sys/firmware/dmi/entries/%d-%d/raw", structure, instance); if( (fd=open(pathname, O_RDONLY)) != -1 ) { retval=pread(fd, buf, nbyte, offset); close(fd); rc=(retval != nbyte) ? -1 : 0; } return(rc); } int Get_Ratio(int target) { unsigned long long msr=0; if( Read_MSR(target, IA32_PERF_STATUS, &msr) != -1) return((int) msr); else return(0); } int External_Clock() { int clock=0; if( Read_SMBIOS(SMBIOS_PROCINFO_STRUCTURE, SMBIOS_PROCINFO_INSTANCE, SMBIOS_PROCINFO_EXTCLK, &clock, 1) != -1) return(clock); else return(0); } int main(int argc, char *argv[]) { int target=0, first=0, last=0, clock=0, ratio=0, rc=0; switch(argc) { case 2: first = atoi(argv[1]); last = first; break; case 3: first = atoi(argv[1]); last = atoi(argv[2]); break; } if((clock=External_Clock())!=0) for(target=first; target<=last && (ratio=Get_Ratio(target)) != 0; target++) printf("Processor#%d : Ratio[%d] x Clock[%d] = %d Mhz\n", target, ratio, clock, ratio*clock); else rc=-1; return(rc); }