Analyzing Parallel Programs with Pin

Analyzing Parallel Programs with Pin

COVER FEATURE ANALYZING PARALLEL PROGRAMS WITH PIN Moshe Bach, Mark Charney, Robert Cohn, Elena Demikhovsky, Tevi Devor, Kim Hazelwood, Aamer Jaleel, Chi-Keung Luk, Gail Lyons, Harish Patil, and Ady Tal, Intel Software instrumentation provides the Instrumentation is one tool for collecting the information means to collect information on and effi- needed to understand programs. Instrumentation- based ciently analyze parallel programs. Using tools typically insert extra code into a program to record Pin, developers can build tools to detect events during execution.1-4 The cost of executing the extra and examine dynamic behavior including code can be as low as a few cycles, enabling fine-grained observation down to the instruction level. data races, memory system behavior, and Pin2 (www.pintool.org) is a software system that per- parallelizable loops. forms runtime binary instrumentation of Linux and Microsoft Windows applications. Pin’s aim is to provide an instrumentation platform for building a wide variety of decade ago, systems with multiple proces- program analysis tools, called pintools. By performing the sors were expensive and relatively rare; only instrumentation on the binary at runtime, Pin eliminates developers with highly specialized skills could the need to modify or recompile the application’s source successfully parallelize server and scientific and supports the instrumentation of programs that dy- applications to exploit the power of multipro- namically generate code. Acessor systems. In the past few years, multicore systems have become pervasive, and more programmers want to INSTRUMENTATION employ parallelism to wring the most performance out of Pin provides a platform for building instrumentation their applications. tools. A pintool consists of instrumentation, analysis, Exploiting multiple cores introduces new correctness and callback routines.1 Instrumentation routines inspect and performance problems such as data races, deadlocks, the application’s instructions and insert calls to analysis load balancing, and false sharing. Old problems such as routines. Analysis routines are called when the program memory corruption become more difficult because par- executes an instrumented instruction and often perform allel programs can be nondeterministic. Programmers ancillary tasks. The program invokes callbacks when an need a deeper understanding of their software’s dynamic event occurs, for example, when it is about to exit. behavior to successfully make the transition from single Figure 1 shows a simple pintool that prints the memory to multiple threads and processes. addresses of all data a program reads or writes. Instruc- 56 COMPUTER Published by the IEEE Computer Society 0018-9162/10/$26.00 © 2010 IEEE #include <stdio.h> #include “pin.H” tion is an instrumentation routine that Pin FILE trace;* calls the first time the program executes an VOID Address(VOID * addr) { fprintf(trace,”%p\n”, addr); } instruction, so the routine can specify how VOID Instruction(INS ins, VOID *v) { it should be instrumented. If the instruc- if (INS_IsMemoryRead(ins)) { tion reads or writes memory, this example INS_InsertCall(ins, IPOINT_BEFORE, AFUNPTR(Address), pintool inserts a call to Address—an analy- IARG_MEMORYREAD_EA, IARG_END); sis routine—and directs Pin to pass it the } memory reference’s effective address. if (INS_IsMemoryWrite(ins)) { Immediately before a memory reference INS_InsertCall(ins, IPOINT_BEFORE, AFUNPTR(Address), executes, the program calls Address, which IARG_MEMORYWRITE_EA, IARG_END); prints the address to a file. The program in- } vokes a callback routine, Fini, when it exits. } Instrumentation and callback routines are VOID Fini(INT32 code, VOID *v) { fclose(trace); } registered in the pintool’s main function. int main(int argc, char *argv[]) { Figure 1 demonstrates only a small part PIN_Init(argc, argv); of the Pin API. Whereas the example uses an trace = fopen(“pinatrace.out”, “w”); instrumentation routine that can only see a INS_AddInstrumentFunction(Instruction, 0); single instruction at a time, Pin lets instru- PIN_AddFiniFunction(Fini, 0); mentation routines see instruction blocks or PIN_StartProgram(); whole binaries. The argument to Address is return 0; an effective address, but Pin provides much } more, including register contents (for ex- ample, value of R9), the instruction pointer Figure 1. Pintool for printing all program memory read and write addresses. (IP or PC), procedure argument values, and constants. The only callback used in the typedef void (*malloc_funptr_t)(size_t size); example is for program end, but Pin also malloc_funptr_t app_malloc; provides callbacks to notify a pintool about VOID * malloc_wrap(size_t size) { shared library loads, thread creation, system void * ptr = app_malloc(size); calls, Unix signals, and Microsoft Windows printf(\”Malloc %d return %p\”, size, ptr); exceptions. return ptr; Although the instrumentation in this } example is very simple, it is sufficient for VOID Image(IMG img, VOID *v) { a variety of useful tools. Instead of writing RTN mallocRtn = RTN_FindByName(img, “malloc”); addresses to a file, a tool could feed the ad- if (RTN_Valid(mallocRtn)) { dresses to a software model of a cache and app_malloc= compute the cache miss rate for the appli- (malloc_funptr_t)RTN_ReplaceProbed(mallocRtn,AFUNPTR(m cation. By watching all the references to a alloc_wrap)); specific memory location, it is possible to } find an erroneous write through a pointer } that overwrites a value with 1/100th the overhead of doing the same analysis in a Figure 2. Pintool’s fragment for wrapping malloc. debugger. Pin uses a just-in-time (JIT) compiler to insert instrumentation into a running application. The In Pin’s high-performance probe mode option, the JIT compiler recompiles and instruments small chunks of base overhead is near zero. The probe mode has a binary instructions immediately prior to executing them. limited set of callbacks available and restricts tools Pin stores the modified instructions in a software code to interposing wrapper routines for global functions. cache where they execute in lieu of the original applica- Figure 2 shows a pintool’s fragment that wraps calls to tion instructions. The code cache allows Pin to generate malloc so it can print the argument and return values. code regions once and reuse them for the remainder of Image is an instrumentation routine that the program program execution, amortizing compilation costs. Pin’s invokes every time a binary or shared library loads. It average base overhead is 30 percent, and user-inserted searches the binary for a function called malloc and instrumentation adds to the time. replaces it with a call to malloc_wrap. When the pro- MARCH 2010 57 cover FEATURE gram calls malloc, malloc_wrap is called instead, separate output file for each thread and retrieve the file which calls the application malloc, then prints the descriptor from thread-local storage. argument and return value. To avoid infinite recur- sion, the call to malloc from malloc_wrap should not Performance considerations be redirected, so we instead call the function pointer Correcting a parallel program by adding locks is usually returned by RTN_ReplaceProbed. The data collected straightforward. However, a highly contended lock serializes from this tool could be used to find a program that in- execution and leads to poor CPU utilization. Because applica- correctly freed the same memory twice or track down tion threads execute analysis routines, a highly contended some code that allocated too much memory. lock in an analysis routine will also serialize the application’s In probe mode, the program binary is modified in execution. The serialization increases the pintool’s over- memory. Pin overwrites the entry point of procedures head when compared to the application’s uninstrumented with jumps (called probes) to dynamically generated in- execution and might alter the parallel program’s behavior strumentation. This code can invoke analysis routines drastically. Pintool authors must employ standard parallel or a replacement routine. When the replacement routine programming techniques to avoid excessive serialization. needs to invoke the original function, it calls a copy of the They should use thread-local storage to avoid the need to entry point (without the probe) and continues executing lock global storage. Instead of a single monolithic lock for a the original program. data structure, they should use fine-grained locks. False sharing is another pitfall in naïve pintools, occurring when multiple threads access different parts of the same In probe mode, Pin overwrites the cache line and at least one of them is a write. To maintain memory coherency, the computer must copy the memory entry point of procedures with from one CPU’s cache to another, even though data is not jumps to dynamically generated truly shared. False sharing is less costly when CPUs operate instrumentation. out of a shared cache, as is true for the four cores in the Intel Core i7 processor. Developers can eliminate false sharing by padding critical data structures to the size of a cache line or Instrumenting parallel programs rearranging the structures’ data layout. Instrumenting a parallel program is not very different from instrumenting single-threaded programs. Pin provides Multithreaded versus callbacks when a new thread or new process is created. multiprocess instrumentation Analysis routines can be passed a thread ID so it is possible Pin allows instrumentation of parallel

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    8 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us