RE: Raw I/O question

  • From: "Magni Fabrizio" <Fabrizio.Magni@xxxxxxxxx>
  • To: "Kramer, James P." <james.p.kramer@xxxxxxxxxx>, <oracle-l@xxxxxxxxxxxxx>
  • Date: Tue, 4 Jan 2005 10:36:18 +0100

Sorry,
I was on vacation. :)

Below you can find the program modified to be tested with raw devices.

It could take long if you have a large device so I suggest you to bind
raw1 to a small partition.

Regards
Fabrizio

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <asm/fcntl.h>
#include <errno.h>
#include <string.h>
#define BUFFSIZE 65536
#define BLKSIZE  4096
#define ALIGN    4096

main() {
unsigned char  * buff;
int stat1=3D0,stat2=3D0,stat3=3D0;
int fd1=3D0,fd2=3D0;
=20

   buff=3D(char *)(((unsigned long)buff + (BLKSIZE - 1)) & (~(BLKSIZE -
1)));

   if (stat3=3Dposix_memalign(&buff,ALIGN,BUFFSIZE)) {
      fprintf(stderr,"ALIGN ERR:%s\n",strerror(stat3));
      exit(0);
   }

   fd1=3Dopen("/dev/raw/raw1", O_RDONLY);

   if ((lseek(fd1, 0, SEEK_SET)) < 0){
        perror("lseek"); // problem if 2nd arg > 2G
        exit (1);
        }


   while(stat1=3Dread(fd1,buff,BLKSIZE)) {
         if (errno) {
            fprintf(stderr,"%d READ ERR:%s\n",stat1,strerror(errno));
            exit(0);
         }

   }
   close(fd1);
}

> -----Original Message-----
> From: Kramer, James P. [mailto:james.p.kramer@xxxxxxxxxx]=20
> Sent: Tuesday, December 28, 2004 8:01 PM
> To: Magni Fabrizio; oracle-l@xxxxxxxxxxxxx
> Subject: Raw I/O question
>=20
>=20
> I now have another question pertaining to I/O.  In my=20
> previous question, I was having problems getting Direct I/O=20
> to work.  With your help, I was able to get that working. =20
> Now I want to compare Raw I/O and Direct I/O and see if there=20
> is any difference.
>=20
> I bound a disk device to a raw device by typing
>=20
> raw /dev/raw/raw1 /dev/sde1
>=20
> This worked.  I then changed the code below to use=20
> "/dev/raw/raw1" as the file that gets opened.  I needed to=20
> have the right privileges to get past the EACCES error in the=20
> open.  Now I am receiving an EBUSY (16) error on the open.  I=20
> tried changing the flags to O_RDWR | O_NONBLOCK and that=20
> didn't work.  Is there another way to do raw I/O?  I assumed=20
> that this is the way to do it.  Any other gotchas that I forgot about?
>=20
> Thanks again,
>=20
> James Kramer
>=20
> THIS COMMUNICATION MAY CONTAIN CONFIDENTIAL AND/OR OTHERWISE=20
> PROPRIETARY MATERIAL and is thus for use only by the intended=20
> recipient. If you received this in error, please contact the=20
> sender and delete the e-mail and its attachments from all computers.=20
>=20
>=20
>=20
> -----Original Message-----
> From: Magni Fabrizio [mailto:Fabrizio.Magni@xxxxxxxxx]
> Sent: Monday, December 13, 2004 9:19 AM
> To: Kramer, James P.; oracle-l@xxxxxxxxxxxxx
> Subject: RE: Direct I/O questions
>=20
>=20
> Hello James,
> eventually I was able to make O_DIRECT work even on x86_64 (kernel
> 2.6.5-7.97-smp and 2.6.9 vanilla).
> Unfortunately I haven't got any itanium where to test it.
>=20
> I had to create a file named xxx in the same directory of the=20
> executable
> with:
>=20
> dd if=3D/dev/zero of=3Dxxx bs=3D4196 count=3D100
>=20
> I tested on ext2, ext3, reiserfs.
>=20
> Regards
> Fabrizio
>=20
> The code (Thanks to Andrea Arcangeli for the revision):
>=20
> #define _GNU_SOURCE
> #include <sys/types.h>
> #include <sys/stat.h>
> #include <sys/ioctl.h>
> #include <sys/mman.h>
> #include <stdlib.h>
> #include <unistd.h>
> #include <stdio.h>
> #include <errno.h>
> #include <string.h>
> #include <fcntl.h>
> #include <stdlib.h>
>=20
> #define O_DIRECT         040000 /* direct disk access hint */
>=20
>=20
> int main(int argc, char *argv[])
> {
>=20
>      int fd =3D open("xxx", O_RDWR|O_DIRECT);
>      int len, pagesize =3D getpagesize();
>      char *message;
>=20
>         printf("Pagesize: %d\n", pagesize);
>=20
>      posix_memalign((void **)&message, pagesize, pagesize);
>      memset(message, 0xff, pagesize);
>      printf("%p\n", message);
>      if(fd < 0) {
>          printf("Unable to open file, errno is %d.\n", errno);
>      } else {
>          if((len =3D read(fd, message, pagesize)) < 0) {
>             perror("read");
>           } else {
>             printf("%d bytes read from file.\n", len);
>             printf("Message: %s", message);
>           }
>     }
>     close(fd);
>=20
>     return 0;
> }
>=20
>=20
--
//www.freelists.org/webpage/oracle-l

Other related posts: