Home > OS >  Two threads, nuclear, CPU statistics difference is big, who can explain?
Two threads, nuclear, CPU statistics difference is big, who can explain?

Time:10-10

Code:
-- -- --
# include & lt; Stdio. H>
# define _USE_GNU
# include & lt; Pthread. H>
# include & lt; Sys/PRCTL. H>
# include & lt; Stdint. H>
# include & lt; Sys/types. H>
# include & lt; Sched. H>
# include & lt; Sys/ipc. H>
# include & lt; Sys/MSG. H>

Int32_t cpu1.
Int32_t cpu2.
Int set_cpu (int I)
{
The cpu_set_t mask;
CPU_ZERO (& amp; Mask);

CPU_SET (I, & amp; Mask);

Printf (" thread % u, I=% d \ n ", pthread_self (), I);
If (1==pthread_setaffinity_np (pthread_self (), sizeof (mask), & amp; Mask))
{
Fprintf (stderr, "pthread_setaffinity_np erro \ n");
return -1;
}
return 0;
}
Void * task1 (void * a) {
PRCTL (PR_SET_NAME, (char *) "task1");
Set_cpu (cpu1);
Uint32_t I=1;
Volatile char * SRC=https://bbs.csdn.net/topics/(char *) malloc (1 <20);
Volatile char * DST=(char *) malloc (1 & lt; <20);
While (1) the memcpy (DST, SRC, 1 & lt; <20);
return NULL;
}

Void * task2 (void * a) {
PRCTL (PR_SET_NAME, (char *) "task2");
Set_cpu (cpu2);
Uint32_t I=1;
Volatile char * SRC=https://bbs.csdn.net/topics/(char *) malloc (1 <20);
Volatile char * DST=(char *) malloc (1 & lt; <20);
While (1) {usleep (10000); Memcpy (DST, SRC, 1 & lt; <20); }
return NULL;
}

Int main (int arg c, char * argv []) {
Pthread_t t1, t2;
Printf (" sample:./test 0 0 \ n ");
If (arg c!=3) {printf (" input param \ n "); return 1; }
Cpu1=atoi (argv [1]);
If (cpu1 & gt;=0) {
Pthread_create (& amp; T1, NULL, task1, NULL);
Usleep (1000000);
}
Cpu2=atoi (argv [2]);
If (cpu2 & gt;=0) {
Pthread_create (& amp; T2, NULL, task2, NULL);
}
While (1) the usleep (100000);
return 0;
}
-- -- --
Run
Case 1, the two threads in a CPU core:/test 0 0
Usr CPU0:99.0% 0.0% 0.0% 0.0% 0.9% 0.0% sys nic idle IO irq 0.0% sirq
CPU1: usr sys nic idle 93.4% 0.0% 5.6% 0.0% 0.9% 0.0% IO irq 0.0% sirq
The Load average: 1.00 0.92 0.99 2/92 3719
PID PPID USER STAT VSZ VSZ % % CPU CPU COMMAND
3717 2839 root R 24412 9.6 0 {task1} 46.4./test 0 0
3718 2839 root S 24412 0 {task2} 2.7. 9.6/test 0 0

Case 2, the two threads run in two core:./test 0 1
Usr CPU0:100% 0.0% 0.0% 0.0% 0.0% 0.0% sys nic idle IO irq 0.0% sirq
CPU1: usr sys nic idle 85.1% 0.0% 5.5% 0.0% 9.2% 0.0% IO irq 0.0% sirq
The Load average: 1.02 0.96 1.00 2/92 3763
PID PPID USER STAT VSZ VSZ % % CPU CPU COMMAND
3761 2839 root R 24412 9.6 0 {task1} 48.4./test 0 1
3762 2839 root S 24412 9.6 1 3.6 {task2}./test 0 1

3, run only task2 threads in the core 1:./test - 1 1
Usr CPU0:0.0% 0.0% 0.0% 99.0% 0.9% 0.0% sys nic idle IO irq 0.0% sirq
CPU1: usr sys nic idle 93.1% 0.0% 1.9% 0.0% 4.9% 0.0% IO irq 0.0% sirq
The Load average: 0.92 0.97 1.00 2/97 3806
PID PPID USER STAT VSZ VSZ % % CPU CPU COMMAND
3785 2839 root S 13140 5.2 1 1.9 {task2}. 1/test - 1
3786 2905 root R 3088 1.2 0.9 top - 1 d 1

=========================================================
My question:
Three kinds of cases, why task2 CPU statistics difference is bigger?

CodePudding user response:

Procedure is simpler, two threads to bind to different CPU core, check the thread CPU loading, why task2 CPU statistics difference is very big

CodePudding user response:

To solve......

CodePudding user response:

Your program has a lot of sleep

CodePudding user response:

Why cpu0 ran high, cpu1 threads on the CPU is high up?