6.823: computer system architecture introduction to...

40
Introduction to PIN Victor Ying [email protected] Adapted from: Prior 6.823 offerings, and Intel’s Tutorial at CGO 2010 2/7/2020 6.823 Spring 2020 1 6.823: Computer System Architecture

Upload: others

Post on 25-Sep-2020

5 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 6.823: Computer System Architecture Introduction to PINcsg.csail.mit.edu/6.823/Recitations/Recitation-1_Pin... · 2020. 2. 8. · Introduction to PIN Victor Ying 6823-tas@csail.mit.edu

Introduction to PIN

Victor [email protected]

Adapted from: Prior 6.823 offerings, and

Intel’s Tutorial at CGO 2010

2/7/2020 6.823 Spring 2020 1

6.823: Computer System Architecture

Page 2: 6.823: Computer System Architecture Introduction to PINcsg.csail.mit.edu/6.823/Recitations/Recitation-1_Pin... · 2020. 2. 8. · Introduction to PIN Victor Ying 6823-tas@csail.mit.edu

Designing Computer Architectures

Two important components:

1. Study of programs and patterns

– Guides interface, design choices

2. Engineering under constraints

– Evaluate tradeoffs of architectural choices

2

Build computers that run programs efficiently

2/7/2020 6.823 Spring 2020

Page 3: 6.823: Computer System Architecture Introduction to PINcsg.csail.mit.edu/6.823/Recitations/Recitation-1_Pin... · 2020. 2. 8. · Introduction to PIN Victor Ying 6823-tas@csail.mit.edu

Simulation: An Essential Tool in Architecture Research

• A tool to reproduce the behavior of a computing device

• Why use simulators?

– Obtain fine-grained details about internal behavior

– Enable software development

– Obtain performance predictions for candidate architectures

– Cheaper than building system32/7/2020 6.823 Spring 2020

Page 4: 6.823: Computer System Architecture Introduction to PINcsg.csail.mit.edu/6.823/Recitations/Recitation-1_Pin... · 2020. 2. 8. · Introduction to PIN Victor Ying 6823-tas@csail.mit.edu

Labs

• Focus on understanding program behavior, evaluating architectural tradeoffs

4

Ben

chm

ark

Suit

e

Model Results

2/7/2020 6.823 Spring 2020

Page 5: 6.823: Computer System Architecture Introduction to PINcsg.csail.mit.edu/6.823/Recitations/Recitation-1_Pin... · 2020. 2. 8. · Introduction to PIN Victor Ying 6823-tas@csail.mit.edu

PIN

• www.pintool.org

– Developed by Intel

– Free for download and use

• A tool for dynamic binary instrumentation

5

Runtime No need to re-compile or re-link

Insert code in the program to collect information

2/7/2020 6.823 Spring 2020

Page 6: 6.823: Computer System Architecture Introduction to PINcsg.csail.mit.edu/6.823/Recitations/Recitation-1_Pin... · 2020. 2. 8. · Introduction to PIN Victor Ying 6823-tas@csail.mit.edu

Pin: A Versatile Tool

• Architecture research– Simulators: zsim, CMPsim, Graphite, Sniper, Swarm

• Software Development– Intel Parallel Studio, Intel SDE

– Memory debugging, correctness/perf. analysis

• Security– Taint analysis

6

Useful tool to have in your arsenal!

2/7/2020 6.823 Spring 2020

Page 7: 6.823: Computer System Architecture Introduction to PINcsg.csail.mit.edu/6.823/Recitations/Recitation-1_Pin... · 2020. 2. 8. · Introduction to PIN Victor Ying 6823-tas@csail.mit.edu

PIN

• www.pintool.org

– Developed by Intel

– Free for download and use

• A tool for dynamic binary instrumentation

7

Runtime No need to re-compile or re-link

Insert code in the program to collect information

2/7/2020 6.823 Spring 2020

Page 8: 6.823: Computer System Architecture Introduction to PINcsg.csail.mit.edu/6.823/Recitations/Recitation-1_Pin... · 2020. 2. 8. · Introduction to PIN Victor Ying 6823-tas@csail.mit.edu

