CCTLib
Calling-context and data-object attribution library for Intel Pin
cct_metric_client.cpp
Go to the documentation of this file.
1 // @COPYRIGHT@
2 // Licensed under MIT license.
3 // See LICENSE.TXT file in the project root for more information.
4 // ==============================================================
5 
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <stdint.h>
9 #include <iostream>
10 #include <unistd.h>
11 #include <assert.h>
12 #include <string.h>
13 #include <sstream>
14 #include <sys/types.h>
15 #include <sys/stat.h>
16 #include "pin.H"
17 
18 #define HAVE_METRIC_PER_IPNODE
19 #include "cctlib.H"
20 
21 #define MAX_THREADS 1024
22 
23 using namespace std;
24 using namespace PinCCTLib;
25 
26 static INT32 Usage() {
27  PIN_ERROR("CCTLib client Pin tool to gather calling context on each instruction.\n" + KNOB_BASE::StringKnobSummary() + "\n");
28  return -1;
29 }
30 
31 
32 // Main for DeadSpy, initialize the tool, register instrumentation functions and call the target program.
33 FILE* gTraceFile;
34 
36 int ins_metric_id = 0;
37 
38 // Initialized the needed data structures before launching the target program
39 void ClientInit(int argc, char* argv[]) {
40  // Create output file
41  char name[MAX_FILE_PATH] = "client.out.";
42  char* envPath = getenv("CCTLIB_CLIENT_OUTPUT_FILE");
43 
44  if (envPath) {
45  // assumes max of MAX_FILE_PATH
46  snprintf(name, sizeof(name), "%s", envPath);
47  }
48 
49  gethostname(name + strlen(name), MAX_FILE_PATH - strlen(name));
50  pid_t pid = getpid();
51  sprintf(name + strlen(name), "%d", pid);
52  cerr << "\n Creating log file at:" << name << "\n";
53  gTraceFile = fopen(name, "w");
54  // print the arguments passed
55  fprintf(gTraceFile, "\n");
56 }
57 
58 VOID ThreadStartFunc(THREADID threadid, CONTEXT* ctxt, INT32 code, VOID* v) {
59  mm[threadid] = 0;
60 }
61 
62 VOID ThreadFiniFunc(THREADID threadid, const CONTEXT* ctxt, INT32 code, VOID* v) {
63  printf("instructionCount is %ld\n", mm[threadid]);
64  newCCT_hpcrun_write(threadid);
65 }
66 
67 // user-defined function for metric merging
68 void mergeFunc(void* des, void* src) {
69  uint64_t* m = (uint64_t*)des;
70  uint64_t* n = (uint64_t*)src;
71  *m += *n;
72 }
73 
74 // user-defined function for metric computation
75 // hpcviewer can only show the numbers for the metric
76 uint64_t computeMetricVal(void* metric) {
77  if (!metric)
78  return 0;
79  return (uint64_t) * ((uint64_t*)metric);
80 }
81 
82 // user needs to define the metrics and the method to accumulate the metrics in each node
83 VOID SimpleCCTQuery(THREADID id, const uint32_t slot) {
84  GetContextHandle(id, slot);
85  void** c_m = GetIPNodeMetric(id, slot);
86  uint64_t* m;
87  if (*c_m == nullptr) {
88  m = (uint64_t*)malloc(sizeof(uint64_t));
89  *m = 0;
90  *c_m = (void*)m;
91  } else
92  m = (uint64_t*)(*c_m);
93  (*m)++;
94  // record the number of instructions per each thread
95  mm[id]++;
96 }
97 
98 VOID InstrumentInsCallback(INS ins, VOID* v, const uint32_t slot) {
99  // only count memory store instructions
100  if (INS_IsMemoryWrite(ins))
101  INS_InsertPredicatedCall(ins, IPOINT_BEFORE, (AFUNPTR)SimpleCCTQuery, IARG_THREAD_ID, IARG_UINT32, slot, IARG_END);
102 }
103 
104 
105 int main(int argc, char* argv[]) {
106  // Initialize PIN
107  if (PIN_Init(argc, argv))
108  return Usage();
109 
110  // Initialize Symbols, we need them to report functions and lines
111  PIN_InitSymbols();
112  // Init Client
113  ClientInit(argc, argv);
114  // Intialize CCTLib
116  // Init hpcrun format output
117  init_hpcrun_format(argc, argv, mergeFunc, computeMetricVal, false);
118  ins_metric_id = hpcrun_create_metric("TOT_INS");
119 
120  // Collete data for visualization
121  PIN_AddThreadStartFunction(ThreadStartFunc, nullptr);
122  PIN_AddThreadFiniFunction(ThreadFiniFunc, nullptr);
123  // Launch program now
124  PIN_StartProgram();
125  return 0;
126 }
uint64_t computeMetricVal(void *metric)
int main(int argc, char *argv[])
FILE * gTraceFile
static INT32 Usage()
void ClientInit(int argc, char *argv[])
VOID ThreadFiniFunc(THREADID threadid, const CONTEXT *ctxt, INT32 code, VOID *v)
#define MAX_THREADS
int ins_metric_id
VOID SimpleCCTQuery(THREADID id, const uint32_t slot)
long mm[MAX_THREADS]
void mergeFunc(void *des, void *src)
VOID InstrumentInsCallback(INS ins, VOID *v, const uint32_t slot)
VOID ThreadStartFunc(THREADID threadid, CONTEXT *ctxt, INT32 code, VOID *v)
#define MAX_FILE_PATH
Definition: cctlib.H:18
#define INTERESTING_INS_ALL
Definition: cctlib.H:85
static uint64_t src[N]
uint64_t metric
Definition: cctlib.H:60
int PinCCTLibInit(IsInterestingInsFptr isInterestingIns, FILE *logFile, CCTLibInstrumentInsCallback userCallback, VOID *userCallbackArg, BOOL doDataCentric=false)
Definition: cctlib.cpp:3132
int hpcrun_create_metric(const char *name)
Definition: cctlib.cpp:4281
int newCCT_hpcrun_write(THREADID threadid)
Definition: cctlib.cpp:4291
int init_hpcrun_format(int argc, char *argv[], void(*mergeFunc)(void *des, void *src)=nullptr, uint64_t(*computeMetricVal)(void *metric)=nullptr, bool skip=false)
Definition: cctlib.cpp:4254
ContextHandle_t GetContextHandle(const THREADID id, const uint32_t slot)
Definition: cctlib.cpp:1356