-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathcuda-wrapper2.c
278 lines (250 loc) · 7.69 KB
/
cuda-wrapper2.c
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
#include <stdio.h>
#include <stdlib.h>
#include <dlfcn.h>
#include <cuda.h>
#include <string.h>
#include <pthread.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#define SIZE 10000
unsigned long long mod = 9973L;
static const char CONFIG_STRING[] = "WRAPPER_MAX_MEMORY";
static const char LIB_STRING[] = "libcuda.so";
static const char LOG_FILENAME[] = "/tmp/wrapper-log";
int open_flag = 0;
void *handle = NULL;
static size_t total_mem = 0L;
static size_t total_quota = 4217928960L; //default set to 4GB
static pthread_mutex_t mem_cnt_lock;
char *error;
char timebuf[30];
struct HashArray
{
unsigned long long key;
size_t value;
struct HashArray* next;
}allocsize[10000];
#define checkCudaErrors(err) __checkCudaErrors (err, __FILE__, __LINE__)
CUresult __checkCudaErrors( CUresult err, const char *file, const int line ) {
if( CUDA_SUCCESS != err) {
fprintf(stderr,
"CUDA Driver API error = %04d from file <%s>, line %i.\n",
err, file, line );
//exit(-1);
}
return err;
}
void getCurrentTime(char *buff) {
struct tm *sTm;
time_t now = time (0);
sTm = gmtime (&now);
strftime (buff, sizeof(buff), "%Y-%m-%d %H:%M:%S", sTm);
}
void addHash(unsigned long long key,size_t value) {
int temp=key >> 19;
if(allocsize[temp].key==0) {
allocsize[temp].key=key;
allocsize[temp].value=value;
//printf("allocsize %lld %zu\n", key, value);
}
else if(allocsize[temp].key==key) {
allocsize[temp].value=value;
}
else {
struct HashArray *p=&allocsize[temp];
while(p->key!=key&&p->next!=NULL) {
p=p->next;
}
if(p->key==key) {
p->value=value;
}
else {
p->next=(struct HashArray*)malloc(sizeof(struct HashArray));
p=p->next;
p->key=key;
p->value=value;
p->next=NULL;
}
}
getCurrentTime(timebuf);
printf("addHash\nTime: %s addHash: key: %lld value: %zu\n", timebuf, key, value);
}
size_t getHash(unsigned long long key) {
int temp=key%mod;
struct HashArray *p=&allocsize[temp];
if (p == NULL) {
printf("getHash miss\n");
getCurrentTime(timebuf);
printf("Time: %s key: %lld \n", timebuf, key );
return 0;
}
//printf("pkey: %lld\n", p->key);
while(p->key!=key&&p->next!=NULL) {
p=p->next;
}
if (p->key == key) {
printf("getHash hit\n");
getCurrentTime(timebuf);
printf("Time: %s key: %lld value: %zu \n", timebuf, key ,p->value);
return p->value;
}
else {
printf("hash hit and miss\n");
getCurrentTime(timebuf);
printf("Time: %s key: %lld \n", timebuf, key );
return 0;
}
}
void set_quota() {
char *q = NULL;
q = getenv(CONFIG_STRING);
if (q == NULL) {
//printf("set_quota: no env %s found. use default: %zu", CONFIG_STRING, total_quota);
}
else {
total_quota = strtoull(q, NULL, 10);
//printf("set_quota: set total_quota: %zu", total_quota);
}
}
void init_func() {
int fd;
fd = open(LOG_FILENAME, O_WRONLY | O_CREAT, 0644);
if (fd == -1) {
perror("open log file failed");
exit(1);
}
if (dup2(fd, 1) == -1) {
perror("dup2 failed");
exit(1);
}
if(open_flag == 0 && handle == NULL) {
//char *error;
handle = dlopen (LIB_STRING, RTLD_LAZY);
if (!handle) {
fprintf (stderr, "%s\n", dlerror());
exit(1);
}
open_flag = 1;
dlerror();
}
pthread_mutex_init(&mem_cnt_lock, NULL);
set_quota();
printf("Init!\n");
getCurrentTime(timebuf);
printf("Time: %s total_quota: %zu\n", timebuf, total_quota);
}
void before_func() {
}
void post_func() {
}
CUresult cuInit(unsigned int Flags) {
init_func();
before_func();
//printf("init!!!\n");
CUresult (*fakecuInit)(unsigned int);
fakecuInit = dlsym(handle, "cuInit");
if ((error = dlerror()) != NULL) {
fprintf (stderr, "%s\n", error);
exit(1);
}
post_func();
CUresult r;
r = checkCudaErrors((*fakecuInit)(Flags));
return r;
}
CUresult cuMemGetInfo_v2(size_t *free, size_t *total) {
before_func();
CUresult (*fakecuMemGetInfo_v2)(size_t *, size_t *);
fakecuMemGetInfo_v2 = dlsym(handle, "cuMemGetInfo_v2");
if ((error = dlerror()) != NULL) {
fprintf (stderr, "%s\n", error);
exit(1);
}
CUresult r;
r = checkCudaErrors((*fakecuMemGetInfo_v2)(free, total));
//TODO: change free and total to proper value
//*free = *free / 2;
//*total = *total / 2;
//printf("cumemgetinfo: free : %zu, total : %zu\n", *free, *total);
post_func();
return r;
}
int check_alloc_valid(size_t bytesize) {
//printf("lock mem in check_alloc_valid\n");
pthread_mutex_lock(&mem_cnt_lock);
if(total_mem + bytesize > total_quota) {
fprintf (stderr, "alloc %zu failed, total_mem %zu, quota %zu\n", bytesize, total_mem, total_quota);
//printf("unlock mem in check_alloc_valid\n");
pthread_mutex_unlock(&mem_cnt_lock);
return 0;
}
//printf("unlock mem in check\n");
pthread_mutex_unlock(&mem_cnt_lock);
return 1;
}
CUresult cuMemAlloc_v2(CUdeviceptr* dptr, size_t bytesize) {
before_func();
CUresult (*fakecuMemAlloc_v2)(CUdeviceptr* , size_t);
fakecuMemAlloc_v2 = dlsym(handle, "cuMemAlloc_v2");
if ((error = dlerror()) != NULL) {
fprintf (stderr, "%s\n", error);
exit(1);
}
post_func();
if(check_alloc_valid(bytesize)) {
//printf("lock mem in cumemalloc\n");
pthread_mutex_lock(&mem_cnt_lock);
total_mem += bytesize;
//printf("%d\n", dptr);
//printf("bs: %zu\n", bytesize);
pthread_mutex_unlock(&mem_cnt_lock);
//printf("unlock mem in cumemalloc\n");
CUresult r = checkCudaErrors((*fakecuMemAlloc_v2)(dptr, bytesize));
if(CUDA_SUCCESS != r) {
//printf("lock if r != CUDA_SUCCESS\n");
pthread_mutex_lock(&mem_cnt_lock);
total_mem -= bytesize;
pthread_mutex_unlock(&mem_cnt_lock);
//printf("unlock if r != CUDA_SUCCESS\n ");
}
else {
addHash((unsigned long long)dptr,bytesize);
//TODO: assert
//printf("cumemalloc: hash insert with bytesize %zu\n", bytesize);
//printf("cumemalloc: hash insert with bytesize %zu\n", getHash((unsigned long long)dptr));
getCurrentTime(timebuf);
printf("cuMemAlloc_v2\nTime: %s total_mem: %zu bytesize: %zu total_quota: %zu \n", timebuf, total_mem, bytesize, total_quota);
}
return r;
}
else {
return CUDA_ERROR_OUT_OF_MEMORY;
}
}
CUresult cuMemFree_v2(CUdeviceptr dptr) {
before_func();
CUresult (*fakecuMemFree_v2)(CUdeviceptr);
fakecuMemFree_v2 = dlsym(handle, "cuMemFree_v2");
if ((error = dlerror()) != NULL) {
fprintf (stderr, "%s\n", error);
exit(1);
}
post_func();
CUresult r = checkCudaErrors((*fakecuMemFree_v2)(dptr));
if(CUDA_SUCCESS == r) {
//printf("lock mem in cumemfree\n");
pthread_mutex_lock(&mem_cnt_lock);
size_t tbytesize=getHash(dptr);
//printf("%lld %zu",dptr, tbytesize);
total_mem -= tbytesize;
//printf("lock mem in cumemfree\n");
pthread_mutex_unlock(&mem_cnt_lock);
getCurrentTime(timebuf);
printf("cuMemFree_v2\nTime: %s total_mem: %zu bytesize: %zu total_quota: %zu \n", timebuf, total_mem, tbytesize, total_quota);
}
return r;
}