summaryrefslogtreecommitdiff
path: root/pixman/pixman-perf-stat.c
blob: e270b53544a5e3a8c7aaeee5916340e5905e56e2 (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
538
539
540
541
/*
 * Copyright © 2011 SCore Corporation
 *
 * 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 (including the next
 * paragraph) 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.
 *
 * Author:  Siarhei Siamashka (siarhei.siamashka@nokia.com)
 * Author:  Taekyun Kim (podain77@gmail.com)
 */

/* Performance statistics analyzer.
 * This tool accumulate performance data for each composite path and
 * report the result at the end of the program.
 *
 * TODO: Defense against tool chain which does not support attribute constructor
 * TODO: Use debug channel rather than stdout
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include "pixman-private.h"

#include <stdio.h>
#include <stdlib.h>

#ifdef HAVE_GETTIMEOFDAY
#include <sys/time.h>
#else
#include <time.h>
#endif

#define PERFSTAT_MAX_ENTRIES	1024
#define PERFSTAT_LOG_CHANNEL	stdout

typedef enum
{
    PERFSTAT_COMPOSITE,
    PERFSTAT_FILL,
    PERFSTAT_BLT,
} perfstat_type_t;

typedef struct perfstat_entry	    perfstat_entry_t;
typedef struct perfstat_composite   perfstat_composite_t;
typedef struct perfstat_fill	    perfstat_fill_t;
typedef struct perfstat_blt	    perfstat_blt_t;

struct perfstat_composite
{
    pixman_op_t			    op;
    pixman_format_code_t	    src;
    uint32_t			    src_flags;
    pixman_format_code_t	    mask;
    uint32_t			    mask_flags;
    pixman_format_code_t	    dest;
    uint32_t			    dest_flags;
};

struct perfstat_fill
{
    int32_t			    bpp;
};

struct perfstat_blt
{
    int32_t			    src_bpp;
    int32_t			    dst_bpp;
};

struct perfstat_entry
{
    perfstat_type_t		    type;
    pixman_implementation_type_t    imp_type;

    /* API info. */
    union
    {
	perfstat_composite_t    composite;
	perfstat_fill_t	        fill;
	perfstat_blt_t		blt;
    } api;

    /* Performance data. */
    uint32_t			    images_count;
    uint32_t			    scanlines_count;
    uint64_t			    pixels_count;
    double			    time;
};

static pixman_mutex_t perfstat_mutex;
static perfstat_entry_t perfstat[PERFSTAT_MAX_ENTRIES];
static int perfstat_entry_count = 0;
static pixman_bool_t enable_perfstat = FALSE;

double
perfstat_get_time (void)
{
#ifdef HAVE_GETTIMEOFDAY
    struct timeval tv;

    gettimeofday (&tv, NULL);
    return (double)((int64_t)tv.tv_sec * 1000000 + tv.tv_usec) / 1000000.;
#else
    return (double)clock() / (double)CLOCKS_PER_SEC;
#endif
}

