| 1 |
/* This file is modified from source available at http://www.kernel.org/pub/linux/utils/cpu/msr-tools/ |
| 2 |
for Model specific cpu registers |
| 3 |
Modified to take i7 into account by Abhishek Jaiantilal [email protected] |
| 4 |
|
| 5 |
// Information about i7's MSR in |
| 6 |
// http://download.intel.com/design/processor/applnots/320354.pdf |
| 7 |
// Appendix B of http://www.intel.com/Assets/PDF/manual/253669.pdf |
| 8 |
|
| 9 |
|
| 10 |
#ident "$Id: rdmsr.c,v 1.4 2004/07/20 15:54:59 hpa Exp $" |
| 11 |
----------------------------------------------------------------------- * |
| 12 |
* |
| 13 |
* Copyright 2000 Transmeta Corporation - All Rights Reserved |
| 14 |
* |
| 15 |
* This program is free software; you can redistribute it and/or modify |
| 16 |
* it under the terms of the GNU General Public License as published by |
| 17 |
* the Free Software Foundation, Inc., 675 Mass Ave, Cambridge MA 02139, |
| 18 |
* USA; either version 2 of the License, or (at your option) any later |
| 19 |
* version; incorporated herein by reference. |
| 20 |
* |
| 21 |
* ----------------------------------------------------------------------- */ |
| 22 |
#include <memory.h> |
| 23 |
#include <errno.h> |
| 24 |
#include <stdio.h> |
| 25 |
#include <fcntl.h> |
| 26 |
#include <unistd.h> |
| 27 |
#include <stdlib.h> |
| 28 |
#include <getopt.h> |
| 29 |
#include <inttypes.h> |
| 30 |
#include <sys/types.h> |
| 31 |
#include <sys/time.h> |
| 32 |
#include <time.h> |
| 33 |
#include <assert.h> |
| 34 |
#include "perfmon-i7z.h" |
| 35 |
|
| 36 |
#define UINT32_MAX (0xffffffff) |
| 37 |
|
| 38 |
//#define ULLONG_MAX 18446744073709551615 |
| 39 |
|
| 40 |
/////////////////////////////////////////READ TEMPERATURE//////////////////////////////////////////// |
| 41 |
#define IA32_THERM_STATUS 0x19C |
| 42 |
#define IA32_TEMPERATURE_TARGET 0x1a2 |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
/*int Get_Bits_Value(unsigned long val,int highbit, int lowbit){ |
| 47 |
unsigned long data = val; |
| 48 |
int bits = highbit - lowbit + 1; |
| 49 |
if(bits<64){ |
| 50 |
data >>= lowbit; |
| 51 |
data &= (1ULL<<bits) - 1; |
| 52 |
} |
| 53 |
return(data); |
| 54 |
} |
| 55 |
|
| 56 |
|
| 57 |
int Read_Thermal_Status_CPU(int cpu_num){ |
| 58 |
int error_indx; |
| 59 |
unsigned long val= get_msr_value(cpu_num,IA32_THERM_STATUS,63,0,&error_indx); |
| 60 |
int digital_readout = Get_Bits_Value(val,23,16); |
| 61 |
|
| 62 |
val= get_msr_value(cpu_num,IA32_TEMPERATURE_TARGET,63,0,&error_indx); |
| 63 |
int PROCHOT_temp = Get_Bits_Value(val,24,16); |
| 64 |
|
| 65 |
//temperature is prochot - digital readout |
| 66 |
return(PROCHOT_temp - digital_readout); |
| 67 |
} */ |
| 68 |
|
| 69 |
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 70 |
int num_msr() |
| 71 |
{ |
| 72 |
unsigned int b; |
| 73 |
asm volatile ("mov %1, %%eax;" |
| 74 |
"cpuid;" |
| 75 |
"mov %%eax, %0;" |
| 76 |
:"=r" (b) |
| 77 |
:"r" (0x0a) |
| 78 |
:"%eax" |
| 79 |
); |
| 80 |
return( (b & 0xFF00) >> 8 ); |
| 81 |
} |
| 82 |
|
| 83 |
int bit_width_PMCx() |
| 84 |
{ |
| 85 |
unsigned int b; |
| 86 |
asm volatile ("mov %1, %%eax;" |
| 87 |
"cpuid;" |
| 88 |
"mov %%eax, %0;" |
| 89 |
:"=r" (b) |
| 90 |
:"r" (0x0a) |
| 91 |
:"%eax" |
| 92 |
); |
| 93 |
return( (b & 0xFF0000) >> 16 ); |
| 94 |
} |
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
void |
| 101 |
print_family_info (struct family_info *proc_info) |
| 102 |
{ |
| 103 |
//print CPU info |
| 104 |
printf ("i7z DEBUG: Stepping %x\n", proc_info->stepping); |
| 105 |
printf ("i7z DEBUG: Model %x\n", proc_info->model); |
| 106 |
printf ("i7z DEBUG: Family %x\n", proc_info->family); |
| 107 |
printf ("i7z DEBUG: Processor Type %x\n", proc_info->processor_type); |
| 108 |
printf ("i7z DEBUG: Extended Model %x\n", proc_info->extended_model); |
| 109 |
// printf(" Extended Family %x\n", (short int*)(&proc_info->extended_family)); |
| 110 |
// printf(" Extended Family %d\n", proc_info->extended_family); |
| 111 |
} |
| 112 |
|
| 113 |
|
| 114 |
#ifdef x64_BIT |
| 115 |
void get_vendor (char *vendor_string) |
| 116 |
{ |
| 117 |
//get vendor name |
| 118 |
unsigned int b, c, d, e; |
| 119 |
// int i; |
| 120 |
asm volatile ("mov %4, %%eax; " // 0 into eax |
| 121 |
"cpuid;" "mov %%eax, %0;" // eeax into b |
| 122 |
"mov %%ebx, %1;" // eebx into c |
| 123 |
"mov %%edx, %2;" // eeax into d |
| 124 |
"mov %%ecx, %3;" // eeax into e |
| 125 |
:"=r" (b), "=r" (c), "=r" (d), "=r" (e) /* output */ |
| 126 |
:"r" (0) /* input */ |
| 127 |
:"%eax", "%ebx", "%ecx", "%edx" /* clobbered register, will be modifying inside the asm routine so dont use them */ |
| 128 |
); |
| 129 |
memcpy (vendor_string, &c, 4); |
| 130 |
memcpy (vendor_string + 4, &d, 4); |
| 131 |
memcpy (vendor_string + 8, &e, 4); |
| 132 |
vendor_string[12] = '\0'; |
| 133 |
// printf("Vendor %s\n",vendor_string); |
| 134 |
} |
| 135 |
#endif |
| 136 |
|
| 137 |
int turbo_status () |
| 138 |
{ |
| 139 |
//turbo state flag |
| 140 |
unsigned int eax; |
| 141 |
// int i; |
| 142 |
asm volatile ("mov %1, %%eax; " // 0 into eax |
| 143 |
"cpuid;" "mov %%eax, %0;" // eeax into b |
| 144 |
:"=r" (eax) /* output */ |
| 145 |
:"r" (6) /* input */ |
| 146 |
:"%eax" /* clobbered register, will be modifying inside the asm routine so dont use them */ |
| 147 |
); |
| 148 |
|
| 149 |
//printf("eax %d\n",(eax&0x2)>>1); |
| 150 |
|
| 151 |
return ((eax & 0x2) >> 1); |
| 152 |
} |
| 153 |
|
| 154 |
void get_familyinformation (struct family_info *proc_info) |
| 155 |
{ |
| 156 |
//get info about CPU |
| 157 |
unsigned int b; |
| 158 |
asm volatile ("mov %1, %%eax; " // 0 into eax |
| 159 |
"cpuid;" "mov %%eax, %0;" // eeax into b |
| 160 |
:"=r" (b) /* output */ |
| 161 |
:"r" (1) /* input */ |
| 162 |
:"%eax" /* clobbered register, will be modifying inside the asm routine so dont use them */ |
| 163 |
); |
| 164 |
// printf ("eax %x\n", b); |
| 165 |
proc_info->stepping = b & 0x0000000F; //bits 3:0 |
| 166 |
proc_info->model = (b & 0x000000F0) >> 4; //bits 7:4 |
| 167 |
proc_info->family = (b & 0x00000F00) >> 8; //bits 11:8 |
| 168 |
proc_info->processor_type = (b & 0x00007000) >> 12; //bits 13:12 |
| 169 |
proc_info->extended_model = (b & 0x000F0000) >> 16; //bits 19:16 |
| 170 |
proc_info->extended_family = (b & 0x0FF00000) >> 20; //bits 27:20 |
| 171 |
} |
| 172 |
|
| 173 |
double estimate_MHz () |
| 174 |
{ |
| 175 |
//copied blantantly from http://www.cs.helsinki.fi/linux/linux-kernel/2001-37/0256.html |
| 176 |
/* |
| 177 |
* $Id: MHz.c,v 1.4 2001/05/21 18:58:01 davej Exp $ |
| 178 |
* This file is part of x86info. |
| 179 |
* (C) 2001 Dave Jones. |
| 180 |
* |
| 181 |
* Licensed under the terms of the GNU GPL License version 2. |
| 182 |
* |
| 183 |
* Estimate CPU MHz routine by Andrea Arcangeli <[email protected]> |
| 184 |
* Small changes by David Sterba <[email protected]> |
| 185 |
* |
| 186 |
*/ |
| 187 |
struct timezone tz; |
| 188 |
struct timeval tvstart, tvstop; |
| 189 |
unsigned long long int cycles[2]; /* gotta be 64 bit */ |
| 190 |
unsigned long long int microseconds; /* total time taken */ |
| 191 |
|
| 192 |
memset (&tz, 0, sizeof (tz)); |
| 193 |
|
| 194 |
/* get this function in cached memory */ |
| 195 |
gettimeofday (&tvstart, &tz); |
| 196 |
cycles[0] = rdtsc (); |
| 197 |
gettimeofday (&tvstart, &tz); |
| 198 |
|
| 199 |
/* we don't trust that this is any specific length of time */ |
| 200 |
//1 sec will cause rdtsc to overlap multiple times perhaps. 100msecs is a good spot |
| 201 |
usleep (100000); |
| 202 |
|
| 203 |
cycles[1] = rdtsc (); |
| 204 |
gettimeofday (&tvstop, &tz); |
| 205 |
microseconds = ((tvstop.tv_sec - tvstart.tv_sec) * 1000000) + |
| 206 |
(tvstop.tv_usec - tvstart.tv_usec); |
| 207 |
|
| 208 |
unsigned long long int elapsed = 0; |
| 209 |
if (cycles[1] < cycles[0]) |
| 210 |
{ |
| 211 |
//printf("c0 = %llu c1 = %llu",cycles[0],cycles[1]); |
| 212 |
elapsed = UINT32_MAX - cycles[0]; |
| 213 |
elapsed = elapsed + cycles[1]; |
| 214 |
//printf("c0 = %llu c1 = %llu max = %llu elapsed=%llu\n",cycles[0], cycles[1], UINT32_MAX,elapsed); |
| 215 |
} |
| 216 |
else |
| 217 |
{ |
| 218 |
elapsed = cycles[1] - cycles[0]; |
| 219 |
//printf("\nc0 = %llu c1 = %llu elapsed=%llu\n",cycles[0], cycles[1],elapsed); |
| 220 |
} |
| 221 |
|
| 222 |
double mhz = elapsed / microseconds; |
| 223 |
|
| 224 |
|
| 225 |
//printf("%llg MHz processor (estimate). diff cycles=%llu microseconds=%llu \n", mhz, elapsed, microseconds); |
| 226 |
//printf("%g elapsed %llu microseconds %llu\n",mhz, elapsed, microseconds); |
| 227 |
return (mhz); |
| 228 |
} |
| 229 |
|
| 230 |
/* Number of decimal digits for a certain number of bits */ |
| 231 |
/* (int) ceil(log(2^n)/log(10)) */ |
| 232 |
int decdigits[] = { |
| 233 |
1, 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 4, 5, 5, |
| 234 |
5, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 9, 9, 9, 10, 10, |
| 235 |
10, 10, 11, 11, 11, 12, 12, 12, 13, 13, 13, 13, 14, 14, 14, 15, |
| 236 |
15, 15, 16, 16, 16, 16, 17, 17, 17, 18, 18, 18, 19, 19, 19, 19, |
| 237 |
20 |
| 238 |
}; |
| 239 |
|
| 240 |
#define mo_hex 0x01 |
| 241 |
#define mo_dec 0x02 |
| 242 |
#define mo_oct 0x03 |
| 243 |
#define mo_raw 0x04 |
| 244 |
#define mo_uns 0x05 |
| 245 |
#define mo_chx 0x06 |
| 246 |
#define mo_mask 0x0f |
| 247 |
#define mo_fill 0x40 |
| 248 |
#define mo_c 0x80 |
| 249 |
|
| 250 |
const char *program; |
| 251 |
|
| 252 |
|
| 253 |
uint64_t get_msr_value (int cpu, uint32_t reg, unsigned int highbit, |
| 254 |
unsigned int lowbit, int* error_indx) |
| 255 |
{ |
| 256 |
uint64_t data; |
| 257 |
int fd; |
| 258 |
// char *pat; |
| 259 |
// int width; |
| 260 |
char msr_file_name[64]; |
| 261 |
int bits; |
| 262 |
*error_indx =0; |
| 263 |
|
| 264 |
sprintf (msr_file_name, "/dev/cpu/%d/msr", cpu); |
| 265 |
fd = open (msr_file_name, O_RDONLY); |
| 266 |
if (fd < 0) |
| 267 |
{ |
| 268 |
if (errno == ENXIO) |
| 269 |
{ |
| 270 |
//fprintf (stderr, "rdmsr: No CPU %d\n", cpu); |
| 271 |
*error_indx = 1; |
| 272 |
return 1; |
| 273 |
} else if (errno == EIO) { |
| 274 |
//fprintf (stderr, "rdmsr: CPU %d doesn't support MSRs\n", cpu); |
| 275 |
*error_indx = 1; |
| 276 |
return 1; |
| 277 |
} else { |
| 278 |
//perror ("rdmsr:open"); |
| 279 |
*error_indx = 1; |
| 280 |
return 1; |
| 281 |
//exit (127); |
| 282 |
} |
| 283 |
} |
| 284 |
|
| 285 |
if (pread (fd, &data, sizeof data, reg) != sizeof data) |
| 286 |
{ |
| 287 |
//perror ("rdmsr:pread"); |
| 288 |
*error_indx = 1; |
| 289 |
return 1; |
| 290 |
//exit (127); |
| 291 |
} |
| 292 |
|
| 293 |
close (fd); |
| 294 |
|
| 295 |
bits = highbit - lowbit + 1; |
| 296 |
if (bits < 64) |
| 297 |
{ |
| 298 |
/* Show only part of register */ |
| 299 |
data >>= lowbit; |
| 300 |
data &= (1ULL << bits) - 1; |
| 301 |
} |
| 302 |
|
| 303 |
/* Make sure we get sign correct */ |
| 304 |
if (data & (1ULL << (bits - 1))) |
| 305 |
{ |
| 306 |
data &= ~(1ULL << (bits - 1)); |
| 307 |
data = -data; |
| 308 |
} |
| 309 |
|
| 310 |
*error_indx = 0; |
| 311 |
return (data); |
| 312 |
} |
| 313 |
|
| 314 |
uint64_t set_msr_value (int cpu, uint32_t reg, uint64_t data, int* error_indx) |
| 315 |
{ |
| 316 |
int fd; |
| 317 |
char msr_file_name[64]; |
| 318 |
|
| 319 |
sprintf (msr_file_name, "/dev/cpu/%d/msr", cpu); |
| 320 |
fd = open (msr_file_name, O_WRONLY); |
| 321 |
if (fd < 0) |
| 322 |
{ |
| 323 |
if (errno == ENXIO) |
| 324 |
{ |
| 325 |
//fprintf (stderr, "wrmsr: No CPU %d\n", cpu); |
| 326 |
//exit (2); |
| 327 |
*error_indx = 1; |
| 328 |
return 1; |
| 329 |
} else if (errno == EIO) { |
| 330 |
//fprintf (stderr, "wrmsr: CPU %d doesn't support MSRs\n", cpu); |
| 331 |
//exit (3); |
| 332 |
*error_indx = 1; |
| 333 |
return 1; |
| 334 |
} else { |
| 335 |
//perror ("wrmsr:open"); |
| 336 |
//exit (127); |
| 337 |
*error_indx = 1; |
| 338 |
return 1; |
| 339 |
} |
| 340 |
} |
| 341 |
|
| 342 |
if (pwrite (fd, &data, sizeof data, reg) != sizeof data) |
| 343 |
{ |
| 344 |
//perror ("wrmsr:pwrite"); |
| 345 |
//exit (127); |
| 346 |
*error_indx = 1; |
| 347 |
return 1; |
| 348 |
} |
| 349 |
close(fd); |
| 350 |
return(1); |
| 351 |
} |
| 352 |
|
| 353 |
|
| 354 |
#ifdef USE_INTEL_CPUID |
| 355 |
void get_CPUs_info (unsigned int *num_Logical_OS, |
| 356 |
unsigned int *num_Logical_process, |
| 357 |
unsigned int *num_Processor_Core, |
| 358 |
|
| 359 |
unsigned int *num_Physical_Socket); |
| 360 |
|
| 361 |
#endif |
| 362 |
|
| 363 |
|
| 364 |
//Below code |
| 365 |
/* ----------------------------------------------------------------------- * |
| 366 |
* |
| 367 |
* Copyright 2010 Abhishek Jaiantilal |
| 368 |
* |
| 369 |
* Under GPL v2 |
| 370 |
* |
| 371 |
* ----------------------------------------------------------------------- */ |
| 372 |
|
| 373 |
void Print_Version_Information() |
| 374 |
{ |
| 375 |
printf ("i7z DEBUG: i7z version: %s\n",i7z_VERSION_INFO); |
| 376 |
} |
| 377 |
|
| 378 |
|
| 379 |
void Print_Information_Processor() |
| 380 |
{ |
| 381 |
struct family_info proc_info; |
| 382 |
|
| 383 |
#ifdef x64_BIT |
| 384 |
char vendor_string[13]; |
| 385 |
get_vendor (vendor_string); |
| 386 |
if (strcmp (vendor_string, "GenuineIntel") == 0) |
| 387 |
printf ("i7z DEBUG: Found Intel Processor\n"); |
| 388 |
else |
| 389 |
{ |
| 390 |
printf |
| 391 |
("this was designed to be a intel proc utility. You can perhaps mod it for your machine?\n"); |
| 392 |
exit (1); |
| 393 |
} |
| 394 |
#endif |
| 395 |
|
| 396 |
#ifndef x64_BIT |
| 397 |
//anecdotal evidence: get_vendor doesnt seem to work on 32-bit |
| 398 |
printf |
| 399 |
("I dont know the CPUID code to check on 32-bit OS, so i will assume that you have an Intel processor\n"); |
| 400 |
printf ("Don't worry if i don't find a nehalem next, i'll quit anyways\n"); |
| 401 |
#endif |
| 402 |
|
| 403 |
get_familyinformation (&proc_info); |
| 404 |
print_family_info (&proc_info); |
| 405 |
|
| 406 |
//printf("%x %x",proc_info.extended_model,proc_info.family); |
| 407 |
|
| 408 |
//check if its nehalem or exit |
| 409 |
//Info from page 641 of Intel Manual 3B |
| 410 |
//Extended model and Model can help determine the right cpu |
| 411 |
printf("i7z DEBUG: msr = Model Specific Register\n"); |
| 412 |
if (proc_info.family == 0x6) |
| 413 |
{ |
| 414 |
if (proc_info.extended_model == 0x1) |
| 415 |
{ |
| 416 |
switch (proc_info.model) |
| 417 |
{ |
| 418 |
case 0xA: |
| 419 |
printf ("i7z DEBUG: Detected a nehalem (i7)\n"); |
| 420 |
break; |
| 421 |
case 0xE: |
| 422 |
case 0xF: |
| 423 |
printf ("i7z DEBUG: Detected a nehalem (i7/i5)\n"); |
| 424 |
break; |
| 425 |
default: |
| 426 |
printf ("i7z DEBUG: Unknown processor, not exactly based on Nehalem\n"); |
| 427 |
exit (1); |
| 428 |
} |
| 429 |
} else if (proc_info.extended_model == 0x2) |
| 430 |
{ |
| 431 |
switch (proc_info.model) |
| 432 |
{ |
| 433 |
case 0xE: |
| 434 |
printf ("i7z DEBUG: Detected a nehalem (Xeon)\n"); |
| 435 |
break; |
| 436 |
case 0x5: |
| 437 |
case 0xC: |
| 438 |
printf ("i7z DEBUG: Detected a nehalem (32nm Westmere)\n"); |
| 439 |
break; |
| 440 |
default: |
| 441 |
printf ("i7z DEBUG: Unknown processor, not exactly based on Nehalem\n"); |
| 442 |
exit (1); |
| 443 |
} |
| 444 |
} else { |
| 445 |
printf ("i7z DEBUG: Unknown processor, not exactly based on Nehalem\n"); |
| 446 |
exit (1); |
| 447 |
} |
| 448 |
} else { |
| 449 |
printf ("i7z DEBUG: Unknown processor, not exactly based on Nehalem\n"); |
| 450 |
exit (1); |
| 451 |
} |
| 452 |
|
| 453 |
} |
| 454 |
|
| 455 |
void Test_Or_Make_MSR_DEVICE_FILES() |
| 456 |
{ |
| 457 |
//test if the msr file exists |
| 458 |
if (access ("/dev/cpu/0/msr", F_OK) == 0) |
| 459 |
{ |
| 460 |
printf ("i7z DEBUG: msr device files exist /dev/cpu/*/msr\n"); |
| 461 |
if (access ("/dev/cpu/0/msr", W_OK) == 0) |
| 462 |
{ |
| 463 |
//a system mght have been set with msr allowable to be written |
| 464 |
//by a normal user so... |
| 465 |
//Do nothing. |
| 466 |
printf ("i7z DEBUG: You have write permissions to msr device files\n"); |
| 467 |
} else { |
| 468 |
printf ("i7z DEBUG: You DONOT have write permissions to msr device files\n"); |
| 469 |
printf ("i7z DEBUG: A solution is to run this program as root\n"); |
| 470 |
exit (1); |
| 471 |
} |
| 472 |
} else { |
| 473 |
printf ("i7z DEBUG: msr device files DONOT exist, trying out a makedev script\n"); |
| 474 |
if (geteuid () == 0) |
| 475 |
{ |
| 476 |
//Try the Makedev script |
| 477 |
system ("msr_major=202; \ |
| 478 |
cpuid_major=203; \ |
| 479 |
n=0; \ |
| 480 |
while [ $n -lt 16 ]; do \ |
| 481 |
mkdir -m 0755 -p /dev/cpu/$n; \ |
| 482 |
mknod /dev/cpu/$n/msr -m 0600 c $msr_major $n; \ |
| 483 |
mknod /dev/cpu/$n/cpuid -m 0444 c $cpuid_major $n; \ |
| 484 |
n=`expr $n + 1`; \ |
| 485 |
done; \ |
| 486 |
"); |
| 487 |
printf ("i7z DEBUG: modprobbing for msr\n"); |
| 488 |
system ("modprobe msr"); |
| 489 |
} else { |
| 490 |
printf ("i7z DEBUG: You DONOT have root privileges, mknod to create device entries won't work out\n"); |
| 491 |
printf ("i7z DEBUG: A solution is to run this program as root\n"); |
| 492 |
exit (1); |
| 493 |
} |
| 494 |
} |
| 495 |
} |
| 496 |
double cpufreq_info() |
| 497 |
{ |
| 498 |
//CPUINFO is wrong for i7 but correct for the number of physical and logical cores present |
| 499 |
//If Hyperthreading is enabled then, multiple logical processors will share a common CORE ID |
| 500 |
//http://www.redhat.com/magazine/022aug06/departments/tips_tricks/ |
| 501 |
system |
| 502 |
("cat /proc/cpuinfo |grep MHz|sed 's/cpu\\sMHz\\s*:\\s//'|tail -n 1 > /tmp/cpufreq.txt"); |
| 503 |
|
| 504 |
|
| 505 |
//Open the parsed cpufreq file and obtain the cpufreq from /proc/cpuinfo |
| 506 |
FILE *tmp_file; |
| 507 |
tmp_file = fopen ("/tmp/cpufreq.txt", "r"); |
| 508 |
char tmp_str[30]; |
| 509 |
fgets (tmp_str, 30, tmp_file); |
| 510 |
fclose (tmp_file); |
| 511 |
return atof(tmp_str); |
| 512 |
} |
| 513 |
|
| 514 |
int check_and_return_processor(char*strinfo) |
| 515 |
{ |
| 516 |
char *t1; |
| 517 |
if (strstr(strinfo,"processor") !=NULL) { |
| 518 |
strtok(strinfo,":"); |
| 519 |
t1 = strtok(NULL, " "); |
| 520 |
return(atoi(t1)); |
| 521 |
} else { |
| 522 |
return(-1); |
| 523 |
} |
| 524 |
} |
| 525 |
|
| 526 |
int check_and_return_physical_id(char*strinfo) |
| 527 |
{ |
| 528 |
char *t1; |
| 529 |
if (strstr(strinfo,"physical id") !=NULL) { |
| 530 |
strtok(strinfo,":"); |
| 531 |
t1 = strtok(NULL, " "); |
| 532 |
return(atoi(t1)); |
| 533 |
} else { |
| 534 |
return(-1); |
| 535 |
} |
| 536 |
} |
| 537 |
|
| 538 |
int check_and_return_core_id(char*strinfo) |
| 539 |
{ |
| 540 |
char *t1; |
| 541 |
if (strstr(strinfo,"core id") !=NULL) { |
| 542 |
strtok(strinfo,":"); |
| 543 |
t1 = strtok(NULL, " "); |
| 544 |
return(atoi(t1)); |
| 545 |
} else { |
| 546 |
return(-1); |
| 547 |
} |
| 548 |
} |
| 549 |
|
| 550 |
void construct_sibling_list(struct cpu_heirarchy_info* chi) |
| 551 |
{ |
| 552 |
int i,j,core_id,socket_id; |
| 553 |
for (i=0;i< chi->max_online_cpu ;i++) { |
| 554 |
assert(i < MAX_HI_PROCESSORS); |
| 555 |
chi->sibling_num[i]=-1; |
| 556 |
} |
| 557 |
|
| 558 |
chi->HT=false; |
| 559 |
for (i=0;i< chi->max_online_cpu ;i++) { |
| 560 |
assert(i < MAX_HI_PROCESSORS); |
| 561 |
core_id = chi->coreid_num[i]; |
| 562 |
socket_id = chi->package_num[i]; |
| 563 |
for (j=i+1;j< chi->max_online_cpu ;j++) { |
| 564 |
assert(j < MAX_HI_PROCESSORS); |
| 565 |
if (chi->coreid_num[j] == core_id && chi->package_num[j] == socket_id) { |
| 566 |
chi->sibling_num[j] = i; |
| 567 |
chi->sibling_num[i] = j; |
| 568 |
chi->display_cores[i] = 1; |
| 569 |
chi->display_cores[j] = -1; |
| 570 |
chi->HT=true; |
| 571 |
continue; |
| 572 |
} |
| 573 |
} |
| 574 |
} |
| 575 |
//for cores that donot have a sibling put in 1 |
| 576 |
for (i=0;i< chi->max_online_cpu ;i++) { |
| 577 |
assert(i < MAX_HI_PROCESSORS); |
| 578 |
if (chi->sibling_num[i] ==-1) |
| 579 |
chi->display_cores[i] = 1; |
| 580 |
} |
| 581 |
} |
| 582 |
|
| 583 |
void construct_socket_information(struct cpu_heirarchy_info* chi,struct cpu_socket_info* socket_0,struct cpu_socket_info* socket_1) |
| 584 |
{ |
| 585 |
int i; |
| 586 |
|
| 587 |
socket_0->max_cpu=0; |
| 588 |
socket_0->num_physical_cores=0; |
| 589 |
socket_0->num_logical_cores=0; |
| 590 |
socket_1->max_cpu=0; |
| 591 |
socket_1->num_physical_cores=0; |
| 592 |
socket_1->num_logical_cores=0; |
| 593 |
|
| 594 |
|
| 595 |
for (i=0;i< chi->max_online_cpu ;i++) { |
| 596 |
assert(i < MAX_HI_PROCESSORS); |
| 597 |
if (chi->display_cores[i]!=-1) { |
| 598 |
if (chi->package_num[i]==0) { |
| 599 |
assert(socket_0->max_cpu < MAX_SK_PROCESSORS); |
| 600 |
socket_0->processor_num[socket_0->max_cpu]=chi->processor_num[i]; |
| 601 |
socket_0->max_cpu++; |
| 602 |
socket_0->num_physical_cores++; |
| 603 |
socket_0->num_logical_cores++; |
| 604 |
} |
| 605 |
if (chi->package_num[i]==1) { |
| 606 |
assert(socket_1->max_cpu < MAX_SK_PROCESSORS); |
| 607 |
socket_1->processor_num[socket_1->max_cpu]=chi->processor_num[i]; |
| 608 |
socket_1->max_cpu++; |
| 609 |
socket_1->num_physical_cores++; |
| 610 |
socket_1->num_logical_cores++; |
| 611 |
} |
| 612 |
} else { |
| 613 |
if (chi->package_num[i]==0) { |
| 614 |
socket_0->num_logical_cores++; |
| 615 |
} |
| 616 |
if (chi->package_num[i]==1) { |
| 617 |
socket_1->num_logical_cores++; |
| 618 |
} |
| 619 |
} |
| 620 |
} |
| 621 |
} |
| 622 |
|
| 623 |
void print_socket_information(struct cpu_socket_info* socket) |
| 624 |
{ |
| 625 |
int i; |
| 626 |
char socket_list[200]=""; |
| 627 |
|
| 628 |
for (i=0;i< socket->max_cpu ;i++) { |
| 629 |
assert(i < MAX_SK_PROCESSORS); |
| 630 |
if (socket->processor_num[i]!=-1) { |
| 631 |
sprintf(socket_list,"%s%d,",socket_list,socket->processor_num[i]); |
| 632 |
} |
| 633 |
} |
| 634 |
printf("Socket-%d [num of cpus %d physical %d logical %d] %s\n",socket->socket_num,socket->max_cpu,socket->num_physical_cores,socket->num_logical_cores,socket_list); |
| 635 |
} |
| 636 |
|
| 637 |
void construct_CPU_Heirarchy_info(struct cpu_heirarchy_info* chi) |
| 638 |
{ |
| 639 |
FILE *fp = fopen("/proc/cpuinfo","r"); |
| 640 |
char strinfo[200]; |
| 641 |
|
| 642 |
int processor_num, physicalid_num, coreid_num; |
| 643 |
int it_processor_num=-1, it_physicalid_num=-1, it_coreid_num=-1; |
| 644 |
int tmp_processor_num, tmp_physicalid_num, tmp_coreid_num; |
| 645 |
int old_processor_num=-1; |
| 646 |
|
| 647 |
memset(chi, 0, sizeof(*chi)); |
| 648 |
|
| 649 |
if (fp!=NULL) { |
| 650 |
while ( fgets(strinfo,200,fp) != NULL) { |
| 651 |
// printf(strinfo); |
| 652 |
tmp_processor_num = check_and_return_processor(strinfo); |
| 653 |
tmp_physicalid_num = check_and_return_physical_id(strinfo); |
| 654 |
tmp_coreid_num = check_and_return_core_id(strinfo); |
| 655 |
|
| 656 |
|
| 657 |
if (tmp_processor_num != -1) { |
| 658 |
it_processor_num++; |
| 659 |
processor_num = tmp_processor_num; |
| 660 |
assert(it_processor_num < MAX_HI_PROCESSORS); |
| 661 |
chi->processor_num[it_processor_num] = processor_num; |
| 662 |
} |
| 663 |
if (tmp_physicalid_num != -1) { |
| 664 |
it_physicalid_num++; |
| 665 |
physicalid_num = tmp_physicalid_num; |
| 666 |
assert(it_physicalid_num < MAX_HI_PROCESSORS); |
| 667 |
chi->package_num[it_physicalid_num] = physicalid_num; |
| 668 |
} |
| 669 |
if (tmp_coreid_num != -1) { |
| 670 |
it_coreid_num++; |
| 671 |
coreid_num = tmp_coreid_num; |
| 672 |
assert(it_coreid_num < MAX_HI_PROCESSORS); |
| 673 |
chi->coreid_num[it_coreid_num] = coreid_num; |
| 674 |
} |
| 675 |
if (processor_num != old_processor_num) { |
| 676 |
old_processor_num = processor_num; |
| 677 |
} |
| 678 |
} |
| 679 |
} |
| 680 |
chi->max_online_cpu = it_processor_num+1; |
| 681 |
fclose(fp); |
| 682 |
} |
| 683 |
|
| 684 |
void print_CPU_Heirarchy(struct cpu_heirarchy_info chi) |
| 685 |
{ |
| 686 |
int i; |
| 687 |
printf("\n------------------------------\n--[core id]--- Other information\n-------------------------------------\n"); |
| 688 |
for (i=0;i < chi.max_online_cpu;i++) { |
| 689 |
assert(i < MAX_HI_PROCESSORS); |
| 690 |
printf("--[%d] Processor number %d\n",i,chi.processor_num[i]); |
| 691 |
printf("--[%d] Socket number/Hyperthreaded Sibling number %d,%d\n",i,chi.package_num[i],chi.sibling_num[i]); |
| 692 |
printf("--[%d] Core id number %d\n",i,chi.coreid_num[i]); |
| 693 |
printf("--[%d] Display core in i7z Tool: %s\n\n",i,(chi.display_cores[i]==1)?"Yes":"No"); |
| 694 |
} |
| 695 |
} |
| 696 |
|
| 697 |
int in_core_list(int ii,int* core_list) |
| 698 |
{ |
| 699 |
int i; |
| 700 |
int in=0; |
| 701 |
for (i=0;i<8;i++) { |
| 702 |
if (ii == core_list[i]) { |
| 703 |
in=1; |
| 704 |
break; |
| 705 |
} |
| 706 |
} |
| 707 |
return(in); |
| 708 |
} |
| 709 |
|