Regarding PHP file operations, we will also learn through a series of articles. Today we first learn an extension that few people have used, and many people even don’t know, is slightly different from our daily file operations. However, these differences are not something we can see directly with the naked eye, but mainly lies in the balance between business needs and performance.
What is Direct IO
Direct IO is actually a concept in the Linux operating system. It means to directly manipulate file streams, why is it said to be direct? In fact, when our operating system operates files, we do not immediately read and write files on the disk, and there is also a layer of page cache in the middle. Since it is cache, it will certainly bring about a certain performance improvement, but this is not completely absolute. The direct operation is to ignore this layer of cache operations and directly read and write files on the disk. We all know that there is a huge gap between disks, even solid-state drives, and the processing speed of CPU and memory, and the default page cache is used to make up for this gap. However, page cache will increase the CPU's calculation operations and take up memory, and direct operation will not have such problems. However, relatively speaking, its speed is not comparable to file reading operations with cache.
The above is a simple understanding of Direct IO. For a more detailed explanation, you can refer to the content of the second link in the document at the end of the article and conduct in-depth study. In PHP, we can directly download the Direct IO extension in PECL and install and use it according to the normal installation method of the extension.
Create a write file
Since it is a file operation, we will first create and write some file data.
$fd = dio_open("./test", O_RDWR | O_CREAT); echo dio_write($fd, "This is 'm me the money4i"), PHP_EOL; // 43 print_r(dio_stat($fd)); // Array // ( // [device] => 64768 // [inode] => 652548 // [mode] => 35432 // [nlink] => 1 // [uid] => 0 // [gid] => 0 // [device_type] => 0 // [size] => 43 // [block_size] => 4096 // [blocks] => 8 // [atime] => 1602643459 // [mtime] => 1602656963 // [ctime] => 1602656963 // ) dio_close($fd);
Similar to the f-series functions, we need to use a dio_open() function to open a file. The O_RDWR | O_CREAT parameter means to open a readable and writeable file and create it if the file does not exist. These two constants correspond to constants related to direct operation files in Linux. The explanations about these constants can also be seen in the link at the end of the article.
The write operation can be completed using a dio_write() . The content it returns is the length of the written content, and we wrote 43 characters here.
dio_stat() returns some information about the current file handle. We can see some information such as device number device, uid, gid, atime, mtime, etc. They are similar to the information we can see in Linux, but they are actually some simple information about this file.
Read the file
Reading files is very simple and you can complete it by using a function.
$fd = dio_open("./test", O_RDWR | O_CREAT); echo dio_read($fd), PHP_EOL; // This is 'm me the money4i dio_close($fd);
The dio_read() function also contains another parameter, which can read the content according to the specified byte length. We will see related examples later.
File Operation
During the file reading process, we may only need to read part of the content, or start reading the file content from a certain location. The following operation function operates on these two aspects.
$fd = dio_open("./test", O_RDWR | O_CREAT); var_dump(dio_truncate ($fd , 20)); // bool(true) echo dio_read($fd), PHP_EOL; // This is 'm ZyB dio_seek($fd, 3); echo dio_read($fd), PHP_EOL; // s is 'm ZyB dio_close($fd);
In fact, we can see from the name that dio_truncate() is used to truncate the content of the file. Here we truncate from the 20th character, and then read using dio_read() is only the content of the first 20 characters.
dio_seek() specifies which character to start reading content. After we specify that the starting character position is 3, the first three characters will not be read. It should be noted that dio_truncate() will modify the contents of the original file, while dio_seek() will not.
Other settings
$fd = dio_open('./test', O_RDWR | O_NOCTTY | O_NONBLOCK); dio_fcntl($fd, F_SETFL, O_SYNC); dio_tcsetattr($fd, array( 'baud' => 9600, 'bits' => 8, 'stop' => 1, 'parity' => 0 )); while (($data = dio_read($fd, 4))!=false) { echo $data, PHP_EOL; } // This // is // Test // .I'm // ZyB dio_close($fd);
The dio_fcntl() function is the fcntl function in the c function library called. The purpose is to perform some specified operations on the file descriptor. This operation is also fixed with some constants. Here we are using F_SETFL, which means setting the file descriptor flag to the specified value. This O_SYNC means that if this descriptor is set, the writing operation of the file will not end until the data is written to disk. Of course, this function can also set many other operators. You can refer to the official PHP documentation for in-depth study.
dio_tcsetattr() is used to set the terminal properties and baud rate of the opened file. baud represents the baud rate, bits represents the bit, stop represents the stop bit, and parity represents the parity bit. I don't know much about the contents of this aspect, so I won't explain it in detail. From this we can see that the basic courses in university classes are really very important. I believe that students who have studied these basic courses well will immediately understand the role of this function.
Finally, we use the second parameter in dio_read() to read the file content according to the byte length. We can see that the read content is output in 4 characters in length one by one.
Summarize
The learning of functions is relatively simple. The core thing is to know which business scenario this extension is more suitable for use. In the introduction at the beginning of the article, we have explained some of the differences between direct operation of files and ordinary file operations. When self-caching applications or when very large data needs to be transferred, direct operation is more friendly to CPU and memory. In other cases, we can just use the system's default file operation method. In fact, in most cases, we basically cannot see the significant difference between them. So in actual applications, the same sentence is still the case, based on the actual business situation, choose the best solution.
Test code:
Use of DirectIO Direct Operation File Extension in /zhangyue0503/dev-blog/blob/master/php/202010/source/.php
Reference documentation:
/manual/zh/
/developerworks/cn/linux/l-cn-directio/
This is the article about the use of DirectIO direct operation file extension in PHP. For more related content on using php extensions, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!