Commit c695746d authored by julien.delange's avatar julien.delange
Browse files

finish to integrate gprof for LEON/RTEMS



git-svn-id: https://tecsw.estec.esa.int/svn/taste/trunk/po-hi-c@642 129961e7-ef38-4bb5-a8f7-c9a525a55882
parent 7b8fa421
......@@ -43,11 +43,6 @@ ifeq ($(ENABLE_DYNAMIC_CONFIGURATION),1)
CFLAGS+=-D__PO_HI_USE_DYNAMIC_CONFIGURATION
endif
ifdef USE_GPROF
CFLAGS+=-pg -D__PO_HI_USE_GPROF
LDFLAGS+=-pg
endif
ifdef TARGET_INCLUDE
INCLUDE=-I. -I$(RUNTIME_PATH)/include/ $(TARGET_INCLUDE)
else
......
......@@ -28,7 +28,7 @@ TARGET_TRANSPORT_SOURCES =
TARGET_CFLAGS = -DRTEMS_POSIX -DLEON_RTEMS
TARGET_INCLUDE = -I $(RUNTIME_PATH)/config/leon-rtems/
LD =
LD = sparc-rtems-ld
TARGET_LDFLAGS = -lrtemsbsp -lc
target-clean:
......@@ -48,3 +48,8 @@ define bsp-link-cxx
-o $(BINARY) $(LINK_OBJS) $(LINK_LIBS)
endef
ifdef USE_GPROF
TARGET_LDFLAGS+=-Wl,--wrap,Clock_isr
TARGET_CFLAGS+=-D__PO_HI_USE_GPROF
endif
......@@ -65,9 +65,7 @@ static char sccsid[] = "@(#)gmon.c 1.0 (E.S.A) 10/05/2010";
#include <rtems.h>
#include <bsp.h>
/*
* This was derived from above and modified to add different profiling info.
*/
#define CONFIG_OS_PROFILE_OVER_SERIAL 1
/*
* Histogram counters are unsigned shorts (according to the kernel).
......@@ -140,10 +138,10 @@ static char sccsid[] = "@(#)gmon.c 1.0 (E.S.A) 10/05/2010";
* the recording of calls via mcount().
*/
struct tostruct {
char *selfpc;
long count;
unsigned short link;
unsigned short order;
char *selfpc;
long count;
unsigned short link;
unsigned short order;
};
/*
......@@ -153,10 +151,10 @@ struct tostruct {
* header for ncnt - sizeof(struct phdr) bytes.
*/
struct phdr {
char *lpc; /* low program counter */
char *hpc; /* high program counter */
int ncnt; /* number of bytes of histogram counters minius
sizeof(struct phdr) that follow */
char *lpc; /* low program counter */
char *hpc; /* high program counter */
int ncnt; /* number of bytes of histogram counters minius
sizeof(struct phdr) that follow */
};
/*
......@@ -166,9 +164,9 @@ struct phdr {
* counters to the end of the file.
*/
struct rawarc {
unsigned long raw_frompc;
unsigned long raw_selfpc;
unsigned long raw_count;
unsigned long raw_frompc;
unsigned long raw_selfpc;
unsigned long raw_count;
};
/*
......@@ -179,8 +177,8 @@ struct rawarc {
*/
#define GMON_MAGIC 0xbeefbabe
struct gmon_data {
unsigned long type; /* constant for type of data following this struct */
unsigned long size; /* size in bytes of the data following this struct */
unsigned long type; /* constant for type of data following this struct */
unsigned long size; /* size in bytes of the data following this struct */
};
/*
......@@ -200,10 +198,10 @@ struct gmon_data {
*/
#define GMONTYPE_ARCS_ORDERS 3
struct rawarc_order {
unsigned long raw_frompc;
unsigned long raw_selfpc;
unsigned long raw_count;
unsigned long raw_order;
unsigned long raw_frompc;
unsigned long raw_selfpc;
unsigned long raw_count;
unsigned long raw_order;
};
/*
* The GMONTYPE_RLD_STATE gmon_data.type is for the rld_load()'ed state of the
......@@ -268,297 +266,363 @@ static char *s_profil = 0;
#define MSG "No space for profiling buffer(s)\n"
#define SAVE_L1( _pc_ ) \
do{ \
asm volatile( "mov %%l1, %0" : "=r" (_pc_) : "0" (_pc_) ); \
}while(0)
do{ \
asm volatile( "mov %%l1, %0" : "=r" (_pc_) : "0" (_pc_) ); \
}while(0)
static void moncontrol( int mode );
static rtems_isr profile_clock_isr(rtems_vector_number vector);
static rtems_isr Clock_isr(rtems_vector_number vector);
static void moncontrol( int mode )
{
if (mode) {
/* start */
s_profil = sbuf + sizeof(struct phdr);
s_profil_sz = ssiz - sizeof(struct phdr);
profiling = 0;
} else {
/* stop */
s_profil = 0;
s_profil_sz = 0;
profiling = 3;
}
if (mode) {
/* start */
s_profil = sbuf + sizeof(struct phdr);
s_profil_sz = ssiz - sizeof(struct phdr);
profiling = 0;
} else {
/* stop */
s_profil = 0;
s_profil_sz = 0;
profiling = 3;
}
}
static void _mcleanup()
{
int fromindex;
int endfrom;
char *frompc;
int toindex;
struct rawarc rawarc;
int ret = -1;
moncontrol(0);
int fromindex;
int endfrom;
char *frompc;
int toindex;
struct rawarc rawarc;
int ret = -1;
moncontrol(0);
#if (CONFIG_OS_PROFILE_OVER_SERIAL == 1)
ret = initialize_serial();
ret = initialize_serial();
#endif
if( ret >= 0 )
{
if( ret >= 0 )
{
#if (CONFIG_OS_PROFILE_OVER_SERIAL == 1)
write_serial( (uint8_t*)sbuf , (unsigned int)ssiz );
write_serial( (uint8_t*)sbuf , (unsigned int)ssiz );
#endif
}
fprintf( stderr , "[mcleanup] sbuf 0x%x ssiz %d\n" , (unsigned int)sbuf , (unsigned int)ssiz );
endfrom = s_textsize / (HASHFRACTION * sizeof(*froms));
for ( fromindex = 0 ; fromindex < endfrom ; fromindex++ ) {
if ( froms[fromindex] == 0 ) {
continue;
}
frompc = s_lowpc + (fromindex * HASHFRACTION * sizeof(*froms));
for (toindex=froms[fromindex]; toindex!=0; toindex=tos[toindex].link)
{
fprintf( stderr ,
"[mcleanup] frompc 0x%x selfpc 0x%x count %d\n" ,
(unsigned int)frompc ,
(unsigned int)tos[toindex].selfpc ,
(unsigned int)tos[toindex].count );
rawarc.raw_frompc = (unsigned long) frompc;
rawarc.raw_selfpc = (unsigned long) tos[toindex].selfpc;
rawarc.raw_count = tos[toindex].count;
if( ret >= 0 )
{
}
fprintf( stderr , "[mcleanup] sbuf 0x%x ssiz %d\n" , (unsigned int)sbuf , (unsigned int)ssiz );
endfrom = s_textsize / (HASHFRACTION * sizeof(*froms));
for ( fromindex = 0 ; fromindex < endfrom ; fromindex++ ) {
if ( froms[fromindex] == 0 ) {
continue;
}
frompc = s_lowpc + (fromindex * HASHFRACTION * sizeof(*froms));
for (toindex=froms[fromindex]; toindex!=0; toindex=tos[toindex].link)
{
fprintf( stderr ,
"[mcleanup] frompc 0x%x selfpc 0x%x count %d\n" ,
(unsigned int)frompc ,
(unsigned int)tos[toindex].selfpc ,
(unsigned int)tos[toindex].count );
rawarc.raw_frompc = (unsigned long) frompc;
rawarc.raw_selfpc = (unsigned long) tos[toindex].selfpc;
rawarc.raw_count = tos[toindex].count;
if( ret >= 0 )
{
#if (CONFIG_OS_PROFILE_OVER_SERIAL == 1)
write_serial( (uint8_t*)&rawarc , (unsigned int)sizeof(rawarc));
#endif
}
}
}
}
}
}
}
void monstartup(char *lowpc, char *highpc)
{
int monsize;
char *buffer;
printf("%s\n", sccsid);
/*
* round lowpc and highpc to multiples of the density we're using
* so the rest of the scaling (here and in gprof) stays in ints.
*/
lowpc = (char *)
ROUNDDOWN((unsigned) lowpc, HISTFRACTION*sizeof(HISTCOUNTER));
s_lowpc = lowpc;
highpc = (char *)
ROUNDUP((unsigned) highpc, HISTFRACTION*sizeof(HISTCOUNTER));
s_highpc = highpc;
s_textsize = highpc - lowpc;
monsize = (s_textsize / HISTFRACTION) + sizeof(struct phdr);
buffer = (char*)malloc( monsize );
if ( buffer == (char *) -1 )
{
printf("%s\n", MSG);
return;
}
froms = (unsigned short *) malloc( s_textsize / HASHFRACTION );
if ( froms == (unsigned short *) -1 )
{
printf("%s\n", MSG);
froms = 0;
return;
}
tolimit = s_textsize * ARCDENSITY / 100;
if ( tolimit < MINARCS ) {
tolimit = MINARCS;
} else if ( tolimit > 65534 ) {
tolimit = 65534;
}
tos = (struct tostruct *) malloc( tolimit * sizeof( struct tostruct ) );
if ( tos == (struct tostruct *) -1 )
{
printf("%s\n", MSG);
froms = 0;
tos = 0;
return;
}
tos[0].link = 0;
sbuf = buffer;
bzero( buffer, sizeof(buffer) );
ssiz = monsize;
( (struct phdr *) sbuf ) -> lpc = lowpc;
( (struct phdr *) sbuf ) -> hpc = highpc;
( (struct phdr *) sbuf ) -> ncnt = ssiz;
monsize -= sizeof(struct phdr);
if ( monsize <= 0 )
return;
printf("PC histrogram scale = %d\n", HISTFRACTION);
/* Register the new clock driver */
set_vector( profile_clock_isr, 0x18, 1 );
atexit( _mcleanup );
moncontrol(1);
int monsize;
char *buffer;
printf("%s\n", sccsid);
/*
* round lowpc and highpc to multiples of the density we're using
* so the rest of the scaling (here and in gprof) stays in ints.
*/
lowpc = (char *)
ROUNDDOWN((unsigned) lowpc, HISTFRACTION*sizeof(HISTCOUNTER));
s_lowpc = lowpc;
highpc = (char *)
ROUNDUP((unsigned) highpc, HISTFRACTION*sizeof(HISTCOUNTER));
s_highpc = highpc;
s_textsize = highpc - lowpc;
monsize = (s_textsize / HISTFRACTION) + sizeof(struct phdr);
buffer = (char*)malloc( monsize );
if ( buffer == (char *) -1 )
{
printf("%s\n", MSG);
return;
}
froms = (unsigned short *) malloc( s_textsize / HASHFRACTION );
if ( froms == (unsigned short *) -1 )
{
printf("%s\n", MSG);
froms = 0;
return;
}
tolimit = s_textsize * ARCDENSITY / 100;
if ( tolimit < MINARCS ) {
tolimit = MINARCS;
} else if ( tolimit > 65534 ) {
tolimit = 65534;
}
tos = (struct tostruct *) malloc( tolimit * sizeof( struct tostruct ) );
if ( tos == (struct tostruct *) -1 )
{
printf("%s\n", MSG);
froms = 0;
tos = 0;
return;
}
tos[0].link = 0;
sbuf = buffer;
bzero( buffer, sizeof(buffer) );
ssiz = monsize;
( (struct phdr *) sbuf ) -> lpc = lowpc;
( (struct phdr *) sbuf ) -> hpc = highpc;
( (struct phdr *) sbuf ) -> ncnt = ssiz;
monsize -= sizeof(struct phdr);
if ( monsize <= 0 )
return;
printf("PC histrogram scale = %d\n", HISTFRACTION);
/* Register the new clock driver */
set_vector( profile_clock_isr, 0x18, 1 );
atexit( _mcleanup );
moncontrol(1);
}
void mcount()
{
register char *selfpc;
register unsigned short *frompcindex;
register struct tostruct *top;
register struct tostruct *prevtop;
register long toindex;
static int first_call = 1;
if( first_call )
{
monstartup((char*)&text_start, (char*)&_endtext);
first_call = 0;
}
/*
* find the return address for mcount,
* and the return address for mcount's caller.
*/
/* selfpc = pc pushed by mcount call.
This identifies the function that was just entered. */
selfpc = (void *) __builtin_return_address (0);
/* frompcindex = pc in preceding frame.
This identifies the caller of the function just entered. */
frompcindex = (void *) __builtin_return_address (1);
/*
* check that we are profiling
* and that we aren't recursively invoked.
*/
if (profiling) {
goto out;
}
profiling++;
/*
* check that frompcindex is a reasonable pc value.
* for example: signal catchers get called from the stack,
* not from text space. too bad.
*/
frompcindex = (unsigned short *) ((long) frompcindex - (long) s_lowpc);
if ((unsigned long) frompcindex > s_textsize) {
goto done;
}
frompcindex =
&froms[((long) frompcindex) / (HASHFRACTION * sizeof(*froms))];
toindex = *frompcindex;
if (toindex == 0) {
/*
* first time traversing this arc
*/
toindex = ++tos[0].link;
if (toindex >= tolimit) {
goto overflow;
}
*frompcindex = toindex;
top = &tos[toindex];
top->selfpc = selfpc;
top->count = 1;
top->link = 0;
goto done;
}
top = &tos[toindex];
if (top->selfpc == selfpc) {
/*
* arc at front of chain; usual case.
*/
top->count++;
goto done;
}
/*
* have to go looking down chain for it.
* top points to what we are looking at,
* prevtop points to previous top.
* we know it is not at the head of the chain.
*/
for (; /* goto done */; ) {
if (top->link == 0) {
/*
* top is end of the chain and none of the chain
* had top->selfpc == selfpc.
* so we allocate a new tostruct
* and link it to the head of the chain.
*/
toindex = ++tos[0].link;
if (toindex >= tolimit) {
register char *selfpc;
register unsigned short *frompcindex;
register struct tostruct *top;
register struct tostruct *prevtop;
register long toindex;
static int first_call = 1;
if( first_call )
{
monstartup((char*)&text_start, (char*)&_endtext);
first_call = 0;
}
/*
* find the return address for mcount,
* and the return address for mcount's caller.
*/
/* selfpc = pc pushed by mcount call.
This identifies the function that was just entered. */
selfpc = (void *) __builtin_return_address (0);
/* frompcindex = pc in preceding frame.
This identifies the caller of the function just entered. */
frompcindex = (void *) __builtin_return_address (1);
/*
* check that we are profiling
* and that we aren't recursively invoked.
*/
if (profiling) {
goto out;
}
profiling++;
/*
* check that frompcindex is a reasonable pc value.
* for example: signal catchers get called from the stack,
* not from text space. too bad.
*/
frompcindex = (unsigned short *) ((long) frompcindex - (long) s_lowpc);
if ((unsigned long) frompcindex > s_textsize) {
goto done;
}
frompcindex =
&froms[((long) frompcindex) / (HASHFRACTION * sizeof(*froms))];
toindex = *frompcindex;
if (toindex == 0) {
/*
* first time traversing this arc
*/
toindex = ++tos[0].link;
if (toindex >= tolimit) {
goto overflow;
}
top = &tos[toindex];
top->selfpc = selfpc;
top->count = 1;
top->link = *frompcindex;
*frompcindex = toindex;
goto done;
}
/*
* otherwise, check the next arc on the chain.
*/
prevtop = top;
top = &tos[top->link];
if (top->selfpc == selfpc) {
/*
* there it is.
* increment its count
* move it to the head of the chain.
*/
top->count++;
toindex = prevtop->link;
prevtop->link = top->link;
top->link = *frompcindex;
*frompcindex = toindex;
goto done;
}
}
}
*frompcindex = toindex;
top = &tos[toindex];
top->selfpc = selfpc;
top->count = 1;
top->link = 0;
goto done;
}
top = &tos[toindex];
if (top->selfpc == selfpc) {
/*
* arc at front of chain; usual case.
*/
top->count++;
goto done;
}
/*
* have to go looking down chain for it.
* top points to what we are looking at,
* prevtop points to previous top.
* we know it is not at the head of the chain.
*/
for (; /* goto done */; ) {
if (top->link == 0) {
/*
* top is end of the chain and none of the chain
* had top->selfpc == selfpc.
* so we allocate a new tostruct
* and link it to the head of the chain.
*/
toindex = ++tos[0].link;
if (toindex >= tolimit) {
goto overflow;
}
top = &tos[toindex];
top->selfpc = selfpc;
top->count = 1;
top->link = *frompcindex;
*frompcindex = toindex;
goto done;
}
/*
* otherwise, check the next arc on the chain.
*/
prevtop = top;
top = &tos[top->link];
if (top->selfpc == selfpc) {
/*
* there it is.
* increment its count
* move it to the head of the chain.
*/
top->count++;
toindex = prevtop->link;
prevtop->link = top->link;
top->link = *frompcindex;
*frompcindex = toindex;
goto done;
}
}
done:
profiling--;
/* and fall through */
profiling--;
/* and fall through */
out:
return; /* normal return restores saved registers */
return; /* normal return restores saved registers */
overflow:
profiling++; /* halt further profiling */
fprintf( stderr, "%s: tos overflow", __func__ );
goto out;
profiling++; /* halt further profiling */
fprintf( stderr, "%s: tos overflow", __func__ );
goto out;
}
extern rtems_status_code __real_Clock_isr();
static uint32_t pc = 0;
/*
rtems_isr __wrap_Clock_isr()
{
if( s_profil && s_profil_sz )
{
// SAVE_L1(pc);
if( pc )
{
pc -= (int)s_lowpc;
pc = (pc >> HISTFRACTION_LOG2);
if( pc < s_profil_sz ) s_profil[pc]++;
}
}