Instrumenting Instructions

8

sub $0xff, %edx

cmp %esi, %edx

jle <L1>

mov $0x1, %edi

add $0x10, %eax

2/7/2020 6.823 Spring 2020

Page 9: 6.823: Computer System Architecture Introduction to PINcsg.csail.mit.edu/6.823/Recitations/Recitation-1_Pin... · 2020. 2. 8. · Introduction to PIN Victor Ying 6823-tas@csail.mit.edu

Example 1: Instruction Count

sub $0xff, %edx

cmp %esi, %edx

jle <L1>

mov $0x1, %edi

add $0x10, %eax

9

I want to count the number of

instructions executed..

2/7/2020 6.823 Spring 2020

Page 10: 6.823: Computer System Architecture Introduction to PINcsg.csail.mit.edu/6.823/Recitations/Recitation-1_Pin... · 2020. 2. 8. · Introduction to PIN Victor Ying 6823-tas@csail.mit.edu

Example 1: Instruction Count

sub $0xff, %edx

cmp %esi, %edx

jle <L1>

mov $0x1, %edi

add $0x10, %eax

10

Let’s increment counter by one

before every instruction!

counter++;

counter++;

counter++;

counter++;

counter++;

2/7/2020 6.823 Spring 2020

Page 11: 6.823: Computer System Architecture Introduction to PINcsg.csail.mit.edu/6.823/Recitations/Recitation-1_Pin... · 2020. 2. 8. · Introduction to PIN Victor Ying 6823-tas@csail.mit.edu

Example 2: Instruction Trace

11

sub $0xff, %edx

cmp %esi, %edx

jle <L1>

mov $0x1, %edi

add $0x10, %eax

I want to generateinstruction trace..

2/7/2020 6.823 Spring 2020

Page 12: 6.823: Computer System Architecture Introduction to PINcsg.csail.mit.edu/6.823/Recitations/Recitation-1_Pin... · 2020. 2. 8. · Introduction to PIN Victor Ying 6823-tas@csail.mit.edu

Example 2: Instruction Trace

sub $0xff, %edx

cmp %esi, %edx

jle <L1>

mov $0x1, %edi

add $0x10, %eax

12

Print(ip);

Print(ip);

Print(ip);

Print(ip);

Print(ip);

Let’s printinstruction pointers

before every instruction!

2/7/2020 6.823 Spring 2020

Page 13: 6.823: Computer System Architecture Introduction to PINcsg.csail.mit.edu/6.823/Recitations/Recitation-1_Pin... · 2020. 2. 8. · Introduction to PIN Victor Ying 6823-tas@csail.mit.edu

Example 2: Instruction Trace

sub $0xff, %edx

cmp %esi, %edx

jle <L1>

mov $0x1, %edi

add $0x10, %eax

13

Print(ip);

Print(ip);

Print(ip);

Print(ip);

Print(ip);

Let’s printinstruction pointers

before every instruction!

This is “Instrumentation”

2/7/2020 6.823 Spring 2020

Page 14: 6.823: Computer System Architecture Introduction to PINcsg.csail.mit.edu/6.823/Recitations/Recitation-1_Pin... · 2020. 2. 8. · Introduction to PIN Victor Ying 6823-tas@csail.mit.edu

What is Instrumentation?• A technique that inserts extra code into a program

to collect runtime information– Program Analysis: performance profiling, error

detection, capture and replay

– Architectural study: processor and cache simulation, trace collection

• Instrumentation approaches:– Source instrumentation:

• Instrument source programs

– Binary instrumentation:• Instrument executables directly

142/7/2020 6.823 Spring 2020

Page 15: 6.823: Computer System Architecture Introduction to PINcsg.csail.mit.edu/6.823/Recitations/Recitation-1_Pin... · 2020. 2. 8. · Introduction to PIN Victor Ying 6823-tas@csail.mit.edu

What can you do with Pin?

• Pin gives you the ability to

– inspect every instruction, and then

