)
#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)
--- /dev/null
+/*
+ * 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);
+}
+
--- /dev/null
+/*
+ * 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;
+ }
+ }
+}
+
#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
--- /dev/null
+/*
+ * 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
+++ /dev/null
-/*
- * 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
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) */
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",
* 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;
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;
* 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;
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;
/* 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
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
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;
}
}
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;
}
}
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);
_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
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 */
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) {
*/
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;
}
/* 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;
}
* 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];
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);
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);
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;
}
}
+++ /dev/null
-/*
- * 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;
- }
- }
-}
-
#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);
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 },
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;
}
#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];
};
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);