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 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358
|
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "libMems/dmSML/asyncio.h"
#include "libMems/dmSML/alibc.h"
#include "libMems/dmSML/awin32aio.h"
#include "libMems/dmSML/aPOSIXaio.h"
#include "libMems/dmSML/util.h"
#include "libMems/dmSML/buffer.h"
#include <string.h>
#if defined(USE_POSIX_AIO)||defined(USE_LINUX_AIO)
#include <unistd.h>
#include <sys/stat.h>
#endif
static int OperationNumber = 0;
int QueueEmpty( aFILE * file );
void RemoveOperation( aFILE * file );
void FreeQueue( aFILE * file );
int ExecuteWrite( aFILE * file, aIORec * rec );
int ExecuteRead( aFILE * file, aIORec * rec );
int EnqueueOperation( char * buffer, offset_t size, offset_t count, aFILE * file, offset_t pos );
void ExecuteOperation( aFILE * file );
int QueryOpComplete( aFILE * file );
int aAct( void * buffer, offset_t size, offset_t count, aFILE * file, offset_t pos );
int QueueEmpty( aFILE * file ) {
return( (file->queuehead == file->queuetail) && (file->queuehead == NULL) );
}
void RemoveOperation( aFILE * file ) {
aIORec * tofree;
if( !QueueEmpty( file ) ) {
tofree = file->queuetail;
if( file->queuetail == file->queuehead ) {
file->queuehead = file->queuetail = NULL;
} else {
file->queuetail->next->last = NULL;
file->queuetail = file->queuetail->next;
}
// FIXME: ack hack from my poor design for win32
#if defined USE_WIN32
free( tofree->w32overlapped );
#elif defined(USE_POSIX_AIO)||defined(USE_LINUX_AIO)
free( tofree->aio_cb );
#endif
free( tofree );
}
}
void FreeQueue( aFILE * file ) {
while( !QueueEmpty( file ) ) {
RemoveOperation( file );
}
}
// opens a file
aFILE * aOpen( const char * path, int mode ) {
int err = 0;
aFILE *ret = malloc( sizeof( *ret ) );
memset( ret, 0, sizeof( *ret ) );
ret->mode = mode;
ret->busy = 0;
#if defined USE_LINUX_AIO
err = !OpenLinux( ret, path, mode );
#elif defined USE_POSIX_AIO
err = !OpenPAIO( ret, path, mode );
#elif defined USE_LIBC
err = !OpenLibC( ret, path, mode );
#elif defined USE_WIN32
err = !OpenWIN32( ret, path, mode );
#endif
if( err ) {
free( ret );
ret = NULL;
}
return( ret );
}
// helper to close a file
int aClose( aFILE * file ) {
int err = 0;
// block until the file is no longer busy.
aWaitNotBusy( file );
#if defined USE_LINUX_AIO
err = CloseLinux( file );
#elif defined USE_POSIX_AIO
err = ClosePAIO( file );
#elif defined USE_LIBC
err = CloseLibC( file );
#elif defined USE_WIN32
err = CloseWIN32( file );
#endif
FreeQueue( file );
free( file );
return( err );
}
int ExecuteWrite( aFILE * file, aIORec * rec ) {
int err = 0;
#if defined USE_LINUX_AIO
err = !WriteLinux( file, rec );
#elif defined USE_POSIX_AIO
err = !WritePAIO( file, rec );
#elif defined USE_LIBC
err = !WriteLibC( file, rec );
#elif defined USE_WIN32
err = !WriteWIN32( file, rec );
#endif
if( err ) {
//printf( "error in ExecuteWrite\n" );
} else {
file->busy = 1;
}
return( err );
}
int ExecuteRead( aFILE * file, aIORec * rec ) {
int err = 0;
#if defined USE_LINUX_AIO
err = !ReadLinux( file, rec );
#elif defined USE_POSIX_AIO
err = !ReadPAIO( file, rec );
#elif defined USE_LIBC
err = !ReadLibC( file, rec );
#elif defined USE_WIN32
err = !ReadWIN32( file, rec );
#endif
if( err ) {
//printf( "error in ExecuteRead\n" );
} else {
file->busy = 1;
}
return( err );
}
int EnqueueOperation( char * buffer, offset_t size, offset_t count, aFILE * file, offset_t pos ) {
if( QueueEmpty( file ) ) {
file->queuehead = file->queuetail = malloc( sizeof( *file->queuehead ) );
memset( file->queuehead, 0, sizeof( *(file->queuehead) ) );
file->queuehead->last = NULL;
} else {
file->queuehead->next = malloc( sizeof( *file->queuehead->next ) );
memset( file->queuehead->next, 0, sizeof( *(file->queuehead->next) ) );
file->queuehead->next->last = file->queuehead;
file->queuehead = file->queuehead->next;
}
file->queuehead->buf = buffer;
file->queuehead->size = size;
file->queuehead->count = count;
file->queuehead->pos = pos;
file->queuehead->operation = ++OperationNumber;
file->queuehead->next = NULL;
return( file->queuehead->operation );
}
void ExecuteOperation( aFILE * file ) {
// if file is busy or there are no pending ops, we can't do much.
if( !QueueEmpty( file ) && !file->busy ) {
int err = 0;
if( file->mode == A_WRITE ) {
err = ExecuteWrite( file, file->queuetail );
} else {
err = ExecuteRead( file, file->queuetail );
}
if( !err ) {
// advance file pointer
AddTo64( file->queuetail->size * file->queuetail->count, &(file->filep_high), &(file->filep_low) );
}
}
}
int QueryOpComplete( aFILE * file ) {
// check to see if the last operation was completed.
if( file->queuetail != NULL ) {
#if defined USE_LINUX_AIO
return( QueryLastCompleteLinux( file ) );
#elif defined USE_POSIX_AIO
return( QueryLastCompletePAIO( file ) );
#elif defined USE_LIBC
return( 1 );
#elif defined USE_WIN32
return( QueryLastCompleteWIN32( file ) );
#endif
}
return( 1 );
}
// for files open for writing, ensures that all data is
// safely on disk (flushes buffer cache).
void aFlush( aFILE *file ) {
#if defined(USE_POSIX_AIO)||defined(USE_LINUX_AIO)
if( fsync( file->file_descriptor ) )
perror("fsync");
#elif defined USE_LIBC
if( fflush( file->libchandle ) ) {
printf( "error flushing stdio libc file\n" );
}
#elif defined USE_WIN32
if( !FlushFileBuffers( file->w32handle ) ) {
printf( "error flushing win32 file\n" );
}
#endif
}
// get the size in bytes of a particular file
unsigned long long aStatFileSize( const char * path ) {
#if defined(USE_POSIX_AIO)||defined(USE_LINUX_AIO)
struct stat stat_data;
if( stat( path , &stat_data) ){
perror(path);
return 0;
}
return stat_data.st_size;
#elif defined USE_LIBC
#error "libc aStatSize not implemented"
#elif defined USE_WIN32
WIN32_FILE_ATTRIBUTE_DATA file_data;
unsigned long long f_size;
GetFileAttributesEx( path, GetFileExInfoStandard, (void*)&file_data );
f_size = file_data.nFileSizeHigh;
f_size <<= 32;
f_size += file_data.nFileSizeLow;
return f_size;
//#error "Implement me! WIN32 aStatSize"
#endif
}
// get the size in records of a particular file
// used when skipping the binning phase
unsigned long aStatSize( const char * path ) {
#if defined(USE_POSIX_AIO)||defined(USE_LINUX_AIO)
struct stat stat_data;
if( stat( path , &stat_data) ){
perror(path);
return 0;
}
return stat_data.st_size / sizeof(record_t);
#elif defined USE_LIBC
#error "libc aStatSize not implemented"
#elif defined USE_WIN32
return aStatFileSize( path ) / sizeof(record_t);
printf("Implement me! WIN32 aStatSize");
//#error "Implement me! WIN32 aStatSize"
#endif
}
void aUpdateOperations( aFILE * file ) {
int op_complete;
// if we are busy, see if the last thing has completed.
op_complete = QueryOpComplete( file );
if( !op_complete ) {
//printf( "op not yet complete on file 0x%X\n", file );
}
if( !QueueEmpty( file ) && file->busy && op_complete ) {
RemoveOperation( file );
file->busy = 0;
}
// if the queue is still not empty, start the next one up.
if( !QueueEmpty( file ) ) {
ExecuteOperation( file );
}
}
int aAct( void * buffer, offset_t size, offset_t count, aFILE * file, offset_t pos ) {
int operation = 0;
// enter the operation in the queue, and then
// try to execute what we can.
// enqueue the op.
operation = EnqueueOperation( buffer, size, count, file, pos );
// execute operations
ExecuteOperation( file );
return( operation );
}
// these allow you to queue reads and writes.
// these return 0 for a failure, or an operation
// code that can be checked for completion with
// a_OperationComplete
int aWrite( void * buffer, offset_t size, offset_t count, aFILE * file, offset_t pos ) {
return( aAct( buffer, size, count, file, pos ) );
}
int aRead( void * buffer, offset_t size, offset_t count, aFILE * file, offset_t pos ) {
return( aAct( buffer, size, count, file, pos ) );
}
// returns 1 if the operation was completed, 0 otherwise.
int aOperationComplete( aFILE * file, int operation ) {
aIORec *qp;
// scan through the queue until we find the op
// or we get to the end. if we get to the end
// and don't find it, it must have completed,
// otherwise it hasn't.
for( qp = file->queuetail; qp != NULL; qp = qp->next ) {
if( qp->operation == operation ) {
return( 0 );
}
}
return( 1 );
}
// returns 1 if the file is doing IO, 0 otherwise.
int aFileBusy( aFILE * file ) {
return( file->busy );
}
// blocks and waits for the specified operation to
// complete.
void aWaitComplete( aFILE * file, int operation ) {
while( !aOperationComplete( file, operation ) ) {
aUpdateOperations( file );
}
}
// blocks and waits for the file to not be busy
// and for all IO operations to complete.
void aWaitNotBusy( aFILE * file ) {
while( file->busy ) {
aUpdateOperations( file );
}
}
|