服务器 频道

如何判断Solaris上是否支持异步IO

   【IT168 服务器学院】在Solaris Internals这本书上,作者提到一段代码用以检测系统是否支持异步I/O.感觉很有助于理解Solaris系统异步IO的实现以及系统调用。

   列在这里和大家分享.

   1.源码:

   /*
   * Quick kaio test. Read 1k bytes from a file using async I/O.
   * To compile:
   * cc -o aio aio.c -laio
   * To run:
   * aio file_name
   */
   #include
   #include
   #include
   #include 

   #define BSIZE 1024

   main(int argc, char *argv[])
   {
   aio_result_t res;
   char buf[BSIZE];
   int fd;

   if ((fd=open(argv[1], O_RDONLY)) == -1) {
   perror("open");
   exit(-1);
   }
   aioread(fd, buf, BSIZE, 0L, SEEK_SET, &res);
   aiowait(0);
   if (res.aio_return == BSIZE) {
   printf("aio succeeded\n");
   close(fd);
   exit(0);
   }
   perror("aio");
   }

   2.编译:

   # cc -o aio aio.c -laio

   3.运行:

   首先测试裸设备:

# truss -t kaio,lwp_create ./aio /dev/rdsk/c0t0d0s1

kaio(5, 0xFFBEF640, 0x00000000, 0xFF21FB68, 0x00000000, 0xFFBEF648, 0x00000000) = 0

lwp_create(0xFFBEF640, 0, 0xFF21FF5C)           = 2

lwp_create()    (returning as new lwp ...)      = 0

kaio(AIOREAD, 3, 0xFFBEF9C0, 1024, 0, 0xFFBEFDC0) = 0

kaio(AIOWAIT, 0x00000000)                       = 4290706880

aio succeeded


   我们看到,Solaris对于裸设备,最终异步IO调用通过AIOREAD来实现,异步操作成功。

   而对于常规文件系统文件的读取:

# truss -t kaio,lwp_create ./aio /a.sh

kaio(5, 0xFFBEF650, 0x00000000, 0xFF21FB68, 0x00000000, 0xFFBEF658, 0x00000000) = 0

lwp_create(0xFFBEF650, 0, 0xFF21FF5C)           = 2

lwp_create()    (returning as new lwp ...)      = 0

kaio(AIOREAD, 3, 0xFFBEF9D0, 1024, 0, 0xFFBEFDD0) Err#48 ENOTSUP

lwp_create(0xFFBEF5F0, 0, 0xFF20DF5C)           = 3

lwp_create()    (returning as new lwp ...)      = 0

lwp_create(0xFFBEF5F0, 0, 0xFF1FBF5C)           = 4

lwp_create()    (returning as new lwp ...)      = 0

lwp_create(0xFFBEF5F0, 0, 0xFF1E9F5C)           = 5

lwp_create()    (returning as new lwp ...)      = 0

lwp_create(0xFFBEF5F0, 0, 0xFF1D7F5C)           = 6

lwp_create()    (returning as new lwp ...)      = 0

lwp_create(0xFFBEF5F0, 0, 0xFF1C5F5C)           = 7

lwp_create()    (returning as new lwp ...)      = 0

lwp_create(0xFFBEF5F0, 0, 0xFF1B3F5C)           = 8

lwp_create()    (returning as new lwp ...)      = 0

lwp_create(0xFFBEF5F0, 0, 0xFF1A1F5C)           = 9

lwp_create()    (returning as new lwp ...)      = 0

lwp_create(0xFFBEF5F0, 0, 0xFF18FF5C)           = 10

lwp_create()    (returning as new lwp ...)      = 0

lwp_create(0xFFBEF5F0, 0, 0xFF17DF5C)           = 11

lwp_create()    (returning as new lwp ...)      = 0

kaio(AIOWAIT, 0x00000000)                       = 1

kaio(AIONOTIFY, -13008896)                      = 0

aio succeeded


   我们注意到,首先对于AIOREAD系统给出了一个Err#48错误,表明对于文件系统的异步IO不被支持。

   最后操作成功,实际上是系统把异步IO调用转化为pread读取。

   在完整的truss输出中你可以看到如下一行:

   pread64(3, "7F E L F010201\0\0\0\0\0".., 1024, 0) = 1024

   而成功的异步IO是通过如下一行来完成读取的:

   kaio(AIOREAD, 3, 0xFFBEF9C0, 1024, 0, 0xFFBEFDC0) = 0

   下面是完整的truss输出供参考:

# truss /aio a.sh

