Forum Discussion

Altera_Forum's avatar
Altera_Forum
Icon for Honored Contributor rankHonored Contributor
13 years ago

Using nios2-elf-gprof with UCOS-II?

Hello All,

Is it possible to use the Nios II GNU profiler with applications that use an OS (such as UCOS-II) with multitasking?

I have followed the the "Profiling Nios II Systems" guide (unable to post link at the time - google title). However, when I execute the necessary commands, the gmon.out file is not created. Thus, I can not do any profiling. Here is what I have done:

1) Set enable_gprof and enable_exit to true in my applications BSP settings.

2) Regenerated BSP and re-built.

3) Opened two terminals, let's call them terminal A & B.

4) In terminal A, I executed the following command:

--- Quote Start ---

nios2-terminal

--- Quote End ---

5) In terminal B, I then execute the following command:

--- Quote Start ---

nios2-download -g --write-gmon gmon.out my_eth_app.elf

--- Quote End ---

6) I run my application and then exit with Ctrl-l C.

7) I then noticed that no gmon.out file is created.

Is there something I am missing? One thing that the guide above states is to verify that your main function returns. However, in an UCOS-II application, main should not return as tasks are continually running. Typically, the UCOS-II API, OSStart(), is called from main, which starts multitasking and never returns. As an example of main, have a look at the code below.


int main (int argc, char* argv, char* envp)
{
  
  INT8U error_code;
  /* Clear the RTOS timer */
  OSTimeSet(0);
  error_code = OSTaskCreateExt(initial_task,
                             NULL,
                             (void *)&initial_task_stack,
                             INITIAL_TASK_PRIORITY,
                             INITIAL_TASK_PRIORITY,
                             &initial_task_stack,
                             TASK_STACKSIZE,
                             NULL,
                             0);
  alt_uCOSIIErrorHandler(error_code, 0);
  /*
   * As with all MicroC/OS-II designs, once the initial thread(s) and 
   * associated RTOS resources are declared, we start the RTOS. That's it!
   */
  OSStart();
  
  while(1); /* Correct Program Flow never gets here. */
  return -1;
}
As you can see from above, main will not return. Is this my issue? Do I have to look at other options to profile my application?

Thanks for your help and I look forward to any feedback.

-JQ

5 Replies

  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    IIRC the gmon.out file is usually written from a function registered with libc using at_exit() - so you probably need to call exit() somewhere in order to get those functions called (assuming that uCOSII will do it).

    Alternatively you may be able to directkly call the 'dump all the tables' command.

    Another possiblity is to request gcc add calls to profiling functions on every function entry/exit - and write your own stubs.
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    Hello dsl,

    Thanks for taking the time to reply.

    --- Quote Start ---

    IIRC the gmon.out file is usually written from a function registered with libc using at_exit() - so you probably need to call exit() somewhere in order to get those functions called (assuming that uCOSII will do it).

    --- Quote End ---

    I suppose what I can do here is just go ahead and hard card an exit function call into my source after I have executed the behavior I want to monitor (since ctrl-c does not initiate the dumps). I will update this thread when I try this.

    --- Quote Start ---

    Alternatively you may be able to directkly call the 'dump all the tables' command.

    --- Quote End ---

    I am not familiar with the 'dump all the tables' command. Is this specific to gprof? Can you post a link so that I can get more information?

    Again, thanks for your reply and I'll update this thread when I run my test.

    -JQ
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    dsl,

    Thanks for your responses.

    I was able to get gprof to spit out the gmon.out by just placing an exit(0) function call after the portion of the code I wanted executed.

    I will say for anyone who reads this thread, make sure leave some time for the I/O to make it out of the JTAG UART before calling your exit function. For example, in my application (as well as in the "Profiling Nios II Systems" guide tutorial) a usleep function is called before the exit function call. This is necessary when using the profiler, because as soon as the BREAK 2 instruction is executed during the exit(), the application halts and the debug core takes over and transfers the gmon.out data back to the host.

    In my particular example, I had to invoke usleep with a larger tick count than what the tutorial used. When the tick count was too small, I would get the following error:

    --- Quote Start ---

    Using cable "USB-Blaster [USB-0]", device 1, instance 0x00

    Processor is already paused

    Initializing CPU cache (if present)

    OK

    Downloaded 275KB in 4.7s (58.5KB/s)

    Verified OK

    Running target program until exit

    Uploading GMON data: 75%

    gmon data upload failed

    Leaving target processor paused

    --- Quote End ---

    However, when I used a larger tick count, gmon.out was created and uploaded back to the host:

    --- Quote Start ---

    Using cable "USB-Blaster [USB-0]", device 1, instance 0x00

    Processor is already paused

    Initializing CPU cache (if present)

    OK

    Downloaded 275KB in 4.7s (58.5KB/s)

    Verified OK

    Running target program until exit

    uploaded gmon data: 28k in 0.8s

    Leaving target processor paused

    --- Quote End ---

    Again, thanks dsl for your responses and I hope this helps someone out there.

    -JQ
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    please jq,

    can you explain your solution (codes and tricks)?

    best regards

    Edited...

    JQ, I understood your trick.

    Works fine here!

    Thanks! :)