summaryrefslogtreecommitdiff
path: root/wl1271/platforms/os/common/src/tracebuf.c
blob: 5a0f1569e62f815ded9a404b689d8fa9aff76fdc (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
/*
 * tracebuf.c
 *
 * Copyright(c) 1998 - 2009 Texas Instruments. All rights reserved.      
 * All rights reserved.                                                  
 *                                                                       
 * Redistribution and use in source and binary forms, with or without    
 * modification, are permitted provided that the following conditions    
 * are met:                                                              
 *                                                                       
 *  * Redistributions of source code must retain the above copyright     
 *    notice, this list of conditions and the following disclaimer.      
 *  * Redistributions in binary form must reproduce the above copyright  
 *    notice, this list of conditions and the following disclaimer in    
 *    the documentation and/or other materials provided with the         
 *    distribution.                                                      
 *  * Neither the name Texas Instruments nor the names of its            
 *    contributors may be used to endorse or promote products derived    
 *    from this software without specific prior written permission.      
 *                                                                       
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS   
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT     
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT  
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT      
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT   
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

/*
 * Benchmark tracing utility
 */

#include "osApi.h"
#include "tracebuf.h"
#include "tracebuf_api.h"
#include "report.h"

typedef struct {
      unsigned long loc;/* trace entry identification */
      unsigned long ts;/* Timestamp */
      unsigned long p1; /* Parameter 1 */
      unsigned long p2; /* Parameter 2 */
      char msg[MAX_TB_MSG];
} tb_entry_t;

typedef struct {
      int pos;
      int count;
      int print_pos;
      int nusers;
      unsigned long self_delay;
      unsigned long options;
      tb_entry_t entry[1]; /* Array of entries */
} tb_control_t;

static tb_control_t *tb_control;

static  int tb_control_size(void)
{
   return TI_FIELD_OFFSET(tb_control_t, entry) + sizeof(tb_entry_t)*TB_NUM_ENTRIES;
}


/* Initialization */
int tb_init(unsigned long options)
{
   if (tb_control)
   {
      ++tb_control->nusers;
      return 0;
   }
   tb_control = (tb_control_t *)TB_MALLOC(tb_control_size());
   if (!tb_control)
      return -1;
   memset(tb_control, 0, tb_control_size());
   tb_control->nusers = 1;

   /* Measure self-delay */
   tb_trace(0, 0, 0);
   tb_trace(0, 0, 0);
   tb_control->self_delay = tb_control->entry[1].ts - tb_control->entry[0].ts;
   tb_control->pos = tb_control->count = 0;
   tb_control->options = options;
   return 0;
}

/* De-initialization */
void tb_destroy(void)
{
   if (--tb_control->nusers)
      return;
   TB_FREE(tb_control );
}

static int tb_next(void)
{
    int pos;
    if (!tb_control || tb_control->print_pos)
       return -1;
    pos = tb_control->pos;
    tb_control->pos = (pos+1) % TB_NUM_ENTRIES;
    ++tb_control->count;

    tb_control->entry[tb_control->pos].ts = 
    tb_control->entry[tb_control->pos].loc= 
    tb_control->entry[tb_control->pos].p1 = 
    tb_control->entry[tb_control->pos].p2 = 0xffffffff;

    return pos;
}
static void tb_autoprint(void)
{
    if ((tb_control->pos == 0) && (tb_control->count))
    {
        if (tb_control->options & TB_OPTION_PRINTONCE)
        {
            tb_printf();
            tb_reset_option(TB_OPTION_PRINTONCE);
        }
        else if (tb_control->options & TB_OPTION_AUTOPRINT)
        {
            tb_printf();
        }
    }
}

/* Add trace entry. not safe, but will do */
int tb_trace(int loc, unsigned long p1, unsigned long p2)
{
   int pos;

   if ((tb_control->options & TB_OPTION_STOP) || ((pos = tb_next()) < 0))
   {
       return -1;
   }
   tb_control->entry[pos].ts = os_timeStampUs(NULL);
   tb_control->entry[pos].loc= loc;
   tb_control->entry[pos].p1 = p1;
   tb_control->entry[pos].p2 = p2;

   return pos;
}
void tb_dump(void)
{
	int j, pos;

	WLAN_OS_REPORT(("Trace Dump:\n"));
	WLAN_OS_REPORT(("===========\n\n"));
    if (tb_control->count < TB_NUM_ENTRIES)
    {
        pos = 0;
    }
    else
    {
        pos = (tb_control->pos + 1) % TB_NUM_ENTRIES;
    }
	for (j=0; (unsigned int)j < tb_min((unsigned int)TB_NUM_ENTRIES,(unsigned int)tb_control->count); j++)
	{
		WLAN_OS_REPORT(("%4i %08x %08x %08x %08x\n", j,
			(int)tb_control->entry[pos].ts,
			(int)tb_control->entry[pos].loc,
			(int)tb_control->entry[pos].p1,
			(int)tb_control->entry[pos].p2));
		pos = (pos+1) % TB_NUM_ENTRIES;
	}

}

int tb_sprintf(const char *format ,...)
{

	va_list ap;
    int pos;

    if ((tb_control->options & TB_OPTION_STOP) || ((pos = tb_next()) < 0))
    {
        return -1;
    }
    tb_control->entry[pos].loc = TB_ID;
	va_start(ap,format);
	vsprintf(&tb_control->entry[pos].msg[0], format, ap);
    tb_autoprint();
    return pos;
}

void tb_printf(void)
{
	int j, pos;
    unsigned long saved_options=tb_control->options;

    tb_set_option(TB_OPTION_STOP);
	WLAN_OS_REPORT(("Trace Dump:\n"));
	WLAN_OS_REPORT(("===========\n\n"));
    if (tb_control->count < TB_NUM_ENTRIES)
    {
        pos = 0;
    }
    else
    {
        pos = (tb_control->pos + 1) % TB_NUM_ENTRIES;
    }
	for (j=0; (unsigned int)j < tb_min((unsigned int)TB_NUM_ENTRIES,(unsigned int)tb_control->count); j++)
	{
		WLAN_OS_REPORT(("%4i id=0x%8x %s \n", j,
tb_control->entry[pos].loc, tb_control->entry[pos].msg));
		pos = (pos+1) % TB_NUM_ENTRIES;
	}
    tb_control->options = saved_options;
}
void tb_set_option(unsigned long option)
{
    tb_control->options |= option;
}

void tb_reset_option(unsigned long option)
{
    tb_control->options &= ~option;
}

void tb_scan(void)
{

  int j,k, Size, nAllocs=0, nFrees=0;
  unsigned long address, Allocs=0, Frees=0;

  for (j=0; j < TB_NUM_ENTRIES; j++)
  {
    Size = (int)tb_control->entry[j].p2;
    if (Size > 0) /* Alloc */
    {
      nAllocs += 1;
      Allocs  += Size;
      address = tb_control->entry[j].p1;
      for (k=j+1; k < TB_NUM_ENTRIES; k++)
      {
	if (address == tb_control->entry[k].p1)
	{
	  if (tb_control->entry[k].p2 != -Size)
	  {
	    TB_PRINTF("Bad free size at 0x%lx address = 0x%lx Size = %ld Allocated = %d\n", 
		   tb_control->entry[k].loc, tb_control->entry[k].p1, (long)tb_control->entry[k].p2, Size);
	  }
	  Frees  += tb_control->entry[k].p2;
	  nFrees += 1;
	  break;
	}
      }
      if (k == TB_NUM_ENTRIES)
      {
	TB_PRINTF("Memory leak at 0x%lx address = 0x%lx Size = %d\n", 
	       tb_control->entry[j].loc, address, Size);
      }
    } 
  }
  TB_PRINTF("tb_scan() Allocs = %ld nAllocs = %d Frees = %ld nFrees = %d\n", Allocs, nAllocs, Frees, nFrees);
}