CCTLib
Calling-context and data-object attribution library for Intel Pin
redspy_client.cpp File Reference
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <iostream>
#include <unistd.h>
#include <assert.h>
#include <string.h>
#include <sys/mman.h>
#include <sstream>
#include <functional>
#include <unordered_set>
#include <vector>
#include <unordered_map>
#include <algorithm>
#include "pin.H"
#include "pin_isa_compat.H"
#include "cctlib.H"
+ Include dependency graph for redspy_client.cpp:

Go to the source code of this file.

Classes

struct  AddrValPair
 
struct  RedSpyThreadData
 
struct  UnrolledLoop< start, end, incr >
 
struct  UnrolledLoop< end, end, incr >
 
struct  UnrolledConjunction< start, end, incr >
 
struct  UnrolledConjunction< end, end, incr >
 
struct  RedSpyAnalysis< AccessLen, bufferOffset >
 
struct  RedSpyInstrument< readBufferSlotIndex >
 
struct  RedundacyData
 

Macros

#define PAGE_OFFSET_BITS   (16LL)
 
#define PAGE_OFFSET(addr)   (addr & 0xFFFF)
 
#define PAGE_OFFSET_MASK   (0xFFFF)
 
#define PAGE_SIZE   (1 << PAGE_OFFSET_BITS)
 
#define PTR_SIZE   (sizeof(struct Status*))
 
#define LEVEL_1_PAGE_TABLE_BITS   (20)
 
#define LEVEL_1_PAGE_TABLE_ENTRIES   (1 << LEVEL_1_PAGE_TABLE_BITS)
 
#define LEVEL_1_PAGE_TABLE_SIZE   (LEVEL_1_PAGE_TABLE_ENTRIES * PTR_SIZE)
 
#define LEVEL_2_PAGE_TABLE_BITS   (12)
 
#define LEVEL_2_PAGE_TABLE_ENTRIES   (1 << LEVEL_2_PAGE_TABLE_BITS)
 
#define LEVEL_2_PAGE_TABLE_SIZE   (LEVEL_2_PAGE_TABLE_ENTRIES * PTR_SIZE)
 
#define LEVEL_1_PAGE_TABLE_SLOT(addr)   (((addr) >> (LEVEL_2_PAGE_TABLE_BITS + PAGE_OFFSET_BITS)) & 0xfffff)
 
#define LEVEL_2_PAGE_TABLE_SLOT(addr)   (((addr) >> (PAGE_OFFSET_BITS)) & 0xFFF)
 
#define READ_ACTION   (0)
 
#define WRITE_ACTION   (0xff)
 
#define ONE_BYTE_READ_ACTION   (0)
 
#define TWO_BYTE_READ_ACTION   (0)
 
#define FOUR_BYTE_READ_ACTION   (0)
 
#define EIGHT_BYTE_READ_ACTION   (0)
 
#define ONE_BYTE_WRITE_ACTION   (0xff)
 
#define TWO_BYTE_WRITE_ACTION   (0xffff)
 
#define FOUR_BYTE_WRITE_ACTION   (0xffffffff)
 
#define EIGHT_BYTE_WRITE_ACTION   (0xffffffffffffffff)
 
#define IS_ACCESS_WITHIN_PAGE_BOUNDARY(accessAddr, accessLen)   (PAGE_OFFSET((accessAddr)) <= (PAGE_OFFSET_MASK - (accessLen)))
 
#define MAX_REDUNDANT_CONTEXTS_TO_LOG   (1000)
 
#define THREAD_MAX   (1024)
 
#define ENCODE_ADDRESS_AND_ACCESS_LEN(addr, len)   ((addr) | (((uint64_t)(len)) << 48))
 
#define DECODE_ADDRESS(addrAndLen)   ((addrAndLen) & ((1L << 48) - 1))
 
#define DECODE_ACCESS_LEN(addrAndLen)   ((addrAndLen) >> 48)
 
#define MAX_WRITE_OP_LENGTH   (512)
 
#define MAX_WRITE_OPS_IN_INS   (8)
 
#define WINDOW_ENABLE   1000000
 
#define WINDOW_DISABLE   1000000000
 
#define DECODE_DEAD(data)   static_cast<ContextHandle_t>(((data)&0xffffffffffffffff) >> 32)
 