execve("/aio", 0xFFBEFE4C, 0xFFBEFE58)  argc = 2

resolvepath("/usr/lib/ld.so.1", "/usr/lib/ld.so.1", 1023) = 16

open("/var/ld/ld.config", O_RDONLY)             Err#2 ENOENT

stat("/usr/lib/libaio.so.1", 0xFFBEF56C)        = 0

open("/usr/lib/libaio.so.1", O_RDONLY)          = 3

fstat(3, 0xFFBEF56C)                            = 0

mmap(0x00000000, 8192, PROT_READ|PROT_EXEC, MAP_PRIVATE, 3, 0) = 0xFF3A0000

mmap(0x00000000, 106496, PROT_READ|PROT_EXEC, MAP_PRIVATE, 3, 0) = 0xFF380000

mmap(0xFF398000, 1584, PROT_READ|PROT_WRITE|PROT_EXEC, MAP_PRIVATE|MAP_FIXED, 3, 32768) = 0xFF398000

munmap(0xFF388000, 65536)                       = 0

memcntl(0xFF380000, 7184, MC_ADVISE, MADV_WILLNEED, 0, 0) = 0

close(3)                                        = 0

stat("/usr/lib/libc.so.1", 0xFFBEF56C)          = 0

open("/usr/lib/libc.so.1", O_RDONLY)            = 3

fstat(3, 0xFFBEF56C)                            = 0

mmap(0xFF3A0000, 8192, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_FIXED, 3, 0) = 0xFF3A0000

mmap(0x00000000, 802816, PROT_READ|PROT_EXEC, MAP_PRIVATE, 3, 0) = 0xFF280000

mmap(0xFF33C000, 24764, PROT_READ|PROT_WRITE|PROT_EXEC, MAP_PRIVATE|MAP_FIXED, 3, 704512) = 0xFF33C000

munmap(0xFF32C000, 65536)                       = 0

memcntl(0xFF280000, 113504, MC_ADVISE, MADV_WILLNEED, 0, 0) = 0

close(3)                                        = 0

stat("/usr/lib/libdl.so.1", 0xFFBEF56C)         = 0

open("/usr/lib/libdl.so.1", O_RDONLY)           = 3

fstat(3, 0xFFBEF56C)                            = 0

mmap(0xFF3A0000, 8192, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_FIXED, 3, 0) = 0xFF3A0000

close(3)                                        = 0

stat("/usr/platform/SUNW,Ultra-4/lib/libc_psr.so.1", 0xFFBEF37C) = 0

open("/usr/platform/SUNW,Ultra-4/lib/libc_psr.so.1", O_RDONLY) = 3

fstat(3, 0xFFBEF37C)                            = 0

mmap(0x00000000, 8192, PROT_READ|PROT_EXEC, MAP_PRIVATE, 3, 0) = 0xFF370000

mmap(0x00000000, 8192, PROT_READ|PROT_WRITE|PROT_EXEC, MAP_PRIVATE|MAP_ANON, -1, 0) = 0xFF360000

mmap(0x00000000, 16384, PROT_READ|PROT_EXEC, MAP_PRIVATE, 3, 0) = 0xFF350000

close(3)                                        = 0

munmap(0xFF370000, 8192)                        = 0

open("a.sh", O_RDONLY)                          = 3

sysconfig(_CONFIG_PAGESIZE)                     = 8192

open("/dev/zero", O_RDWR)                       = 4
mmap(0x00000000, 1179648, PROT_READ|PROT_WRITE|PROT_EXEC, MAP_PRIVATE|MAP_NORESERVE, 4, 0) = 0xFF100000

close(4)                                        = 0

mprotect(0xFF100000, 8192, PROT_NONE)           = 0

mprotect(0xFF112000, 8192, PROT_NONE)           = 0

mprotect(0xFF124000, 8192, PROT_NONE)           = 0

mprotect(0xFF136000, 8192, PROT_NONE)           = 0

mprotect(0xFF148000, 8192, PROT_NONE)           = 0

mprotect(0xFF15A000, 8192, PROT_NONE)           = 0

mprotect(0xFF16C000, 8192, PROT_NONE)           = 0

mprotect(0xFF17E000, 8192, PROT_NONE)           = 0

mprotect(0xFF190000, 8192, PROT_NONE)           = 0

mprotect(0xFF1A2000, 8192, PROT_NONE)           = 0

mprotect(0xFF1B4000, 8192, PROT_NONE)           = 0

mprotect(0xFF1C6000, 8192, PROT_NONE)           = 0

mprotect(0xFF1D8000, 8192, PROT_NONE)           = 0