• insert extra code (optional)

15

Analysis routine

Instrumentation routine

2/7/2020 6.823 Spring 2020

Page 16: 6.823: Computer System Architecture Introduction to PINcsg.csail.mit.edu/6.823/Recitations/Recitation-1_Pin... · 2020. 2. 8. · Introduction to PIN Victor Ying 6823-tas@csail.mit.edu

How to use Pin

16

Application

Pin Instrumentation APIs

JIT Compiler Code Cache

$ pin –t pintool –- application

Pintool (User-defined plug in tool)

2/7/2020 6.823 Spring 2020

Page 17: 6.823: Computer System Architecture Introduction to PINcsg.csail.mit.edu/6.823/Recitations/Recitation-1_Pin... · 2020. 2. 8. · Introduction to PIN Victor Ying 6823-tas@csail.mit.edu

Advantages of Pin

• Dynamic Instrumentation– No need for source code, re-compilation or post-linking

• Programmable Instrumentation– Provides rich APIs to write in C/C++ your own instrumentation

tools (called Pintools)

• Multiplatform– Supports IA-32, IA-64, Itanium– Supports Linux, Windows, MacOS

• Robust– Instrument real life applications: web browsers, databases– Instrument multithreaded applications

• If you can run it, you can Pin it

172/7/2020 6.823 Spring 2020

Page 18: 6.823: Computer System Architecture Introduction to PINcsg.csail.mit.edu/6.823/Recitations/Recitation-1_Pin... · 2020. 2. 8. · Introduction to PIN Victor Ying 6823-tas@csail.mit.edu

How is Instrumentation used in Computer Architecture?

• Trace Generation

• Branch Predictor and Cache Modeling

• Fault Tolerance Study

• Emulating Speculation

• Emulating New Instructions

• Cache Coherence Protocols

182/7/2020 6.823 Spring 2020

Page 19: 6.823: Computer System Architecture Introduction to PINcsg.csail.mit.edu/6.823/Recitations/Recitation-1_Pin... · 2020. 2. 8. · Introduction to PIN Victor Ying 6823-tas@csail.mit.edu

Writing Pintools

192/7/2020 6.823 Spring 2020

Page 20: 6.823: Computer System Architecture Introduction to PINcsg.csail.mit.edu/6.823/Recitations/Recitation-1_Pin... · 2020. 2. 8. · Introduction to PIN Victor Ying 6823-tas@csail.mit.edu

Pin Instrumentation APIs• Basic APIs are architecture independent:

– Provide common functionalities like determining:• Control-flow changes

• Memory accesses

• Architecture-specific APIs– E.g., Info about segmentation registers on IA32

• Call-based APIs:– Instrumentation routines

– Analysis routines

202/7/2020 6.823 Spring 2020

Page 21: 6.823: Computer System Architecture Introduction to PINcsg.csail.mit.edu/6.823/Recitations/Recitation-1_Pin... · 2020. 2. 8. · Introduction to PIN Victor Ying 6823-tas@csail.mit.edu

Example 1: Instruction Count

sub $0xff, %edx

cmp %esi, %edx

jle <L1>

mov $0x1, %edi

add $0x10, %eax

21

Let’s increment counter by one

before every instruction!

counter++;

counter++;

counter++;

counter++;

counter++;

2/7/2020 6.823 Spring 2020

Page 22: 6.823: Computer System Architecture Introduction to PINcsg.csail.mit.edu/6.823/Recitations/Recitation-1_Pin... · 2020. 2. 8. · Introduction to PIN Victor Ying 6823-tas@csail.mit.edu

Example 1: Instruction Count

sub $0xff, %edx

cmp %esi, %edx

jle <L1>

mov $0x1, %edi

add $0x10, %eax

22

Let’s increment counter by one

before every instruction!

counter++;

counter++;

counter++;

counter++;

counter++;

Instrumentation routine

2/7/2020 6.823 Spring 2020

Page 23: 6.823: Computer System Architecture Introduction to PINcsg.csail.mit.edu/6.823/Recitations/Recitation-1_Pin... · 2020. 2. 8. · Introduction to PIN Victor Ying 6823-tas@csail.mit.edu

