Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions build-static.sh
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,9 @@ if [ -n "${COMPRESS}" ] && [ -z "${DEBUG_SYMBOLS}" ] && [ "${os}" = "linux" ]; t
SPC_OPT_INSTALL_ARGS="${SPC_OPT_INSTALL_ARGS} upx"
fi

MTLS_CFLAGS="$(sh "${CURRENT_DIR}/mtls-cflags.sh")"
export CGO_CFLAGS="${CGO_CFLAGS} ${MTLS_CFLAGS}"

export SPC_DEFAULT_C_FLAGS="-fPIC -O2"
if [ -n "${DEBUG_SYMBOLS}" ]; then
SPC_CMD_VAR_PHP_MAKE_EXTRA_CFLAGS="${SPC_CMD_VAR_PHP_MAKE_EXTRA_CFLAGS} -fPIE -g"
Expand Down
2 changes: 1 addition & 1 deletion docs/performance.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ We strongly recommend changing these values. For best system stability, it is re
To find the right values, it's best to run load tests simulating real traffic.
[k6](https://k6.io) and [Gatling](https://gatling.io) are good tools for this.

To configure the number of threads, use the `num_threads` option of the `php_server` and `php` directives.
To configure the number of threads, use the `num_threads` option of the global `frankenphp` directive.
Comment thread
dunglas marked this conversation as resolved.
To change the number of workers, use the `num` option of the `worker` section of the `frankenphp` directive.

### `max_threads`
Expand Down
16 changes: 11 additions & 5 deletions frankenphp.c
Original file line number Diff line number Diff line change
Expand Up @@ -98,21 +98,27 @@ bool original_user_abort_setting = 0;
frankenphp_interned_strings_t frankenphp_strings = {0};
HashTable *main_thread_env = NULL;

__thread uintptr_t thread_index;
__thread bool is_worker_thread = false;
__thread HashTable *sandboxed_env = NULL;
#if defined(__ELF__) && defined(__GNUC__)
#define THREAD_LOCAL __thread __attribute__((tls_model("local-exec")))
#else
#define THREAD_LOCAL __thread
#endif

static THREAD_LOCAL uintptr_t thread_index;
static THREAD_LOCAL bool is_worker_thread = false;
static THREAD_LOCAL HashTable *sandboxed_env = NULL;
/* prepared_env holds entries from php(_server)'s `env KEY VAL`, exposed to
* getenv() and merged into $_ENV when 'E' is in variables_order. Separate from
* putenv() so those don't leak into $_ENV. */
__thread HashTable *prepared_env = NULL;
static THREAD_LOCAL HashTable *prepared_env = NULL;

/* Published via SG(server_context) so ext-parallel children, which inherit
* the parent's SG(server_context), can route SAPI callbacks back to the
* parent's thread_index instead of their zero-initialized TLS. */
typedef struct {
uintptr_t thread_index;
} frankenphp_server_ctx;
static __thread frankenphp_server_ctx frankenphp_local_server_ctx;
static THREAD_LOCAL frankenphp_server_ctx frankenphp_local_server_ctx;

static inline uintptr_t frankenphp_thread_index(void) {
frankenphp_server_ctx *ctx = (frankenphp_server_ctx *)SG(server_context);
Expand Down
2 changes: 1 addition & 1 deletion go.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
# Runs the go command with the proper Go and cgo flags.

GOFLAGS="$GOFLAGS -tags=nobadger,nomysql,nopgx" \
CGO_CFLAGS="$CGO_CFLAGS $(php-config --includes)" \
CGO_CFLAGS="$CGO_CFLAGS $(php-config --includes) $(sh "$(dirname "$0")/mtls-cflags.sh")" \
CGO_LDFLAGS="$CGO_LDFLAGS $(php-config --ldflags) $(php-config --libs)" \
go "$@"
6 changes: 6 additions & 0 deletions mtls-cflags.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/sh
# Prints -mtls-size=12 if the toolchain compiles and links local-exec TLS with it (excludes non-AArch64 and ld.lld).
Comment thread
dunglas marked this conversation as resolved.
d="$(mktemp -d)" || exit 0
printf '__thread __attribute__((tls_model("local-exec"))) int v;\nint main(void){v=1;return v;}\n' >"$d/p.c"
"${CC:-cc}" -O2 -mtls-size=12 "$d/p.c" -o "$d/p" >/dev/null 2>&1 && printf -- -mtls-size=12
rm -rf "$d"
Loading