Skip to content

Commit 0ce7792

Browse files
committed
fbtl/posix: cleanup of preadv and pwritev routines
and some additional error handling Signed-off-by: Edgar Gabriel <egabriel@central.uh.edu>
1 parent 615f4ef commit 0ce7792

File tree

4 files changed

+143
-67
lines changed

4 files changed

+143
-67
lines changed

ompi/mca/fbtl/posix/fbtl_posix_ipreadv.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,11 @@ ssize_t mca_fbtl_posix_ipreadv (ompio_file_t *fh,
8585
off_t len = end_offset - (off_t)fh->f_io_array[0].offset;
8686
ret = mca_fbtl_posix_lock ( &data->aio_lock, fh, F_RDLCK, (off_t)fh->f_io_array[0].offset,
8787
len, OMPIO_LOCK_ENTIRE_REGION, &data->aio_lock_counter);
88+
if ( ret == -1 ) {
89+
opal_output(1, "mca_fbtl_posix_ipreadv: error in mca_fbtl_posix_lock():%s", strerror(errno));
90+
return OMPI_ERROR;
91+
}
92+
8893
fh->f_flags = orig_flags;
8994
}
9095

ompi/mca/fbtl/posix/fbtl_posix_ipwritev.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,10 @@ ssize_t mca_fbtl_posix_ipwritev (ompio_file_t *fh,
8383
off_t len = end_offset - (off_t)fh->f_io_array[0].offset;
8484
ret = mca_fbtl_posix_lock ( &data->aio_lock, fh, F_WRLCK, (off_t)fh->f_io_array[0].offset,
8585
len, OMPIO_LOCK_ENTIRE_REGION, &data->aio_lock_counter);
86+
if ( ret == -1 ) {
87+
opal_output(1, "mca_fbtl_posix_ipwritev: error in mca_fbtl_posix_lock():%s", strerror(errno));
88+
return OMPI_ERROR;
89+
}
8690
fh->f_flags = orig_flags;
8791
}
8892

ompi/mca/fbtl/posix/fbtl_posix_preadv.c

Lines changed: 56 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,15 @@
3131

3232
static ssize_t mca_fbtl_posix_preadv_datasieving (ompio_file_t *fh, struct flock *lock, int *lock_counter);
3333
static ssize_t mca_fbtl_posix_preadv_generic (ompio_file_t *fh, struct flock *lock, int *lock_counter);
34+
static ssize_t mca_fbtl_posix_preadv_single (ompio_file_t *fh, struct flock *lock, int *lock_counter);
3435

3536
#define MAX_RETRIES 10
3637

3738
ssize_t mca_fbtl_posix_preadv (ompio_file_t *fh )
3839
{
39-
ssize_t bytes_read=0, ret_code=0;
40+
ssize_t bytes_read=0;
4041
struct flock lock;
41-
int lock_counter=0;
42-
int ret;
42+
int ret, lock_counter=0;
4343
int32_t orig_flags;
4444

4545
if (NULL == fh->f_io_array) {
@@ -58,6 +58,10 @@ ssize_t mca_fbtl_posix_preadv (ompio_file_t *fh )
5858
off_t len = end_offset - (off_t)fh->f_io_array[0].offset;
5959
ret = mca_fbtl_posix_lock ( &lock, fh, F_RDLCK, (off_t)fh->f_io_array[0].offset,
6060
len, OMPIO_LOCK_ENTIRE_REGION, &lock_counter);
61+
if ( ret == -1 ) {
62+
opal_output(1, "mca_fbtl_posix_preadv: error in mca_fbtl_posix_lock():%s", strerror(errno));
63+
return OMPI_ERROR;
64+
}
6165
fh->f_flags = orig_flags;
6266
}
6367

@@ -92,31 +96,9 @@ ssize_t mca_fbtl_posix_preadv (ompio_file_t *fh )
9296
}
9397
else {
9498
// i.e. fh->f_num_of_io_entries == 1
95-
ret = mca_fbtl_posix_lock ( &lock, fh, F_RDLCK, (off_t)fh->f_io_array[0].offset,
96-
(off_t)fh->f_io_array[0].length, OMPIO_LOCK_ENTIRE_REGION,
97-
&lock_counter );
98-
if ( 0 < ret ) {
99-
opal_output(1, "mca_fbtl_posix_preadv: error in mca_fbtl_posix_lock() ret=%d: %s",
100-
ret, strerror(errno));
101-
/* Just in case some part of the lock worked */
102-
mca_fbtl_posix_unlock ( &lock, fh, &lock_counter);
103-
bytes_read = OMPI_ERROR;
104-
goto exit;
105-
}
106-
107-
ret_code = pread(fh->fd, fh->f_io_array[0].memory_address, fh->f_io_array[0].length,
108-
(off_t)fh->f_io_array[0].offset );
109-
mca_fbtl_posix_unlock ( &lock, fh, &lock_counter );
110-
if ( ret_code == -1 ) {
111-
opal_output(1, "mca_fbtl_posix_preadv: error in (p)read(v):%s", strerror(errno));
112-
bytes_read = OMPI_ERROR;
113-
goto exit;
114-
}
115-
116-
bytes_read += ret_code;
99+
bytes_read = mca_fbtl_posix_preadv_single (fh, &lock, &lock_counter );
117100
}
118101

119-
exit:
120102
if ( fh->f_atomicity ) {
121103
mca_fbtl_posix_unlock ( &lock, fh, &lock_counter );
122104
}
@@ -125,6 +107,52 @@ ssize_t mca_fbtl_posix_preadv (ompio_file_t *fh )
125107
return bytes_read;
126108
}
127109

110+
ssize_t mca_fbtl_posix_preadv_single (ompio_file_t *fh, struct flock *lock, int *lock_counter)
111+
{
112+
ssize_t bytes_read=0, ret_code;
113+
size_t total_bytes = 0;
114+
int ret;
115+
116+
ret = mca_fbtl_posix_lock ( lock, fh, F_RDLCK, (off_t)fh->f_io_array[0].offset,
117+
(off_t)fh->f_io_array[0].length, OMPIO_LOCK_ENTIRE_REGION,
118+
lock_counter );
119+
if ( 0 < ret ) {
120+
opal_output(1, "mca_fbtl_posix_preadv_single: error in mca_fbtl_posix_lock() ret=%d: %s",
121+
ret, strerror(errno));
122+
/* Just in case some part of the lock worked */
123+
mca_fbtl_posix_unlock ( lock, fh, lock_counter);
124+
return OMPI_ERROR;
125+
}
126+
127+
int retries = 0;
128+
size_t len = fh->f_io_array[0].length;
129+
while ( total_bytes < len ) {
130+
ret_code = pread(fh->fd, (char*)fh->f_io_array[0].memory_address+total_bytes,
131+
fh->f_io_array[0].length-total_bytes,
132+
(off_t)fh->f_io_array[0].offset+total_bytes );
133+
if ( ret_code == -1 ) {
134+
opal_output(1, "mca_fbtl_posix_preadv_single: error in (p)read(v):%s", strerror(errno));
135+
mca_fbtl_posix_unlock ( lock, fh, lock_counter );
136+
return OMPI_ERROR;
137+
}
138+
if ( ret_code == 0 ) {
139+
// end of file
140+
retries++;
141+
if ( retries == MAX_RETRIES ) {
142+
break;
143+
}
144+
else {
145+
continue;
146+
}
147+
}
148+
total_bytes += ret_code;
149+
}
150+
bytes_read = total_bytes;
151+
mca_fbtl_posix_unlock ( lock, fh, lock_counter );
152+
153+
return bytes_read;
154+
}
155+
128156
ssize_t mca_fbtl_posix_preadv_datasieving (ompio_file_t *fh, struct flock *lock, int *lock_counter)
129157
{
130158
size_t start, end, len;
@@ -188,13 +216,14 @@ ssize_t mca_fbtl_posix_preadv_datasieving (ompio_file_t *fh, struct flock *lock,
188216
free ( temp_buf);
189217
return OMPI_ERROR;
190218
}
191-
long unsigned int total_bytes = 0;
219+
size_t total_bytes = 0;
192220
int retries = 0;
193221

194222
while ( total_bytes < len ) {
195223
ret_code = pread (fh->fd, temp_buf+total_bytes, len-total_bytes, start+total_bytes);
196224
if ( ret_code == -1 ) {
197225
opal_output(1, "mca_fbtl_posix_preadv_datasieving: error in (p)read(v):%s", strerror(errno));
226+
mca_fbtl_posix_unlock ( lock, fh, lock_counter );
198227
free ( temp_buf);
199228
return OMPI_ERROR;
200229
}

ompi/mca/fbtl/posix/fbtl_posix_pwritev.c

Lines changed: 78 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,13 @@
3232

3333
static ssize_t mca_fbtl_posix_pwritev_datasieving (ompio_file_t *fh, struct flock *lock, int *lock_counter );
3434
static ssize_t mca_fbtl_posix_pwritev_generic (ompio_file_t *fh, struct flock *lock, int *lock_counter );
35+
static ssize_t mca_fbtl_posix_pwritev_single (ompio_file_t *fh, struct flock *lock, int *lock_counter );
36+
37+
#define MAX_RETRIES 10
3538

3639
ssize_t mca_fbtl_posix_pwritev(ompio_file_t *fh )
3740
{
38-
ssize_t bytes_written=0, ret_code=0;
41+
ssize_t bytes_written=0;
3942
struct flock lock;
4043
int lock_counter=0, ret;
4144
int32_t orig_flags;
@@ -56,6 +59,12 @@ ssize_t mca_fbtl_posix_pwritev(ompio_file_t *fh )
5659
off_t len = end_offset - (off_t)fh->f_io_array[0].offset;
5760
ret = mca_fbtl_posix_lock ( &lock, fh, F_WRLCK, (off_t)fh->f_io_array[0].offset,
5861
len, OMPIO_LOCK_ENTIRE_REGION, &lock_counter);
62+
if ( 0 < ret ) {
63+
opal_output(1, "mca_fbtl_posix_pwritev: error in mca_fbtl_posix_lock() ret=%d: %s",
64+
ret, strerror(errno));
65+
mca_fbtl_posix_unlock ( &lock, fh, &lock_counter);
66+
return OMPI_ERROR;
67+
}
5968
fh->f_flags = orig_flags;
6069
}
6170

@@ -92,38 +101,50 @@ ssize_t mca_fbtl_posix_pwritev(ompio_file_t *fh )
92101
}
93102
else {
94103
// i.e. fh->f_num_of_io_entries == 1
95-
ret = mca_fbtl_posix_lock ( &lock, fh, F_WRLCK, (off_t)fh->f_io_array[0].offset,
96-
(off_t)fh->f_io_array[0].length, OMPIO_LOCK_ENTIRE_REGION,
97-
&lock_counter);
98-
if ( 0 < ret ) {
99-
opal_output(1, "mca_fbtl_posix_pwritev: error in mca_fbtl_posix_lock() ret=%d: %s",
100-
ret, strerror(errno));
101-
/* Just in case some part of the lock worked */
102-
mca_fbtl_posix_unlock ( &lock, fh, &lock_counter);
103-
bytes_written = OMPI_ERROR;
104-
goto exit;
105-
}
106-
107-
ret_code = pwrite(fh->fd, fh->f_io_array[0].memory_address, fh->f_io_array[0].length,
108-
(off_t)fh->f_io_array[0].offset );
109-
mca_fbtl_posix_unlock ( &lock, fh, &lock_counter );
110-
if ( ret_code == -1 ) {
111-
opal_output(1, "mca_fbtl_posix_pwritev: error in (p)write(v):%s", strerror(errno));
112-
bytes_written = OMPI_ERROR;
113-
goto exit;
114-
}
115-
116-
bytes_written += ret_code;
104+
bytes_written = mca_fbtl_posix_pwritev_single (fh, &lock, &lock_counter);
117105
}
118106

119-
exit:
120107
if ( fh->f_atomicity ) {
121108
mca_fbtl_posix_unlock ( &lock, fh, &lock_counter );
122109
}
123110

124111
return bytes_written;
125112
}
126113

114+
ssize_t mca_fbtl_posix_pwritev_single (ompio_file_t *fh, struct flock *lock, int *lock_counter)
115+
{
116+
int ret;
117+
ssize_t bytes_written=0, ret_code;
118+
size_t total_bytes = 0;
119+
size_t len = fh->f_io_array[0].length;
120+
121+
ret = mca_fbtl_posix_lock ( lock, fh, F_WRLCK, (off_t)fh->f_io_array[0].offset,
122+
(off_t)fh->f_io_array[0].length, OMPIO_LOCK_ENTIRE_REGION,
123+
lock_counter);
124+
if ( 0 < ret ) {
125+
opal_output(1, "mca_fbtl_posix_pwritev_single: error in mca_fbtl_posix_lock() ret=%d: %s",
126+
ret, strerror(errno));
127+
/* Just in case some part of the lock worked */
128+
mca_fbtl_posix_unlock ( lock, fh, lock_counter);
129+
return OMPI_ERROR;
130+
}
131+
while ( total_bytes < len ) {
132+
ret_code = pwrite(fh->fd, (char*)fh->f_io_array[0].memory_address+total_bytes,
133+
fh->f_io_array[0].length-total_bytes,
134+
(off_t)fh->f_io_array[0].offset+total_bytes );
135+
if ( ret_code == -1 ) {
136+
opal_output(1, "mca_fbtl_posix_pwritev: error in (p)write(v):%s", strerror(errno));
137+
mca_fbtl_posix_unlock ( lock, fh, lock_counter );
138+
return OMPI_ERROR;
139+
}
140+
total_bytes += ret_code;
141+
}
142+
mca_fbtl_posix_unlock ( lock, fh, lock_counter );
143+
144+
bytes_written = total_bytes;
145+
return bytes_written;
146+
}
147+
127148
ssize_t mca_fbtl_posix_pwritev_datasieving (ompio_file_t *fh, struct flock *lock, int *lock_counter)
128149
{
129150
size_t start, end, len;
@@ -135,6 +156,7 @@ ssize_t mca_fbtl_posix_pwritev_datasieving (ompio_file_t *fh, struct flock *lock
135156
int startindex = 0;
136157
int endindex = 0;
137158
bool done = false;
159+
size_t total_bytes = 0;
138160

139161
while (!done) {
140162
// Break the io_array into chunks such that the size of the temporary
@@ -188,17 +210,29 @@ ssize_t mca_fbtl_posix_pwritev_datasieving (ompio_file_t *fh, struct flock *lock
188210
return OMPI_ERROR;
189211
}
190212

191-
ret_code = pread (fh->fd, temp_buf, len, start);
192-
if ( ret_code == -1 ) {
193-
//opal_output(1, "mca_fbtl_posix_pwritev_datasieving: error in pwrite:%s", strerror(errno));
194-
opal_output(1, "mca_fbtl_posix_pwritev_datasieving: error in pwrite:%s", strerror(errno));
195-
/* Just in case some part of the lock worked */
196-
mca_fbtl_posix_unlock ( lock, fh, lock_counter);
197-
free ( temp_buf);
198-
return OMPI_ERROR;
213+
int retries=0;
214+
while ( total_bytes < len ) {
215+
ret_code = pread (fh->fd, temp_buf, len, start);
216+
if ( ret_code == -1 ) {
217+
opal_output(1, "mca_fbtl_posix_pwritev_datasieving: error in pread:%s", strerror(errno));
218+
mca_fbtl_posix_unlock ( lock, fh, lock_counter);
219+
free ( temp_buf);
220+
return OMPI_ERROR;
221+
}
222+
if ( ret_code == 0 ) {
223+
// end of file
224+
retries++;
225+
if ( retries == MAX_RETRIES ) {
226+
break;
227+
}
228+
else {
229+
continue;
230+
}
231+
}
232+
total_bytes += ret_code;
199233
}
200234

201-
// Copy out the elements to write into temporary buffer.
235+
// Copy the elements to write into temporary buffer.
202236
size_t pos = 0;
203237
size_t num_bytes;
204238
size_t start_offset = (size_t) fh->f_io_array[startindex].offset;
@@ -208,13 +242,17 @@ ssize_t mca_fbtl_posix_pwritev_datasieving (ompio_file_t *fh, struct flock *lock
208242
memcpy (temp_buf + pos, fh->f_io_array[i].memory_address, num_bytes);
209243
bytes_written += num_bytes;
210244
}
211-
ret_code = pwrite (fh->fd, temp_buf, len, start);
212-
if ( ret_code == -1 ) {
213-
opal_output(1, "mca_fbtl_posix_pwritev_datasieving: error in pwrite:%s", strerror(errno));
214-
/* Just in case some part of the lock worked */
215-
mca_fbtl_posix_unlock ( lock, fh, lock_counter);
216-
free ( temp_buf);
217-
return OMPI_ERROR;
245+
246+
total_bytes = 0;
247+
while ( total_bytes < len ) {
248+
ret_code = pwrite (fh->fd, temp_buf+total_bytes, len-total_bytes, start+total_bytes);
249+
if ( ret_code == -1 ) {
250+
opal_output(1, "mca_fbtl_posix_pwritev_datasieving: error in pwrite:%s", strerror(errno));
251+
mca_fbtl_posix_unlock ( lock, fh, lock_counter);
252+
free ( temp_buf);
253+
return OMPI_ERROR;
254+
}
255+
total_bytes += ret_code;
218256
}
219257

220258
mca_fbtl_posix_unlock ( lock, fh, lock_counter);

0 commit comments

Comments
 (0)