Skip to content

Commit 5e6c3ae

Browse files
committed
added new FILE streams implementation
1 parent a113b29 commit 5e6c3ae

File tree

5 files changed

+217
-1
lines changed

5 files changed

+217
-1
lines changed

Makefile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ libminimal_thefunc.so: minimal_thefunc.o
2121
libthefunc.so : thefunc.o
2222
${CC} -shared -g3 -fPIC -o $@ $^
2323

24+
collfs_fopen.o: collfs_fopen.c
25+
${CC} ${CFLAGS} -c collfs_fopen.c -o collfs_fopen.o
26+
2427
fmemopen.o: fmemopen.c
2528
${CC} ${CFLAGS} -c fmemopen.c -o fmemopen.o
2629

@@ -37,6 +40,9 @@ minimal_main : minimal_main.o libminimal_thefunc.so
3740
${MPICC} -g3 -o $@ minimal_main.o ${LDFLAGS}
3841

3942
# Explicitly uses libcollfs to push communicators. Loads libminimal_thefunc.so (so only a run-time dependency)
43+
#main-mpi : main-mpi.o collfs_fopen.o libcollfs.so libminimal_thefunc.so
44+
# ${MPICC} -g3 -o $@ $< collfs_fopen.o libcollfs.so ${LDFLAGS} ${LDCOLLFSFLAGS}
45+
4046
main-mpi : main-mpi.o libcollfs.so libminimal_thefunc.so
4147
${MPICC} -g3 -o $@ $< libcollfs.so ${LDFLAGS} ${LDCOLLFSFLAGS}
4248

collfs-private.h

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
#ifndef _collfs_private_h
2+
#define _collfs_private_h
3+
4+
#include <sys/param.h>
5+
#include <sys/stat.h>
6+
#include <sys/types.h>
7+
#include <unistd.h>
8+
9+
struct libc_collfs_api {
10+
void (*fxstat64)(void);
11+
void (*xstat64)(void);
12+
void (*open)(void);
13+
void (*close)(void);
14+
void (*read)(void);
15+
void (*lseek)(void);
16+
void (*mmap)(void);
17+
void (*munmap)(void);
18+
};
19+
20+
extern struct libc_collfs_api _dl_collfs_api;
21+
22+
typedef int (*collfs_fxstat64_fp)(int vers, int fd, struct stat64 *buf);
23+
typedef int (*collfs_xstat64_fp)(int vers, const char *file, struct stat64 *buf);
24+
typedef int (*collfs_open_fp)(const char *pathname, int flags, mode_t mode);
25+
typedef int (*collfs_close_fp)(int fd);
26+
typedef ssize_t (*collfs_read_fp)(int fd, void *buf, size_t count);
27+
typedef off_t (*collfs_lseek_fp)(int fildes, off_t offset, int whence);
28+
typedef void *(*collfs_mmap_fp)(void *addr, size_t len, int prot, int flags, int fildes, off_t off);
29+
typedef int (*collfs_munmap_fp)(__ptr_t addr, size_t len);
30+
31+
#define __collfs_open(pathname,flags) \
32+
((_dl_collfs_api.open != NULL) ? \
33+
((collfs_open_fp) _dl_collfs_api.open)(pathname,flags,O_RDONLY) : open(pathname, flags))
34+
35+
#define __collfs_close(fd) \
36+
((_dl_collfs_api.close != NULL) ? \
37+
((collfs_close_fp) _dl_collfs_api.close)(fd) : close(fd))
38+
39+
#define __collfs_lseek(fildes, offset, whence) \
40+
((_dl_collfs_api.lseek != NULL) ? \
41+
((collfs_lseek_fp) _dl_collfs_api.lseek)(fildes, offset, whence) : lseek(fildes, offset, whence))
42+
43+
#define __collfs_fxstat64(vers, fd, buf) \
44+
((_dl_collfs_api.fxstat64 != NULL) ? \
45+
((collfs_fxstat64_fp) _dl_collfs_api.fxstat64)(vers, fd, buf) : fxstat64(vers, fd, buf))
46+
47+
#define __collfs_xstat64(vers, file, buf) \
48+
((_dl_collfs_api.xstat64 != NULL) ? \
49+
((collfs_xstat64_fp) _dl_collfs_api.xstat64)(vers, file, buf) : xstat64(vers, file, buf))
50+
51+
#define __collfs_libc_read(fd, buf, count) \
52+
((_dl_collfs_api.read != NULL) ? \
53+
((collfs_read_fp) _dl_collfs_api.read)(fd, buf, count) : read(fd, buf, count))
54+
55+
#define __collfs_mmap(addr, len, prot, flags, fildes, off) \
56+
((_dl_collfs_api.mmap != NULL) ? \
57+
((collfs_mmap_fp) _dl_collfs_api.mmap)(addr, len, prot, flags, fildes, off) : mmap(addr, len, prot, flags, fildes, off))
58+
59+
#define __collfs_munmap(addr, len) \
60+
((_dl_collfs_api.munmap != NULL) ? \
61+
((collfs_munmap_fp) _dl_collfs_api.munmap)(addr, len) : munmap(addr, len))
62+
63+
#endif