#define DECODE_KILL(data)   (static_cast<ContextHandle_t>((data)&0x00000000ffffffff))
 
#define MAKE_CONTEXT_PAIR(a, b)   (((uint64_t)(a) << 32) | ((uint64_t)(b)))
 
#define HANDLE_CASE(NUM, BUFFER_INDEX)
 

Functions

RedSpyThreadDataClientGetTLS (const THREADID threadId)
 
static INT32 Usage ()
 
static void ClientInit (int argc, char *argv[])
 
static uint8_t * GetOrCreateShadowBaseAddress (uint64_t address)
 
static void AddToRedTable (uint64_t key, uint16_t value, THREADID threadId)
 
static VOID RecordValueBeforeLargeWrite (void *addr, UINT32 accessLen, uint32_t bufferOffset, THREADID threadId)
 
static VOID CheckAfterLargeWrite (UINT32 accessLen, uint32_t bufferOffset, uint32_t opaqueHandle, THREADID threadId)
 
static int GetNumWriteOperandsInIns (INS ins, UINT32 &whichOp)
 
static VOID InstrumentInsCallback (INS ins, VOID *v, const uint32_t opaqueHandle)
 
static bool RedundacyCompare (const struct RedundacyData &first, const struct RedundacyData &second)
 
static void PrintRedundancyPairs (THREADID threadId)
 
static void HPCRunRedundancyPairs (THREADID threadId)
 
static VOID ImageUnload (IMG img, VOID *v)
 
static VOID ThreadFiniFunc (THREADID threadid, const CONTEXT *ctxt, INT32 code, VOID *v)
 
static VOID FiniFunc (INT32 code, VOID *v)
 
static void InitThreadData (RedSpyThreadData *tdata)
 
static VOID ThreadStart (THREADID threadid, CONTEXT *ctxt, INT32 flags, VOID *v)
 
int main (int argc, char *argv[])
 

Variables

__thread long long NUM_INS = 0
 
__thread bool Sample_flag = true
 
int redspy_metric_id = 0
 
static TLS_KEY client_tls_key
 
static FILE * gTraceFile
 
static uint8_t ** gL1PageTable [LEVEL_1_PAGE_TABLE_SIZE]
 
static const uint64_t READ_ACCESS_STATES [] = { 0, ONE_BYTE_READ_ACTION, TWO_BYTE_READ_ACTION, 0, FOUR_BYTE_READ_ACTION, 0, 0, 0, EIGHT_BYTE_READ_ACTION}
 
static const uint64_t WRITE_ACCESS_STATES [] = { 0, ONE_BYTE_WRITE_ACTION, TWO_BYTE_WRITE_ACTION, 0, FOUR_BYTE_WRITE_ACTION, 0, 0, 0, EIGHT_BYTE_WRITE_ACTION}
 
static const uint8_t OVERFLOW_CHECK [] = { 0, 0, 0, 1, 2, 3, 4, 5, 6}
 
static unordered_map< uint64_t, uint64_t > RedMap [THREAD_MAX]
 

Macro Definition Documentation

◆ DECODE_ACCESS_LEN

#define DECODE_ACCESS_LEN (   addrAndLen)    ((addrAndLen) >> 48)

Definition at line 73 of file redspy_client.cpp.

◆ DECODE_ADDRESS

#define DECODE_ADDRESS (   addrAndLen)    ((addrAndLen) & ((1L << 48) - 1))

Definition at line 72 of file redspy_client.cpp.

◆ DECODE_DEAD

#define DECODE_DEAD (   data)    static_cast<ContextHandle_t>(((data)&0xffffffffffffffff) >> 32)

Definition at line 83 of file redspy_client.cpp.

◆ DECODE_KILL

#define DECODE_KILL (   data)    (static_cast<ContextHandle_t>((data)&0x00000000ffffffff))

Definition at line 84 of file redspy_client.cpp.

◆ EIGHT_BYTE_READ_ACTION

#define EIGHT_BYTE_READ_ACTION   (0)

Definition at line 57 of file redspy_client.cpp.

◆ EIGHT_BYTE_WRITE_ACTION

#define EIGHT_BYTE_WRITE_ACTION   (0xffffffffffffffff)

