From 95bb5a0a8899abd2abe62f4f0fd359e222c20700 Mon Sep 17 00:00:00 2001 From: ahodgkinson Date: Fri, 14 Jul 2006 22:30:23 +0000 Subject: [PATCH] FTK change. Added flag to prevent misaligned I/O operations to occur when a file has been opened in directio mode. This will allow us to find and fix sub-optimal I/O operations. git-svn-id: https://svn.code.sf.net/p/flaim/code/trunk@671 0109f412-320b-0410-ab79-c3e0c5ffbbe6 --- ftk/src/ftk.h | 13 ++++++++++--- ftk/src/ftkfsys.cpp | 15 ++++++++++++++- ftk/src/ftkmem.cpp | 8 ++++---- ftk/src/ftknlm.cpp | 8 ++++++++ ftk/src/ftksupr.cpp | 32 +++++++++++++++++++------------- ftk/src/ftksys.h | 1 + ftk/src/ftkthrd.cpp | 25 +++++++++++++++++++++++++ ftk/src/ftkunix.cpp | 10 +++++++++- ftk/src/ftkwin.cpp | 8 ++++++++ 9 files changed, 98 insertions(+), 22 deletions(-) diff --git a/ftk/src/ftk.h b/ftk/src/ftk.h index 0ca93c6..9f009f2 100644 --- a/ftk/src/ftk.h +++ b/ftk/src/ftk.h @@ -469,7 +469,8 @@ #define NE_FLM_SETTING_FILE_INFO 0xC22C ///< 0xC22C - Unexpected error occurred while setting a file's information. #define NE_FLM_IO_PENDING 0xC22D ///< 0xC22D - I/O has not yet completed #define NE_FLM_ASYNC_FAILED 0xC22E ///< 0xC22E - An async I/O operation failed - + #define NE_FLM_MISALIGNED_IO 0xC22F ///< 0xC22F - Misaligned buffer, offset, or sector multiple encountered during I/O request + // Stream Errors - These are new #define NE_FLM_STREAM_DECOMPRESS_ERROR 0xC400 ///< 0xC400 - Error decompressing data stream. @@ -653,6 +654,7 @@ #define FLM_IO_SH_DENYNONE 0x0040 #define FLM_IO_DIRECT 0x0080 #define FLM_IO_DELETE_ON_RELEASE 0x0100 + #define FLM_IO_MISALIGNED_OK 0x0200 // File Positioning Definitions @@ -1751,7 +1753,8 @@ RCODE FLMAPI setup( IF_SuperFileClient * pSuperFileClient, IF_FileHdlCache * pFileHdlCache, - FLMBOOL bUseDirectIO); + FLMUINT uiFileOpenFlags, + FLMUINT uiFileCreateFlags); RCODE FLMAPI createFile( FLMUINT uiFileNumber, @@ -1834,7 +1837,8 @@ FLMUINT m_uiBlockSize; FLMUINT m_uiExtendSize; FLMUINT m_uiMaxAutoExtendSize; - FLMUINT m_uiDirectIOFlag; + FLMUINT m_uiFileOpenFlags; + FLMUINT m_uiFileCreateFlags; }; /**************************************************************************** @@ -2305,6 +2309,9 @@ virtual FLMUINT FLMAPI getThreadGroup( void) = 0; virtual void FLMAPI cleanupThread( void) = 0; + + virtual void FLMAPI sleep( + FLMUINT uiMilliseconds) = 0; }; RCODE FLMAPI f_threadCreate( diff --git a/ftk/src/ftkfsys.cpp b/ftk/src/ftkfsys.cpp index dc0d7b1..2973758 100644 --- a/ftk/src/ftkfsys.cpp +++ b/ftk/src/ftkfsys.cpp @@ -2757,6 +2757,7 @@ void F_FileHdl::initCommonData( void) m_bOpenedReadOnly = FALSE; m_bOpenedExclusive = FALSE; m_bOpenedInAsyncMode = FALSE; + m_bRequireAlignedIO = FALSE; m_bDoDirectIO = FALSE; m_numAsyncPending = 0; } @@ -3353,6 +3354,12 @@ RCODE F_FileHdl::directRead( (((FLMUINT64)pucDestBuffer) & m_ui64NotOnSectorBoundMask) || (((FLMUINT64)uiBytesToRead & m_ui64NotOnSectorBoundMask))) { + if( m_bRequireAlignedIO) + { + rc = RC_SET_AND_ASSERT( NE_FLM_MISALIGNED_IO); + goto Exit; + } + pucReadBuffer = m_pucAlignedBuff; // Must read enough bytes to cover all of the sectors that @@ -3399,7 +3406,7 @@ RCODE F_FileHdl::directRead( // bytes read by the difference between the start of the // sector and the actual read offset. - if( uiCurrentBytesRead && ui64ReadOffset & m_ui64NotOnSectorBoundMask) + if( uiCurrentBytesRead && (ui64ReadOffset & m_ui64NotOnSectorBoundMask)) { pucReadBuffer += (ui64ReadOffset & m_ui64NotOnSectorBoundMask); f_assert( uiCurrentBytesRead >= m_uiBytesPerSector); @@ -3523,6 +3530,12 @@ RCODE F_FileHdl::directWrite( !bBufferOnSectorBound || !bSizeIsSectorMultiple) { + if( m_bRequireAlignedIO) + { + rc = RC_SET_AND_ASSERT( NE_FLM_MISALIGNED_IO); + goto Exit; + } + bWaitForWrite = TRUE; pucWriteBuffer = m_pucAlignedBuff; diff --git a/ftk/src/ftkmem.cpp b/ftk/src/ftkmem.cpp index 841ec6f..8a15ed1 100644 --- a/ftk/src/ftkmem.cpp +++ b/ftk/src/ftkmem.cpp @@ -2237,9 +2237,9 @@ Desc: RCODE FLMAPI F_SlabManager::setup( FLMUINT uiPreallocSize) { - RCODE rc = NE_FLM_OK; - FLMUINT uiSysSlabSize = 0; - FLMUINT uiSlabSize = 64 * 1024; + RCODE rc = NE_FLM_OK; + FLMUINT uiSysSlabSize = 0; + FLMUINT uiSlabSize = 64 * 1024; if( RC_BAD( rc = f_mutexCreate( &m_hMutex))) { @@ -2846,7 +2846,7 @@ RCODE F_FixedAlloc::setup( FLM_SLAB_USAGE * pUsageStats, FLMUINT * puiTotalBytesAllocated) { - RCODE rc = NE_FLM_OK; + RCODE rc = NE_FLM_OK; f_assert( pSlabManager); f_assert( uiCellSize); diff --git a/ftk/src/ftknlm.cpp b/ftk/src/ftknlm.cpp index 591b442..961b3c1 100644 --- a/ftk/src/ftknlm.cpp +++ b/ftk/src/ftknlm.cpp @@ -1472,6 +1472,14 @@ Retry_Create: goto Exit; } + if( bDoDirectIO) + { + if( (uiIoFlags & FLM_IO_MISALIGNED_OK) == 0) + { + m_bRequireAlignedIO = TRUE; + } + } + m_bFileOpened = TRUE; m_bDoDirectIO = bDoDirectIO; m_bOpenedInAsyncMode = bUsingAsync; diff --git a/ftk/src/ftksupr.cpp b/ftk/src/ftksupr.cpp index b304928..5c33fcf 100644 --- a/ftk/src/ftksupr.cpp +++ b/ftk/src/ftksupr.cpp @@ -39,7 +39,8 @@ F_SuperFileHdl::F_SuperFileHdl( void) m_uiBlockSize = 0; m_uiExtendSize = (8 * 1024 * 1024); m_uiMaxAutoExtendSize = f_getMaxFileSize(); - m_uiDirectIOFlag = 0; + m_uiFileOpenFlags = 0; + m_uiFileCreateFlags = 0; } /**************************************************************************** @@ -85,7 +86,8 @@ Desc: Configures the super file object RCODE FLMAPI F_SuperFileHdl::setup( IF_SuperFileClient * pSuperFileClient, IF_FileHdlCache * pFileHdlCache, - FLMBOOL bUseDirectIO) + FLMUINT uiFileOpenFlags, + FLMUINT uiFileCreateFlags) { RCODE rc = NE_FLM_OK; @@ -107,10 +109,8 @@ RCODE FLMAPI F_SuperFileHdl::setup( } } - if( bUseDirectIO) - { - m_uiDirectIOFlag |= FLM_IO_DIRECT; - } + m_uiFileOpenFlags = uiFileOpenFlags; + m_uiFileCreateFlags = uiFileCreateFlags; Exit: @@ -130,6 +130,15 @@ RCODE FLMAPI F_SuperFileHdl::createFile( f_assert( m_uiBlockSize); + // If the file creation flags are not set we won't allow this operation + // to continue + + if( !m_uiFileCreateFlags) + { + rc = RC_SET_AND_ASSERT( NE_FLM_ILLEGAL_OP); + goto Exit; + } + // See if we already have an open file handle (or if we can open the file). // If so, truncate the file and use it. @@ -150,10 +159,9 @@ RCODE FLMAPI F_SuperFileHdl::createFile( { goto Exit; } - + if( RC_BAD( rc = m_pFileHdlCache->createFile( szFilePath, - FLM_IO_RDWR | FLM_IO_EXCL | FLM_IO_SH_DENYNONE | m_uiDirectIOFlag, - &pFileHdl))) + m_uiFileCreateFlags, &pFileHdl))) { goto Exit; } @@ -585,8 +593,7 @@ RCODE FLMAPI F_SuperFileHdl::getFileHdl( } if( RC_BAD( rc = m_pFileHdlCache->openFile( szFilePath, - FLM_IO_RDWR | FLM_IO_SH_DENYNONE | m_uiDirectIOFlag, - &pFileHdl))) + m_uiFileOpenFlags, &pFileHdl))) { goto Exit; } @@ -632,8 +639,7 @@ RCODE FLMAPI F_SuperFileHdl::getFileHdl( } if( RC_BAD( rc = m_pFileHdlCache->openFile( szFilePath, - FLM_IO_RDWR | FLM_IO_SH_DENYNONE | m_uiDirectIOFlag, - &pFileHdl))) + m_uiFileOpenFlags, &pFileHdl))) { goto Exit; } diff --git a/ftk/src/ftksys.h b/ftk/src/ftksys.h index 561e45b..c0bbbdb 100644 --- a/ftk/src/ftksys.h +++ b/ftk/src/ftksys.h @@ -780,6 +780,7 @@ FLMBOOL m_bOpenedExclusive; FLMBOOL m_bDoDirectIO; FLMBOOL m_bOpenedInAsyncMode; + FLMBOOL m_bRequireAlignedIO; FLMATOMIC m_numAsyncPending; #if defined( FLM_WIN) diff --git a/ftk/src/ftkthrd.cpp b/ftk/src/ftkthrd.cpp index ff5aa58..64f3a9c 100644 --- a/ftk/src/ftkthrd.cpp +++ b/ftk/src/ftkthrd.cpp @@ -266,6 +266,9 @@ public: void FLMAPI cleanupThread( void); + void FLMAPI sleep( + FLMUINT uiMilliseconds); + F_MUTEX m_hMutex; F_Thread * m_pPrev; F_Thread * m_pNext; @@ -776,6 +779,28 @@ void FLMAPI F_Thread::cleanupThread( void) m_exitRc = NE_FLM_OK; } +/**************************************************************************** +Desc: +****************************************************************************/ +void FLMAPI F_Thread::sleep( + FLMUINT uiMilliseconds) +{ + FLMUINT uiTimeToSleep; + + if( !uiMilliseconds) + { + f_yieldCPU(); + return; + } + + while( uiMilliseconds && !m_bShutdown) + { + uiTimeToSleep = f_min( uiMilliseconds, 50); + f_sleep( uiTimeToSleep); + uiMilliseconds -= uiTimeToSleep; + } +} + /**************************************************************************** Desc: Set the thread's status ****************************************************************************/ diff --git a/ftk/src/ftkunix.cpp b/ftk/src/ftkunix.cpp index a40a281..c980d51 100644 --- a/ftk/src/ftkunix.cpp +++ b/ftk/src/ftkunix.cpp @@ -299,6 +299,14 @@ Retry_Create: { goto Exit; } + + if( bDoDirectIO) + { + if( (uiIoFlags & FLM_IO_MISALIGNED_OK) == 0) + { + m_bRequireAlignedIO = TRUE; + } + } m_bFileOpened = TRUE; m_bDoDirectIO = bDoDirectIO; @@ -515,7 +523,7 @@ RCODE F_FileHdl::lowLevelRead( } #if defined( FLM_LINUX) || defined( FLM_SOLARIS) || defined( FLM_OSX) - if( m_bOpenedInAsyncMode) + if( m_bOpenedInAsyncMode && pIOBuffer) { struct aiocb * pAIO; diff --git a/ftk/src/ftkwin.cpp b/ftk/src/ftkwin.cpp index 354e86e..8a5379d 100644 --- a/ftk/src/ftkwin.cpp +++ b/ftk/src/ftkwin.cpp @@ -233,6 +233,14 @@ Retry_Create: goto Exit; } + if( bDoDirectIO) + { + if( (uiIoFlags & FLM_IO_MISALIGNED_OK) == 0) + { + m_bRequireAlignedIO = TRUE; + } + } + m_bFileOpened = TRUE; m_ui64CurrentPos = 0; m_bOpenedReadOnly = (uiIoFlags & FLM_IO_RDONLY) ? TRUE : FALSE;