Example 1: Instruction Count

sub $0xff, %edx

cmp %esi, %edx

jle <L1>

mov $0x1, %edi

add $0x10, %eax

23

Let’s increment counter by one

before every instruction!

counter++;

counter++;

counter++;

counter++;

counter++;

Analysis routine

Instrumentation routine

2/7/2020 6.823 Spring 2020

Page 24: 6.823: Computer System Architecture Introduction to PINcsg.csail.mit.edu/6.823/Recitations/Recitation-1_Pin... · 2020. 2. 8. · Introduction to PIN Victor Ying 6823-tas@csail.mit.edu

Instrumentation vs. Analysis

• Instrumentation routines define where instrumentation is inserted

– e.g. before instruction

– C Occurs first time an instruction is executed

• Analysis routines define what to do when instrumentation is activated

– e.g. increment counter

– C Occurs every time an instruction is executed

242/7/2020 6.823 Spring 2020

Page 25: 6.823: Computer System Architecture Introduction to PINcsg.csail.mit.edu/6.823/Recitations/Recitation-1_Pin... · 2020. 2. 8. · Introduction to PIN Victor Ying 6823-tas@csail.mit.edu

Pintool 1: Instruction Count

sub $0xff, %edx

cmp %esi, %edx

jle <L1>

mov $0x1, %edi

add $0x10, %eax

25

counter++;

counter++;

counter++;

counter++;

counter++;

2/7/2020 6.823 Spring 2020

Page 26: 6.823: Computer System Architecture Introduction to PINcsg.csail.mit.edu/6.823/Recitations/Recitation-1_Pin... · 2020. 2. 8. · Introduction to PIN Victor Ying 6823-tas@csail.mit.edu

Pintool 1: Instruction Count Output

$ /bin/ls

Makefile atrace.o imageload.out itraceproccount Makefile.example imageloadinscount0 itrace.o proccount.o atraceimageload.o inscount0.o itrace.out

$ pin -t inscount0 -- /bin/ls

Makefile atrace.o imageload.out itraceproccount Makefile.example imageloadinscount0 itrace.o proccount.o atraceimageload.o inscount0.o itrace.out

Count 422838

262/7/2020 6.823 Spring 2020

Page 27: 6.823: Computer System Architecture Introduction to PINcsg.csail.mit.edu/6.823/Recitations/Recitation-1_Pin... · 2020. 2. 8. · Introduction to PIN Victor Ying 6823-tas@csail.mit.edu

ManualExamples/inscount0.C

instrumentation routine

analysis routine

272/8/2019 6.823 Spring 2019

#include <iostream>

#include "pin.h"

UINT64 icount = 0;

KNOB<string> KnobOutputFile(KNOB_MODE_WRITEONCE, “pintool”, “o”,

“results.out”, “specify output file”);

void docount() { icount++; }

void Instruction(INS ins, void *v)

{

INS_InsertCall(ins, IPOINT_BEFORE, (AFUNPTR)docount, IARG_END);

}

void Fini(INT32 code, void *v)

{ FILE* outfile = fopen(KnobOutputFile.Value().c_str(),”w”);

fprintf(outfile, “Count %d\n”, icount);}

int main(int argc, char * argv[])

{

PIN_Init(argc, argv);

INS_AddInstrumentFunction(Instruction, 0);

PIN_AddFiniFunction(Fini, 0);

PIN_StartProgram();

return 0;

}

Page 28: 6.823: Computer System Architecture Introduction to PINcsg.csail.mit.edu/6.823/Recitations/Recitation-1_Pin... · 2020. 2. 8. · Introduction to PIN Victor Ying 6823-tas@csail.mit.edu

ManualExamples/inscount0.C

instrumentation routine

analysis routine

28

#include <iostream>

#include "pin.h"

UINT64 icount = 0;

KNOB<string> KnobOutputFile(KNOB_MODE_WRITEONCE, “pintool”, “o”,

“results.out”, “specify output file”);

