-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcpu.c
More file actions
130 lines (106 loc) · 2.48 KB
/
Copy pathcpu.c
File metadata and controls
130 lines (106 loc) · 2.48 KB
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#include<sys/types.h>
#include<sys/sysinfo.h>
#include <sys/time.h>
#include<unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <pthread.h>
#include <string.h>
#include <iostream>
using namespace std;
#define ENABLE_WHCIH_CPU
#define ENABLE_SET_CPU
#define EXEC_COUNT (100 * 1000 * 1000)
struct bits_t
{
int a;
char placeholder[64];
int b;
};
struct bits_t bits;
int which_cpu(const char* prefix_)
{
#ifdef ENABLE_WHCIH_CPU
cpu_set_t cur_cpu;
CPU_ZERO(&cur_cpu);
if (sched_getaffinity(0, sizeof(cur_cpu), &cur_cpu) == -1)
{
printf("warning: cound not get cpu affinity, continuing...\n");
return -1;
}
int num = sysconf(_SC_NPROCESSORS_CONF);
for (int i = 0; i < num; i++)
{
if (CPU_ISSET(i, &cur_cpu))
{
printf("[%s] this process %d is running processor : %d\n", prefix_, getpid(), i);
}
}
#endif
return 0;
}
int set_cpu(int cpu_id_)
{
#ifdef ENABLE_SET_CPU
cpu_set_t mask;
CPU_ZERO(&mask);
CPU_SET(cpu_id_, &mask);
if (sched_setaffinity(0, sizeof(mask), &mask) == -1)
{
printf("warning: could not set CPU affinity, continuing...\n");
return -1;
}
#endif
return 0;
}
void* thd_func1(void* arg_)
{
set_cpu(0);
which_cpu("thread 1 start");
timeval begin_tv;
gettimeofday(&begin_tv, NULL);
for (int i = 0; i < EXEC_COUNT; i++)
{
bits.a += 1;
int a = bits.a;
}
timeval end_tv;
gettimeofday(&end_tv, NULL);
printf("thd1 perf:[%lu]us\n", (end_tv.tv_sec * 1000 * 1000 + end_tv.tv_usec) - (begin_tv.tv_sec * 1000 * 1000 + begin_tv.tv_usec));
which_cpu("thread 1 end");
return NULL;
}
void* thd_func2(void* arg_)
{
set_cpu(1);
which_cpu("thread 2 start");
timeval begin_tv;
gettimeofday(&begin_tv, NULL);
for (int i = 0; i < EXEC_COUNT; i++)
{
bits.b += 2;
int b = bits.b;
}
timeval end_tv;
gettimeofday(&end_tv, NULL);
printf("thd2 perf:[%lu]us\n", (end_tv.tv_sec * 1000 * 1000 + end_tv.tv_usec) - (begin_tv.tv_sec * 1000 * 1000 + begin_tv.tv_usec));
which_cpu("thread 2 end");
return NULL;
}
int main(int argc_, char* argv_[])
{
int num = sysconf(_SC_NPROCESSORS_CONF);
printf("system has %d processor(s).\n", num);
cpu_set_t cpu_mask;
cpu_set_t cur_cpu_info;
memset((void*)&bits, 0, sizeof(bits_t));
set_cpu(0);
which_cpu("main thread");
pthread_t pid1;
pthread_create(&pid1, NULL, thd_func1, NULL);
pthread_t pid2;
pthread_create(&pid2, NULL, thd_func2, NULL);
pthread_join(pid1, NULL);
pthread_join(pid2, NULL);
return 0;
}