Skip to content

Commit

Permalink
Abuse macros to increase coverage a bit
Browse files Browse the repository at this point in the history
hopefully
  • Loading branch information
jbboehr committed Apr 5, 2024
1 parent c882c4a commit 4f9ebc6
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 32 deletions.
1 change: 1 addition & 0 deletions src/extension.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
#include "php_perfidious.h"
#include "functions.h"
#include "handle.h"
#include "private.h"

#define DEFAULT_METRICS "perf::PERF_COUNT_HW_CPU_CYCLES:u,perf::PERF_COUNT_HW_INSTRUCTIONS:u"

Expand Down
39 changes: 13 additions & 26 deletions src/handle.c
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,7 @@ zend_result perfidious_handle_reset(struct perfidious_handle *restrict handle)
{
int err;

if (UNEXPECTED(handle->metrics_count <= 0)) {
return FAILURE;
}
PERFIDIOUS_ASSERT_RETURN(handle->metrics_count > 0);

err = ioctl(handle->metrics[0].fd, PERF_EVENT_IOC_RESET, PERF_IOC_FLAG_GROUP);
HANDLE_IOCTL_ERROR(err);
Expand All @@ -108,9 +106,7 @@ zend_result perfidious_handle_enable(struct perfidious_handle *restrict handle)
{
int err;

if (UNEXPECTED(handle->metrics_count <= 0)) {
return FAILURE;
}
PERFIDIOUS_ASSERT_RETURN(handle->metrics_count > 0);

err = ioctl(handle->metrics[0].fd, PERF_EVENT_IOC_ENABLE, PERF_IOC_FLAG_GROUP);
HANDLE_IOCTL_ERROR(err);
Expand All @@ -127,9 +123,7 @@ zend_result perfidious_handle_disable(struct perfidious_handle *restrict handle)
{
int err;

if (UNEXPECTED(handle->metrics_count <= 0)) {
return FAILURE;
}
PERFIDIOUS_ASSERT_RETURN(handle->metrics_count > 0);

err = ioctl(handle->metrics[0].fd, PERF_EVENT_IOC_DISABLE, PERF_IOC_FLAG_GROUP);
HANDLE_IOCTL_ERROR(err);
Expand Down Expand Up @@ -179,11 +173,9 @@ zend_result perfidious_handle_read_raw(struct perfidious_handle *restrict handle
{
ssize_t bytes_read = read(handle->metrics[0].fd, buffer, size);

if (bytes_read == -1) {
return FAILURE;
} else {
return SUCCESS;
}
PERFIDIOUS_ASSERT_RETURN(bytes_read == (ssize_t) size);

return SUCCESS;
}

ZEND_HOT
Expand Down Expand Up @@ -237,11 +229,10 @@ zend_result perfidious_handle_read_to_array_with_times(
zend_long value_zl = 0;
zval tmp = {0};

if (UNEXPECTED(false == perfidious_uint64_t_to_zend_long(value->value, &value_zl))) {
PERFIDIOUS_ASSERT_RETURN_EX(perfidious_uint64_t_to_zend_long(value->value, &value_zl), {
zval_ptr_dtor(return_value);
ZVAL_UNDEF(return_value);
return FAILURE;
}
});

ZVAL_LONG(&tmp, value_zl);
zend_symtable_update(Z_ARRVAL_P(return_value), metric->name, &tmp);
Expand Down Expand Up @@ -273,21 +264,17 @@ zend_result perfidious_handle_read_to_result(struct perfidious_handle *handle, z
zval tmp = {0};

zend_result err = perfidious_handle_read_to_array_with_times(handle, &arr, &time_enabled, &time_running);
if (err == FAILURE) {
return FAILURE;
}
PERFIDIOUS_ASSERT_RETURN(err == SUCCESS);

zend_long time_enabled_zl = 0;
if (false == perfidious_uint64_t_to_zend_long(time_enabled, &time_enabled_zl)) {
PERFIDIOUS_ASSERT_RETURN_EX(perfidious_uint64_t_to_zend_long(time_enabled, &time_enabled_zl), {
zval_ptr_dtor(&arr);
return FAILURE;
}
});

zend_long time_running_zl = 0;
if (false == perfidious_uint64_t_to_zend_long(time_running, &time_running_zl)) {
PERFIDIOUS_ASSERT_RETURN_EX(perfidious_uint64_t_to_zend_long(time_running, &time_running_zl), {
zval_ptr_dtor(&arr);
return FAILURE;
}
});

object_init_ex(return_value, perfidious_read_result_ce);

Expand Down
4 changes: 1 addition & 3 deletions src/pmu_event_info.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,7 @@ perfidious_pmu_event_info_ctor(pfm_pmu_info_t *pmu_info, pfm_event_info_t *info,
char buf[512];
zval tmp = {0};

if (UNEXPECTED(FAILURE == object_init_ex(return_value, perfidious_pmu_event_info_ce))) {
return FAILURE;
}
PERFIDIOUS_ASSERT_RETURN(SUCCESS == object_init_ex(return_value, perfidious_pmu_event_info_ce));

size_t buf_len = snprintf(buf, sizeof(buf), "%s::%s", pmu_info->name, info->name);

Expand Down
6 changes: 3 additions & 3 deletions src/pmu_info.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@
#include <perfmon/pfmlib.h>
#include <Zend/zend_API.h>
#include <Zend/zend_exceptions.h>

#include "php_perfidious.h"
#include "private.h"

PERFIDIOUS_LOCAL zend_string *PERFIDIOUS_INTERNED_NAME;
PERFIDIOUS_LOCAL zend_string *PERFIDIOUS_INTERNED_DESC;
Expand All @@ -41,9 +43,7 @@ static inline zend_result perfidious_pmu_info_ctor(pfm_pmu_info_t *pmu_info, zva
{
zval tmp = {0};

if (UNEXPECTED(FAILURE == object_init_ex(return_value, perfidious_pmu_info_ce))) {
return FAILURE;
}
PERFIDIOUS_ASSERT_RETURN(SUCCESS == object_init_ex(return_value, perfidious_pmu_info_ce));

ZVAL_STRING(&tmp, pmu_info->name);
zend_update_property_ex(Z_OBJCE_P(return_value), Z_OBJ_P(return_value), PERFIDIOUS_INTERNED_NAME, &tmp);
Expand Down
12 changes: 12 additions & 0 deletions src/private.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,18 @@ PERFIDIOUS_LOCAL extern zend_string *PERFIDIOUS_INTERNED_VALUES;

// interned strings for handle

#define PERFIDIOUS_ASSERT_RETURN(expr) \
if (UNEXPECTED(!(expr))) { \
return FAILURE; \
}

#define PERFIDIOUS_ASSERT_RETURN_EX(expr, block) \
if (UNEXPECTED(!(expr))) { \
do \
block while (false); \
return FAILURE; \
}

static inline bool perfidious_uint64_t_to_zend_long(uint64_t from, zend_long *restrict to)
{
#if SIZEOF_UINT64_T >= SIZEOF_ZEND_LONG
Expand Down

0 comments on commit 4f9ebc6

Please sign in to comment.