Skip to content

Commit

Permalink
refs none, optimized codes
Browse files Browse the repository at this point in the history
  • Loading branch information
Tengfei1010 committed Jan 16, 2018
1 parent cf354fd commit 4c6391a
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 88 deletions.
2 changes: 2 additions & 0 deletions include/AprofHook/AprofHookPass.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ struct AprofHook : public ModulePass
void SetupFunctions();
void SetupHooks();

void InstrumentCostUpdater(BasicBlock *);

void InsertAprofInit(Instruction *);
void InsertAprofIncrementCost(Instruction *);
void InsertAprofIncrementRms(Instruction *);
Expand Down
51 changes: 30 additions & 21 deletions lib/AprofHook/AprofHookPass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -153,15 +153,17 @@ void AprofHook::SetupFunctions() {
ArgTypes.clear();

// aprof_call_before

ArgTypes.push_back(this->IntType);
ArgTypes.push_back(this->LongType);
FunctionType *AprofCallBeforeType = FunctionType::get(this->VoidPointerType,
this->IntType, false);
ArgTypes, false);
this->aprof_call_before = Function::Create
(AprofCallBeforeType, GlobalValue::ExternalLinkage, "aprof_call_before", this->pModule);
this->aprof_call_before->setCallingConv(CallingConv::C);
ArgTypes.clear();

// aprof_return
ArgTypes.push_back(this->LongType);
FunctionType *AprofReturnType = FunctionType::get(this->VoidType, ArgTypes, false);
this->aprof_return = Function::Create
(AprofReturnType, GlobalValue::ExternalLinkage, "aprof_return", this->pModule);
Expand All @@ -180,15 +182,15 @@ void AprofHook::InsertAprofInit(Instruction *firstInst) {

}

