Presentation is loading. Please wait.

Presentation is loading. Please wait.

Ways to read data from disk to memory Tan Li. read, write read, write -- low level file access, it's an operation between two file discriptors. SYNOPSIS.

Similar presentations


Presentation on theme: "Ways to read data from disk to memory Tan Li. read, write read, write -- low level file access, it's an operation between two file discriptors. SYNOPSIS."— Presentation transcript:

1 Ways to read data from disk to memory Tan Li

2 read, write read, write -- low level file access, it's an operation between two file discriptors. SYNOPSIS #include size_t read(int fildes, void *buf, size_t nbytes); size_t write(int fildes, const void *buf, size_t nbytes); DESCRIPTION The read system call reads up to nbytes bytes of data from the file associated with the file descriptor fildes and places them in the data area buf. The write system call arranges for the first nbytes bytes from buf to be written to the file associated with the file descriptor fildes.

3 fread, fwrite fread, fwrite - binary stream input/output, they come from the standarded C library. SYNOPSIS #include size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream); size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream); DESCRIPTION The fread library function is used to read data from a file stream. Data is read into a data buffer given by ptr from the stream, stream. Both fread and fwrite deal with data records. These are specified by a record size, size, and a count, nitems, of records to transfer.

4 sendfile sendfile - transfer data between file descriptors SYNOPSIS #include ssize_t sendfile(int out_fd, int in_fd, off_t *offset, size_t count); DESCRIPTION Because this copying is done within the kernel, sendfile() is more efficient than the combination of read and write, which would require transferring data to and from user space. It appears that as for Linux kernel 2.6.X sendfile() syscall can only be used to transfer data from a file descriptor to a socket descriptor. The so called "misfeature" of allowing data transfer between two files has been removed, while I could not find any reported problems about it in 2.4.X kernels where it can work with 2 file descriptors.

5 read() VS fread read Read with O_DIRECT fread Buffer sizeCPUSpeedtime 1 B99%2MB/s534.56 sec 1 KB99%1.42G/s0.7 sec Buffer sizeCPUSpeedtime 1 B99%53.2MB/s18.8 sec 1 KB99%2.85G/s0.35 sec Buffer sizeCPUSpeedtime 4 KB6%29.4MB/s33.99 sec 4 MB18%1.85G/s0.54 sec

6 Test Method Client: read data from file (1GB), and then send it to the socket. Server: read data from socket, and then write it to the output file. Using 1. Regular read() and write() 2. Read() and write() with O_DIRECT 3. Client: Sendfile() + Server: read() and write()

7 Test Results read()+write() O_DIRECT Sendfile()+read()+write Buffer sizeCPUSpeedtime 4 KB55%549.45MB/s1.82 sec 400 KB45%787.4 MB/s1.27 sec Buffer sizeCPUSpeedTime 4 KB7%14.97MB/s66.81 sec 400 KB16%335.57 MB/s2.98 sec Buffer sizeCPUSpeedTime 4 KB48%555.55 MB/s1.8 sec 400 KB31%751.88 MB/s1.33 sec

8 Test Results read()+write() VS sendfile() + read() + write() buffer size: 400KB, file size: 100GB CPUSpeedtime R+W27%305.39MB/s327.45 sec S+R+W27%307.97MB/s324.71 sec


Download ppt "Ways to read data from disk to memory Tan Li. read, write read, write -- low level file access, it's an operation between two file discriptors. SYNOPSIS."

Similar presentations


Ads by Google