Application execution with optimized code for use profiles

Method and system are provided for software application execution including switching between optimized code for use profiles. The method includes: accessing an application having multiple executables for the same function for multiple use profiles. The method includes: executing code for a first use profile; receiving a prompt to change to a second use profile; suspending the execution of the code for the first use profile; retrieving switching code from a pre-computed data structure, wherein the switching code is for carrying out operations to transfer from executing code for a first use profile to executing code for a second use profile; performing the operations of the switching code; and resuming executing the application by executing code for the second use profile.

BACKGROUND

The present invention relates to application execution optimization for different use profiles, and more specifically, application execution with switching to optimized code for different use profiles.

Dynamic optimization of code in its final run-time environment is known using on-line profiling. The profiling and optimizations incur an overhead on the execution of the user code.

Off-line processing is known in the form of Profile-Guided Optimization that allows software developers to test optimized code before it is rolled out into production. Separate profiles may be created for each use profile to ensure the best possible performance for that particular use case.

SUMMARY

According to a first aspect of the present invention there is provided a computer-implemented method for software application execution including switching between optimized code for use profiles, comprising: accessing an application having multiple executables for the same function for multiple use profiles; executing code for a first use profile; receiving a prompt to change to a second use profile; suspending the execution of the code for the first use profile; retrieving switching code from a pre-computed data structure, wherein the switching code is for carrying out operations to transfer from executing code for a first use profile to executing code for a second use profile; performing the operations of the switching code; and resuming executing the application by executing code for the second use profile.

According to a second aspect of the present invention there is provided a system for software application execution including switching between optimized code for use profiles, comprising: a processor and a memory configured to provide computer program instructions to the processor to execute the function of the components; an application accessing component for accessing an application having multiple executables for the same function for multiple use profiles; a code executing component for executing code for a first use profile; a prompt receiving component for receiving a prompt to change to a second use profile; a suspending component for suspending the execution of the code for the first use profile; a switching code retrieving component for retrieving switching code from a pre-computed data structure, wherein the switching code is for carrying out operations to transfer from executing code for a first use profile to executing code for a second use profile; a switching component for performing the operations of the switching code; and wherein the executing component resumes executing the application by executing code for the second use profile.

According to a third aspect of the present invention there is provided a computer program product for software application execution including switching between optimized code for use profiles, the computer program product comprising a computer readable storage medium having program instructions embodied therewith, the program instructions executable by a processor to cause the processor to: access an application having multiple executables for the same function for multiple use profiles; execute code for a first use profile; receive a prompt to change to a second use profile; suspend the execution of the code for the first use profile; retrieve switching code from a pre-computed data structure, wherein the switching code is for carrying out operations to transfer from executing code for a first use profile to executing code for a second use profile; perform the operations of the switching code; and resume executing the application by executing code for the second use profile.

DETAILED DESCRIPTION

Methods and systems are described which provide an efficient way of switching between pre-computed use profiles while an application is running, with as small overhead as possible.

The described methods involve a pre-processing method for generating switching code for switching between pairs of use profiles and a runtime method for switching between two use profiles during execution of the application.

Referring toFIG. 1, a flow diagram100illustrates an example embodiment of the pre-processing method.

As part of the software production process, developers may prepare a set of potential use cases in which they expect their application to be used. They may use the profile-guided optimization (PGO) feature of a compiler to generate101optimized versions of the application code for each respective use profile. This may obtain102a set of executables that perform the same function, but for each potential use profile, there is an optimized version that performs that particular use case the best. An executable is a set of instructions that causes a computer to perform tasks.

The influence of code caching may be considered during the use profile optimization. As jumps or calls within the cache can be expected to be significantly cheaper than jumps or calls to un-cached pages, it is important to optimize the runtime code layout so that code units that frequently cross-call each other can all be fitted in the appropriate caches at the same time. Due to the complex nature of cache performance, it is likely that an off-line optimizer can do a better job at ensuring optimal cache usage than a processor-constrained online method.

The different executables may be compared and merged103into a single “multi-profile” executable, whose exact format may be dependent on the host operating system. A low-level analysis of the code contained within each executable may be performed104with differences and/or similarities between the executables being identified and recorded. In particular, special consideration may be given to the appearance of various sections of code and data after they have been loaded into memory during normal execution. Sections of code may be units of code generated by the compiler for a single function or method definition.

Switching code may be generated105between each pair of use profiles. The switching code may be generated off-line by loading both the first use profile being switched from as well as the second use profile being switched to in memory, and examining the differences between their respective layouts. Such switching code may typically consist of operations that simply relocate memory blocks, update relocation addresses, and possibly change code or data areas if there is a difference between the respective code/data sections of the two profiles being switched between. Pre-generated switching code may be executed significantly faster than simply overlaying the entire new use profile on the previous one, due to the expected high degree of similarity between the two.

The switching codes may be organized106into a profile matrix in the form of a pre-computed data structure that facilitates switching between two profiles during execution.

For each pair of profiles, the matrix may contain a set of instructions to be performed in order to achieve a switch from a first use profile to a second use profile, including mapping the values of processor registers from their respective pre-switch states to the intended post-switch states.

The instructions that may be performed include those carried out when the executable or shared library is first loaded into memory, such as loading code or data, or performing relocations. The switching instructions do not perform useful work from the viewpoint of the application; they merely load the second use profile into memory.

Referring toFIG. 2, a flow diagram200illustrates an example embodiment of the runtime method for switching between two use profiles during execution of an application. As shown at block201, an application may be accessed that has multiple executables for multiple use profiles as developed off-line.

The initial code layout for the first use profile may be loaded, as shown at block202, which may include from executable files, dynamic libraries, or other means. The use profiles apply to momentary memory layouts, after all the symbols necessary for executing a section of code have been loaded from executable file(s) and/or dynamic libraries. At this point, the unit of code is ready for execution, and also for profile switching. Next, as shown at block203, the code for the current use profile may be executed.

As shown at decision block204, it may be determined if a prompt to switch use profiles is received. The prompt may be received from an external procedure and may designate a second use profile which is to be switched to. If there is no prompt to switch use profiles, then the application may continue execution for the first use profile, as shown at block205. If there is a prompt to switch profiles, the execution of the application may be suspended, as shown at block206. Next, as shown at block207, the switching code for switching from the first use profile to the second use profile may be retrieved from the pre-computed matrix profile data structure. The operations of the switching code may be performed, as shown at block208.

Whether profile switching can be performed collectively on all modules (i.e., executables and dynamic libraries) used by a program, or just a single module may depend on how the modules were loaded. If several programs are using the same address space loaded originally from a dynamic library, then changing the layout of this shared module may adversely affect the other programs. Therefore it is likely that profile switching may only operate inside the address space of a single module (executable or dynamic library).

However, the profile switcher may make a copy of an appropriate section of code in the address space of a dynamic library, and copy it to a more suitable location within the address space of the program for enhancing runtime performance. As shown at block209, the execution of the application may be resumed with executables for the second use profile. The method200may loop to execute current code and may receive a further prompt to switch profiles at a later time.

For the duration of the switchover, the running application is effectively suspended, and all processing state is preserved. The switch code is able to map the current processing state to the new processing state. The second use profile being switched to is able to continue execution from the new processing state, even in multiprocessing environments.

Mapping to a new processing state is complex if the running application is suspended at an arbitrary point; however, this is typically not necessary. Provided that the code in various profiles performs exactly the same function, the compiler may designate appropriate switchover points where execution may be suspended and all environmental factors mapped to their equivalents in the use profile being switched to.

The entire process requires coordination from the operating system level. In particular, scheduling the threads of the application while they are in the process of stopping needs operating system level control of the state in which the threads are suspended.

Referring toFIG. 3, a flow diagram illustrating an example embodiment of a method300for the suspension of execution inFIG. 2is shown. As shown at block301, the method300includes suspending the execution of the application. Next, as shown at decision block302, it may be determined if all threads are suspended in switchover states. If all threads are suspended then the method300may terminate, as shown at block303.

If not all the threads are suspended, then it may be determined, at decision block304, if all threads scheduled for suspension in a switchover state. If so, execution may be scheduled normally, as shown at block305, and the method300may loop to decision block302to determine if all threads are suspended in switchover states. If not all threads are scheduled for suspension in a switchover state, then the first thread is taken, as shown at decision block306, which is not scheduled for suspension and suspension may be scheduled, as shown at decision block307, at a first subsequent switchover point. The method300may loop to again determine if all threads scheduled for suspension in a switchover state, as shown at decision block304.

For example in use profile A, foo( ) and bar( ) call each other frequently, so they need to be in close memory range of each other. In use profile B, bar( ) and baz( ) call each other frequently, but foo( ) is almost idle, so the PGO advises that the code for bar( ) and baz( ) need to be close. They may be sufficiently lengthy to prevent all three of them being within this optimum threshold.

It may be required to switch from use profile A to use profile B at runtime. Therefore, there may be a need to effectively move the code of foo( ) and baz( ) perhaps change the relocations in bar( ) but may not need to touch quux( ) and xyzzy( ). So it is not worth re-loading the whole new executable, but following the pre-computed set of instructions may enable the switching of executables. This may take the following form

set offset+17 in bar( ) to the new address of foo( )

set offset+29 in bar( ) to the new address of baz( )

set value of static pointer declared at +8 in foo( ) to the base address of object @0x34598347 (taken from the old foo( )).

Generating instructions 1 to 5 may be straightforward; however, instruction 6 and similar instructions may be harder and are likely to result in restrictions on what code may be run.

