]> pd.if.org Git - liblfds/blob - liblfds/liblfds6.1.0/test/src/benchmark_freelist.c
Initial import (all versions, including the new 7.1.0)
[liblfds] / liblfds / liblfds6.1.0 / test / src / benchmark_freelist.c
1 #include "internal.h"
2
3
4
5
6
7 /****************************************************************************/
8 void benchmark_lfds610_freelist( void )
9 {
10   unsigned int
11     loop,
12     thread_count,
13     cpu_count;
14
15   struct lfds610_freelist_state
16     *fs;
17
18   struct lfds610_freelist_benchmark
19     *fb;
20
21   thread_state_t
22     *thread_handles;
23
24   lfds610_atom_t
25     total_operations_for_full_test_for_all_cpus,
26     total_operations_for_full_test_for_all_cpus_for_one_cpu = 0;
27
28   double
29     mean_operations_per_second_per_cpu,
30     difference_per_second_per_cpu,
31     total_difference_per_second_per_cpu,
32     std_dev_per_second_per_cpu,
33     scalability;
34
35   /* TRD : here we benchmark the freelist
36
37            the benchmark is to have a single freelist
38            where a worker thread busy-works popping and then pushing
39   */
40
41   cpu_count = abstraction_cpu_count();
42
43   thread_handles = (thread_state_t *) malloc( sizeof(thread_state_t) * cpu_count );
44
45   fb = (struct lfds610_freelist_benchmark *) malloc( sizeof(struct lfds610_freelist_benchmark) * cpu_count );
46
47   // TRD : print the benchmark ID and CSV header
48   printf( "\n"
49           "Release %s Freelist Benchmark #1\n"
50           "CPUs,total ops,mean ops/sec per CPU,standard deviation,scalability\n", LFDS610_RELEASE_NUMBER_STRING );
51
52   // TRD : we run CPU count times for scalability
53   for( thread_count = 1 ; thread_count <= cpu_count ; thread_count++ )
54   {
55     // TRD : initialisation
56     lfds610_freelist_new( &fs, 1000, NULL, NULL );
57
58     for( loop = 0 ; loop < cpu_count ; loop++ )
59     {
60       (fb+loop)->fs = fs;
61       (fb+loop)->operation_count = 0;
62     }
63
64     // TRD : main test
65     for( loop = 0 ; loop < thread_count ; loop++ )
66       abstraction_thread_start( &thread_handles[loop], loop, benchmark_lfds610_freelist_thread_pop_and_push, fb+loop );
67
68     for( loop = 0 ; loop < thread_count ; loop++ )
69       abstraction_thread_wait( thread_handles[loop] );
70
71     // TRD : post test math
72     total_operations_for_full_test_for_all_cpus = 0;
73     total_difference_per_second_per_cpu = 0;
74
75     for( loop = 0 ; loop < thread_count ; loop++ )
76       total_operations_for_full_test_for_all_cpus += (fb+loop)->operation_count;
77
78     mean_operations_per_second_per_cpu = ((double) total_operations_for_full_test_for_all_cpus / (double) thread_count) / (double) 10;
79
80     if( thread_count == 1 )
81       total_operations_for_full_test_for_all_cpus_for_one_cpu = total_operations_for_full_test_for_all_cpus;
82
83     for( loop = 0 ; loop < thread_count ; loop++ )
84     {
85       difference_per_second_per_cpu = ((double) (fb+loop)->operation_count / (double) 10) - mean_operations_per_second_per_cpu;
86       total_difference_per_second_per_cpu += difference_per_second_per_cpu * difference_per_second_per_cpu;
87     }
88
89     std_dev_per_second_per_cpu = sqrt( (double) total_difference_per_second_per_cpu );
90
91     scalability = (double) total_operations_for_full_test_for_all_cpus / (double) (total_operations_for_full_test_for_all_cpus_for_one_cpu * thread_count);
92
93     printf( "%u,%u,%.0f,%.0f,%0.2f\n", thread_count, (unsigned int) total_operations_for_full_test_for_all_cpus, mean_operations_per_second_per_cpu, std_dev_per_second_per_cpu, scalability );
94
95     // TRD : cleanup
96     lfds610_freelist_delete( fs, NULL, NULL );
97   }
98
99   free( fb );
100
101   free( thread_handles );
102
103   return;
104 }
105
106
107
108
109
110 /****************************************************************************/
111 thread_return_t CALLING_CONVENTION benchmark_lfds610_freelist_thread_pop_and_push( void *freelist_benchmark )
112 {
113   struct lfds610_freelist_benchmark
114     *fb;
115
116   struct lfds610_freelist_element
117     *fe;
118
119   time_t
120     start_time;
121
122   assert( freelist_benchmark != NULL );
123
124   fb = (struct lfds610_freelist_benchmark *) freelist_benchmark;
125
126   time( &start_time );
127
128   while( time(NULL) < start_time + 10 )
129   {
130     lfds610_freelist_pop( fb->fs, &fe );
131     lfds610_freelist_push( fb->fs, fe );
132
133     fb->operation_count += 2;
134   }
135
136   return( (thread_return_t) EXIT_SUCCESS );
137 }
138