tests: refactored code
authorPrajwal A N <an.prajwal@samsung.com>
Fri, 18 Dec 2015 05:51:10 +0000 (14:51 +0900)
committerPrajwal A N <an.prajwal@samsung.com>
Thu, 24 Dec 2015 08:09:56 +0000 (17:09 +0900)
* organized code into usable modules

Change-Id: I898bb93a44eb3aa99d770b85773f824168a4e57e
Signed-off-by: Prajwal A N <an.prajwal@samsung.com>
src/tests/CMakeLists.txt
src/tests/common/fio.c [new file with mode: 0644]
src/tests/common/procfs.c [new file with mode: 0644]
src/tests/include/resourced_tests.h
src/tests/include/utils.h [new file with mode: 0644]
src/tests/memory/include/utils.h [deleted file]
src/tests/memory/resourced_memory_test.c
src/tests/memory/resourced_memory_test_helper.c
src/tests/memory/utils.c [deleted file]
src/tests/network/resourced_data_usage_test.c
src/tests/proc-stat/resourced_proc_stat_test.c

index e7875685ab8ce579925c115ec087359110faaba1..22f71392409808b82be1c659b0603da90d914251 100644 (file)
@@ -10,14 +10,18 @@ INCLUDE_DIRECTORIES(
 )
 
 #Setting sources
-SET(SRC_MEMORY_TEST ${TESTS_SOURCE_DIR}/memory/resourced_memory_test.c
+SET(SRC_TESTS_COMMON   ${TESTS_SOURCE_DIR}/common/fio.c
+                       ${TESTS_SOURCE_DIR}/common/procfs.c)
+
+                       
+SET(SRC_MEMORY_TEST    ${TESTS_SOURCE_DIR}/memory/resourced_memory_test.c
                        ${TESTS_SOURCE_DIR}/memory/resourced_memory_test_helper.c
-                       ${TESTS_SOURCE_DIR}/memory/utils.c)
+                       ${SRC_TESTS_COMMON})
 
-SET(SRC_DUMMY_PROCESS ${TESTS_SOURCE_DIR}/util/resourced_dummy_process.c)
-SET(SRC_MEMORY_HOG ${TESTS_SOURCE_DIR}/util/resourced_hogger_memory.c)
-SET(SRC_PROC_STAT_TEST ${TESTS_SOURCE_DIR}/proc-stat/resourced_proc_stat_test.c)
-SET(SRC_DATA_USAGE_TEST ${TESTS_SOURCE_DIR}/network/resourced_data_usage_test.c)
+SET(SRC_DUMMY_PROCESS  ${TESTS_SOURCE_DIR}/util/resourced_dummy_process.c)
+SET(SRC_MEMORY_HOG     ${TESTS_SOURCE_DIR}/util/resourced_hogger_memory.c)
+SET(SRC_PROC_STAT_TEST         ${TESTS_SOURCE_DIR}/proc-stat/resourced_proc_stat_test.c)
+SET(SRC_DATA_USAGE_TEST        ${TESTS_SOURCE_DIR}/network/resourced_data_usage_test.c)
 
 #Linking flags
 INCLUDE(FindPkgConfig)
