summaryrefslogtreecommitdiff
path: root/Source/DirectFB/tools/fusion_bench.c
blob: 8ea04df2b5fe69e01e940881349d0ff89da522eb (plain)
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
/*
   (c) Copyright 2001-2009  The world wide DirectFB Open Source Community (directfb.org)
   (c) Copyright 2000-2004  Convergence (integrated media) GmbH

   All rights reserved.

   Written by Denis Oliver Kropp <dok@directfb.org>,
              Andreas Hundt <andi@fischlustig.de>,
              Sven Neumann <neo@directfb.org>,
              Ville Syrjälä <syrjala@sci.fi> and
              Claudio Ciccani <klan@users.sf.net>.

   This file is subject to the terms and conditions of the MIT License:

   Permission is hereby granted, free of charge, to any person
   obtaining a copy of this software and associated documentation
   files (the "Software"), to deal in the Software without restriction,
   including without limitation the rights to use, copy, modify, merge,
   publish, distribute, sublicense, and/or sell copies of the Software,
   and to permit persons to whom the Software is furnished to do so,
   subject to the following conditions:

   The above copyright notice and this permission notice shall be
   included in all copies or substantial portions of the Software.

   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
   MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
   IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
   CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
   TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
   SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

#include <config.h>

#include <stdio.h>
#include <unistd.h>

#include <sys/file.h>

#include <pthread.h>

#include <direct/clock.h>

#include <fusion/build.h>
#include <fusion/fusion.h>
#include <fusion/lock.h>
#include <fusion/property.h>
#include <fusion/reactor.h>
#include <fusion/ref.h>
#include <fusion/shmalloc.h>

#include <directfb.h>

#include <core/system.h>


static long long     t1, t2;
static unsigned int  loops;
static FusionWorld  *world;

#define BENCH_START()       do { sync(); usleep(100000); sync(); t1 = direct_clock_get_millis(); loops = 0; } while (0)
#define BENCH_STOP()        do { t2 = direct_clock_get_millis(); } while (0)

#define BENCH_LOOP()        while ((++loops & 0xfff) || (direct_clock_get_millis() - t1 < 1000))

#define BENCH_RESULT()      (loops / (float)(t2 - t1))
#define BENCH_RESULT_BY(x)  ((loops * x) / (float)(t2 - t1))


static ReactionResult
reaction_callback (const void *msg_data,
                   void       *ctx)
{
     return RS_OK;
}

static void
bench_reactor( void )
{
     FusionReactor  *reactor;
     Reaction        reaction;
     Reaction        reaction2;
     GlobalReaction  global_reaction;

     reactor = fusion_reactor_new( 16, "Benchmark", world );
     if (!reactor) {
          fprintf( stderr, "Fusion Error\n" );
          return;
     }


     /* reactor attach/detach */
     BENCH_START();

     BENCH_LOOP() {
          fusion_reactor_attach( reactor, reaction_callback, NULL, &reaction );
          fusion_reactor_detach( reactor, &reaction );
     }

     BENCH_STOP();

     printf( "reactor attach/detach                 -> %8.2f k/sec\n", BENCH_RESULT() );


     /* reactor attach/detach (2nd) */
     fusion_reactor_attach( reactor, reaction_callback, NULL, &reaction );

     BENCH_START();

     BENCH_LOOP() {
          fusion_reactor_attach( reactor, reaction_callback, NULL, &reaction2 );
          fusion_reactor_detach( reactor, &reaction2 );
     }

     BENCH_STOP();

     fusion_reactor_detach( reactor, &reaction );

     printf( "reactor attach/detach (2nd)           -> %8.2f k/sec\n", BENCH_RESULT() );


     /* reactor attach/detach (global) */
     fusion_reactor_attach( reactor, reaction_callback, NULL, &reaction );

     BENCH_START();

     BENCH_LOOP() {
          fusion_reactor_attach_global( reactor, 0, NULL, &global_reaction );
          fusion_reactor_detach_global( reactor, &global_reaction );
     }

     BENCH_STOP();

     fusion_reactor_detach( reactor, &reaction );

     printf( "reactor attach/detach (global)        -> %8.2f k/sec\n", BENCH_RESULT() );


     /* reactor dispatch */
     fusion_reactor_attach( reactor, reaction_callback, NULL, &reaction );

     BENCH_START();

     BENCH_LOOP() {
          char msg[16];

          fusion_reactor_dispatch( reactor, msg, true, NULL );
     }

     BENCH_STOP();

     printf( "reactor dispatch                      -> %8.2f k/sec\n", BENCH_RESULT() );


     fusion_reactor_detach( reactor, &reaction );


     fusion_reactor_free( reactor );

     printf( "\n" );
}

