Forráskód Böngészése

H2O: Switch the build system to Ninja (#6319)

Also clean up the sources and fix a couple of minor issues.
Anton Kirilov 4 éve
szülő
commit
1b068fbd24

+ 4 - 0
frameworks/C/h2o/README.md

@@ -6,6 +6,10 @@ This is a framework implementation using the [H2O](https://h2o.examp1e.net) HTTP
 
 [CMake](https://cmake.org), [H2O](https://h2o.examp1e.net), [libpq](https://www.postgresql.org), [mustache-c](https://github.com/x86-64/mustache-c), [OpenSSL](https://www.openssl.org), [YAJL](https://lloyd.github.io/yajl)
 
+## Test implementations
+
+The test implementations are located into the `src/handlers` directory.
+
 ## Performance tuning
 
 If the test environment changes, it will probably be necessary to tune some of the framework settings in order to achieve the best performance possible. The most significant parameter is the maximum number of database connections per thread, which is controlled by the `DB_CONN` variable in the `h2o.sh` script.

+ 4 - 3
frameworks/C/h2o/h2o.dockerfile

@@ -6,7 +6,7 @@ COPY ./ ./
 ENV DEBIAN_FRONTEND noninteractive
 RUN apt-get update && \
     apt-get install -yqq autoconf bison cmake curl file flex g++ git libnuma-dev libpq-dev \
-                         libssl-dev libtool libyajl-dev libz-dev make wget
+                         libssl-dev libtool libyajl-dev libz-dev make ninja-build wget
 
 ### Install mustache-c
 
@@ -37,8 +37,9 @@ RUN mkdir -p "${H2O_BUILD_DIR}/build" && \
     tar xz --strip-components=1 && \
     cd build && \
     cmake -DCMAKE_INSTALL_PREFIX="$H2O_PREFIX" -DCMAKE_C_FLAGS="-flto -march=native" \
-          -DCMAKE_AR=/usr/bin/gcc-ar -DCMAKE_RANLIB=/usr/bin/gcc-ranlib .. && \
-    make -j "$(nproc)" install && \
+          -DCMAKE_AR=/usr/bin/gcc-ar -DCMAKE_RANLIB=/usr/bin/gcc-ranlib -G Ninja .. && \
+    cmake --build . -j && \
+    cmake --install . && \
     cd ../.. && \
     rm -rf "$H2O_BUILD_DIR"
 

+ 4 - 6
frameworks/C/h2o/h2o.sh

@@ -2,7 +2,6 @@
 
 set -e
 
-CPU_COUNT=$(nproc)
 H2O_APP_PROFILE_PORT=54321
 H2O_APP_PROFILE_URL="http://127.0.0.1:$H2O_APP_PROFILE_PORT"
 SCRIPT_PATH=$(realpath "$0")
@@ -26,7 +25,7 @@ if [[ -z "$MUSTACHE_C_PREFIX" ]]; then
 fi
 
 # A hacky way to detect whether we are running in the physical hardware or the cloud environment.
-if [[ "$CPU_COUNT" -gt 16 ]]; then
+if [[ $(nproc) -gt 16 ]]; then
 	echo "Running h2o_app in the physical hardware environment."
 	DB_CONN=5
 else
@@ -38,8 +37,8 @@ build_h2o_app()
 {
 	cmake -DCMAKE_INSTALL_PREFIX="$H2O_APP_PREFIX" -DCMAKE_BUILD_TYPE=Release \
 	      -DCMAKE_PREFIX_PATH="${H2O_PREFIX};${MUSTACHE_C_PREFIX}" \
-	      -DCMAKE_C_FLAGS="-march=native $1" "$H2O_APP_SRC_ROOT"
-	make -j "$CPU_COUNT"
+	      -DCMAKE_C_FLAGS="-march=native $1" -G Ninja "$H2O_APP_SRC_ROOT"
+	cmake --build . --clean-first -j
 }
 
 run_curl()
@@ -77,10 +76,9 @@ install -d "$H2O_APP_BUILD_DIR"
 pushd "$H2O_APP_BUILD_DIR"
 build_h2o_app "-fprofile-generate"
 generate_profile_data
-make clean
 rm -f CMakeCache.txt
 build_h2o_app "-fprofile-use"
-make -j "$CPU_COUNT" install
+cmake --install .
 popd
 rm -rf "$H2O_APP_BUILD_DIR"
 echo "Maximum database connections per thread: $DB_CONN"

+ 4 - 0
frameworks/C/h2o/src/database.c

@@ -17,11 +17,15 @@
  OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
 
+#include <assert.h>
 #include <errno.h>
 #include <fcntl.h>
 #include <h2o.h>
+#include <stdbool.h>
+#include <stdint.h>
 #include <stdlib.h>
 #include <string.h>
+#include <unistd.h>
 #include <postgresql/libpq-fe.h>
 
 #include "database.h"

+ 0 - 1
frameworks/C/h2o/src/database.h

@@ -22,7 +22,6 @@
 #define DATABASE_H_
 
 #include <h2o.h>
-#include <inttypes.h>
 #include <stdint.h>
 #include <postgresql/libpq-fe.h>
 

+ 1 - 1
frameworks/C/h2o/src/error.h

@@ -22,7 +22,7 @@
 #define ERROR_H_
 
 #include <errno.h>
-#include <stdarg.h>
+#include <stdlib.h>
 #include <yajl/yajl_gen.h>
 
 #define CHECK_ERRNO(function, ...) \

+ 2 - 2
frameworks/C/h2o/src/event_loop.c

@@ -22,7 +22,6 @@
 #include <inttypes.h>
 #include <limits.h>
 #include <netdb.h>
-#include <stdarg.h>
 #include <stdbool.h>
 #include <stdint.h>
 #include <stdio.h>
@@ -39,7 +38,6 @@
 #include "event_loop.h"
 #include "global_data.h"
 #include "thread.h"
-#include "utility.h"
 
 #define DEFAULT_TCP_FASTOPEN_QUEUE_LEN 4096
 
@@ -66,6 +64,8 @@ static void accept_connection(h2o_socket_t *listener, const char *err)
 		if (!ctx->shutdown) {
 			size_t accepted = ctx->config->max_accept;
 
+			assert(accepted);
+
 			do {
 				h2o_socket_t * const sock = h2o_evloop_socket_accept(listener);
 

+ 1 - 0
frameworks/C/h2o/src/event_loop.h

@@ -23,6 +23,7 @@
 
 #include <h2o.h>
 #include <stdbool.h>
+#include <stddef.h>
 
 #include "global_data.h"
 

+ 1 - 0
frameworks/C/h2o/src/global_data.h

@@ -22,6 +22,7 @@
 #define GLOBAL_DATA_H_
 
 #include <h2o.h>
+#include <stddef.h>
 #include <stdint.h>
 #include <openssl/ssl.h>
 

+ 2 - 3
frameworks/C/h2o/src/handlers/fortune.c

@@ -19,7 +19,6 @@
 
 #include <assert.h>
 #include <ctype.h>
-#include <errno.h>
 #include <h2o.h>
 #include <mustache.h>
 #include <stdbool.h>
@@ -39,9 +38,9 @@
 #include "thread.h"
 #include "utility.h"
 
-#define ID_FIELD_NAME "id"
 #define FORTUNE_TABLE_NAME "Fortune"
 #define FORTUNE_QUERY "SELECT * FROM " FORTUNE_TABLE_NAME ";"
+#define ID_FIELD_NAME "id"
 #define MAX_IOVEC 64
 #define MESSAGE_FIELD_NAME "message"
 #define NEW_FORTUNE_ID "0"
@@ -438,7 +437,7 @@ void initialize_fortunes_handler(const config_t *config,
                                  h2o_access_log_filehandle_t *log_handle)
 {
 	mustache_template_t *template = NULL;
-	const size_t template_path_prefix_len = strlen(config->template_path);
+	const size_t template_path_prefix_len = config->template_path ? strlen(config->template_path) : 0;
 	char path[template_path_prefix_len + sizeof(TEMPLATE_PATH_SUFFIX)];
 
 	memcpy(path, config->template_path, template_path_prefix_len);

+ 9 - 7
frameworks/C/h2o/src/handlers/world.c

@@ -20,11 +20,13 @@
 #include <assert.h>
 #include <ctype.h>
 #include <h2o.h>
+#include <inttypes.h>
 #include <stdbool.h>
 #include <stddef.h>
 #include <stdint.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include <string.h>
 #include <arpa/inet.h>
 #include <h2o/cache.h>
 #include <postgresql/libpq-fe.h>
@@ -48,6 +50,8 @@
 #define IS_COMPLETED 8
 #define MAX_ID 10000
 #define MAX_QUERIES 500
+#define WORLD_TABLE_NAME "World"
+#define POPULATE_CACHE_QUERY "SELECT * FROM " WORLD_TABLE_NAME ";"
 #define QUERIES_PARAMETER "queries="
 #define RANDOM_NUM_KEY "randomNumber"
 
@@ -66,18 +70,11 @@
 	        2 * (sizeof(MKSTR(MAX_ID)) - 1) - 2 * (sizeof(PRIu32) - 1) - 2))
 
 #define USE_CACHE 2
-#define WORLD_TABLE_NAME "World"
-#define POPULATE_CACHE_QUERY "SELECT * FROM " WORLD_TABLE_NAME ";"
 #define WORLD_QUERY "SELECT * FROM " WORLD_TABLE_NAME " WHERE id = $1::integer;"
 
 typedef struct multiple_query_ctx_t multiple_query_ctx_t;
 typedef struct update_ctx_t update_ctx_t;
 
-typedef struct {
-	uint32_t id;
-	uint32_t random_number;
-} query_result_t;
-
 typedef struct {
 	thread_context_t *ctx;
 	db_query_param_t param;
@@ -92,6 +89,11 @@ typedef struct {
 	db_query_param_t param;
 } query_param_t;
 
+typedef struct {
+	uint32_t id;
+	uint32_t random_number;
+} query_result_t;
+
 typedef struct {
 	const char *id_pointer;
 	h2o_req_t *req;

+ 2 - 0
frameworks/C/h2o/src/main.c

@@ -30,9 +30,11 @@
 #include <sys/signalfd.h>
 #include <sys/time.h>
 
+#include "database.h"
 #include "error.h"
 #include "event_loop.h"
 #include "global_data.h"
+#include "list.h"
 #include "request_handler.h"
 #include "thread.h"
 #include "tls.h"

+ 1 - 3
frameworks/C/h2o/src/thread.c

@@ -19,11 +19,11 @@
 
 #define _GNU_SOURCE
 
-#include <errno.h>
 #include <h2o.h>
 #include <limits.h>
 #include <numaif.h>
 #include <pthread.h>
+#include <sched.h>
 #include <stdbool.h>
 #include <stdlib.h>
 #include <string.h>
@@ -37,8 +37,6 @@
 #include "global_data.h"
 #include "request_handler.h"
 #include "thread.h"
-#include "tls.h"
-#include "utility.h"
 
 static void *run_thread(void *arg);
 static void set_thread_memory_allocation_policy(size_t thread_num);

+ 1 - 2
frameworks/C/h2o/src/thread.h

@@ -21,15 +21,14 @@
 
 #define THREAD_H_
 
-#include <assert.h>
 #include <h2o.h>
 #include <pthread.h>
 #include <stdbool.h>
-#include <sys/types.h>
 
 #include "database.h"
 #include "event_loop.h"
 #include "global_data.h"
+#include "list.h"
 #include "handlers/request_handler_data.h"
 
 typedef struct thread_context_t thread_context_t;

+ 13 - 14
frameworks/C/h2o/src/utility.c

@@ -20,7 +20,6 @@
 #include <assert.h>
 #include <h2o.h>
 #include <errno.h>
-#include <inttypes.h>
 #include <limits.h>
 #include <stdbool.h>
 #include <stdint.h>
@@ -226,6 +225,19 @@ bool is_power_of_2(size_t x)
 	return !!x & !(x & (x - 1));
 }
 
+size_t round_up_to_power_of_2(size_t x)
+{
+	static_assert(sizeof(size_t) == sizeof(unsigned long),
+	              "The size_t type must have the same size as unsigned long.");
+
+	size_t ret = (SIZE_MAX ^ SIZE_MAX >> 1) >> __builtin_clzl(x);
+
+	if (x - ret)
+		ret <<= 1;
+
+	return ret;
+}
+
 // merge sort
 list_t *sort_list(list_t *head, int (*compare)(const list_t *, const list_t *))
 {
@@ -256,16 +268,3 @@ list_t *sort_list(list_t *head, int (*compare)(const list_t *, const list_t *))
 
 	return head;
 }
-
-size_t round_up_to_power_of_2(size_t x)
-{
-	static_assert(sizeof(size_t) == sizeof(unsigned long),
-	              "The size_t type must have the same size as unsigned long.");
-
-	size_t ret = (SIZE_MAX ^ SIZE_MAX >> 1) >> __builtin_clzl(x);
-
-	if (x - ret)
-		ret <<= 1;
-
-	return ret;
-}

+ 4 - 2
frameworks/C/h2o/src/utility.h

@@ -28,14 +28,16 @@
 
 #include "list.h"
 
+// Note that the parameter must be an actual array, and not an array that has decayed into a
+// pointer, for example.
 #define ARRAY_SIZE(a) (sizeof(a) / sizeof(*(a)))
 // mainly used to silence compiler warnings about unused function parameters
 #define IGNORE_FUNCTION_PARAMETER(p) ((void) (p))
 // Do not use the following MAX and MIN macros with parameters that have side effects.
 #define MAX(x, y) ((x) > (y) ? (x) : (y))
 #define MIN(x, y) ((x) < (y) ? (x) : (y))
-#define MKSTR(x) TOSTRING(x)
 #define TOSTRING(x) # x
+#define MKSTR(x) TOSTRING(x)
 #define YAJL_STRLIT(s) (const unsigned char *) (s), sizeof(s) - 1
 
 typedef struct {
@@ -49,8 +51,8 @@ json_generator_t *get_json_generator(list_t **pool, size_t *gen_num);
 size_t get_maximum_cache_line_size(void);
 uint32_t get_random_number(uint32_t max_rand, unsigned int *seed);
 bool is_power_of_2(size_t x);
+size_t round_up_to_power_of_2(size_t x);
 // stable sort
 list_t *sort_list(list_t *head, int (*compare)(const list_t *, const list_t *));
-size_t round_up_to_power_of_2(size_t x);
 
 #endif // UTILITY_H_