-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathruntimeTracker.c
More file actions
232 lines (216 loc) · 7.77 KB
/
runtimeTracker.c
File metadata and controls
232 lines (216 loc) · 7.77 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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
//
// Created by Congyu Luo on 5/4/23.
//
#include "runtimeTracker.h"
// Runtime logging functions
runtimeList rList = {NULL, NULL, 0};
bool isAllocAction(enum runtimeActionType type) {
return type == MALLOC || type == CALLOC || type == VALLOC || type == ALIGNED_ALLOC;
}
bool isReallocAction(enum runtimeActionType type) {
return type == REALLOC;
}
bool isDerefAction(enum runtimeActionType type) {
return type == DEREFERENCE || type == EXT_PARAM;
}
bool isFreeAction(enum runtimeActionType type) {
return type == FREE;
}
void addRuntimeNode(runtimeNode *node) {
if (rList.head == NULL) {
rList.head = node;
rList.tail = node;
} else {
// Add node to the end of the list
rList.tail->next = node;
rList.tail = node;
}
// Increment size
rList.size++;
}
runtimeNode* basicRuntimeNode(enum runtimeActionType type, int id) {
// Allocate new node
runtimeNode *node = (runtimeNode *) malloc(sizeof(runtimeNode));
node->id = id;
node->type = type;
if (clock_gettime(CLOCK_REALTIME, &node->time) != 0) {
fprintf(stderr, "Error getting time\n");
exit(1);
}
node->next = NULL;
node->alocNode = NULL;
node->freedAllocNode = NULL;
return node;
}
void logRuntimeGeneral(enum runtimeActionType type, int id) {
runtimeNode *node = basicRuntimeNode(type, id);
// Add node to list
addRuntimeNode(node);
}
void logRuntimeAlloc(enum runtimeActionType type, int id, allocNode* newAlloc) {
runtimeNode *node = basicRuntimeNode(type, id);
node->alocNode = newAlloc;
// Add node to list
addRuntimeNode(node);
}
void logRuntimeRealloc(enum runtimeActionType type, int id, allocNode* newAlloc, allocNode* oldAlloc) {
runtimeNode *node = basicRuntimeNode(type, id);
node->alocNode = newAlloc;
node->freedAllocNode = oldAlloc;
// Add node to list
addRuntimeNode(node);
}
void logRuntimeDeref(enum runtimeActionType type, int id, allocNode* alloc) {
runtimeNode *node = basicRuntimeNode(type, id);
node->alocNode = alloc;
// Add node to list
addRuntimeNode(node);
}
void logRuntimeFree(enum runtimeActionType type, int id, allocNode* alloc) {
runtimeNode *node = basicRuntimeNode(type, id);
node->freedAllocNode = alloc;
// Add node to list
addRuntimeNode(node);
}
void deleteRuntimeList() {
runtimeNode *curr = rList.head;
runtimeNode *next = NULL;
while (curr != NULL) {
next = curr->next;
free(curr);
curr = next;
}
rList.head = NULL;
rList.tail = NULL;
rList.size = 0;
}
void printRuntimeList() {
runtimeNode *curr = rList.head;
if (curr == NULL) {
printf("List is empty\n");
return;
}
int count = 0;
while (curr != NULL) {
printf("%d: ", count);
if (isAllocAction(curr->type)) {
switch (curr->type) {
case MALLOC:
printf("MALLOC");
break;
case CALLOC:
printf("CALLOC");
break;
case VALLOC:
printf("VALLOC");
break;
case ALIGNED_ALLOC:
printf("ALIGNED_ALLOC");
break;
}
allocNode* a_Node = curr->alocNode;
printf(" [id: %d, time: %ld.%09ld] ([%p - %p], size: [%zu])\n", curr->id, curr->time.tv_sec, curr->time.tv_nsec, a_Node->ptr, a_Node->ptr + a_Node->size - 1, a_Node->size);
} else if (isReallocAction(curr->type)) {
allocNode* a_Node = curr->alocNode;
allocNode* f_Node = curr->freedAllocNode;
printf("REALLOC [id: %d, time: %ld.%09ld] ([%p - %p], size: [%zu]) -> ([%p - %p], size: [%zu])\n", curr->id, curr->time.tv_sec, curr->time.tv_nsec, f_Node->ptr, f_Node->ptr + f_Node->size - 1, f_Node->size, a_Node->ptr, a_Node->ptr + a_Node->size - 1, a_Node->size);
} else if (isDerefAction(curr->type)) {
switch (curr->type) {
case DEREFERENCE:
printf("DEREFERENCE");
break;
case EXT_PARAM:
printf("EXT_PARAM");
break;
}
allocNode* a_Node = curr->alocNode;
printf(" [id: %d, time: %ld.%09ld] ([%p - %p], size: [%zu])\n", curr->id, curr->time.tv_sec, curr->time.tv_nsec, a_Node->ptr, a_Node->ptr + a_Node->size - 1, a_Node->size);
} else if (isFreeAction(curr->type)) {
allocNode* f_Node = curr->freedAllocNode;
printf("FREE [id: %d, time: %ld.%09ld] ([%p - %p], size: [%zu])\n", curr->id, curr->time.tv_sec, curr->time.tv_nsec, f_Node->ptr, f_Node->ptr + f_Node->size - 1, f_Node->size);
} else { // General action
switch (curr->type) {
case SCOPE_ENTER:
printf("SCOPE_ENTER");
break;
case SCOPE_EXIT:
printf("SCOPE_EXIT");
break;
case RETURN:
printf("RETURN");
break;
case EXIT:
printf("EXIT");
break;
}
printf(" [id: %d, time: %ld.%09ld]\n", curr->id, curr->time.tv_sec, curr->time.tv_nsec);
}
curr = curr->next;
count++;
}
}
void exportRuntimeList() {
const char *filename = "runtime.cymp";
FILE *fp = fopen(filename, "w");
if (fp == NULL) {
fprintf(stderr, "Error opening file %s\n", filename);
exit(1);
}
runtimeNode *curr = rList.head;
while (curr != NULL) {
if (isAllocAction(curr->type)) {
switch (curr->type) {
case MALLOC:
fprintf(fp, "MLOC");
break;
case CALLOC:
fprintf(fp, "CLOC");
break;
case VALLOC:
fprintf(fp, "VLOC");
break;
case ALIGNED_ALLOC:
fprintf(fp, "ALOC");
break;
}
allocNode* a_Node = curr->alocNode;
fprintf(fp, ", %d, %ld.%09ld, %d\n", curr->id, curr->time.tv_sec, curr->time.tv_nsec, a_Node->id);
} else if (isReallocAction(curr->type)) {
allocNode* a_Node = curr->alocNode;
allocNode* f_Node = curr->freedAllocNode;
fprintf(fp, "REAL, %d, %ld.%09ld, %d, %d\n", curr->id, curr->time.tv_sec, curr->time.tv_nsec, f_Node->id, a_Node->id);
} else if (isDerefAction(curr->type)) {
switch (curr->type) {
case DEREFERENCE:
fprintf(fp, "DREF");
break;
case EXT_PARAM:
fprintf(fp, "PRAM");
break;
}
allocNode* a_Node = curr->alocNode;
fprintf(fp, ", %d, %ld.%09ld, %d\n", curr->id, curr->time.tv_sec, curr->time.tv_nsec, a_Node->id);
} else if (isFreeAction(curr->type)) {
allocNode* f_Node = curr->freedAllocNode;
fprintf(fp, "FREE, %d, %ld.%09ld, %d\n", curr->id, curr->time.tv_sec, curr->time.tv_nsec, f_Node->id);
} else { // General action
switch (curr->type) {
case SCOPE_ENTER:
fprintf(fp, "SENT");
break;
case SCOPE_EXIT:
fprintf(fp, "SEXT");
break;
case RETURN:
fprintf(fp, "RETN");
break;
case EXIT:
fprintf(fp, "EXIT");
break;
}
fprintf(fp, ", %d, %ld.%09ld\n", curr->id, curr->time.tv_sec, curr->time.tv_nsec);
}
curr = curr->next;
}
fclose(fp);
}