diff --git a/src/builder/b-nspawn.c b/src/builder/b-nspawn.c
index 90297ad..e864c0c 100644
--- a/src/builder/b-nspawn.c
+++ b/src/builder/b-nspawn.c
@@ -217,10 +217,23 @@ sai_lsp_reap_cb(void *opaque, const lws_spawn_resource_us_t *res, siginfo_t *si,
/* step succeeded */
lws_dir_du_t du;
+ uint64_t peak_mem_bytes;
memset(&du, 0, sizeof(du));
lws_dir(ns->inp, &du, lws_dir_du_cb);
+ peak_mem_bytes = res->peak_mem_rss;
+#if defined(__linux__)
+ /* on linux, getrusage reports in KiB, on OSX in bytes */
+ peak_mem_bytes *= 1024;
+#elif defined(__APPLE__)
+ /*
+ * lws seems to be multiplying OSX ru_maxrss by 1024 when it's already
+ * in bytes. Let's divide it back to bytes.
+ */
+ peak_mem_bytes /= 1024;
+#endif
+
{
// char h1[40], h2[40], h3[40], h4[40], h10[40];
char h5[40], h6[40], h7[40], h8[40], h9[40];
@@ -231,8 +244,8 @@ sai_lsp_reap_cb(void *opaque, const lws_spawn_resource_us_t *res, siginfo_t *si,
if (du.size_in_bytes > ns->worst_stg)
ns->worst_stg = du.size_in_bytes;
- if (res->peak_mem_rss > ns->worst_mem)
- ns->worst_mem = res->peak_mem_rss;
+ if (peak_mem_bytes > ns->worst_mem)
+ ns->worst_mem = peak_mem_bytes;
// lws_humanize_pad(h1, sizeof(h1), ns->us_cpu_user, humanize_schema_us);
// lws_humanize_pad(h2, sizeof(h2), ns->us_cpu_sys, humanize_schema_us);
@@ -240,7 +253,7 @@ sai_lsp_reap_cb(void *opaque, const lws_spawn_resource_us_t *res, siginfo_t *si,
// lws_humanize_pad(h4, sizeof(h4), ns->worst_stg, humanize_schema_si);
lws_humanize_pad(h5, sizeof(h5), res->us_cpu_user, humanize_schema_us);
lws_humanize_pad(h6, sizeof(h6), res->us_cpu_sys, humanize_schema_us);
- lws_humanize_pad(h7, sizeof(h7), res->peak_mem_rss, humanize_schema_si);
+ lws_humanize_pad(h7, sizeof(h7), peak_mem_bytes, humanize_schema_si);
lws_humanize_pad(h8, sizeof(h8), du.size_in_bytes, humanize_schema_si);
lws_humanize_pad(h9, sizeof(h9), us_wallclock, humanize_schema_us);
// lws_humanize_pad(h10, sizeof(h10), ns->us_wallclock, humanize_schema_us);
@@ -296,7 +309,7 @@ sai_lsp_reap_cb(void *opaque, const lws_spawn_resource_us_t *res, siginfo_t *si,
m->us_cpu_user = res->us_cpu_user;
m->us_cpu_sys = res->us_cpu_sys;
m->wallclock_us = us_wallclock;
- m->peak_mem_rss = res->peak_mem_rss;
+ m->peak_mem_rss = peak_mem_bytes;
m->stg_bytes = du.size_in_bytes;
m->parallel = ns->task->parallel;
diff --git a/src/builder/b-power.c b/src/builder/b-power.c
index 4d13efa..fba3adb 100644
--- a/src/builder/b-power.c
+++ b/src/builder/b-power.c
@@ -229,6 +229,34 @@ LWS_SS_INFO("sai_power", saib_power_link_t)
};
+static void
+sul_do_suspend_cb(lws_sorted_usec_list_t *sul)
+{
+#if !defined(WIN32)
+ uint8_t te = 1;
+ ssize_t n;
+
+ lwsl_notice("%s: actioning suspend...\n", __func__);
+
+ n = write(lws_spawn_get_fd_stdxxx(lsp_suspender, 0), &te, 1);
+ if (n == 1) {
+#if defined(WIN32)
+ Sleep(2000);
+#else
+ sleep(2);
+#endif
+ /*
+ * There were 0 tasks ongoing for us to suspend, start off
+ * with the same assumption and set the idle grace time
+ */
+ lws_sul_schedule(builder.context, 0, &builder.sul_idle,
+ sul_idle_cb, SAI_IDLE_GRACE_US);
+ lwsl_notice("%s: resuming after suspend\n", __func__);
+ } else
+ lwsl_err("%s: failed to request suspend\n", __func__);
+#endif
+}
+
/*
* The grace time is up, ask for the suspend
*/
@@ -238,10 +266,8 @@ sul_idle_cb(lws_sorted_usec_list_t *sul)
{
lws_ss_state_return_t r;
char path[256];
-#if !defined(WIN32)
- ssize_t n;
- uint8_t te = 1;
+#if !defined(WIN32)
if (builder.stay)
return;
@@ -250,24 +276,35 @@ sul_idle_cb(lws_sorted_usec_list_t *sul)
if (builder.power_off_type &&
!strcmp(builder.power_off_type, "suspend")) {
- lwsl_notice("%s: requesting suspend...\n", __func__);
+ lwsl_notice("%s: starting suspend...\n", __func__);
- n = write(lws_spawn_get_fd_stdxxx(lsp_suspender, 0), &te, 1);
- if (n == 1) {
-#if defined(WIN32)
- Sleep(2000);
-#else
- sleep(2);
-#endif
- /*
- * There were 0 tasks ongoing for us to suspend, start off
- * with the same assumption and set the idle grace time
- */
- lws_sul_schedule(builder.context, 0, &builder.sul_idle,
- sul_idle_cb, SAI_IDLE_GRACE_US);
- lwsl_notice("%s: resuming after suspend\n", __func__);
- } else
- lwsl_err("%s: failed to request suspend\n", __func__);
+ /*
+ * Let everybody know we are trying to power down
+ */
+ lws_start_foreach_dll(struct lws_dll2 *, d,
+ builder.sai_plat_owner.head) {
+ sai_plat_t *p = lws_container_of(d, sai_plat_t,
+ sai_plat_list);
+ p->powering_down = 1;
+ } lws_end_foreach_dll(d);
+
+ lws_start_foreach_dll(struct lws_dll2 *, d,
+ builder.sai_plat_server_owner.head) {
+ struct sai_plat_server *spm = lws_container_of(d,
+ struct sai_plat_server, list);
+
+ spm->phase = PHASE_START_ATTACH;
+ if (lws_ss_request_tx(spm->ss))
+ lwsl_ss_warn(spm->ss, "Unable to request tx");
+
+ } lws_end_foreach_dll(d);
+
+ /*
+ * give the event loop a moment to send the notifications out
+ * before we do the blocking suspend part
+ */
+ lws_sul_schedule(builder.context, 0, &builder.sul_do_suspend,
+ sul_do_suspend_cb, 2 * LWS_US_PER_SEC);
return;
}
diff --git a/src/builder/b-private.h b/src/builder/b-private.h
index 8a0c794..9cd447b 100644
--- a/src/builder/b-private.h
+++ b/src/builder/b-private.h
@@ -100,6 +100,7 @@ struct sai_builder {
struct lws_vhost *vhost;
lws_sorted_usec_list_t sul_idle;
+ lws_sorted_usec_list_t sul_do_suspend;
lws_sorted_usec_list_t sul_stay;
lws_sorted_usec_list_t sul_cleanup_jobs;
diff --git a/src/server/s-websrv.c b/src/server/s-websrv.c
index 89835d6..e71cdf6 100644
--- a/src/server/s-websrv.c
+++ b/src/server/s-websrv.c
@@ -760,7 +760,9 @@ websrvss_ws_rx(void *userobj, const uint8_t *buf, size_t len, int flags)
lws_start_foreach_dll(struct lws_dll2 *, p,
m->vhd->sai_powers.head) {
struct pss *pss_power = lws_container_of(p, struct pss, same);
- sai_stay_t *s = malloc(sizeof(*s));
+ sai_stay_t *s;
+
+ s = malloc(sizeof(*s));
if (s) {
*s = *stay;
lws_dll2_add_tail(&s->list, &pss_power->stay_owner);
diff --git a/src/web/w-websrv.c b/src/web/w-websrv.c
index 3ef960b..8161887 100644
--- a/src/web/w-websrv.c
+++ b/src/web/w-websrv.c
@@ -297,7 +297,7 @@ saiw_lp_tx(void *userobj, lws_ss_tx_ordinal_t ord, uint8_t *buf, size_t *len,
*len = used;
*flags = (som ? LWSSS_FLAG_SOM : 0) | (final ? LWSSS_FLAG_EOM : 0);
- lwsl_ss_notice(m->ss, "Sending %d ssflags %d", (int)*len, (int)*flags);
+ lwsl_ss_notice(m->ss, "Sending %d web->srv: ssflags %d", (int)*len, (int)*flags);
lwsl_hexdump_notice(buf, *len);
if (m->wbltx)