summaryrefslogtreecommitdiff
path: root/hifi/xaf/hifi-dpf/include/xa_profiler.h
blob: 675ff15c8a015add117a81e9983d2185f4756ef4 (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
/*******************************************************************************
* Copyright (C) 2018 Cadence Design Systems, Inc.
* 
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to use this Software with Cadence processor cores only and 
* not with any other processors and platforms, 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.

******************************************************************************/

#ifndef __XA_PROFILER_H__
#define __XA_PROFILER_H__

#if !defined(PROFILE) && __XCC__
#define PROFILE 1
#endif

#if !defined(PREFETCH_CTL) && __XCC__
#define PREFETCH_CTL 1
#endif

#ifdef PROFILE
#include <xtensa/hal.h>
#include <sys/times.h>

#include "xa_type_def.h"
#include "xa_timer.h"

TRACE_TAG(MCPS,1);

typedef struct XA_PROFILER {
#if 1
    unsigned long cstart;
    unsigned long cstop;
    unsigned long cycles;
    unsigned long g_output_bytes;
    unsigned long Peak;
    unsigned long Sum;

    unsigned long sample_rate;
    unsigned long channels;
    unsigned long pcm_width;
#else
    struct tms start;
    struct tms stop;
    double Peak;
    double Sum;
    long long cycles;
#endif
    int Peak_frame;
    int frame_count;
    long long output_bytes;
    long long total_cycles;
    long long total_samples;
    char *id;
} xa_profiler;

extern xa_profiler prof;

static inline void xa_compute_mcps(xa_profiler *p_prof, WORD32 samples_decoded, UWORD32 samp_freq, long long gen_strm_pos)
{
    double Ave=0.0, Curr;

    if (samples_decoded <= 0)
        return;

    p_prof->total_samples += samples_decoded;
    p_prof->frame_count++;

    clock_t cycles = p_prof->cycles;
    p_prof->total_cycles = 0;
    Curr = ((double) cycles / samples_decoded * samp_freq / 1000000);
    
    if (p_prof->frame_count > 1) {
        p_prof->Sum += Curr;
        Ave = p_prof->Sum / (p_prof->frame_count-1);

        if (p_prof->Peak < Curr) {
            p_prof->Peak = Curr;
            p_prof->Peak_frame = (p_prof->frame_count);
        }
    }                

    unsigned long long total_msec =
        (unsigned long long)((double)p_prof->total_samples / samp_freq * 1000.0);
    int msec = (int)(total_msec % 1000);
    unsigned long long total_seconds = total_msec / 1000;
    int seconds = (int)(total_seconds % 60);
    int minutes = (int)(total_seconds / 60);

#if 1
    TRACE(MCPS, _b("[%d] %d:%d.%d MCPS: %d Average: %d samples: %d\n"),
        p_prof->frame_count, (uint32_t)minutes, (uint32_t)seconds, (uint32_t)msec,
        (uint32_t)Curr, (uint32_t)Ave, samples_decoded);
#else
    TRACE(MCPS, _b("[%d|%lld] %d:%02d.%03d MCPS: %.2f Average: %.2f Peak: %.2f @ [%d] %s\n"),
        p_prof->frame_count, gen_strm_pos, minutes, seconds, msec,
        Curr, Ave, p_prof->Peak, p_prof->Peak_frame, p_prof->id);
#endif

    /* reset counters */
    p_prof->g_output_bytes = 0;
    p_prof->cycles = 0;
}

#define INIT_XA_PROFILER(x, a)                  do { memset(&x, 0, sizeof(xa_profiler)); \
                                                     x.sample_rate = 48000;\
                                                     x.channels = 2;\
                                                     x.pcm_width = 16;\
                                                     x.id = a; } while(0)

#define START_TIME_XA_PROFILER(x)               do { set_ccount(0); \
                                                     x.cstart=read_ccount(); } while(0)
#define STOP_TIME_XA_PROFILER(x)                do { x.cstop =read_ccount(); \
                                                     x.cycles += (x.cstop - x.cstart); } while(0)
#define COMPUTE_MHZ_XA_PROFILER(x, a, b, c)        do { xa_compute_mcps(&x, a, b, c); } while(0)
#define SUMMARY_XA_PROFILER(x)                  do { fprintf(stdout,"\n%5s Peak MCPS = %f\n", x.id, x.Peak); \
                                                     fprintf(stdout,"%5s Peak frame = %d\n", x.id, x.Peak_frame); \
                                                     fprintf(stdout,"%5s Average MCPS = %f\n", x.id, (x.frame_count < 2) ? 0 : (x.Sum/(x.frame_count-1))); } while(0) 

#else

typedef struct XA_PROFILER {    
    int place_holder;
} xa_profiler;

#define INIT_XA_PROFILER(x, a)                  do {} while(0)
#define START_TIME_XA_PROFILER(x)               do {} while(0)
#define STOP_TIME_XA_PROFILER(x)                do {} while(0)
#define COMPUTE_MHZ_XA_PROFILER(x, a, b)        do {} while(0)
#define SUMMARY_XA_PROFILER(x)                  do {} while(0)
#endif 

#ifdef PREFETCH_CTL
#define PREFETCH_AGGRESSIVE(x)                  do { x = xthal_set_cache_prefetch ((XTHAL_DCACHE_PREFETCH_HIGH | XTHAL_ICACHE_PREFETCH_HIGH)); \
                                                   } while(0)
#define PREFETCH_RESTORE(x)                     do { xthal_set_cache_prefetch (x); } while(0)
#else
#define PREFETCH_AGGRESSIVE(x)                  do {} while(0)
#define PREFETCH_RESTORE(x)                     do {} while(0)
#endif

#endif /* __XA_PROFILER_H__ */