# define _GNU_SOURCE
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
Typedef struct __test {
Uint64_t a;
Uint64_t b;
Uint64_t c;
Uint64_t d;
Uint64_t e;
} test_t;
# define CPU_MAX 128
Int cpuid [CPU_MAX];
Test_t tdata [CPU_MAX];
Int seq [CPU_MAX];
Int thd_num=1;
Uint64_t prev [CPU_MAX];
Uint64_t TMP [CPU_MAX];
Int flag_stat=0;
The static void
Init (void)
{
For (int I=0; I & lt; CPU_MAX; I++) {
Cpuid [I]=1;
Seq [I]=I;
Tdata [I]. E=100;
Tdata [I] a=0;
Tdata [I]. B=0;
Tdata [I]. C=0;
Tdata [I] d=0;
Prev [I]=0;
}
}
Static int
Parse_cpuid (const char * STR)
{
Char buf [256].
Int flag_start=0;
Int thdnum=0;
Int len=strlen (STR);
If (len & gt; 255) {
Memcpy (buf, STR, 255);
Buf [255]=0;
}
Else if (0 & lt; Len) {
Strcpy (buf, STR);
}
The else return 0;
For (int I=0; I & lt; 256; I++) {
If (buf [I]=='| | buf [I]==' \ t '| | buf [I]==', ') buf [I]=0;
}
For (int I=0; I & lt; 256; I++) {
If (flag_start & amp; & 0==buf flag_start [I])=0;
If (0==flag_start & amp; & 0!=buf [I]) {
Flag_start=1;
Cpuid [thdnum]=atoi (buf + I);
If (0 & gt; Cpuid [thdnum] | | CPU_MAX & lt;=cpuid [thdnum]) return 1;
Thdnum++;
}
}
return 0;
}
Static int
Parse_cmd (int arg c, char * * argv)
{
If (0 & lt; Arg c) {
Thd_num=atoi (argv [0]);
If (0 & gt; Thd_num) return 1;
Arg c -, argv++;
}
If (0 & lt; Arg c) {
Return parse_cpuid (argv [0]);
}
return 0;
}
The static void *
Task_root (void * arg)
{
The register int seq=* arg (int *);
Int cid=cpuid [seq];
The cpu_set_t cpumask;
Pid_t pid=the syscall (__NR_gettid);
If (0 & lt;=cid & amp; & CPU_MAX & gt; Cid) {
//bind CPUID
CPU_ZERO (& amp; Cpumask);
CPU_SET (cid, & amp; Cpumask);
Sched_setaffinity (pid, sizeof (cpumask), & amp; Cpumask);
Usleep (1000);
Printf (" task [% d] run on cpuid=% d \ n ", seq, cid);
}
The else {
Printf (" task [% d] run on cpuid=auto \ n ", seq);
}
While (1) {
Tdata [seq]. A +=1;
Tdata [seq]. B +=tdata [seq] e * 8;
Tdata [seq] c +=1;
Tdata [seq] d +=tdata [seq] e * 8;
}
Return NULL;
}
The static void *
Stat_root (void * arg)
{
Arg=arg;
Uint64_t TTL.
Uint64_t d10 [CPU_MAX];
While (1) {
If (0==flag_stat) {
Usleep (1000);
}
The else {
Flag_stat=0;
TTL=0;
For (int I=0; I & lt; Thd_num; I++) {
TMP [I]=tdata [I] a;
}
For (int I=0; I & lt; Thd_num; I++) {
TMP [I]=tdata [I] a;
D10 [I]=TMP [I] - prev [I];
TTL +=d10 [I];
}
Printf (" \ nTotal do times: % lu (SEC) \ n ", TTL/10);
For (int I=0; I & lt; Thd_num; I++) {
If (1==cpuid [I]) {
Printf (" \ ttask [% d]=auto do CPU times: % lu (/SEC) \ n ", I, d10 [I]/10);
}
The else {
Printf (" \ ttask [% d]=% d do CPU times: % lu (/SEC) \ n ", I, cpuid [I], d10 [I]/10);
}
Prev=TMP [I] [I];
}
}
}
Return NULL;
}
The static void *
Timer_root (void * arg)
{
Arg=arg;
Uint64_t TTL.
Uint64_t d10 [CPU_MAX];
While (1) {
Sleep (10);
Flag_stat=1;
}
Return NULL;
}
Static int
Task_set (void)
{
Pthread_t thdid;
For (int I=0; I & lt; Thd_num; I++) {
Pthread_create (& amp; Thdid, NULL, task_root, & amp; Seq [I]);
}
Pthread_create (& amp; Thdid, NULL, timer_root, NULL);
Pthread_create (& amp; Thdid, NULL, stat_root, NULL);
return 0;
}
Int main (int arg c, char * * argv)
{
//program thread - num thread - CPU - seq
init();
Arg c -, argv++;
If (1==parse_cmd (arg c, argv)) {
Printf (" Input param error \ n ");
The exit (1);
}
Task_set ();
While (1) {
sleep(1000);
}
return 0;
}
Double CPU - 4 nuclear - hyper-threading Intel Xeon (R) (R) E5620 @ 2.40 GHz CPU
Logical CPU 0 8 2 10 4 12 June 14 1 9 3 11 5 13 July 15
Pysical id 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0
The core id 0 0 1 1 9 9 10 10 0 0 1 1 9 9 10 10
System: centos8
Test thread:
Single thread binding 0 logical CPU test results (implementation accumulative number) 108363138 (times/SEC)
Double thread binding logical CPU 0, 1 test results (implementation accumulative number) 79961987 (times/SEC)
3 thread binding 0 logical CPU test results (implementation accumulative number) 108639280 (times/SEC)
4 thread binding logical CPU 0,1,2,3 test results (implementation accumulative number) 97511278 (times/SEC)
5 thread binding logical CPU 0,1,2,3,4 test results (implementation accumulative number) 115751101 (times/SEC)
6 thread binding logical CPU 0,1,2,3,4,5 test results (implementation accumulative number) 138198505 (times/SEC)
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull