tests: added tests for CPU module
authorPrajwal A N <an.prajwal@samsung.com>
Fri, 18 Dec 2015 05:56:02 +0000 (14:56 +0900)
committerPrajwal A N <an.prajwal@samsung.com>
Thu, 24 Dec 2015 08:09:56 +0000 (17:09 +0900)
* tests cgroup creation
* tests different app actions

Change-Id: I1843cde8dff486ebc0127d12b320426cb669527b
Signed-off-by: Prajwal A N <an.prajwal@samsung.com>
packaging/resourced.spec
src/tests/CMakeLists.txt
src/tests/common/cgroup.c [new file with mode: 0644]
src/tests/cpu/resourced_cpu_test.c [new file with mode: 0644]
src/tests/include/utils.h

index 92be727484687ce69af96d8ce2f0912d01fc6338..4884ea323815230e3ff9d74f6b0904d87c2ef01d 100644 (file)
@@ -274,6 +274,8 @@ fi
 %defattr(-,root,root,-)
 %{_bindir}/resourced_memory_test
 %defattr(-,root,root,-)
+%{_bindir}/resourced_cpu_test
+%defattr(-,root,root,-)
 %{_bindir}/resourced_proc_stat_test
 %defattr(-,root,root,-)
 %{_bindir}/resourced_data_usage_test
index 22f71392409808b82be1c659b0603da90d914251..6d36c59b1e98c9dc6d158f807a64e83965f05ed6 100644 (file)
@@ -11,13 +11,16 @@ INCLUDE_DIRECTORIES(
 
 #Setting sources
 SET(SRC_TESTS_COMMON   ${TESTS_SOURCE_DIR}/common/fio.c
-                       ${TESTS_SOURCE_DIR}/common/procfs.c)
+                       ${TESTS_SOURCE_DIR}/common/procfs.c
+                       ${TESTS_SOURCE_DIR}/common/cgroup.c)
 
                        
 SET(SRC_MEMORY_TEST    ${TESTS_SOURCE_DIR}/memory/resourced_memory_test.c
                        ${TESTS_SOURCE_DIR}/memory/resourced_memory_test_helper.c
                        ${SRC_TESTS_COMMON})
 
+SET(SRC_CPU_TEST       ${TESTS_SOURCE_DIR}/cpu/resourced_cpu_test.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)
@@ -36,6 +39,10 @@ ADD_EXECUTABLE (resourced_memory_test ${SRC_MEMORY_TEST})
 TARGET_LINK_LIBRARIES(resourced_memory_test ${daemon_pkgs_LDFLAGS})
 INSTALL(TARGETS resourced_memory_test DESTINATION bin)
 
+ADD_EXECUTABLE (resourced_cpu_test ${SRC_CPU_TEST})
+TARGET_LINK_LIBRARIES(resourced_cpu_test ${daemon_pkgs_LDFLAGS})
+INSTALL(TARGETS resourced_cpu_test DESTINATION bin)
+
 ADD_EXECUTABLE(resourced_proc_stat_test ${SRC_PROC_STAT_TEST})
 TARGET_LINK_LIBRARIES(resourced_proc_stat_test ${daemon_pkgs_LDFLAGS} ${PROC-STAT})
 INSTALL(TARGETS resourced_proc_stat_test DESTINATION bin)
