1 #include <stdio.h>\r
2 #include <pthread.h>\r
3 #include <unistd.h>\r
4 #include <stdlib.h>\r
5 #include <sched.h>\r
6 #include <linux/unistd.h>\r
7 #include <sys/syscall.h>\r
8 #include <errno.h>\r
9 \r
10 #include "netsync.h"\r
11 #include "netapi_util.h"\r
12 //#define INC_TO 1000000 // one million.\r
13 #define INC_TO 10000 // one million...\r
14 \r
15 NETAPI_ATOMIC64_T global_int=NETAPI_ATOMIC_INIT64(0LL) ;\r
16 __thread blah=0;\r
17 int blah2=0;\r
18 pid_t gettid( void )\r
19 {\r
20 return syscall( __NR_gettid );\r
21 }\r
22 \r
23 void *thread_routine( void *arg )\r
24 {\r
25 int i;\r
26 int proc_num = (int)(long)arg;\r
27 cpu_set_t set;\r
28 int v1, v2,v3;\r
29 \r
30 #if 0\r
31 CPU_ZERO( &set );\r
32 CPU_SET( proc_num, &set );\r
33 \r
34 if (sched_setaffinity( gettid(), sizeof( cpu_set_t ), &set ))\r
35 {\r
36 perror( "sched_setaffinity" );\r
37 return NULL;\r
38 }\r
39 #endif\r
40 \r
41 for (i = 0; i < INC_TO; i++)\r
42 {\r
43 // global_int++;\r
44 //__sync_fetch_and_add( &global_int, 1 );\r
45 \r
46 v1 = netapi_timing_start(); \r
47 netapi_atomic_add64(&global_int,1LL);\r
48 v1= netapi_timing_stop() - v1;\r
49 sched_yield();\r
50 v2 = netapi_timing_start(); \r
51 blah+=1; \r
52 v2= netapi_timing_stop() - v2;\r
53 \r
54 v3 = netapi_timing_start(); \r
55 blah2+=1; \r
56 v3= netapi_timing_stop() - v3;\r
57 if(i<10) { printf("thread %d -> a64 costs %d, tls costs %d simple++ costs %d\n", proc_num,v1,v2,v3);}\r
58 sched_yield();\r
59 }\r
60 printf("thead %d -> blah=%d\n",proc_num, blah);\r
61 return NULL;\r
62 }\r
63 \r
64 int main()\r
65 {\r
66 int procs = 0;\r
67 int i;\r
68 pthread_t *thrs;\r
69 printf("at start up\n");\r
70 \r
71 // Getting number of CPUs\r
72 procs = (int)sysconf( _SC_NPROCESSORS_ONLN );\r
73 if (procs < 0)\r
74 {\r
75 perror( "sysconf" );\r
76 return -1;\r
77 }\r
78 printf(" num cpus = %d\n",procs);\r
79 if (procs==1) {procs+=1; printf("adding 2nd 'core' \n");}\r
80 thrs = malloc( sizeof( pthread_t ) * procs );\r
81 if (thrs == NULL)\r
82 {\r
83 perror( "malloc" );\r
84 return -1;\r
85 }\r
86 \r
87 printf( "Starting %d threads...\n", procs );\r
88 \r
89 for (i = 0; i < procs; i++)\r
90 {\r
91 if (pthread_create( &thrs[i], NULL, thread_routine,\r
92 (void *)(long)i ))\r
93 {\r
94 perror( "pthread_create" );\r
95 procs = i;\r
96 break;\r
97 }\r
98 }\r
99 \r
100 for (i = 0; i < procs; i++)\r
101 pthread_join( thrs[i], NULL );\r
102 \r
103 free( thrs );\r
104 \r
105 printf( "After doing all the math, global_int value is: %lld bare global_int is %d\n",\r
106 global_int.val, blah2 );\r
107 printf( "Expected value is: %d\n", INC_TO * procs );\r
108 \r
109 return 0;\r
110 }\r