void
perfstat_add_composite (pixman_implementation_type_t	imp_type,
			pixman_op_t			op,
			pixman_format_code_t		src,
			uint32_t			src_flags,
			pixman_format_code_t		mask,
			uint32_t			mask_flags,
			pixman_format_code_t		dest,
			uint32_t			dest_flags,
			int32_t				width,
			int32_t				height,
			double				elapsed)
{
    int i;

    if (src == PIXMAN_unknown || mask == PIXMAN_unknown)
	return;

    if (!enable_perfstat)
	return;

    PIXMAN_MUTEX_LOCK (perfstat_mutex);

    for (i = 0; i < perfstat_entry_count; ++i)
    {
	if (perfstat[i].type == PERFSTAT_COMPOSITE &&
	    perfstat[i].imp_type == imp_type &&
	    perfstat[i].api.composite.op == op &&
	    perfstat[i].api.composite.src == src &&
	    perfstat[i].api.composite.src_flags == src_flags &&
	    perfstat[i].api.composite.mask == mask &&
	    perfstat[i].api.composite.mask_flags == mask_flags &&
	    perfstat[i].api.composite.dest == dest &&
	    perfstat[i].api.composite.dest_flags == dest_flags)
	{
	    perfstat[i].images_count++;
	    perfstat[i].scanlines_count += height;
	    perfstat[i].pixels_count += (uint64_t)width * height;
	    perfstat[i].time += elapsed;

	    PIXMAN_MUTEX_UNLOCK (perfstat_mutex);
	    return;
	}
    }

    if (perfstat_entry_count >= PERFSTAT_MAX_ENTRIES)
    {
	PIXMAN_MUTEX_UNLOCK (perfstat_mutex);
	return;
    }

    perfstat[perfstat_entry_count].type = PERFSTAT_COMPOSITE;
    perfstat[perfstat_entry_count].imp_type = imp_type;
    perfstat[perfstat_entry_count].api.composite.op = op;
    perfstat[perfstat_entry_count].api.composite.src = src;
    perfstat[perfstat_entry_count].api.composite.src_flags = src_flags;
    perfstat[perfstat_entry_count].api.composite.mask = mask;
    perfstat[perfstat_entry_count].api.composite.mask_flags = mask_flags;
    perfstat[perfstat_entry_count].api.composite.dest = dest;
    perfstat[perfstat_entry_count].api.composite.dest_flags = dest_flags;

    perfstat[perfstat_entry_count].images_count = 1;
    perfstat[perfstat_entry_count].scanlines_count = height;
    perfstat[perfstat_entry_count].pixels_count = (uint64_t)width * height;
    perfstat[perfstat_entry_count].time = elapsed;

    perfstat_entry_count++;

    PIXMAN_MUTEX_UNLOCK (perfstat_mutex);
}

void
perfstat_add_fill (pixman_implementation_type_t	imp_type,
		   int32_t			bpp,
		   int32_t			width,
		   int32_t			height,
		   double			elapsed)
{
    int i;

    if (!enable_perfstat)
	return;

    PIXMAN_MUTEX_LOCK (perfstat_mutex);

    for (i = 0; i < perfstat_entry_count; ++i)
    {
	if (perfstat[i].type == PERFSTAT_FILL &&
	    perfstat[i].imp_type == imp_type &&
	    perfstat[i].api.fill.bpp == bpp)
	{
	    perfstat[i].images_count++;
	    perfstat[i].scanlines_count += height;
	    perfstat[i].pixels_count += (uint64_t)width * height;
	    perfstat[i].time += elapsed;

	    PIXMAN_MUTEX_UNLOCK (perfstat_mutex);
	    return;
	}
    }

    if (perfstat_entry_count >= PERFSTAT_MAX_ENTRIES)
    {
	PIXMAN_MUTEX_UNLOCK (perfstat_mutex);
	return;
    }

    perfstat[perfstat_entry_count].type = PERFSTAT_FILL;
    perfstat[perfstat_entry_count].imp_type = imp_type;
    perfstat[perfstat_entry_count].api.fill.bpp = bpp;

    perfstat[perfstat_entry_count].images_count = 1;
    perfstat[perfstat_entry_count].scanlines_count = height;
    perfstat[perfstat_entry_count].pixels_count = (uint64_t)width * height;
    perfstat[perfstat_entry_count].time = elapsed;

    perfstat_entry_count++;

    PIXMAN_MUTEX_UNLOCK (perfstat_mutex);
}

void
perfstat_add_blt (pixman_implementation_type_t	imp_type,
		  int32_t			src_bpp,
		  int32_t			dst_bpp,
		  int32_t			width,
		  int32_t			height,
		  double			elapsed)
{
    int i;

    if (!enable_perfstat)
	return;

    PIXMAN_MUTEX_LOCK (perfstat_mutex);

    for (i = 0; i < perfstat_entry_count; ++i)
    {
	if (perfstat[i].type == PERFSTAT_BLT &&
	    perfstat[i].imp_type == imp_type &&
	    perfstat[i].api.blt.src_bpp == src_bpp &&
	    perfstat[i].api.blt.dst_bpp == dst_bpp)
	{
	    perfstat[i].images_count++;
	    perfstat[i].scanlines_count += height;
	    perfstat[i].pixels_count += (uint64_t)width * height;
	    perfstat[i].time += elapsed;

	    PIXMAN_MUTEX_UNLOCK (perfstat_mutex);
	    return;
	}
    }

    if (perfstat_entry_count >= PERFSTAT_MAX_ENTRIES)
    {
	PIXMAN_MUTEX_UNLOCK (perfstat_mutex);
	return;
    }

    perfstat[perfstat_entry_count].type = PERFSTAT_BLT;
    perfstat[perfstat_entry_count].imp_type = imp_type;
    perfstat[perfstat_entry_count].api.blt.src_bpp = src_bpp;
    perfstat[perfstat_entry_count].api.blt.dst_bpp = dst_bpp;

    perfstat[perfstat_entry_count].images_count = 1;
    perfstat[perfstat_entry_count].scanlines_count = height;
    perfstat[perfstat_entry_count].pixels_count = (uint64_t)width * height;
    perfstat[perfstat_entry_count].time = elapsed;

    perfstat_entry_count++;

    PIXMAN_MUTEX_UNLOCK (perfstat_mutex);

}

