improve performance of scanner 29/274329/4 accepted/tizen/unified/20220501.223626 submit/tizen/20220428.022151
authorchengyj1985 <yujie.cheng@samsung.com>
Tue, 26 Apr 2022 09:13:44 +0000 (17:13 +0800)
committerchengyj1985 <yujie.cheng@samsung.com>
Wed, 27 Apr 2022 02:43:09 +0000 (10:43 +0800)
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
src/scanner-v2/media-scanner-scan-v2.c

index 1acac7a..acfe8fb 100755 (executable)
@@ -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);
index 2f20378..c24339f 100644 (file)
@@ -20,6 +20,7 @@
  */
 
 #include <malloc.h>
+#include <dirent.h>
 
 #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;
        }