Skip to content

Commit

Permalink
Add some changes required for win support
Browse files Browse the repository at this point in the history
This patch is not a complete support of windows. Its goal is to
port to master branch right now the current changes made to src/ to
support windows.

See here: #263
  • Loading branch information
NoiseByNorthwest committed Nov 29, 2024
1 parent 9f4d0e4 commit 2c0b651
Show file tree
Hide file tree
Showing 18 changed files with 251 additions and 109 deletions.
11 changes: 5 additions & 6 deletions src/php_spx.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,8 @@


#include <stdio.h>
#include <unistd.h>

#ifndef ZTS
#if ! defined(ZTS) && ! defined(_WIN32)
# define USE_SIGNAL
#endif

Expand Down Expand Up @@ -215,7 +214,7 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_spx_profiler_stop, 0, 0, 0)
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_INFO_EX(arginfo_spx_profiler_full_report_set_custom_metadata_str, 0, 0, 1)
#if PHP_API_VERSION >= 20151012
#if ZEND_MODULE_API_NO >= 20151012
ZEND_ARG_TYPE_INFO(0, customMetadataStr, IS_STRING, 0)
#else
ZEND_ARG_INFO(0, customMetadataStr)
Expand Down Expand Up @@ -437,7 +436,7 @@ static PHP_FUNCTION(spx_profiler_stop)
profiling_handler_stop();

