OpenCores
URL https://opencores.org/ocsvn/ao486/ao486/trunk

Subversion Repositories ao486

[/] [ao486/] [trunk/] [syn/] [components/] [ps2/] [software/] [exe_bsp/] [HAL/] [src/] [alt_gmon.c] - Blame information for rev 2

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 alfik
/******************************************************************************
2
*                                                                             *
3
* License Agreement                                                           *
4
*                                                                             *
5
* Copyright (c) 2003-2005 Altera Corporation, San Jose, California, USA.      *
6
* All rights reserved.                                                        *
7
*                                                                             *
8
* Permission is hereby granted, free of charge, to any person obtaining a     *
9
* copy of this software and associated documentation files (the "Software"),  *
10
* to deal in the Software without restriction, including without limitation   *
11
* the rights to use, copy, modify, merge, publish, distribute, sublicense,    *
12
* and/or sell copies of the Software, and to permit persons to whom the       *
13
* Software is furnished to do so, subject to the following conditions:        *
14
*                                                                             *
15
* The above copyright notice and this permission notice shall be included in  *
16
* all copies or substantial portions of the Software.                         *
17
*                                                                             *
18
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR  *
19
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,    *
20
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
21
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER      *
22
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING     *
23
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER         *
24
* DEALINGS IN THE SOFTWARE.                                                   *
25
*                                                                             *
26
* This agreement shall be governed in all respects by the laws of the State   *
27
* of California and by the laws of the United States of America.              *
28
*                                                                             *
29
******************************************************************************/
30
 
31
#include <assert.h>
32
#include <string.h>
33
#include <unistd.h>
34
 
35
#include "priv/nios2_gmon_data.h"
36
 
37
#include "sys/alt_irq.h"
38
#include "sys/alt_alarm.h"
39
 
40
 
41
/* Macros */
42
 
43
/* How large should the bins be which we use to generate the histogram */
44
#define PCSAMPLE_BYTES_PER_BUCKET 32
45
 
46
#define NIOS2_READ_EA(dest)  __asm__ ("mov %0, ea" : "=r" (dest))
47
 
48
/* The compiler inserts calls to mcount() at the start of
49
 * every function call. The structure mcount_fn_arc records t
50
 * he return address of the function called (in from_pc)
51
 * and the return address of the mcount function
52
 * (in self_pc). The number of times this arc is executed is
53
 * recorded in the field count.
54
 */
55
struct mcount_fn_arc
56
{
57
  struct mcount_fn_arc * next;
58
  void * from_pc;
59
  unsigned int count;
60
};
61
 
62
/* We need to maintain a list of pointers to the heads of each adjacency
63
 * list so that we can find them when writing out the gmon.out file. Since
64
 * we don't know at the start of program execution how many functions will
65
 * be called we use a list structure to do this.
66
 */
67
struct mcount_fn_entry
68
{
69
  struct mcount_fn_entry * next;
70
  void * self_pc;
71
  struct mcount_fn_arc * arc_head;
72
};
73
 
74
/* function prototypes */
75
 
76
void __mcount_record(void * self_pc, void * from_pc, struct mcount_fn_entry * fn_entry, struct mcount_fn_entry * * fn_head) __attribute__ ((no_instrument_function));
77
 
78
static __inline__ void * mcount_allocate(unsigned int size) __attribute__ ((no_instrument_function));
79
static int nios2_pcsample_init(void) __attribute__ ((no_instrument_function));
80
static alt_u32 nios2_pcsample(void* alarm) __attribute__ ((no_instrument_function));
81
 
82
/* global variables */
83
 
84
/* stext and etext are defined in the linker script */
85
extern char stext[];
86
extern char etext[];
87
 
88
/* Is the PC sampling stuff enabled yet? */
89
static int pcsample_need_init = 1;
90
 
91
#define HASH_BUCKETS 64 /* Must be a power of 2 */
92
 
93
/* This points to the list of adjacency list pointers. */
94
struct mcount_fn_entry * __mcount_fn_head[HASH_BUCKETS];
95
 
96
/* pointer to the in-memory buffer containing the histogram */
97
static unsigned short* s_pcsamples = 0;
98
 
99
/* the address of the start and end of text section */
100
static const unsigned int s_low_pc  = (unsigned int)stext;
101
static const unsigned int s_high_pc = (unsigned int)etext;
102
 
103
/* the alarm structure to register for pc sampling */
104
static alt_alarm s_nios2_pcsample_alarm;
105
 
106
unsigned int alt_gmon_data[GMON_DATA_SIZE] =
107
{
108
  0x6e6f6d67, /* "gmon" */
109
  GMON_DATA_SIZE,
110
  0,
111
  (unsigned int)stext,
112
  (unsigned int)etext,
113
  PCSAMPLE_BYTES_PER_BUCKET,
114
  0,
115
  (unsigned int)__mcount_fn_head,
116
  (unsigned int)(__mcount_fn_head + HASH_BUCKETS)
117
};
118
 
119
/* This holds the current slab of memory we're allocating out of */
120
static char * mcount_slab_ptr = 0;
121
static int    mcount_slab_size = 0;
122
 
123
#define MCOUNT_SLAB_INCREMENT 1020
124
 
125
 
