Skip to content

Commit

Permalink
refs #1, update c++ stl stack to c stack
Browse files Browse the repository at this point in the history
  • Loading branch information
Tengfei1010 committed Jan 16, 2018
1 parent 087cf37 commit 8336d4e
Show file tree
Hide file tree
Showing 5 changed files with 158 additions and 23 deletions.
1 change: 1 addition & 0 deletions runtime/AProfHooks/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Source files to be used in the library
set(LIB_SOURCES
src/logger.c
src/stack.c
src/aproflib.cpp
)

Expand Down
8 changes: 0 additions & 8 deletions runtime/AProfHooks/include/aproflib.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,6 @@
#define APROFHOOKS_LIBRARY_H


typedef struct _ShadowStackElem {
int funcId; // function id
unsigned long ts; // time stamp
unsigned long rms;
unsigned long cost;
} ShadowStackElement;


#ifdef __cplusplus
extern "C" {

Expand Down
50 changes: 50 additions & 0 deletions runtime/AProfHooks/include/stack.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#ifndef COMAIR_STACK_H
#define COMAIR_STACK_H

#include <stdint.h>
#include <stddef.h>

struct stack_elem {
int funcId; // function id
unsigned long ts; // time stamp
unsigned long rms;
unsigned long cost;
struct stack_elem *next;
};

struct stack {
struct stack_elem *top;
};

#ifdef __cplusplus
extern "C" {


/*
* Converts a pointer to the stack's element ptr into a pointer
* to the structure that ptr is embedded inside. Supply the name
* of the outer struct, 'type', and the member name of the stack
* element 'member'. Taken from the Linux kernel container_of().
*/
#define stack_entry(ptr, type, member) ({ \
const typeof( ((type *)0)->member ) *__mptr = (ptr); \
(type *)( (char *)__mptr - offsetof(type,member) );})

void stack_init(struct stack *stack);
void push(struct stack *stack, struct stack_elem *elem);
struct stack_elem *pop(struct stack *);
struct stack_elem *top(struct stack *);
int size(struct stack *);
int isEmpty(struct stack *);

/*
* Returns a stack with the cleared nodes contained.
* You must iterate through the stack, convert the pointers
* and free the pointers manually.
*/
struct stack clear(struct stack *);

};
#endif

#endif //COMAIR_STACK_H
34 changes: 19 additions & 15 deletions runtime/AProfHooks/src/aproflib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,24 @@

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

using namespace std;

unsigned long count = 0;
std::vector<ShadowStackElement *> shadow_stack;
std::unordered_map<unsigned long, unsigned long> ts_map;
struct stack *shadow_stack = (struct stack *) malloc(
sizeof(struct stack));

std::unordered_map<unsigned long, unsigned long> ts_map;

int aprof_init() {
const char *FILENAME = "aprof_logger.txt";
int LEVEL = 4; // "TRACE" < "DEBUG" < "INFO" < "WARN" < "ERROR" < "FATAL"
int QUIET = 1;
FILE *fp = fopen(FILENAME, "w");
log_init(fp, LEVEL, QUIET);
stack_init(shadow_stack);

}

void aprof_write(void *memory_addr, unsigned int length) {
Expand All @@ -34,7 +38,7 @@ void aprof_write(void *memory_addr, unsigned int length) {
void aprof_read(void *memory_addr, unsigned int length) {
unsigned long i;
unsigned long start_addr = (unsigned long) memory_addr;
ShadowStackElement *topEle = shadow_stack.back();
struct stack_elem *topEle = top(shadow_stack);

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

Expand All @@ -47,13 +51,13 @@ void aprof_read(void *memory_addr, unsigned int length) {
ts_map[i], topEle->ts);

if (ts_map[i] != 0) {
auto rit = shadow_stack.rbegin();
for (; rit != shadow_stack.rend(); ++rit) {
topEle = *rit;
while (topEle->next != NULL) {
if (topEle->ts <= ts_map[i]) {
topEle->rms--;
break;
}

topEle = topEle->next;
}
}
}
Expand All @@ -65,30 +69,30 @@ void aprof_read(void *memory_addr, unsigned int length) {


void aprof_increment_rms() {
ShadowStackElement *topElement = shadow_stack.back();
if (topElement) {
struct stack_elem *topElement = top(shadow_stack);
if (topElement != NULL) {
topElement->rms++;
}
}

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

}


void aprof_return(unsigned long numCost) {

ShadowStackElement *topElement = shadow_stack.back();
struct stack_elem *topElement = top(shadow_stack);
topElement->cost = numCost - topElement->cost;

log_fatal(" ID %d ; RMS %ld ; Cost %ld ;",
Expand All @@ -97,10 +101,10 @@ void aprof_return(unsigned long numCost) {
topElement->cost
);

shadow_stack.pop_back();
pop(shadow_stack);

if (shadow_stack.size() >= 2) {
ShadowStackElement *secondEle = shadow_stack.back();
if (size(shadow_stack) >= 2) {
struct stack_elem *secondEle = top(shadow_stack);
secondEle->rms += topElement->rms;
log_trace("aprof_return: top element rms is %d", secondEle->rms);
}
Expand Down
88 changes: 88 additions & 0 deletions runtime/AProfHooks/src/stack.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
#include "stack.h"
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>

void stack_init(struct stack *stack) {
assert(stack != NULL);
stack->top = NULL;
}

void push(struct stack *stack, struct stack_elem *elem) {
elem->next = stack->top;
stack->top = elem;
}

struct stack_elem * pop(struct stack *stack) {

if (isEmpty(stack) == 1)
return NULL;

else {
struct stack_elem *node = stack->top;
stack->top = stack->top->next;
return node;
}
}

struct stack_elem * top(struct stack *stack) {

if (isEmpty(stack) == 1)
return NULL;

else
return stack->top;
}

int size(struct stack *stack) {

if (isEmpty(stack) == 1)
return 0;

else {
struct stack_elem *temp;
temp = stack->top;

int count = 0;
while (temp != NULL) {
count++;
temp = temp->next;
}

return count;
}
}

int isEmpty(struct stack *stack) {

if (stack->top == NULL)
return 1;

else
return 0;
}

struct stack clear(struct stack *stack) {

if (isEmpty(stack) == 0) {

struct stack cleared_nodes;
stack_init(&cleared_nodes);

struct stack_elem *elem;
elem = stack->top;

while (elem != NULL) {

push(&cleared_nodes, elem);

elem = stack->top->next;
stack->top = NULL;
stack->top = elem;
}

stack->top = NULL;

return cleared_nodes;
}
}

0 comments on commit 8336d4e

Please sign in to comment.