collfs_fopen.c

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
/*
2+
This file implements custom C string streams for read-only collective file system access. For more information about custom C strings, see:
3+
4+
http://www.gnu.org/software/libc/manual/html_node/Custom-Streams.html#Custom-Streams
5+
*/
6+
7+
#define _GNU_SOURCE // needed to expose cookie_io_functions_t
8+
#include <errno.h>
9+
#include <libio.h>
10+
#include <stdio.h>
11+
#include <stdlib.h>
12+
#include <stdint.h>
13+
#include <string.h>
14+
#include <sys/types.h>
15+
#include <fcntl.h>
16+
#include "collfs-private.h"
17+
18+
typedef struct fcollfs_cookie_struct fcollfs_cookie_t;
19+
struct fcollfs_cookie_struct
20+
{
21+
int fd;
22+
};
23+
24+
25+
static ssize_t
26+
fcollfs_read (void *cookie, char *b, size_t s)
27+
{
28+
int fd;
29+
fcollfs_cookie_t *c;
30+
31+
c = (fcollfs_cookie_t *) cookie;
32+
fd = c->fd;
33+
34+
return __collfs_libc_read(fd, b, s);
35+
}
36+
37+
38+
static ssize_t
39+
fcollfs_write (void *cookie __attribute__ ((unused)), const char *b __attribute__ ((unused)), size_t s __attribute__ ((unused)))
40+
{
41+
// this is an error!
42+
return 0;
43+
}
44+
45+
46+
static int
47+
fcollfs_seek (void *cookie, _IO_off64_t *p, int w)
48+
{
49+
int fd;
50+
off_t new_p;
51+
fcollfs_cookie_t *c;
52+
53+
c = (fcollfs_cookie_t *) cookie;
54+
fd = c->fd;
55+
56+
new_p = __collfs_lseek(fd, *p, w);
57+
58+
if (new_p == -1) {
59+
return -1;
60+
}
61+
62+
*p = new_p;
63+
return 0;
64+
}
65+
66+
67+
static int
68+
fcollfs_close (void *cookie)
69+
{
70+
int fd;
71+
fcollfs_cookie_t *c;
72+
73+
c = (fcollfs_cookie_t *) cookie;
74+
fd = c->fd;
75+
76+
free (c);
77+
return __collfs_close(fd);
78+
}
79+
80+
FILE *
81+
fcollfs_open (const char *pathname, const char *mode)
82+
{
83+
cookie_io_functions_t iof;
84+
fcollfs_cookie_t *c;
85+
86+
if (mode[0] != 'r')
87+
return NULL;
88+
89+
c = (fcollfs_cookie_t *) malloc (sizeof (fcollfs_cookie_t));
90+
if (c == NULL)
91+
return NULL;
92+
93+
c->fd = __collfs_open(pathname, O_RDONLY);
94+
95+
iof.read = fcollfs_read;
96+
iof.write = fcollfs_write;
97+
iof.seek = fcollfs_seek;
98+
iof.close = fcollfs_close;
99+
100+
return fopencookie (c, mode, iof);
101+
}
102+

collfs_fopen.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#include <errno.h>
2+
#include <libio.h>
3+
#include <stdio.h>
4+
#include <stdlib.h>
5+
#include <stdint.h>
6+
#include <string.h>
7+
#include <sys/types.h>
8+
9+
FILE * fcollfs_open (const char *pathname, const char *mode);

main-mpi.c

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,16 @@
77
#include <dlfcn.h>
88

99
#include "collfs.h"
10+
#include "collfs_fopen.h"
1011
#include "errmacros.h"
1112

1213
int run_tests(int verbosity,const char *soname)
1314
{
1415
void *handle;
15-
int err, rank;
16+
int err, rank, count;
1617
int (*func)(int);
18+
FILE* alphabet;
19+
char abcde[6];
1720

1821
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
1922

@@ -30,6 +33,39 @@ int run_tests(int verbosity,const char *soname)
3033

3134
err = dlclose(handle);
3235
if (err) ERR("dlclose failed due to: %s", dlerror());
36+
37+
// // fcollfs_open
38+
// if (verbosity > 0) printf("[%d] attempting to collfs_fopen alphabet.txt\n", rank);
39+
// alphabet = fcollfs_open("./alphabet.txt","r");
40+
41+
// // fread
42+
// if (verbosity > 0) printf("[%d] checking fread\n", rank);
43+
// count = fread(abcde, sizeof(char), 5, alphabet);
44+
// if (count < 5) {
45+
// ERR("expected to read 5 bytes, read: %d", count);
46+
// }
47+
// abcde[5] = '\0';
48+
// if (strcmp(abcde, "abcde") != 0) {
49+
// ERR("expected to read 'abcde', read: %s", abcde);
50+
// }
51+
52+
// // fseek
53+
// if (verbosity > 0) printf("[%d] checking fseek\n", rank);
54+
// err = fseek(alphabet, -4, SEEK_CUR);
55+
// if (err==-1) perror("error in calling fseek:");
56+
// count = fread(abcde, sizeof(char), 5, alphabet);
57+
// if (count < 5) {
58+
// ERR("expected to read 5 bytes, read: %d", count);
59+
// }
60+
// abcde[5] = '\0';
61+
// if (strcmp(abcde, "bcdef") != 0) {
62+
// ERR("expected to read 'bcdef', read: %s", abcde);
63+
// }
64+
65+
// // fclose
66+
// if (verbosity > 0) printf("[%d] checking fclose\n", rank);
67+
// err = fclose(alphabet);
68+
// if (err==-1) perror("error in calling fclose:");
3369
return 0;
3470
}
3571

0 commit comments

Comments
 (0)