126
/*
127
 * We can't use malloc to allocate memory because that's too complicated, and
128
 * can't be called at interrupt time.  Use the lower level allocator instead
129
 * because that's interrupt safe (and because we never free anything).
130
 *
131
 * For speed, we allocate a block of data at once.
132
 */
133
static __inline__ void * mcount_allocate(unsigned int size)
134
{
135
  void * data;
136
 
137
  if (size > mcount_slab_size)
138
  {
139
    mcount_slab_ptr = sbrk(MCOUNT_SLAB_INCREMENT);
140
    mcount_slab_size = MCOUNT_SLAB_INCREMENT;
141
  }
142
 
143
  data = mcount_slab_ptr;
144
  mcount_slab_ptr += size;
145
  mcount_slab_size -= size;
146
 
147
  return data;
148
}
149
 
150
 
151
/*
152
 * Add the arc with the values of frompc and topc given to the graph.
153
 * This function might be called at interrupt time so must be able to
154
 * cope with reentrancy.
155
 *
156
 * The fast case, where we have already allocated a function arc, has been
157
 * handled by the assmebler code.
158
 */
159
void __mcount_record(void * self_pc, void * from_pc, struct mcount_fn_entry * fn_entry, struct mcount_fn_entry * * fn_head)
160
{
161
  alt_irq_context context;
162
  struct mcount_fn_arc * arc_entry;
163
 
164
  /* Keep trying to start up the PC sampler until it is running.
165
   * (It can't start until the timer is going).
166
   */
167
  if (pcsample_need_init)
168
  {
169
    pcsample_need_init = 0;
170
    pcsample_need_init = nios2_pcsample_init();
171
  }
172
 
173
  /*
174
   * We must disable interrupts around the allocation and the list update to
175
   * prevent corruption if the instrumented function is re-entrant.
176
   *
177
   * It's safe for the code above to be stepping through the chain and be
178
   * interrupted by this code modifying it - there is an edge case which will
179
   * leave two copies of the same arc on the list (both with count=1), but
180
   * this is dealt with on the host.
181
   */
182
  context = alt_irq_disable_all();
183
 
184
  if (fn_entry == NULL)
185
  {
186
    /* Add it to the list of functions we must output later. */
187
    fn_entry = (struct mcount_fn_entry *)mcount_allocate(sizeof(struct mcount_fn_entry));
188
 
189
    fn_entry->self_pc = self_pc;
190
    fn_entry->arc_head = NULL;
191
 
192
    fn_entry->next = *fn_head;
193
    *fn_head = fn_entry;
194
  }
195
 
196
  /* We will need a new list entry - if there was a list entry before
197
   * then the assembler code would have handled it. */
198
  arc_entry = (struct mcount_fn_arc *)mcount_allocate(sizeof(struct mcount_fn_arc));
199
 
200
  arc_entry->from_pc = from_pc;
201
  arc_entry->count = 1;
202
 
203
  arc_entry->next = fn_entry->arc_head;
204
  fn_entry->arc_head = arc_entry;
205
 
206
  alt_irq_enable_all(context);
207
}
208
 
209
 
210
/*
211
 * nios2_pcsample_init starts profiling.
212
 * It is called the first time mcount is called, and on subsequent calls to
213
 * mcount until it returns zero. It initializes the pc histogram and turns on
214
 * timer driven pc sampling.
215
 */
216
static int nios2_pcsample_init(void)
217
{
218
  unsigned int pcsamples_size;
219
 
220
  /* We sample the PC every tick */
221
  unsigned int prof_rate = alt_ticks_per_second();
222
  if (prof_rate == 0)
223
    return 1;
224
 
225
  /* allocate the histogram buffer s_pcsamples */
226
  pcsamples_size = (s_high_pc - s_low_pc)/PCSAMPLE_BYTES_PER_BUCKET;
227
  s_pcsamples    = (unsigned short*)sbrk(pcsamples_size * sizeof(unsigned short));
228
 
229
  if (s_pcsamples != 0)
230
  {
231
    /* initialize the buffer to zero */
232
    memset(s_pcsamples, 0, pcsamples_size * sizeof(unsigned short));
233
 
234
    alt_gmon_data[GMON_DATA_PROFILE_DATA] = (int)s_pcsamples;
235
    alt_gmon_data[GMON_DATA_PROFILE_RATE] = prof_rate;
236
 
237
    /* Sample every tick (it's cheap) */
238
    alt_alarm_start(&s_nios2_pcsample_alarm, 1, nios2_pcsample, 0);
239
  }
240
 
241
  return 0;
242
}
243
 
244
 
245
/*
246
 * Sample the PC value and store it in the histogram
247
 */
248
static alt_u32 nios2_pcsample(void* context)
249
{
250
  unsigned int pc;
251
  unsigned int bucket;
252
 
253
  /* read the exception return address - this will be
254
   * inaccurate if there are nested interrupts but we
255
   * assume that this is rare and the inaccuracy will
256
   * not be great */
257
  NIOS2_READ_EA(pc);
258
 
259
  /*
260
   * If we're within the profilable range then increment the relevant
261
   * bucket in the histogram
262
   */
263
  if (pc >= s_low_pc && pc < s_high_pc && s_pcsamples != 0)
264
  {
265
    bucket = (pc - s_low_pc)/PCSAMPLE_BYTES_PER_BUCKET;
266
    s_pcsamples[bucket]++;
267
  }
268
 
269
  /* Sample every tick */
270
  return 1;
271
}
272
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.