From fbc1ff6e7b2fd1a3990cd1b49a33a1da78a02c4e Mon Sep 17 00:00:00 2001
From: Andy Green <andy@warmcat.com>
Date: Mon, 15 May 2017 07:30:06 +0800
Subject: [PATCH] stats: ah and ssl restriction stats

---
 lib/libwebsockets.c         | 51 +++++++++++++++++++++++++++++++++++++
 lib/lws-plat-esp32.c        |  9 ++++---
 lib/parsers.c               |  2 +-
 lib/private-libwebsockets.h |  2 +-
 lib/server.c                |  8 ++++--
 lib/ssl.c                   | 10 ++++++++
 6 files changed, 74 insertions(+), 8 deletions(-)

diff --git a/lib/libwebsockets.c b/lib/libwebsockets.c
index b69a4ef2..9fd89cd9 100755
--- a/lib/libwebsockets.c
+++ b/lib/libwebsockets.c
@@ -3126,6 +3126,9 @@ lws_stats_get(struct lws_context *context, int index)
 LWS_VISIBLE LWS_EXTERN void
 lws_stats_log_dump(struct lws_context *context)
 {
+	struct lws_vhost *v = context->vhost_list;
+	int n;
+
 	if (!context->updated)
 		return;
 
@@ -3170,6 +3173,54 @@ lws_stats_log_dump(struct lws_context *context)
 		lwsl_notice("  Avg writable delay:                       %8lluus\n",
 			(unsigned long long)(lws_stats_get(context, LWSSTATS_MS_WRITABLE_DELAY) /
 			lws_stats_get(context, LWSSTATS_C_WRITEABLE_CB)));
+	lwsl_notice("Simultaneous SSL restriction:               %8d/%d/%d\n", context->simultaneous_ssl,
+		context->simultaneous_ssl_restriction, context->ssl_gate_accepts);
+
+	lwsl_notice("Live wsi:                                   %8d\n", context->count_wsi_allocated);
+
+#if defined(LWS_WITH_STATS)
+	context->updated = 1;
+#endif
+
+	while (v) {
+		if (v->use_ssl && v->lserv_wsi) {
+			struct lws_context_per_thread *pt = &context->pt[(int)v->lserv_wsi->tsi];
+			struct lws_pollfd *pfd;
+
+			pfd = &pt->fds[v->lserv_wsi->position_in_fds_table];
+
+			lwsl_notice("  Listen port %d actual POLLIN:   %d\n",
+					v->listen_port, (int)pfd->events & LWS_POLLIN);
+		}
+
+		v = v->vhost_next;
+	}
+
+	for (n = 0; n < context->count_threads; n++) {
+		struct lws_context_per_thread *pt = &context->pt[n];
+		struct lws *wl;
+		int m = 0;
+
+		lwsl_notice("PT %d\n", n + 1);
+
+		lws_pt_lock(pt);
+
+		lwsl_notice("  AH in use / max:                  %d / %d\n",
+				pt->ah_count_in_use,
+				context->max_http_header_pool);
+
+		wl = pt->ah_wait_list;
+		while (wl) {
+			m++;
+			wl = wl->u.hdr.ah_wait_list;
+		}
+
+		lwsl_notice("  AH wait list count / actual:      %d / %d\n",
+				pt->ah_wait_list_length, m);
+
+		lws_pt_unlock(pt);
+	}
+
 	lwsl_notice("\n");
 }
 
diff --git a/lib/lws-plat-esp32.c b/lib/lws-plat-esp32.c
index b7548474..1d73f2fd 100644
--- a/lib/lws-plat-esp32.c
+++ b/lib/lws-plat-esp32.c
@@ -914,6 +914,7 @@ esp_err_t lws_esp32_event_passthru(void *ctx, system_event_t *event)
 	char slot[8];
 	nvs_handle nvh;
 	uint32_t use;
+	int n;
 
 	switch(event->event_id) {
 	case SYSTEM_EVENT_STA_START:
@@ -956,7 +957,8 @@ esp_err_t lws_esp32_event_passthru(void *ctx, system_event_t *event)
 		lws_gapss_to(LWS_GAPSS_STAT_HAPPY);
 
 #if !defined(CONFIG_LWS_IS_FACTORY_APPLICATION)
-		if (!mdns_init(TCPIP_ADAPTER_IF_STA, &lws_esp32.mdns)) {
+		n = mdns_init(TCPIP_ADAPTER_IF_STA, &lws_esp32.mdns);
+		if (!n) {
 			static char *txta[4];
 
 			mdns_set_hostname(lws_esp32.mdns, lws_esp32.hostname);
@@ -982,7 +984,7 @@ esp_err_t lws_esp32_event_passthru(void *ctx, system_event_t *event)
 						 (const char **)txta))
 				lwsl_notice("txt set failed\n");
 		} else
