source: libcaca/trunk/caca/prof.c @ 4094

Last change on this file since 4094 was 4094, checked in by Sam Hocevar, 10 years ago

Gather profiling information in caca_refresh_display() and caca_get_event().

  • Property svn:keywords set to Id
File size: 2.7 KB
Line 
1/*
2 *  libcaca       Colour ASCII-Art library
3 *  Copyright (c) 2009 Sam Hocevar <sam@hocevar.net>
4 *                All Rights Reserved
5 *
6 *  $Id: prof.c 4094 2009-12-06 21:21:40Z sam $
7 *
8 *  This library is free software. It comes without any warranty, to
9 *  the extent permitted by applicable law. You can redistribute it
10 *  and/or modify it under the terms of the Do What The Fuck You Want
11 *  To Public License, Version 2, as published by Sam Hocevar. See
12 *  http://sam.zoy.org/wtfpl/COPYING for more details.
13 */
14
15/*
16 *  This file contains profiling functions. They are not supposed to be
17 *  built with release versions.
18 */
19
20#include "config.h"
21
22#if !defined(__KERNEL__)
23#   include <stdio.h>
24#   include <stdarg.h>
25#   include <stdlib.h>
26#endif
27
28#include "caca.h"
29#include "caca_internals.h"
30
31#if defined PROF
32static struct caca_stat **stats = NULL;
33static int nstats = 0;
34
35void _caca_dump_stats(void)
36{
37    int i, j;
38
39    fprintf(stderr, "** libcaca profiling stats **\n");
40
41    for (i = 0; i < nstats; i++)
42    {
43         int64_t total = 0;
44
45         for (j = 0; j < STAT_VALUES; j++)
46             total += stats[i]->itable[j];
47
48         fprintf(stderr, " %s: last %i mean %i sliding mean %i\n",
49                 stats[i]->name, stats[i]->itable[0],
50                 (int)((total + STAT_VALUES / 2) / STAT_VALUES),
51                 stats[i]->imean);
52/*fprintf(stderr, "%i %i %i %i %i %i %i %i %i %i %i %i %i %i %i %i %i %i %i %i\n",
53stats[i]->itable[0], stats[i]->itable[1], stats[i]->itable[2],
54stats[i]->itable[3], stats[i]->itable[4], stats[i]->itable[5],
55stats[i]->itable[6], stats[i]->itable[7], stats[i]->itable[8],
56stats[i]->itable[9], stats[i]->itable[10], stats[i]->itable[11],
57stats[i]->itable[12], stats[i]->itable[13], stats[i]->itable[14],
58stats[i]->itable[15], stats[i]->itable[16], stats[i]->itable[17],
59stats[i]->itable[18], stats[i]->itable[19]);*/
60    }
61
62    fprintf(stderr, "** %i counters dumped **\n", nstats);
63}
64
65void _caca_init_stat(struct caca_stat *s, const char *format, ...)
66{
67    int i;
68
69    s->name = malloc(128 * sizeof(char));
70    va_list args;
71    va_start(args, format);
72    vsnprintf(s->name, 128, format, args);
73    s->name[127] = '\0';
74    va_end(args);
75
76    nstats++;
77    stats = realloc(stats, nstats * sizeof(struct caca_stat *));
78    stats[nstats - 1] = s;
79
80    for (i = 0; i < STAT_VALUES; i++)
81        s->itable[i] = 0;
82    s->imean = 0;
83}
84
85void _caca_fini_stat(struct caca_stat *s)
86{
87    int i, j;
88
89    for (i = 0; i < nstats; i++)
90    {
91        if (stats[i] == s)
92        {
93            free(stats[i]->name);
94
95            for (j = i + 1; j < nstats; j++)
96                stats[j - 1] = stats[j];
97            nstats--;
98            stats = realloc(stats, nstats * sizeof(struct caca_stats *));
99
100            return;
101        }
102    }
103}
104
105#endif
106
Note: See TracBrowser for help on using the repository browser.