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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
|
From 762c6c0c4c8618fc95aa25696345e369681d0465 Mon Sep 17 00:00:00 2001
From: Yang Yanchao <yangyanchao6@huawei.com>
Date: Fri, 25 Aug 2023 18:39:54 +0800
Subject: [PATCH] use ioctl instead of syscall
---
Makefile.am | 3 ++-
include/gmem_common.h | 16 ++++++------
include/libgmem.h | 24 +++++++++++------
src/ascend/gmem_ascend.c | 22 ++++------------
src/ascend/gmem_ascend.h | 35 -------------------------
src/init.c | 56 ++++++++++++++++++++++++++++++++++++++++
src/libgmem.c | 18 +++++++------
7 files changed, 97 insertions(+), 77 deletions(-)
delete mode 100644 src/ascend/gmem_ascend.h
create mode 100644 src/init.c
diff --git a/Makefile.am b/Makefile.am
index e18c2fc..06b1435 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -23,7 +23,8 @@ include_HEADERS = include/libgmem.h
lib_LTLIBRARIES = libgmem.la
libgmem_la_SOURCES = \
- src/libgmem.c
+ src/libgmem.c \
+ src/init.c
libgmem_la_CFLAGS = $(AM_CFLAGS) \
-I$(top_srcdir)/include
diff --git a/include/gmem_common.h b/include/gmem_common.h
index 994f953..a400b37 100644
--- a/include/gmem_common.h
+++ b/include/gmem_common.h
@@ -13,22 +13,22 @@
#ifndef _GMEM_WAPPER_H_
#define _GMEM_WAPPER_H_
+#include <libgmem.h>
#include <stdlib.h>
-struct gm_msg {
- int behavior;
- unsigned long addr;
- size_t size;
- int hnid;
-};
-typedef struct gm_msg *gm_msg_t;
-
typedef struct {
int (*FreeEager)(void *userData, void *stream);
int (*Prefetch)(void *userData, void *stream);
int (*GetNumaId)(void);
} gmem_semantics;
extern gmem_semantics gmemSemantics;
+extern int gmem_fd;
+
+typedef struct hmadvise_arg gm_msg;
+typedef gm_msg *gm_msg_t;
+
+void init_device(void);
+void destory_device(void);
int gmemAdvise(void *userData);
#endif
diff --git a/include/libgmem.h b/include/libgmem.h
index ea313a3..4cd918b 100644
--- a/include/libgmem.h
+++ b/include/libgmem.h
@@ -20,14 +20,22 @@ __BEGIN_DECLS
/*
* TODO: Remove these "ifndef" after kernel upgrade
*/
-/*
- * __NR_madvise 441
- */
-#ifndef SYS_hmdvise
-# ifndef __NR_hmadvise
-# define __NR_hmadvise 441
-# endif
-# define SYS_hmadvise __NR_hmadvise
+#include <sys/ioctl.h>
+struct hmadvise_arg {
+ int hnid;
+ unsigned long start;
+ size_t len_in;
+ int behavior;
+};
+struct gmem_hnid_arg {
+ int *hnuma_id;
+};
+#ifndef GMEM_GET_HNUMA_ID
+ #define GMEM_GET_HNUMA_ID _IOW(0x55, 1, struct gmem_hnid_arg)
+#endif
+
+#ifndef GMEM_MADVISE
+ #define GMEM_MADVISE _IOW(0x55, 2, struct hmadvise_arg)
#endif
#ifndef MADV_PREFETCH
diff --git a/src/ascend/gmem_ascend.c b/src/ascend/gmem_ascend.c
index be54813..6181c07 100644
--- a/src/ascend/gmem_ascend.c
+++ b/src/ascend/gmem_ascend.c
@@ -19,13 +19,8 @@
#include <gmem_common.h>
-#include "gmem_ascend.h"
-
#define LIB_ASCENDCL_PATH "/usr/local/Ascend/ascend-toolkit/latest/aarch64-linux/lib64/libascendcl.so"
-void init_gmemSemantics(void) __attribute__ ((constructor));
-void exit_gmemSemantics(void) __attribute__ ((destructor));
-
typedef void (*Callback_fn)(void *userData);
typedef int (*ACLRT_LAUNCH_CB_FUNC)(Callback_fn fn, void *userData, int vlockType, void *stream);
ACLRT_LAUNCH_CB_FUNC aclrtLaunchCallback_fn;
@@ -43,26 +38,19 @@ int ascend_prefech(void *userData, void *stream)
int ascend_numaid(void)
{
- int ret, id, fd;
- struct rpg_hnid_arg arg;
-
- fd = open("/dev/remote_pager", O_RDWR);
- if (fd == -1) {
- perror("Get davinci_manager fd failed");
- return -ENXIO;
- }
+ int ret, id;
+ struct gmem_hnid_arg arg;
arg.hnuma_id = &id;
- ret = ioctl(fd, RPG_GET_HNUMA_ID, &arg);
+ ret = ioctl(gmem_fd, GMEM_GET_HNUMA_ID, &arg);
if (ret < 0) {
perror("Get hnuma id ioctl failed.");
return ret;
}
- close(fd);
return id;
}
-void init_gmemSemantics()
+void init_device()
{
handle = dlopen(LIB_ASCENDCL_PATH, RTLD_LAZY);
if (!handle) {
@@ -76,7 +64,7 @@ void init_gmemSemantics()
gmemSemantics.GetNumaId = ascend_numaid;
}
-void exit_gmemSemantics()
+void destory_device()
{
dlclose(handle);
}
diff --git a/src/ascend/gmem_ascend.h b/src/ascend/gmem_ascend.h
deleted file mode 100644
index 5348c2e..0000000
--- a/src/ascend/gmem_ascend.h
+++ /dev/null
@@ -1,35 +0,0 @@
-/*
- * Copyright (c) 2023 yangyanchao6@huawei.com
- * libgmem is licensed under Mulan PSL v2.
- * You can use this software according to the terms and conditions of the Mulan PSL v2.
- * You may obtain a copy of Mulan PSL v2 at:
- * http://license.coscl.org.cn/MulanPSL2
- * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
- * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
- * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
- * See the Mulan PSL v2 for more details.
- */
-
-#ifndef _GMEM_ASCEND_H_
-#define _GMEM_ASCEND_H_
-
-/*
- * These definitions should be placed in the kernel,
- * which is a temporary implementation here.
- */
-
-#define RPG_MAGIC 0x55
-
-enum _RPG_IOCTL_CMD {
- _RPG_GET_HNUMA_ID = 1,
- _RPG_IOC_MAX_NR
-};
-
-struct rpg_hnid_arg {
- int *hnuma_id;
-};
-
-#define RPG_GET_HNUMA_ID \
- _IOW(RPG_MAGIC, _RPG_GET_HNUMA_ID, struct rpg_hnid_arg)
-
-#endif
diff --git a/src/init.c b/src/init.c
new file mode 100644
index 0000000..08b2800
--- /dev/null
+++ b/src/init.c
@@ -0,0 +1,56 @@
+/*
+ * Copyright (c) 2023 yangyanchao6@huawei.com
+ * libgmem is licensed under Mulan PSL v2.
+ * You can use this software according to the terms and conditions of the Mulan PSL v2.
+ * You may obtain a copy of Mulan PSL v2 at:
+ * http://license.coscl.org.cn/MulanPSL2
+ * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
+ * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
+ * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
+ * See the Mulan PSL v2 for more details.
+ */
+
+#include <errno.h>
+#include <linux/mman.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <fcntl.h>
+
+#include <libgmem.h>
+#include <gmem_common.h>
+
+int gmem_fd;
+
+int init_libgmem(void) __attribute__ ((constructor));
+void exit_libgmem(void) __attribute__ ((destructor));
+
+int get_gmem_fd()
+{
+ gmem_fd = open("/dev/gmem", O_RDWR);
+ if (gmem_fd == -1) {
+ perror("Get gmem fd failed");
+ return -ENXIO;
+ }
+ return 0;
+}
+
+void release_gmem_fd()
+{
+ close(gmem_fd);
+}
+
+int init_libgmem(void)
+{
+ if (get_gmem_fd()) {
+ return -ENXIO;
+ }
+ init_device();
+ return 0;
+}
+
+void exit_libgmem(void)
+{
+ release_gmem_fd();
+ destory_device();
+}
+
diff --git a/src/libgmem.c b/src/libgmem.c
index a1a1640..9a34de0 100644
--- a/src/libgmem.c
+++ b/src/libgmem.c
@@ -23,17 +23,19 @@ gmem_semantics gmemSemantics;
static void mix_userData(gm_msg_t userData, unsigned long addr, size_t length, int hnid, int behavior)
{
userData->behavior = behavior;
- userData->addr = addr;
- userData->size = length;
+ userData->start = addr;
+ userData->len_in = length;
userData->hnid = hnid;
}
int gmemAdvise(void *userData)
{
- gm_msg_t msg = (gm_msg_t)userData;
- int ret = syscall(SYS_hmadvise, msg->hnid, msg->addr, msg->size, msg->behavior);
+ int ret;
+ gm_msg msg = *(gm_msg_t)userData;
+
+ ret = ioctl(gmem_fd, GMEM_MADVISE, msg);
if(ret) {
- printf("hmadvise failed: addr:%lx, size:%lx, behavior:%d, hnid:%d\n", msg->addr, msg->size, msg->behavior, msg->hnid);
+ printf("hmadvise failed: addr:%lx, size:%lx, behavior:%d, hnid:%d\n", msg.start, msg.len_in, msg.behavior, msg.hnid);
}
return ret;
}
@@ -41,7 +43,7 @@ int gmemAdvise(void *userData)
int gmemFreeEager(unsigned long addr, size_t length, void *stream)
{
int ret = -1;
- gm_msg_t userData = (gm_msg_t)malloc(sizeof(struct gm_msg));
+ gm_msg_t userData = (gm_msg_t)malloc(sizeof(gm_msg));
mix_userData(userData, addr, length, -1, MADV_DONTNEED);
if (!stream) {
ret = gmemAdvise(userData);
@@ -55,12 +57,12 @@ int gmemFreeEager(unsigned long addr, size_t length, void *stream)
int gmemPrefetch(unsigned long addr, size_t length, void *stream)
{
int ret = -1;
- gm_msg_t userData = (gm_msg_t)malloc(sizeof(struct gm_msg));
+ gm_msg_t userData = (gm_msg_t)malloc(sizeof(gm_msg));
mix_userData(userData, addr, length, gmemGetNumaId(), MADV_PREFETCH);
if (!stream) {
ret = gmemAdvise(userData);
} else if(gmemSemantics.Prefetch != NULL) {
- ret = gmemSemantics.FreeEager(userData,stream);
+ ret = gmemSemantics.Prefetch(userData, stream);
}
free(userData);
return ret;
--
2.41.0.windows.3
|