void docount() { icount++; }

void Instruction(INS ins, void *v)

{

INS_InsertCall(ins, IPOINT_BEFORE, (AFUNPTR)docount, IARG_END);

}

void Fini(INT32 code, void *v)

{ FILE* outfile = fopen(KnobOutputFile.Value().c_str(),”w”);

fprintf(outfile, “Count %d\n”, icount);}

int main(int argc, char * argv[])

{

PIN_Init(argc, argv);

INS_AddInstrumentFunction(Instruction, 0);

PIN_AddFiniFunction(Fini, 0);

PIN_StartProgram();

return 0;

}2/7/2020 6.823 Spring 2020

Page 29: 6.823: Computer System Architecture Introduction to PINcsg.csail.mit.edu/6.823/Recitations/Recitation-1_Pin... · 2020. 2. 8. · Introduction to PIN Victor Ying 6823-tas@csail.mit.edu

count()

Instrumentation Points• Instrument points relative to an instruction:

– Before (IPOINT_BEFORE)

– After: • Fall-through edge (IPOINT_AFTER)

• Taken edge (IPOINT_TAKEN_BRANCH)

29

cmp %esi, %edx

jle <L1>

mov $0x1, %edi

<L1>:

mov $0x8,%edi

count()

count()

2/7/2020 6.823 Spring 2020

Page 30: 6.823: Computer System Architecture Introduction to PINcsg.csail.mit.edu/6.823/Recitations/Recitation-1_Pin... · 2020. 2. 8. · Introduction to PIN Victor Ying 6823-tas@csail.mit.edu

Pintool 2: Instruction Trace

sub $0xff, %edx

cmp %esi, %edx

jle <L1>

mov $0x1, %edi

add $0x10, %eax

30

Print(ip);

Print(ip);

Print(ip);

Print(ip);

Print(ip);

2/7/2020 6.823 Spring 2020

Page 31: 6.823: Computer System Architecture Introduction to PINcsg.csail.mit.edu/6.823/Recitations/Recitation-1_Pin... · 2020. 2. 8. · Introduction to PIN Victor Ying 6823-tas@csail.mit.edu

Pintool 2: Instruction Trace Output$ pin -t itrace -- /bin/lsMakefile atrace.o imageload.outitrace proccountMakefile.example imageloadinscount0 itrace.o proccount.oatrace imageload.o inscount0.o itrace.out

$ head -4 itrace.out

0x40001e90

0x40001e91

0x40001ee4

0x40001ee5

312/7/2020 6.823 Spring 2020

Page 32: 6.823: Computer System Architecture Introduction to PINcsg.csail.mit.edu/6.823/Recitations/Recitation-1_Pin... · 2020. 2. 8. · Introduction to PIN Victor Ying 6823-tas@csail.mit.edu

ManualExamples/itrace.C

argument to analysis routine

analysis routine

instrumentation routine

32

#include <stdio.h>

#include "pin.H"

FILE * trace;

void printip(void *ip) { fprintf(trace, "%p\n", ip); }

void Instruction(INS ins, void *v) {

INS_InsertCall(ins, IPOINT_BEFORE, (AFUNPTR)printip, IARG_INST_PTR, IARG_END);

}

void Fini(INT32 code, void *v) { fclose(trace); }

int main(int argc, char * argv[]) {

trace = fopen("itrace.out", "w");

PIN_Init(argc, argv);

INS_AddInstrumentFunction(Instruction, 0);

PIN_AddFiniFunction(Fini, 0);

PIN_StartProgram();

return 0;

}

2/7/2020 6.823 Spring 2020

Page 33: 6.823: Computer System Architecture Introduction to PINcsg.csail.mit.edu/6.823/Recitations/Recitation-1_Pin... · 2020. 2. 8. · Introduction to PIN Victor Ying 6823-tas@csail.mit.edu

Examples of Arguments to Analysis Routine

• IARG_INST_PTR

– Instruction pointer (program counter) value

• IARG_PTR <pointer>

– A pointer to some data