diff --git a/src/tests/common/fio.c b/src/tests/common/fio.c
new file mode 100644 (file)
index 0000000..69c0f6f
--- /dev/null
@@ -0,0 +1,62 @@
+/*
+ * resourced
+ *
+ * Copyright (c) 2015 Samsung Electronics Co., Ltd. All rights reserved.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+/**
+ * @file  fio.c
+ * @desc  File I/O related functions
+ **/
+
+#include <errno.h>
+#include "resourced_tests.h"
+
+/* File IO abstract function
+ * Writes the string (in str) to the file (with path as path)
+ */
+int fwrite_str(char *path, char *str)
+{
+       int ret;
+       FILE *file;
+
+       file = fopen(path, "w");
+       if (!file) {
+               _E("IO: Error opening file %s", path);
+               return RESOURCED_ERROR_FAIL;
+       }
+
+       ret = fputs(str, file);
+       fclose(file);
+
+       if (ret < 0)
+               return RESOURCED_ERROR_FAIL;
+       else
+               return RESOURCED_ERROR_NONE;
+}
+
+/* File IO abstract function
+ * Writes the integer (in num) to the file (with path as path)
+ * Uses fwrite_str to accomplish the task
+ */
+int fwrite_int(char *path, int num)
+{
+       char content_str[STRING_MAX];
+
+       snprintf(content_str, sizeof(content_str), "%d", num);
+       return fwrite_str(path, content_str);
+}
+
diff --git a/src/tests/common/procfs.c b/src/tests/common/procfs.c
new file mode 100644 (file)
index 0000000..7d800b5
--- /dev/null
@@ -0,0 +1,168 @@
+/*
+ * resourced
+ *
+ * Copyright (c) 2015 Samsung Electronics Co., Ltd. All rights reserved.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+/**
+ * @file  procfs.c
+ * @desc  proc fs related functions
+ **/
+
+#include <errno.h>
+#include "utils.h"
+
+#define MEM_AVAILABLE "MemAvailable"
+#define MEM_TOTAL "MemTotal"
+#define MEM_FREE "MemFree"
+#define MEM_CACHED "Cached"
+
+/* Proc fs util function to get the available (usable) memory in the system
+ * Scans the /proc/meminfo file and returns the value of the field MemAvailable
+ * The value returned is in kB (1000 bytes)
+ */
+unsigned int procfs_get_available(void)
+{
+       char buf[STRING_MAX];
+       FILE *fp;
+       unsigned int available = 0;
+       unsigned int free, cached;
+
+       free = cached = 0;
+
+       fp = fopen("/proc/meminfo", "r");
+
+       if (!fp) {
+               _E("IO: Failed to open /proc/meminfo");
+               return available;
+       }
+
+       while (fgets(buf, STRING_MAX, fp) != NULL) {
+               if (!strncmp(buf, MEM_FREE, strlen(MEM_FREE))) {
+                       if (sscanf(buf, "%*s %d kB", &free) != 1) {
+                               _E("IO: Failed to get free memory from /proc/meminfo");
+                               free = 0;
+                       }
+               } else if (!strncmp(buf, MEM_CACHED, strlen(MEM_CACHED))) {
+                       if (sscanf(buf, "%*s %d kB", &cached) != 1) {
+                               _E("IO: Failed to get cached memory from /proc/meminfo");
+                               cached = 0;
+                       }
+               } else if (!strncmp(buf, MEM_AVAILABLE, strlen(MEM_AVAILABLE))) {
+                       if (sscanf(buf, "%*s %d kB", &available) != 1) {
+                               _E("IO: Failed to get available memory from /proc/meminfo");
+                               available = 0;
+                       }
+                       break;
+               }
+       }
+
+       if (!available && (free && cached))
+               available = free + cached;
+
+       fclose(fp);
+
+       return available;
+}
+
+/* Proc fs util function to get the total memory in the system
+ * Scans the /proc/meminfo file and returns the value of the field MemTotal.
+ * The value returned is in kB (1000 bytes)
+ */
+unsigned int procfs_get_total(void)
+{
+       char buf[STRING_MAX];
+       FILE *fp;
+       unsigned int total = 0;
+
+       fp = fopen("/proc/meminfo", "r");
+
+       if (!fp) {
+               _E("IO: Failed to open /proc/meminfo");
+               return total;
+       }
+
+       while (fgets(buf, STRING_MAX, fp) != NULL) {
+               if (!strncmp(buf, MEM_TOTAL, strlen(MEM_TOTAL))) {
+                       if (sscanf(buf, "%*s %d kB", &total) != 1) {
+                               _E("IO: Failed to get total memory from /proc/meminfo");
+                               total = 0;
+                       }
+                       break;
+               }
+       }
+       fclose(fp);
+
+       return total;
+}
+
+/* Proc fs util function to set oom score adj (given by oom) of
+ * the process (with id pid)
+ */
+int procfs_set_oom_score_adj(int pid, int oom)
+{
+       int ret;
+       char name[STRING_MAX];
+
+       snprintf(name, sizeof(name), "/proc/%d/oom_score_adj", pid);
+       ret = fwrite_int(name, oom);
+       if (IS_ERROR(ret))
+               _E("IO: Not able to change oom score of process %d", pid);
+       return ret;
+}
+
+/* Finds out if the process with input pid is still running.
+ * Uses the existence of the respective /proc/<pid>/ directory
+ * to find if the process is running.
+ */
+int pid_exists(int pid)
+{
+       char name[STRING_MAX];
+
+       /* If the file /proc/pid/cmdline cannot be accessed, the process does not exist */
+       snprintf(name, sizeof(name), "/proc/%d/stat", pid);
+       if (access(name, F_OK)) {
+               return 0;
+       } else {
+               /* Zombie processes which are not accounted for by the parent processes
+                * still retain their /proc/pid/ directory until the parent processes dies
+                * So we check the process status field of the stat file to see if the process
+                * is either in Z (zombie) or X/x (killed) state
+                * If the process is in either of these states, then it is not running.
+                * Otherwise the process exists
+                */
+               FILE *stat_fp;
+               int ret;
+               char proc_stat;
+
+               stat_fp = fopen(name, "r");
+               if (!stat_fp)
+                       return 0;
+               else {
+                       while (fgets(name, sizeof(name), stat_fp) != NULL) {
+                               if (sscanf(name, "%*d %*s %c", &proc_stat) != 1)
+                                       ret = 0;
+                               else if (proc_stat == 'Z' || proc_stat == 'X' || proc_stat == 'x')
+                                       ret = 0;
+                               else
+                                       ret = 1;
+                       }
+                       fclose(stat_fp);
+                       return ret;
+               }
+       }
+}
+
index cb31dd39e46ed4aa1b1edc44348a7d366ac1260d..5da3894604bffadbada6357df44ab79b418407c8 100644 (file)
 #include <sys/types.h>
 #include <systemd/sd-journal.h>
 
