cleanup: apply 10mins interval 61/231661/9
authorYunmi Ha <yunmi.ha@samsung.com>
Thu, 23 Apr 2020 09:10:11 +0000 (18:10 +0900)
committerYunmi Ha <yunmi.ha@samsung.com>
Wed, 29 Apr 2020 05:09:23 +0000 (14:09 +0900)
- When requesting the same or lower level of cleanup again
(warning->warning, full->critical or critical->warning),
do it at a specific interval (10 minutes).

- When requesting the higher level of cleanup again
(warning->critical or critical->full),
do it immediately.

Change-Id: I87adc82a6c761394ff1d876b2729202204441279
Signed-off-by: Yunmi Ha <yunmi.ha@samsung.com>
src/storage/cleanup.c

index 5206aa8..f27f4b1 100644 (file)
@@ -24,6 +24,7 @@
 #include <gmodule.h>
 #include <limits.h>
 #include <regex.h>
+#include <time.h>
 
 #include "log.h"
 #include "cleanup.h"
@@ -40,6 +41,12 @@ struct cleanup_request {
        int level;
 };
 
+struct cleanup_history {
+       enum cleanup_running_type type;
+       int level;
+       time_t tm;
+};
+
 #define REMOVE(path) { \
        if(remove(path) != 0) {         \
                _E("Failed to remove(%s): %d", path, errno);    \
@@ -55,11 +62,14 @@ struct cleanup_request {
        }       \
 }
 
+#define CLEANUP_INTERVAL_SEC   (10 * 60)
+
 static pthread_mutex_t mutex_cancel;
 static pthread_mutex_t mutex_lock;
 static pthread_t cleanup_th = 0;
 static GList *request_queue = NULL;
 static int cleanup_canceled = 0;
+static GSList *history = NULL;
 
 static int is_cleanup_canceled()
 {
@@ -186,6 +196,70 @@ static int cleanup_recursive(const char *path, GList *except, int target)
        return ret;
 }
 
+static bool check_history(enum cleanup_running_type type, int level)
+{
+       time_t interval;
+       GSList *list = NULL;
+       struct cleanup_history *item = NULL;
+       bool ret = true;
+
+       /*
+        * When requesting the same or lower level of cleanup again
+          (warning->warning, full->critical or critical->warning),
+          do it at a specific interval (10 minutes).
+
+        * When requesting the higher level of cleanup again
+          (warning->critical or critical->full),
+          do it immediately.
+       */
+       for (list = history; list != NULL; list = g_slist_next(list)) {
+               item = list->data;
+               if (item->type == type) {
+                       if (item->level > level)
+                               break;
+
+                       interval = time(NULL) - item->tm;
+                       if (interval < CLEANUP_INTERVAL_SEC) {
+                               //_D("Cleanup(type:%d, level:%d) is already requested before %u seconds.", type, level, (unsigned int)interval);
+                               ret = false;
+                       }
+
+                       break;
+               }
+       }
+
+       return ret;
+}
+
+static void update_history(enum cleanup_running_type type, int level)
+{
+       GSList *list = NULL;
+       struct cleanup_history *item = NULL, *find = NULL;
+
+       for (list = history; list != NULL; list = g_slist_next(list)) {
+               item = list->data;
+               if (item->type == type) {
+                       find = item;
+                       break;
+               }
+       }
+
+       if (find == NULL) {
+               find = malloc(sizeof(struct cleanup_history));
+               if (!find) {
+                       _E("Failed to call malloc function.");
+                       return ;
+               }
+               find->type = type;
+               find->level = level;
+               find->tm = time(NULL);
+               history = g_slist_prepend(history, find);
+       } else {
+               find->level = level;
+               find->tm = time(NULL);
+       }
+}
+
 static int add_request_queue(int type, int level)
 {
        int ret = 0;
@@ -283,7 +357,7 @@ void *cleanup_storage_start(void *arg)
 void cleanup_storage(enum tzplatform_variable path_id, int level)
 {
        bool bth = (get_request_queue() == NULL);
-       int type = CLEANUP_TYPE_NONE;
+       enum cleanup_running_type type = CLEANUP_TYPE_NONE;
        int ret;
 
        if (path_id == TZ_SYS_OPT)
@@ -300,12 +374,17 @@ void cleanup_storage(enum tzplatform_variable path_id, int level)
                return ;
        }
 
+       if (!check_history(type, level))
+               return ;
+
        if (add_request_queue(type, level) != 0) {
                _E("Failed to add request.");
                return ;
        }
        //_D("Add cleanup request.(type:%d, level:%d)", type, level);
 
+       update_history(type, level);
+
        if (bth) {
                ret = pthread_create(&cleanup_th, NULL, cleanup_storage_start, NULL);
                if (ret != 0)
@@ -329,6 +408,10 @@ void free_cleanup_storage()
        request_queue = NULL;
        pthread_mutex_unlock(&mutex_lock);
 
+       if (history)
+               g_slist_free_full(history, free);
+       history = NULL;
+
        if (cleanup_th) {
                _D("Cancel cleanup thread %d.", (int)cleanup_th);
                cleanup_cancel();