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

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

Increase profiling timers' sliding mean accuracy.

  • Property svn:keywords set to Id
File size: 2.2 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 4095 2009-12-07 01:05:31Z 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 sliding mean %i smoothed mean %i\n",
49                 stats[i]->name, stats[i]->itable[0],
50                 (int)((total + STAT_VALUES / 2) / STAT_VALUES),
51                 (int)(stats[i]->imean / 64));
52    }
53
54    fprintf(stderr, "** %i counters dumped **\n", nstats);
55}
56
57void _caca_init_stat(struct caca_stat *s, const char *format, ...)
58{
59    int i;
60
61    s->name = malloc(128 * sizeof(char));
62    va_list args;
63    va_start(args, format);
64    vsnprintf(s->name, 128, format, args);
65    s->name[127] = '\0';
66    va_end(args);
67
68    nstats++;
69    stats = realloc(stats, nstats * sizeof(struct caca_stat *));
70    stats[nstats - 1] = s;
71
72    for (i = 0; i < STAT_VALUES; i++)
73        s->itable[i] = 0;
74    s->imean = 0;
75}
76
77void _caca_fini_stat(struct caca_stat *s)
78{
79    int i, j;
80
81    for (i = 0; i < nstats; i++)
82    {
83        if (stats[i] == s)
84        {
85            free(stats[i]->name);
86
87            for (j = i + 1; j < nstats; j++)
88                stats[j - 1] = stats[j];
89            nstats--;
90            stats = realloc(stats, nstats * sizeof(struct caca_stats *));
91
92            return;
93        }
94    }
95}
96
97#endif
98
Note: See TracBrowser for help on using the repository browser.