if (context.profiling_handler.full_report_key[0]) {
#if PHP_API_VERSION >= 20151012
#if ZEND_MODULE_API_NO >= 20151012
RETURN_STRING(context.profiling_handler.full_report_key);
#else
RETURN_STRING(context.profiling_handler.full_report_key, 1);
Expand All @@ -448,13 +447,13 @@ static PHP_FUNCTION(spx_profiler_stop)
static PHP_FUNCTION(spx_profiler_full_report_set_custom_metadata_str)
{
char * custom_metadata_str;
#if PHP_API_VERSION >= 20151012
#if ZEND_MODULE_API_NO >= 20151012
size_t custom_metadata_str_len;
#else
int custom_metadata_str_len;
#endif

#if PHP_API_VERSION >= 20170718
#if ZEND_MODULE_API_NO >= 20170718
ZEND_PARSE_PARAMETERS_START(1, 1)
Z_PARAM_STRING(custom_metadata_str, custom_metadata_str_len)
ZEND_PARSE_PARAMETERS_END();
Expand Down
2 changes: 1 addition & 1 deletion src/php_spx.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
# error "Only Linux-based OSes, Apple MacOS and FreeBSD are supported"
#endif

#if !defined(__x86_64__) && !defined(__aarch64__)
#if (defined(_MSC_VER) && !(defined(_M_X64) || defined(_M_ARM64))) || (!defined(_MSC_VER) && !(defined(__x86_64__) || defined(__aarch64__)))
# error "Only x86-64 and ARM64 architectures are supported"
#endif

Expand Down
6 changes: 3 additions & 3 deletions src/spx_fmt.c
Original file line number Diff line number Diff line change
Expand Up @@ -190,21 +190,21 @@ void spx_fmt_row_print(const spx_fmt_row_t * row, spx_output_stream_t * output)
;

if (row->cells[i].ansi_fmt) {
spx_output_stream_printf(output, "\e[%sm", row->cells[i].ansi_fmt);
spx_output_stream_printf(output, "%c[%sm", 0x1b, row->cells[i].ansi_fmt);
}

snprintf(
format,
sizeof(format),
"%%-%lu.%lus",
"%%-%zu.%zus",
cell_width,
cell_width
);

spx_output_stream_printf(output, format, text);

if (row->cells[i].ansi_fmt) {
spx_output_stream_print(output, "\e[0m");
spx_output_stream_printf(output, "%c[0m", 0x1b);
}

spx_output_stream_print(output, " |");
Expand Down
4 changes: 3 additions & 1 deletion src/spx_hmap.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@
#ifndef SPX_HMAP_H_DEFINED
#define SPX_HMAP_H_DEFINED

#include <stdint.h>

typedef struct spx_hmap_t spx_hmap_t;
typedef struct spx_hmap_entry_t spx_hmap_entry_t;

typedef unsigned long (*spx_hmap_hash_key_func_t) (const void *);
typedef uint64_t (*spx_hmap_hash_key_func_t) (const void *);
typedef int (*spx_hmap_cmp_key_func_t) (const void *, const void *);

spx_hmap_t * spx_hmap_create(
Expand Down
47 changes: 30 additions & 17 deletions src/spx_php.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/


#include "main/php.h"
#include "main/SAPI.h"
#if defined(_WIN32) && ZEND_MODULE_API_NO >= 20170718
# include "win32/console.h"
#endif

/* _GNU_SOURCE is implicitly defined since PHP 8.2 https://github.com/php/php-src/pull/8807 */
#ifndef _GNU_SOURCE
Expand Down Expand Up @@ -84,14 +86,14 @@ static struct {

zend_op_array * (*zend_compile_file)(zend_file_handle * file_handle, int type TSRMLS_DC);
zend_op_array * (*zend_compile_string)(
#if PHP_API_VERSION >= 20200930
#if ZEND_MODULE_API_NO >= 20200930
zend_string * source_string,
const
#else
zval * source_string,
#endif
char * filename
#if PHP_API_VERSION >= 20210903
#if ZEND_MODULE_API_NO >= 20210903
, zend_compile_position position
#endif
TSRMLS_DC
Expand All @@ -103,13 +105,13 @@ static struct {

void (*zend_error_cb) (
int type,
#if PHP_API_VERSION >= 20210902
#if ZEND_MODULE_API_NO >= 20210902
zend_string *error_filename,
#else
const char *error_filename,
#endif
const uint error_lineno,
#if PHP_API_VERSION >= 20200930
#if ZEND_MODULE_API_NO >= 20200930
zend_string *message
#else
const char *format,
Expand Down Expand Up @@ -202,14 +204,14 @@ static void global_hook_execute_internal(

static zend_op_array * global_hook_zend_compile_file(zend_file_handle * file_handle, int type TSRMLS_DC);
static zend_op_array * global_hook_zend_compile_string(
#if PHP_API_VERSION >= 20200930
#if ZEND_MODULE_API_NO >= 20200930
zend_string * source_string,
const
#else
zval * source_string,
#endif
char * filename
#if PHP_API_VERSION >= 20210903
#if ZEND_MODULE_API_NO >= 20210903
, zend_compile_position position
#endif
TSRMLS_DC
Expand All @@ -221,13 +223,13 @@ static int global_hook_gc_collect_cycles(void);

static void global_hook_zend_error_cb(
int type,
#if PHP_API_VERSION >= 20210902
#if ZEND_MODULE_API_NO >= 20210902
zend_string *error_filename,
#else
const char *error_filename,
#endif
const uint error_lineno,
#if PHP_API_VERSION >= 20200930
#if ZEND_MODULE_API_NO >= 20200930
zend_string *message
#else
const char *format,
Expand All @@ -244,6 +246,17 @@ int spx_php_is_cli_sapi(void)
return 0 == strcmp(sapi_module.name, "cli");
}

int spx_php_are_ansi_sequences_supported(void)
{
return
spx_php_is_cli_sapi()
&& isatty(STDOUT_FILENO)
#if defined(_WIN32) && ZEND_MODULE_API_NO >= 20170718
&& php_win32_console_fileno_has_vt100(STDOUT_FILENO)
#endif
;
}

void spx_php_current_function(spx_php_function_t * function)
{
TSRMLS_FETCH();
Expand Down Expand Up @@ -1213,14 +1226,14 @@ static zend_op_array * global_hook_zend_compile_file(zend_file_handle * file_han
}

static zend_op_array * global_hook_zend_compile_string(
#if PHP_API_VERSION >= 20200930
#if ZEND_MODULE_API_NO >= 20200930
zend_string * source_string,
const
#else
zval * source_string,
#endif
char * filename
#if PHP_API_VERSION >= 20210903
#if ZEND_MODULE_API_NO >= 20210903
, zend_compile_position position
#endif
TSRMLS_DC
Expand All @@ -1229,7 +1242,7 @@ static zend_op_array * global_hook_zend_compile_string(
return ze_hooked_func.zend_compile_string(
source_string,
filename
#if PHP_API_VERSION >= 20210903
#if ZEND_MODULE_API_NO >= 20210903
, position
#endif
TSRMLS_CC
Expand All @@ -1249,7 +1262,7 @@ static zend_op_array * global_hook_zend_compile_string(
zend_op_array * op_array = ze_hooked_func.zend_compile_string(
source_string,
filename
#if PHP_API_VERSION >= 20210903
#if ZEND_MODULE_API_NO >= 20210903
, position
#endif
TSRMLS_CC
Expand Down Expand Up @@ -1311,13 +1324,13 @@ static int global_hook_gc_collect_cycles(void)

static void global_hook_zend_error_cb(
int type,
#if PHP_API_VERSION >= 20210902
#if ZEND_MODULE_API_NO >= 20210902
zend_string *error_filename,
#else
const char *error_filename,
#endif
const uint error_lineno,
#if PHP_API_VERSION >= 20200930
#if ZEND_MODULE_API_NO >= 20200930
zend_string *message
#else
const char *format,
Expand All @@ -1329,7 +1342,7 @@ static void global_hook_zend_error_cb(
type,
error_filename,
error_lineno,
#if PHP_API_VERSION >= 20200930
#if ZEND_MODULE_API_NO >= 20200930
message
#else
format,
Expand All @@ -1349,7 +1362,7 @@ static void global_hook_zend_error_cb(
type,
error_filename,
error_lineno,
#if PHP_API_VERSION >= 20200930
#if ZEND_MODULE_API_NO >= 20200930
message
#else
format,
Expand Down
8 changes: 6 additions & 2 deletions src/spx_php.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,15 @@
#ifndef SPX_PHP_H_DEFINED
#define SPX_PHP_H_DEFINED

#include "main/php.h"


/*
TSRMLS_* macros, which were deprecated since PHP7, are removed in PHP8.
More details here:
https://github.com/php/php-src/blob/PHP-8.0/UPGRADING.INTERNALS#L50
*/
#if PHP_API_VERSION >= 20200930
#if ZEND_MODULE_API_NO >= 20200930
#define TSRMLS_CC
#define TSRMLS_C
#define TSRMLS_DC
Expand All @@ -33,13 +36,14 @@
#endif

typedef struct {
unsigned long hash_code;
uint64_t hash_code;

const char * func_name;
const char * class_name;
} spx_php_function_t;

int spx_php_is_cli_sapi(void);
int spx_php_are_ansi_sequences_supported(void);

void spx_php_current_function(spx_php_function_t * function);

Expand Down
46 changes: 23 additions & 23 deletions src/spx_profiler_tracer.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,34 +32,34 @@

#define METRIC_VALUES_ZERO(m) \
do { \
SPX_METRIC_FOREACH(i, { \
(m).values[i] = 0; \
SPX_METRIC_FOREACH(i_, { \
(m).values[i_] = 0; \
}); \
} while (0)

#define METRIC_VALUES_ADD(a, b) \
do { \
SPX_METRIC_FOREACH(i, { \
(a).values[i] += (b).values[i]; \
SPX_METRIC_FOREACH(i_, { \
(a).values[i_] += (b).values[i_]; \
}); \
} while (0)

#define METRIC_VALUES_SUB(a, b) \
do { \
SPX_METRIC_FOREACH(i, { \
(a).values[i] -= (b).values[i]; \
}); \
#define METRIC_VALUES_SUB(a, b) \
do { \
SPX_METRIC_FOREACH(i_, { \
(a).values[i_] -= (b).values[i_]; \
}); \
} while (0)

#define METRIC_VALUES_MAX(a, b) \
do { \
SPX_METRIC_FOREACH(i, { \
(a).values[i] = \
(a).values[i] > (b).values[i] ? \
(a).values[i] : \
(b).values[i] \
; \
}); \
#define METRIC_VALUES_MAX(a, b) \
do { \
SPX_METRIC_FOREACH(i_, { \
(a).values[i_] = \
(a).values[i_] > (b).values[i_] ? \
(a).values[i_] : \
(b).values[i_] \
; \
}); \
} while (0)

typedef struct {
Expand Down Expand Up @@ -444,8 +444,8 @@ static void calibrate(tracing_profiler_t * profiler, const spx_php_function_t *

avg_noise = (spx_resource_stats_cpu_time() - start) / iter_count;

profiler->call_start_noise.values[SPX_METRIC_WALL_TIME] = avg_noise;
profiler->call_start_noise.values[SPX_METRIC_CPU_TIME] = avg_noise;
profiler->call_start_noise.values[SPX_METRIC_WALL_TIME] = (double) avg_noise;
profiler->call_start_noise.values[SPX_METRIC_CPU_TIME] = (double) avg_noise;

start = spx_resource_stats_cpu_time();

Expand All @@ -456,16 +456,16 @@ static void calibrate(tracing_profiler_t * profiler, const spx_php_function_t *

avg_noise = (spx_resource_stats_cpu_time() - start) / iter_count;

profiler->call_end_noise.values[SPX_METRIC_WALL_TIME] = avg_noise;
profiler->call_end_noise.values[SPX_METRIC_CPU_TIME] = avg_noise;
profiler->call_end_noise.values[SPX_METRIC_WALL_TIME] = (double) avg_noise;
profiler->call_end_noise.values[SPX_METRIC_CPU_TIME] = (double) avg_noise;

profiler->reporter = orig_reporter;
profiler->called = 0;
profiler->stack.depth = 0;
func_table_reset(&profiler->func_table);
}

static unsigned long func_table_hmap_hash_key(const void * v)
static uint64_t func_table_hmap_hash_key(const void * v)
{
return ((const spx_php_function_t *) v)->hash_code;
}
Expand Down
Loading

0 comments on commit 2c0b651

Please sign in to comment.