A method and system for improving the ejection speed of a USB flash drive in a Linux system
By dynamically adjusting the cache threshold of the USB drive in Linux system and limiting the number of cache page writebacks, the problem of long-term pop-up time of USB drive is solved and the user experience is improved.
Patent Information
- Application Number
- CN202510000806.9
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2025-01-02
- Publication Date
- 2025-03-04
- Estimated Expiration
- 2045-01-02
AI Technical Summary
The file cache management strategy of existing Linux systems causes the USB flash drive to pop up for a long time and poor user experience.
By dynamically adjusting the threshold based on the amount of data written to the USB disk and the usage rate timing, the number of cache pages that the system needs to write back for the cache writing mechanism of the USB disk is limited, ensuring that the number of cache pages that need to be written back to the USB disk is controllable, thereby increasing the pop-up speed of the USB disk.
It effectively solves the problem of long time and poor user experience of USB drive pop-up under Linux file cache management strategy, ensuring the fast and controllable U disk pop-up operation time, and improving user experience.
Smart Images

Figure CN119376658B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of device management of operating systems, and in particular to a method and system for improving the ejection speed of a USB flash drive in a Linux system. Background Art
[0002] In recent years, with the rapid rise of domestic operating systems, domestic operating systems are experiencing a rapid transformation from usable to easy to use. In addition, domestic operating systems are mostly secondary developed based on the Linux system, and many strategies of the Linux system are also used, such as the file cache mechanism. The Linux system uses the file cache mechanism to improve the access performance of files. However, as the memory of current desktop hardware becomes larger and larger, the file cache mechanism of the Linux system is becoming less and less friendly to the ejection operation of slow USB flash drives, and the user experience is very poor. Cache write is a technology used to improve the performance of storage systems. It uses the cache in the memory to temporarily store the data of write operations. This practice can improve the overall response speed and throughput of the system. Linux usually uses a write-back cache strategy, where data is first written to the cache in the memory, and then asynchronously written back to the disk in the background at the appropriate time. The current file system cache mechanism of Linux is to allocate cache pages from the memory when performing cache write operations (almost all write operations in the desktop environment are cache writes), and then cache the data written to the storage medium to the cache pages, indicating that the write operation is completed. Then, the cache pages are written back to the storage medium at a certain time (for example, when the memory is tight and needs to be reclaimed). Although this mechanism greatly improves the read and write operation performance of the application, it is not friendly to the ejection operation of the slow USB flash drive. When the user needs to eject the USB flash drive after using it, the system needs to write all cache pages back to the USB flash drive and release the cache pages to ensure the integrity of the data written to the USB flash drive before the ejection operation can be performed normally. In addition, the system cannot perceive and calculate the number of cache pages that need to be written back to the USB flash drive, and the ejection operation time of the USB flash drive becomes unpredictable. If the USB flash drive write speed is slow and the number of system USB flash drive cache pages is large, the user will see that the system is always stuck in the ejection operation when ejecting the USB flash drive, without any time-related prompts, which greatly affects the user experience. Summary of the invention
[0003] Technical problem to be solved by the present invention: In view of the above-mentioned problems in the prior art, a method and system for improving the ejection speed of a USB flash drive under a Linux system are provided. The present invention aims to solve the problems of long ejection time of a USB flash drive and poor user experience that may exist under the current Linux file cache management strategy.
[0004] In order to solve the above technical problems, the technical solution adopted by the present invention is:
[0005] A method for improving the ejection speed of a USB flash drive in a Linux system comprises the following steps:
[0006] Dynamically adjust the threshold based on the amount of data written to the USB flash drive and its usage rate;
[0007] According to the current threshold limit, the system limits the number of cache pages that need to be written back for the USB flash drive's cache write mechanism. When the number of cache pages that need to be written back exceeds the threshold, no new cache pages will be allocated. Instead, the cache pages in the USB flash drive cache linked list will be written back to the USB flash drive first, and then the cache pages will be recycled for use. This ensures that the number of cache pages that need to be written back to the USB flash drive is controllable, thereby improving the USB flash drive ejection speed.
[0008] Optionally, when the threshold value is adjusted dynamically based on the statistical written data volume and usage rate of the USB flash drive, each round of adjusting the threshold value includes:
[0009] Step ①, counting the amount of data written and the usage rate of the USB flash drive per unit time;
[0010] Step ②, predicting the maximum writing speed S of the USB flash drive according to the amount of data written per unit time and the usage rate of the USB flash drive;
[0011] Step ③, updating the threshold value according to the maximum writing speed S of the USB flash drive, and the threshold value is positively correlated with the maximum writing speed S.
[0012] Optionally, when counting the amount of data written and the usage rate of the U disk per unit time in step ①, counting the amount of data written per unit time of the U disk includes: first initializing the amount of data written N to 0, and then for the operation req issued by the data sending function scsi_queue_rq() of the scsi layer per unit time, calling the read-write judgment function rq_data_dir() in the kernel to determine whether rq_data_dir(req) is equal to the write operation constant WRITE, if not equal to the write operation constant WRITE, ignoring the operation req; otherwise, determining whether the storage medium written by the operation req is a U disk storage medium, if it is a U disk storage medium, adding the number of read and write bytes of the operation req to the amount of data written N through the function blk_req_bytes() to count the number of read and write operations, and finally obtaining the amount of data written N completed by the U disk per unit time after the unit time ends.
[0013] Optionally, when counting the amount of written data and the usage rate of the U disk per unit time in step ①, counting the usage rate of the U disk per unit time includes: (1) initializing and setting the sampling number samping_time per unit time as the total sampling number per unit time, and initializing the value of the U disk busy frequency per_busy as the total sampling number per unit time; (2) obtaining the number of uncompleted IO write requests of the U disk through the kernel function part_in_flight_rw(). If the number of uncompleted IO write requests of the U disk is greater than 0, it means that the U disk is busy, and the value of the U disk busy frequency per_busy is kept unchanged; otherwise, the value of the U disk busy frequency per_busy is reduced by 1; (3) firstly decrementing the sampling number samping_time by 1, and then judging whether it is greater than 0, if it is greater than 0, sleeping for a sampling interval and jumping to step (2); otherwise, calculating the U disk usage rate Rate_Busy per unit time, and dividing the U disk busy frequency per_busy by the total sampling number samping_time per unit time to obtain the U disk usage rate Rate_Busy per unit time.
[0014] Optionally, in step ②, the function expression for predicting the maximum write speed S of the USB flash drive based on the amount of written data and the usage rate of the USB flash drive per unit time is: S=N / Rate_Busy, where N is the amount of written data written by the USB flash drive per unit time, and Rate_Busy is the usage rate of the USB flash drive per unit time.
[0015] Optionally, in step ③, the function expression for setting the threshold according to the maximum writing speed S of the USB flash drive is:
[0016] cache_threshold =(S*T) / PAGE_SIZE,
[0017] In the above formula, cache_threshold is the threshold, T is the preset expected ejection time of the USB flash drive, and PAGE_SIZE is the page size.
[0018] Optionally, the number of cache pages that need to be written back by the cache write mechanism of the USB flash drive according to the current threshold limiting system is implemented based on a USB flash drive cache limiting module, and the number of cache pages that need to be written back by the USB flash drive cache limiting module according to the current threshold limiting system includes:
[0019] S101, declare and initialize the data structure required by the USB disk cache restriction module;
[0020] S102, when the storage medium is inserted into the computer, determining whether the storage medium is a USB flash drive, and storing the determination result in a disk descriptor of the storage medium;
[0021] S103, when the system writes data to the storage medium through the VFS layer, first determine whether the storage medium is a USB flash drive, if so, execute step S104, otherwise terminate the operation;
[0022] S104, determine whether the number of pages cached in the storage medium being written exceeds the threshold, if it exceeds the threshold, execute step S105, if it is equal to the threshold, execute step S106, otherwise, first allocate cache pages, and insert the allocated cache pages into the end of the USB disk cache linked list of the USB disk being written, and update the number of pages cached in the USB disk at the same time, and then end and exit;
[0023] S105, reclaiming cache pages of the USB flash drive cache linked list until the number of cache pages is equal to a threshold;
[0024] S106, taking a cache page from the head of the USB disk cache linked list, writing the data in the cache page back to the USB disk, and then inserting the cache page into the tail of the USB disk cache linked list.
[0025] In addition, the present invention also provides a system for improving the ejection speed of a USB flash drive under a Linux system, comprising a microprocessor and a memory connected to each other, wherein the microprocessor is programmed or configured to execute the method for improving the ejection speed of a USB flash drive under a Linux system.
[0026] In addition, the present invention also provides a computer-readable storage medium, in which a computer program or instruction is stored. The computer program or instruction is programmed or configured to execute the method for improving the ejection speed of a USB flash drive under the Linux system through a processor.
[0027] In addition, the present invention also provides a computer program product, including a computer program or instruction, which is programmed or configured to execute the method for improving the ejection speed of a USB flash drive under a Linux system through a processor.
[0028] Compared with the prior art, the present invention mainly has the following advantages: the method for improving the ejection speed of a USB flash drive under a Linux system of the present invention comprises dynamically adjusting the size of a threshold based on the statistical written data volume and usage rate of the USB flash drive; limiting the number of cache pages that need to be written back by the cache write mechanism of the USB flash drive according to the current threshold, so that when the number of cache pages that need to be written back exceeds the threshold, no new cache pages are allocated but the cache pages in the USB flash drive cache linked list are first written back to the USB flash drive and then the cache pages are recycled for use to ensure that the number of cache pages that need to be written back to the USB flash drive is controllable to improve the ejection speed of the USB flash drive. The present invention can solve the problems of long ejection time of the USB flash drive and poor user experience that may exist under the current Linux file cache management strategy. BRIEF DESCRIPTION OF THE DRAWINGS
[0029] Figure 1 Schematic diagram of the principle of the method of the embodiment of the present invention.
[0030] Figure 2 The following is a flow chart of the design and implementation of the USB disk cache limitation module in an embodiment of the present invention.
[0031] Figure 3 The figure is a flow chart of the design and implementation of the USB flash drive maximum write speed prediction module in the embodiment of the present invention.
[0032] Figure 4 This is a flow chart of counting the amount of data written and the usage rate of a USB flash drive per unit time of 1 second in an embodiment of the present invention. DETAILED DESCRIPTION
[0033] The embodiments of the present invention are described in detail below with reference to the accompanying drawings. In the description process, unnecessary details and functions for the present invention are omitted to avoid confusion in understanding the present invention.
[0034] like Figure 1 As shown, the method for improving the ejection speed of a USB flash drive in a Linux system in this embodiment includes the following steps:
[0035] Based on the statistical data volume and usage rate of the USB flash drive, the threshold value is dynamically adjusted regularly.
[0036] According to the current threshold limit, the system limits the number of cache pages that need to be written back for the USB flash drive's cache write mechanism. When the number of cache pages that need to be written back exceeds the threshold, no new cache pages will be allocated. Instead, the cache pages in the USB flash drive cache linked list will be written back to the USB flash drive first, and then the cache pages will be recycled for use. This ensures that the number of cache pages that need to be written back to the USB flash drive is controllable, thereby improving the USB flash drive ejection speed.
[0037] like Figure 1 As shown, in this embodiment, when the threshold value is dynamically adjusted based on the statistical written data volume and usage rate of the USB flash drive, the threshold value of each round of adjustment includes:
[0038] Step ①, counting the amount of data written and the usage rate of the USB flash drive per unit time;
[0039] Step ②, predicting the maximum writing speed S of the USB flash drive according to the amount of data written per unit time and the usage rate of the USB flash drive;
[0040] Step ③, updating the threshold value according to the maximum writing speed S of the USB flash drive, and the threshold value is positively correlated with the maximum writing speed S.
[0041] When counting the amount of data written and the usage rate of the U disk per unit time in step ① of this embodiment, counting the amount of data written per unit time of the U disk includes: firstly initializing the amount of data written N to 0, and then, for the operation req issued by the data sending function scsi_queue_rq() of the scsi layer per unit time, calling the read-write judgment function rq_data_dir() in the kernel to judge whether rq_data_dir(req) is equal to the write operation constant WRITE, if not equal to the write operation constant WRITE, then ignoring the operation req; otherwise, judging whether the storage medium written by the operation req is a U disk storage medium, if it is a U disk storage medium, adding the number of read and write bytes of the operation req to the amount of data written N through the function blk_req_bytes() to count the number of read and write operations, and finally obtaining the amount of data written N completed by the U disk per unit time after the unit time ends. By counting the amount of data N (in Kb) completed by the U disk per unit time 1s, it can be concluded that the current writing speed of the U disk is N (Kb / s).
[0042] In step ① of this embodiment, when counting the amount of written data and the usage rate of the U disk per unit time, counting the usage rate of the U disk per unit time includes: (1) initializing and setting the sampling number samping_time per unit time as the total sampling number per unit time, and initializing the value of the U disk busy frequency per_busy as the total sampling number per unit time; (2) obtaining the number of uncompleted IO write requests of the U disk through the kernel function part_in_flight_rw(), if the number of uncompleted IO write requests of the U disk is greater than 0, it means that the U disk is busy, then the value of the U disk busy frequency per_busy is kept unchanged, otherwise the value of the U disk busy frequency per_busy is reduced by 1; (3) judging whether the sampling number samping_time is greater than 0, if it is greater than 0, sleeping for a sampling interval and jumping to step (2); otherwise calculating the usage rate per unit time, the U disk busy frequency per_busy is divided by the total sampling number samping_time per unit time to obtain the usage rate Rate_Busy of the U disk per unit time.
[0043] The prediction of the maximum write speed S of the USB flash drive does not need to be particularly accurate, and the threshold value is positively correlated with the maximum write speed S and can be implemented in a variety of ways. As an optional implementation, in step ② of this embodiment, the function expression for predicting the maximum write speed S of the USB flash drive based on the amount of written data and the usage rate of the USB flash drive per unit time is: S=N / Rate_Busy, where N is the amount of written data written by the USB flash drive per unit time, and Rate_Busy is the usage rate of the USB flash drive per unit time.
[0044] In step ③ of this embodiment, the function expression for setting the threshold value according to the maximum writing speed S (in Kb / s) of the USB flash drive is:
[0045] cache_threshold =(S*T) / PAGE_SIZE
[0046] In the above formula, cache_threshold is the threshold, T is the preset expected eject time of the USB flash drive, and PAGE_SIZE is the page size (in Kb).
[0047] like Figure 1 As shown, in this embodiment, according to the current threshold, the system limits the number of cache pages that need to be written back for the cache write mechanism of the USB flash drive ( Figure 1 Step ④) is implemented based on the U disk cache limitation module. The function of the U disk cache limitation module is as follows: when the storage medium is inserted into the computer, the cache limitation module first identifies whether it is a U disk. If it is a U disk, the number of cache pages that need to be written back to the U disk is limited within a threshold. When the number of cache pages exceeds the threshold, no new cache pages are allocated. Instead, the FIFO first-in-first-out algorithm is used to write the cache pages in the U disk cache linked list back to the U disk first, and then the cache pages are recycled for use to ensure that the number of cache pages that need to be written back to the U disk is controllable. Figure 2 As shown, in this embodiment, the USB flash drive cache limiting module limits the number of cache pages that need to be written back by the system for the USB flash drive cache write mechanism according to the current threshold value, including:
[0048] S101, declare and initialize the data structure required by the USB disk cache restriction module;
[0049] S102, when the storage medium is inserted into the computer, determining whether the storage medium is a USB flash drive, and storing the determination result in a disk descriptor of the storage medium;
[0050] S103, when the system writes data to the storage medium through the VFS (Virtual File System) layer, first determine whether the storage medium is a USB flash drive. If yes, execute step S104, otherwise terminate the execution;
[0051] S104, determine whether the number of pages cached in the storage medium being written exceeds the threshold, if it exceeds the threshold, execute step S105, if it is equal to the threshold, execute step S106, otherwise, first allocate cache pages, and insert the allocated cache pages into the end of the USB disk cache linked list of the USB disk being written, and update the number of pages cached in the USB disk at the same time, and then end and exit;
[0052] S105, reclaiming cache pages of the USB flash drive cache linked list until the number of cache pages is equal to a threshold;
[0053] S106, taking a cache page from the head of the USB disk cache linked list, writing the data in the cache page back to the USB disk, and then inserting the cache page into the tail of the USB disk cache linked list.
[0054] In step S101, the data structure required for defining the USB disk cache restriction module and initializing it includes:
[0055] 1) Add a new member bool is_usb_host to the Scsi_Host structure of the kernel SCSI host adapter to indicate whether it is a scsi device virtualized from a usb device; where bool is_usb_host represents a Boolean variable is_usb_host;
[0056] 2) Add a new member int is_usb_storage to the kernel disk descriptor struct gendisk structure to indicate whether the disk is a USB disk, a new member int cache_threshold to limit the number of USB disk cache pages, a new member int page_cached to store the number of USB disk cache pages, a new member long write_num to store the amount of data written to the USB disk, a new member int exp_time to store the expected USB disk eject time, and a new member structlist_head page_list to manage all USB disk cache pages. Among them, int is_usb_storage represents the integer type variable is_usb_storage, struct list_head page_list represents the doubly linked list type variable page_list, and the rest are similar;
[0057] 3) In the function __alloc_disk_node() that allocates the disk descriptor struct gendisk structure, is_usb_storage is initialized to 0, cache_threshold is initialized to 25600 (based on the maximum speed of the USB flash drive of 50MB / s and the expected ejection time of 2 seconds. It should be noted that this value is only an initial value and will be reset later based on the dynamically measured maximum speed of the USB flash drive and the expected ejection time), page_cached is initialized to 0, write_num is initialized to 0, exp_time is initialized to 2, and INIT_LIST_HEAD(&page_list) is called to initialize page_list.
[0058] In step S102, when the storage medium is connected to the system, a proprietary driver is loaded for the U disk storage medium, and the kernel will send a SCSI command to query the characteristics of the storage medium. The characteristic data of the storage medium's response to the query command contains a characteristic of whether a device is pluggable. These two points can be used to determine whether it is a U disk. SCSI (Small Computer System Interface) is a standardized interface for connecting computers and external devices to transfer data. It allows hard disk drives, optical drives, printers and other devices to communicate with computers. In Linux systems, U disks are usually identified as SCSI devices, so U disks also use SCSI commands to interact with them. In this embodiment, 1) in the usb_stor_probe1 function of the USB storage driver, the is_usb_host field in the Scsi_Host structure of the kernel SCSI host adapter is set to 1: host->is_usb_host =1; 2) during SCSI scanning, the system will send a SCSI command to query the characteristics of the device, and the mark of whether it is a pluggable device is stored in the removable member of the scsi_device structure. The pluggable device is 1, and the non-pluggable device is 0; 3) in the function sd_probe, after the USB disk descriptor is allocated, it is determined whether the is_usb_host member of the Scsi_Host structure and the removable member of the scsi_device structure are both 1. If so, the disk descriptor is marked as a USB disk: gd->is_usb_storage =1.
[0059] In step S103, the selection to judge at the VFS layer is for universal processing. If judging at the specific file system layer, it is necessary to modify the judgment for all the file systems of the kernel. VFS, as the virtual file system layer of the Linux kernel, provides a unified file system interface, which is above all the file systems of the kernel. Therefore, judging the VFS layer can be applicable to all the file systems of the kernel. Wherein, VFS is the technology of the file system abstraction layer in the Linux operating system kernel. Its main function is to provide a unified interface for different file systems, so that the operating system can access various types of file systems in a consistent manner. The existence of VFS greatly simplifies the management and operation of the file system, so that the operating system can process various storage devices and file systems more efficiently and flexibly. In this embodiment, step S103 includes: 1) first obtaining disk descriptor structure data: struct gendisk gd = mapping->host->i_sb->s_bdev->bd_disk, where mapping is the address space struct address_space structure data of the file file, which is one of the parameters of the pagecache_get_page function; 2) obtaining a flag of whether it is a USB disk: int is_usb_storage =gd->is_usb_storage; if is_usb_storage is 1, execute step S104, otherwise terminate the operation process and exit.
[0060] In this embodiment, steps S104 to S106 are modifications to the kernel operation cache page function pagecache_get_page(). In step S104, the threshold is first initialized with an empirical value, and then reset according to the maximum write speed of the U disk predicted by the U disk maximum write speed prediction module and the expected ejection time of the U disk, that is, the step of dynamically adjusting the size of the threshold based on the statistical write data volume and usage rate of the U disk in the previous text. In this embodiment, the size of the threshold is dynamically adjusted based on the statistical write data volume and usage rate of the U disk, which is specifically implemented based on the U disk maximum write speed prediction module. The function of the U disk maximum write speed prediction module is as follows: the amount of data written to the U disk and the usage rate of the U disk in a unit time are counted, and the maximum write speed that the U disk can reach can be predicted. In this embodiment, step S104 includes: 1) comparing the number of cached pages gd->page_cached with the cache page threshold gd->cache_threshold, if gd->page_cached is greater than gd->cache_threshold, executing step S105, if gd->page_cached is equal to gd->cache_threshold, executing step S106, otherwise executing step 2); 2) calling the kernel function __page_cache_alloc to allocate a cache page: struct page*page = __page_cache_alloc(gfp_mask), where gpf_mask is one of the parameters of the pagecache_get_page function; 3) adding the allocated cache page to the disk cache page management list: list_add(&page->lru,&gd->page_list); 4) increasing the number of pages cached by the USB flash drive: gd->page_cached++.
[0061] In this embodiment, step S105 includes: 1) obtaining the page at the head of the USB disk cache list: struct page*page= list_first_entry(gd->page_list, struct page, lru); 2) writing back the cache page data to the USB disk: mapping->a_pos->writepage(page, wbc); wbc is a write-back control parameter, and here the write-back mode sync_mode is mainly set to WB_SYNC_ALL, that is, synchronous write-back; 3) deleting the cache page from the USB disk cache list: list_del(page); 4) decrementing the number of pages cached on the disk: gd->page_cached--; 5) comparing the number of cached pages gd->page_cached with the cache page threshold gd->cache_threshold, if gd->page_cached is greater than gd->cache_threshold, jump to step 1).
[0062] In this embodiment, step S106 includes: 1) obtaining the page at the head of the USB disk cache list: struct page*page= list_first_entry(gd->page_list, struct page, lru); 2) writing back the cache page data to the USB disk: mapping->a_pos->writepage(page, wbc); 3) moving the cache page to the tail of the USB disk cache list: list_move_tail(&page->lru,&gd->page_list).
[0063] Figure 3 The flowchart of the design and implementation of the USB flash drive maximum write speed prediction module in this embodiment. The specific execution steps of the USB flash drive maximum write speed prediction module in this embodiment include: S201: counting the amount of data written to the USB flash drive; S202: counting the amount of data written to the USB flash drive per unit time 1s and the usage rate; S203: predicting the maximum write speed of the USB flash drive based on the statistics of step S202 and updating the threshold.
[0064] Specifically, the statistical logic for counting the amount of data written to the USB flash drive in step S201 of this embodiment is implemented in the data sending function scsi_queue_rq() of the SCSI layer, and the steps include: 1) determining whether it is a data write operation: whether rq_data_dir(req) is equal to WRITE, if so, executing step 2), otherwise ending step S201; it should be noted that the function rq_data_dir() is implemented by the kernel to obtain whether it is a write operation or a read operation, req is the operation assigned to the disk in the function scsi_queue_rq(), and WRITE is a constant defined by the kernel to represent a write operation; 2) determining whether it is a USB flash drive: whether req->q->disk->is_usb_storage is 1, if so, executing step 3), otherwise ending step S201; 3) counting the amount of data written to the USB flash drive: req->q->disk->write_num += blk_req_bytes(req); it should be noted that the function blk_req_bytes() is implemented by the kernel to count the amount of data for read and write operations.
[0065] Figure 4 This is a flow chart of the amount of data written and the usage rate of the USB flash drive per unit time 1s in this embodiment, as shown in FIG. Figure 4As shown, step S202 of this embodiment includes: 1) obtaining the amount of data written to the current USB flash drive: unsigned long unit_write = gd->write_num; 2) setting the sampling times int samping_time to 100, and setting the variable int indicating the busy frequency of the USB flash drive per_busy is 100 (the overall design is to sample once every 10ms, 100 samples are just 1s per time, per_busy is initially 100, that is, the disk is assumed to be 100% busy, and every time the USB flash drive is sampled to be idle, per_busy is decremented by 1. After 100 samples are completed, per_busy divided by 100 can represent the usage rate of the USB flash drive in 1s per time); 3) Check the number of unfinished IO write requests of the USB flash drive. If it is greater than 0, it means that the USB flash drive is busy, then keep the USB flash drive busy frequency per_busy unchanged, otherwise reduce the USB flash drive busy frequency per_busy by 1: per_busy-- (the number of unfinished IO write requests of the USB flash drive can be obtained through the kernel function part_in_flight_rw()); 4) The number of sampling times is decremented by 1: sampling_times--; 5) Determine whether the sampling number sampling_times is greater than 0. If so, sleep for 10ms and then jump to step 3), otherwise run step 6); 6) The amount of data written per 1s per time is: unit_write = gd->write_num - unit_write; 7) The USB disk usage rate per unit time 1s is: float util = per_busy / 100.
[0066] Specifically, in step S203 of this embodiment, predicting the maximum write speed of the USB flash drive according to the statistics of step S202 and updating the threshold includes: 1) predicting the maximum write speed of the USB flash drive: int max_speed = unit_write / util; 2) updating the USB flash drive cache page threshold: gd->cache_threshold = (max_speed* gd->exp_time) / PAGE_SIZE.
[0067] In summary, the method of this embodiment implements a cache strategy for a pluggable slow storage medium, namely a USB flash drive, including detecting the insertion of a storage medium based on a USB flash drive cache limitation module. When the storage medium is inserted into a computer, the USB flash drive cache limitation module first identifies whether it is a USB flash drive. If it is a USB flash drive, the number of cache pages that need to be written back to the USB flash drive is limited within a threshold. When the number of cache pages exceeds the threshold, no new cache pages are allocated. Instead, a FIFO first-in-first-out algorithm is used to write the cache pages in the USB flash drive cache linked list back to the USB flash drive, and then the cache pages are recycled for use, so as to ensure that the number of cache pages that need to be written back to the USB flash drive is controllable. Secondly, based on the maximum write speed prediction module of the USB flash drive, the amount of data written to the USB flash drive and the usage rate of the USB flash drive per unit time are counted, and the maximum write speed that the USB flash drive can achieve can be predicted. The above-mentioned cache strategy dynamically predicts the maximum write speed of the USB flash drive according to the characteristics of the user's use of the USB flash drive, and then uses this as a basis to limit the number of system USB flash drive file cache pages. The method of this embodiment can not only guarantee the read and write operation performance of the application to a certain extent, but also ensure the controllability of the cache write-back time during the USB flash drive ejection operation, thereby solving the problems of long USB flash drive ejection time and poor user experience that may exist under the current Linux file cache management strategy, ensuring that the USB flash drive ejection operation time is fast and controllable, and improving the user's USB flash drive usage experience.
[0068] In addition, this embodiment also provides a system for improving the ejection speed of a USB flash drive under a Linux system, comprising a microprocessor and a memory connected to each other, wherein the microprocessor is programmed or configured to execute the method for improving the ejection speed of a USB flash drive under a Linux system.
[0069] In addition, this embodiment also provides a computer-readable storage medium, in which a computer program or instruction is stored. The computer program or instruction is programmed or configured to execute the method for improving the ejection speed of a USB flash drive under the Linux system through a processor.
[0070] In addition, this embodiment also provides a computer program product, including a computer program or instructions, which are programmed or configured to execute the method for improving the ejection speed of a USB flash drive under a Linux system through a processor.
[0071] Those skilled in the art should understand that the technical solutions provided by the embodiments of the present application may be in the form of methods, systems, or computer program products. Therefore, the present application may take the form of a complete hardware embodiment, a complete software embodiment, or an embodiment combining software and hardware. Moreover, the present application may take the form of a computer program product implemented on one or more computer-readable storage media (including but not limited to disk storage, CD-ROM, optical storage, etc.) containing computer-usable program codes. The present application is described with reference to the flowcharts and / or block diagrams of the methods, devices (systems), and computer program products according to the embodiments of the present application. It should be understood that each process and / or box in the flowchart and / or block diagram, as well as the combination of the processes and / or boxes in the flowchart and / or block diagram, can be implemented by computer program instructions. These computer program instructions can be provided to a processor of a general-purpose computer, a special-purpose computer, an embedded processor, or other programmable data processing device to produce a machine, so that the instructions executed by the processor of the computer or other programmable data processing device generate instructions for implementing the processes in the process Figure 1 A process or multiple processes and / or boxes Figure 1 These computer program instructions can also be stored in a computer-readable memory that can guide a computer or other programmable data processing device to work in a specific way, so that the instructions stored in the computer-readable memory produce a product including an instruction device, which implements the functions specified in the process. Figure 1 A process or multiple processes and / or boxes Figure 1 These computer program instructions can also be loaded onto a computer or other programmable data processing device, so that a series of operation steps are executed on the computer or other programmable device to produce a computer-implemented process, so that the instructions executed on the computer or other programmable device provide for implementing the process in the process. Figure 1 A process or multiple processes and / or boxes Figure 1 A step that specifies a function in one or more boxes.
[0072] The above is only a preferred embodiment of the present invention, and the protection scope of the present invention is not limited to the above embodiments. All technical solutions under the concept of the present invention belong to the protection scope of the present invention. It should be pointed out that for ordinary technicians in this technical field, some improvements and modifications without departing from the principle of the present invention should also be regarded as the protection scope of the present invention.
Claims
1. A method for improving the ejection speed of a USB flash drive in a Linux system, characterized in that: The steps include: Based on the statistical data volume and usage rate of the USB flash drive, the threshold value is dynamically adjusted regularly. According to the current threshold limiting system, the number of cache pages that need to be written back for the cache write mechanism of the U disk is limited, so that when the number of cache pages that need to be written back exceeds the threshold, no new cache pages are allocated, but the cache pages in the U disk cache linked list are first written back to the U disk and then the cache pages are recycled for use to ensure that the number of cache pages that need to be written back to the U disk is controllable to improve the ejection speed of the U disk; the number of cache pages that need to be written back for the cache write mechanism of the U disk according to the current threshold limiting system is implemented based on the U disk cache limiting module, and the U disk cache limiting module includes: S101, declare and initialize the data structure required by the USB disk cache restriction module; S102, when the storage medium is inserted into the computer, determining whether the storage medium is a USB flash drive, and storing the determination result in a disk descriptor of the storage medium; S103, when the system writes data to the storage medium through the VFS layer, first determine whether the storage medium is a USB flash drive. If so, execute step S104, otherwise terminate the operation; S104, determine whether the number of pages cached in the storage medium being written exceeds the threshold, if it exceeds the threshold, execute step S105, if it is equal to the threshold, execute step S106, otherwise, first allocate cache pages, and insert the allocated cache pages into the end of the USB disk cache linked list of the USB disk being written, and update the number of pages cached in the USB disk at the same time, and then end and exit; S105, reclaiming cache pages of the USB flash drive cache linked list until the number of cache pages is equal to a threshold; S106, taking a cache page from the head of the USB disk cache linked list, writing the data in the cache page back to the USB disk, and then inserting the cache page into the tail of the USB disk cache linked list.
2. The method for improving the ejection speed of a USB flash drive under a Linux system according to claim 1, characterized in that: When the threshold value is adjusted dynamically based on the statistical written data volume and usage rate of the USB flash drive, each round of adjusting the threshold value includes: Step ①, counting the amount of data written and the usage rate of the USB flash drive per unit time; Step ②, predicting the maximum writing speed S of the USB flash drive according to the amount of data written per unit time and the usage rate of the USB flash drive; Step ③, updating the threshold value according to the maximum writing speed S of the USB flash drive, and the threshold value is positively correlated with the maximum writing speed S.
3. The method for improving the ejection speed of a USB flash drive under a Linux system according to claim 2, characterized in that: When counting the amount of data written and the usage rate of the U disk per unit time in step ①, counting the amount of data written per unit time of the U disk includes: first, initializing the amount of data written N to 0, and then, for the operation req issued by the data sending function scsi_queue_rq() of the scsi layer per unit time, calling the read-write judgment function rq_data_dir() in the kernel to determine whether rq_data_dir(req) is equal to the write operation constant WRITE, if not equal to the write operation constant WRITE, ignoring the operation req; otherwise, determining whether the storage medium written by the operation req is the U disk storage medium, if it is the U disk storage medium, adding the number of read and write bytes of the operation req to the amount of data written N through the function blk_req_bytes() to count the number of read and write operations, and finally obtaining the amount of data written N completed by the U disk per unit time after the unit time ends.
4. The method for improving the ejection speed of a USB flash drive under a Linux system according to claim 2, characterized in that: When counting the amount of data written and the usage rate of the U disk per unit time in step ①, counting the usage rate of the U disk per unit time includes: (1) initializing the sampling number samping_time per unit time as the total sampling number per unit time, and initializing the value of the U disk busy frequency per_busy as the total sampling number per unit time; (2) obtaining the number of uncompleted IO write requests of the U disk through the kernel function part_in_flight_rw(). If the number of uncompleted IO write requests of the U disk is greater than 0, it means that the U disk is busy, then the value of the U disk busy frequency per_busy is kept unchanged, otherwise the value of the U disk busy frequency per_busy is reduced by 1; (3) firstly decrementing the sampling number samping_time by 1, and then judging whether it is greater than 0, if it is greater than 0, sleeping for one sampling interval and jumping to step (2); otherwise, calculating the U disk usage rate Rate_Busy per unit time, and dividing the U disk busy frequency per_busy by the total sampling number samping_time per unit time to obtain the U disk usage rate Rate_Busy per unit time.
5. The method for improving the ejection speed of a USB flash drive under a Linux system according to claim 2, characterized in that: In step ②, the function expression for predicting the maximum write speed S of the USB flash drive based on the amount of written data and the usage rate of the USB flash drive per unit time is: S=N / Rate_Busy, where N is the amount of written data written by the USB flash drive per unit time, and Rate_Busy is the usage rate of the USB flash drive per unit time.
6. The method for improving the ejection speed of a USB flash drive in a Linux system according to claim 2, characterized in that: In step ③, the function expression for setting the threshold according to the maximum writing speed S of the USB flash drive is: cache_threshold =(S*T) / PAGE_SIZE In the above formula, cache_threshold is the threshold, T is the preset expected ejection time of the USB flash drive, and PAGE_SIZE is the page size.
7. A system for improving the ejection speed of a USB flash drive under a Linux system, comprising a microprocessor and a memory connected to each other, characterized in that: The microprocessor is programmed or configured to execute the method for improving the ejection speed of a USB flash drive in a Linux system as described in any one of claims 1 to 6.
8. A computer-readable storage medium having a computer program or instruction stored therein, characterized in that: The computer program or instruction is programmed or configured to execute the method for improving the ejection speed of a USB flash drive in a Linux system as described in any one of claims 1 to 6 through a processor.
9. A computer program product comprising a computer program or instructions, characterized in that The computer program or instruction is programmed or configured to execute the method for improving the ejection speed of a USB flash drive in a Linux system as described in any one of claims 1 to 6 through a processor.
Citation Information
Patent Citations
Storage system using solid state disk as computer write cache and corresponding management scheduling method
CN102637147A
Device and method for diagnosing USB interface by simulating manual plugging and unplugging of USB equipment
CN111324501A