#define SWAP_RECLAIM_PAGES_MIN 128
#define SWAP_MEMCG_SWAPPINESS 60
#define SWAP_MIN_SWAPPINESS 0
-#define SWAP_EARLYRECALIM_TIME 60
+#define SWAP_EARLYRECLAIM_TIME_DEFAULT 60
+
+#define EARLYRECLAIM_WITH_AN_EXPLANATION_FOR_LAYMEN "early memory reclaim (done to retrieve resources used by daemons during system start-up)"
enum swap_thread_op {
SWAP_OP_ACTIVATE,
static enum swap_state swap_state;
static bool arg_swap_enable = false;
+static bool arg_swap_at_boot = false;
+static int arg_timer_swap_at_boot = SWAP_EARLYRECLAIM_TIME_DEFAULT;
static enum swap_type arg_swap_type = SWAP_TYPE_ZRAM;
static int current_swappiness = SWAP_MEMCG_SWAPPINESS;
_E("pthread_mutex_trylock fail: %d, errno: %d", ret, errno);
return RESOURCED_ERROR_FAIL;
-
}
static int swap_start_handler(void *data)
static gboolean swap_activate_timer_cb(gpointer data)
{
+ _I("Starting an " EARLYRECLAIM_WITH_AN_EXPLANATION_FOR_LAYMEN);
swap_activating_timer = NULL;
swap_internal_bundle_sender(SWAP_OP_ACTIVATE);
return false;
static int swap_booting_done(void *data)
{
- swap_activating_timer = g_timeout_source_new_seconds(SWAP_EARLYRECALIM_TIME);
+ if (!arg_swap_at_boot) {
+ _D(EARLYRECLAIM_WITH_AN_EXPLANATION_FOR_LAYMEN " is disabled");
+ return RESOURCED_ERROR_NONE;
+ }
+
+ /* No need to involve the timer mechanism when the delay is 0,
+ * partially to keep things simple but primarily because it can
+ * introduce an artificial delay since the timer is ran async
+ * in another thread. */
+ if (arg_timer_swap_at_boot == 0) {
+ swap_activate_timer_cb(NULL);
+ 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);
+ swap_activating_timer = g_timeout_source_new_seconds((guint) arg_timer_swap_at_boot);
g_source_set_callback(swap_activating_timer, swap_activate_timer_cb, NULL, NULL);
g_source_attach(swap_activating_timer, NULL);
0, &arg_swap_enable },
{ "SWAP", "Type", config_parse_swap_types,
0, &arg_swap_type },
+ { "SWAP", "ReclaimAtBoot", config_parse_bool,
+ 0, &arg_swap_at_boot },
+ { "SWAP", "TimerReclaimAtBoot", config_parse_int,
+ 0, &arg_timer_swap_at_boot },
{ NULL, NULL, NULL,
0, NULL }
};
return r;
}
+ /* `g_timeout_source_new_seconds` wants an unsigned value,
+ * but there is no `config_parse_uint` to produce one. */
+ if (arg_timer_swap_at_boot < 0) {
+ _E("The `TimerReclaimAtBoot` field in the `" SWAP_CONF_FILE "` config file cannot be negative because it represents a time period for " EARLYRECLAIM_WITH_AN_EXPLANATION_FOR_LAYMEN);
+ return -EINVAL;
+ }
+
return 0;
}