Skip to content

Commit 70dce28

Browse files
Adrien Rousseladamariej
authored andcommitted
Cea/deepsea/het memory
1 parent b65708d commit 70dce28

9 files changed

+378
-4
lines changed

CHANGELOG

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,16 @@
22

33
All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog][Keep a Changelog].
44

5+
## [0.3]
6+
7+
### Added
8+
9+
- Support for Persistent Memory
10+
11+
### Fixed
12+
13+
- Debug code is now compiled in debug builds, not in release builds
14+
515
## [0.2]
616

717
### Changed
@@ -15,5 +25,6 @@ All notable changes to this project will be documented in this file. The format
1525

1626
<!-- Links -->
1727
[Keep a Changelog]: https://keepachangelog.com/
28+
[0.3] https://france.paratools.com/mpcalloc-0.3.tar.gz
1829
[0.2] https://france.paratools.com/mpcalloc-0.2.tar.gz
1930
[0.1]: https://france.paratools.com/mpcalloc-0.1.tar.gz

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ endif(NOT DISABLE_UNIT_TESTS)
6969
#Files to install
7070
install(FILES include/mpcalloc.h DESTINATION include/)
7171

72-
install(FILES src/sctk_alloc_posix.h src/sctk_alloc_common.h src/sctk_alloc_to_recode.h src/sctk_alloc_on_node.h DESTINATION include/mpcalloc/)
72+
install(FILES src/sctk_alloc_posix.h src/sctk_alloc_common.h src/sctk_alloc_to_recode.h src/sctk_alloc_on_node.h src/sctk_alloc_pmem.h DESTINATION include/mpcalloc/)
7373

7474
#Pkg-Config
7575

src/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ endfunction (cm_export_file)
1111
#################### DEFINE SOURCES #####################
1212
#Message("CMAKE_BUILD_TYPE = ${CMAKE_BUILD_TYPE}")
1313
#if (${CMAKE_BUILD_TYPE} STREQUAL "Debug")
14-
set(alloc_SRC sctk_alloc_debug.c sctk_allocator.c sctk_alloc_posix.c sctk_alloc_topology.c sctk_alloc_light_mm_source.c sctk_alloc_common.c sctk_alloc_numa_stat.c sctk_alloc_on_node.c sctk_alloc_config.c sctk_alloc_hooks.c sctk_alloc_to_recode.c)
14+
set(alloc_SRC sctk_alloc_debug.c sctk_allocator.c sctk_alloc_posix.c sctk_alloc_topology.c sctk_alloc_light_mm_source.c sctk_alloc_common.c sctk_alloc_numa_stat.c sctk_alloc_on_node.c sctk_alloc_config.c sctk_alloc_hooks.c sctk_alloc_to_recode.c sctk_alloc_pmem.c)
1515
#else()
1616
# set(alloc_SRC sctk_allocator.c sctk_alloc_posix.c sctk_alloc_topology.c sctk_alloc_light_mm_source.c sctk_alloc_common.c sctk_alloc_numa_stat.c)
1717
#endif()