#ifdef PIXMAN_ENABLE_PERFSTAT

static int
compare (const void *p1, const void *p2)
{
    const perfstat_entry_t *e1 = p1;
    const perfstat_entry_t *e2 = p2;

    if (e1->time > e2->time)
	return -1;
    else if (e1->time == e2->time)
	return 0;
    else
	return 1;
}

static const char*
get_implementation_string (pixman_implementation_type_t imp_type)
{
    switch (imp_type)
    {
    case PIXMAN_IMPLEMENTATION_UNKNOWN:
	return " Unknown";
    case PIXMAN_IMPLEMENTATION_GENERAL:
	return " General";
    case PIXMAN_IMPLEMENTATION_C_FAST_PATH:
	return "  C fast";
    case PIXMAN_IMPLEMENTATION_SSE2:
	return "    SSE2";
    case PIXMAN_IMPLEMENTATION_MMX:
	return "     MMX";
    case PIXMAN_IMPLEMENTATION_ARM_SIMD:
	return "ARM SIMD";
    case PIXMAN_IMPLEMENTATION_ARM_NEON:
	return "ARM NEON";
    default:
	break;
    }

    return "Unknown ";
}

static const char*
get_op_string (pixman_op_t op)
{
    switch (op)
    {
    case PIXMAN_OP_CLEAR:
	return "   clear";
    case PIXMAN_OP_SRC:
	return "     src";
    case PIXMAN_OP_DST:
	return "     dst";
    case PIXMAN_OP_OVER:
	return "    over";
    case PIXMAN_OP_OVER_REVERSE:
	return "over_rev";
    case PIXMAN_OP_IN:
	return "      in";
    case PIXMAN_OP_IN_REVERSE:
	return "  in_rev";
    case PIXMAN_OP_OUT:
	return "     out";
    case PIXMAN_OP_OUT_REVERSE:
	return " out_rev";
    case PIXMAN_OP_ATOP:
	return "    atop";
    case PIXMAN_OP_ATOP_REVERSE:
	return "atop_rev";
    case PIXMAN_OP_XOR:
	return "     xor";
    case PIXMAN_OP_ADD:
	return "     add";
    case PIXMAN_OP_SATURATE:
	return "     sat";
    default:
	return "     etc";
    };
}

static const char*
get_format_string (pixman_format_code_t format)
{
    switch ((uint32_t)format)
    {
    case PIXMAN_null:
	return "null";
    case PIXMAN_solid:
	return "   n";
    case PIXMAN_pixbuf:
	return " pix";
    case PIXMAN_rpixbuf:
	return "rpix";
    case PIXMAN_any:
	return " any";
    case PIXMAN_a1:
	return "   1";
    case PIXMAN_a8:
	return "   8";
    case PIXMAN_r5g6b5:
	return "0565";
    case PIXMAN_r8g8b8:
	return "0888";
    case PIXMAN_x8r8g8b8:
	return "x888";
    case PIXMAN_a8r8g8b8:
	return "8888";
    default:
	return " etc";
    }
}

static const char*
get_transform_string (uint32_t flag)
{
    if (flag & FAST_PATH_ID_TRANSFORM)
	return "non-scaled";
    else if (flag & FAST_PATH_SCALE_TRANSFORM)
    {
	if (flag & FAST_PATH_NEAREST_FILTER)
	    return "   nearest";
	else if (flag & FAST_PATH_BILINEAR_FILTER)
	    return "  bilinear";
    }
    else if (flag & FAST_PATH_ROTATE_90_TRANSFORM)
	return " rotate_90";
    else if (flag & FAST_PATH_ROTATE_180_TRANSFORM)
	return "rotate_180";
    else if (flag & FAST_PATH_ROTATE_270_TRANSFORM)
	return "rotate_270";

    return "    affine";
}