Definition at line 62 of file redspy_client.cpp.

◆ ENCODE_ADDRESS_AND_ACCESS_LEN

#define ENCODE_ADDRESS_AND_ACCESS_LEN (   addr,
  len 
)    ((addr) | (((uint64_t)(len)) << 48))

Definition at line 71 of file redspy_client.cpp.

◆ FOUR_BYTE_READ_ACTION

#define FOUR_BYTE_READ_ACTION   (0)

Definition at line 56 of file redspy_client.cpp.

◆ FOUR_BYTE_WRITE_ACTION

#define FOUR_BYTE_WRITE_ACTION   (0xffffffff)

Definition at line 61 of file redspy_client.cpp.

◆ HANDLE_CASE

#define HANDLE_CASE (   NUM,
  BUFFER_INDEX 
)
Value:
case (NUM): { \
INS_InsertPredicatedCall(ins, IPOINT_BEFORE, (AFUNPTR)RedSpyAnalysis<(NUM), (BUFFER_INDEX)>::RecordNByteValueBeforeWrite, IARG_MEMORYOP_EA, memOp, IARG_THREAD_ID, IARG_END); \
INS_InsertPredicatedCall(ins, IPOINT_AFTER, (AFUNPTR)RedSpyAnalysis<(NUM), (BUFFER_INDEX)>::CheckNByteValueAfterWrite, IARG_UINT32, opaqueHandle, IARG_THREAD_ID, IARG_INST_PTR, IARG_END); \
} break

Definition at line 432 of file redspy_client.cpp.

◆ IS_ACCESS_WITHIN_PAGE_BOUNDARY

#define IS_ACCESS_WITHIN_PAGE_BOUNDARY (   accessAddr,
  accessLen 
)    (PAGE_OFFSET((accessAddr)) <= (PAGE_OFFSET_MASK - (accessLen)))

Definition at line 65 of file redspy_client.cpp.

◆ LEVEL_1_PAGE_TABLE_BITS

#define LEVEL_1_PAGE_TABLE_BITS   (20)

Definition at line 38 of file redspy_client.cpp.

◆ LEVEL_1_PAGE_TABLE_ENTRIES

#define LEVEL_1_PAGE_TABLE_ENTRIES   (1 << LEVEL_1_PAGE_TABLE_BITS)

Definition at line 39 of file redspy_client.cpp.

◆ LEVEL_1_PAGE_TABLE_SIZE

#define LEVEL_1_PAGE_TABLE_SIZE   (LEVEL_1_PAGE_TABLE_ENTRIES * PTR_SIZE)

Definition at line 40 of file redspy_client.cpp.

◆ LEVEL_1_PAGE_TABLE_SLOT

#define LEVEL_1_PAGE_TABLE_SLOT (   addr)    (((addr) >> (LEVEL_2_PAGE_TABLE_BITS + PAGE_OFFSET_BITS)) & 0xfffff)

Definition at line 46 of file redspy_client.cpp.

◆ LEVEL_2_PAGE_TABLE_BITS

#define LEVEL_2_PAGE_TABLE_BITS   (12)

Definition at line 42 of file redspy_client.cpp.

◆ LEVEL_2_PAGE_TABLE_ENTRIES

#define LEVEL_2_PAGE_TABLE_ENTRIES   (1 << LEVEL_2_PAGE_TABLE_BITS)

Definition at line 43 of file redspy_client.cpp.

◆ LEVEL_2_PAGE_TABLE_SIZE

#define LEVEL_2_PAGE_TABLE_SIZE   (LEVEL_2_PAGE_TABLE_ENTRIES * PTR_SIZE)

Definition at line 44 of file redspy_client.cpp.

◆ LEVEL_2_PAGE_TABLE_SLOT

#define LEVEL_2_PAGE_TABLE_SLOT (   addr)    (((addr) >> (PAGE_OFFSET_BITS)) & 0xFFF)

Definition at line 47 of file redspy_client.cpp.

◆ MAKE_CONTEXT_PAIR

#define MAKE_CONTEXT_PAIR (   a,
 
)    (((uint64_t)(a) << 32) | ((uint64_t)(b)))

Definition at line 87 of file redspy_client.cpp.

◆ MAX_REDUNDANT_CONTEXTS_TO_LOG

