Performance/Reorder Symbols For Libraries

From Apache OpenOffice Wiki
< Performance
Revision as of 20:24, 25 March 2009 by Cd (Talk | contribs)

Jump to: navigation, search

Reorder code/data for libraries to improve file I/O

The comprehensive analysis of the cold start up behavior of OpenOffice.org shows that file I/O is the main bottleneck. About 80% of the start up time is spent waiting for data from the disk. Most file I/O depends on library loading. This part describes what can be done to reduce I/O time for loading OpenOffice.org libraries. The main ideas are system independent but the solutions must be system/compiler specific. The following chapters describe in detail how we want to reorder code/data within the libraries.

Main idea

Normally the compiler and linker produce a library which consists of many object files. The order of the code/data is dependent on the strategy of the linker and the layout of the library format. During the start up the application libraries are loaded on demand. Dependend on the program float new code and therefore pages are loaded from disk into memory. Unfortunately the linker doesn't know how the application accesses every library during the start up phase. Therefore the needed code/data is distributed all over the library which causes many page faults and disk access.

Main Idea Library Optimization.png

System dependent solution

Windows

This chapter describes the solution for the Windows platform.

Microsoft Visual Studio 2008

OpenOffice.org uses the Microsoft Visual Studio 2008 C/C++ compiler suite for the Windows build, called wntmsci12[.pro]. Unfortunately Microsoft discontinued the Working Set Tuner application which was part of the Platform SDK. That application allowed developers to optimize the layout of application libraries. A successor called Smooth Working Set Tool is also not available for download.

So we have to look for a solution on our own. This has also the big advantage that the solution can be adapted to our needs. What options are available to support us reordering code/data in libraries? If you start the C/C++ compiler and linker with the help option you can see all supported options. The following section shows the options which can help us.

Compiler options
Microsoft (R) 32-Bit C/C++-Optimizing Compiler Version 15.00.30729.01 for 80x86
 
Copyright (C) Microsoft Corporation.  All rights reserved.
 
...
/Gh Enable _penter function call        
/GH Enable _pexit function call
/Gy Enable Function-Level Linking
...
 
Microsoft (R) Incremental Linker Version 9.00.30729.01
Copyright (C) Microsoft Corporation.  All rights reserved.
 
 Syntax: LINK [Options] [Files] [@Commandfile]
 
   Options:
      ...
      /ORDER:@Filename
      ...

The /Gh compiler option provides us the ability to be called by every function entry. The call to the hook function will added by the compiler. That means that the code we want to instrument must be build with the /Gh option set. The /GH option is useful if we want to measure timing therefore we don't need it for record function calls. The /Gy options is needed to reorder the symbols by the linker.

Looking at the documentation for the /Gh option Microsoft states that the hook function must be declared as naked. The function must also preserve all register content.

void __declspec(naked) _cdecl _penter( void );

A function declared with the naked attribute doesn't have prolog or epilog code. It enables a developer to write his own custom prolog/epilog code using the inline assembler. The following skeleton can be used for our target to record all function calls during the start up.

extern "C" void __declspec(naked) _cdecl _penter( void )
{
   _asm
   {
      push eax
      push ebx
      push ecx
      push edx
      push ebp
      push edi
      push esi
   }
 
   // TODO: Add code to determine the caller address and provide it
   // to a function which records the call.
 
   _asm
   {
      pop esi
      pop edi
      pop ebp
      pop edx
      pop ecx
      pop ebx
      pop eax
      ret
   }
}

What we have to do is to retrieve the address of the caller function. This can be done with a little calculation as the address is on the stack. See the following code.

extern "C" void __declspec(naked) _cdecl _penter( void )
{
    _asm
    {
        push eax
        push ebx
        push ecx
        push edx
        push ebp
        push edi
        push esi
 
        // calculate the pointer to the return address
        mov  ecx, esp
        add  ecx, 28
 
        // retrieve return address from stack
        mov  eax, dword ptr[ecx]
 
        // subtract 5 bytes as instruction for call _penter is 5 bytes long on 32-bit machines, e.g. E8 <00 00 00 00>
        sub  eax, 5
 
        // provide return address to recordFunctionCall
        push eax
        call forwardFunctionCall
 
        pop esi
        pop edi
        pop ebp
        pop edx
        pop ecx
        pop ebx
        pop eax
        ret
    }
}

The implementation of the _penter function provides the start address of the called function to an external function which can be implemented by C++ code. This function must

  • Determine to which module the address belongs
  • Control a counter for every module which tag every new detected function with the current count. This gives us the opportunity to sort the function symbols related to their call sequence.
  • An access counter for every function to give us a chance to sort the function symbols related to their importance.
Determine what functions are called during start up

With the help of the _penter function we are able to get the function addresses which are called during the start up. The _penter function calls an second function which can be implemented using C++. The function has to implement the following tasks:

  • Determine to which module the address belongs
  • Control a counter for every module which tag every new detected function with the current count. This gives us the opportunity to sort the function symbols related to their call sequence.
  • An access counter for every function to give us a chance to sort the function symbols related to their importance.
How to create an ORDER file that is accepted by the linker

There is no real documentation about the decoration schema Microsoft uses for their C++ compilers. A very comprehensive description can be found on the following Wikipedia page: http://en.wikipedia.org/wiki/Microsoft_Visual_C%2B%2B_Name_Mangling.

Linux

MacOS X

The following web page from Apple describes what must be done to reorder code/data of a library to improvde locality.

Solaris

OpenOffice.org uses the Sun Studio C++ compiler suite for building on both Sparc and x86 CPU systems. The following web page describes what can be done to optimize the code layout of libraries with the Sun Studio C++ compiler suite.

Personal tools