浏览代码

Prepare data for server status (Step 13/?) (See #243)

bel2125 8 年之前
父节点
当前提交
0675846cdd
共有 4 个文件被更改,包括 101 次插入31 次删除
  1. 10 9
      include/civetweb.h
  2. 18 18
      src/civetweb.c
  3. 2 1
      src/mod_duktape.inl
  4. 71 3
      src/mod_lua.inl

+ 10 - 9
include/civetweb.h

@@ -345,11 +345,10 @@ typedef int (*mg_request_handler)(struct mg_connection *conn, void *cbdata);
       ctx: server context
       uri: the URI (exact or pattern) for the handler
       handler: the callback handler to use when the URI is requested.
-               If NULL, an already registered handler for this URI will be
-   removed.
-               The URI used to remove a handler must match exactly the one used
-   to
-               register it (not only a pattern match).
+               If NULL, an already registered handler for this URI will
+               be removed.
+               The URI used to remove a handler must match exactly the
+               one used to register it (not only a pattern match).
       cbdata: the callback data to give to the handler when it is called. */
 CIVETWEB_API void mg_set_request_handler(struct mg_context *ctx,
                                          const char *uri,
@@ -724,8 +723,8 @@ CIVETWEB_API void mg_send_mime_file(struct mg_connection *conn,
      mime_type: Content-Type for file.  NULL will cause the type to be
                 looked up by the file extension.
      additional_headers: Additional custom header fields appended to the header.
-                         Each header must start with an X- to ensure it is not
-   included twice.
+                         Each header should start with an X-, to ensure it is
+                         not included twice.
                          NULL does not append anything.
 */
 CIVETWEB_API void mg_send_mime_file2(struct mg_connection *conn,
@@ -1137,8 +1136,9 @@ CIVETWEB_API int mg_get_response(struct mg_connection *conn,
         32  support Lua scripts and Lua server pages (USE_LUA is set)
         64  support server side JavaScript (USE_DUKTAPE is set)
        128  support caching (NO_CACHING not set)
+       256  support server statistics (USE_SERVER_STATS is set)
        The result is undefined, if bits are set that do not represent a
-       defined feature (currently: feature >= 256).
+       defined feature (currently: feature >= 512).
        The result is undefined, if no bit is set (feature == 0).
 
    Return:
@@ -1174,7 +1174,8 @@ CIVETWEB_API int mg_get_system_info(char *buffer, int buflen);
    Note:
      It is possible to determine the required buflen, by first calling this
      function with buffer = NULL and buflen = NULL. The required buflen is
-     one byte more than the returned value.
+     one byte more than the returned value. However, since the available
+     context information changes, you should allocate a few bytes more.
 */
 CIVETWEB_API int
 mg_get_context_info(const struct mg_context *ctx, char *buffer, int buflen);

+ 18 - 18
src/civetweb.c

@@ -860,7 +860,7 @@ mg_atomic_dec(volatile int *addr)
 
 #if defined(USE_SERVER_STATS)
 static int
-mg_atomic_add(volatile long long *addr, long long value)
+mg_atomic_add(volatile int64_t *addr, int64_t value)
 {
 	int ret;
 #if defined(_WIN32) && !defined(__SYMBIAN32__)
@@ -889,8 +889,8 @@ mg_atomic_add(volatile long long *addr, long long value)
 #if defined(USE_SERVER_STATS)
 
 struct mg_memory_stat {
-	volatile long long totalMemUsed;
-	volatile long long maxMemUsed;
+	volatile int64_t totalMemUsed;
+	volatile int64_t maxMemUsed;
 	volatile int blockCount;
 };
 
@@ -934,8 +934,8 @@ mg_malloc_ex(size_t size,
 	        "MEM: %p %5lu alloc   %7lu %4lu --- %s:%u\n",
 	        memory,
 	        (unsigned long)size,
-	        mstat->totalMemUsed,
-	        mstat->blockCount,
+	        (unsigned long)mstat->totalMemUsed,
+	        (unsigned long)mstat->blockCount,
 	        file,
 	        line);
 #if defined(_WIN32)
@@ -989,8 +989,8 @@ mg_free_ex(void *memory, const char *file, unsigned line)
 		        "MEM: %p %5lu free    %7lu %4lu --- %s:%u\n",
 		        memory,
 		        (unsigned long)size,
-		        mstat->totalMemUsed,
-		        mstat->blockCount,
+		        (unsigned long)mstat->totalMemUsed,
+		        (unsigned long)mstat->blockCount,
 		        file,
 		        line);
 #if defined(_WIN32)
@@ -1038,8 +1038,8 @@ mg_realloc_ex(void *memory,
 				        "MEM: %p %5lu r-free  %7lu %4lu --- %s:%u\n",
 				        memory,
 				        (unsigned long)oldsize,
-				        mstat->totalMemUsed,
-				        mstat->blockCount,
+				        (unsigned long)mstat->totalMemUsed,
+				        (unsigned long)mstat->blockCount,
 				        file,
 				        line);
 #if defined(_WIN32)
@@ -1054,8 +1054,8 @@ mg_realloc_ex(void *memory,
 				        "MEM: %p %5lu r-alloc %7lu %4lu --- %s:%u\n",
 				        memory,
 				        (unsigned long)newsize,
-				        mstat->totalMemUsed,
-				        mstat->blockCount,
+				        (unsigned long)mstat->totalMemUsed,
+				        (unsigned long)mstat->blockCount,
 				        file,
 				        line);
 #if defined(_WIN32)
@@ -2092,9 +2092,9 @@ struct mg_context {
 
 #if defined(USE_SERVER_STATS)
 	int active_connections;
-	int total_connections;
-	int total_requests;
 	int max_connections;
+	int64_t total_connections;
+	int64_t total_requests;
 	struct mg_memory_stat ctx_memory;
 #endif
 };
@@ -14317,7 +14317,7 @@ process_new_connection(struct mg_connection *conn)
 
 #if defined(USE_SERVER_STATS)
 		int mcon = mg_atomic_inc(&(conn->ctx->active_connections));
-		mg_atomic_inc(&(conn->ctx->total_connections));
+		mg_atomic_add(&(conn->ctx->total_connections), 1);
 		if (mcon > (conn->ctx->max_connections)) {
 			/* could use atomic compare exchange, but this
 			 * seems overkill for statistics data */
@@ -15918,8 +15918,8 @@ mg_get_context_info_impl(const struct mg_context *ctx, char *buffer, int buflen)
 		            sizeof(block),
 		            "\"memory\" : {%s"
 		            "\"blocks\" : %i%s"
-		            "\"used\" : %i%s"
-		            "\"maxUsed\" : %i%s"
+		            "\"used\" : %" INT64_FMT "%s"
+		            "\"maxUsed\" : %" INT64_FMT "%s"
 		            "},%s",
 		            eol,
 		            ms->blockCount,
@@ -15946,7 +15946,7 @@ mg_get_context_info_impl(const struct mg_context *ctx, char *buffer, int buflen)
 		            "\"connections\" : {%s"
 		            "\"active\" : %i%s"
 		            "\"maxActive\" : %i%s"
-		            "\"total\" : %i%s"
+		            "\"total\" : %" INT64_FMT "%s"
 		            "},%s",
 		            eol,
 		            ctx->active_connections,
@@ -15970,7 +15970,7 @@ mg_get_context_info_impl(const struct mg_context *ctx, char *buffer, int buflen)
 		            block,
 		            sizeof(block),
 		            "\"requests\" : {%s"
-		            "\"total\" : %i%s"
+		            "\"total\" : %" INT64_FMT "%s"
 		            "},%s",
 		            eol,
 		            ctx->total_requests,

+ 2 - 1
src/mod_duktape.inl

@@ -36,6 +36,7 @@ mg_duk_mem_realloc(void *udata, void *ptr, duk_size_t newsize)
 static void
 mg_duk_mem_free(void *udata, void *ptr)
 {
+	(void)udata;
 	mg_free(ptr);
 }
 
@@ -52,7 +53,7 @@ mg_duk_fatal_handler(duk_context *duk_ctx, duk_errcode_t code, const char *msg)
 	duk_get_prop_string(duk_ctx, -1, civetweb_conn_id);
 	conn = (struct mg_connection *)duk_to_pointer(duk_ctx, -1);
 
-	mg_cry(conn, "%s", msg);
+	mg_cry(conn, "JavaScript fatal (%u): %s", (unsigned)code, msg);
 }
 
 

+ 71 - 3
src/mod_lua.inl

@@ -1016,6 +1016,70 @@ lsp_random(lua_State *L)
 }
 
 
+/* mg.get_info */
+static int
+lsp_get_info(lua_State *L)
+{
+	int num_args = lua_gettop(L);
+	int type1;
+	const char *arg;
+	int len;
+	char *buf;
+
+	if (num_args == 1) {
+		type1 = lua_type(L, 1);
+		if (type1 == LUA_TSTRING) {
+			arg = lua_tostring(L, 1);
+			/* Get info according to argument */
+			if (!mg_strcasecmp(arg, "system")) {
+				/* Get system info */
+				len = mg_get_system_info(NULL, 0);
+				buf = mg_malloc(len + 64);
+				if (!buf) {
+					return luaL_error(L, "OOM in get_info() call");
+				}
+				len = mg_get_system_info(buf, len + 63);
+				lua_pushlstring(L, buf, len);
+				return 1;
+			}
+			if (!mg_strcasecmp(arg, "context")) {
+				/* Get context */
+				struct mg_context *ctx;
+				lua_pushlightuserdata(L, (void *)&lua_regkey_ctx);
+				lua_gettable(L, LUA_REGISTRYINDEX);
+				ctx = (struct mg_context *)lua_touserdata(L, -1);
+
+				/* Get context info for server context */
+				len = mg_get_context_info(ctx, NULL, 0);
+				buf = mg_malloc(len + 64);
+				if (!buf) {
+					return luaL_error(L, "OOM in get_info() call");
+				}
+				len = mg_get_context_info(ctx, buf, len + 63);
+				lua_pushlstring(L, buf, len);
+				return 1;
+			}
+			if (!mg_strcasecmp(arg, "common")) {
+				/* Get context info for NULL context */
+				len = mg_get_context_info(NULL, NULL, 0);
+				buf = mg_malloc(len + 64);
+				if (!buf) {
+					return luaL_error(L, "OOM in get_info() call");
+				}
+				len = mg_get_context_info(NULL, buf, len + 63);
+				lua_pushlstring(L, buf, len);
+				return 1;
+			}
+			return 0;
+		}
+	}
+
+	/* Syntax error */
+	return luaL_error(L, "invalid get_info() call");
+}
+
+
+/* UUID library and function pointer */
 union {
 	void *p;
 	void (*f)(unsigned char uuid[16]);
@@ -1406,7 +1470,7 @@ prepare_lua_request_info(struct mg_connection *conn, lua_State *L)
 }
 
 
-void
+static void
 civetweb_open_lua_libs(lua_State *L)
 {
 	{
@@ -1537,6 +1601,7 @@ prepare_lua_environment(struct mg_context *ctx,
 	reg_function(L, "base64_decode", lsp_base64_decode);
 	reg_function(L, "get_response_code_text", lsp_get_response_code_text);
 	reg_function(L, "random", lsp_random);
+	reg_function(L, "get_info", lsp_get_info);
 	if (pf_uuid_generate.f) {
 		reg_function(L, "uuid", lsp_uuid);
 	}
@@ -1979,7 +2044,7 @@ lua_websocket_close(struct mg_connection *conn, void *ws_arg)
 #endif
 
 
-lua_State *
+static lua_State *
 mg_prepare_lua_context_script(const char *file_name,
                               struct mg_context *ctx,
                               char *ebuf,
@@ -1989,6 +2054,8 @@ mg_prepare_lua_context_script(const char *file_name,
 	int lua_ret;
 	const char *lua_err_txt;
 
+	(void)ctx;
+
 	L = luaL_newstate();
 	if (L == NULL) {
 		mg_snprintf(NULL,
@@ -2003,7 +2070,8 @@ mg_prepare_lua_context_script(const char *file_name,
 
 	lua_ret = luaL_loadfile(L, file_name);
 	if (lua_ret != LUA_OK) {
-		/* Error when loading the file (e.g. file not found, out of memory, ...)
+		/* Error when loading the file (e.g. file not found,
+		 * out of memory, ...)
 		 */
 		lua_err_txt = lua_tostring(L, -1);
 		mg_snprintf(NULL,