static void
bench_ref( void )
{
     DirectResult ret;
     FusionRef    ref;

     ret = fusion_ref_init( &ref, "Benchmark", world );
     if (ret) {
          fprintf( stderr, "Fusion Error %d\n", ret );
          return;
     }


     /* ref up/down (local) */
     BENCH_START();

     BENCH_LOOP() {
          fusion_ref_up( &ref, false );
          fusion_ref_down( &ref, false );
     }

     BENCH_STOP();

     printf( "ref up/down (local)                   -> %8.2f k/sec\n", BENCH_RESULT() );


     /* ref up/down (global) */
     BENCH_START();

     BENCH_LOOP() {
          fusion_ref_up( &ref, true );
          fusion_ref_down( &ref, true );
     }

     BENCH_STOP();

     printf( "ref up/down (global)                  -> %8.2f k/sec\n", BENCH_RESULT() );


     fusion_ref_destroy( &ref );

     printf( "\n" );
}

static void
bench_property( void )
{
     DirectResult   ret;
     FusionProperty property;

     ret = fusion_property_init( &property, world );
     if (ret) {
          fprintf( stderr, "Fusion Error %d\n", ret );
          return;
     }


     /* property lease/cede */
     BENCH_START();

     BENCH_LOOP() {
          fusion_property_lease( &property );
          fusion_property_cede( &property );
     }

     BENCH_STOP();

     printf( "property lease/cede                   -> %8.2f k/sec\n", BENCH_RESULT() );


     fusion_property_destroy( &property );

     printf( "\n" );
}

static void
bench_skirmish( void )
{
     DirectResult   ret;
     FusionSkirmish skirmish;

     ret = fusion_skirmish_init( &skirmish, "Benchmark", world );
     if (ret) {
          fprintf( stderr, "Fusion Error %d\n", ret );
          return;
     }


     /* skirmish prevail/dismiss */
     BENCH_START();

     BENCH_LOOP() {
          fusion_skirmish_prevail( &skirmish );
          fusion_skirmish_dismiss( &skirmish );
     }

     BENCH_STOP();

     printf( "skirmish prevail/dismiss              -> %8.2f k/sec\n", BENCH_RESULT() );


     fusion_skirmish_destroy( &skirmish );

     printf( "\n" );
}

static void *
prevail_dismiss_loop( void *arg )
{
     FusionSkirmish *skirmish = (FusionSkirmish *) arg;

     BENCH_LOOP() {
          fusion_skirmish_prevail( skirmish );
          fusion_skirmish_dismiss( skirmish );
     }

     return NULL;
}

static void
bench_skirmish_threaded( void )
{
     int            i;
     DirectResult   ret;
     FusionSkirmish skirmish;

     ret = fusion_skirmish_init( &skirmish, "Threaded Benchmark", world );
     if (ret) {
          fprintf( stderr, "Fusion Error %d\n", ret );
          return;
     }


     /* skirmish prevail/dismiss (2-5 threads) */
     for (i=2; i<=5; i++) {
          int       t;
          pthread_t threads[i];

          BENCH_START();

          for (t=0; t<i; t++)
               pthread_create( &threads[t], NULL, prevail_dismiss_loop, &skirmish );

          for (t=0; t<i; t++)
               pthread_join( threads[t], NULL );

          BENCH_STOP();

          printf( "skirmish prevail/dismiss (%d threads)  -> %8.2f k/sec\n", i, BENCH_RESULT() );
     }


     fusion_skirmish_destroy( &skirmish );

     printf( "\n" );
}

static void *
mutex_lock_unlock_loop( void *arg )
{
     pthread_mutex_t *lock = (pthread_mutex_t *) arg;

     BENCH_LOOP() {
          pthread_mutex_lock( lock );
          pthread_mutex_unlock( lock );
     }

     return NULL;
}

