-
Notifications
You must be signed in to change notification settings - Fork 0
/
xmt-luc-blech.cc
183 lines (165 loc) · 3.57 KB
/
xmt-luc-blech.cc
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
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cerrno>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#if defined(__MTA__)
#include <sys/mta_task.h>
#include <machine/runtime.h>
#include <luc/luc_exported.h>
#include <snapshot/client.h>
#endif
extern "C" {
void xmt_luc_io_init (void);
void xmt_luc_snapin (const char*, void*, size_t);
void xmt_luc_snapout (const char*, void*, size_t);
void xmt_luc_stat (const char*, size_t*);
}
using namespace std;
#if defined(__MTA__)
static int use_NFS = 0;
void
xmt_luc_io_init (void)
{
int err;
if (mta_get_max_teams () < 2)
use_NFS = 1;
else if ((err = snap_init ()) != SNAP_ERR_OK) {
fprintf (stderr, "snap_init failed: %d\n", err);
abort ();
}
}
#else
void
xmt_luc_io_init (void)
{
}
#endif
static void
read_in (const char *fname, void* buf, size_t sz)
{
char * tmpbuf = (char *) buf;
int fd;
fd = open (fname, O_RDONLY);
if (fd < 0) {
fprintf (stderr, "Failed to open %s for reading: %s\n",
fname, strerror (errno));
abort ();
}
size_t bytes_read = 0;
while (bytes_read < sz) {
int last = read(fd, tmpbuf, sz);
bytes_read += last;
tmpbuf += last;
}
if (bytes_read != sz)
{
fprintf (stderr, "Failed to read %ld octets from %s: %s\n",
(long)sz, fname, strerror (errno));
abort ();
}
close (fd);
}
#if defined(__MTA__)
void
xmt_luc_snapin (const char *fname, void* buf, size_t sz)
{
luc_error_t err;
int snap_error_wtf;
if (use_NFS) {
read_in (fname, buf, sz);
return;
}
err = snap_restore (fname, buf, sz, &snap_error_wtf);
/* err = snap_pread (fname, SNAP_ANY_SW, buf, sz, 0, &snap_error_wtf); */
if (err != SNAP_ERR_OK) {
fprintf (stderr, "snap_restore of %s failed, errors %d and %d\n",
fname, err, snap_error_wtf);
abort ();
}
}
#else
void
xmt_luc_snapin (const char *fname, void* buf, size_t sz)
{
read_in (fname, buf, sz);
}
#endif
static void
write_out (const char *fname, void* buf, size_t sz)
{
int fd;
fd = open (fname, O_WRONLY|O_CREAT|O_TRUNC, 0666);
if (fd < 0) {
fprintf (stderr, "Failed to open %s for writing: %s\n",
fname, strerror (errno));
abort ();
}
if (write (fd, buf, sz) < sz) {
fprintf (stderr, "Failed to write %ld octets to %s: %s\n",
(long)sz, fname, strerror (errno));
abort ();
}
close (fd);
}
#if defined(__MTA__)
void
xmt_luc_snapout (const char *fname, void* buf, size_t sz)
{
luc_error_t err;
int snap_error_wtf;
if (use_NFS) {
write_out (fname, buf, sz);
return;
}
err = snap_snapshot (fname, buf, sz, &snap_error_wtf);
/* err = snap_pwrite (fname, SNAP_ANY_SW, buf, sz, 0, &snap_error_wtf); */
if (err != SNAP_ERR_OK) {
fprintf (stderr, "snap_snapshot of %s failed, errors %d and %d\n",
fname, err, snap_error_wtf);
abort ();
}
}
#else
void
xmt_luc_snapout (const char *fname, void* buf, size_t sz)
{
write_out (fname, buf, sz);
}
#endif
static void
stat_fname (const char *fname, size_t *sz)
{
struct stat st;
if (stat (fname, &st)) {
fprintf (stderr, "Failed to stat %s: %s\n",
fname, strerror (errno));
abort ();
}
*sz = st.st_size;
}
#if defined(__MTA__)
void
xmt_luc_stat (const char *fname, size_t *sz)
{
snap_stat_buf statBuf;
int64_t sn_err = 0;
luc_endpoint_id_t swEP = SNAP_ANY_SW;
if (use_NFS) {
stat_fname (fname, sz);
return;
}
if (!fname || snap_stat(fname, swEP, &statBuf, &sn_err) != LUC_ERR_OK)
perror("Can't stat sources file.\n");
*sz = statBuf.st_size;
}
#else
void
xmt_luc_stat (const char *fname, size_t *sz)
{
stat_fname (fname, sz);
}
#endif