From d38c0ce956461c6e0db0c71b4cee41ff66b9ae0c Mon Sep 17 00:00:00 2001 From: chengyj1985 Date: Tue, 26 Apr 2022 17:13:44 +0800 Subject: [PATCH] improve performance of scanner 1.remove g_file_test, it use too much time. Use readdir64 to get name and type. 2.reduce frequency of sleep. Change-Id: I2bdd3529e1f465a7b9c099403f32b42ac2fc1977 --- src/scanner-v2/include/media-scanner-common-v2.h | 1 + src/scanner-v2/media-scanner-scan-v2.c | 59 +++++++++++------------- 2 files changed, 28 insertions(+), 32 deletions(-) diff --git a/src/scanner-v2/include/media-scanner-common-v2.h b/src/scanner-v2/include/media-scanner-common-v2.h index 1acac7a..acfe8fb 100755 --- a/src/scanner-v2/include/media-scanner-common-v2.h +++ b/src/scanner-v2/include/media-scanner-common-v2.h @@ -25,6 +25,7 @@ #include "stdbool.h" #define SCAN_SLEEP_TIME 10000 +#define SCAN_SLEEP_FILECOUNT 10 bool _msc_is_power_off(void); void _msc_set_power_status(bool status); diff --git a/src/scanner-v2/media-scanner-scan-v2.c b/src/scanner-v2/media-scanner-scan-v2.c index 2f20378..c24339f 100644 --- a/src/scanner-v2/media-scanner-scan-v2.c +++ b/src/scanner-v2/media-scanner-scan-v2.c @@ -20,6 +20,7 @@ */ #include +#include #include "media-util.h" #include "media-server-ipc.h" @@ -193,9 +194,8 @@ static int __msc_dir_scan_for_folder(sqlite3 *handle, const char *storage_id, ch int scan_count = 0; int sleep_count = 0; - GDir *dir = NULL; - GError *error = NULL; - const char *name; + DIR *dir = NULL; + struct dirent64 *dir_entry = NULL; MS_DBG_SWARN("storage id [%s] start path [%s]", storage_id, start_path); @@ -255,18 +255,16 @@ static int __msc_dir_scan_for_folder(sqlite3 *handle, const char *storage_id, ch ms_set_folder_scan_status(handle, storage_id, current_path, MS_DIR_SCAN_PROCESSING, uid); sleep_count = 0; - dir = g_dir_open(current_path, 0, &error); - if (error != NULL) { + dir = opendir(current_path); + if (dir == NULL) { MS_DBG_ERR("g_dir_open fails [%s]", current_path); MS_SAFE_FREE(current_path); - g_error_free(error); - error = NULL; continue; } - while ((name = g_dir_read_name(dir))) { + while ((dir_entry = readdir64(dir))) { sleep_count++; - if (sleep_count % 5 == 0) { + if (sleep_count % SCAN_SLEEP_FILECOUNT == 0) { sleep_count = 0; usleep(SCAN_SLEEP_TIME); } @@ -290,18 +288,18 @@ static int __msc_dir_scan_for_folder(sqlite3 *handle, const char *storage_id, ch goto STOP_SCAN; } - if (name[0] == '.') + if (dir_entry->d_name[0] == '.') continue; - if (strcmp(name, RECYCLE_DIR_NAME) == 0) + if (strcmp(dir_entry->d_name, RECYCLE_DIR_NAME) == 0) continue; - if (g_snprintf(path, MS_FILE_PATH_LEN_MAX, "%s/%s", current_path, name) >= MS_FILE_PATH_LEN_MAX) { + if (g_snprintf(path, MS_FILE_PATH_LEN_MAX, "%s/%s", current_path, dir_entry->d_name) >= MS_FILE_PATH_LEN_MAX) { MS_DBG_ERR("g_snprintf failed"); continue; } - if (g_file_test(path, G_FILE_TEST_IS_REGULAR)) { + if (dir_entry->d_type == DT_REG) { if (!ms_check_support_media_type(path)) continue; @@ -325,7 +323,7 @@ static int __msc_dir_scan_for_folder(sqlite3 *handle, const char *storage_id, ch } } } - } else { + } else if (dir_entry->d_type == DT_DIR) { if (msg_type == MS_MSG_DIRECTORY_SCANNING) { new_path = g_strdup(path); g_ptr_array_add(dir_array, new_path); @@ -349,7 +347,7 @@ static int __msc_dir_scan_for_folder(sqlite3 *handle, const char *storage_id, ch MS_SAFE_FREE(current_path); if (dir) { - g_dir_close(dir); + closedir(dir); dir = NULL; } @@ -366,7 +364,7 @@ STOP_SCAN: END_SCAN: if (dir) { - g_dir_close(dir); + closedir(dir); dir = NULL; } @@ -396,9 +394,8 @@ static int __msc_dir_scan_for_storage(sqlite3 *handle, const char *storage_id, c int scan_count = 0; int sleep_count = 0; - GDir *dir = NULL; - GError *error = NULL; - const char *name; + DIR *dir = NULL; + struct dirent64 *dir_entry = NULL; bool is_missing = false; ms_dir_scan_status_e scan_status = MS_DIR_SCAN_NONE; @@ -471,18 +468,16 @@ static int __msc_dir_scan_for_storage(sqlite3 *handle, const char *storage_id, c ms_set_folder_scan_status(handle, storage_id, current_path, MS_DIR_SCAN_PROCESSING, uid); sleep_count = 0; - dir = g_dir_open(current_path, 0, &error); - if (error != NULL) { + dir = opendir(current_path); + if (dir == NULL) { MS_DBG_ERR("g_dir_open fails [%s]", current_path); MS_SAFE_FREE(current_path); - g_error_free(error); - error = NULL; continue; } - while ((name = g_dir_read_name(dir))) { + while ((dir_entry = readdir64(dir))) { sleep_count++; - if (sleep_count % 5 == 0) { + if (sleep_count % SCAN_SLEEP_FILECOUNT == 0) { sleep_count = 0; usleep(SCAN_SLEEP_TIME); } @@ -495,18 +490,18 @@ static int __msc_dir_scan_for_storage(sqlite3 *handle, const char *storage_id, c if (ret != MS_MEDIA_ERR_NONE) goto STOP_SCAN; - if (name[0] == '.') + if (dir_entry->d_name[0] == '.') continue; - if (strcmp(name, RECYCLE_DIR_NAME) == 0) + if (strcmp(dir_entry->d_name, RECYCLE_DIR_NAME) == 0) continue; - if (g_snprintf(path, MS_FILE_PATH_LEN_MAX, "%s/%s", current_path, name) >= MS_FILE_PATH_LEN_MAX) { + if (g_snprintf(path, MS_FILE_PATH_LEN_MAX, "%s/%s", current_path, dir_entry->d_name) >= MS_FILE_PATH_LEN_MAX) { MS_DBG_ERR("g_snprintf failed"); continue; } - if (g_file_test(path, G_FILE_TEST_IS_REGULAR)) { + if (dir_entry->d_type == DT_REG) { if (!ms_check_support_media_type(path)) continue; @@ -521,7 +516,7 @@ static int __msc_dir_scan_for_storage(sqlite3 *handle, const char *storage_id, c msc_insert_exactor_request(msg_type, false, storage_id, current_path, 0, uid, MS_ITEM_UPDATE); } } - } else { + } else if (dir_entry->d_type == DT_DIR) { new_path = g_strdup(path); g_ptr_array_add(dir_array, new_path); @@ -534,7 +529,7 @@ static int __msc_dir_scan_for_storage(sqlite3 *handle, const char *storage_id, c ms_set_folder_scan_status(handle, storage_id, current_path, MS_DIR_SCAN_DONE, uid); if (dir) { - g_dir_close(dir); + closedir(dir); dir = NULL; } @@ -589,7 +584,7 @@ STOP_SCAN: END_SCAN: if (dir) { - g_dir_close(dir); + closedir(dir); dir = NULL; } -- 2.7.4