static void
bench_mutex_threaded( void )
{
     int             i;
     pthread_mutex_t lock = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;


     /* mutex lock/unlock (2-5 threads) */
     for (i=2; i<=5; i++) {
          int       t;
          pthread_t threads[i];

          BENCH_START();

          for (t=0; t<i; t++)
               pthread_create( &threads[t], NULL, mutex_lock_unlock_loop, &lock );

          for (t=0; t<i; t++)
               pthread_join( threads[t], NULL );

          BENCH_STOP();

          printf( "mutex lock/unlock (rec., %d threads)   -> %8.2f k/sec\n", i, BENCH_RESULT() );
     }


     pthread_mutex_destroy( &lock );

     printf( "\n" );
}

static void
bench_mutex( void )
{
     pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
     pthread_mutex_t rmutex = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;


     /* pthread_mutex lock/unlock */
     BENCH_START();

     BENCH_LOOP() {
          pthread_mutex_lock( &mutex );
          pthread_mutex_unlock( &mutex );
     }

     BENCH_STOP();

     printf( "mutex lock/unlock                     -> %8.2f k/sec\n", BENCH_RESULT() );


     /* pthread_mutex lock/unlock */
     BENCH_START();

     BENCH_LOOP() {
          pthread_mutex_lock( &rmutex );
          pthread_mutex_unlock( &rmutex );
     }

     BENCH_STOP();

     printf( "mutex lock/unlock (recursive)         -> %8.2f k/sec\n", BENCH_RESULT() );


     pthread_mutex_destroy( &mutex );
     pthread_mutex_destroy( &rmutex );

     printf( "\n" );
}

static void
bench_flock( void )
{
     int   fd;
     FILE *tmp;

     tmp = tmpfile();
     if (!tmp) {
          perror( "tmpfile()" );
          return;
     }

     fd = fileno( tmp );
     if (fd < 0) {
          perror( "fileno()" );
          fclose( tmp );
          return;
     }

     BENCH_START();

     BENCH_LOOP() {
          flock( fd, LOCK_EX );
          flock( fd, LOCK_UN );
     }

     BENCH_STOP();

     printf( "flock lock/unlock                     -> %8.2f k/sec\n", BENCH_RESULT() );
     printf( "\n" );

     fclose( tmp );
}

static void
bench_shmpool( bool debug )
{
     DirectResult  ret;
     void         *mem[256];
     const int     sizes[8] = { 12, 36, 200, 120, 39, 3082, 8, 1040 };

     FusionSHMPoolShared *pool;

     ret = fusion_shm_pool_create( world, "Benchmark Pool", 524288, debug, &pool );
     if (ret) {
          DirectFBError( "fusion_shm_pool_create() failed", ret );
          return;
     }

     BENCH_START();

     BENCH_LOOP() {
          int i;

          for (i=0; i<128; i++)
               mem[i] = SHMALLOC( pool, sizes[i&7] );

          for (i=0; i<64; i++)
               SHFREE( pool, mem[i] );

          for (i=128; i<192; i++)
               mem[i] = SHMALLOC( pool, sizes[i&7] );

          for (i=64; i<128; i++)
               SHFREE( pool, mem[i] );

          for (i=192; i<256; i++)
               mem[i] = SHMALLOC( pool, sizes[i&7] );

          for (i=128; i<256; i++)
               SHFREE( pool, mem[i] );
     }

     BENCH_STOP();

     printf( "shm pool alloc/free %s           -> %8.2f k/sec\n",
             debug ? "(debug)" : "       ", BENCH_RESULT_BY(256) );

     fusion_shm_pool_destroy( world, pool );
}

int
main( int argc, char *argv[] )
{
     DirectResult ret;

     /* Initialize DirectFB. */
     ret = DirectFBInit( &argc, &argv );
     if (ret)
          return DirectFBError( "DirectFBInit()", ret );

     dfb_system_lookup();

     ret = fusion_enter( -1, 0, FER_MASTER, &world );
     if (ret)
          return DirectFBError( "fusion_enter()", ret );

     printf( "\n" );

#if FUSION_BUILD_MULTI
     printf( "Fusion Benchmark (Multi Application Core)\n" );
#else
     printf( "Fusion Benchmark (Single Application Core)\n" );
#endif

     printf( "\n" );

     bench_flock();

     bench_mutex();
     bench_mutex_threaded();

     bench_skirmish();
     bench_skirmish_threaded();

     //bench_spinlock_threaded();

     bench_property();

     bench_ref();

     bench_reactor();

     bench_shmpool( false );
     bench_shmpool( true );

     printf( "\n" );

     fusion_exit( world, false );

     return 0;
}