diff --git a/src/tests/common/cgroup.c b/src/tests/common/cgroup.c
new file mode 100644 (file)
index 0000000..fc6e78e
--- /dev/null
@@ -0,0 +1,56 @@
+/*
+ * 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  cgroup.c
+ * @desc  Cgroup related functions
+ **/
+
+#include <errno.h>
+#include "resourced_tests.h"
+
+/* Checks if the input pid is present in the input cgroup */
+int is_pid_in_cgroup(char *cgroup_path, int pid)
+{
+       int curr_pid;
+       FILE *procs_file;
+       char buf[STRING_MAX];
+
+       snprintf(buf, sizeof(buf), "%s%s", cgroup_path, "cgroup.procs");
+       procs_file = fopen(buf, "r");
+       if (!procs_file) {
+               _E("IO: Error opening file %s", buf);
+               return RESOURCED_ERROR_FAIL;
+       }
+
+       curr_pid = -1;
+       _D("reading %s file for %d pid", buf, pid);
+       while (fgets(buf, sizeof(buf), procs_file) != NULL) {
+               if (sscanf(buf, "%d", &curr_pid) != 1) {
+                       _E("IO: Error reading pid value %s", buf);
+                       curr_pid = -1;
+                       break;
+               }
+               if (curr_pid == pid)
+                       break;
+       }
+       fclose(procs_file);
+
+       return (curr_pid == pid) ? RESOURCED_ERROR_NONE : RESOURCED_ERROR_FAIL;
+}
diff --git a/src/tests/cpu/resourced_cpu_test.c b/src/tests/cpu/resourced_cpu_test.c
new file mode 100644 (file)
index 0000000..7089100
--- /dev/null
@@ -0,0 +1,152 @@
+/*
+   Copyright (c) 2000 - 2014 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.
+
+   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
+*/
+
+/* @author: Prajwal A N
+ * @file: cpu.c
+ * @desc: Tests for the CPU module in resourced
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <sys/types.h>
+#include <errno.h>
+#include <time.h>
+
+#include "resourced_tests.h"
+#include "utils.h"
+
+#define CGROUP_FS "/sys/fs/cgroup/"
+#define CPU_MAIN_CGROUP CGROUP_FS"cpu/"
+#define CPU_BACKGROUND_CGROUP CPU_MAIN_CGROUP"background/"
+#define CPU_QUOTA_CGROUP CPU_BACKGROUND_CGROUP"quota/"
+#define CPU_DOWNLOAD_CGROUP CPU_BACKGROUND_CGROUP"download/"
+#define CPU_QUOTA_US "cpu.cfs_quota_us"
+
+static bool cpu_quota_enabled = false;
+
+int cpu_check_cgroups(void)
+{
+       char *cgroups[] = {
+               CPU_MAIN_CGROUP,
+               CPU_BACKGROUND_CGROUP,
+               CPU_DOWNLOAD_CGROUP,
+               NULL,
+       };
+       int i;
+       char buf[STRING_MAX];
+
+       for (i = 0; cgroups[i]; ++i) {
+               snprintf(buf, sizeof(buf), "%s%s", cgroups[i], "cgroup.procs");
+               if (access(buf, F_OK)) {
+                       _E("%s cgroup not created (%d)!", cgroups[i], errno);
+                       return RESOURCED_ERROR_FAIL;
+               }
+       }
+
+       cpu_quota_enabled = false;
+       if (!access(CPU_MAIN_CGROUP CPU_QUOTA_US, F_OK)) {
+               cpu_quota_enabled = true;
+               snprintf(buf, sizeof(buf), "%s%s", CPU_QUOTA_CGROUP, "cgroup.procs");
+               if (access(buf, F_OK)) {
+                       _E("%s cgroup not created (%d)!", CPU_QUOTA_CGROUP, errno);
+                       return RESOURCED_ERROR_FAIL;
+               }
+       }
+
+       return RESOURCED_ERROR_NONE;
+}
+
+int cpu_app_cycle_check(void)
+{
+       int ret;
+       int app_pid;
+
+#define CGROUP_ENTRY_WAIT(time_sec) \
+       do { \
+               struct timespec req, rem; \
+               req.tv_sec = time_sec; \
+               req.tv_nsec = 0; \
+               nanosleep(&req, &rem); \
+       } while(0);
+
+       printf("Checking app cycle. Launch an app and enter its pid:");
+       ret = scanf("%d", &app_pid);
+       CGROUP_ENTRY_WAIT(3);
+
+       ret = is_pid_in_cgroup(CPU_MAIN_CGROUP, app_pid);
+       if (IS_ERROR(ret)) {
+               _E("%d not found in main cpu cgroup after launch", app_pid);
+               return RESOURCED_ERROR_FAIL;
+       }
+
+       printf("Move the app to background\n");
+       CGROUP_ENTRY_WAIT(6);
+
+       ret = is_pid_in_cgroup(CPU_BACKGROUND_CGROUP, app_pid);
+       if (IS_ERROR(ret)) {
+               _E("%d not found in background cpu cgroup after sent to background", app_pid);
+               return RESOURCED_ERROR_FAIL;
+       }
+
+       if (cpu_quota_enabled) {
+               printf("Open another app and send it to background\n");
+               CGROUP_ENTRY_WAIT(15);
+
+               ret = is_pid_in_cgroup(CPU_QUOTA_CGROUP, app_pid);
+               if (IS_ERROR(ret)) {
+                       _E("%d not found in quota cpu cgroup after readied for suspend", app_pid);
+                       return RESOURCED_ERROR_FAIL;
+               }
+       }
+
+       printf("Resume the first app through task mgr\n");
+       CGROUP_ENTRY_WAIT(10);
+
+       ret = is_pid_in_cgroup(CPU_MAIN_CGROUP, app_pid);
+       if (IS_ERROR(ret)) {
+               _E("%d not found in main cpu cgroup after resuming", app_pid);
+               return RESOURCED_ERROR_FAIL;
+       }
+
+       printf("Testing complete. Exit the app\n");
+       return RESOURCED_ERROR_NONE;
+}
+
+static struct resourced_test_t cpu_tests[] = {
+       { "cpu_check_cgroups", cpu_check_cgroups },
+       { "cpu_app_cycle", cpu_app_cycle_check },
+       { "", NULL },
+};
+
+int main(int argc, char *argv[])
+{
+       int i, ret;
+
+       TEST_START_MESSAGE("cpu module");
+
+       for (i = 0; cpu_tests[i].test_func; ++i) {
+               _D("=======================================");
+               _D("Current Test: %s", cpu_tests[i].name);
+               ret = (*cpu_tests[i].test_func)();
+               if (IS_ERROR(ret))
+                       _E("Test %s failed!", cpu_tests[i].name);
+               else
+                       _D("Test %s passed!", cpu_tests[i].name);
+
+               if (!i && IS_ERROR(ret))
+                       break;
+       }
+       return 0;
+}
index f6ee4760c72e642cb6ddc5febf29bf2ab848287d..c4863ffb7dcae2eed76a3e11344c6fbe9e481bbb 100644 (file)
@@ -42,5 +42,6 @@ unsigned int procfs_get_total(void);
 int procfs_set_oom_score_adj(int pid, int oom);
 
 int pid_exists(int pid);
+int is_pid_in_cgroup(char *cgroup_path, int pid);
 
 #endif