+#include "resourced.h"
+
 #define STRING_MAX 256
 
 #define _E(fmt, arg...) sd_journal_print(LOG_ERR, "[%s,%d] "fmt, __FUNCTION__, __LINE__, ##arg)
 #define _D(fmt, arg...) sd_journal_print(LOG_DEBUG, "[%s,%d] "fmt, __FUNCTION__, __LINE__, ##arg)
 #define _I(fmt, arg...) sd_journal_print(LOG_INFO, "[%s,%d] "fmt, __FUNCTION__, __LINE__, ##arg)
 
-/* resourced_tests package error codes */
-enum {
-       ERROR_NONE = 0,
-       ERROR_INVALID_INPUT = -1,
-       ERROR_IO = -2,
-       ERROR_MEMORY = -3,
-       ERROR_FAIL = -4,
+struct resourced_test_t{
+       char name[STRING_MAX];
+       int (*test_func)(void);
 };
 
+#define IS_ERROR(ret) (ret != RESOURCED_ERROR_NONE)
+
+/* This provides an opportunity to start a journalctl session following only the
+ * current process.
+ */
+#define TEST_START_MESSAGE(test_name) \
+{ \
+       int test_usage_ret; \
+       char test_usage_buf[STRING_MAX]; \
+       printf("Testing %s. Current pid: %d\n", test_name, getpid()); \
+       printf("Start journalctl and enter input:"); \
+       test_usage_ret = scanf("%s", test_usage_buf); \
+       test_usage_ret++; \
+}
+
 #endif
diff --git a/src/tests/include/utils.h b/src/tests/include/utils.h
new file mode 100644 (file)
index 0000000..f6ee476
--- /dev/null
@@ -0,0 +1,46 @@
+/*
+ * resourced
+ *
+ * Copyright (c) 2015 Samsung Electronics Co., Ltd. All rights reserved.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+/**
+ * @file  utils.h
+ * @desc  file IO and proc fs functions
+ **/
+
+#ifndef __RESOURCED_TESTS_UTILS_H__
+#define __RESOURCED_TESTS_UTILS_H__
+
+#include "resourced_tests.h"
+
+/* Memory size conversion macros */
+#define kBtoMB(val) (int)((val*1000) >> 20)
+#define KBtoB(val) (int)(val << 10)
+#define MBtoB(val) (int)(val << 20)
+
+/* File write abstract functions */
+int fwrite_str(char *path, char *str);
+int fwrite_int(char *path, int num);
+
+/* Proc fs util functions */
+unsigned int procfs_get_available(void);
+unsigned int procfs_get_total(void);
+int procfs_set_oom_score_adj(int pid, int oom);
+
+int pid_exists(int pid);
+
+#endif
diff --git a/src/tests/memory/include/utils.h b/src/tests/memory/include/utils.h
deleted file mode 100644 (file)
index f6ee476..0000000
+++ /dev/null
@@ -1,46 +0,0 @@
-/*
- * resourced
- *
- * Copyright (c) 2015 Samsung Electronics Co., Ltd. All rights reserved.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- *
- */
-
-/**
- * @file  utils.h
- * @desc  file IO and proc fs functions
- **/
-
-#ifndef __RESOURCED_TESTS_UTILS_H__
-#define __RESOURCED_TESTS_UTILS_H__
-
-#include "resourced_tests.h"
-
-/* Memory size conversion macros */
-#define kBtoMB(val) (int)((val*1000) >> 20)
-#define KBtoB(val) (int)(val << 10)
-#define MBtoB(val) (int)(val << 20)
-
-/* File write abstract functions */
-int fwrite_str(char *path, char *str);
-int fwrite_int(char *path, int num);
-
-/* Proc fs util functions */
-unsigned int procfs_get_available(void);
-unsigned int procfs_get_total(void);
-int procfs_set_oom_score_adj(int pid, int oom);
-
-int pid_exists(int pid);
-
-#endif
index 634b87c202ecdc4e94dc05c7b3dd7a91ff1b7b4f..81b187cc074974c245d3d73d94ae7e495537538d 100644 (file)
@@ -167,7 +167,7 @@ int proactive_oom_killer(void)
        to_populate += memory_margin[memconf][MEMORY_MARGIN_MEDIUM];
        if (to_populate < 0) {
                _E("Not able to test proactive oom killer. Not enough memory");
-               return ERROR_MEMORY;
+               return RESOURCED_ERROR_OOM;
        }
 
        /* Add a process each to the background and the swap cgroups (with sizes in ratio 0.4/0.6 of to_populate) */
@@ -177,7 +177,7 @@ int proactive_oom_killer(void)
        pid_swap = launch_memory_hogger((int)(to_populate*0.4), 350, swap_path);
        if (pid_bck < 0 || pid_swap < 0) {
                _E("error in creating processes");
-               return ERROR_MEMORY;
+               return RESOURCED_ERROR_OOM;
        }
        prev_available = available = kBtoMB(procfs_get_available());
        _D("Added %d MB to raise to dynamic threshold (%d MB). current available %d MB",
@@ -207,7 +207,7 @@ int proactive_oom_killer(void)
         * Keep track of recovered memory and # of kills for each encountered kill
         * (Note that this calculation is just approximate and not accurate which is why memory margins are used)
         */
-       ret_val = ERROR_NONE;
+       ret_val = RESOURCED_ERROR_NONE;
        recovery_target = vmpressure_limits[memconf][LIMIT_DYNAMIC_THRESH_LEAVE] - prev_available;
        recovery_target += memory_margin[memconf][MEMORY_MARGIN_LOW];
        recovered = 0;
@@ -260,7 +260,7 @@ int proactive_oom_killer(void)
                ret = check_cgroup_kill_status(TEST_PROACTIVE_KILLER, memcg_index, kill_flag,
                                                recovery_target, &recovered,
                                                num_max_victims, &num_victims);
-               if (ret != ERROR_NONE)
+               if (IS_ERROR(ret))
                        ret_val = ret;
        }
        return ret_val;
@@ -290,7 +290,7 @@ int oom_dbus_trigger(void)
         * and background cgroups are expected to be killed irrespective of recovered
         * memory. Thus recovery_target is set to the total available memory on the target.
         */
-       ret_val = ERROR_NONE;
+       ret_val = RESOURCED_ERROR_NONE;
        recovery_target = vmpressure_limits[memconf][LIMIT_TOTAL_MEMORY];
        recovered = 0;
        num_max_victims = RESOURCED_MAX_VICTIMS;
@@ -312,7 +312,7 @@ int oom_dbus_trigger(void)
                ret = check_cgroup_kill_status(TEST_OOM_DBUS_TRIGGER, memcg_index, kill_flag,
                                                recovery_target, &recovered,
                                                num_max_victims, &num_victims);
-               if (ret != ERROR_NONE)
+               if (IS_ERROR(ret))
                        ret_val = ret;
        }
        return ret_val;
