fopen execution time become longer after calling frequently

WeinanCai
Posts: 4
Joined: Thu Dec 26, 2019 2:37 pm

fopen execution time become longer after calling frequently

Postby WeinanCai » Fri Dec 27, 2019 9:45 am

Environment
• Chip used: ESP32D0WDQ6
• Peripheral used: Kingston SD card(16G)
• IDF version: v3.3
• Power Supply: USB
Problem Description
I'm suffering fopen execution time becoming longer with its called times increasing. After calling fopen for many times (around 2000 or more), the fopen execution time increase to 110ms from 5ms in the beginning. However, there is no exception and it completely works as I expected. I can't find any other similar case like this and it is really hard to figure it out because it can work well but the execution time becomes longer and longer.
And if I already put some files in the directory (around 2000 files), the fopen execution time is 116ms in the beginning which is unacceptable.
I have already tested the SD card in my computer and called fopen more than 10000 times to create files in the SD card and the execution is always 5ms. So it seems not the SD card reason.
If there's a solution it would be great and I'd like to know what is the reason and how to debug like this.

Code to reproduce this issue

Code: Select all

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <unistd.h>//usleep使用
#include <sys/unistd.h>
#include <sys/stat.h>
#include <time.h>
#include <sys/time.h>
#include "esp_vfs_fat.h"
#include "driver/sdmmc_host.h"
#include "driver/sdspi_host.h"
#include "sdmmc_cmd.h"
#include <dirent.h> 
#include <sys/types.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_system.h"
#include "driver/spi_master.h"
#include "driver/gpio.h"
#include <esp_log.h>
#include "esp_err.h"
#include <esp_heap_caps.h>

static char tag[] = "sdcard";   //声明静态字符串
void sdcard_init(void){
    sdmmc_host_t host = SDSPI_HOST_DEFAULT();
    sdspi_slot_config_t slot_config = SDSPI_SLOT_CONFIG_DEFAULT();

    esp_vfs_fat_sdmmc_mount_config_t mount_config = {
        .format_if_mount_failed = true,
        .max_files = 5,
        .allocation_unit_size = 16 * 1024
    };
    sdmmc_card_t* card;
    esp_err_t ret = esp_vfs_fat_sdmmc_mount("/sdcard", &host, &slot_config, &mount_config, &card);

    if (ret != ESP_OK) {
        if (ret == ESP_FAIL) {
            ESP_LOGE(tag, "Failed to mount filesystem. "
                "If you want the card to be formatted, set format_if_mount_failed = true.");
        } else {
            ESP_LOGE(tag, "Failed to initialize the card (%s). "
                "Make sure SD card lines have pull-up resistors in place.", esp_err_to_name(ret));
        }
        return;
    }
}
void app_main(void)
{
    sdcard_init();
    DIR *dr = opendir("/sdcard/data");  
    if (dr == NULL)  
    { 
        ESP_LOGD(tag, "no data dir, trying to create");
        mkdir("/sdcard/data", 0777);
    } 
    char filename[80];
    int filenum = 0;
    char str[] = "This is runoob.com";
    while(1){
                sprintf(filename, "/sdcard/data/file-%d.bin", filenum);
                filenum++;
                FILE *fp = NULL;
                            TickType_t sticks = xTaskGetTickCount();
                fp = fopen(filename, "wb");
                            TickType_t eticks = xTaskGetTickCount();
                            int tdiff = eticks-sticks;
                            ESP_LOGI(tag,"fopen %d\n", tdiff);

                ESP_LOGI(tag,"open new file %s\n", filename);
                fwrite(str, sizeof(str) , 1, fp );
                fclose(fp);
                usleep(1000);
    }
    esp_vfs_fat_sdmmc_unmount(); 
}

ESP_Sprite
Posts: 9770
Joined: Thu Nov 26, 2015 4:08 am

Re: fopen execution time become longer after calling frequently

Postby ESP_Sprite » Sun Dec 29, 2019 2:27 am

No idea what the issue is, but:
I have already tested the SD card in my computer and called fopen more than 10000 times to create files in the SD card and the execution is always 5ms. So it seems not the SD card reason.
I'm not sure if you can draw that conclusion. Your PC has huge caches in which it stores the state of the filesystem and will write them back asynchroneously; at the moment your fopen() call is finished, the newly-created file probably hasn't even hit the SD-card yet.

WeinanCai
Posts: 4
Joined: Thu Dec 26, 2019 2:37 pm

Re: fopen execution time become longer after calling frequently

Postby WeinanCai » Sun Dec 29, 2019 11:58 am

Thanks, Mr Sprite.
https://github.com/espressif/esp-idf/issues/4547
Above url explain my confusion.

Who is online

Users browsing this forum: Baidu [Spider], Majestic-12 [Bot], QQ26750452, theodote and 50 guests