My work environment is: Linux 3.2.0,ARMV7L,GNU/Linux; CPU is AM335x.
Background:
1.The original file system in flash was UBIFS, but due to some reasons that I have not yet found, it was prone to corruption, so I replaced the file system format with YAFFS2.
2.In a certain folder, a file is generated every second. I need to periodically check the remaining space in flash to ensure that the space occupied by this folder does not exceed 50%. When it exceeds 50%, I need to delete the earliest file in the folder until the remaining space is greater than 50%.
3.I wrote a shell script to implement the above function, which uses ls -rt**
#!/bin/sh
folder_path="/path/to/folder"
min_space_percent=50
while true
do
free_space=$(df -k | grep "/dev/mtdblock" | awk '{print $4}')
oldest_file=$(ls -rt $folder_path | head -n 1)
oldest_file_path="$folder_path/$oldest_file"
if [ $free_space -gt $min_space_percent ]
then
rm $oldest_file_path
else
break
fi
done
When I execute this script, I encounter an issue where RS-485 cannot communicate properly.
After investigation, I found that when there are a large number of files in the folder, more than a thousand, executing ls -rt
will cause this issue.
But the key point is that this issue occurs on the YAFFS2 file system, while it does not occur on the UBIFS file system.
My solution:
1.I asked ChatGPT and it said: If the performance of the file system is poor, processing a large number of files may cause the system to respond slowly, the CPU usage rate to be high, and thus cause 485 communication anomalies.
2.So I rewrote a program to find the earliest created file in the folder. After each search, there is a 2ms delay to reduce resource usage. Test results: When the number of files is around 5000, there is no problem as before. However, when the number of files is larger, it still affects 485 communication.
c
int main(int argc, char *argv[]) {
DIR *dir;
struct dirent *entry;
struct stat file_stat;
time_t earliest_time = time(NULL);
char *earliest_file = NULL;
if (argc != 2) {
printf("Usage: %s <directory>\n", argv[0]);
exit(EXIT_FAILURE);
}
dir = opendir(argv[1]);
if (dir == NULL) {
perror("opendir");
exit(EXIT_FAILURE);
}
while ((entry = readdir(dir)) != NULL) {
char *filename = entry->d_name;
char filepath[1024];
sprintf(filepath, "%s/%s", argv[1], filename);
if (stat(filepath, &file_stat) == -1) {
perror("stat");
continue;
}
if (file_stat.st_mtime < earliest_time) {
earliest_time = file_stat.st_mtime;
earliest_file = filename;
}
usleep(2000);
}
printf("Earliest file: %s\n", earliest_file);
closedir(dir);
exit(EXIT_SUCCESS);
}
My doubts:
What are the specific reasons for the differences in the ls command between the YAFFS2 and UBIFS file systems?
How to better solve this problem?