• IARG_REG_VALUE <register name>

– Value of the register specified

• IARG_BRANCH_TARGET_ADDR

– Target address of the branch instrumented

• IARG_MEMORY_READ_EA

– Effective address of a memory readAnd many more … (refer to the Pin manual for details)

332/7/2020 6.823 Spring 2020

Page 34: 6.823: Computer System Architecture Introduction to PINcsg.csail.mit.edu/6.823/Recitations/Recitation-1_Pin... · 2020. 2. 8. · Introduction to PIN Victor Ying 6823-tas@csail.mit.edu

Modifying Program Behavior• Pin allows you not only to observe, but also change

program behavior

• Ways to change program behavior:

– Add/delete instructions

– Change register values

– Change memory values

– Change control flow

– Inject errors

342/7/2020 6.823 Spring 2020

Page 35: 6.823: Computer System Architecture Introduction to PINcsg.csail.mit.edu/6.823/Recitations/Recitation-1_Pin... · 2020. 2. 8. · Introduction to PIN Victor Ying 6823-tas@csail.mit.edu

Writing Efficient Pintools

(we will cover this in detail next week)

352/7/2020 6.823 Spring 2020

Page 36: 6.823: Computer System Architecture Introduction to PINcsg.csail.mit.edu/6.823/Recitations/Recitation-1_Pin... · 2020. 2. 8. · Introduction to PIN Victor Ying 6823-tas@csail.mit.edu

Reducing Instrumentation Overhead

36

Total Overhead = Pin’s Overhead + Pintool’s Overhead

• The job of Pin developers to minimize this

• ~5% for SPECfp and ~20% for SPECint

2/7/2020 6.823 Spring 2020

Page 37: 6.823: Computer System Architecture Introduction to PINcsg.csail.mit.edu/6.823/Recitations/Recitation-1_Pin... · 2020. 2. 8. · Introduction to PIN Victor Ying 6823-tas@csail.mit.edu

Reducing Instrumentation Overhead

37

Total Overhead = Pin’s Overhead + Pintool’s Overhead

• The job of Pin developers to minimize this

• ~5% for SPECfp and ~20% for SPECint

• Pintool writers can help minimize this!

2/7/2020 6.823 Spring 2020

Page 38: 6.823: Computer System Architecture Introduction to PINcsg.csail.mit.edu/6.823/Recitations/Recitation-1_Pin... · 2020. 2. 8. · Introduction to PIN Victor Ying 6823-tas@csail.mit.edu

Reducing Pintool’s Overhead

38

Pintool’s Overhead

Instrumentation Routines Overhead + Analysis Routines Overhead

Frequency of calling an Analysis Routine x Work required in the Analysis Routine

Next week, we will see how we can reduce these overheads

2/7/2020 6.823 Spring 2020

Page 39: 6.823: Computer System Architecture Introduction to PINcsg.csail.mit.edu/6.823/Recitations/Recitation-1_Pin... · 2020. 2. 8. · Introduction to PIN Victor Ying 6823-tas@csail.mit.edu

Conclusions• Instrumentation is a technique for inserting extra code

into a program to observe its behavior

• Pin is a dynamic binary instrumentation system

• Instrumentation tools (Pintools) are written in C/C++ using Pin’s rich API

• Instrumentation routines define where instrumentation is inserted

• Analysis routines define what to do when instrumentation is activated

392/7/2020 6.823 Spring 2020

Page 40: 6.823: Computer System Architecture Introduction to PINcsg.csail.mit.edu/6.823/Recitations/Recitation-1_Pin... · 2020. 2. 8. · Introduction to PIN Victor Ying 6823-tas@csail.mit.edu

Conclusions• Instrumentation is a technique for inserting extra code

into a program to observe its behavior

• Pin is a dynamic binary instrumentation system

• Instrumentation tools (Pintools) are written in C/C++ using Pin’s rich API

• Instrumentation routines define where instrumentation is inserted

• Analysis routines define what to do when instrumentation is activated

40

Lab 0 released on website

2/7/2020 6.823 Spring 2020