mprotect(0xFF1EA000, 8192, PROT_NONE)           = 0

mprotect(0xFF1FC000, 8192, PROT_NONE)           = 0

mprotect(0xFF20E000, 8192, PROT_NONE)           = 0

sigfillset(0xFF3428C8)                          = 0

kaio(5, 0xFFBEF650, 0x00000000, 0xFF21FB68, 0x00000000, 0xFFBEF658, 0x00000000) = 0

lwp_create(0xFFBEF650, 0, 0xFF21FF5C)           = 2

lwp_create()    (returning as new lwp ...)      = 0

kaio(AIOREAD, 3, 0xFFBEF9D0, 1024, 0, 0xFFBEFDD0) Err#48 ENOTSUP

getpid()                                        = 22727 [22726]

sigaction(SIGPROF, 0xFFBEF798, 0xFF399768)      = 0

brk(0x00020B20)                                 = 0

brk(0x0002AB20)                                 = 0

lwp_create(0xFFBEF5F0, 0, 0xFF20DF5C)           = 3

lwp_create()    (returning as new lwp ...)      = 0

lwp_self()                                      = 3

lwp_create(0xFFBEF5F0, 0, 0xFF1FBF5C)           = 4

lwp_create()    (returning as new lwp ...)      = 0

lwp_self()                                      = 4

lwp_create(0xFFBEF5F0, 0, 0xFF1E9F5C)           = 5

lwp_create()    (returning as new lwp ...)      = 0

lwp_self()                                      = 5

lwp_create(0xFFBEF5F0, 0, 0xFF1D7F5C)           = 6

lwp_create()    (returning as new lwp ...)      = 0

lwp_self()                                      = 6

lwp_create(0xFFBEF5F0, 0, 0xFF1C5F5C)           = 7

lwp_create()    (returning as new lwp ...)      = 0

lwp_self()                                      = 7

lwp_create(0xFFBEF5F0, 0, 0xFF1B3F5C)           = 8

lwp_create()    (returning as new lwp ...)      = 0

lwp_self()                                      = 8

lwp_create(0xFFBEF5F0, 0, 0xFF1A1F5C)           = 9

lwp_create()    (returning as new lwp ...)      = 0

lwp_self()                                      = 9

lwp_create(0xFFBEF5F0, 0, 0xFF18FF5C)           = 10

lwp_create()    (returning as new lwp ...)      = 0

lwp_self()                                      = 10

lwp_create(0xFFBEF5F0, 0, 0xFF17DF5C)           = 11

lwp_create()    (returning as new lwp ...)      = 0

lwp_self()                                      = 11

brk(0x0002AB20)                                 = 0

brk(0x0014AB20)                                 = 0

lwp_cond_broadcast(0xFF3997C0)                  = 0

lwp_cond_signal(0xFF20DFA0)                     = 0

lwp_cond_wait(0xFF20DFA0, 0xFF20DFB0, 0x00000000) = 0

pread64(3, "7F E L F010201\0\0\0\0\0".., 1024, 0) = 1024

ioctl(1, TCGETA, 0xFFBEEBD4)                    = 0

aio succeeded

write(1, " a i o   s u c c e e d e".., 14)      = 14

close(3)                                        = 0

llseek(0, 0, SEEK_CUR)                          = 14361

_exit(0)



 

# truss ./aio /dev/rdsk/c0t2d0s5

execve("/aio", 0xFFBEFE3C, 0xFFBEFE48)  argc = 2

resolvepath("/usr/lib/ld.so.1", "/usr/lib/ld.so.1", 1023) = 16

open("/var/ld/ld.config", O_RDONLY)             Err#2 ENOENT

stat("/usr/lib/libaio.so.1", 0xFFBEF55C)        = 0

open("/usr/lib/libaio.so.1", O_RDONLY)          = 3

fstat(3, 0xFFBEF55C)                            = 0

mmap(0x00000000, 8192, PROT_READ|PROT_EXEC, MAP_PRIVATE, 3, 0) = 0xFF3A0000

mmap(0x00000000, 106496, PROT_READ|PROT_EXEC, MAP_PRIVATE, 3, 0) = 0xFF380000

mmap(0xFF398000, 1584, PROT_READ|PROT_WRITE|PROT_EXEC, MAP_PRIVATE|MAP_FIXED, 3, 32768) = 0xFF398000

munmap(0xFF388000, 65536)                       = 0

memcntl(0xFF380000, 7184, MC_ADVISE, MADV_WILLNEED, 0, 0) = 0