The set of instructions is pre-computed for each pair of profiles (i.e., switching from A to B, switching from A to C, from A to D, from B to D, etc.), so when the switch happens, the very minimal runtime that needs to be incorporated in the executable loads the appropriate set of instructions, runs it, and resumes all threads in their new execution contexts.

The described methods and systems provide a pre-computed, limited set of PGO use profiles based, as deemed appropriate by the developers of the application. Multiple pre-optimized executables are generated for the use profiles, all containing only native code. In practice, this would be in the order of a few tens of different executables, so that each could be analyzed and properly tested before shipping.

The described method is aimed at a set of optimizations that include code layout in memory and code in-lining. The set of executables or variants may be generated off-line, and the method addresses the problem of switching to the variant most suitable to the momentary circumstances of the production environment.

There are numerous specific optimization techniques performed by modern compilers, but of particular interest to this method are code in-lining and code layout that allows frequently called code to reside near the call site in memory, allowing the use of short-range jump/call instructions instead of more expensive long-range ones. Both of these techniques involve trade-offs: a decision to use a particular layout may improve performance in certain use cases, but degrade it in others. Also, in-lining the same code at several call sites may cause memory bloat. Therefore, the optimal code layout will be different for the potential use cases of an application.

Conventional on-line optimizations take significant processing power from the executing application, and their behavior is not always completely predictable. Moreover, the optimizations will always be tailored to the code being executed, as it is being executed. In reality, it may be more desirable to allow the application developer guide the optimizations using specific test cases.

At the opposite end of the spectrum where source code is compiled to native code, for example, using PGO, may result in an executable that cannot adapt during runtime to significant changes in requirements such as, for example, a new application, new type of workload, etc.

Having the pre-computed use profiles and switching operations means that no compilation needs to occur at run-time and all processing power is available to the application.

Most work required for performing the address relocations in the executable code; this is normally done at application startup by the relocating loader. The pre-computation in this context means that instead of having to completely wipe out the old code (which would likely require stopping execution completely and losing a lot of application state), then load the new one from disk and modify all call/jump/etc. addresses as specified by the relocation table, the compiler may generate a profile matrix giving specific instructions for whatever needs to be done for switching from profile A to profile B.

The code optimizations for use profiles and the profile matrix are pre-computed off-line by the compiler. The offline optimization can use virtually unlimited CPU power, and furthermore, the developers can freely choose what input they want to feed into the optimizer. The result is that the performance characteristic can be verified in advance, and some potentially stochastic behavior is eliminated.

The described method uses the adaptability of Just-In-Time compilation in combination with the high performance of the off-line optimization carried out by profile-guided optimizing compilers. The desired effect is achieved by creating multiple versions of the executable code, all optimized off-line, then continuously monitoring the run-time environment and selecting the most appropriate version for the momentary circumstances.

The different use profiles may share significant areas of common code that would appear as redundant copies if the different profiles were represented using full copies of the executable. Other pieces of code may appear multiple times with small variations such as differences in alignment, relocation addresses, small subsets of instructions, etc. Only under rare circumstances will the different profiles represent code performing the same function in vastly different ways. Taking advantage of such similarities not only reduces the amount of storage space required to store the full set of profiles, but it also reduces the time it takes to switch from one profile to another while the application is executing.

Referring toFIG. 4, a block diagram shows a pre-computation system400for preparing an application for optimization for pre-defined use profiles and a profile matrix of switching code for switching between the pre-defined use profiles.

The pre-computation system400may include at least one processor401, a hardware module, or a circuit for executing the functions of the described components which may be software units executing on the at least one processor. Multiple processors running parallel processing threads may be provided enabling parallel processing of some or all of the functions of the components. Memory402may be configured to provide computer instructions403to the at least one processor401to carry out the functionality of the components.

An application generating component410may be provided by the pre-computation system400for developing an application with executables optimized for multiple pre-defined use profiles. The application generating component410may include a use optimization component411for preparing optimized versions of the application code for each use profile. The use optimization component411may include a caching component415for optimizing caching of pages within the application.

The application generating component410may include an executable obtaining component412for obtaining a set of executables that perform the same function of the application code for the use profiles. The application generating component410may include a merging component413for comparing and merging the set of executables and an output component414for providing the merged sets of executables.

The pre-computation system400may include a switching code component420for providing switching code for the application. The switching code component420may include an analyzing component421for analyzing code of each of the set of executables output by the output component414of the application generating component410. The analyzing component421may analyze the executables that perform the same function to determine differences and/or similarities of the executables with consideration of the appearance of sections of the code and data after they have been loaded into memory during normal execution.

The switching code component420may include a switching code generating component422for generating switching code based on the analysis of the sets of executables for switching between pairs of use profiles, and a storing component423for storing the switching code between pairs of use profiles in a profile matrix425as a pre-computed data structure for access by an application during execution.

