int cgroup_write_node_uint32(const char *cgroup_name,
const char *file_name, uint32_t value)
{
+ int ret;
char buf[MAX_PATH_LENGTH];
snprintf(buf, sizeof(buf), "%s/%s", cgroup_name, file_name);
- _SD("cgroup_buf %s, value %u\n", buf, value);
- return fwrite_uint(buf, value);
+ ret = fwrite_uint(buf, value);
+ if (ret == RESOURCED_ERROR_NONE)
+ _SD("cgroup_buf = %s, value = %u\n", buf, value);
+ else
+ _SE("errno = %d", ret);
+
+ return ret;
}
int cgroup_write_node_int32(const char *cgroup_name,
const char *file_name, int32_t value)
{
+ int ret;
char buf[MAX_PATH_LENGTH];
snprintf(buf, sizeof(buf), "%s/%s", cgroup_name, file_name);
- _SD("cgroup_buf %s, value %d\n", buf, value);
- return fwrite_int(buf, value);
+ ret = fwrite_int(buf, value);
+ if (ret == RESOURCED_ERROR_NONE)
+ _SD("cgroup_buf = %s, value = %d\n", buf, value);
+ else
+ _SE("errno = %d", ret);
+
+ return ret;
}
int cgroup_write_node_ulonglong(const char *cgroup_name,
const char *file_name, unsigned long long value)
{
+ int ret;
char buf[MAX_PATH_LENGTH];
snprintf(buf, sizeof(buf), "%s/%s", cgroup_name, file_name);
- _SD("cgroup_buf %s, value %llu\n", buf, value);
- return fwrite_ulonglong(buf, value);
+ ret = fwrite_ulonglong(buf, value);
+ if (ret == RESOURCED_ERROR_NONE)
+ _SD("cgroup_buf = %s, value = %llu\n", buf, value);
+ else
+ _SE("errno = %d", ret);
+
+ return ret;
}
int cgroup_write_node_longlong(const char *cgroup_name,
const char *file_name, long long value)
{
+ int ret;
char buf[MAX_PATH_LENGTH];
snprintf(buf, sizeof(buf), "%s/%s", cgroup_name, file_name);
- _SD("cgroup_buf %s, value %lld\n", buf, value);
- return fwrite_longlong(buf, value);
+ ret = fwrite_longlong(buf, value);
+ if (ret == RESOURCED_ERROR_NONE)
+ _SD("cgroup_buf = %s, value = %lld\n", buf, value);
+ else
+ _SE("errno = %d", ret);
+
+ return ret;
}
int cgroup_write_node_str(const char *cgroup_name,
const char *file_name, const char *string)
{
+ int ret;
char buf[MAX_PATH_LENGTH];
snprintf(buf, sizeof(buf), "%s/%s", cgroup_name, file_name);
- _SD("cgroup_buf %s, string %s\n", buf, string);
- return fwrite_str(buf, string);
+ ret = fwrite_str(buf, string);
+ if (ret == RESOURCED_ERROR_NONE)
+ _SD("cgroup_buf = %s, value = %s\n", buf, string);
+ else
+ _SE("errno = %d", ret);
+
+ return ret;
}
int cgroup_read_node_uint32(const char *cgroup_name,
const char *file_name, uint32_t *value)
{
- char buf[MAX_PATH_LENGTH];
int ret;
+ char buf[MAX_PATH_LENGTH];
snprintf(buf, sizeof(buf), "%s/%s", cgroup_name, file_name);
ret = fread_uint(buf, value);
- _SD("cgroup_buf %s, value %d\n", buf, *value);
+ if (ret == RESOURCED_ERROR_NONE)
+ _SD("cgroup_buf = %s, value = %u\n", buf, *value);
+ else
+ _SE("errno = %d", ret);
+
return ret;
}
int cgroup_read_node_int32(const char *cgroup_name,
const char *file_name, int32_t *value)
{
- char buf[MAX_PATH_LENGTH];
int ret;
+ char buf[MAX_PATH_LENGTH];
snprintf(buf, sizeof(buf), "%s/%s", cgroup_name, file_name);
ret = fread_int(buf, value);
- _SD("cgroup_buf %s, value %d\n", buf, *value);
+ if (ret == RESOURCED_ERROR_NONE)
+ _SD("cgroup_buf = %s, value = %d\n", buf, *value);
+ else
+ _SE("errno = %d", ret);
+
return ret;
}
int cgroup_read_node_ulonglong(const char *cgroup_name,
const char *file_name, unsigned long long *value)
{
- char buf[MAX_PATH_LENGTH];
int ret;
+ char buf[MAX_PATH_LENGTH];
snprintf(buf, sizeof(buf), "%s/%s", cgroup_name, file_name);
ret = fread_ulonglong(buf, value);
- _SD("cgroup_buf %s, value %llu\n", buf, *value);
+ if (ret == RESOURCED_ERROR_NONE)
+ _SD("cgroup_buf = %s, value = %llu\n", buf, *value);
+ else
+ _SE("errno = %d", ret);
+
return ret;
}
enum swap_thread_op {
SWAP_OP_ACTIVATE,
+ SWAP_OP_EARLY_RECLAIM,
SWAP_OP_RECLAIM,
SWAP_OP_COMPACT,
SWAP_OP_END,
error = proc_get_oom_score_adj(pid, &oom_score_adj);
if (error) {
- _E("Cannot get oom_score_adj of pid (%d)", pid);
+ _E("[SWAP] Cannot get oom_score_adj of pid (%d)", pid);
return RESOURCED_ERROR_FAIL;
}
}
if (ret != RESOURCED_ERROR_NONE)
- _E("Not able to set hard limit of %s memory cgroup", memcg);
+ _E("[SWAP] Not able to set hard limit of %s memory cgroup", memcg);
return ret;
}
r = cgroup_write_node_uint32(memcg,
MEMCG_SWAPPINESS, SWAP_MEMCG_SWAPPINESS);
if (r) {
- _I("failed to write %s %d to %s the",
+ _I("[SWAP] failed to write %s %d to %s the",
MEMCG_SWAPPINESS, SWAP_MEMCG_SWAPPINESS, memcg);
return -errno;
}
r = cgroup_write_node_uint32(memcg,
MEMCG_SWAPPINESS, current_swappiness);
if (r) {
- _I("failed to write %s %d to %s the",
+ _I("[SWAP] failed to write %s %d to %s the",
MEMCG_SWAPPINESS, SWAP_MEMCG_SWAPPINESS, memcg);
return -errno;
}
if (!is_empty(crypt_type)) {
r = gen_urandom_string(passwd, LO_KEY_SIZE);
if (r < 0) {
- _E("Failed to generate random passwd: %m");
+ _E("[SWAP] Failed to generate random passwd: %m");
return r;
}
}
r = find_loop_device(swapfile, swapdevice);
if (r < 0) {
if (r == -EEXIST) {
- _I("'%s' is already setuped to '%s'", swapfile, swapdevice);
+ _I("[SWAP] '%s' is already setuped to '%s'", swapfile, swapdevice);
goto finish;
} else
- _E("Failed to find available loop device.");
+ _E("[SWAP] Failed to find available loop device.");
return r;
}
crypt_type, passwd, false);
if (r < 0) {
- _E("Failed to setup loop: %d", r);
+ _E("[SWAP] Failed to setup loop: %d", r);
return r;
}
r = do_dd("/dev/zero", filename, PAGE_SIZE,
KBYTE_TO_BYTE(swap->k_size) >> PAGE_SHIFT);
if (r > 0) {
- _E("Failed to create swap file(%s), exit code: %d",
+ _E("[SWAP] Failed to create swap file(%s), exit code: %d",
filename, r);
return -1;
} else if (r < 0) {
- _E("Failed to create swap file(%s): %d",
+ _E("[SWAP] Failed to create swap file(%s): %d",
filename, r);
return r;
}
- _I("SwapFile is created: %s", filename);
+ _I("[SWAP] SwapFile is created: %s", filename);
} else if (r != 0) {
- _E("Failed to access swap file(%s): %m", filename);
+ _E("[SWAP] Failed to access swap file(%s): %m", filename);
return -errno;
}
r = do_mkswap(swap->path);
if (r > 0) {
- _E("Failed to make swap device(%s), exit code: %d", swap->path, r);
+ _E("[SWAP] Failed to make swap device(%s), exit code: %d", swap->path, r);
return -EBUSY;
} else if (r < 0) {
- _E("Failed to make swap device(%s): %d", swap->path, r);
+ _E("[SWAP] Failed to make swap device(%s): %d", swap->path, r);
}
return r;
}
return false;
}
+static void swap_early_reclaim_memcg(void)
+{
+ int r;
+ static bool early_reclaim = false;
+
+ /*
+ * Most system daemons use much memory for intializing own process.
+ * but there is little change to reuse it while running.
+ * So early reclaiming moves unused system memory to swap device.
+ * If multiple swap devices are available,
+ * first swap with the lowest priority will start reclaiming.
+ */
+ if (!early_reclaim) {
+ int try = arg_swap_at_boot_maxtry;
+ unsigned long long usage_bytes, prev_usage_bytes = 0;
+
+ r = cgroup_read_node_ulonglong(MEMCG_PATH,
+ MEMCG_SWAP_USAGE, &prev_usage_bytes);
+ if (r) {
+ r = cgroup_read_node_ulonglong(MEMCG_PATH,
+ MEMCG_USAGE, &prev_usage_bytes);
+ if (r)
+ prev_usage_bytes = ULONG_MAX;
+ }
+
+ for (;;) {
+ try--;
+ swap_start_reclaim(MEMCG_PATH);
+
+ r = cgroup_read_node_ulonglong(MEMCG_PATH,
+ MEMCG_SWAP_USAGE, &usage_bytes);
+ if (r) {
+ r = cgroup_read_node_ulonglong(MEMCG_PATH,
+ MEMCG_USAGE, &usage_bytes);
+ if (r) {
+ _E("[SWAP] Early reclaim is aborted");
+ break;
+ }
+ }
+
+ /*
+ * Continue to reclaim memory at boot time until
+ * there is no more saving or trial reaches maximum.
+ * The default threshold is very large, so it may
+ * reclaim once.
+ */
+ if (!try || prev_usage_bytes - usage_bytes < arg_swap_at_boot_threshold_bytes)
+ break;
+
+ prev_usage_bytes = usage_bytes;
+ /*
+ * To prevent continuous reclaim to harm entire system,
+ * having relaxation on each reclaim
+ */
+ sleep(arg_swap_at_boot_interval_sec);
+ }
+
+ early_reclaim = true;
+ }
+}
+
static void swap_activate_in_module(void)
{
int r;
GSList *iter;
struct swap_module_ops *swaps;
- static bool early_reclaim;
r = cgroup_read_node_int32(MEMCG_PATH,
- MEMCG_SWAPPINESS, ¤t_swappiness);
+ MEMCG_SWAPPINESS, ¤t_swappiness);
if (r)
return;
r = swaps->activate(swaps);
if (r != 0) {
if (r != -EEXIST) {
- _E("%s activate failed", swaps->name);
+ _E("[SWAP] %s activate failed", swaps->name);
swap_remove(swaps);
}
swap_set_state(SWAP_ON);
#endif
r = swapon(swaps->path, flags);
if (r < 0) {
- _E("Failed to turn on swap device(%s): %m", swaps->path);
+ _E("[SWAP] Failed to turn on swap device(%s): %m", swaps->path);
continue;
}
- _I("swap device(%s) was turned on: %s", swaps->name, swaps->path);
-
- /*
- * Most system daemons use much memory for intializing own process.
- * but there is little change to reuse it while running.
- * So early reclaiming moves unused system memory to swap device.
- * If multiple swap devices are available,
- * first swap with the lowest priority will start reclaiming.
- */
- if (!early_reclaim) {
- int try = arg_swap_at_boot_maxtry;
- unsigned long long usage_bytes, prev_usage_bytes = 0;
-
- r = cgroup_read_node_ulonglong(MEMCG_PATH,
- MEMCG_SWAP_USAGE, &prev_usage_bytes);
- if (r)
- prev_usage_bytes = ULONG_MAX;
+ _I("[SWAP] swap device(%s) was turned on: %s", swaps->name, swaps->path);
- for (;;) {
- try--;
- swap_start_reclaim(MEMCG_PATH);
-
- r = cgroup_read_node_ulonglong(MEMCG_PATH,
- MEMCG_SWAP_USAGE, &usage_bytes);
- if (r) {
- _E("Early reclaimed is aborted");
- break;
- }
-
- /*
- * Continue to reclaim memory at boot time until
- * there is no more saving or trial reaches maximum.
- * The default threshold is very large, so it may
- * reclaim once.
- */
- if (!try || prev_usage_bytes - usage_bytes < arg_swap_at_boot_threshold_bytes)
- break;
-
- prev_usage_bytes = usage_bytes;
- /*
- * To prevent continuous reclaim to harm entire system,
- * having relaxation on each reclaim
- */
- sleep(arg_swap_at_boot_interval_sec);
- }
- early_reclaim = true;
- }
swap_set_state(SWAP_ON);
}
goto unlock_out;
switch (bundle->op) {
- /* Swap activation operttion: mkswap, swapon etc. */
- case SWAP_OP_ACTIVATE:
- swap_activate_in_module();
- break;
- /* Swap reclaim opertation: move to swap, force_reclaim */
- case SWAP_OP_RECLAIM:
- swap_reclaim_memcg(bundle->msg.path);
- break;
- /* Swap compact operation of zsmalloc. */
- case SWAP_OP_COMPACT:
- swap_compact_in_module();
- break;
- /* Move inactive procesess to swap, and reclaim after that. */
- case SWAP_OP_END:
- default:
- _D("wrong swap thread operation selected");
+ /* Swap activation operttion: mkswap, swapon etc. */
+ case SWAP_OP_ACTIVATE:
+ swap_activate_in_module();
+ break;
+ /* Swap early reclaim operation: move to swap, force_reclaim */
+ case SWAP_OP_EARLY_RECLAIM:
+ swap_early_reclaim_memcg();
+ break;
+ /* Swap reclaim opertation: move to swap, force_reclaim */
+ case SWAP_OP_RECLAIM:
+ swap_reclaim_memcg(bundle->msg.path);
+ break;
+ /* Swap compact operation of zsmalloc. */
+ case SWAP_OP_COMPACT:
+ swap_compact_in_module();
+ break;
+ /* Move inactive procesess to swap, and reclaim after that. */
+ case SWAP_OP_END:
+ default:
+ _D("[SWAP] wrong swap thread operation selected");
}
free(bundle);
if (ret == 0) {
pthread_cond_signal(&swap_cond);
pthread_mutex_unlock(&swap_mutex);
- _I("send signal to swap thread");
+ _I("[SWAP] send signal to swap thread");
return RESOURCED_ERROR_NONE;
} else if (ret == EBUSY) {
- _D("swap thread already active");
+ _D("[SWAP] swap thread already active");
return RESOURCED_ERROR_NONE;
}
- _E("pthread_mutex_trylock fail: %d, errno: %d", ret, errno);
+ _E("[SWAP] pthread_mutex_trylock fail: %d, errno: %d", ret, errno);
return RESOURCED_ERROR_FAIL;
}
struct swap_thread_bundle *bundle;
if (!path) {
- _E("path name is NULL");
+ _E("[SWAP] path name is NULL");
return RESOURCED_ERROR_NO_DATA;
}
ret = str_name_cpy(bundle->msg.path, path, sizeof(bundle->msg.path), strlen(path));
if (ret < 0) {
- _E("msg size (%d) is smaller than source length (%d)",
+ _E("[SWAP] msg size (%d) is smaller than source length (%d)",
(int)sizeof(bundle->msg.path), (int)strlen(path) + 1);
free(bundle);
return ret;
static gboolean swap_activate_timer_cb(gpointer data)
{
- _I("Starting an " EARLYRECLAIM_WITH_AN_EXPLANATION_FOR_LAYMEN);
+ _I("[SWAP] Starting an " EARLYRECLAIM_WITH_AN_EXPLANATION_FOR_LAYMEN);
swap_activating_timer = NULL;
- swap_internal_bundle_sender(SWAP_OP_ACTIVATE);
+ swap_internal_bundle_sender(SWAP_OP_EARLY_RECLAIM);
return false;
}
return RESOURCED_ERROR_NONE;
}
- _D("booting done; starting up a timer to perform an " EARLYRECLAIM_WITH_AN_EXPLANATION_FOR_LAYMEN " %ds from now", arg_timer_swap_at_boot_sec);
+ _D("[SWAP] booting done; starting up a timer to perform an " EARLYRECLAIM_WITH_AN_EXPLANATION_FOR_LAYMEN " %ds from now", arg_timer_swap_at_boot_sec);
swap_activating_timer = g_timeout_source_new_seconds((guint) arg_timer_swap_at_boot_sec);
g_source_set_callback(swap_activating_timer, swap_activate_timer_cb, NULL, NULL);
g_source_attach(swap_activating_timer, NULL);
static int swap_compact_handler(void *data)
{
- _I("compaction request. Reason: %s",
+ _I("[SWAP] compaction request. Reason: %s",
compact_reason_to_str((enum swap_compact_reason)data));
return swap_internal_bundle_sender(SWAP_OP_COMPACT);
}
struct memcg_info *mi = (struct memcg_info *)data;
if (!mi) {
- _E("memory cgroup information is NULL");
+ _E("[SWAP] memory cgroup information is NULL");
return RESOURCED_ERROR_INVALID_PARAMETER;
}
ret = check_oom_and_set_limit(mi->name, mi->limit_bytes);
if (ret != RESOURCED_ERROR_NONE)
- _E("Failed to change hard limit of %s cgroup to -1", mi->name);
+ _E("[SWAP] Failed to change hard limit of %s cgroup to -1", mi->name);
else
- _D("changed hard limit of %s cgroup to -1", mi->name);
+ _D("[SWAP] changed hard limit of %s cgroup to -1", mi->name);
return ret;
}
do_expr_unless_g_variant_get_typechecked(return, params, "(i)", &pid);
if (pid <= 0) {
- _D("Invalid parameter");
+ _D("[SWAP] Invalid parameter");
return;
}
return;
swap_move_to_cgroup_by_pid(MEMCG_LOW, pid);
swap_start_handler(cgroup_swap->memcg_info->name);
- _I("swap cgroup entered : pid : %d", (int)pid);
+ _I("[SWAP] swap cgroup entered : pid : %d", (int)pid);
}
swap_thread_queue.queue = g_queue_new();
if (!swap_thread_queue.queue) {
- _E("fail to allocate swap thread queue");
+ _E("[SWAP] fail to allocate swap thread queue");
return RESOURCED_ERROR_FAIL;
}
ret = pthread_create(&pth, NULL, &swap_thread_main, (void *)NULL);
if (ret) {
- _E("pthread creation for swap_thread failed\n");
+ _E("[SWAP] pthread creation for swap_thread failed\n");
return ret;
} else {
pthread_detach(pth);
*/
if (swap_pri == SWAP_PRI_DISABLE) {
swap_remove(swaps);
- _E("don't allow other %s module", swaps->name);
+ _E("[SWAP] don't allow other %s module", swaps->name);
continue;
}
swap_pri = swaps->priority;
r = swaps->init(swaps);
if (r != 0) {
- _E("%s module init failed", swaps->name);
+ _E("[SWAP] %s module init failed", swaps->name);
swap_remove(swaps);
}
}
r = swap_thread_create();
if (r) {
- _E("swap thread create failed");
+ _E("[SWAP] swap thread create failed");
return r;
}
swap_dbus_init();
fp = fopen(path, "w");
if (fp == NULL) {
- _E("%s open failed", path);
+ _E("[SWAP] %s open failed", path);
return RESOURCED_ERROR_NO_DATA;
}
return RESOURCED_ERROR_NONE;
static int resourced_swap_check_runtime_support(void *data)
{
- int r;
- unsigned long long usage_bytes;
+ int r = RESOURCED_ERROR_NONE;
+// unsigned long long usage_bytes;
/*
* Check whether CONFIG_SWAP is enabled in kernel.
*/
- if (access("/proc/swaps", R_OK) != 0)
+ if (access("/proc/swaps", R_OK) != 0) {
+ _W("CONFIG_SWAP is disabled");
return -ENOENT;
+ }
/*
* Check whether kernel is supporting MEMCG_SWAP.
*/
- r = cgroup_read_node_ulonglong(MEMCG_PATH,
+/* r = cgroup_read_node_ulonglong(MEMCG_PATH,
MEMCG_SWAP_USAGE, &usage_bytes);
if (r)
- return -ENOENT;
+ return -ENOENT;*/
return r;
}
ret = cgroup_write_node_uint32(cgroup_swap->memcg_info->name, MEMCG_MOVE_CHARGE, 1);
if (ret != RESOURCED_ERROR_NONE)
{
- _E("Failed to write at %s/%s", cgroup_swap->memcg_info->name, MEMCG_MOVE_CHARGE);
+ _E("[SWAP] Failed to write at %s/%s", cgroup_swap->memcg_info->name, MEMCG_MOVE_CHARGE);
}
snprintf(buf, sizeof(buf), "%s/%s", MEMCG_PATH, MEMCG_FORCE_RECLAIM);
ret = swap_check_node(buf);
if (ret == RESOURCED_ERROR_NONE) {
swap_node = SWAP_NODE_FORCE_RECLAIM;
- _I("use %s node for swapping memory", MEMCG_FORCE_RECLAIM);
+ _I("[SWAP] use %s node for swapping memory", MEMCG_FORCE_RECLAIM);
} else {
swap_node = SWAP_NODE_HARD_LIMIT;
- _I("use %s node for swapping memory", MEMCG_LIMIT_BYTE);
+ _I("[SWAP] use %s node for swapping memory", MEMCG_LIMIT_BYTE);
}
}
{
int ret;
+ _D("[SWAP] resourced swap init start");
+
resourced_swap_change_memcg_settings(MEMCG_LOW);
swap_set_state(SWAP_OFF);
return ret;
if (!resourced_restarted()) {
+ swap_activate_handler(NULL);
register_notifier(RESOURCED_NOTIFIER_BOOTING_DONE, swap_booting_done);
} else {
- _I("Assuming swap is already set up. To reconfigure, reboot the device.");
+ _I("[SWAP] Assuming swap is already set up. To reconfigure, reboot the device.");
}
register_notifier(RESOURCED_NOTIFIER_SWAP_START, swap_start_handler);
}
static struct module_ops swap_modules_ops = {
- .priority = MODULE_PRIORITY_NORMAL,
+ .priority = MODULE_PRIORITY_HIGH,
.name = "swap",
.init = resourced_swap_init,
.exit = resourced_swap_finalize,