Friday, July 15, 2011

Scatter/gather I/O operation

This is a common way to do scatter/gather I/O (sometime called vectored I/O), it has great performance over large chunk of data. The readv() and writev() function are atomic means these system call only issued for once to transfer multiple data into the kernel.

unsigned len;
long iovcnt, iovcnt_max;

iovcnt_max = sysconf(_SC_IOV_MAX);
/* Determine how many part of data to be written */

while (len > 0) {
  iovcnt = len >= iovcnt_max ? iovcnt_max : len;
  struct iovec iov[iovcnt];
  /* Initialize iov's members, process with readv() or writev() */
  len -= iovcnt;

The above code first determine maximum allowed size of iovcnt, in Linux this value is 1024 but we should not rely on this but using system specific sysconf() function, after that the code determine struct iovec array and declare it as VLA and loop to write or read data until all data has been processed.