-
Notifications
You must be signed in to change notification settings - Fork 3
/
fromCuda.cpp
189 lines (164 loc) · 7.56 KB
/
fromCuda.cpp
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
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <map>
#include "global.h"
#include "executionSequence.h"
#ifdef ANALYSIS
#include "analysis.h"
#endif
#define KERNEL_PARAMETER_SCAN_RANGE 8
#define STRING_LENGTH 1024
std::map<uint64_t, uint64_t> memoryUsageTable;
pthread_mutex_t memoryUsageTableMutex = PTHREAD_MUTEX_INITIALIZER;
bool memoryUsageTableUpdateRequired = false;
void updateMemoryUsageTable(void) {
char fileName[STRING_LENGTH];
pid_t pid = getpid();
memset(fileName, 0, sizeof(fileName));
sprintf(fileName, "/proc/%d/maps", (int) pid);
FILE *inFile = fopen(fileName, "r");
if (inFile) {
pthread_mutex_lock(&memoryUsageTableMutex);
memoryUsageTable.clear();
char input[STRING_LENGTH];
while(fgets(input, sizeof(input), inFile)) {
char address[STRING_LENGTH], perm[STRING_LENGTH];
sscanf(input, "%s %s", address, perm);
if (!strstr(perm, "r")) {
continue;
}
char *sep = strstr(address, "-");
unsigned long long int startAddr, endAddr;
sscanf(address, "%llx", &startAddr);
sscanf(sep + 1, "%llx", &endAddr);
memoryUsageTable[(uint64_t) startAddr] = (uint64_t) endAddr;
}
DEBUG("Memory maps\n");
for (auto iterator = memoryUsageTable.begin(); iterator != memoryUsageTable.end(); ++iterator) {
DEBUG("Start = %llx, End = %llx\n", (unsigned long long int) iterator->first, (unsigned long long int) iterator->second);
}
pthread_mutex_unlock(&memoryUsageTableMutex);
fclose(inFile);
}
}
void fromCudaKernelSymbol(const char *symbolName) {
DEBUG("CUDA symbolName = %s\n", symbolName);
executionSequenceCudaKernel(symbolName);
}
void accessMemoryBlock(uint64_t ptr) {
pthread_mutex_lock(&allocatedMemoryBlockFromFrameworkMutex);
if (allocatedMemoryBlockFromFramework.find(ptr) != allocatedMemoryBlockFromFramework.end()) {
uint64_t size = allocatedMemoryBlockFromFramework[ptr];
pthread_mutex_unlock(&allocatedMemoryBlockFromFrameworkMutex);
DEBUG("accessMemoryBlock (from framework malloc), addr = %llx, size = %llu\n", (unsigned long long int) ptr, (unsigned long long int) size);
executionSequenceCudaAccessBlock((void*) ptr, (size_t) size);
return;
}
pthread_mutex_unlock(&allocatedMemoryBlockFromFrameworkMutex);
pthread_mutex_lock(&allMemoryBlockFromCudnnCublasMutex);
if (!controlVariables.hasFrameworkMalloc && allMemoryBlockFromCudnnCublas.find(ptr) != allMemoryBlockFromCudnnCublas.end()) {
uint64_t size = allMemoryBlockFromCudnnCublas[ptr];
pthread_mutex_unlock(&allMemoryBlockFromCudnnCublasMutex);
DEBUG("accessMemoryBlock (from cudnn/cublas), addr = %llx, size = %llu\n", (unsigned long long int) ptr, (unsigned long long int) size);
executionSequenceCudaAccessBlock((void*) ptr, (size_t) size);
return;
}
pthread_mutex_unlock(&allMemoryBlockFromCudnnCublasMutex);
pthread_mutex_lock(&allocatedMemoryBlockFromCudaMutex);
if (!controlVariables.hasFrameworkMalloc && allocatedMemoryBlockFromCuda.find(ptr) != allocatedMemoryBlockFromCuda.end()) {
uint64_t size = allocatedMemoryBlockFromCuda[ptr];
pthread_mutex_unlock(&allocatedMemoryBlockFromCudaMutex);
DEBUG("accessMemoryBlock (from cuda malloc), addr = %llx, size = %llu\n", (unsigned long long int) ptr, (unsigned long long int) size);
executionSequenceCudaAccessBlock((void*) ptr, (size_t) size);
return;
}
pthread_mutex_unlock(&allocatedMemoryBlockFromCudaMutex);
}
std::pair<uint64_t, uint64_t> getAddressBoundaryFromMemoryUsageTable(uint64_t ptr) {
pthread_mutex_lock(&memoryUsageTableMutex);
if (memoryUsageTable.size() > 0) {
auto iterator = memoryUsageTable.upper_bound(ptr);
if (iterator != memoryUsageTable.begin()) {
--iterator;
if (iterator->first <= ptr && ptr <= iterator->second) {
auto ret = std::pair<uint64_t, uint64_t>(iterator->first, iterator->second);
pthread_mutex_unlock(&memoryUsageTableMutex);
return ret;
}
}
}
pthread_mutex_unlock(&memoryUsageTableMutex);
return std::pair<uint64_t, uint64_t>(0, 0);
}
void fromCudaKernelParameters(void **kernelParams) {
if (memoryUsageTableUpdateRequired) {
updateMemoryUsageTable();
memoryUsageTableUpdateRequired = false;
}
DEBUG("fromCudaKernelParameters, kernelParams = %llx\n", (unsigned long long int) kernelParams);
uint64_t targetAddr = (uint64_t) kernelParams;
auto addressBoundary = getAddressBoundaryFromMemoryUsageTable(targetAddr);
DEBUG("fromCudaKernelParameters, address start = %llx, address end = %llx\n", (unsigned long long int) addressBoundary.first, (unsigned long long int) addressBoundary.second);
for (int index = 0; index < KERNEL_PARAMETER_SCAN_RANGE; index++) {
DEBUG("fromCudaKernelParameters, kernelParams[%d] = %llx\n", index, (unsigned long long int) kernelParams[index]);
if (addressBoundary.first != addressBoundary.second && addressBoundary.first <= (uint64_t) kernelParams[index] && (uint64_t) kernelParams[index] <= addressBoundary.second) {
DEBUG("fromCudaKernelParameters, try to deref kernelParams[%d]\n", index);
uint64_t ptr = (uint64_t) *(void**) kernelParams[index];
DEBUG("fromCudaKernelParameters, deref-kernelParams[%d] = %llx\n", index, (unsigned long long int) ptr);
accessMemoryBlock(ptr);
}
}
}
void addAllocatedMemoryBlockFromCuda(uint64_t ptr, uint64_t size) {
pthread_mutex_lock(&allocatedMemoryBlockFromCudaMutex);
allocatedMemoryBlockFromCuda[ptr] = size;
controlVariables.totalAllocatedMemorySizeFromCuda += size;
pthread_mutex_unlock(&allocatedMemoryBlockFromCudaMutex);
DEBUG("totalAllocatedMemorySizeFromCuda = %llu\n", (unsigned long long int) controlVariables.totalAllocatedMemorySizeFromCuda);
}
void invalidateMemoryBlockFromCudnnCublas(uint64_t ptr, uint64_t size) {
pthread_mutex_lock(&allMemoryBlockFromCudnnCublasMutex);
while (true) {
if (allMemoryBlockFromCudnnCublas.empty()) {
break;
}
auto iterator = allMemoryBlockFromCudnnCublas.lower_bound(ptr);
if (iterator == allMemoryBlockFromCudnnCublas.end()) {
auto last = --allMemoryBlockFromCudnnCublas.end();
if (last->first + last->second > ptr) {
allMemoryBlockFromCudnnCublas.erase(last);
}
break;
}
if (iterator->first >= ptr + size) {
break;
}
allMemoryBlockFromCudnnCublas.erase(iterator);
}
pthread_mutex_unlock(&allMemoryBlockFromCudnnCublasMutex);
}
void delAllocatedMemoryBlockFromCuda(uint64_t ptr) {
pthread_mutex_lock(&allocatedMemoryBlockFromCudaMutex);
if (allocatedMemoryBlockFromCuda.find(ptr) != allocatedMemoryBlockFromCuda.end()) {
invalidateMemoryBlockFromCudnnCublas(ptr, allocatedMemoryBlockFromCuda[ptr]);
executionSequenceRemoveBlock(ptr, allocatedMemoryBlockFromCuda[ptr]);
controlVariables.totalAllocatedMemorySizeFromCuda -= allocatedMemoryBlockFromCuda[ptr];
DEBUG("totalAllocatedMemorySizeFromCuda = %llu\n", (unsigned long long int) controlVariables.totalAllocatedMemorySizeFromCuda);
allocatedMemoryBlockFromCuda.erase(ptr);
}
pthread_mutex_unlock(&allocatedMemoryBlockFromCudaMutex);
}
void fromCudaMalloc(void* ptr, size_t size) {
DEBUG("Allocate memory block: addr = %llx, size = %llu\n", (unsigned long long int) ptr, (unsigned long long int) size);
addAllocatedMemoryBlockFromCuda((uint64_t) ptr, (uint64_t) size);
#ifdef ANALYSIS
analysisDumpCudaMalloc((uint64_t) ptr, (uint64_t) size);
#endif
memoryUsageTableUpdateRequired = true;
}
void fromCudaFree(void* ptr) {
DEBUG("Deallocate memory block: addr = %llx\n", (unsigned long long int) ptr);
delAllocatedMemoryBlockFromCuda((uint64_t) ptr);
memoryUsageTableUpdateRequired = true;
}