Referring toFIG. 5, a block diagram shows a runtime system500executing an application for a first use profile and applying switching code for switching between pre-defined use profiles.

The runtime system500may include at least one processor501, a hardware module, or a circuit for executing the functions of the described components which may be software units executing on the at least one processor. Multiple processors running parallel processing threads may be provided enabling parallel processing of some or all of the functions of the components. Memory502may be configured to provide computer instructions403to the at least one processor501to carry out the functionality of the components.

The runtime system500may include an application accessing component511for accessing an application having multiple executables for the same function for multiple use profiles.

The runtime system500may include a code executing component512for executing code for a first use profile and a prompt receiving component513for receiving a prompt to change to a second use profile. The runtime system500may include a suspending component514for suspending the execution of the code for the first use profile. The runtime system500may include a defined location component517for, after receiving a prompt to change to a second use profile, continuing executing the code for the first use profile until a pre-defined location of the code is reached which is suitable for switching executions of the use profiles.

The runtime system500may include a switching code retrieving component515for retrieving switching code from a profile matrix518in the form of a pre-computed data structure which stores the switching code for pairs of use profiles including required operations to transfer from executing code for a first use profile to executing code for a second use profile. The runtime system500may also include a switching component516for performing the operations of the switching code.

The code executing component512may then resumes executing the application by executing code for the second use profile.

Referring now toFIG. 6, a schematic of an example of a system600in the form of a computer system or server is shown in which the described pre-computation system400or runtime system500may be implemented.

InFIG. 6, a computer system/server612is shown in the form of a general-purpose computing device. The components of the computer system/server612may include, but are not limited to, one or more processors or processing units616, a system memory628, and a bus618that couples various system components including system memory628to processor616.

Computer system/server612typically includes a variety of computer system readable media. Such media may be any available media that is accessible by computer system/server612, and it includes both volatile and non-volatile media, removable and non-removable media.

Program/utility640, having a set (at least one) of program modules642, may be stored in memory628by way of example, and not limitation, as well as an operating system, one or more application programs, other program modules, and program data. Each of the operating system, one or more application programs, other program modules, and program data or some combination thereof, may include an implementation of a networking environment. Program modules642generally carry out the functions and/or methodologies of embodiments of the invention as described herein.

Computer system/server612may also communicate with one or more external devices614such as a keyboard, a pointing device, a display624, etc.; one or more devices that enable a user to interact with computer system/server612; and/or any devices (e.g., network card, modem, etc.) that enable computer system/server612to communicate with one or more other computing devices. Such communication can occur via Input/Output (I/O) interfaces622. Still yet, computer system/server612can communicate with one or more networks such as a local area network (LAN), a general wide area network (WAN), and/or a public network (e.g., the Internet) via network adapter620. As depicted, network adapter620communicates with the other components of computer system/server612via bus618. It should be understood that although not shown, other hardware and/or software components could be used in conjunction with computer system/server612. Examples, include, but are not limited to: microcode, device drivers, redundant processing units, external disk drive arrays, RAID systems, tape drives, and data archival storage systems, etc.

Service Models are as follows:

Deployment Models are as follows:

Referring now toFIG. 7, illustrative cloud computing environment750is depicted. As shown, cloud computing environment750includes one or more cloud computing nodes710with which local computing devices used by cloud consumers, such as, for example, personal digital assistant (PDA) or cellular telephone754A, desktop computer754B, laptop computer754C, and/or automobile computer system754N may communicate. Nodes710may communicate with one another. They may be grouped (not shown) physically or virtually, in one or more networks, such as Private, Community, Public, or Hybrid clouds as described hereinabove, or a combination thereof. This allows cloud computing environment750to offer infrastructure, platforms and/or software as services for which a cloud consumer does not need to maintain resources on a local computing device. It is understood that the types of computing devices754A-N shown inFIG. 7are intended to be illustrative only and that computing nodes710and cloud computing environment750can communicate with any type of computerized device over any type of network and/or network addressable connection (e.g., using a web browser).

Hardware and software layer860includes hardware and software components. Examples of hardware components include: mainframes861; RISC (Reduced Instruction Set Computer) architecture based servers862; servers863; blade servers864; storage devices865; and networks and networking components866. In some embodiments, software components include network application server software867and database software868.

Virtualization layer870provides an abstraction layer from which the following examples of virtual entities may be provided: virtual servers871; virtual storage872; virtual networks873, including virtual private networks; virtual applications and operating systems874; and virtual clients875.

Workloads layer890provides examples of functionality for which the cloud computing environment may be utilized. Examples of workloads and functions which may be provided from this layer include: mapping and navigation891; software development and lifecycle management892; virtual classroom education delivery893; data analytics processing894; transaction processing895; and software execution including swapping between executables for pre-defined use profiles896.