Home | History | Annotate | Download | only in ht_affinity
      1 
      2 #include "ht_utils.h"
      3 
      4 #include <stdio.h>
      5 #include <stdlib.h>
      6 #include <alloca.h>
      7 #include <string.h>
      8 #include <linux/unistd.h>
      9 #include "ltp_cpuid.h"
     10 
     11 #define PROC_PATH	"/proc"
     12 #define BUFF_SIZE	8192
     13 #define PROCESSOR_STR	"processor"
     14 
     15 #define MAX_CPU_NUM 128
     16 
     17 char buffer[BUFF_SIZE];
     18 
     19 int is_ht_cpu(void)
     20 {
     21 	/*Number of logic processor in a physical processor */
     22 	int smp_num_siblings = -1;
     23 	/*ht flag */
     24 	int ht = -1;
     25 	unsigned int eax, ebx, ecx, edx;
     26 	cpuid(1, &eax, &ebx, &ecx, &edx);
     27 	smp_num_siblings = (ebx & 0xff0000) >> 16;
     28 	ht = (edx & 0x10000000) >> 28;
     29 
     30 	if (ht == 1 && smp_num_siblings >= 2) {
     31 		/*printf("The processor in this system supports HT\n"); */
     32 		return 1;
     33 	} else {
     34 		/*printf("The processor in this system does not support
     35 		 * HT\n");*/
     36 		return 0;
     37 	}
     38 }
     39 
     40 /* return 0 means Pass,
     41  return 1 means ht is not enabled,*/
     42 int check_ht_capability(void)
     43 {
     44 	int result;
     45 	if (is_ht_cpu())
     46 		result = 0;	/*HT is enabled by default in this system. */
     47 	else
     48 		result = 1;	/*HT is not enabled by default in this system. */
     49 
     50 	return result;
     51 }
     52 
     53 #define PROCFS_PATH "/proc/"
     54 #define CPUINFO_PATH "/proc/cpuinfo"
     55 #define CPU_NAME "processor"
     56 #define STAT_NAME "stat"
     57 
     58 char buf[256];
     59 
     60 int get_cpu_count(void)
     61 {
     62 	FILE *pfile;
     63 	int count;
     64 
     65 	pfile = fopen(CPUINFO_PATH, "r");
     66 	if (pfile == NULL)
     67 		return 0;
     68 
     69 	count = 0;
     70 
     71 	while (fgets(buf, 255, pfile) != NULL) {
     72 		if (strncmp(buf, CPU_NAME, strlen(CPU_NAME)) == 0)
     73 			count++;
     74 	}
     75 
     76 	fclose(pfile);
     77 
     78 	return count;
     79 }
     80 
     81 int get_current_cpu(pid_t pid)
     82 {
     83 	int cpu = -1;
     84 	int da;
     85 	char str[100];
     86 	char ch;
     87 
     88 	FILE *pfile;
     89 
     90 	sprintf(buf, "%s%d/%s%c", PROCFS_PATH, pid, STAT_NAME, 0);
     91 
     92 	pfile = fopen(buf, "r");
     93 	if (pfile == NULL)
     94 		return -1;
     95 
     96 	if (fscanf(pfile, "%d %s %c %d %d %d %d %d %d %d %d %d %d %d %d %d %d\
     97 	 %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d", &da, str, &ch, &da, &da, &da, &da, &da, &da, &da, &da, &da, &da, &da, &da, &da, &da, &da, &da, &da, &da, &da, &da, &da, &da, &da, &da, &da, &da, &da, &da, &da, &da, &da, &da, &da, &da, &da, &cpu) <= 0) {
     98 		fclose(pfile);
     99 		return -1;
    100 	}
    101 
    102 	fclose(pfile);
    103 
    104 	return cpu;
    105 }
    106