static const char*
get_repeat_string (uint32_t flag)
{
    if (flag & FAST_PATH_SAMPLES_COVER_CLIP_NEAREST)
	return "  cover";
    else if ((flag & FAST_PATH_NORMAL_REPEAT) == FAST_PATH_NORMAL_REPEAT)
	return " normal";
     else if ((flag & FAST_PATH_PAD_REPEAT) == FAST_PATH_PAD_REPEAT)
	return "    pad";
    else if ((flag & FAST_PATH_NONE_REPEAT) == FAST_PATH_NONE_REPEAT)
	return "   none";
    else if ((flag & FAST_PATH_REFLECT_REPEAT) == FAST_PATH_REFLECT_REPEAT)
	return "reflect";

    return "unknown";
}

pixman_bool_t
perfstat_is_enabled (void)
{
    return enable_perfstat;
}

static void __attribute__((constructor))
perfstat_constructor (void)
{
    if (getenv ("PIXMAN_ENABLE_PERFSTAT"))
	enable_perfstat = TRUE;

    PIXMAN_MUTEX_INIT (perfstat_mutex);
}

static void __attribute__((destructor))
perfstat_destructor (void)
{
    int i;
    int32_t total_images = 0;
    int32_t total_scanlines = 0;
    uint64_t total_pixels = 0;
    double total_time = 0.0;

    PIXMAN_MUTEX_FINI (perfstat_mutex);

    if (!enable_perfstat)
	return;

    /* sort entries */
    qsort (perfstat, perfstat_entry_count, sizeof(perfstat[0]), compare);

    fprintf (PERFSTAT_LOG_CHANNEL,
	     "[   #]  function    transform       op   src  mask   dst   repeat    "
	     "images  scanlines  pixels(Mpix)   time(s)  speed(Mpix/s)   backend\n");

    /* show performance statistics */
    for (i = 0; i < perfstat_entry_count; ++i)
    {
	fprintf (PERFSTAT_LOG_CHANNEL, "[%4d] ", i);

	if (perfstat[i].type == PERFSTAT_COMPOSITE)
	{
	    fprintf (PERFSTAT_LOG_CHANNEL,
		     "composite : "
		     "%s %s  %s  %s  %s  %s  ",
		     get_transform_string (perfstat[i].api.composite.src_flags),
		     get_op_string (perfstat[i].api.composite.op),
		     get_format_string (perfstat[i].api.composite.src),
		     get_format_string (perfstat[i].api.composite.mask),
		     get_format_string (perfstat[i].api.composite.dest),
		     get_repeat_string (perfstat[i].api.composite.src_flags));
	}
	else if (perfstat[i].type == PERFSTAT_FILL)
	{
	    fprintf (PERFSTAT_LOG_CHANNEL,
		     "     fill : "
		     "                                 %4d           ",
		     perfstat[i].api.fill.bpp);
	}
	else if (perfstat[i].type == PERFSTAT_BLT)
	{
	    fprintf (PERFSTAT_LOG_CHANNEL,
		     "      blt : "
		     "                   %4d           %4d          ",
		     perfstat[i].api.blt.src_bpp,
		     perfstat[i].api.blt.dst_bpp);
	}

	fprintf (PERFSTAT_LOG_CHANNEL,
		 "%8d  %9d      %8.3f  %8.3f      %8.3f   %s\n",
		 perfstat[i].images_count,
		 perfstat[i].scanlines_count,
		 perfstat[i].pixels_count / 1000000.0,
		 perfstat[i].time,
		 (perfstat[i].pixels_count / perfstat[i].time) / 1000000.0,
		 get_implementation_string (perfstat[i].imp_type));

	total_images += perfstat[i].images_count;
	total_scanlines += perfstat[i].scanlines_count;
	total_pixels += perfstat[i].pixels_count;
	total_time += perfstat[i].time;
    }

    printf ("Total images    : %d\n", total_images);
    printf ("Total scanlines : %d\n", total_scanlines);
    printf ("Total pixels    : %.3f Mpix\n", total_pixels / 1000000.0);
    printf ("Total time      : %.6f s\n", total_time);
}

#endif