Fix a memory bug 13/274413/6 submit/tizen/20220428.053150
authorUnsung Lee <unsung.lee@samsung.com>
Thu, 28 Apr 2022 03:36:04 +0000 (12:36 +0900)
committerUnsung Lee <unsung.lee@samsung.com>
Thu, 28 Apr 2022 05:06:21 +0000 (14:06 +0900)
pass a pids_array pointer instead of memory_limit_event
structure argument to liveness_check_cb function

Change-Id: I79f48df22fa96129f89fc76f3460ae59a9366e32
Signed-off-by: Unsung Lee <unsung.lee@samsung.com>
src/resource-limiter/memory/lowmem-handler.h
src/resource-limiter/memory/lowmem-limit.c

index c57cbec..d8bc2f1 100644 (file)
@@ -65,7 +65,6 @@ struct memory_limit_event {
        unsigned long long threshold_bytes;             /* byte */
        char *path;
        enum proc_action action;
-       GArray *pids_array;
 };
 
 /**
index f4f92a3..e36903b 100644 (file)
@@ -141,32 +141,33 @@ static int lowmem_limit_broadcast(pid_t pid)
 
 static gboolean liveness_check_cb(gpointer data)
 {
-       struct memory_limit_event *mle = (struct memory_limit_event *)data;
+       GArray *pids_array = (GArray *)data;
 
-       if (!mle) {
-               _E("[MEMORY-LIMIT] memory limit event structure is NULL");
+       if (pids_array == NULL) {
+               _E("[MEMORY-LIMIT] pids array should not be NULL");
                goto timer_out;
        }
 
-       if (mle->pids_array == NULL) {
-               _E("[MEMORY-LIMIT] pids array should not be NULL");
-               goto timer_out;
+       if (!pids_array->len) {
+               _E("[MEMORY-LIMIT] pids array size should be larger than 0");
+               goto array_free;
        }
 
-       for (int i = 0; i < mle->pids_array->len; i++) {
-               pid_t pid = g_array_index(mle->pids_array, pid_t, i);
+       for (int i = 0; i < pids_array->len; i++) {
+               pid_t pid = g_array_index(pids_array, pid_t, i);
                if (!pid)
                        continue;
 
+               _I("[MEMORY-LIMIT] Check target pid (%d)'s liveness - %d procs/%d procs", pid, i + 1, pids_array->len);
+
                if (kill(pid, 0) == 0) {
-                       _I("[MEMORY-LIMIT] dir %s's pid (%d) is still avlie, so kill forcely", mle->path, pid);
+                       _I("[MEMORY-LIMIT] pid (%d) is still alive, so kill forcibly", pid);
                        safe_kill(pid, SIGKILL);
                }
        }
 
-       g_array_free(mle->pids_array, true);
-
-       mle->pids_array = NULL;
+array_free:
+       g_array_free(pids_array, true);
 timer_out:
        return G_SOURCE_REMOVE;
 }
@@ -251,13 +252,7 @@ static bool memory_action_cb(int fd, void *data)
 
                                safe_kill(pid, SIGTERM);
                        }
-
-                       if (mle->pids_array == NULL) {
-                               mle->pids_array = pids_array;
-                               g_timeout_add_seconds(2, liveness_check_cb, mle);
-                       }
-                       else
-                               g_array_free(pids_array, true);
+                       g_timeout_add_seconds(2, liveness_check_cb, pids_array);
 
                        break;
                default:
@@ -279,7 +274,20 @@ int lowmem_reassign_limit(const char *dir,
        gpointer hash_entry;
        struct memory_limit_event *mle = NULL;
        char buf[MAX_DEC_SIZE(int)] = {0};
-       unsigned long long max_limit_bytes;
+       unsigned long long max_limit_bytes = limit_bytes;
+
+
+       if (lowmem_get_totalram() > 0) {
+               if (limit_bytes > lowmem_get_totalram()) {
+                       max_limit_bytes = lowmem_get_totalram();
+                       limit_bytes = lowmem_get_totalram();
+               }
+               else if (limit_bytes * 1.2 > lowmem_get_totalram()) {
+                       max_limit_bytes = lowmem_get_totalram();
+               }
+               else
+                       max_limit_bytes = limit_bytes * 1.2;
+       }
 
        if (memory_limit_hash) {
                /* TO DO: currently concurrent processes with same app name are located
@@ -303,15 +311,6 @@ int lowmem_reassign_limit(const char *dir,
                }
        }
 
-       if (limit_bytes > lowmem_get_totalram()) {
-               max_limit_bytes = lowmem_get_totalram();
-               limit_bytes = lowmem_get_totalram();
-       }
-       else if (limit_bytes * 1.2 > lowmem_get_totalram()) {
-               max_limit_bytes = lowmem_get_totalram();
-       }
-       else
-               max_limit_bytes = limit_bytes * 1.2;
 
        check_oom_and_set_limit(dir, max_limit_bytes);
 
@@ -339,7 +338,6 @@ int lowmem_reassign_limit(const char *dir,
                }
                mle->action = action;
                mle->threshold_bytes = limit_bytes;
-               mle->pids_array = NULL;
                add_fd_read_handler(fd, memory_action_cb, mle->path, NULL, &fdh);
                mle->fdh = fdh;
                g_hash_table_insert(memory_limit_hash, (gpointer)mle->path,