How to profile a “Made With Marmalade” app

Profiling is a form of dynamic program analysis that measures, for example, the usage of memory, the usage of particular instructions, or frequency and duration of function calls. The most common use of profiling information is to aid program optimization.

Profiling is achieved by instrumenting either the program source code or its binary executable form using a tool called profiler.

Although Marmalade SDK doesn’t provide a profiler for the x86 simulator, it is still possibile manually add profiling instructions directly in the code in order to find code bottlenecks. However this feature is not well documented, so I will try to explain it a little better.

First of all define the symbol IW_USE_PROFILE. I suggest to do it in your MBK, adding a define command:

define IW_USE_PROFILE

Then add this portion of code to your program:

#include “IwProfileMenu.h”

#ifdef IW_DEBUG


#ifdef IW_USE_PROFILE


class CIwMenuItemProfile : public CIwMenuItem

{
public:
  CIwMenuItemProfile() : CIwMenuItem(“”, “Profile”, 0, CIwMenuItem::MENU) {};
  void Select();
};

void CIwMenuItemProfile::Select()
{
  // Create debug menu
   new CIwProfileMenu();
  CIwProfileManager::Get().SetActive(true);
}

#endif


void createDebugMenu()

{
  new CIwMenuManager;
  // Rendering and main menu construction callbacks
   IwGetMenuManager()->SetTextCallback(IwGxPrintMenuCallback);
   IwGetMenuManager()->SetMainMenuFn(DebugCreateMainMenu);

#ifdef IW_USE_PROFILE

  new CIwProfileManager();
  CIwProfileManager::Get().SetActive(true);
#endif
}

void destroyDebugMenu()
{
#ifdef IW_USE_PROFILE
   delete &CIwProfileMenu::Get();
   delete &CIwProfileManager::Get();
#endif

delete IwGetMenuManager();

}

CIwMenu* DebugCreateMainMenu()

{
  CIwMenu* pMenu = new CIwMenu;
  pMenu->AddItem( new CIwMenuItemResManager );
  pMenu->AddItem( new CIwMenuItemGx );

#ifdef IW_USE_PROFILE
  pMenu->AddItem( new CIwMenuItemProfile );
#endif

return pMenu;

}

#endif // IW_DEBUG


Now ensure to make a call to createDebugMenu() in your main initialization function and to destroyDebugMenu() in your main cleanup function. Also place a call to IW_PROFILE_NEWFRAME() at the start of each frame: the best place is at the start of your main loop. Then add the following lines in your main loop:

#ifdef IW_DEBUG
  if (IwGetMenuManager()) {
    IwGetMenuManager()->Update();
    IwGetMenuManager()->Render();
  }
#endif

You are now free to add the IW_PROFILE() macro at the beginning of every function you want to profile, e.g. IW_PROFILE(“my_function”). You can also use IW_PROFILE_START() and IW_PROFILE_STOP() to explicitly profile a block of code.

Compile your app and run it in debug mode, then press F6 to see profile info, using cursor keys to select the menu items.

That’s all folks!

Note that the debug menu includes iwgx and iwresmanager menu items.

Leave a Reply