#define MAX_REDUNDANT_CONTEXTS_TO_LOG   (1000)

Definition at line 68 of file redspy_client.cpp.

◆ MAX_WRITE_OP_LENGTH

#define MAX_WRITE_OP_LENGTH   (512)

Definition at line 76 of file redspy_client.cpp.

◆ MAX_WRITE_OPS_IN_INS

#define MAX_WRITE_OPS_IN_INS   (8)

Definition at line 77 of file redspy_client.cpp.

◆ ONE_BYTE_READ_ACTION

#define ONE_BYTE_READ_ACTION   (0)

Definition at line 54 of file redspy_client.cpp.

◆ ONE_BYTE_WRITE_ACTION

#define ONE_BYTE_WRITE_ACTION   (0xff)

Definition at line 59 of file redspy_client.cpp.

◆ PAGE_OFFSET

#define PAGE_OFFSET (   addr)    (addr & 0xFFFF)

Definition at line 30 of file redspy_client.cpp.

◆ PAGE_OFFSET_BITS

#define PAGE_OFFSET_BITS   (16LL)

Definition at line 29 of file redspy_client.cpp.

◆ PAGE_OFFSET_MASK

#define PAGE_OFFSET_MASK   (0xFFFF)

Definition at line 31 of file redspy_client.cpp.

◆ PAGE_SIZE

#define PAGE_SIZE   (1 << PAGE_OFFSET_BITS)

Definition at line 34 of file redspy_client.cpp.

◆ PTR_SIZE

#define PTR_SIZE   (sizeof(struct Status*))

Definition at line 37 of file redspy_client.cpp.

◆ READ_ACTION

#define READ_ACTION   (0)

Definition at line 51 of file redspy_client.cpp.

◆ THREAD_MAX

#define THREAD_MAX   (1024)

Definition at line 69 of file redspy_client.cpp.

◆ TWO_BYTE_READ_ACTION

#define TWO_BYTE_READ_ACTION   (0)

Definition at line 55 of file redspy_client.cpp.

◆ TWO_BYTE_WRITE_ACTION

#define TWO_BYTE_WRITE_ACTION   (0xffff)

Definition at line 60 of file redspy_client.cpp.

◆ WINDOW_DISABLE

#define WINDOW_DISABLE   1000000000

Definition at line 80 of file redspy_client.cpp.

◆ WINDOW_ENABLE

#define WINDOW_ENABLE   1000000

Definition at line 79 of file redspy_client.cpp.

◆ WRITE_ACTION

#define WRITE_ACTION   (0xff)

Definition at line 52 of file redspy_client.cpp.

Function Documentation

◆ AddToRedTable()

static void AddToRedTable ( uint64_t  key,
uint16_t  value,
THREADID  threadId 
)
inlinestatic

Definition at line 201 of file redspy_client.cpp.

References RedMap, and value.

Referenced by CheckAfterLargeWrite(), and RedSpyAnalysis< AccessLen, bufferOffset >::if().

◆ CheckAfterLargeWrite()

static VOID CheckAfterLargeWrite ( UINT32  accessLen,
uint32_t  bufferOffset,
uint32_t  opaqueHandle,
THREADID  threadId 
)
inlinestatic

◆ ClientGetTLS()

RedSpyThreadData* ClientGetTLS ( const THREADID  threadId)
inline

◆ ClientInit()

static void ClientInit ( int  argc,
char *  argv[] 
)
static

Definition at line 156 of file redspy_client.cpp.

References gTraceFile, and MAX_FILE_PATH.

Referenced by main().

◆ FiniFunc()

static VOID FiniFunc ( INT32  code,
VOID *  v 
)
static

Definition at line 682 of file redspy_client.cpp.

Referenced by main().

◆ GetNumWriteOperandsInIns()

static int GetNumWriteOperandsInIns ( INS  ins,
UINT32 &  whichOp 
)
static

Definition at line 439 of file redspy_client.cpp.

Referenced by InstrumentInsCallback().

◆ GetOrCreateShadowBaseAddress()

static uint8_t* GetOrCreateShadowBaseAddress ( uint64_t  address)
static

◆ HPCRunRedundancyPairs()

static void HPCRunRedundancyPairs ( THREADID  threadId)
static