src/sctk_alloc_on_node.c

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ static struct sctk_alloc_mm_source_light sctk_global_alloc_on_node_mm_src[SCTK_M
4949
static bool sctk_global_alloc_on_node_initilized = false;
5050
#endif //HAVE_HWLOC
5151

52+
5253
/************************* FUNCTION ************************/
5354
/**
5455
* Global initialisation of the array of allocation chain used for malloc on node.
@@ -169,6 +170,34 @@ void *sctk_malloc_on_node_numa(size_t size, int node) {
169170
}
170171
#endif //HAVE_HWLOC
171172

173+
/************************* FUNCTION ************************/
174+
/**
175+
* Request memory allocation on a MCDRAM memory bank (Intel KNL processor).
176+
* @param size Size of the memory bloc to allocate.
177+
**/
178+
#ifdef HAVE_HWLOC
179+
SCTK_PUBLIC void *sctk_hbw_malloc(size_t size)
180+
{
181+
int mcdram_node = -1;
182+
//detection of possible MCDRAM memory bank
183+
// HWLOC version >= 2.0 is required to detect memory type
184+
#if (HWLOC_API_VERSION >= 0x00020000)
185+
mcdram_node = sctk_topology_mcdram_detection();
186+
#endif
187+
if(mcdram_node == -1)
188+
{ // no MCDRAM found, forward allocation to numa bank 0
189+
mcdram_node = 0;
190+
}
191+
192+
return sctk_malloc_on_node_numa(size, mcdram_node);
193+
}
194+
195+
SCTK_PUBLIC void sctk_hbw_free(void *ptr)
196+
{
197+
return free(ptr);
198+
}
199+
#endif
200+
172201
/************************* FUNCTION ************************/
173202
/**
174203
* Request memory allocation on a specific NUMA node.

src/sctk_alloc_on_node.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ void sctk_malloc_on_node_reset(void);
4545
//optional intern function depend on NUMA status
4646
#ifdef HAVE_HWLOC
4747
void *sctk_malloc_on_node_numa(size_t size, int node);
48+
void *sctk_hbw_malloc(size_t size);
49+
void sctk_hbw_free(void *ptr);
4850
struct sctk_alloc_chain *sctk_malloc_on_node_get_chain(int node);
4951
#endif //HAVE_HWLOC
5052

src/sctk_alloc_pmem.c

Lines changed: 219 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,219 @@
1+
/* ############################# MPC License ############################## */
2+
/* # Wed Nov 19 15:19:19 CET 2008 # */
3+
/* # Copyright or (C) or Copr. Commissariat a l'Energie Atomique # */
4+
/* # # */
5+
/* # IDDN.FR.001.230040.000.S.P.2007.000.10000 # */
6+
/* # This file is part of the MPC Runtime. # */
7+
/* # # */
8+
/* # This software is governed by the CeCILL-C license under French law # */
9+
/* # and abiding by the rules of distribution of free software. You can # */
10+
/* # use, modify and/ or redistribute the software under the terms of # */
11+
/* # the CeCILL-C license as circulated by CEA, CNRS and INRIA at the # */
12+
/* # following URL http://www.cecill.info. # */
13+
/* # # */
14+
/* # The fact that you are presently reading this means that you have # */
15+
/* # had knowledge of the CeCILL-C license and that you accept its # */
16+
/* # terms. # */
17+
/* # # */
18+
/* # Authors: # */
19+
/* # - Roussel Adrien [email protected] # */
20+
/* # # */
21+
/* ######################################################################## */
22+
23+
#include "sctk_alloc_pmem.h"
24+
25+
SCTK_PUBLIC sctk_pmem_desc_t* sctk_pmem_create(const char* dir, size_t* size)
26+
{
27+
28+
sctk_pmem_desc_t *cur_desc = NULL;
29+
cur_desc = (sctk_pmem_desc_t*) malloc(sizeof(sctk_pmem_desc_t));
30+
cur_desc->cur_size = 0x0;
31+
cur_desc->max_size = 0x0;
32+
cur_desc->ptr = NULL;
33+
cur_desc->start = NULL;
34+
35+
SCTK_PMEM_FILE_TYPE type = TYPE_ERROR;
36+
long int alignment = sysconf(_SC_PAGE_SIZE);
37+
38+
/* Print Banner */
39+
fprintf(stdout, "System Page size: %ld\n", alignment);
40+
41+
type = get_file_type(dir);
42+
43+
switch(type)
44+
{
45+
case TYPE_REGULAR:
46+
fprintf(stdout, "File Type: Regular\n");
47+
break;
48+
case TYPE_DEVDAX:
49+
fprintf(stdout, "File Type: Device\n");
50+
break;
51+
case TYPE_ERROR:
52+
default:
53+
fprintf(stdout, "File Type: Unknown\n");
54+
return NULL;
55+
}
56+
57+
*size = SCTK_ROUNDUP(*size, alignment);
58+
fprintf(stdout, "Requested size: %ld bytes\n", *size);
59+
60+
if(type == TYPE_REGULAR)
61+
{
62+
#ifdef O_TMPFILE
63+
64+
int fd = create_tmp_file(dir);
65+
66+
/* Declare random access pattern for fd */
67+
if(declare_random_access(fd, *size) != 0)
68+
{
69+
fprintf(stderr, "Error: fail of advise access pattern for pmem\n");
70+
return NULL;
71+
}
72+
73+
/* Allocate the file space for fd */
74+
if(allocate_file_space(fd, *size) != 0)
75+
{
76+
fprintf(stderr, "Error: unable to ensure that pmem space has been allocated..\n");
77+
return NULL;
78+
}
79+
80+
void* base = mmap(NULL, *size, PROT_READ|PROT_WRITE, MAP_SHARED_VALIDATE|MAP_SYNC, fd, 0);
81+
if(base == NULL)
82+
{
83+
fprintf(stderr, "Error: unable to map a file into %s\n", dir);
84+
return NULL;
85+
}
86+
87+
close(fd);
88+
89+
cur_desc->max_size = *size;
90+
cur_desc->ptr = base;
91+
cur_desc->start = base;
92+
93+
return cur_desc;
94+
95+
#else
96+
fprintf(stderr, "O_TMPFILE is not defined on the system...\n");
97+
return NULL;
98+
#endif
99+
}
100+
else if(type == TYPE_DEVDAX)
101+
{
102+
fprintf(stderr, "Error: not supported now...\n");
103+
free(cur_desc);
104+
return NULL;
105+
}
106+
107+
free(cur_desc);
108+
return NULL;
109+
}
110+
111+
SCTK_PUBLIC int sctk_pmem_destroy(sctk_pmem_desc_t* base, size_t size)
112+
{
113+
int ret = munmap(base->start, size);
114+
base->ptr = NULL;
115+
free(base);
116+
return ret;
117+
}
118+
119+
SCTK_INTERN SCTK_PMEM_FILE_TYPE get_file_type(const char* dir)
120+
{
121+
fprintf(stdout, "Checking directory \"%s\"...\n", dir);
122+
123+
/* Check the access to the file 'dev' */
124+
if(access(dir, F_OK) != 0)
125+
{
126+
fprintf(stderr, "Can't access to \"%s\"\n", dir);
127+
return -1;
128+
}
129+
130+
struct stat st;
131+
/* Get Information about the file pointed by 'dev' */
132+
stat(dir, &st);
133+
134+
/* Check if 'st' point to a character device (DEVDAX = char device) */
135+
if(S_ISCHR(st.st_mode))
136+
{
137+
/* Get the device ID */
138+
dev_t device_id = st.st_rdev;
139+
140+
/* Get major (class of the device) and minor */
141+
unsigned int major = major(device_id);
142+
unsigned int minor = minor(device_id);
143+
144+
char path[8192];
145+
snprintf(path, 8192, "/sys/dev/char/%u:%u/subsystem", major, minor);
146+
147+
fprintf(stdout, "Device Path: %s\n", path);
148+
149+
char rpath[8192];
150+
char* tpath = realpath(path, rpath);
151+
if(tpath == NULL)
152+
{
153+
fprintf(stderr, "Unable to resolve device path...\n");
154+
return -1;
155+
}
156+
fprintf(stdout, "Real Path: %s\n", rpath);
157+
158+
/*TODO: Is the real path containing "/dev/dax" string?
159+
* If yes: DEVDAX supported
160+
* If no: error
161+
* */
162+
163+
return TYPE_DEVDAX;
164+
}
165+
else
166+
{
167+
/* Check if dev refers to a directory */
168+
if((st.st_mode & S_IFMT) != S_IFDIR)
169+
{
170+
fprintf(stderr, "Fatal Error: %s is not a directory\n", dir);
171+
return TYPE_ERROR;
172+
}
173+
174+
return TYPE_REGULAR;
175+
}
176+
}
177+
178+
SCTK_INTERN int create_tmp_file(const char* dir)
179+
{
180+
/* Create a new temporary file (read/write permissions) into dir directory */
181+
//int fd = open(dir, O_TMPFILE | O_RDWR, S_IRUSR | S_IWUSR);
182+
int fd = open(dir, O_TMPFILE | O_RDWR | O_EXCL, S_IRUSR | S_IWUSR);
183+
if(fd < 0)
184+
{
185+
fprintf(stderr, "Fatal Error: unable to open directory %s (Error code: %d)\n", dir, errno);
186+
return -1;
187+
}
188+
return fd;
189+
}
190+
191+
/* Predeclaration of random access pattern for fd */
192+
SCTK_INTERN int declare_random_access(int fd, const size_t size)
193+
{
194+
return posix_fadvise(fd, 0, size, POSIX_FADV_RANDOM);
195+
}
196+
197+
/* Try to allocate space into file fd to ensure that memory space is allocated */
198+
SCTK_INTERN int allocate_file_space(int fd, const size_t size)
199+
{
200+
return posix_fallocate(fd, 0, size);
201+
}
202+
203+
204+
SCTK_PUBLIC void* sctk_pmem_malloc(sctk_pmem_desc_t* desc, size_t size)
205+
{
206+
void* ret = NULL;
207+
if(size + desc->cur_size <= desc->max_size)
208+
{
209+
desc->cur_size += size;
210+
ret = desc->ptr;
211+
desc->ptr += size;
212+
}
213+
return ret;
214+
}
215+
216+
SCTK_PUBLIC void sctk_pmem_free(sctk_pmem_desc_t* desc, size_t size, void* ptr)
217+
{
218+
memset(ptr, 0x0, size);
219+
}

src/sctk_alloc_pmem.h

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/* ############################# MPC License ############################## */
2+
/* # Wed Nov 19 15:19:19 CET 2008 # */
3+
/* # Copyright or (C) or Copr. Commissariat a l'Energie Atomique # */
4+
/* # # */
5+
/* # IDDN.FR.001.230040.000.S.P.2007.000.10000 # */
6+
/* # This file is part of the MPC Runtime. # */
7+
/* # # */
8+
/* # This software is governed by the CeCILL-C license under French law # */
9+
/* # and abiding by the rules of distribution of free software. You can # */
10+
/* # use, modify and/ or redistribute the software under the terms of # */
11+
/* # the CeCILL-C license as circulated by CEA, CNRS and INRIA at the # */
12+
/* # following URL http://www.cecill.info. # */
13+
/* # # */
14+
/* # The fact that you are presently reading this means that you have # */
15+
/* # had knowledge of the CeCILL-C license and that you accept its # */
16+
/* # terms. # */
17+
/* # # */
18+
/* # Authors: # */
19+
/* # - Roussel Adrien [email protected] # */
20+
/* # # */
21+
/* ######################################################################## */
22+
23+
#ifndef SCTK_ALLOC_PMEM_H
24+
#define SCTK_ALLOC_PMEM_H
25+
26+
#define _GNU_SOURCE
27+
28+
#include <unistd.h>
29+
#include <limits.h>
30+
#include <string.h>
31+
#include <inttypes.h>
32+
#include <sys/types.h>
33+
#include <sys/stat.h>
34+
#include <sys/sysmacros.h>
35+
#include <fcntl.h>
36+
#include <sys/mman.h>
37+
#include <errno.h>
38+
#include <math.h>
39+
#include <stdio.h>
40+
#include <stdlib.h>
41+
42+
#include "sctk_alloc_common.h"
43+
44+
#ifdef __cplusplus
45+
extern "C"
46+
{
47+
#endif
48+
49+
#define SCTK_ROUNDUP(x,y) (ceil((float)x/y)*y)
50+
51+
#ifndef S_ISCHR
52+
#define S_ISCHR(x) ((x & S_IFMT) == S_IFCHR)
53+
#endif
54+
55+
typedef enum
56+
{
57+
TYPE_ERROR = -1,
58+
TYPE_DEVDAX = 0,
59+
TYPE_REGULAR = 1
60+
} SCTK_PMEM_FILE_TYPE;
61+
62+
typedef struct
63+
{
64+
uint64_t cur_size;
65+
uint64_t max_size;
66+
void* ptr;
67+
void* start;
68+
} sctk_pmem_desc_t;
69+
70+
SCTK_PUBLIC sctk_pmem_desc_t* sctk_pmem_create(const char*, size_t*);
71+
SCTK_PUBLIC int sctk_pmem_destroy(sctk_pmem_desc_t*, size_t);
72+
SCTK_PUBLIC void* sctk_pmem_malloc(sctk_pmem_desc_t*, size_t);
73+
SCTK_PUBLIC void sctk_pmem_free(sctk_pmem_desc_t*, size_t, void*);
74+
SCTK_INTERN SCTK_PMEM_FILE_TYPE get_file_type(const char*);
75+
SCTK_INTERN int create_tmp_file(const char*);
76+
SCTK_INTERN int declare_random_access(int, const size_t);
77+
SCTK_INTERN int allocate_file_space(int fd, const size_t);
78+
79+
#ifdef __cplusplus
80+
}
81+
#endif
82+
83+
#endif //SCTK_ALLOC_PMEM_H

0 commit comments

Comments
 (0)