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