-
Notifications
You must be signed in to change notification settings - Fork 9
/
util.c
101 lines (77 loc) · 1.67 KB
/
util.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/**
* Linux exploit dev training - module 6
* vnik@cyseclabs.com
**/
#define _GNU_SOURCE
#include <stdio.h>
#include <limits.h>
#include <unistd.h>
#include <sys/types.h>
#include <sched.h>
#include <pthread.h>
#include <cpuid.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sched.h>
#define RTM_BIT (1 << 11)
cpu_set_t cpuset;
int cont;
/* number of logical processors */
int get_nprocs(void) {
return (int)sysconf(_SC_NPROCESSORS_ONLN);
}
void *loop(void *ptr) {
while (cont) ;
}
void start_cpu_load(void) {
int i, ncores;
cpu_set_t set;
CPU_ZERO(&set);
ncores = get_nprocs() / 2; /* assuming Intel hyperthreading for now */
pthread_t threads[ncores];
for (i = 0; i < ncores; i++) {
CPU_SET(i, &set);
}
cont = 1;
for (i = 0; i < ncores; i++) {
pthread_create(&threads[i], NULL, loop, NULL);
}
}
void stop_cpu_load(void) {
cont = 0;
}
int pin_cpu(void) {
cpu_set_t set;
CPU_ZERO(&set);
CPU_SET(0, &set);
sched_getaffinity(0, sizeof(cpuset), &cpuset);
if (sched_setaffinity(0, sizeof(set), &set) == -1) {
return -1;
}
}
int unpin_cpu(void) {
if (sched_setaffinity(0, sizeof(cpuset), &cpuset) == -1)
return -1;
}
/* CPU supports RTM execution if CPUID.07H.EBX.RTM [bit 11] = 1 */
int cpu_has_rtm(void) {
if (__get_cpuid_max(0, NULL) >= 7) {
unsigned a, b, c, d;
__cpuid_count(7, 0, a, b, c, d);
return (b & RTM_BIT);
}
return 0;
}
int get_ncores(void) {
if (__get_cpuid_max(0, NULL) >= 7) {
unsigned eax = 0, ebx = 0, ecx = 0, edx = 0;
__get_cpuid(0xb, &eax, &ebx, &ecx, &edx);
printf("ebx = %d\n", ebx);
return (ebx >> 16) & 0xff ;
}
return 0;
}