-			lwsl_err("unable to init mdns on STA\n");
+			lwsl_err("unable to init mdns on STA: %d\n", n);
 
 		mdns_query(lws_esp32.mdns, "_lwsgrmem", "_tcp", 0);
 		xTimerStart(mdns_timer, 0);
@@ -1236,8 +1238,7 @@ lws_esp32_wlan_start_station(void)
 	esp_wifi_set_auto_connect(1);
 	ESP_ERROR_CHECK( esp_wifi_connect());
 
-	if (mdns_init(TCPIP_ADAPTER_IF_STA, &lws_esp32.mdns))
-		lwsl_notice("mdns init failed\n");
+	lws_esp32_scan_timer_cb(NULL);
 }
 
 const esp_partition_t *
diff --git a/lib/parsers.c b/lib/parsers.c
index 2355bc10..c2834484 100644
--- a/lib/parsers.c
+++ b/lib/parsers.c
@@ -181,7 +181,7 @@ lws_header_table_attach(struct lws *wsi, int autoservice)
 
 	_lws_change_pollfd(wsi, 0, LWS_POLLIN, &pa);
 
-	lwsl_info("%s: wsi %p: ah %p: count %d (on exit)\n", __func__,
+	lwsl_notice("%s: did attach wsi %p: ah %p: count %d (on exit)\n", __func__,
 		  (void *)wsi, (void *)wsi->u.hdr.ah, pt->ah_count_in_use);
 
 	lws_pt_unlock(pt);
diff --git a/lib/private-libwebsockets.h b/lib/private-libwebsockets.h
index 1d86c584..8de93dc0 100644
--- a/lib/private-libwebsockets.h
+++ b/lib/private-libwebsockets.h
@@ -919,7 +919,7 @@ struct lws_context {
 	unsigned int being_destroyed1:1;
 	unsigned int requested_kill:1;
 	unsigned int protocol_init_done:1;
-
+	unsigned int ssl_gate_accepts:1;
 	/*
 	 * set to the Thread ID that's doing the service loop just before entry
 	 * to poll indicates service thread likely idling in poll()
diff --git a/lib/server.c b/lib/server.c
index f442b94a..762d628b 100644
--- a/lib/server.c
+++ b/lib/server.c
@@ -1872,9 +1872,13 @@ lws_adopt_descriptor_vhost(struct lws_vhost *vh, lws_adoption_type type,
 			new_wsi, n, new_wsi->user_space, NULL, 0))
 		goto fail;
 
-	if (type & LWS_ADOPT_HTTP)
-		if (!lws_header_table_attach(new_wsi, 0))
+	if (type & LWS_ADOPT_HTTP) {
+		if (!lws_header_table_attach(new_wsi, 0)) {
 			lwsl_debug("Attached ah immediately\n");
+		} else {
+			lwsl_notice("%s: waiting for ah\n", __func__);
+		}
+	}
 
 	return new_wsi;
 
diff --git a/lib/ssl.c b/lib/ssl.c
index d32e3a40..51254303 100644
--- a/lib/ssl.c
+++ b/lib/ssl.c
@@ -527,6 +527,10 @@ lws_gate_accepts(struct lws_context *context, int on)
 	struct lws_vhost *v = context->vhost_list;
 
 	lwsl_info("gating accepts %d\n", on);
+	context->ssl_gate_accepts = !on;
+#if defined(LWS_WITH_STATS)
+	context->updated = 1;
+#endif
 
 	while (v) {
 		if (v->use_ssl &&  v->lserv_wsi) /* gate ability to accept incoming connections */
@@ -558,6 +562,9 @@ lws_ssl_close(struct lws *wsi)
 			    wsi->context->simultaneous_ssl_restriction)
 		/* we made space and can do an accept */
 		lws_gate_accepts(wsi->context, 1);
+#if defined(LWS_WITH_STATS)
+	wsi->context->updated = 1;
+#endif
 
 	return 1; /* handled */
 }
@@ -605,6 +612,9 @@ lws_server_socket_service_ssl(struct lws *wsi, lws_sockfd_type accept_fd)
 		    ++context->simultaneous_ssl == context->simultaneous_ssl_restriction)
 			/* that was the last allowed SSL connection */
 			lws_gate_accepts(context, 0);
+#if defined(LWS_WITH_STATS)
+	context->updated = 1;
+#endif
 
 #if !defined(LWS_WITH_ESP32)
 		SSL_set_ex_data(wsi->ssl,