os0file.h
上传用户:tsgydb
上传日期:2007-04-14
资源大小:10674k
文件大小:12k
源码类别:

MySQL数据库

开发平台:

Visual C++

  1. /******************************************************
  2. The interface to the operating system file io
  3. (c) 1995 Innobase Oy
  4. Created 10/21/1995 Heikki Tuuri
  5. *******************************************************/
  6. #ifndef os0file_h
  7. #define os0file_h
  8. #include "univ.i"
  9. #ifdef __WIN__
  10. #if (defined(__NT__) || defined(__WIN2000__))
  11. #define WIN_ASYNC_IO
  12. #endif
  13. #define UNIV_NON_BUFFERED_IO
  14. #else
  15. #if defined(HAVE_AIO_H) && defined(HAVE_LIBRT)
  16. #define POSIX_ASYNC_IO
  17. #endif
  18. #endif
  19. #ifdef __WIN__
  20. #define os_file_t HANDLE
  21. #else
  22. typedef int os_file_t;
  23. #endif
  24. /* If this flag is TRUE, then we will use the native aio of the
  25. OS (provided we compiled Innobase with it in), otherwise we will
  26. use simulated aio we build below with threads */
  27. extern ibool os_aio_use_native_aio;
  28. #define OS_FILE_SECTOR_SIZE 512
  29. /* The next value should be smaller or equal to the smallest sector size used
  30. on any disk. A log block is required to be a portion of disk which is written
  31. so that if the start and the end of a block get written to disk, then the
  32. whole block gets written. This should be true even in most cases of a crash:
  33. if this fails for a log block, then it is equivalent to a media failure in the
  34. log. */
  35. #define OS_FILE_LOG_BLOCK_SIZE 512
  36. /* Options for file_create */
  37. #define OS_FILE_OPEN 51
  38. #define OS_FILE_CREATE 52
  39. #define OS_FILE_OVERWRITE 53
  40. /* Options for file_create */
  41. #define OS_FILE_AIO 61
  42. #define OS_FILE_NORMAL 62
  43. /* Error codes from os_file_get_last_error */
  44. #define OS_FILE_NOT_FOUND 71
  45. #define OS_FILE_DISK_FULL 72
  46. #define OS_FILE_ALREADY_EXISTS 73
  47. #define OS_FILE_AIO_RESOURCES_RESERVED 74 /* wait for OS aio resources
  48. to become available again */
  49. #define OS_FILE_ERROR_NOT_SPECIFIED 75
  50. /* Types for aio operations */
  51. #define OS_FILE_READ 10
  52. #define OS_FILE_WRITE 11
  53. #define OS_FILE_LOG 256 /* This can be ORed to type */
  54. #define OS_AIO_N_PENDING_IOS_PER_THREAD 32 /* Win NT does not allow more
  55. than 64 */
  56. /* Modes for aio operations */
  57. #define OS_AIO_NORMAL 21 /* Normal asynchronous i/o not for ibuf
  58. pages or ibuf bitmap pages */
  59. #define OS_AIO_IBUF 22 /* Asynchronous i/o for ibuf pages or ibuf
  60. bitmap pages */
  61. #define OS_AIO_LOG   23 /* Asynchronous i/o for the log */
  62. #define OS_AIO_SYNC 24 /* Asynchronous i/o where the calling thread
  63. will itself wait for the i/o to complete,
  64. doing also the job of the i/o-handler thread;
  65. can be used for any pages, ibuf or non-ibuf.
  66. This is used to save CPU time, as we can do
  67. with fewer thread switches. Plain synchronous
  68. i/o is not as good, because it must serialize
  69. the file seek and read or write, causing a
  70. bottleneck for parallelism. */
  71. #define OS_AIO_SIMULATED_WAKE_LATER 512 /* This can be ORed to mode
  72. in the call of os_aio(...),
  73. if the caller wants to post several i/o
  74. requests in a batch, and only after that
  75.   wake the i/o-handler thread; this has
  76. effect only in simulated aio */ 
  77. /********************************************************************
  78. Opens an existing file or creates a new. */
  79. os_file_t
  80. os_file_create(
  81. /*===========*/
  82. /* out, own: handle to the file, not defined if error,
  83. error number can be retrieved with os_get_last_error */
  84. char* name, /* in: name of the file or path as a null-terminated
  85. string */
  86. ulint create_mode,/* in: OS_FILE_OPEN if an existing file is opened
  87. (if does not exist, error), or OS_FILE_CREATE if a new
  88. file is created (if exists, error), OS_FILE_OVERWRITE
  89. if a new file is created or an old overwritten */
  90. ulint purpose,/* in: OS_FILE_AIO, if asynchronous, non-buffered i/o
  91. is desired, OS_FILE_NORMAL, if any normal file */
  92. ibool* success);/* out: TRUE if succeed, FALSE if error */
  93. /***************************************************************************
  94. Closes a file handle. In case of error, error number can be retrieved with
  95. os_file_get_last_error. */
  96. ibool
  97. os_file_close(
  98. /*==========*/
  99. /* out: TRUE if success */
  100. os_file_t file); /* in, own: handle to a file */
  101. /***************************************************************************
  102. Gets a file size. */
  103. ibool
  104. os_file_get_size(
  105. /*=============*/
  106. /* out: TRUE if success */
  107. os_file_t file, /* in: handle to a file */
  108. ulint* size, /* out: least significant 32 bits of file
  109. size */
  110. ulint* size_high);/* out: most significant 32 bits of size */
  111. /***************************************************************************
  112. Sets a file size. This function can be used to extend or truncate a file. */
  113. ibool
  114. os_file_set_size(
  115. /*=============*/
  116. /* out: TRUE if success */
  117. char* name, /* in: name of the file or path as a
  118. null-terminated string */
  119. os_file_t file, /* in: handle to a file */
  120. ulint size, /* in: least significant 32 bits of file
  121. size */
  122. ulint size_high);/* in: most significant 32 bits of size */
  123. /***************************************************************************
  124. Flushes the write buffers of a given file to the disk. */
  125. ibool
  126. os_file_flush(
  127. /*==========*/
  128. /* out: TRUE if success */
  129. os_file_t file); /* in, own: handle to a file */
  130. /***************************************************************************
  131. Retrieves the last error number if an error occurs in a file io function.
  132. The number should be retrieved before any other OS calls (because they may
  133. overwrite the error number). If the number is not known to this program,
  134. the OS error number + 100 is returned. */
  135. ulint
  136. os_file_get_last_error(void);
  137. /*========================*/
  138. /* out: error number, or OS error number + 100 */
  139. /***********************************************************************
  140. Requests a synchronous read operation. */
  141. ibool
  142. os_file_read(
  143. /*=========*/
  144. /* out: TRUE if request was
  145. successful, FALSE if fail */
  146. os_file_t file, /* in: handle to a file */
  147. void* buf, /* in: buffer where to read */
  148. ulint offset, /* in: least significant 32 bits of file
  149. offset where to read */
  150. ulint offset_high,/* in: most significant 32 bits of
  151. offset */
  152. ulint n); /* in: number of bytes to read */
  153. /***********************************************************************
  154. Requests a synchronous write operation. */
  155. ibool
  156. os_file_write(
  157. /*==========*/
  158. /* out: TRUE if request was
  159. successful, FALSE if fail */
  160. char* name, /* in: name of the file or path as a
  161. null-terminated string */
  162. os_file_t file, /* in: handle to a file */
  163. void* buf, /* in: buffer from which to write */
  164. ulint offset, /* in: least significant 32 bits of file
  165. offset where to write */
  166. ulint offset_high,/* in: most significant 32 bits of
  167. offset */
  168. ulint n); /* in: number of bytes to write */
  169. /****************************************************************************
  170. Initializes the asynchronous io system. Creates separate aio array for
  171. non-ibuf read and write, a third aio array for the ibuf i/o, with just one
  172. segment, two aio arrays for log reads and writes with one segment, and a
  173. synchronous aio array of the specified size. The combined number of segments
  174. in the three first aio arrays is the parameter n_segments given to the
  175. function. The caller must create an i/o handler thread for each segment in
  176. the four first arrays, but not for the sync aio array. */
  177. void
  178. os_aio_init(
  179. /*========*/
  180. ulint n, /* in: maximum number of pending aio operations
  181. allowed; n must be divisible by n_segments */
  182. ulint n_segments, /* in: combined number of segments in the four
  183. first aio arrays; must be >= 4 */
  184. ulint n_slots_sync); /* in: number of slots in the sync aio array */
  185. /***********************************************************************
  186. Requests an asynchronous i/o operation. */
  187. ibool
  188. os_aio(
  189. /*===*/
  190. /* out: TRUE if request was queued
  191. successfully, FALSE if fail */
  192. ulint type, /* in: OS_FILE_READ or OS_FILE_WRITE */
  193. ulint mode, /* in: OS_AIO_NORMAL, ..., possibly ORed
  194. to OS_AIO_SIMULATED_WAKE_LATER: the
  195. last flag advises this function not to wake
  196. i/o-handler threads, but the caller will
  197. do the waking explicitly later, in this
  198. way the caller can post several requests in
  199. a batch; NOTE that the batch must not be
  200. so big that it exhausts the slots in aio
  201. arrays! NOTE that a simulated batch
  202. may introduce hidden chances of deadlocks,
  203. because i/os are not actually handled until
  204. all have been posted: use with great
  205. caution! */
  206. char* name, /* in: name of the file or path as a
  207. null-terminated string */
  208. os_file_t file, /* in: handle to a file */
  209. void* buf, /* in: buffer where to read or from which
  210. to write */
  211. ulint offset, /* in: least significant 32 bits of file
  212. offset where to read or write */
  213. ulint offset_high, /* in: most significant 32 bits of
  214. offset */
  215. ulint n, /* in: number of bytes to read or write */
  216. void* message1,/* in: messages for the aio handler (these
  217. can be used to identify a completed aio
  218. operation); if mode is OS_AIO_SYNC, these
  219. are ignored */
  220. void* message2);
  221. /**************************************************************************
  222. Wakes up simulated aio i/o-handler threads if they have something to do. */
  223. void
  224. os_aio_simulated_wake_handler_threads(void);
  225. /*=======================================*/
  226. #ifdef WIN_ASYNC_IO
  227. /**************************************************************************
  228. This function is only used in Windows asynchronous i/o.
  229. Waits for an aio operation to complete. This function is used to wait the
  230. for completed requests. The aio array of pending requests is divided
  231. into segments. The thread specifies which segment or slot it wants to wait
  232. for. NOTE: this function will also take care of freeing the aio slot,
  233. therefore no other thread is allowed to do the freeing! */
  234. ibool
  235. os_aio_windows_handle(
  236. /*==================*/
  237. /* out: TRUE if the aio operation succeeded */
  238. ulint segment, /* in: the number of the segment in the aio
  239. arrays to wait for; segment 0 is the ibuf
  240. i/o thread, segment 1 the log i/o thread,
  241. then follow the non-ibuf read threads, and as
  242. the last are the non-ibuf write threads; if
  243. this is ULINT_UNDEFINED, then it means that
  244. sync aio is used, and this parameter is
  245. ignored */
  246. ulint pos, /* this parameter is used only in sync aio:
  247. wait for the aio slot at this position */  
  248. void** message1, /* out: the messages passed with the aio
  249. request; note that also in the case where
  250. the aio operation failed, these output
  251. parameters are valid and can be used to
  252. restart the operation, for example */
  253. void** message2);
  254. #endif
  255. #ifdef POSIX_ASYNC_IO
  256. /**************************************************************************
  257. This function is only used in Posix asynchronous i/o. Waits for an aio
  258. operation to complete. */
  259. ibool
  260. os_aio_posix_handle(
  261. /*================*/
  262. /* out: TRUE if the aio operation succeeded */
  263. ulint array_no, /* in: array number 0 - 3 */
  264. void** message1, /* out: the messages passed with the aio
  265. request; note that also in the case where
  266. the aio operation failed, these output
  267. parameters are valid and can be used to
  268. restart the operation, for example */
  269. void** message2);
  270. #endif
  271. /**************************************************************************
  272. Does simulated aio. This function should be called by an i/o-handler
  273. thread. */
  274. ibool
  275. os_aio_simulated_handle(
  276. /*====================*/
  277. /* out: TRUE if the aio operation succeeded */
  278. ulint segment, /* in: the number of the segment in the aio
  279. arrays to wait for; segment 0 is the ibuf
  280. i/o thread, segment 1 the log i/o thread,
  281. then follow the non-ibuf read threads, and as
  282. the last are the non-ibuf write threads */
  283. void** message1, /* out: the messages passed with the aio
  284. request; note that also in the case where
  285. the aio operation failed, these output
  286. parameters are valid and can be used to
  287. restart the operation, for example */
  288. void** message2);
  289. /**************************************************************************
  290. Validates the consistency of the aio system. */
  291. ibool
  292. os_aio_validate(void);
  293. /*=================*/
  294. /* out: TRUE if ok */
  295. /**************************************************************************
  296. Prints info of the aio arrays. */
  297. void
  298. os_aio_print(void);
  299. /*==============*/
  300. /**************************************************************************
  301. Checks that all slots in the system have been freed, that is, there are
  302. no pending io operations. */
  303. ibool
  304. os_aio_all_slots_free(void);
  305. /*=======================*/
  306. /* out: TRUE if all free */
  307. #endif