-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevaluate.c
More file actions
182 lines (147 loc) · 3.74 KB
/
evaluate.c
File metadata and controls
182 lines (147 loc) · 3.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static FILE *fp_trace;
struct data_stack {
char name[200];
double beginSec;
double totalSec;
int recursion;
struct data_stack* next;
};
struct depth {
char name[256];
int count;
struct depth* parent;
struct depth** child;
};
struct data_stack* functionEntered(struct data_stack *stack, struct data_stack *new) {
if (new == NULL) {
return stack;
}
if (stack == NULL) { // if we just begin, we just initialize the stack using the first record
stack = new;
stack->next = NULL;
stack->recursion = 1;
stack->totalSec = 0;
} else { // otherwise we are going to find out, if this is recursion function, or just another function call within some other function
struct data_stack *pos = stack;
while(pos != NULL) { // testing whether function is already present on the stack
if (strcmp(pos->name, new->name) == 0) { // this means that this function is called recursively (or there are 2+ consecutive function calls)
if (pos->recursion > 0) {
pos->recursion += 1;
} else { // no recursion, different function call
pos->beginSec = new->beginSec;
pos->recursion = 1;
}
free(new); // deleting temporary data_stack
return stack;
}
pos = pos->next;
}
pos = stack; // returning back to the first record on the stack
while(pos->next != NULL) { // iterating to find the last record
pos = pos->next;
}
pos->next = new;
new->next = NULL;
new->totalSec = 0;
new->recursion = 1;
}
return stack;
}
void functionExited(struct data_stack *stack, struct data_stack *new)
{
struct data_stack *pos = stack;
while(pos != NULL) {
if (strcmp(pos->name, new->name) == 0) { // if we just exited the function and previous record has the same name
if (pos->recursion > 1) {
pos->recursion -= 1;
} else {
pos->totalSec += new->beginSec - pos->beginSec;
pos->recursion = 0;
}
free(new);
return;
}
pos = pos->next;
}
free(new);
}
void printStack(struct data_stack *stack){
FILE *fp;
char functionName[250];
struct data_stack *toDel;
struct depth* root = NULL;
while(stack != NULL) {
char cmd[250];
strcpy(cmd, "addr2line -f -e ");
strcat(cmd, "main");
strcat(cmd, " ");
strcat(cmd, stack->name);
strcat(cmd," | head -1");
fp = popen( cmd , "r");
fgets(functionName, sizeof(functionName)-1, fp);
pclose(fp);
printf("Function %s %fs\n", functionName, stack->totalSec);
/*
struct depth* tree = malloc(sizeof(struct depth));
if(root == NULL) {
root = tree;
}
tree->parent = NULL;
tree->count = 0;
tree->child = NULL;
*/
toDel = stack;
stack = stack->next;
free(toDel); // deleting the records on stack while printing them
}
}
struct data_stack* load(char* file_name) {
fp_trace = fopen(file_name, "r");
if(fp_trace == NULL) {
return;
}
char line[256] = {0};
struct data_stack *out = NULL;
while(NULL != fgets(line, 255, fp_trace)) {
char name[200] = {0};
int sec;
int usec;
char type;
sscanf(line, "%c %s %*s %d %d", &type, name, &sec, &usec);
struct data_stack *data;
data = malloc(sizeof(struct data_stack));
strcpy(data->name, name);
data->beginSec = sec + (double)(usec)/1000000;
/* // lalala
struct depth* tree = malloc(sizeof(struct depth));
if(root == NULL) {
root = tree;
}
tree->parent = NULL;
tree->count = 0;
tree->child = NULL;
// lalala */
if(type == 'e') {
out = functionEntered(out, data);
} else if(type == 'x') {
functionExited(out, data);
} else {
return;
}
}
fclose(fp_trace);
return out;
}
int main(int argc, char **argv) {
struct data_stack* functions = NULL;
if(argc > 1) {
functions = load(argv[1]);
} else {
functions = load("trace.out");
}
printStack(functions);
return EXIT_SUCCESS;
}