◆ ImageUnload()

static VOID ImageUnload ( IMG  img,
VOID *  v 
)
static

Definition at line 664 of file redspy_client.cpp.

References gTraceFile, PrintRedundancyPairs(), and RedMap.

Referenced by main().

◆ InitThreadData()

static void InitThreadData ( RedSpyThreadData tdata)
static

Definition at line 687 of file redspy_client.cpp.

References RedSpyThreadData::bytesWritten.

Referenced by ThreadStart().

◆ InstrumentInsCallback()

static VOID InstrumentInsCallback ( INS  ins,
VOID *  v,
const uint32_t  opaqueHandle 
)
static

Definition at line 476 of file redspy_client.cpp.

References GetNumWriteOperandsInIns().

Referenced by main().

◆ main()

◆ PrintRedundancyPairs()

◆ RecordValueBeforeLargeWrite()

static VOID RecordValueBeforeLargeWrite ( void *  addr,
UINT32  accessLen,
uint32_t  bufferOffset,
THREADID  threadId 
)
inlinestatic

◆ RedundacyCompare()

static bool RedundacyCompare ( const struct RedundacyData first,
const struct RedundacyData second 
)
inlinestatic

Definition at line 577 of file redspy_client.cpp.

References RedundacyData::frequency.

Referenced by HPCRunRedundancyPairs(), and PrintRedundancyPairs().

◆ ThreadFiniFunc()

static VOID ThreadFiniFunc ( THREADID  threadid,
const CONTEXT *  ctxt,
INT32  code,
VOID *  v 
)
static

Definition at line 676 of file redspy_client.cpp.

References HPCRunRedundancyPairs(), and PinCCTLib::newCCT_hpcrun_selection_write().

Referenced by main().

◆ ThreadStart()

static VOID ThreadStart ( THREADID  threadid,
CONTEXT *  ctxt,
INT32  flags,
VOID *  v 
)
static

Definition at line 691 of file redspy_client.cpp.

References client_tls_key, and InitThreadData().

Referenced by main().

◆ Usage()

static INT32 Usage ( )
static

Definition at line 146 of file redspy_client.cpp.

Referenced by main().

Variable Documentation

◆ client_tls_key

TLS_KEY client_tls_key
static

Definition at line 107 of file redspy_client.cpp.

Referenced by ClientGetTLS(), main(), and ThreadStart().

◆ gL1PageTable

uint8_t** gL1PageTable[LEVEL_1_PAGE_TABLE_SIZE]
static

Definition at line 153 of file redspy_client.cpp.

Referenced by GetOrCreateShadowBaseAddress().

◆ gTraceFile

FILE* gTraceFile
static

Definition at line 152 of file redspy_client.cpp.

Referenced by ClientInit(), ImageUnload(), main(), and PrintRedundancyPairs().

◆ NUM_INS

__thread long long NUM_INS = 0

◆ OVERFLOW_CHECK

const uint8_t OVERFLOW_CHECK[] = { 0, 0, 0, 1, 2, 3, 4, 5, 6}
static

Definition at line 198 of file redspy_client.cpp.

◆ READ_ACCESS_STATES

const uint64_t READ_ACCESS_STATES[] = { 0, ONE_BYTE_READ_ACTION, TWO_BYTE_READ_ACTION, 0, FOUR_BYTE_READ_ACTION, 0, 0, 0, EIGHT_BYTE_READ_ACTION}
static

Definition at line 196 of file redspy_client.cpp.

◆ RedMap

unordered_map<uint64_t, uint64_t> RedMap[THREAD_MAX]
static

◆ redspy_metric_id

int redspy_metric_id = 0

Definition at line 104 of file redspy_client.cpp.

Referenced by HPCRunRedundancyPairs(), and main().

◆ Sample_flag

__thread bool Sample_flag = true

◆ WRITE_ACCESS_STATES

const uint64_t WRITE_ACCESS_STATES[] = { 0, ONE_BYTE_WRITE_ACTION, TWO_BYTE_WRITE_ACTION, 0, FOUR_BYTE_WRITE_ACTION, 0, 0, 0, EIGHT_BYTE_WRITE_ACTION}
static

Definition at line 197 of file redspy_client.cpp.