summaryrefslogtreecommitdiff
path: root/stan/v4l2-decode/tracer.c
blob: a3201039e37f13972497363aae23955df2ec4fff (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

#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>

#include "tracer.h"
#include "common.h"

int tracer_init(struct instance *i)
{
	i->stats = calloc(STAT_BUFS, sizeof(struct buf_stats));
	if (!i->stats)
		return -1;

	return 0;
}

void tracer_deinit(struct instance *i)
{
	if (i->stats)
		free(i->stats);
}

void tracer_buf_start(struct instance *i, unsigned int type)
{
	struct buf_stats *stats = i->stats;

	if (stats->dqbuf_counter >= STAT_BUFS ||
	    stats->qbuf_counter >= STAT_BUFS)
		return;

	if (stats->dqbuf_counter == 0)
		gettimeofday(&stats->start, NULL);

	if (type == TYPE_DQBUF)
		gettimeofday(&stats[stats->dqbuf_counter].dqbuf_start, NULL);
	else
		gettimeofday(&stats[stats->qbuf_counter].qbuf_start, NULL);
}

void tracer_buf_finish(struct instance *i, unsigned int type)
{
	struct buf_stats *stats = i->stats;

	if (stats->dqbuf_counter >= STAT_BUFS ||
	    stats->qbuf_counter >= STAT_BUFS)
		return;

	if (type == TYPE_DQBUF) {
		gettimeofday(&stats[stats->dqbuf_counter].dqbuf_end, NULL);
		stats->dqbuf_counter++;
	} else {
		gettimeofday(&stats[stats->qbuf_counter].qbuf_end, NULL);
		stats->qbuf_counter++;
	}

	gettimeofday(&stats->end, NULL);
}

void tracer_show(struct instance *inst)
{
	struct buf_stats *stats = inst->stats;
	unsigned long long delta, time, fps;
	unsigned int last = stats->dqbuf_counter - 1;

	delta = (stats[last].dqbuf_end.tv_sec * 1000000 +
		 stats[last].dqbuf_end.tv_usec);

	delta -=(stats[0].dqbuf_end.tv_sec * 1000000 +
		 stats[0].dqbuf_end.tv_usec);

	time = delta / last; /* time per frame in us */
	fps = 1000000 / time;

	fprintf(stdout, "%lld fps (%lld ms)\n", fps, time / 1000);

	time = stats->end.tv_sec * 1000000 + stats->end.tv_usec;
	time -= stats->start.tv_sec * 1000000 + stats->start.tv_usec;

	fprintf(stdout, "total time %lld ms (%lld)\n", time / 1000,
		last * 1000000 / time);
}