@@ -337,7 +337,7 @@ int vmpressure_root(int test)
 
        /* If the test is to check the working at the first call (without callback) */
        if (test == TEST_VMPRESSURE_ROOT) {
-               ret_val = ERROR_NONE;
+               ret_val = RESOURCED_ERROR_NONE;
 
                /* Input as soon as you see oom killer thread related messages on the dlog
                 * of resourced. We wait for VMPRESSURE_ROOT_SLEEP seconds to ensure that the
@@ -381,14 +381,14 @@ int vmpressure_root(int test)
                        ret = check_cgroup_kill_status(test, memcg_index, kill_flag,
                                                recovery_target, &recovered,
                                                num_max_victims, &num_victims);
-                       if (ret != ERROR_NONE)
+                       if (IS_ERROR(ret))
                                ret_val = ret;
                }
        } else {
                int i, pid_list_num;
                int oom;
 
-               ret_val = ERROR_NONE;
+               ret_val = RESOURCED_ERROR_NONE;
 
                /* Input after you see that the oom killer thread has started to run
                 * We let the first run go through and check for reach of the target
@@ -446,7 +446,7 @@ int vmpressure_root(int test)
                                        continue;
                                else {
                                        ret = procfs_set_oom_score_adj(pid_list[memcg_index][i], oom);
-                                       if (ret == ERROR_NONE)
+                                       if (!IS_ERROR(ret))
                                                memcg_base_process_oom[test][memcg_index][i] = oom;
                                }
                        }
@@ -469,7 +469,7 @@ int vmpressure_root(int test)
                        ret = check_cgroup_kill_status(test, memcg_index, kill_flag,
                                        recovery_target, &recovered,
                                        num_max_victims, &num_victims);
-                       if (ret != ERROR_NONE)
+                       if (IS_ERROR(ret))
                                ret_val = ret;
                }
        }
@@ -524,13 +524,13 @@ int create_base_usage(int test)
                break;
        default:
                _E("Invalid input");
-               return ERROR_INVALID_INPUT;
+               return RESOURCED_ERROR_INVALID_PARAMETER;
        }
 
        if (to_populate < 0) {
                _E("%s: Base usage cannot be created. Not enough memory (%d/%d MB)",
                                test_name[test], to_populate, available);
-               return ERROR_MEMORY;
+               return RESOURCED_ERROR_OOM;
        } else
                _D("%s: Available %d MB, Base usage to populate %d MB",
                                test_name[test], available, to_populate);
@@ -547,24 +547,24 @@ int create_base_usage(int test)
        _I("%s: revised available memory is: %d MB, limit is: %d MB",
                        test_name[test], available, limit);
 
-       return ERROR_NONE;
+       return RESOURCED_ERROR_NONE;
 }
 
 /* Runs the needed sequence of tests for the input test
  * Refer to the README in the memory submodule to understand the
  * checks conducted in each test
  */
-int run_test(int test)
+void run_test(int test)
 {
-       int  ret;
+       int ret;
 
        /* Create the base usage scenario before proceeding to test for
         * correct working of memory module in resourced
         */
        ret = create_base_usage(test);
-       if (ret != ERROR_NONE) {
+       if (IS_ERROR(ret)) {
                _E("%s: Not able to create base usage. Error %d", test_name[test], ret);
-               return ret;
+               return;
        }
 
        /* After base usage scenario was created successfully, test
@@ -583,14 +583,13 @@ int run_test(int test)
                break;
        default:
                _E("Invalid input");
-               return ERROR_INVALID_INPUT;
+               return;
        }
-       if (ret != ERROR_NONE)
+       if (IS_ERROR(ret))
                _E("%s: Error running test", test_name[test]);
        else
                _I("%s: Test successfully completed", test_name[test]);
 
-       return ERROR_NONE;
 }
 
 /* Usage function */
@@ -611,14 +610,9 @@ void usage(void)
 int main(int argc, char *argv[])
 {
        int i;
-       char inp_str[STRING_MAX];
 
-       /* This is done so as to provide an opportunity to start a journalctl
-        * session following only this pid
-        */
-       printf("Running as pid %d\n", getpid());
-       printf("Enter input after starting journalctl: ");
-       i = scanf("%s", inp_str);
+
+       TEST_START_MESSAGE("memory module");
 
        /* Invalid argument */
        if (argc < 2) {
index cb5276fcfc4bf0af5e03c608e42a0e086c3be652..6772687817bcfae2c87edb166897dcd7c34e5d55 100644 (file)
@@ -69,7 +69,7 @@ int launch_memory_hogger(int memory, int oom, char *cgroup_path)
                 */
                snprintf(oom_path, sizeof(oom_path), "/proc/%d/oom_score_adj", getpid());
                ret = fwrite_int(oom_path, oom);
-               if (ret != ERROR_NONE) {
+               if (IS_ERROR(ret)) {
                        _E("IO: Error writing oom %d of pid %d", oom, pid);
                        return 0;
                }
@@ -101,7 +101,7 @@ int launch_memory_hogger(int memory, int oom, char *cgroup_path)
 
                /* Writing pid to the cgroup. Error returned if this fails. */
                ret = fwrite_int(cgroup_path, (int)pid);
-               if (ret != ERROR_NONE) {
+               if (IS_ERROR(ret)) {
                        _E("IO: Not able to write %d to %s", pid, cgroup_path);
                        return ret;
                }
@@ -174,7 +174,7 @@ int check_cgroup_kill_status(int test, int memcg_index, int kill_flag,
         * Check if they are killed/not killed and output error/debug messages
         * according to the condition (code and log msgs are self-explanatory)
         */
-       ret_val = ERROR_NONE;
+       ret_val = RESOURCED_ERROR_NONE;
        pid_list_num = memcg_base_process_num[test][memcg_index];
        for (i = pid_list_num-1; i >= 0; --i) {
                curr_pid = pid_list[memcg_index][i];
@@ -182,7 +182,7 @@ int check_cgroup_kill_status(int test, int memcg_index, int kill_flag,
                        ret = pid_exists(curr_pid);
                        oom = memcg_base_process_oom[test][memcg_index][i];
                        if (!ret) {
-                               ret = ERROR_FAIL;
+                               ret = RESOURCED_ERROR_FAIL;
                                if (!kill_flag)
                                        _E("process %d (oom: %d) should not be killed (%d / %d ; killflag)",
                                                        curr_pid, oom, *recovered, recovery_target);
@@ -198,13 +198,13 @@ int check_cgroup_kill_status(int test, int memcg_index, int kill_flag,
                                else {
                                        _D("process %d (oom: %d) killed as expected (%d / %d)",
                                                        curr_pid, oom, *recovered, recovery_target);
-                                       ret = ERROR_NONE;
+                                       ret = RESOURCED_ERROR_NONE;
                                }
 
                                *recovered = *recovered + pid_memory_list[memcg_index][i];
                                *num_victims = *num_victims + 1;
                        } else {
-                               ret = ERROR_NONE;
+                               ret = RESOURCED_ERROR_NONE;
                                if (!kill_flag)
                                        _D("process %d (oom: %d) not killed as expected (%d / %d ; killflag)",
                                                        curr_pid, oom, *recovered, recovery_target);
@@ -220,10 +220,10 @@ int check_cgroup_kill_status(int test, int memcg_index, int kill_flag,
                                else {
                                        _E("process %d (oom: %d) expected to be killed (%d / %d)",
                                                        curr_pid, oom, *recovered, recovery_target);
-                                       ret = ERROR_FAIL;
+                                       ret = RESOURCED_ERROR_FAIL;
                                }
                        }
-                       if (ret != ERROR_NONE)
+                       if (IS_ERROR(ret))
                                ret_val = ret;
                }
        }
diff --git a/src/tests/memory/utils.c b/src/tests/memory/utils.c
deleted file mode 100644 (file)
index ebf5737..0000000
+++ /dev/null
@@ -1,203 +0,0 @@
-/*
- * resourced
- *
- * Copyright (c) 2015 Samsung Electronics Co., Ltd. All rights reserved.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- *
- */
-
-/**
- * @file  utils.c
- * @desc  file IO and proc fs related functions
- **/
-
-#include <errno.h>
-#include "utils.h"
-
-#define MEM_AVAILABLE "MemAvailable"
-#define MEM_TOTAL "MemTotal"
-#define MEM_FREE "MemFree"
-#define MEM_CACHED "Cached"
-
-/* File IO abstract function
- * Writes the string (in str) to the file (with path as path)
- */
-int fwrite_str(char *path, char *str)
-{
-       int ret;
-       FILE *file;
-
-       file = fopen(path, "w");
-       if (!file) {
-               _E("IO: Error opening file %s", path);
-               return ERROR_IO;
-       }
-
-       ret = fputs(str, file);
-       fclose(file);
-
-       if (ret < 0)
-               return ERROR_IO;
-       else
-               return ERROR_NONE;
-}
-
-/* File IO abstract function
- * Writes the integer (in num) to the file (with path as path)
- * Uses fwrite_str to accomplish the task
- */
-int fwrite_int(char *path, int num)
-{
-       char content_str[STRING_MAX];
-
-       snprintf(content_str, sizeof(content_str), "%d", num);
-       return fwrite_str(path, content_str);
-}
-
-/* Proc fs util function to get the available (usable) memory in the system
- * Scans the /proc/meminfo file and returns the value of the field MemAvailable
- * The value returned is in kB (1000 bytes)
- */
-unsigned int procfs_get_available(void)
-{
-       char buf[STRING_MAX];
-       FILE *fp;
-       unsigned int available = 0;
-       unsigned int free, cached;
-
-       free = cached = 0;
-
-       fp = fopen("/proc/meminfo", "r");
-
-       if (!fp) {
-               _E("IO: Failed to open /proc/meminfo");
-               return available;
-       }
-
-       while (fgets(buf, STRING_MAX, fp) != NULL) {
-               if (!strncmp(buf, MEM_FREE, strlen(MEM_FREE))) {
-                       if (sscanf(buf, "%*s %d kB", &free) != 1) {
-                               _E("IO: Failed to get free memory from /proc/meminfo");
-                               free = 0;
-                       }
-               } else if (!strncmp(buf, MEM_CACHED, strlen(MEM_CACHED))) {
-                       if (sscanf(buf, "%*s %d kB", &cached) != 1) {
-                               _E("IO: Failed to get cached memory from /proc/meminfo");
-                               cached = 0;
-                       }
-               } else if (!strncmp(buf, MEM_AVAILABLE, strlen(MEM_AVAILABLE))) {
-                       if (sscanf(buf, "%*s %d kB", &available) != 1) {
-                               _E("IO: Failed to get available memory from /proc/meminfo");
-                               available = 0;
-                       }
-                       break;
-               }
-       }
-
-       if (!available && (free && cached))
-               available = free + cached;
-
-       fclose(fp);
-
-       return available;
-}
-
-/* Proc fs util function to get the total memory in the system
- * Scans the /proc/meminfo file and returns the value of the field MemTotal.
- * The value returned is in kB (1000 bytes)
- */
-unsigned int procfs_get_total(void)
-{
-       char buf[STRING_MAX];
-       FILE *fp;
-       unsigned int total = 0;
-
-       fp = fopen("/proc/meminfo", "r");
-
-       if (!fp) {
-               _E("IO: Failed to open /proc/meminfo");
-               return total;
-       }
-
-       while (fgets(buf, STRING_MAX, fp) != NULL) {
-               if (!strncmp(buf, MEM_TOTAL, strlen(MEM_TOTAL))) {
-                       if (sscanf(buf, "%*s %d kB", &total) != 1) {
-                               _E("IO: Failed to get total memory from /proc/meminfo");
-                               total = 0;
-                       }
-                       break;
-               }
-       }
-       fclose(fp);
-
-       return total;
-}
-
-/* Proc fs util function to set oom score adj (given by oom) of
- * the process (with id pid)
- */
-int procfs_set_oom_score_adj(int pid, int oom)
-{
-       int ret;
-       char name[STRING_MAX];
-
-       snprintf(name, sizeof(name), "/proc/%d/oom_score_adj", pid);
-       ret = fwrite_int(name, oom);
-       if (ret != ERROR_NONE)
-               _E("IO: Not able to change oom score of process %d", pid);
-       return ret;
-}
-
-/* Finds out if the process with input pid is still running.
- * Uses the existence of the respective /proc/<pid>/ directory
- * to find if the process is running.
- */
-int pid_exists(int pid)
-{
-       char name[STRING_MAX];
-
-       /* If the file /proc/pid/cmdline cannot be accessed, the process does not exist */
-       snprintf(name, sizeof(name), "/proc/%d/stat", pid);
-       if (access(name, F_OK)) {
-               return 0;
-       } else {
-               /* Zombie processes which are not accounted for by the parent processes
-                * still retain their /proc/pid/ directory until the parent processes dies
-                * So we check the process status field of the stat file to see if the process
-                * is either in Z (zombie) or X/x (killed) state
-                * If the process is in either of these states, then it is not running.
-                * Otherwise the process exists
-                */
-               FILE *stat_fp;
-               int ret;
-               char proc_stat;
-
-               stat_fp = fopen(name, "r");
-               if (!stat_fp)
-                       return 0;
-               else {
-                       while (fgets(name, sizeof(name), stat_fp) != NULL) {
-                               if (sscanf(name, "%*d %*s %c", &proc_stat) != 1)
-                                       ret = 0;
-                               else if (proc_stat == 'Z' || proc_stat == 'X' || proc_stat == 'x')
-                                       ret = 0;
-                               else
-                                       ret = 1;
-                       }
-                       fclose(stat_fp);
-                       return ret;
-               }
-       }
-}
-
index a9f36f6a80eee090a00702717d74f813552a0284..cab7613a11fded38267ce115926ee07d0182766c 100644 (file)
 
 #include "resourced_tests.h"
 
-struct data_usage_test_t {
-       char name[STRING_MAX];
-       int (*test_func)(void);
-};
-
 int data_usage_test_set_resourced_options()
 {
        return set_resourced_options(NULL);
@@ -135,7 +130,7 @@ int data_usage_test_resourced_remove_restriction_by_iftype()
        return resourced_remove_restriction_by_iftype(NULL, 0, NULL);
 }
 
-static struct data_usage_test_t data_usage_tests[] = {
+static struct resourced_test_t data_usage_tests[] = {
        { "set_resourced_options", data_usage_test_set_resourced_options },
        { "get_resourced_options", data_usage_test_get_resourced_options },
        { "set_net_restriction", data_usage_test_set_net_restriction },
@@ -163,22 +158,17 @@ static struct data_usage_test_t data_usage_tests[] = {
 int main(int argc, char *argv[])
 {
        int i, ret;
-       char buf[STRING_MAX];
 
-       printf("Testing data-usage library. Current pid: %d\n", getpid());
-       printf("Start journalctl and enter input:");
-       ret = scanf("%s\n", buf);
+       TEST_START_MESSAGE("data-usage library");
 
-       i = 0;
-       while(data_usage_tests[i].test_func) {
+       for(i = 0; data_usage_tests[i].test_func; ++i) {
                _D("=======================================");
                _D("Current Test: %s", data_usage_tests[i].name);
                ret = (*data_usage_tests[i].test_func)();
-               if (ret)
+               if (IS_ERROR(ret))
                        _E("Test %s failed!", data_usage_tests[i].name);
                else
                        _D("Test %s passed!", data_usage_tests[i].name);
-               i++;
        }
        return 0;
 }
index 87320da6e9396424b2af736e01adede4f41eb216..1946a39e591f85805563bd3503ce00110b90e08c 100644 (file)
 
 #define PSTAT_GET_PID_ENTRY_MAX_TESTS 5
 
-enum {
-       PSTAT_TESTS_GET_PID_ENTRY,
-       PSTAT_TESTS_MAX,
-};
-
-struct pstat_test_t {
-       int test_num;
-       char name[STRING_MAX];
-       int (*test_func)(void);
-};
-
 int pstat_get_pid_entry(void)
 {
        char buf[4*STRING_MAX];
@@ -59,38 +48,35 @@ int pstat_get_pid_entry(void)
        };
 
        pid = getpid();
-       final_ret = ERROR_NONE;
+       final_ret = RESOURCED_ERROR_NONE;
        for (i = 0; i < PSTAT_GET_PID_ENTRY_MAX_TESTS; ++i) {
                ret = proc_stat_get_pid_entry(test_inp[i], pid, buf, sizeof(buf)-1);
                buf[sizeof(buf)-1] = 0;
                if (ret != RESOURCED_ERROR_NONE) {
                        _E("Test %s: failed with %d error", tests[i], ret);
-                       final_ret = ERROR_FAIL;
+                       final_ret = RESOURCED_ERROR_FAIL;
                } else
                        _D("Test %s: Passed. buf: %s", tests[i], buf);
        }
        return final_ret;
 }
 
-static struct pstat_test_t pstat_tests[] = {
-       { PSTAT_TESTS_GET_PID_ENTRY, "pstat_get_pid_entry", pstat_get_pid_entry },
-       { PSTAT_TESTS_MAX, "", NULL },
+static struct resourced_test_t pstat_tests[] = {
+       { "pstat_get_pid_entry", pstat_get_pid_entry },
+       { "", NULL },
 };
 
 int main(int argc, char *argv[])
 {
        int i, ret;
-       char buf[STRING_MAX];
 
-       printf("Testing proc-stat module. Current pid: %d\n", getpid());
-       printf("Start journalctl and enter input:");
-       ret = scanf("%s\n", buf);
+       TEST_START_MESSAGE("proc-stat module");
 
-       for (i = 0; i < PSTAT_TESTS_MAX; ++i) {
+       for (i = 0; pstat_tests[i].test_func; ++i) {
                _D("=======================================");
                _D("Current Test: %s", pstat_tests[i].name);
                ret = (*pstat_tests[i].test_func)();
-               if (ret)
+               if (IS_ERROR(ret))
                        _E("Test %s failed!", pstat_tests[i].name);
                else
                        _D("Test %s passed!", pstat_tests[i].name);