close(3)                                        = 0

stat("/usr/lib/libc.so.1", 0xFFBEF55C)          = 0

open("/usr/lib/libc.so.1", O_RDONLY)            = 3

fstat(3, 0xFFBEF55C)                            = 0

mmap(0xFF3A0000, 8192, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_FIXED, 3, 0) = 0xFF3A0000

mmap(0x00000000, 802816, PROT_READ|PROT_EXEC, MAP_PRIVATE, 3, 0) = 0xFF280000

mmap(0xFF33C000, 24764, PROT_READ|PROT_WRITE|PROT_EXEC, MAP_PRIVATE|MAP_FIXED, 3, 704512) = 0xFF33C000

munmap(0xFF32C000, 65536)                       = 0

memcntl(0xFF280000, 113504, MC_ADVISE, MADV_WILLNEED, 0, 0) = 0

close(3)                                        = 0

stat("/usr/lib/libdl.so.1", 0xFFBEF55C)         = 0

open("/usr/lib/libdl.so.1", O_RDONLY)           = 3

fstat(3, 0xFFBEF55C)                            = 0

mmap(0xFF3A0000, 8192, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_FIXED, 3, 0) = 0xFF3A0000

close(3)                                        = 0

stat("/usr/platform/SUNW,Ultra-4/lib/libc_psr.so.1", 0xFFBEF36C) = 0

open("/usr/platform/SUNW,Ultra-4/lib/libc_psr.so.1", O_RDONLY) = 3

fstat(3, 0xFFBEF36C)                            = 0

mmap(0x00000000, 8192, PROT_READ|PROT_EXEC, MAP_PRIVATE, 3, 0) = 0xFF370000

mmap(0x00000000, 8192, PROT_READ|PROT_WRITE|PROT_EXEC, MAP_PRIVATE|MAP_ANON, -1, 0) = 0xFF360000

mmap(0x00000000, 16384, PROT_READ|PROT_EXEC, MAP_PRIVATE, 3, 0) = 0xFF350000

close(3)                                        = 0

munmap(0xFF370000, 8192)                        = 0

open("/dev/rdsk/c0t2d0s5", O_RDONLY)            = 3

sysconfig(_CONFIG_PAGESIZE)                     = 8192

open("/dev/zero", O_RDWR)                       = 4

mmap(0x00000000, 1179648, PROT_READ|PROT_WRITE|PROT_EXEC, MAP_PRIVATE|MAP_NORESERVE, 4, 0) = 0xFF100000

close(4)                                        = 0

mprotect(0xFF100000, 8192, PROT_NONE)           = 0

mprotect(0xFF112000, 8192, PROT_NONE)           = 0

mprotect(0xFF124000, 8192, PROT_NONE)           = 0

mprotect(0xFF136000, 8192, PROT_NONE)           = 0

mprotect(0xFF148000, 8192, PROT_NONE)           = 0

mprotect(0xFF15A000, 8192, PROT_NONE)           = 0

mprotect(0xFF16C000, 8192, PROT_NONE)           = 0

mprotect(0xFF17E000, 8192, PROT_NONE)           = 0

mprotect(0xFF190000, 8192, PROT_NONE)           = 0

mprotect(0xFF1A2000, 8192, PROT_NONE)           = 0

mprotect(0xFF1B4000, 8192, PROT_NONE)           = 0

mprotect(0xFF1C6000, 8192, PROT_NONE)           = 0

mprotect(0xFF1D8000, 8192, PROT_NONE)           = 0

mprotect(0xFF1EA000, 8192, PROT_NONE)           = 0

mprotect(0xFF1FC000, 8192, PROT_NONE)           = 0

mprotect(0xFF20E000, 8192, PROT_NONE)           = 0

sigfillset(0xFF3428C8)                          = 0

kaio(5, 0xFFBEF640, 0x00000000, 0xFF21FB68, 0x00000000, 0xFFBEF648, 0x00000000) = 0

lwp_create(0xFFBEF640, 0, 0xFF21FF5C)           = 2

lwp_create()    (returning as new lwp ...)      = 0

kaio(AIOREAD, 3, 0xFFBEF9C0, 1024, 0, 0xFFBEFDC0) = 0

kaio(AIOWAIT, 0x00000000)                       = 4290706880

ioctl(1, TCGETA, 0xFFBEEBC4)                    = 0

aio succeeded

write(1, " a i o   s u c c e e d e".., 14)      = 14

close(3)                                        = 0

llseek(0, 0, SEEK_CUR)                          = 18744

_exit(0)

#

0
相关文章