void AprofHook::InsertAprofIncrementCost(Instruction *BeforeInst) {

CallInst *aprof_increment_cost_call = CallInst::Create(
this->aprof_increment_cost, "", BeforeInst);
aprof_increment_cost_call->setCallingConv(CallingConv::C);
aprof_increment_cost_call->setTailCall(false);
AttributeList void_call_PAL;
aprof_increment_cost_call->setAttributes(void_call_PAL);
void AprofHook::InstrumentCostUpdater(BasicBlock *pBlock) {
TerminatorInst *pTerminator = pBlock->getTerminator();
LoadInst *pLoadnumCost = new LoadInst(this->aprof_bb_count, "", false, pTerminator);
pLoadnumCost->setAlignment(8);
BinaryOperator *pAdd = BinaryOperator::Create(
Instruction::Add, pLoadnumCost, this->ConstantLong1, "add", pTerminator);

StoreInst *pStore = new StoreInst(pAdd, this->aprof_bb_count, false, pTerminator);
pStore->setAlignment(8);
}

void AprofHook::InsertAprofIncrementRms(Instruction *BeforeInst) {
Expand Down Expand Up @@ -275,13 +277,19 @@ void AprofHook::InsertAprofAlloc(Value *var, Instruction *AfterInst) {


void AprofHook::InsertAprofCallBefore(int FuncID, Instruction *BeforeCallInst) {
std::vector<Value *> vecParams;
LoadInst *pLoad = new LoadInst(this->aprof_bb_count, "", false, BeforeCallInst);
pLoad->setAlignment(8);

ConstantInt *const_int6 = ConstantInt::get(
this->pModule->getContext(),
APInt(32, StringRef(std::to_string(FuncID)), 10));

vecParams.push_back(const_int6);
vecParams.push_back(pLoad);

CallInst *void_46 = CallInst::Create(
this->aprof_call_before, const_int6, "", BeforeCallInst);
this->aprof_call_before, vecParams, "", BeforeCallInst);
void_46->setCallingConv(CallingConv::C);
void_46->setTailCall(false);
AttributeList void_PAL;
Expand All @@ -291,7 +299,12 @@ void AprofHook::InsertAprofCallBefore(int FuncID, Instruction *BeforeCallInst) {


void AprofHook::InsertAprofReturn(Instruction *BeforeInst) {
CallInst *void_49 = CallInst::Create(this->aprof_return, "", BeforeInst);
std::vector<Value *> vecParams;
LoadInst *pLoad = new LoadInst(this->aprof_bb_count, "", false, BeforeInst);
pLoad->setAlignment(8);
vecParams.push_back(pLoad);

CallInst *void_49 = CallInst::Create(this->aprof_return, vecParams, "", BeforeInst);
void_49->setCallingConv(CallingConv::C);
void_49->setTailCall(false);
AttributeList void_PAL;
Expand Down Expand Up @@ -332,20 +345,16 @@ void AprofHook::SetupHooks() {
for (Function::iterator BI = Func->begin(); BI != Func->end(); BI++) {

BasicBlock *BB = &*BI;
auto BB_it = BB->begin();
Instruction *firstInst = &*(BB_it);
int Inst_ID = GetInstructionID(firstInst);
while (firstInst->getOpcode() == Instruction::PHI || Inst_ID == -1) {
BB_it++;
firstInst = &*(BB_it);
Inst_ID = GetInstructionID(firstInst);
}
InsertAprofIncrementCost(firstInst);
InstrumentCostUpdater(BB);

for (BasicBlock::iterator II = BB->begin(); II != BB->end(); II++) {

Instruction *Inst = &*II;

if (GetInstructionID(Inst) == -1) {
continue;
}

switch (Inst->getOpcode()) {

case Instruction::Store: {
Expand Down
4 changes: 2 additions & 2 deletions runtime/AProfHooks/include/aproflib.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ void aprof_increment_cost();

void aprof_increment_rms();

void aprof_call_before(int funcId);
void aprof_call_before(int funcId, unsigned long numCost);

void aprof_collect();

void aprof_call_after();

void aprof_return();
void aprof_return(unsigned long numCost);

};
#endif
Expand Down
99 changes: 34 additions & 65 deletions runtime/AProfHooks/src/aproflib.cpp
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
#include <stack>
#include <map>
#include <unordered_map>

#include "aproflib.h"
#include "logger.h"

using namespace std;

unsigned long count = 0;
unsigned long bb_count = 0;
std::stack<ShadowStackElement *> shadow_stack;
std::map<unsigned long, unsigned long> ts_map;
std::unordered_map<unsigned long, unsigned long> ts_map;


void logger_init() {
Expand All @@ -28,9 +27,8 @@ int aprof_init() {

void aprof_write(void *memory_addr, unsigned int length) {
unsigned long start_addr = (unsigned long) memory_addr;
unsigned long j = start_addr + length;

for (unsigned long i = start_addr; i < j; i++) {
for (unsigned long i = start_addr; i < start_addr + length; i++) {
ts_map[i] = count;
}

Expand All @@ -46,52 +44,42 @@ void aprof_read(void *memory_addr, unsigned int length) {

for (i = start_addr; i < (start_addr + length); i++) {

if (ts_map.find(i) != ts_map.end()) {
// We assume that w has been wrote before reading.
// ts[w] > 0 and ts[w] < S[top]
if (ts_map[i] < topEle->ts) {

// ts[w] > 0 and ts[w] < S[top]
if (ts_map[i] < topEle->ts) {

topEle->rms++;
log_trace("aprof_read: (ts[i]) %ld < (S[top].ts) %ld",
ts_map[i], topEle->ts);

std::stack<ShadowStackElement *> tempStack;
while (!shadow_stack.empty()) {
topEle->rms++;
log_trace("aprof_read: (ts[i]) %ld < (S[top].ts) %ld",
ts_map[i], topEle->ts);

topEle = shadow_stack.top();
if (topEle->ts <= ts_map[i]) {
std::stack<ShadowStackElement *> tempStack;
while (!shadow_stack.empty()) {

topEle->rms--;
break;
topEle = shadow_stack.top();
if (topEle->ts <= ts_map[i]) {

} else {
topEle->rms--;
break;

tempStack.push(topEle);
shadow_stack.pop();
}
}
} else {

while (!tempStack.empty()) {
shadow_stack.push(tempStack.top());
tempStack.pop();
tempStack.push(topEle);
shadow_stack.pop();
}
}

} else {
while (!tempStack.empty()) {
shadow_stack.push(tempStack.top());
tempStack.pop();
}

// w is not in map
topEle->rms++;
}

ts_map[i] = count;
}

}

void aprof_increment_cost() {
bb_count++;

}

void aprof_increment_rms() {
ShadowStackElement *topElement = shadow_stack.top();
Expand All @@ -100,57 +88,38 @@ void aprof_increment_rms() {
}
}

void aprof_call_before(int funcId) {
void aprof_call_before(int funcId, unsigned long numCost) {
count++;
ShadowStackElement *newEle = (ShadowStackElement *) malloc(
sizeof(ShadowStackElement));
newEle->funcId = funcId;
newEle->ts = count;
newEle->rms = 0;
// newEle->cost update in aprof_call_after
newEle->cost = bb_count;
// newEle->cost update in aprof_return
newEle->cost = numCost;
shadow_stack.push(newEle);
log_trace("aprof_call_before: push new element %d", funcId);

}


void aprof_call_after() {
void aprof_return(unsigned long numCost) {

ShadowStackElement *topElement = shadow_stack.top();
topElement->cost = bb_count - topElement->cost;
log_trace("aprof_call_after: top element cost is %ld", topElement->cost);
topElement->cost = numCost - topElement->cost;

}

void collect() {
if (!shadow_stack.empty()) {
ShadowStackElement *topElement = shadow_stack.top();
log_fatal(" ID %d ; RMS %ld ; Cost %ld ;",
topElement->funcId,
topElement->rms,
topElement->cost
);
}
// log_fatal(" ID %d ; RMS %ld ; Cost %ld ;",
// topElement->funcId,
// topElement->rms,
// topElement->cost
// );

}

void aprof_return() {

aprof_call_after();
collect();
shadow_stack.pop();

if (shadow_stack.size() >= 2) {
ShadowStackElement *topEle = shadow_stack.top();
shadow_stack.pop();
ShadowStackElement *secondEle = shadow_stack.top();
secondEle->rms += topEle->rms;
secondEle->rms += topElement->rms;
log_trace("aprof_return: top element rms is %d", secondEle->rms);

} else {
ShadowStackElement *topEle = shadow_stack.top();
shadow_stack.pop();
log_trace("aprof_return: top element rms is %d", topEle->rms);
}

}
Binary file modified scripts/apache34464/a.out
Binary file not shown.

0 comments on commit 4c6391a

Please sign in to comment.