2601 lines
86 KiB
C
2601 lines
86 KiB
C
/*
|
|
* cryo_reader.c — CryoReader: read .cryo snapshots without loading the
|
|
* graph into the PHP heap (records stay in C arrays), compute the
|
|
* multi-snapshot delta by allocation site, and answer graph queries
|
|
* (path, dominators, orphans, dupes, query, node). Every graph walk is
|
|
* iterative (explicit queues / stacks): no C recursion.
|
|
*/
|
|
#ifdef HAVE_CONFIG_H
|
|
# include "config.h"
|
|
#endif
|
|
|
|
#include "php_cryo.h"
|
|
#include "cryo_format.h"
|
|
#include "cryo_analysis.h"
|
|
#include "zend_exceptions.h"
|
|
#include "zend_interfaces.h"
|
|
#include "zend_smart_str.h"
|
|
#include "ext/spl/spl_array.h"
|
|
#include "ext/standard/php_string.h"
|
|
|
|
zend_class_entry *cryo_ce_reader;
|
|
static zend_object_handlers cryo_reader_handlers;
|
|
|
|
/*
|
|
* Byte counts as PHP computes them: an int while the sum fits zend_long, a
|
|
* float after (PHP's + switches to float on overflow). Sizes are u64 and a
|
|
* hostile file can make any sum overflow: with uint64_t arithmetic the
|
|
* results (and the orders that depend on them) would differ from the
|
|
* pure-PHP reader, and on WebAssembly zend_long is 32 bits.
|
|
*/
|
|
typedef struct { zend_long l; double d; bool is_d; } cnum;
|
|
|
|
static inline cnum cn_long(zend_long l) { cnum r = { l, 0.0, false }; return r; }
|
|
static inline cnum cn_dbl(double d) { cnum r = { 0, d, true }; return r; }
|
|
static inline double cn_d(cnum a) { return a.is_d ? a.d : (double) a.l; }
|
|
/* a u64 read from the file: int when it fits, else float (like PhpCryoReader::u64()) */
|
|
static inline cnum cn_u64(uint64_t v) { return v <= (uint64_t) ZEND_LONG_MAX ? cn_long((zend_long) v) : cn_dbl((double) v); }
|
|
|
|
static inline cnum cn_add(cnum a, cnum b)
|
|
{
|
|
zend_long r;
|
|
if (!a.is_d && !b.is_d) {
|
|
return __builtin_add_overflow(a.l, b.l, &r) ? cn_dbl((double) a.l + (double) b.l) : cn_long(r);
|
|
}
|
|
return cn_dbl(cn_d(a) + cn_d(b));
|
|
}
|
|
|
|
static inline cnum cn_sub(cnum a, cnum b)
|
|
{
|
|
zend_long r;
|
|
if (!a.is_d && !b.is_d) {
|
|
return __builtin_sub_overflow(a.l, b.l, &r) ? cn_dbl((double) a.l - (double) b.l) : cn_long(r);
|
|
}
|
|
return cn_dbl(cn_d(a) - cn_d(b));
|
|
}
|
|
|
|
static inline cnum cn_mul(cnum a, zend_long k)
|
|
{
|
|
zend_long r;
|
|
if (!a.is_d) {
|
|
return __builtin_mul_overflow(a.l, k, &r) ? cn_dbl((double) a.l * (double) k) : cn_long(r);
|
|
}
|
|
return cn_dbl(a.d * (double) k);
|
|
}
|
|
|
|
/* PHP's <=> (int / float compared as floats) */
|
|
static inline int cn_cmp(cnum a, cnum b)
|
|
{
|
|
if (!a.is_d && !b.is_d) {
|
|
return a.l < b.l ? -1 : (a.l > b.l ? 1 : 0);
|
|
}
|
|
double x = cn_d(a), y = cn_d(b);
|
|
return x < y ? -1 : (x > y ? 1 : 0);
|
|
}
|
|
|
|
/* (int) floor($a / $b), $b != 0, for a result known to be small (a percentage) */
|
|
static zend_long cn_div_floor(cnum a, cnum b)
|
|
{
|
|
if (!a.is_d && !b.is_d && b.l != 0 && !(b.l == -1 && a.l == ZEND_LONG_MIN) && a.l % b.l == 0) {
|
|
return a.l / b.l;
|
|
}
|
|
double q = floor(cn_d(a) / cn_d(b));
|
|
return zend_finite(q) && !zend_isnan(q) && ZEND_DOUBLE_FITS_LONG(q) ? (zend_long) q : 0;
|
|
}
|
|
|
|
static void add_assoc_cn(zval *arr, const char *key, cnum v)
|
|
{
|
|
if (v.is_d) {
|
|
add_assoc_double(arr, key, v.d);
|
|
} else {
|
|
add_assoc_long(arr, key, v.l);
|
|
}
|
|
}
|
|
|
|
typedef struct {
|
|
char *buf; /* whole file */
|
|
size_t len;
|
|
uint16_t version;
|
|
uint32_t php_version;
|
|
uint32_t epoch;
|
|
uint32_t nstr;
|
|
const char **str; /* pointers into buf */
|
|
uint32_t *str_len;
|
|
const cryo_node_rec *nodes; uint32_t nnodes;
|
|
const cryo_edge_rec *edges; uint32_t nedges;
|
|
const cryo_root_rec *roots; uint32_t nroots;
|
|
uint32_t *sites; uint32_t nsites; /* u32 arrays: aligned copies (owned) */
|
|
const cryo_hash_rec *hashes; uint32_t nhashes; /* optional section HASH */
|
|
uint32_t *writ; uint32_t nwrit; /* optional WRIT: write site per node */
|
|
uint32_t *nstk; uint32_t nnstk; /* optional NSTK: stack id per node */
|
|
const char *stks; /* optional STKS payload */
|
|
uint32_t *stk_off; uint32_t nstacks; /* byte offset of each stack (its nframes) */
|
|
/* cached analyses */
|
|
uint32_t *dist; /* BFS distance from the roots */
|
|
uint32_t *dom_order, *dom_idom; cnum *dom_ret; uint32_t dom_cnt;
|
|
/* adjacency (CSR, built on first use): edge indexes in file order */
|
|
uint32_t *out_off, *out_e; /* out_e[out_off[v] .. out_off[v+1]) */
|
|
uint32_t *in_off, *in_e;
|
|
} cryo_snap;
|
|
|
|
typedef struct {
|
|
cryo_snap snap;
|
|
bool loaded;
|
|
zend_object std;
|
|
} cryo_reader_obj;
|
|
|
|
static inline cryo_reader_obj *reader_from(zend_object *o)
|
|
{
|
|
return (cryo_reader_obj *)((char *) o - XtOffsetOf(cryo_reader_obj, std));
|
|
}
|
|
|
|
static void snap_free(cryo_snap *s)
|
|
{
|
|
if (s->buf) efree(s->buf);
|
|
if (s->str) efree(s->str);
|
|
if (s->str_len) efree(s->str_len);
|
|
if (s->out_off) efree(s->out_off);
|
|
if (s->out_e) efree(s->out_e);
|
|
if (s->in_off) efree(s->in_off);
|
|
if (s->in_e) efree(s->in_e);
|
|
if (s->stk_off) efree(s->stk_off);
|
|
if (s->sites) efree(s->sites);
|
|
if (s->writ) efree(s->writ);
|
|
if (s->nstk) efree(s->nstk);
|
|
if (s->dist) efree(s->dist);
|
|
if (s->dom_order) efree(s->dom_order);
|
|
if (s->dom_idom) efree(s->dom_idom);
|
|
if (s->dom_ret) efree(s->dom_ret);
|
|
memset(s, 0, sizeof(*s));
|
|
}
|
|
|
|
/* Parse a .cryo file. On error, throws CryoException and returns FAILURE. */
|
|
static zend_result snap_parse(cryo_snap *s, zend_string *data, const char *name);
|
|
|
|
static zend_result snap_load(cryo_snap *s, zend_string *file)
|
|
{
|
|
memset(s, 0, sizeof(*s));
|
|
php_stream *st = php_stream_open_wrapper(ZSTR_VAL(file), "rb", REPORT_ERRORS, NULL);
|
|
if (!st) {
|
|
zend_throw_exception_ex(cryo_ce_exception, 0, "Cannot open %s", ZSTR_VAL(file));
|
|
return FAILURE;
|
|
}
|
|
zend_string *data = php_stream_copy_to_mem(st, PHP_STREAM_COPY_ALL, 0);
|
|
php_stream_close(st);
|
|
return snap_parse(s, data, ZSTR_VAL(file));
|
|
}
|
|
|
|
/* Aligned copy of a section of u32 (count = slen / 4); NULL when empty. */
|
|
static uint32_t *snap_u32s(const char *p, uint64_t slen, uint32_t *count)
|
|
{
|
|
uint64_t n = MIN(slen / 4, (uint64_t) UINT32_MAX - 1);
|
|
*count = (uint32_t) n;
|
|
if (!n) {
|
|
return NULL;
|
|
}
|
|
uint32_t *a = safe_emalloc((size_t) n, sizeof(uint32_t), 0);
|
|
memcpy(a, p, (size_t) n * 4);
|
|
return a;
|
|
}
|
|
|
|
/* Parse a .cryo image (takes ownership of data, which may be NULL). */
|
|
static zend_result snap_parse(cryo_snap *s, zend_string *data, const char *name)
|
|
{
|
|
memset(s, 0, sizeof(*s));
|
|
if (!data || ZSTR_LEN(data) < 16 || memcmp(ZSTR_VAL(data), CRYO_MAGIC, 4) != 0) {
|
|
if (data) zend_string_release(data);
|
|
zend_throw_exception_ex(cryo_ce_exception, 0, "%s is not a .cryo file", name);
|
|
return FAILURE;
|
|
}
|
|
s->len = ZSTR_LEN(data);
|
|
s->buf = emalloc(s->len);
|
|
memcpy(s->buf, ZSTR_VAL(data), s->len);
|
|
zend_string_release(data);
|
|
|
|
memcpy(&s->version, s->buf + 4, 2);
|
|
memcpy(&s->php_version, s->buf + 8, 4);
|
|
memcpy(&s->epoch, s->buf + 12, 4);
|
|
if (s->version != CRYO_FORMAT_VERSION) {
|
|
zend_throw_exception_ex(cryo_ce_exception, 0, "Unsupported format version %u", s->version);
|
|
snap_free(s);
|
|
return FAILURE;
|
|
}
|
|
|
|
/* Hostile files: every count is checked against the bytes really present,
|
|
* a section that appears twice is read once (the first one wins), u32
|
|
* arrays are copied (the file gives no alignment). */
|
|
enum { SEEN_STRS = 1, SEEN_NODE = 2, SEEN_EDGE = 4, SEEN_ROOT = 8, SEEN_SITE = 16,
|
|
SEEN_HASH = 32, SEEN_WRIT = 64, SEEN_NSTK = 128, SEEN_STKS = 256 };
|
|
unsigned seen = 0;
|
|
#define FIRST(bit) (!(seen & (bit)) && (seen |= (bit)))
|
|
size_t off = 16;
|
|
while (off + 12 <= s->len) {
|
|
const char *tag = s->buf + off;
|
|
uint64_t slen;
|
|
memcpy(&slen, s->buf + off + 4, 8);
|
|
off += 12;
|
|
if (slen > s->len - off) {
|
|
zend_throw_exception(cryo_ce_exception, "Truncated section", 0);
|
|
snap_free(s);
|
|
return FAILURE;
|
|
}
|
|
const char *p = s->buf + off;
|
|
|
|
if (memcmp(tag, CRYO_SEC_STRS, 4) == 0 && FIRST(SEEN_STRS)) {
|
|
uint32_t count = 0;
|
|
if (slen >= 4) memcpy(&count, p, 4);
|
|
/* each string takes 4 bytes at least */
|
|
uint32_t room = slen >= 4 ? (uint32_t) MIN((slen - 4) / 4, (uint64_t) UINT32_MAX) : 0;
|
|
if (count > room) count = room;
|
|
s->str = safe_emalloc(count ? count : 1, sizeof(char *), 0);
|
|
s->str_len = safe_emalloc(count ? count : 1, sizeof(uint32_t), 0);
|
|
size_t q = 4;
|
|
uint32_t i;
|
|
for (i = 0; i < count; i++) {
|
|
uint32_t l;
|
|
if (q + 4 > slen) break;
|
|
memcpy(&l, p + q, 4);
|
|
q += 4;
|
|
if (l > slen - q) break;
|
|
s->str[i] = p + q;
|
|
s->str_len[i] = l;
|
|
q += l;
|
|
}
|
|
s->nstr = i; /* strings really present */
|
|
} else if (memcmp(tag, CRYO_SEC_NODE, 4) == 0 && FIRST(SEEN_NODE)) {
|
|
s->nodes = (const cryo_node_rec *) p; s->nnodes = (uint32_t) MIN(slen / sizeof(cryo_node_rec), (uint64_t) UINT32_MAX - 1);
|
|
} else if (memcmp(tag, CRYO_SEC_EDGE, 4) == 0 && FIRST(SEEN_EDGE)) {
|
|
s->edges = (const cryo_edge_rec *) p; s->nedges = (uint32_t) MIN(slen / sizeof(cryo_edge_rec), (uint64_t) UINT32_MAX - 1);
|
|
} else if (memcmp(tag, CRYO_SEC_ROOT, 4) == 0 && FIRST(SEEN_ROOT)) {
|
|
s->roots = (const cryo_root_rec *) p; s->nroots = (uint32_t) MIN(slen / sizeof(cryo_root_rec), (uint64_t) UINT32_MAX - 1);
|
|
} else if (memcmp(tag, CRYO_SEC_SITE, 4) == 0 && FIRST(SEEN_SITE)) {
|
|
s->sites = snap_u32s(p, slen, &s->nsites);
|
|
} else if (memcmp(tag, CRYO_SEC_HASH, 4) == 0 && FIRST(SEEN_HASH)) {
|
|
s->hashes = (const cryo_hash_rec *) p; s->nhashes = (uint32_t) MIN(slen / sizeof(cryo_hash_rec), (uint64_t) UINT32_MAX - 1);
|
|
} else if (memcmp(tag, CRYO_SEC_WRIT, 4) == 0 && FIRST(SEEN_WRIT)) {
|
|
s->writ = snap_u32s(p, slen, &s->nwrit);
|
|
} else if (memcmp(tag, CRYO_SEC_NSTK, 4) == 0 && FIRST(SEEN_NSTK)) {
|
|
s->nstk = snap_u32s(p, slen, &s->nnstk);
|
|
} else if (memcmp(tag, CRYO_SEC_STKS, 4) == 0 && FIRST(SEEN_STKS)) {
|
|
uint32_t count = 0;
|
|
if (slen >= 4) memcpy(&count, p, 4);
|
|
uint32_t room = slen >= 4 ? (uint32_t) MIN((slen - 4) / 4, (uint64_t) UINT32_MAX) : 0;
|
|
if (count > room) count = room;
|
|
s->stks = p;
|
|
s->stk_off = safe_emalloc(count ? count : 1, sizeof(uint32_t), 0);
|
|
size_t q = 4;
|
|
for (uint32_t i = 0; i < count; i++) {
|
|
uint32_t nf;
|
|
if (q + 4 > slen) break;
|
|
memcpy(&nf, p + q, 4);
|
|
if (nf > (slen - q - 4) / 4) break;
|
|
s->stk_off[s->nstacks++] = (uint32_t) q;
|
|
q += 4 + (size_t) nf * 4;
|
|
}
|
|
}
|
|
/* unknown sections are skipped */
|
|
off += slen;
|
|
}
|
|
#undef FIRST
|
|
return SUCCESS;
|
|
}
|
|
|
|
static zend_string *snap_str(const cryo_snap *s, uint32_t idx)
|
|
{
|
|
if (idx == CRYO_NONE || idx >= s->nstr || !s->str[idx]) {
|
|
return ZSTR_EMPTY_ALLOC();
|
|
}
|
|
return zend_string_init(s->str[idx], s->str_len[idx], 0);
|
|
}
|
|
|
|
static const char *root_kinds[] = { "?", "global", "static_prop", "frame", "orphan", "static_var" };
|
|
static const char *node_types[] = { "?", "array", "object", "string", "ref" };
|
|
#define ROOT_KIND(k) root_kinds[(k) <= 5 ? (k) : 0]
|
|
#define NODE_TYPE(t) node_types[(t) <= 4 ? (t) : 0]
|
|
|
|
/* u64 as int when it fits zend_long (32 bits under WebAssembly), else float */
|
|
static void add_assoc_u64(zval *arr, const char *key, uint64_t v)
|
|
{
|
|
if (v <= (uint64_t) ZEND_LONG_MAX) {
|
|
add_assoc_long(arr, key, (zend_long) v);
|
|
} else {
|
|
add_assoc_double(arr, key, (double) v);
|
|
}
|
|
}
|
|
|
|
static zend_string *snap_site(const cryo_snap *s, uint32_t site)
|
|
{
|
|
if (site == CRYO_NONE || site >= s->nsites) {
|
|
return zend_string_init("(unknown)", 9, 0);
|
|
}
|
|
return snap_str(s, s->sites[site]);
|
|
}
|
|
|
|
/* Last write site of node v, NULL if unknown (no WRIT section or no write) */
|
|
static zend_string *snap_write(const cryo_snap *s, uint32_t v)
|
|
{
|
|
if (v >= s->nwrit || s->writ[v] == CRYO_NONE || s->writ[v] >= s->nsites) {
|
|
return NULL;
|
|
}
|
|
return snap_str(s, s->sites[s->writ[v]]);
|
|
}
|
|
|
|
/* Frames of the allocation stack of node v: count, and the site ids */
|
|
static uint32_t snap_stack(const cryo_snap *s, uint32_t v, const char **frames)
|
|
{
|
|
if (v >= s->nnstk || s->nstk[v] == CRYO_NONE || s->nstk[v] >= s->nstacks) {
|
|
return 0;
|
|
}
|
|
uint32_t nf;
|
|
memcpy(&nf, s->stks + s->stk_off[s->nstk[v]], 4);
|
|
*frames = s->stks + s->stk_off[s->nstk[v]] + 4;
|
|
return nf;
|
|
}
|
|
|
|
static zend_string *snap_frame(const cryo_snap *s, const char *frames, uint32_t i)
|
|
{
|
|
uint32_t site;
|
|
memcpy(&site, frames + (size_t) i * 4, 4);
|
|
return snap_site(s, site);
|
|
}
|
|
|
|
/* Stack of node v as one string, frames joined by " <- " (innermost
|
|
* first), "(unknown)" when there is none. */
|
|
static zend_string *snap_stack_str(const cryo_snap *s, uint32_t v)
|
|
{
|
|
const char *frames = NULL;
|
|
uint32_t nf = snap_stack(s, v, &frames);
|
|
if (!nf) {
|
|
return zend_string_init("(unknown)", 9, 0);
|
|
}
|
|
smart_str out = {0};
|
|
for (uint32_t i = 0; i < nf; i++) {
|
|
if (i) smart_str_appends(&out, " <- ");
|
|
zend_string *f = snap_frame(s, frames, i);
|
|
smart_str_append(&out, f);
|
|
zend_string_release(f);
|
|
}
|
|
smart_str_0(&out);
|
|
return out.s;
|
|
}
|
|
|
|
/* aggregation key of node v for top() / delta() */
|
|
enum { BY_CLASS, BY_SITE, BY_STACK, BY_WRITE };
|
|
|
|
static zend_string *snap_key(const cryo_snap *s, uint32_t v, int by)
|
|
{
|
|
const cryo_node_rec *n = &s->nodes[v];
|
|
switch (by) {
|
|
case BY_SITE: return snap_site(s, n->site);
|
|
case BY_STACK: return snap_stack_str(s, v);
|
|
case BY_WRITE: {
|
|
zend_string *w = snap_write(s, v);
|
|
return w ? w : zend_string_init("(unknown)", 9, 0);
|
|
}
|
|
default: return snap_str(s, n->class_str);
|
|
}
|
|
}
|
|
|
|
/* graph analyses, defined below */
|
|
static void snap_class_retained(cryo_snap *s, cnum *cls_ret);
|
|
|
|
/* ------------------------------------------------------------------------ */
|
|
/* Object plumbing */
|
|
/* ------------------------------------------------------------------------ */
|
|
|
|
static zend_object *reader_create(zend_class_entry *ce)
|
|
{
|
|
cryo_reader_obj *r = zend_object_alloc(sizeof(cryo_reader_obj), ce);
|
|
zend_object_std_init(&r->std, ce);
|
|
object_properties_init(&r->std, ce);
|
|
r->std.handlers = &cryo_reader_handlers;
|
|
return &r->std;
|
|
}
|
|
|
|
static void reader_free(zend_object *o)
|
|
{
|
|
cryo_reader_obj *r = reader_from(o);
|
|
if (r->loaded) {
|
|
snap_free(&r->snap);
|
|
}
|
|
zend_object_std_dtor(o);
|
|
}
|
|
|
|
#define READER_THIS() reader_from(Z_OBJ_P(ZEND_THIS))
|
|
#define REQUIRE_LOADED(r) do { if (!(r)->loaded) { \
|
|
zend_throw_exception(cryo_ce_exception, "Use CryoReader::open()", 0); RETURN_THROWS(); } } while (0)
|
|
|
|
/* ------------------------------------------------------------------------ */
|
|
/* Methods */
|
|
/* ------------------------------------------------------------------------ */
|
|
|
|
ZEND_METHOD(CryoReader, __construct)
|
|
{
|
|
ZEND_PARSE_PARAMETERS_NONE();
|
|
zend_throw_exception(cryo_ce_exception, "Use CryoReader::open()", 0);
|
|
}
|
|
|
|
ZEND_METHOD(CryoReader, open)
|
|
{
|
|
zend_string *file;
|
|
ZEND_PARSE_PARAMETERS_START(1, 1)
|
|
Z_PARAM_PATH_STR(file)
|
|
ZEND_PARSE_PARAMETERS_END();
|
|
|
|
object_init_ex(return_value, cryo_ce_reader);
|
|
cryo_reader_obj *r = reader_from(Z_OBJ_P(return_value));
|
|
if (snap_load(&r->snap, file) == FAILURE) {
|
|
zval_ptr_dtor(return_value);
|
|
ZVAL_UNDEF(return_value); /* the VM would release it a second time */
|
|
RETURN_THROWS();
|
|
}
|
|
r->loaded = true;
|
|
}
|
|
|
|
ZEND_METHOD(CryoReader, info)
|
|
{
|
|
ZEND_PARSE_PARAMETERS_NONE();
|
|
cryo_reader_obj *r = READER_THIS();
|
|
REQUIRE_LOADED(r);
|
|
cryo_snap *s = &r->snap;
|
|
|
|
cnum bytes = cn_long(0);
|
|
uint32_t objects = 0, tracked = 0, orphans = 0;
|
|
for (uint32_t i = 0; i < s->nnodes; i++) {
|
|
bytes = cn_add(bytes, cn_u64(s->nodes[i].size));
|
|
if (s->nodes[i].type == CRYO_NODE_OBJECT) {
|
|
objects++;
|
|
if (s->nodes[i].site != CRYO_NONE) tracked++;
|
|
}
|
|
}
|
|
for (uint32_t i = 0; i < s->nroots; i++) {
|
|
if (s->roots[i].kind == CRYO_ROOT_ORPHAN) orphans++;
|
|
}
|
|
|
|
array_init(return_value);
|
|
add_assoc_long(return_value, "format", s->version);
|
|
add_assoc_long(return_value, "php_version_id", s->php_version);
|
|
add_assoc_long(return_value, "epoch", s->epoch);
|
|
add_assoc_long(return_value, "nodes", s->nnodes);
|
|
add_assoc_long(return_value, "edges", s->nedges);
|
|
add_assoc_long(return_value, "roots", s->nroots);
|
|
add_assoc_long(return_value, "objects", objects);
|
|
add_assoc_long(return_value, "objects_with_site", tracked);
|
|
add_assoc_long(return_value, "orphans", orphans);
|
|
add_assoc_long(return_value, "sites", s->nsites);
|
|
add_assoc_cn(return_value, "bytes", bytes);
|
|
}
|
|
|
|
typedef struct { zend_string *key; zend_long count; cnum bytes; uint32_t order; cnum retained; } agg_row;
|
|
|
|
static int agg_cmp(const void *a, const void *b)
|
|
{
|
|
const agg_row *x = a, *y = b;
|
|
int c = cn_cmp(y->retained, x->retained);
|
|
if (c) return c;
|
|
if ((c = cn_cmp(y->bytes, x->bytes))) return c;
|
|
if (x->count != y->count) return x->count < y->count ? 1 : -1;
|
|
return x->order < y->order ? -1 : (x->order > y->order ? 1 : 0); /* stable, like usort() */
|
|
}
|
|
|
|
/* top(string $by = 'class', int $limit = 20): list of [key, count, bytes] */
|
|
ZEND_METHOD(CryoReader, top)
|
|
{
|
|
zend_string *by = NULL;
|
|
zend_long limit = 20;
|
|
ZEND_PARSE_PARAMETERS_START(0, 2)
|
|
Z_PARAM_OPTIONAL
|
|
Z_PARAM_STR(by)
|
|
Z_PARAM_LONG(limit)
|
|
ZEND_PARSE_PARAMETERS_END();
|
|
cryo_reader_obj *r = READER_THIS();
|
|
REQUIRE_LOADED(r);
|
|
cryo_snap *s = &r->snap;
|
|
int mode = BY_CLASS;
|
|
const char *key_name = "class";
|
|
bool retained = !by || zend_string_equals_literal(by, "retained");
|
|
if (by && !retained && !zend_string_equals_literal(by, "class")) {
|
|
if (zend_string_equals_literal(by, "site")) { mode = BY_SITE; key_name = "site"; }
|
|
else if (zend_string_equals_literal(by, "stack")) { mode = BY_STACK; key_name = "stack"; }
|
|
else if (zend_string_equals_literal(by, "write")) { mode = BY_WRITE; key_name = "write"; }
|
|
else {
|
|
zend_argument_value_error(1, "must be \"retained\", \"class\", \"site\", \"stack\" or \"write\"");
|
|
RETURN_THROWS();
|
|
}
|
|
}
|
|
|
|
cnum *cls_ret = NULL;
|
|
if (retained) {
|
|
cls_ret = ecalloc(s->nstr ? s->nstr : 1, sizeof(cnum)); /* all zeros = int 0 */
|
|
snap_class_retained(s, cls_ret);
|
|
}
|
|
|
|
HashTable agg;
|
|
zend_hash_init(&agg, 64, NULL, NULL, 0);
|
|
agg_row *rows = NULL; uint32_t nrows = 0, cap = 0;
|
|
|
|
for (uint32_t i = 0; i < s->nnodes; i++) {
|
|
const cryo_node_rec *n = &s->nodes[i];
|
|
if (n->type != CRYO_NODE_OBJECT) continue;
|
|
zend_string *key = snap_key(s, i, mode);
|
|
zval *slot = zend_hash_find(&agg, key);
|
|
uint32_t at;
|
|
if (slot) {
|
|
at = (uint32_t) Z_LVAL_P(slot);
|
|
zend_string_release(key);
|
|
} else {
|
|
if (nrows == cap) { cap = cap ? cap * 2 : 64; rows = erealloc(rows, cap * sizeof(agg_row)); }
|
|
at = nrows++;
|
|
rows[at].key = key; rows[at].count = 0; rows[at].bytes = cn_long(0); rows[at].order = at;
|
|
rows[at].retained = cls_ret && n->class_str < s->nstr ? cls_ret[n->class_str] : cn_long(0);
|
|
zval z; ZVAL_LONG(&z, at);
|
|
zend_hash_add_new(&agg, key, &z);
|
|
}
|
|
rows[at].count++;
|
|
rows[at].bytes = cn_add(rows[at].bytes, cn_u64(n->size));
|
|
}
|
|
if (nrows) qsort(rows, nrows, sizeof(agg_row), agg_cmp);
|
|
|
|
array_init(return_value);
|
|
for (uint32_t i = 0; i < nrows; i++) {
|
|
if ((zend_long) i < limit) {
|
|
zval row;
|
|
array_init(&row);
|
|
add_assoc_str(&row, key_name, zend_string_copy(rows[i].key));
|
|
add_assoc_long(&row, "count", rows[i].count);
|
|
add_assoc_cn(&row, "bytes", rows[i].bytes);
|
|
if (retained) add_assoc_cn(&row, "retained", rows[i].retained);
|
|
add_next_index_zval(return_value, &row);
|
|
}
|
|
zend_string_release(rows[i].key);
|
|
}
|
|
if (rows) efree(rows);
|
|
if (cls_ret) efree(cls_ret);
|
|
zend_hash_destroy(&agg);
|
|
}
|
|
|
|
/* roots(): list of [kind, name, node] — the entry points of the graph */
|
|
ZEND_METHOD(CryoReader, roots)
|
|
{
|
|
ZEND_PARSE_PARAMETERS_NONE();
|
|
cryo_reader_obj *r = READER_THIS();
|
|
REQUIRE_LOADED(r);
|
|
array_init(return_value);
|
|
for (uint32_t i = 0; i < r->snap.nroots; i++) {
|
|
const cryo_root_rec *rt = &r->snap.roots[i];
|
|
zval row;
|
|
array_init(&row);
|
|
add_assoc_string(&row, "kind", ROOT_KIND(rt->kind));
|
|
add_assoc_str(&row, "name", snap_str(&r->snap, rt->name_str));
|
|
add_assoc_long(&row, "node", rt->node);
|
|
add_next_index_zval(return_value, &row);
|
|
}
|
|
}
|
|
|
|
/*
|
|
* delta(array $files, array $opts = []): CryoDelta — compare N snapshots of
|
|
* the same process.
|
|
*
|
|
* For each interval i (snapshot i-1 -> i), the cohort born in that interval
|
|
* is the set of objects whose birth epoch is in (epoch[i-1], epoch[i]].
|
|
* We report, per key (site, class or stack):
|
|
* new_per_interval[i-1] = cohort i alive at snapshot i
|
|
* survivors = objects born after snapshot 0 still alive at the last one
|
|
* survival = survivors / sum(new_per_interval)
|
|
* growing = true if new_per_interval > 0 and the survivors of
|
|
* every cohort are still there (no cohort was freed)
|
|
* hooked_by = most frequent write site of the survivors (null if none)
|
|
* Rows: growing first, then survivors desc (stable).
|
|
*/
|
|
typedef struct {
|
|
zend_string *key;
|
|
zend_long *born; /* per interval: cohort alive at end of its interval */
|
|
zend_long *left; /* per interval: cohort alive at the last snapshot */
|
|
cnum bytes_last;
|
|
HashTable hooks; /* write site -> count, first-seen order */
|
|
} delta_row;
|
|
|
|
/* Stable sort key: k1 desc, k2 desc, then first-seen order (like usort()).
|
|
* qsort, not an insertion sort: a hostile file can give 10^5+ rows. */
|
|
typedef struct { zend_long k1, k2; uint32_t idx; } sort_key;
|
|
|
|
static int sort_key_cmp(const void *a, const void *b)
|
|
{
|
|
const sort_key *x = a, *y = b;
|
|
if (x->k1 != y->k1) return x->k1 < y->k1 ? 1 : -1;
|
|
if (x->k2 != y->k2) return x->k2 < y->k2 ? 1 : -1;
|
|
return x->idx < y->idx ? -1 : (x->idx > y->idx ? 1 : 0);
|
|
}
|
|
|
|
static void delta_rows(zval *out, cryo_snap *snaps, uint32_t n, int mode)
|
|
{
|
|
static const char *key_names[] = { "class", "site", "stack", "write" };
|
|
uint32_t nint = n - 1;
|
|
HashTable idx;
|
|
zend_hash_init(&idx, 64, NULL, NULL, 0);
|
|
delta_row *rows = NULL; uint32_t nrows = 0, cap = 0;
|
|
|
|
#define ROW_FOR(snapp, node_idx, out_at) do { \
|
|
zend_string *k_ = snap_key((snapp), (node_idx), mode); \
|
|
zval *z_ = zend_hash_find(&idx, k_); \
|
|
if (z_) { (out_at) = (uint32_t) Z_LVAL_P(z_); zend_string_release(k_); } \
|
|
else { \
|
|
if (nrows == cap) { cap = cap ? cap * 2 : 32; rows = erealloc(rows, cap * sizeof(delta_row)); } \
|
|
(out_at) = nrows++; \
|
|
rows[out_at].key = k_; \
|
|
rows[out_at].born = ecalloc(nint, sizeof(zend_long)); \
|
|
rows[out_at].left = ecalloc(nint, sizeof(zend_long)); \
|
|
rows[out_at].bytes_last = cn_long(0); \
|
|
zend_hash_init(&rows[out_at].hooks, 8, NULL, NULL, 0); \
|
|
zval zz_; ZVAL_LONG(&zz_, out_at); zend_hash_add_new(&idx, k_, &zz_); \
|
|
} } while (0)
|
|
|
|
for (uint32_t i = 1; i < n; i++) {
|
|
cryo_snap *cur = &snaps[i];
|
|
uint32_t lo = snaps[i - 1].epoch, hi = cur->epoch;
|
|
for (uint32_t k = 0; k < cur->nnodes; k++) {
|
|
const cryo_node_rec *nd = &cur->nodes[k];
|
|
if (nd->type != CRYO_NODE_OBJECT || nd->site == CRYO_NONE || nd->epoch == CRYO_NONE) continue;
|
|
if (nd->epoch <= lo || nd->epoch > hi) continue;
|
|
uint32_t at; ROW_FOR(cur, k, at);
|
|
rows[at].born[i - 1]++;
|
|
}
|
|
}
|
|
{
|
|
cryo_snap *last = &snaps[n - 1];
|
|
for (uint32_t k = 0; k < last->nnodes; k++) {
|
|
const cryo_node_rec *nd = &last->nodes[k];
|
|
if (nd->type != CRYO_NODE_OBJECT || nd->site == CRYO_NONE || nd->epoch == CRYO_NONE) continue;
|
|
if (nd->epoch <= snaps[0].epoch) continue;
|
|
for (uint32_t i = 1; i < n; i++) {
|
|
if (nd->epoch > snaps[i - 1].epoch && nd->epoch <= snaps[i].epoch) {
|
|
uint32_t at; ROW_FOR(last, k, at);
|
|
rows[at].left[i - 1]++;
|
|
rows[at].bytes_last = cn_add(rows[at].bytes_last, cn_u64(nd->size));
|
|
zend_string *w = snap_write(last, k);
|
|
if (w) {
|
|
zval *c = zend_hash_find(&rows[at].hooks, w);
|
|
if (c) {
|
|
Z_LVAL_P(c)++;
|
|
} else {
|
|
zval one; ZVAL_LONG(&one, 1);
|
|
zend_hash_add_new(&rows[at].hooks, w, &one);
|
|
}
|
|
zend_string_release(w);
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
#undef ROW_FOR
|
|
|
|
/* rows, then a stable sort: growing first, survivors desc */
|
|
sort_key *ord = safe_emalloc(nrows ? nrows : 1, sizeof(sort_key), 0);
|
|
zend_long *surv = safe_emalloc(nrows ? nrows : 1, sizeof(zend_long), 0);
|
|
bool *grow = safe_emalloc(nrows ? nrows : 1, sizeof(bool), 0);
|
|
for (uint32_t r = 0; r < nrows; r++) {
|
|
zend_long s = 0;
|
|
bool g = true;
|
|
for (uint32_t i = 0; i < nint; i++) {
|
|
s += rows[r].left[i];
|
|
if (rows[r].born[i] == 0 || rows[r].left[i] < rows[r].born[i]) g = false;
|
|
}
|
|
surv[r] = s; grow[r] = g;
|
|
ord[r].k1 = g; ord[r].k2 = s; ord[r].idx = r;
|
|
}
|
|
if (nrows) qsort(ord, nrows, sizeof(sort_key), sort_key_cmp);
|
|
|
|
array_init_size(out, nrows);
|
|
for (uint32_t o = 0; o < nrows; o++) {
|
|
uint32_t r = ord[o].idx;
|
|
zend_long born_total = 0;
|
|
zval born, left;
|
|
array_init(&born);
|
|
array_init(&left);
|
|
for (uint32_t i = 0; i < nint; i++) {
|
|
born_total += rows[r].born[i];
|
|
add_next_index_long(&born, rows[r].born[i]);
|
|
add_next_index_long(&left, rows[r].left[i]);
|
|
}
|
|
zval row;
|
|
array_init(&row);
|
|
add_assoc_str(&row, key_names[mode], zend_string_copy(rows[r].key));
|
|
add_assoc_zval(&row, "new_per_interval", &born);
|
|
add_assoc_zval(&row, "cohort_alive_at_last", &left);
|
|
add_assoc_long(&row, "survivors", surv[r]);
|
|
add_assoc_double(&row, "survival", born_total ? (double) surv[r] / (double) born_total : 0.0);
|
|
add_assoc_cn(&row, "bytes_last", rows[r].bytes_last);
|
|
add_assoc_bool(&row, "growing", grow[r]);
|
|
zend_string *best = NULL; zend_long best_n = 0; zend_string *hk; zval *hv;
|
|
ZEND_HASH_FOREACH_STR_KEY_VAL(&rows[r].hooks, hk, hv) {
|
|
if (Z_LVAL_P(hv) > best_n) { best = hk; best_n = Z_LVAL_P(hv); }
|
|
} ZEND_HASH_FOREACH_END();
|
|
if (best) {
|
|
add_assoc_str(&row, "hooked_by", zend_string_copy(best));
|
|
} else {
|
|
add_assoc_null(&row, "hooked_by");
|
|
}
|
|
add_next_index_zval(out, &row);
|
|
}
|
|
for (uint32_t r = 0; r < nrows; r++) {
|
|
zend_string_release(rows[r].key);
|
|
efree(rows[r].born);
|
|
efree(rows[r].left);
|
|
zend_hash_destroy(&rows[r].hooks);
|
|
}
|
|
if (rows) efree(rows);
|
|
efree(ord); efree(surv); efree(grow);
|
|
zend_hash_destroy(&idx);
|
|
}
|
|
|
|
/* timeline: per snapshot (epoch order) totals, and live objects per class /
|
|
* per site in each snapshot (keys in first-seen order). */
|
|
static void delta_timeline(zval *out, cryo_snap *snaps, zend_string **names, uint32_t n)
|
|
{
|
|
zval list, by_class, by_site;
|
|
array_init(&list);
|
|
array_init(&by_class);
|
|
array_init(&by_site);
|
|
for (uint32_t i = 0; i < n; i++) {
|
|
cnum bytes = cn_long(0);
|
|
zend_long objects = 0;
|
|
for (uint32_t k = 0; k < snaps[i].nnodes; k++) {
|
|
const cryo_node_rec *nd = &snaps[i].nodes[k];
|
|
bytes = cn_add(bytes, cn_u64(nd->size));
|
|
if (nd->type != CRYO_NODE_OBJECT) continue;
|
|
objects++;
|
|
for (int view = 0; view < 2; view++) {
|
|
zval *arr = view ? &by_site : &by_class;
|
|
zend_string *key = view ? snap_site(&snaps[i], nd->site) : snap_str(&snaps[i], nd->class_str);
|
|
zval *counts = zend_symtable_find(Z_ARRVAL_P(arr), key);
|
|
if (!counts) {
|
|
zval z;
|
|
array_init_size(&z, n);
|
|
for (uint32_t j = 0; j < n; j++) add_next_index_long(&z, 0);
|
|
counts = zend_symtable_update(Z_ARRVAL_P(arr), key, &z);
|
|
}
|
|
zval *c = zend_hash_index_find(Z_ARRVAL_P(counts), i);
|
|
Z_LVAL_P(c)++;
|
|
zend_string_release(key);
|
|
}
|
|
}
|
|
zval row;
|
|
array_init(&row);
|
|
add_assoc_str(&row, "file", zend_string_copy(names[i]));
|
|
add_assoc_long(&row, "epoch", (zend_long) snaps[i].epoch);
|
|
add_assoc_long(&row, "objects", objects);
|
|
add_assoc_cn(&row, "bytes", bytes);
|
|
add_next_index_zval(&list, &row);
|
|
}
|
|
array_init(out);
|
|
add_assoc_zval(out, "snapshots", &list);
|
|
add_assoc_zval(out, "by_class", &by_class);
|
|
add_assoc_zval(out, "by_site", &by_site);
|
|
}
|
|
|
|
/* ---- CryoDelta object ---------------------------------------------------- */
|
|
|
|
zend_class_entry *cryo_ce_delta;
|
|
static zend_object_handlers cryo_delta_handlers;
|
|
|
|
typedef struct {
|
|
zval views[3]; /* bySite, byClass, byStack */
|
|
zval timeline;
|
|
int def; /* view used by ArrayAccess / Countable / iteration */
|
|
cryo_snap last; /* last snapshot (explain) */
|
|
uint32_t first_epoch;
|
|
bool ready;
|
|
zend_object std;
|
|
} cryo_delta_obj;
|
|
|
|
#define DV_SITE 0
|
|
#define DV_CLASS 1
|
|
#define DV_STACK 2
|
|
|
|
static inline cryo_delta_obj *delta_from(zend_object *o)
|
|
{
|
|
return (cryo_delta_obj *)((char *) o - XtOffsetOf(cryo_delta_obj, std));
|
|
}
|
|
|
|
static zend_object *delta_create(zend_class_entry *ce)
|
|
{
|
|
cryo_delta_obj *d = zend_object_alloc(sizeof(cryo_delta_obj), ce);
|
|
zend_object_std_init(&d->std, ce);
|
|
object_properties_init(&d->std, ce);
|
|
d->std.handlers = &cryo_delta_handlers;
|
|
for (int i = 0; i < 3; i++) ZVAL_UNDEF(&d->views[i]);
|
|
ZVAL_UNDEF(&d->timeline);
|
|
return &d->std;
|
|
}
|
|
|
|
static void delta_free(zend_object *o)
|
|
{
|
|
cryo_delta_obj *d = delta_from(o);
|
|
for (int i = 0; i < 3; i++) zval_ptr_dtor(&d->views[i]);
|
|
zval_ptr_dtor(&d->timeline);
|
|
if (d->ready) snap_free(&d->last);
|
|
zend_object_std_dtor(o);
|
|
}
|
|
|
|
ZEND_METHOD(CryoReader, delta)
|
|
{
|
|
HashTable *files, *opts = NULL;
|
|
zend_string *by_str = NULL;
|
|
ZEND_PARSE_PARAMETERS_START(1, 2)
|
|
Z_PARAM_ARRAY_HT(files)
|
|
Z_PARAM_OPTIONAL
|
|
Z_PARAM_ARRAY_HT_OR_STR(opts, by_str)
|
|
ZEND_PARSE_PARAMETERS_END();
|
|
|
|
int def = DV_SITE;
|
|
zend_string *by = by_str;
|
|
if (opts) {
|
|
zend_string *k; zval *v;
|
|
ZEND_HASH_FOREACH_STR_KEY_VAL(opts, k, v) {
|
|
if (!k || !zend_string_equals_literal(k, "by") || Z_TYPE_P(v) != IS_STRING) {
|
|
zend_argument_value_error(2, "only accepts the key \"by\" (string)");
|
|
RETURN_THROWS();
|
|
}
|
|
by = Z_STR_P(v);
|
|
} ZEND_HASH_FOREACH_END();
|
|
}
|
|
if (by) {
|
|
if (zend_string_equals_literal(by, "site")) def = DV_SITE;
|
|
else if (zend_string_equals_literal(by, "class")) def = DV_CLASS;
|
|
else if (zend_string_equals_literal(by, "stack")) def = DV_STACK;
|
|
else {
|
|
zend_argument_value_error(2, "\"by\" must be \"site\", \"class\" or \"stack\"");
|
|
RETURN_THROWS();
|
|
}
|
|
}
|
|
|
|
uint32_t n = zend_hash_num_elements(files);
|
|
if (n < 2) {
|
|
zend_argument_value_error(1, "must contain at least 2 snapshots");
|
|
RETURN_THROWS();
|
|
}
|
|
|
|
cryo_snap *snaps = ecalloc(n, sizeof(cryo_snap));
|
|
zend_string **names = ecalloc(n, sizeof(zend_string *));
|
|
uint32_t loaded = 0;
|
|
zval *f;
|
|
ZEND_HASH_FOREACH_VAL(files, f) {
|
|
if (Z_TYPE_P(f) != IS_STRING) {
|
|
zend_argument_type_error(1, "must contain only file paths");
|
|
goto cleanup;
|
|
}
|
|
if (snap_load(&snaps[loaded], Z_STR_P(f)) == FAILURE) {
|
|
goto cleanup;
|
|
}
|
|
names[loaded] = Z_STR_P(f);
|
|
loaded++;
|
|
} ZEND_HASH_FOREACH_END();
|
|
|
|
/* order by epoch (stable) */
|
|
for (uint32_t i = 1; i < n; i++) {
|
|
for (uint32_t j = i; j > 0 && snaps[j].epoch < snaps[j - 1].epoch; j--) {
|
|
cryo_snap t = snaps[j]; snaps[j] = snaps[j - 1]; snaps[j - 1] = t;
|
|
zend_string *tn = names[j]; names[j] = names[j - 1]; names[j - 1] = tn;
|
|
}
|
|
}
|
|
|
|
object_init_ex(return_value, cryo_ce_delta);
|
|
cryo_delta_obj *d = delta_from(Z_OBJ_P(return_value));
|
|
delta_rows(&d->views[DV_SITE], snaps, n, BY_SITE);
|
|
delta_rows(&d->views[DV_CLASS], snaps, n, BY_CLASS);
|
|
delta_rows(&d->views[DV_STACK], snaps, n, BY_STACK);
|
|
delta_timeline(&d->timeline, snaps, names, n);
|
|
d->def = def;
|
|
d->first_epoch = snaps[0].epoch;
|
|
d->last = snaps[n - 1]; /* keep the last snapshot for explain() */
|
|
d->ready = true;
|
|
loaded--; /* not freed below */
|
|
|
|
cleanup:
|
|
for (uint32_t i = 0; i < loaded; i++) snap_free(&snaps[i]);
|
|
efree(snaps);
|
|
efree(names);
|
|
}
|
|
|
|
#define DELTA_THIS() delta_from(Z_OBJ_P(ZEND_THIS))
|
|
#define REQUIRE_DELTA(d) do { if (!(d)->ready) { \
|
|
zend_throw_exception(cryo_ce_exception, "Use CryoReader::delta()", 0); RETURN_THROWS(); } } while (0)
|
|
|
|
ZEND_METHOD(CryoDelta, __construct)
|
|
{
|
|
ZEND_PARSE_PARAMETERS_NONE();
|
|
zend_throw_exception(cryo_ce_exception, "Use CryoReader::delta()", 0);
|
|
}
|
|
|
|
#define DELTA_VIEW_METHOD(name, view) \
|
|
ZEND_METHOD(CryoDelta, name) \
|
|
{ \
|
|
ZEND_PARSE_PARAMETERS_NONE(); \
|
|
cryo_delta_obj *d = DELTA_THIS(); \
|
|
REQUIRE_DELTA(d); \
|
|
RETURN_COPY(&d->views[view]); \
|
|
}
|
|
|
|
DELTA_VIEW_METHOD(bySite, DV_SITE)
|
|
DELTA_VIEW_METHOD(byClass, DV_CLASS)
|
|
DELTA_VIEW_METHOD(byStack, DV_STACK)
|
|
|
|
ZEND_METHOD(CryoDelta, timeline)
|
|
{
|
|
ZEND_PARSE_PARAMETERS_NONE();
|
|
cryo_delta_obj *d = DELTA_THIS();
|
|
REQUIRE_DELTA(d);
|
|
RETURN_COPY(&d->timeline);
|
|
}
|
|
|
|
ZEND_METHOD(CryoDelta, count)
|
|
{
|
|
ZEND_PARSE_PARAMETERS_NONE();
|
|
cryo_delta_obj *d = DELTA_THIS();
|
|
REQUIRE_DELTA(d);
|
|
RETURN_LONG(zend_hash_num_elements(Z_ARRVAL(d->views[d->def])));
|
|
}
|
|
|
|
ZEND_METHOD(CryoDelta, getIterator)
|
|
{
|
|
ZEND_PARSE_PARAMETERS_NONE();
|
|
cryo_delta_obj *d = DELTA_THIS();
|
|
REQUIRE_DELTA(d);
|
|
object_init_ex(return_value, spl_ce_ArrayIterator);
|
|
zend_call_known_instance_method_with_1_params(spl_ce_ArrayIterator->constructor,
|
|
Z_OBJ_P(return_value), NULL, &d->views[d->def]);
|
|
}
|
|
|
|
ZEND_METHOD(CryoDelta, offsetExists)
|
|
{
|
|
zval *off;
|
|
ZEND_PARSE_PARAMETERS_START(1, 1)
|
|
Z_PARAM_ZVAL(off)
|
|
ZEND_PARSE_PARAMETERS_END();
|
|
cryo_delta_obj *d = DELTA_THIS();
|
|
REQUIRE_DELTA(d);
|
|
RETURN_BOOL(Z_TYPE_P(off) == IS_LONG && zend_hash_index_exists(Z_ARRVAL(d->views[d->def]), Z_LVAL_P(off)));
|
|
}
|
|
|
|
ZEND_METHOD(CryoDelta, offsetGet)
|
|
{
|
|
zval *off;
|
|
ZEND_PARSE_PARAMETERS_START(1, 1)
|
|
Z_PARAM_ZVAL(off)
|
|
ZEND_PARSE_PARAMETERS_END();
|
|
cryo_delta_obj *d = DELTA_THIS();
|
|
REQUIRE_DELTA(d);
|
|
zval *v = Z_TYPE_P(off) == IS_LONG ? zend_hash_index_find(Z_ARRVAL(d->views[d->def]), Z_LVAL_P(off)) : NULL;
|
|
if (!v) {
|
|
RETURN_NULL();
|
|
}
|
|
RETURN_COPY(v);
|
|
}
|
|
|
|
ZEND_METHOD(CryoDelta, offsetSet)
|
|
{
|
|
zval *off, *val;
|
|
ZEND_PARSE_PARAMETERS_START(2, 2)
|
|
Z_PARAM_ZVAL(off)
|
|
Z_PARAM_ZVAL(val)
|
|
ZEND_PARSE_PARAMETERS_END();
|
|
zend_throw_error(NULL, "CryoDelta is read-only");
|
|
}
|
|
|
|
ZEND_METHOD(CryoDelta, offsetUnset)
|
|
{
|
|
zval *off;
|
|
ZEND_PARSE_PARAMETERS_START(1, 1)
|
|
Z_PARAM_ZVAL(off)
|
|
ZEND_PARSE_PARAMETERS_END();
|
|
zend_throw_error(NULL, "CryoDelta is read-only");
|
|
}
|
|
|
|
/* Split "file:line function" (leftmost ":digits " wins); false if no line. */
|
|
static bool site_split(zend_string *site, size_t *file_len, zend_long *line)
|
|
{
|
|
const char *s = ZSTR_VAL(site);
|
|
size_t len = ZSTR_LEN(site);
|
|
for (size_t k = 0; k < len; k++) {
|
|
if (s[k] != ':') continue;
|
|
size_t j = k + 1;
|
|
while (j < len && j - k <= 9 && s[j] >= '0' && s[j] <= '9') j++;
|
|
if (j > k + 1 && j < len && s[j] == ' ' && j - k - 1 <= 9) {
|
|
zend_long v = 0;
|
|
for (size_t q = k + 1; q < j; q++) v = v * 10 + (s[q] - '0');
|
|
*file_len = k;
|
|
*line = v;
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
/* The file of a site is read only when it is a plain local regular file: the
|
|
* path comes from the snapshot, a hostile one could name a stream wrapper
|
|
* (phar://, http://, data:, php://stdin) or a device / FIFO that never ends.
|
|
* Same rule in PhpCryoReader::code(). */
|
|
static bool site_file_ok(const char *path, size_t len)
|
|
{
|
|
if (!len || memchr(path, '\0', len)) {
|
|
return false;
|
|
}
|
|
size_t n = 0; /* "scheme:" as php_stream_locate_url_wrapper() reads it */
|
|
while (n < len && (isalnum((unsigned char) path[n]) || path[n] == '+' || path[n] == '-' || path[n] == '.')) n++;
|
|
if (n >= 2 && n < len && path[n] == ':') {
|
|
return false;
|
|
}
|
|
char *p = estrndup(path, len);
|
|
zend_stat_t st;
|
|
bool ok = VCWD_STAT(p, &st) == 0 && S_ISREG(st.st_mode);
|
|
efree(p);
|
|
return ok;
|
|
}
|
|
|
|
/* Code around a site: [line => text] for line-2 .. line+2, null if the file
|
|
* cannot be read. Lines split on "\n", "\r" trimmed; a final empty line
|
|
* (file ending with "\n") does not count. */
|
|
static void add_code(zval *arr, const char *key, zend_string *site)
|
|
{
|
|
size_t flen; zend_long line;
|
|
if (!site || !site_split(site, &flen, &line)) {
|
|
add_assoc_null(arr, key);
|
|
return;
|
|
}
|
|
if (!site_file_ok(ZSTR_VAL(site), flen)) {
|
|
add_assoc_null(arr, key);
|
|
return;
|
|
}
|
|
char *path = estrndup(ZSTR_VAL(site), flen);
|
|
php_stream *st = php_stream_open_wrapper(path, "rb", 0, NULL);
|
|
efree(path);
|
|
zend_string *data = st ? php_stream_copy_to_mem(st, PHP_STREAM_COPY_ALL, 0) : NULL;
|
|
if (st) php_stream_close(st);
|
|
if (!st) {
|
|
add_assoc_null(arr, key);
|
|
return;
|
|
}
|
|
const char *p = data ? ZSTR_VAL(data) : "", *end = p + (data ? ZSTR_LEN(data) : 0);
|
|
zval code;
|
|
array_init(&code);
|
|
zend_long no = 1;
|
|
while (p < end) {
|
|
const char *nl = memchr(p, '\n', (size_t) (end - p));
|
|
const char *le = nl ? nl : end;
|
|
if (no >= line - 2 && no <= line + 2) {
|
|
size_t l = (size_t) (le - p);
|
|
while (l && (p[l - 1] == '\r' || p[l - 1] == '\n')) l--;
|
|
add_index_stringl(&code, no, p, l);
|
|
}
|
|
if (!nl || no > line + 2) break;
|
|
p = nl + 1;
|
|
no++;
|
|
}
|
|
if (data) zend_string_release(data);
|
|
add_assoc_zval(arr, key, &code);
|
|
}
|
|
|
|
static void snap_paths(zval *out, cryo_snap *s, uint32_t tgt, zend_long max);
|
|
|
|
/* Path signature: kinds and names, "[123]" written "[n]" */
|
|
static zend_string *path_signature(zval *path)
|
|
{
|
|
smart_str sig = {0};
|
|
zval *step;
|
|
ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(path), step) {
|
|
zend_string *kind = Z_STR_P(zend_hash_str_find(Z_ARRVAL_P(step), "kind", 4));
|
|
zend_string *name = Z_STR_P(zend_hash_str_find(Z_ARRVAL_P(step), "name", 4));
|
|
smart_str_append(&sig, kind);
|
|
smart_str_appendc(&sig, ':');
|
|
size_t l = ZSTR_LEN(name);
|
|
bool idx = l >= 3 && ZSTR_VAL(name)[0] == '[' && ZSTR_VAL(name)[l - 1] == ']';
|
|
for (size_t i = 1; idx && i + 1 < l; i++) {
|
|
if (ZSTR_VAL(name)[i] < '0' || ZSTR_VAL(name)[i] > '9') idx = false;
|
|
}
|
|
if (idx) smart_str_appends(&sig, "[n]"); else smart_str_append(&sig, name);
|
|
smart_str_appendc(&sig, '/');
|
|
} ZEND_HASH_FOREACH_END();
|
|
smart_str_0(&sig);
|
|
return sig.s ? sig.s : ZSTR_EMPTY_ALLOC();
|
|
}
|
|
|
|
/*
|
|
* explain(int|string $site): a bySite() row (index or site string) with the
|
|
* code of its allocation site and of its main write site, and the typical
|
|
* paths that keep its survivors alive in the last snapshot (up to 3,
|
|
* grouped by path signature, from the first 50 survivors). null if unknown.
|
|
*/
|
|
ZEND_METHOD(CryoDelta, explain)
|
|
{
|
|
zend_long index = -1;
|
|
zend_string *want = NULL;
|
|
ZEND_PARSE_PARAMETERS_START(1, 1)
|
|
Z_PARAM_STR_OR_LONG(want, index)
|
|
ZEND_PARSE_PARAMETERS_END();
|
|
cryo_delta_obj *d = DELTA_THIS();
|
|
REQUIRE_DELTA(d);
|
|
HashTable *rows = Z_ARRVAL(d->views[DV_SITE]);
|
|
zval *row = NULL;
|
|
if (want) {
|
|
zval *r;
|
|
ZEND_HASH_FOREACH_VAL(rows, r) {
|
|
if (zend_string_equals(Z_STR_P(zend_hash_str_find(Z_ARRVAL_P(r), "site", 4)), want)) { row = r; break; }
|
|
} ZEND_HASH_FOREACH_END();
|
|
} else if (index >= 0) {
|
|
row = zend_hash_index_find(rows, index);
|
|
}
|
|
if (!row) {
|
|
RETURN_NULL();
|
|
}
|
|
zend_string *site = Z_STR_P(zend_hash_str_find(Z_ARRVAL_P(row), "site", 4));
|
|
zval *hook = zend_hash_str_find(Z_ARRVAL_P(row), "hooked_by", 9);
|
|
cryo_snap *s = &d->last;
|
|
|
|
array_init(return_value);
|
|
add_assoc_str(return_value, "site", zend_string_copy(site));
|
|
Z_TRY_ADDREF_P(row);
|
|
add_assoc_zval(return_value, "row", row);
|
|
size_t flen; zend_long line;
|
|
if (site_split(site, &flen, &line)) {
|
|
add_assoc_stringl(return_value, "file", ZSTR_VAL(site), flen);
|
|
add_assoc_long(return_value, "line", line);
|
|
} else {
|
|
add_assoc_null(return_value, "file");
|
|
add_assoc_null(return_value, "line");
|
|
}
|
|
add_code(return_value, "code", site);
|
|
if (Z_TYPE_P(hook) == IS_STRING) {
|
|
add_assoc_str(return_value, "hooked_by", zend_string_copy(Z_STR_P(hook)));
|
|
} else {
|
|
add_assoc_null(return_value, "hooked_by");
|
|
}
|
|
add_code(return_value, "hook_code", Z_TYPE_P(hook) == IS_STRING ? Z_STR_P(hook) : NULL);
|
|
|
|
/* typical paths */
|
|
HashTable groups; /* signature -> index in `paths` */
|
|
zend_hash_init(&groups, 8, NULL, NULL, 0);
|
|
zval paths;
|
|
array_init(&paths);
|
|
uint32_t seen = 0;
|
|
for (uint32_t k = 0; k < s->nnodes && seen < 50; k++) {
|
|
const cryo_node_rec *nd = &s->nodes[k];
|
|
if (nd->type != CRYO_NODE_OBJECT || nd->site == CRYO_NONE || nd->epoch == CRYO_NONE
|
|
|| nd->epoch <= d->first_epoch) continue;
|
|
zend_string *ks = snap_site(s, nd->site);
|
|
bool same = zend_string_equals(ks, site);
|
|
zend_string_release(ks);
|
|
if (!same) continue;
|
|
seen++;
|
|
zval found;
|
|
array_init(&found);
|
|
snap_paths(&found, s, k, 1);
|
|
zval *p = zend_hash_index_find(Z_ARRVAL(found), 0);
|
|
if (p) {
|
|
zend_string *sig = path_signature(p);
|
|
zval *g = zend_hash_find(&groups, sig);
|
|
if (g) {
|
|
zval *entry = zend_hash_index_find(Z_ARRVAL(paths), Z_LVAL_P(g));
|
|
zval *c = zend_hash_str_find(Z_ARRVAL_P(entry), "count", 5);
|
|
Z_LVAL_P(c)++;
|
|
} else {
|
|
zval entry, gi;
|
|
array_init(&entry);
|
|
add_assoc_long(&entry, "count", 1);
|
|
Z_TRY_ADDREF_P(p);
|
|
add_assoc_zval(&entry, "path", p);
|
|
ZVAL_LONG(&gi, zend_hash_num_elements(Z_ARRVAL(paths)));
|
|
zend_hash_add_new(&groups, sig, &gi);
|
|
add_next_index_zval(&paths, &entry);
|
|
}
|
|
zend_string_release(sig);
|
|
}
|
|
zval_ptr_dtor(&found);
|
|
}
|
|
zend_hash_destroy(&groups);
|
|
/* count desc, stable; keep 3 */
|
|
uint32_t np = zend_hash_num_elements(Z_ARRVAL(paths));
|
|
zval *items = safe_emalloc(np ? np : 1, sizeof(zval), 0);
|
|
uint32_t i = 0; zval *e;
|
|
ZEND_HASH_FOREACH_VAL(Z_ARRVAL(paths), e) { ZVAL_COPY(&items[i++], e); } ZEND_HASH_FOREACH_END();
|
|
for (uint32_t a = 1; a < np; a++) {
|
|
for (uint32_t b = a; b > 0; b--) {
|
|
zend_long cx = Z_LVAL_P(zend_hash_str_find(Z_ARRVAL(items[b - 1]), "count", 5));
|
|
zend_long cy = Z_LVAL_P(zend_hash_str_find(Z_ARRVAL(items[b]), "count", 5));
|
|
if (cy > cx) { zval t = items[b]; items[b] = items[b - 1]; items[b - 1] = t; } else break;
|
|
}
|
|
}
|
|
zval top;
|
|
array_init(&top);
|
|
for (i = 0; i < np; i++) {
|
|
if (i < 3) add_next_index_zval(&top, &items[i]); else zval_ptr_dtor(&items[i]);
|
|
}
|
|
efree(items);
|
|
zval_ptr_dtor(&paths);
|
|
add_assoc_zval(return_value, "paths", &top);
|
|
}
|
|
|
|
/* ------------------------------------------------------------------------ */
|
|
/* Graph queries (M2) */
|
|
/* ------------------------------------------------------------------------ */
|
|
|
|
/* Build the out / in adjacency once. Edges whose ends are not valid node
|
|
* ids are ignored. Within a node, edges keep the file order. */
|
|
static void snap_build_adj(cryo_snap *s)
|
|
{
|
|
if (s->out_off) {
|
|
return;
|
|
}
|
|
uint32_t n = s->nnodes, valid = 0;
|
|
s->out_off = ecalloc((size_t) n + 1, sizeof(uint32_t));
|
|
s->in_off = ecalloc((size_t) n + 1, sizeof(uint32_t));
|
|
for (uint32_t e = 0; e < s->nedges; e++) {
|
|
uint32_t f = s->edges[e].from, t = s->edges[e].to;
|
|
if (f < n && t < n) {
|
|
s->out_off[f + 1]++;
|
|
s->in_off[t + 1]++;
|
|
valid++;
|
|
}
|
|
}
|
|
for (uint32_t v = 0; v < n; v++) {
|
|
s->out_off[v + 1] += s->out_off[v];
|
|
s->in_off[v + 1] += s->in_off[v];
|
|
}
|
|
s->out_e = safe_emalloc(valid ? valid : 1, sizeof(uint32_t), 0);
|
|
s->in_e = safe_emalloc(valid ? valid : 1, sizeof(uint32_t), 0);
|
|
uint32_t *po = safe_emalloc((size_t) n + 1, sizeof(uint32_t), 0);
|
|
uint32_t *pi = safe_emalloc((size_t) n + 1, sizeof(uint32_t), 0);
|
|
memcpy(po, s->out_off, ((size_t) n + 1) * sizeof(uint32_t));
|
|
memcpy(pi, s->in_off, ((size_t) n + 1) * sizeof(uint32_t));
|
|
for (uint32_t e = 0; e < s->nedges; e++) {
|
|
uint32_t f = s->edges[e].from, t = s->edges[e].to;
|
|
if (f < n && t < n) {
|
|
s->out_e[po[f]++] = e;
|
|
s->in_e[pi[t]++] = e;
|
|
}
|
|
}
|
|
efree(po);
|
|
efree(pi);
|
|
}
|
|
|
|
/* "type" and "class" (null if not an object) of a node */
|
|
static void add_type_class(zval *arr, const cryo_snap *s, uint32_t v)
|
|
{
|
|
const cryo_node_rec *nd = &s->nodes[v];
|
|
add_assoc_string(arr, "type", NODE_TYPE(nd->type));
|
|
if (nd->class_str != CRYO_NONE) {
|
|
add_assoc_str(arr, "class", snap_str(s, nd->class_str));
|
|
} else {
|
|
add_assoc_null(arr, "class");
|
|
}
|
|
}
|
|
|
|
static void add_write(zval *arr, const cryo_snap *s, uint32_t v)
|
|
{
|
|
zend_string *w = snap_write(s, v);
|
|
if (w) {
|
|
add_assoc_str(arr, "write", w);
|
|
} else {
|
|
add_assoc_null(arr, "write");
|
|
}
|
|
}
|
|
|
|
/* Node record: node, type, class, size, site, write, epoch, handle (null = unknown) */
|
|
static void add_node_record(zval *arr, const cryo_snap *s, uint32_t v)
|
|
{
|
|
const cryo_node_rec *nd = &s->nodes[v];
|
|
add_assoc_long(arr, "node", (zend_long) v);
|
|
add_type_class(arr, s, v);
|
|
add_assoc_u64(arr, "size", nd->size);
|
|
if (nd->site != CRYO_NONE && nd->site < s->nsites) {
|
|
add_assoc_str(arr, "site", snap_str(s, s->sites[nd->site]));
|
|
} else {
|
|
add_assoc_null(arr, "site");
|
|
}
|
|
add_write(arr, s, v);
|
|
if (nd->epoch != CRYO_NONE) {
|
|
add_assoc_long(arr, "epoch", (zend_long) nd->epoch);
|
|
} else {
|
|
add_assoc_null(arr, "epoch");
|
|
}
|
|
if (nd->handle != CRYO_NONE) {
|
|
add_assoc_long(arr, "handle", (zend_long) nd->handle);
|
|
} else {
|
|
add_assoc_null(arr, "handle");
|
|
}
|
|
}
|
|
|
|
/* BFS from the roots whose kind is (orphan ? ORPHAN : not ORPHAN), through
|
|
* nodes whose mark is 0; reached nodes get `val`. */
|
|
static void bfs_mark(const cryo_snap *s, uint8_t *mark, uint32_t *queue, bool orphan, uint8_t val)
|
|
{
|
|
uint32_t head = 0, tail = 0, n = s->nnodes;
|
|
for (uint32_t i = 0; i < s->nroots; i++) {
|
|
uint32_t v = s->roots[i].node;
|
|
if ((s->roots[i].kind == CRYO_ROOT_ORPHAN) != orphan || v >= n || mark[v]) {
|
|
continue;
|
|
}
|
|
mark[v] = val;
|
|
queue[tail++] = v;
|
|
}
|
|
while (head < tail) {
|
|
uint32_t v = queue[head++];
|
|
for (uint32_t j = s->out_off[v]; j < s->out_off[v + 1]; j++) {
|
|
uint32_t t = s->edges[s->out_e[j]].to;
|
|
if (!mark[t]) {
|
|
mark[t] = val;
|
|
queue[tail++] = t;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/* Distance of every node from the roots (BFS, all roots at distance 0),
|
|
* CRYO_NONE if unreachable. Computed once. */
|
|
static const uint32_t *snap_dist(cryo_snap *s)
|
|
{
|
|
if (s->dist) {
|
|
return s->dist;
|
|
}
|
|
snap_build_adj(s);
|
|
uint32_t n = s->nnodes, head = 0, tail = 0;
|
|
uint32_t *queue = safe_emalloc(n ? n : 1, sizeof(uint32_t), 0);
|
|
s->dist = safe_emalloc(n ? n : 1, sizeof(uint32_t), 0);
|
|
for (uint32_t v = 0; v < n; v++) s->dist[v] = CRYO_NONE;
|
|
for (uint32_t i = 0; i < s->nroots; i++) {
|
|
uint32_t v = s->roots[i].node;
|
|
if (v < n && s->dist[v] == CRYO_NONE) {
|
|
s->dist[v] = 0;
|
|
queue[tail++] = v;
|
|
}
|
|
}
|
|
while (head < tail) {
|
|
uint32_t v = queue[head++];
|
|
for (uint32_t j = s->out_off[v]; j < s->out_off[v + 1]; j++) {
|
|
uint32_t t = s->edges[s->out_e[j]].to;
|
|
if (s->dist[t] == CRYO_NONE) {
|
|
s->dist[t] = s->dist[v] + 1;
|
|
queue[tail++] = t;
|
|
}
|
|
}
|
|
}
|
|
efree(queue);
|
|
return s->dist;
|
|
}
|
|
|
|
static void add_step(zval *list, const cryo_snap *s, const char *kind, zend_string *name, uint32_t v)
|
|
{
|
|
zval step;
|
|
array_init(&step);
|
|
add_assoc_string(&step, "kind", kind);
|
|
add_assoc_str(&step, "name", name);
|
|
add_assoc_long(&step, "node", (zend_long) v);
|
|
add_type_class(&step, s, v);
|
|
add_write(&step, s, v);
|
|
add_next_index_zval(list, &step);
|
|
}
|
|
|
|
/*
|
|
* Up to `max` shortest paths from a root to `tgt`, appended to `out` (list
|
|
* of lists of steps). Enumerated backwards on the BFS layers with an
|
|
* explicit stack: predecessors in file order (in-edges), root entries in
|
|
* file order for nodes at distance 0.
|
|
*/
|
|
static void snap_paths(zval *out, cryo_snap *s, uint32_t tgt, zend_long max)
|
|
{
|
|
const uint32_t *dist = snap_dist(s);
|
|
if (tgt >= s->nnodes || dist[tgt] == CRYO_NONE || max <= 0) {
|
|
return;
|
|
}
|
|
uint32_t depth = dist[tgt] + 1, sp = 1;
|
|
uint32_t *node = safe_emalloc(depth, sizeof(uint32_t), 0);
|
|
uint32_t *pos = safe_emalloc(depth, sizeof(uint32_t), 0);
|
|
uint32_t *edge = safe_emalloc(depth, sizeof(uint32_t), 0); /* edge[k]: node[k] -> node[k-1] */
|
|
zend_long found = 0;
|
|
node[0] = tgt; pos[0] = 0; edge[0] = CRYO_NONE;
|
|
while (sp && found < max) {
|
|
uint32_t v = node[sp - 1];
|
|
if (dist[v] == 0) {
|
|
uint32_t i = pos[sp - 1];
|
|
while (i < s->nroots && s->roots[i].node != v) i++;
|
|
if (i >= s->nroots) {
|
|
sp--;
|
|
continue;
|
|
}
|
|
pos[sp - 1] = i + 1;
|
|
zval path;
|
|
array_init(&path);
|
|
add_step(&path, s, ROOT_KIND(s->roots[i].kind), snap_str(s, s->roots[i].name_str), v);
|
|
for (uint32_t k = sp - 1; k >= 1; k--) {
|
|
add_step(&path, s, "edge", snap_str(s, s->edges[edge[k]].name_str), node[k - 1]);
|
|
}
|
|
add_next_index_zval(out, &path);
|
|
found++;
|
|
} else {
|
|
uint32_t j = s->in_off[v] + pos[sp - 1], end = s->in_off[v + 1];
|
|
while (j < end && dist[s->edges[s->in_e[j]].from] != dist[v] - 1) j++;
|
|
if (j >= end) {
|
|
sp--;
|
|
continue;
|
|
}
|
|
pos[sp - 1] = j - s->in_off[v] + 1;
|
|
node[sp] = s->edges[s->in_e[j]].from;
|
|
pos[sp] = 0;
|
|
edge[sp] = s->in_e[j];
|
|
sp++;
|
|
}
|
|
}
|
|
efree(node);
|
|
efree(pos);
|
|
efree(edge);
|
|
}
|
|
|
|
/* path(int $node, int $max = 5): up to $max shortest paths from a root */
|
|
ZEND_METHOD(CryoReader, path)
|
|
{
|
|
zend_long target, max = 5;
|
|
ZEND_PARSE_PARAMETERS_START(1, 2)
|
|
Z_PARAM_LONG(target)
|
|
Z_PARAM_OPTIONAL
|
|
Z_PARAM_LONG(max)
|
|
ZEND_PARSE_PARAMETERS_END();
|
|
cryo_reader_obj *r = READER_THIS();
|
|
REQUIRE_LOADED(r);
|
|
array_init(return_value);
|
|
if (target >= 0 && (zend_ulong) target < r->snap.nnodes) {
|
|
snap_paths(return_value, &r->snap, (uint32_t) target, max);
|
|
}
|
|
}
|
|
|
|
/*
|
|
* Dominator tree from a super-root S (= nnodes) linked to every root
|
|
* (Cooper-Harvey-Kennedy, iterative DFS) and retained sizes. Computed once:
|
|
* dom_order[0 .. dom_cnt-1] postorder (S last), dom_idom[v] (CRYO_NONE if
|
|
* unreachable, S for roots), dom_ret[v] retained size.
|
|
*/
|
|
static void snap_dom(cryo_snap *s)
|
|
{
|
|
if (s->dom_order) {
|
|
return;
|
|
}
|
|
snap_build_adj(s);
|
|
uint32_t n = s->nnodes, S = n, cnt = 0, sp = 0;
|
|
uint8_t *vis = ecalloc((size_t) n + 1, 1);
|
|
uint8_t *isroot = ecalloc((size_t) n + 1, 1);
|
|
uint32_t *post = safe_emalloc((size_t) n + 1, sizeof(uint32_t), 0);
|
|
uint32_t *order = safe_emalloc((size_t) n + 1, sizeof(uint32_t), 0);
|
|
uint32_t *stk = safe_emalloc((size_t) n + 1, sizeof(uint32_t), 0);
|
|
uint32_t *pos = safe_emalloc((size_t) n + 1, sizeof(uint32_t), 0);
|
|
uint32_t *idom = safe_emalloc((size_t) n + 1, sizeof(uint32_t), 0);
|
|
cnum *ret = ecalloc((size_t) n + 1, sizeof(cnum));
|
|
|
|
for (uint32_t i = 0; i < s->nroots; i++) {
|
|
if (s->roots[i].node < n) isroot[s->roots[i].node] = 1;
|
|
}
|
|
|
|
/* iterative DFS from the super-root: postorder numbers */
|
|
vis[S] = 1; stk[0] = S; pos[0] = 0; sp = 1;
|
|
while (sp) {
|
|
uint32_t v = stk[sp - 1], i = pos[sp - 1], child = CRYO_NONE;
|
|
bool more;
|
|
if (v == S) {
|
|
more = i < s->nroots;
|
|
if (more) child = s->roots[i].node;
|
|
} else {
|
|
more = s->out_off[v] + i < s->out_off[v + 1];
|
|
if (more) child = s->edges[s->out_e[s->out_off[v] + i]].to;
|
|
}
|
|
if (more) {
|
|
pos[sp - 1]++;
|
|
if (child < n && !vis[child]) {
|
|
vis[child] = 1; stk[sp] = child; pos[sp] = 0; sp++;
|
|
}
|
|
} else {
|
|
sp--;
|
|
post[v] = cnt;
|
|
order[cnt++] = v;
|
|
}
|
|
}
|
|
|
|
/* Cooper-Harvey-Kennedy: iterate in reverse postorder until stable */
|
|
for (uint32_t v = 0; v <= n; v++) idom[v] = CRYO_NONE;
|
|
idom[S] = S;
|
|
bool changed = true;
|
|
while (changed) {
|
|
changed = false;
|
|
for (uint32_t k = cnt - 1; k-- > 0;) {
|
|
uint32_t v = order[k], nd = CRYO_NONE;
|
|
if (isroot[v]) {
|
|
nd = S;
|
|
} else {
|
|
for (uint32_t j = s->in_off[v]; j < s->in_off[v + 1]; j++) {
|
|
uint32_t p = s->edges[s->in_e[j]].from;
|
|
if (idom[p] == CRYO_NONE) continue;
|
|
if (nd == CRYO_NONE) { nd = p; continue; }
|
|
uint32_t a = p, b = nd;
|
|
while (a != b) {
|
|
while (post[a] < post[b]) a = idom[a];
|
|
while (post[b] < post[a]) b = idom[b];
|
|
}
|
|
nd = a;
|
|
}
|
|
}
|
|
if (idom[v] != nd) { idom[v] = nd; changed = true; }
|
|
}
|
|
}
|
|
|
|
/* retained sizes: every node before its dominator (postorder) */
|
|
for (uint32_t k = 0; k + 1 < cnt; k++) ret[order[k]] = cn_u64(s->nodes[order[k]].size);
|
|
for (uint32_t k = 0; k + 1 < cnt; k++) {
|
|
uint32_t v = order[k];
|
|
if (idom[v] != S) ret[idom[v]] = cn_add(ret[idom[v]], ret[v]);
|
|
}
|
|
efree(vis); efree(isroot); efree(post); efree(stk); efree(pos);
|
|
s->dom_order = order;
|
|
s->dom_cnt = cnt;
|
|
s->dom_idom = idom;
|
|
s->dom_ret = ret;
|
|
}
|
|
|
|
typedef struct { uint32_t node; cnum retained; } dom_row;
|
|
|
|
static int dom_cmp(const void *a, const void *b)
|
|
{
|
|
const dom_row *x = a, *y = b;
|
|
int c = cn_cmp(y->retained, x->retained);
|
|
if (c) return c;
|
|
return x->node < y->node ? -1 : (x->node > y->node ? 1 : 0);
|
|
}
|
|
|
|
/* dominators(int $limit = 20): the nodes that retain the most
|
|
* (retained desc, node asc). */
|
|
ZEND_METHOD(CryoReader, dominators)
|
|
{
|
|
zend_long limit = 20;
|
|
ZEND_PARSE_PARAMETERS_START(0, 1)
|
|
Z_PARAM_OPTIONAL
|
|
Z_PARAM_LONG(limit)
|
|
ZEND_PARSE_PARAMETERS_END();
|
|
cryo_reader_obj *r = READER_THIS();
|
|
REQUIRE_LOADED(r);
|
|
cryo_snap *s = &r->snap;
|
|
snap_dom(s);
|
|
uint32_t S = s->nnodes, nrows = 0;
|
|
dom_row *rows = safe_emalloc(s->dom_cnt, sizeof(dom_row), 0);
|
|
for (uint32_t k = 0; k + 1 < s->dom_cnt; k++) {
|
|
rows[nrows].node = s->dom_order[k];
|
|
rows[nrows].retained = s->dom_ret[s->dom_order[k]];
|
|
nrows++;
|
|
}
|
|
if (nrows) qsort(rows, nrows, sizeof(dom_row), dom_cmp);
|
|
|
|
array_init(return_value);
|
|
for (uint32_t i = 0; i < nrows && (zend_long) i < limit; i++) {
|
|
uint32_t v = rows[i].node;
|
|
zval row;
|
|
array_init(&row);
|
|
add_assoc_long(&row, "node", (zend_long) v);
|
|
add_type_class(&row, s, v);
|
|
add_assoc_u64(&row, "self", s->nodes[v].size);
|
|
add_assoc_cn(&row, "retained", rows[i].retained);
|
|
if (s->dom_idom[v] == S) {
|
|
add_assoc_null(&row, "idom");
|
|
} else {
|
|
add_assoc_long(&row, "idom", (zend_long) s->dom_idom[v]);
|
|
}
|
|
add_next_index_zval(return_value, &row);
|
|
}
|
|
efree(rows);
|
|
}
|
|
|
|
/*
|
|
* Retained size per class without double counting: an instance counts when
|
|
* no other instance of its class dominates it (iterative DFS on the
|
|
* dominator tree with a per-class counter of open ancestors).
|
|
* Fills cls_ret[class_str] (nstr entries).
|
|
*/
|
|
static void snap_class_retained(cryo_snap *s, cnum *cls_ret)
|
|
{
|
|
snap_dom(s);
|
|
uint32_t n = s->nnodes, S = n;
|
|
uint32_t *coff = ecalloc((size_t) n + 2, sizeof(uint32_t));
|
|
uint32_t *kids = safe_emalloc(s->dom_cnt, sizeof(uint32_t), 0);
|
|
uint32_t *fill = safe_emalloc((size_t) n + 1, sizeof(uint32_t), 0);
|
|
uint32_t *open = ecalloc(s->nstr ? s->nstr : 1, sizeof(uint32_t));
|
|
uint32_t *stk = safe_emalloc(s->dom_cnt, sizeof(uint32_t), 0);
|
|
uint32_t *pos = safe_emalloc(s->dom_cnt, sizeof(uint32_t), 0);
|
|
|
|
for (uint32_t k = 0; k + 1 < s->dom_cnt; k++) coff[s->dom_idom[s->dom_order[k]] + 1]++;
|
|
for (uint32_t v = 0; v <= n; v++) coff[v + 1] += coff[v];
|
|
memcpy(fill, coff, ((size_t) n + 1) * sizeof(uint32_t));
|
|
for (uint32_t k = 0; k + 1 < s->dom_cnt; k++) {
|
|
uint32_t v = s->dom_order[k];
|
|
kids[fill[s->dom_idom[v]]++] = v;
|
|
}
|
|
|
|
uint32_t sp = 1;
|
|
stk[0] = S; pos[0] = 0;
|
|
while (sp) {
|
|
uint32_t v = stk[sp - 1];
|
|
if (coff[v] + pos[sp - 1] < coff[v + 1]) {
|
|
uint32_t c = kids[coff[v] + pos[sp - 1]++];
|
|
uint32_t cls = s->nodes[c].type == CRYO_NODE_OBJECT ? s->nodes[c].class_str : CRYO_NONE;
|
|
if (cls < s->nstr) {
|
|
if (open[cls] == 0) cls_ret[cls] = cn_add(cls_ret[cls], s->dom_ret[c]);
|
|
open[cls]++;
|
|
}
|
|
stk[sp] = c; pos[sp] = 0; sp++;
|
|
} else {
|
|
uint32_t cls = v != S && s->nodes[v].type == CRYO_NODE_OBJECT ? s->nodes[v].class_str : CRYO_NONE;
|
|
if (cls < s->nstr) open[cls]--;
|
|
sp--;
|
|
}
|
|
}
|
|
efree(coff); efree(kids); efree(fill); efree(open); efree(stk); efree(pos);
|
|
}
|
|
|
|
/* orphans(): objects reachable only from "orphan" roots (uncollected
|
|
* cycles), as node records, by node id. */
|
|
static void snap_orphans(zval *return_value, cryo_snap *s);
|
|
|
|
ZEND_METHOD(CryoReader, orphans)
|
|
{
|
|
ZEND_PARSE_PARAMETERS_NONE();
|
|
cryo_reader_obj *r = READER_THIS();
|
|
REQUIRE_LOADED(r);
|
|
snap_orphans(return_value, &r->snap);
|
|
}
|
|
|
|
static void snap_orphans(zval *return_value, cryo_snap *s)
|
|
{
|
|
snap_build_adj(s);
|
|
uint32_t n = s->nnodes;
|
|
uint8_t *mark = ecalloc(n ? n : 1, 1);
|
|
uint32_t *queue = safe_emalloc(n ? n : 1, sizeof(uint32_t), 0);
|
|
bfs_mark(s, mark, queue, false, 1);
|
|
bfs_mark(s, mark, queue, true, 2);
|
|
array_init(return_value);
|
|
for (uint32_t v = 0; v < n; v++) {
|
|
if (mark[v] == 2 && s->nodes[v].type == CRYO_NODE_OBJECT) {
|
|
zval row;
|
|
array_init(&row);
|
|
add_node_record(&row, s, v);
|
|
add_next_index_zval(return_value, &row);
|
|
}
|
|
}
|
|
efree(mark);
|
|
efree(queue);
|
|
}
|
|
|
|
typedef struct {
|
|
uint8_t type;
|
|
uint32_t hash, len, node, count;
|
|
cnum bytes, wasted;
|
|
} dupe_row;
|
|
|
|
static int dupe_key_cmp(const void *a, const void *b)
|
|
{
|
|
const dupe_row *x = a, *y = b;
|
|
if (x->type != y->type) return x->type < y->type ? -1 : 1;
|
|
if (x->hash != y->hash) return x->hash < y->hash ? -1 : 1;
|
|
if (x->len != y->len) return x->len < y->len ? -1 : 1;
|
|
return x->node < y->node ? -1 : (x->node > y->node ? 1 : 0);
|
|
}
|
|
|
|
static int dupe_rank_cmp(const void *a, const void *b)
|
|
{
|
|
const dupe_row *x = a, *y = b;
|
|
int c = cn_cmp(y->wasted, x->wasted);
|
|
if (c) return c;
|
|
if (x->count != y->count) return x->count < y->count ? 1 : -1;
|
|
return dupe_key_cmp(a, b);
|
|
}
|
|
|
|
/*
|
|
* dupes(int $limit = 20): strings and flat arrays present in several copies
|
|
* with the same content (section HASH; [] for files without it).
|
|
* wasted = bytes of all copies - bytes of the smallest one.
|
|
*/
|
|
ZEND_METHOD(CryoReader, dupes)
|
|
{
|
|
zend_long limit = 20;
|
|
ZEND_PARSE_PARAMETERS_START(0, 1)
|
|
Z_PARAM_OPTIONAL
|
|
Z_PARAM_LONG(limit)
|
|
ZEND_PARSE_PARAMETERS_END();
|
|
cryo_reader_obj *r = READER_THIS();
|
|
REQUIRE_LOADED(r);
|
|
cryo_snap *s = &r->snap;
|
|
array_init(return_value);
|
|
|
|
uint32_t n = MIN(s->nnodes, s->nhashes), m = 0, ng = 0;
|
|
if (!n) {
|
|
return;
|
|
}
|
|
dupe_row *items = safe_emalloc(n, sizeof(dupe_row), 0);
|
|
for (uint32_t v = 0; v < n; v++) {
|
|
if (s->hashes[v].hash == 0) continue;
|
|
items[m].type = s->nodes[v].type;
|
|
items[m].hash = s->hashes[v].hash;
|
|
items[m].len = s->hashes[v].len;
|
|
items[m].node = v;
|
|
items[m].count = 1;
|
|
items[m].bytes = cn_u64(s->nodes[v].size);
|
|
items[m].wasted = cn_long(0);
|
|
m++;
|
|
}
|
|
if (m) qsort(items, m, sizeof(dupe_row), dupe_key_cmp);
|
|
|
|
/* collapse each group of equal (type, hash, len) into items[ng] */
|
|
for (uint32_t i = 0; i < m;) {
|
|
uint32_t j = i + 1;
|
|
/* the group in node order, as PhpCryoReader sums it */
|
|
cnum bytes = items[i].bytes, smallest = items[i].bytes;
|
|
while (j < m && items[j].type == items[i].type && items[j].hash == items[i].hash
|
|
&& items[j].len == items[i].len) {
|
|
bytes = cn_add(bytes, items[j].bytes);
|
|
if (cn_cmp(items[j].bytes, smallest) < 0) smallest = items[j].bytes;
|
|
j++;
|
|
}
|
|
if (j - i >= 2) {
|
|
dupe_row g = items[i];
|
|
g.count = j - i;
|
|
g.bytes = bytes;
|
|
g.wasted = cn_sub(bytes, smallest);
|
|
items[ng++] = g;
|
|
}
|
|
i = j;
|
|
}
|
|
if (ng) qsort(items, ng, sizeof(dupe_row), dupe_rank_cmp);
|
|
|
|
for (uint32_t i = 0; i < ng && (zend_long) i < limit; i++) {
|
|
char hex[9];
|
|
snprintf(hex, sizeof(hex), "%08x", (unsigned int) items[i].hash);
|
|
zval row;
|
|
array_init(&row);
|
|
add_assoc_string(&row, "type", NODE_TYPE(items[i].type));
|
|
add_assoc_string(&row, "hash", hex);
|
|
add_assoc_u64(&row, "length", items[i].len);
|
|
add_assoc_long(&row, "count", (zend_long) items[i].count);
|
|
add_assoc_cn(&row, "bytes", items[i].bytes);
|
|
add_assoc_cn(&row, "wasted", items[i].wasted);
|
|
add_assoc_long(&row, "node", (zend_long) items[i].node);
|
|
add_next_index_zval(return_value, &row);
|
|
}
|
|
efree(items);
|
|
}
|
|
|
|
/*
|
|
* Query language: `class=App\* and retained>1M or type=string and size>=10K`
|
|
* term = field op value field: class type site write size retained epoch
|
|
* op = = != > < >= <= (strings: = and != only, `*` is a wildcard)
|
|
* value = "quoted" | run of non-space characters; numbers take K / M / G (1024)
|
|
* `and` binds tighter than `or`. Unknown values (no site, no epoch...) only
|
|
* match `!=`.
|
|
*/
|
|
enum { QF_CLASS, QF_TYPE, QF_SITE, QF_WRITE, QF_SIZE, QF_RETAINED, QF_EPOCH };
|
|
enum { QO_EQ, QO_NE, QO_GT, QO_LT, QO_GE, QO_LE };
|
|
static const char *q_fields[] = { "class", "type", "site", "write", "size", "retained", "epoch" };
|
|
|
|
typedef struct {
|
|
uint8_t field, op;
|
|
bool or_before; /* starts a new `or` group */
|
|
double num;
|
|
const char *str; /* points into the expression */
|
|
size_t str_len;
|
|
} qcond;
|
|
|
|
static inline bool q_space(char c) { return c == ' ' || c == '\t' || c == '\n' || c == '\r'; }
|
|
|
|
/* Parse; on error returns false and sets *err to the offset of the problem. */
|
|
static bool q_parse(const char *src, size_t len, qcond **out, uint32_t *count, size_t *err)
|
|
{
|
|
size_t p = 0;
|
|
uint32_t n = 0, cap = 0;
|
|
qcond *c = NULL;
|
|
bool or_next = false;
|
|
while (1) {
|
|
while (p < len && q_space(src[p])) p++;
|
|
size_t term = p, f0 = p;
|
|
while (p < len && ((src[p] >= 'a' && src[p] <= 'z') || src[p] == '_')) p++;
|
|
int field = -1;
|
|
for (int k = 0; k < 7; k++) {
|
|
if (strlen(q_fields[k]) == p - f0 && memcmp(q_fields[k], src + f0, p - f0) == 0) field = k;
|
|
}
|
|
if (field < 0) { *err = term; goto fail; }
|
|
while (p < len && q_space(src[p])) p++;
|
|
int op;
|
|
if (p + 1 < len && src[p] == '>' && src[p + 1] == '=') { op = QO_GE; p += 2; }
|
|
else if (p + 1 < len && src[p] == '<' && src[p + 1] == '=') { op = QO_LE; p += 2; }
|
|
else if (p + 1 < len && src[p] == '!' && src[p + 1] == '=') { op = QO_NE; p += 2; }
|
|
else if (p < len && src[p] == '=') { op = QO_EQ; p++; }
|
|
else if (p < len && src[p] == '>') { op = QO_GT; p++; }
|
|
else if (p < len && src[p] == '<') { op = QO_LT; p++; }
|
|
else { *err = p; goto fail; }
|
|
while (p < len && q_space(src[p])) p++;
|
|
size_t v0 = p, vlen;
|
|
if (p < len && src[p] == '"') {
|
|
const char *q = memchr(src + p + 1, '"', len - p - 1);
|
|
if (!q) { *err = p; goto fail; }
|
|
v0 = p + 1;
|
|
vlen = (size_t) (q - src) - v0;
|
|
p = (size_t) (q - src) + 1;
|
|
} else {
|
|
while (p < len && !q_space(src[p])) p++;
|
|
vlen = p - v0;
|
|
if (!vlen) { *err = v0; goto fail; }
|
|
}
|
|
if (n == cap) { cap = cap ? cap * 2 : 4; c = erealloc(c, cap * sizeof(qcond)); }
|
|
c[n].field = (uint8_t) field;
|
|
c[n].op = (uint8_t) op;
|
|
c[n].or_before = or_next;
|
|
c[n].str = src + v0;
|
|
c[n].str_len = vlen;
|
|
c[n].num = 0;
|
|
if (field >= QF_SIZE) {
|
|
size_t d = 0;
|
|
while (d < vlen && src[v0 + d] >= '0' && src[v0 + d] <= '9') d++;
|
|
double mult = 1;
|
|
if (d + 1 == vlen) {
|
|
char u = src[v0 + d];
|
|
if (u == 'k' || u == 'K') mult = 1024.0;
|
|
else if (u == 'm' || u == 'M') mult = 1048576.0;
|
|
else if (u == 'g' || u == 'G') mult = 1073741824.0;
|
|
else { *err = v0; goto fail; }
|
|
} else if (d != vlen) {
|
|
*err = v0; goto fail;
|
|
}
|
|
if (d == 0 || d > 15) { *err = v0; goto fail; }
|
|
double v = 0;
|
|
for (size_t k = 0; k < d; k++) v = v * 10 + (src[v0 + k] - '0');
|
|
c[n].num = v * mult;
|
|
} else if (op != QO_EQ && op != QO_NE) {
|
|
*err = term; goto fail;
|
|
}
|
|
n++;
|
|
while (p < len && q_space(src[p])) p++;
|
|
if (p >= len) break;
|
|
size_t w0 = p;
|
|
while (p < len && ((src[p] | 32) >= 'a' && (src[p] | 32) <= 'z')) p++;
|
|
if (p - w0 == 3 && (src[w0] | 32) == 'a' && (src[w0 + 1] | 32) == 'n' && (src[w0 + 2] | 32) == 'd') {
|
|
or_next = false;
|
|
} else if (p - w0 == 2 && (src[w0] | 32) == 'o' && (src[w0 + 1] | 32) == 'r') {
|
|
or_next = true;
|
|
} else {
|
|
*err = w0; goto fail;
|
|
}
|
|
}
|
|
*out = c;
|
|
*count = n;
|
|
return true;
|
|
fail:
|
|
if (c) efree(c);
|
|
return false;
|
|
}
|
|
|
|
static bool q_str_cond(const qcond *c, const char *v, size_t vlen)
|
|
{
|
|
if (!v) return c->op == QO_NE;
|
|
bool m = cryo_glob(c->str, c->str_len, v, vlen, false);
|
|
return c->op == QO_EQ ? m : !m;
|
|
}
|
|
|
|
static bool q_num_cond(const qcond *c, bool known, double v)
|
|
{
|
|
if (!known) return c->op == QO_NE;
|
|
switch (c->op) {
|
|
case QO_EQ: return v == c->num;
|
|
case QO_NE: return v != c->num;
|
|
case QO_GT: return v > c->num;
|
|
case QO_LT: return v < c->num;
|
|
case QO_GE: return v >= c->num;
|
|
default: return v <= c->num;
|
|
}
|
|
}
|
|
|
|
static bool q_site_str(const cryo_snap *s, uint32_t site, const char **v, size_t *vlen)
|
|
{
|
|
if (site == CRYO_NONE || site >= s->nsites) return false;
|
|
uint32_t si = s->sites[site];
|
|
bool ok = si < s->nstr && s->str[si];
|
|
*v = ok ? s->str[si] : "";
|
|
*vlen = ok ? s->str_len[si] : 0;
|
|
return true;
|
|
}
|
|
|
|
static bool q_match(const cryo_snap *s, const qcond *c, uint32_t nc, uint32_t v)
|
|
{
|
|
const cryo_node_rec *nd = &s->nodes[v];
|
|
bool any = false, group = true;
|
|
for (uint32_t i = 0; i < nc; i++) {
|
|
if (c[i].or_before) {
|
|
if (group) return true;
|
|
group = true;
|
|
}
|
|
if (!group) continue;
|
|
const char *str = NULL; size_t sl = 0;
|
|
bool ok;
|
|
switch (c[i].field) {
|
|
case QF_CLASS:
|
|
if (nd->class_str != CRYO_NONE && nd->class_str < s->nstr && s->str[nd->class_str]) {
|
|
str = s->str[nd->class_str]; sl = s->str_len[nd->class_str];
|
|
}
|
|
ok = q_str_cond(&c[i], str, sl);
|
|
break;
|
|
case QF_TYPE:
|
|
str = NODE_TYPE(nd->type); sl = strlen(str);
|
|
ok = q_str_cond(&c[i], str, sl);
|
|
break;
|
|
case QF_SITE:
|
|
if (!q_site_str(s, nd->site, &str, &sl)) str = NULL;
|
|
ok = q_str_cond(&c[i], str, sl);
|
|
break;
|
|
case QF_WRITE:
|
|
if (v >= s->nwrit || !q_site_str(s, s->writ[v], &str, &sl)) str = NULL;
|
|
ok = q_str_cond(&c[i], str, sl);
|
|
break;
|
|
case QF_SIZE:
|
|
ok = q_num_cond(&c[i], true, (double) nd->size);
|
|
break;
|
|
case QF_RETAINED:
|
|
ok = q_num_cond(&c[i], true, s->dom_idom[v] == CRYO_NONE ? 0.0 : cn_d(s->dom_ret[v]));
|
|
break;
|
|
default:
|
|
ok = q_num_cond(&c[i], nd->epoch != CRYO_NONE, (double) nd->epoch);
|
|
break;
|
|
}
|
|
if (!ok) group = false;
|
|
any = true;
|
|
}
|
|
return !any || group;
|
|
}
|
|
|
|
/*
|
|
* query(string|array $expr): node records matching, by node id. A string is
|
|
* an expression of the query language (above); an array holds filters:
|
|
* type (array|object|string|ref), class (exact), site (substring, nodes
|
|
* with a known site only), min_size, epoch_min, epoch_max (nodes with a
|
|
* known epoch only), expr (query language), limit (default 100)
|
|
*/
|
|
ZEND_METHOD(CryoReader, query)
|
|
{
|
|
HashTable *filter = NULL;
|
|
zend_string *expr = NULL;
|
|
ZEND_PARSE_PARAMETERS_START(1, 1)
|
|
Z_PARAM_ARRAY_HT_OR_STR(filter, expr)
|
|
ZEND_PARSE_PARAMETERS_END();
|
|
cryo_reader_obj *r = READER_THIS();
|
|
REQUIRE_LOADED(r);
|
|
cryo_snap *s = &r->snap;
|
|
qcond *qc = NULL;
|
|
uint32_t nq = 0;
|
|
if (expr) {
|
|
zend_string_addref(expr);
|
|
}
|
|
|
|
uint8_t type = 0;
|
|
zend_string *cls = NULL, *site = NULL;
|
|
bool has_emin = false, has_emax = false;
|
|
zend_long min_size = 0, emin = 0, emax = 0, limit = 100;
|
|
zend_string *key; zend_ulong idx; zval *val;
|
|
if (filter) ZEND_HASH_FOREACH_KEY_VAL(filter, idx, key, val) {
|
|
(void) idx;
|
|
if (!key) {
|
|
zend_argument_value_error(1, "must only have string keys");
|
|
goto fail;
|
|
}
|
|
if (zend_string_equals_literal(key, "type")) {
|
|
zend_string *t = zval_get_string(val);
|
|
type = 0;
|
|
for (uint8_t k = 1; k <= 4; k++) {
|
|
if (zend_string_equals_cstr(t, node_types[k], strlen(node_types[k]))) type = k;
|
|
}
|
|
zend_string_release(t);
|
|
if (!type) {
|
|
zend_argument_value_error(1, "key \"type\" must be \"array\", \"object\", \"string\" or \"ref\"");
|
|
goto fail;
|
|
}
|
|
} else if (zend_string_equals_literal(key, "class")) {
|
|
if (cls) zend_string_release(cls);
|
|
cls = zval_get_string(val);
|
|
} else if (zend_string_equals_literal(key, "site")) {
|
|
if (site) zend_string_release(site);
|
|
site = zval_get_string(val);
|
|
} else if (zend_string_equals_literal(key, "min_size")) {
|
|
min_size = zval_get_long(val);
|
|
} else if (zend_string_equals_literal(key, "epoch_min")) {
|
|
has_emin = true; emin = zval_get_long(val);
|
|
} else if (zend_string_equals_literal(key, "epoch_max")) {
|
|
has_emax = true; emax = zval_get_long(val);
|
|
} else if (zend_string_equals_literal(key, "limit")) {
|
|
limit = zval_get_long(val);
|
|
} else if (zend_string_equals_literal(key, "expr")) {
|
|
if (expr) zend_string_release(expr);
|
|
expr = zval_get_string(val);
|
|
} else {
|
|
zend_argument_value_error(1, "has an unknown key \"%s\"", ZSTR_VAL(key));
|
|
goto fail;
|
|
}
|
|
} ZEND_HASH_FOREACH_END();
|
|
|
|
if (expr) {
|
|
size_t err = 0;
|
|
if (!q_parse(ZSTR_VAL(expr), ZSTR_LEN(expr), &qc, &nq, &err)) {
|
|
zend_argument_value_error(1, "invalid query near \"%s\"", ZSTR_VAL(expr) + err);
|
|
goto fail;
|
|
}
|
|
for (uint32_t i = 0; i < nq; i++) {
|
|
if (qc[i].field == QF_RETAINED) snap_dom(s);
|
|
}
|
|
}
|
|
|
|
array_init(return_value);
|
|
zend_long found = 0;
|
|
for (uint32_t v = 0; v < s->nnodes && found < limit; v++) {
|
|
const cryo_node_rec *nd = &s->nodes[v];
|
|
if (type && nd->type != type) continue;
|
|
if (nq && !q_match(s, qc, nq, v)) continue;
|
|
if (cls) {
|
|
uint32_t c = nd->class_str;
|
|
if (c == CRYO_NONE || c >= s->nstr || !s->str[c] || s->str_len[c] != ZSTR_LEN(cls)
|
|
|| memcmp(s->str[c], ZSTR_VAL(cls), ZSTR_LEN(cls)) != 0) continue;
|
|
}
|
|
if (site) {
|
|
if (nd->site == CRYO_NONE || nd->site >= s->nsites) continue;
|
|
uint32_t si = s->sites[nd->site];
|
|
bool ok = si < s->nstr && s->str[si];
|
|
const char *hs = ok ? s->str[si] : "";
|
|
size_t hl = ok ? s->str_len[si] : 0;
|
|
if (ZSTR_LEN(site) && !zend_memnstr(hs, ZSTR_VAL(site), ZSTR_LEN(site), hs + hl)) continue;
|
|
}
|
|
if (min_size > 0 && nd->size < (uint64_t) min_size) continue;
|
|
if (has_emin && (nd->epoch == CRYO_NONE || (int64_t) nd->epoch < (int64_t) emin)) continue;
|
|
if (has_emax && (nd->epoch == CRYO_NONE || (int64_t) nd->epoch > (int64_t) emax)) continue;
|
|
zval row;
|
|
array_init(&row);
|
|
add_node_record(&row, s, v);
|
|
add_next_index_zval(return_value, &row);
|
|
found++;
|
|
}
|
|
if (cls) zend_string_release(cls);
|
|
if (site) zend_string_release(site);
|
|
if (expr) zend_string_release(expr);
|
|
if (qc) efree(qc);
|
|
return;
|
|
|
|
fail:
|
|
if (cls) zend_string_release(cls);
|
|
if (site) zend_string_release(site);
|
|
if (expr) zend_string_release(expr);
|
|
RETURN_THROWS();
|
|
}
|
|
|
|
static void add_edge_list(zval *arr, const char *key, const cryo_snap *s,
|
|
const uint32_t *off, const uint32_t *eidx, uint32_t v, bool out)
|
|
{
|
|
zval list;
|
|
array_init(&list);
|
|
for (uint32_t j = off[v]; j < off[v + 1]; j++) {
|
|
const cryo_edge_rec *e = &s->edges[eidx[j]];
|
|
uint32_t other = out ? e->to : e->from;
|
|
zval it;
|
|
array_init(&it);
|
|
add_assoc_str(&it, "name", snap_str(s, e->name_str));
|
|
add_assoc_long(&it, "node", (zend_long) other);
|
|
add_type_class(&it, s, other);
|
|
add_next_index_zval(&list, &it);
|
|
}
|
|
add_assoc_zval(arr, key, &list);
|
|
}
|
|
|
|
/* node(int $id): node record + out / in edges + roots pointing to it */
|
|
ZEND_METHOD(CryoReader, node)
|
|
{
|
|
zend_long id;
|
|
ZEND_PARSE_PARAMETERS_START(1, 1)
|
|
Z_PARAM_LONG(id)
|
|
ZEND_PARSE_PARAMETERS_END();
|
|
cryo_reader_obj *r = READER_THIS();
|
|
REQUIRE_LOADED(r);
|
|
cryo_snap *s = &r->snap;
|
|
if (id < 0 || (zend_ulong) id >= s->nnodes) {
|
|
RETURN_NULL();
|
|
}
|
|
snap_build_adj(s);
|
|
uint32_t v = (uint32_t) id;
|
|
array_init(return_value);
|
|
add_node_record(return_value, s, v);
|
|
const char *frames = NULL;
|
|
uint32_t nf = snap_stack(s, v, &frames);
|
|
if (nf) {
|
|
zval stack;
|
|
array_init_size(&stack, nf);
|
|
for (uint32_t i = 0; i < nf; i++) {
|
|
add_next_index_str(&stack, snap_frame(s, frames, i));
|
|
}
|
|
add_assoc_zval(return_value, "stack", &stack);
|
|
} else {
|
|
add_assoc_null(return_value, "stack");
|
|
}
|
|
add_edge_list(return_value, "out", s, s->out_off, s->out_e, v, true);
|
|
add_edge_list(return_value, "in", s, s->in_off, s->in_e, v, false);
|
|
zval roots;
|
|
array_init(&roots);
|
|
for (uint32_t i = 0; i < s->nroots; i++) {
|
|
if (s->roots[i].node != v) continue;
|
|
zval it;
|
|
array_init(&it);
|
|
add_assoc_string(&it, "kind", ROOT_KIND(s->roots[i].kind));
|
|
add_assoc_str(&it, "name", snap_str(s, s->roots[i].name_str));
|
|
add_next_index_zval(&roots, &it);
|
|
}
|
|
add_assoc_zval(return_value, "roots", &roots);
|
|
}
|
|
|
|
/* ------------------------------------------------------------------------ */
|
|
/* Heuristics */
|
|
/* ------------------------------------------------------------------------ */
|
|
|
|
#define H_STATIC_PCT 10 /* static cache: retains more than 10 % of the reachable heap */
|
|
#define H_LISTENERS_MIN 10 /* listeners: arrays of 10+ elements, 80 % callables */
|
|
#define H_LOG_MIN 100 /* in-memory log: arrays of 100+ elements, 90 % strings */
|
|
|
|
static void h_row(zval *out, cryo_snap *s, const char *kind, zend_long score, zend_string *msg,
|
|
uint32_t node, zend_string *site, cnum bytes)
|
|
{
|
|
zval row;
|
|
array_init(&row);
|
|
add_assoc_string(&row, "kind", kind);
|
|
add_assoc_long(&row, "score", score > 100 ? 100 : score);
|
|
add_assoc_str(&row, "message", msg);
|
|
if (node != CRYO_NONE) add_assoc_long(&row, "node", (zend_long) node); else add_assoc_null(&row, "node");
|
|
if (site) add_assoc_str(&row, "site", site); else add_assoc_null(&row, "site");
|
|
add_assoc_cn(&row, "bytes", bytes);
|
|
zval paths;
|
|
array_init(&paths);
|
|
if (node != CRYO_NONE) snap_paths(&paths, s, node, 1);
|
|
zval *p = zend_hash_index_find(Z_ARRVAL(paths), 0);
|
|
if (p) {
|
|
Z_TRY_ADDREF_P(p);
|
|
add_assoc_zval(&row, "path", p);
|
|
} else {
|
|
add_assoc_null(&row, "path");
|
|
}
|
|
zval_ptr_dtor(&paths);
|
|
add_next_index_zval(out, &row);
|
|
}
|
|
|
|
/* prefix + name + suffix: names come from the file and may hold NUL bytes
|
|
* (a "%s" would cut them where PhpCryoReader keeps them) */
|
|
static zend_string *h_msg(const char *prefix, zend_string *name, const char *fmt, zend_long n)
|
|
{
|
|
smart_str m = {0};
|
|
smart_str_appends(&m, prefix);
|
|
smart_str_append(&m, name);
|
|
smart_str_append_printf(&m, fmt, n);
|
|
smart_str_0(&m);
|
|
return m.s;
|
|
}
|
|
|
|
static bool h_class_is(const cryo_snap *s, uint32_t v, const char *name)
|
|
{
|
|
uint32_t c = s->nodes[v].class_str;
|
|
size_t l = strlen(name);
|
|
return s->nodes[v].type == CRYO_NODE_OBJECT && c < s->nstr && s->str[c]
|
|
&& s->str_len[c] == l && memcmp(s->str[c], name, l) == 0;
|
|
}
|
|
|
|
/* a callable: Closure, or [object, "method"] */
|
|
static bool h_callable(const cryo_snap *s, uint32_t t)
|
|
{
|
|
if (h_class_is(s, t, "Closure")) return true;
|
|
if (s->nodes[t].type != CRYO_NODE_ARRAY || s->out_off[t + 1] - s->out_off[t] != 2) return false;
|
|
uint8_t a = s->nodes[s->edges[s->out_e[s->out_off[t]]].to].type;
|
|
uint8_t b = s->nodes[s->edges[s->out_e[s->out_off[t] + 1]].to].type;
|
|
return (a == CRYO_NODE_OBJECT && b == CRYO_NODE_STRING) || (a == CRYO_NODE_STRING && b == CRYO_NODE_OBJECT);
|
|
}
|
|
|
|
/*
|
|
* heuristics(array $files = []): suspicious patterns, by score desc
|
|
* (stable: orphans, static caches, listeners, in-memory logs, growth).
|
|
* Rows: kind, score (0-100), message, node, site, bytes, path (first
|
|
* shortest path to the node, or null). With 2+ snapshot files, growing
|
|
* allocation sites of their delta are reported too ("growth").
|
|
*/
|
|
ZEND_METHOD(CryoReader, heuristics)
|
|
{
|
|
HashTable *files = NULL;
|
|
ZEND_PARSE_PARAMETERS_START(0, 1)
|
|
Z_PARAM_OPTIONAL
|
|
Z_PARAM_ARRAY_HT(files)
|
|
ZEND_PARSE_PARAMETERS_END();
|
|
cryo_reader_obj *r = READER_THIS();
|
|
REQUIRE_LOADED(r);
|
|
cryo_snap *s = &r->snap;
|
|
snap_dom(s);
|
|
uint32_t n = s->nnodes, S = n;
|
|
|
|
zval rows;
|
|
array_init(&rows);
|
|
cnum total = cn_long(0), reach = cn_long(0), zero = cn_long(0);
|
|
for (uint32_t v = 0; v < n; v++) {
|
|
total = cn_add(total, cn_u64(s->nodes[v].size));
|
|
if (s->dom_idom[v] != CRYO_NONE) reach = cn_add(reach, cn_u64(s->nodes[v].size));
|
|
}
|
|
|
|
/* 1. orphans */
|
|
{
|
|
uint8_t *mark = ecalloc(n ? n : 1, 1);
|
|
uint32_t *queue = safe_emalloc(n ? n : 1, sizeof(uint32_t), 0);
|
|
bfs_mark(s, mark, queue, false, 1);
|
|
bfs_mark(s, mark, queue, true, 2);
|
|
cnum bytes = cn_long(0);
|
|
uint32_t objects = 0, first = CRYO_NONE;
|
|
for (uint32_t v = 0; v < n; v++) {
|
|
if (mark[v] != 2) continue;
|
|
bytes = cn_add(bytes, cn_u64(s->nodes[v].size));
|
|
if (s->nodes[v].type == CRYO_NODE_OBJECT) objects++;
|
|
}
|
|
for (uint32_t i = 0; i < s->nroots && first == CRYO_NONE; i++) {
|
|
if (s->roots[i].kind == CRYO_ROOT_ORPHAN && s->roots[i].node < n) first = s->roots[i].node;
|
|
}
|
|
if (objects) {
|
|
zend_long pct = cn_cmp(total, zero) ? cn_div_floor(cn_mul(bytes, 100), total) : 0;
|
|
h_row(&rows, s, "orphans", 50 + pct,
|
|
zend_strpprintf(0, "%u objects kept alive only by uncollected cycles", objects), first, NULL, bytes);
|
|
}
|
|
efree(mark);
|
|
efree(queue);
|
|
}
|
|
|
|
/* 2. static caches */
|
|
{
|
|
uint8_t *done = ecalloc(n ? n : 1, 1);
|
|
for (uint32_t i = 0; i < s->nroots; i++) {
|
|
const cryo_root_rec *rt = &s->roots[i];
|
|
if ((rt->kind != CRYO_ROOT_STATIC_PROP && rt->kind != CRYO_ROOT_STATIC_VAR) || rt->node >= n || done[rt->node]) continue;
|
|
done[rt->node] = 1;
|
|
cnum ret = s->dom_idom[rt->node] == S ? s->dom_ret[rt->node] : zero;
|
|
if (!cn_cmp(reach, zero) || cn_cmp(cn_mul(ret, 100), cn_mul(reach, H_STATIC_PCT)) <= 0) continue;
|
|
zend_long pct = cn_div_floor(cn_mul(ret, 100), reach);
|
|
zend_string *name = snap_str(s, rt->name_str);
|
|
h_row(&rows, s, "static_cache", pct,
|
|
h_msg("", name, " retains " ZEND_LONG_FMT "%% of the reachable heap", pct), rt->node, NULL, ret);
|
|
zend_string_release(name);
|
|
}
|
|
efree(done);
|
|
}
|
|
|
|
/* 3. listeners, 4. in-memory logs */
|
|
for (int pass = 0; pass < 2; pass++) {
|
|
for (uint32_t v = 0; v < n; v++) {
|
|
if (s->nodes[v].type != CRYO_NODE_ARRAY) continue;
|
|
uint32_t deg = s->out_off[v + 1] - s->out_off[v], hits = 0;
|
|
if (deg < (pass ? H_LOG_MIN : H_LISTENERS_MIN)) continue;
|
|
for (uint32_t j = s->out_off[v]; j < s->out_off[v + 1]; j++) {
|
|
uint32_t t = s->edges[s->out_e[j]].to;
|
|
if (pass ? s->nodes[t].type == CRYO_NODE_STRING : h_callable(s, t)) hits++;
|
|
}
|
|
cnum ret = s->dom_idom[v] != CRYO_NONE ? s->dom_ret[v] : zero;
|
|
if (!pass && (uint64_t) hits * 10 >= (uint64_t) deg * 8) {
|
|
h_row(&rows, s, "listeners", 10 + hits,
|
|
zend_strpprintf(0, "array of %u callables (listeners) that may keep their objects alive", hits), v, NULL, ret);
|
|
} else if (pass && (uint64_t) hits * 10 >= (uint64_t) deg * 9) {
|
|
h_row(&rows, s, "memory_log", 20 + hits / 20,
|
|
zend_strpprintf(0, "array of %u strings (in-memory log?)", hits), v, NULL, ret);
|
|
}
|
|
}
|
|
}
|
|
|
|
/* 5. growth over the given snapshots */
|
|
if (files && zend_hash_num_elements(files) >= 2) {
|
|
uint32_t nf = zend_hash_num_elements(files), loaded = 0;
|
|
cryo_snap *snaps = ecalloc(nf, sizeof(cryo_snap));
|
|
zval *f;
|
|
bool ok = true;
|
|
ZEND_HASH_FOREACH_VAL(files, f) {
|
|
if (Z_TYPE_P(f) != IS_STRING) {
|
|
zend_argument_type_error(1, "must contain only file paths");
|
|
ok = false;
|
|
break;
|
|
}
|
|
if (snap_load(&snaps[loaded], Z_STR_P(f)) == FAILURE) { ok = false; break; }
|
|
loaded++;
|
|
} ZEND_HASH_FOREACH_END();
|
|
if (ok) {
|
|
for (uint32_t i = 1; i < nf; i++) {
|
|
for (uint32_t j = i; j > 0 && snaps[j].epoch < snaps[j - 1].epoch; j--) {
|
|
cryo_snap t = snaps[j]; snaps[j] = snaps[j - 1]; snaps[j - 1] = t;
|
|
}
|
|
}
|
|
zval drows, *dr;
|
|
delta_rows(&drows, snaps, nf, BY_SITE);
|
|
ZEND_HASH_FOREACH_VAL(Z_ARRVAL(drows), dr) {
|
|
HashTable *h = Z_ARRVAL_P(dr);
|
|
if (!zend_is_true(zend_hash_str_find(h, "growing", 7))) continue;
|
|
zend_string *site = Z_STR_P(zend_hash_str_find(h, "site", 4));
|
|
zend_long surv = Z_LVAL_P(zend_hash_str_find(h, "survivors", 9));
|
|
double survival = Z_DVAL_P(zend_hash_str_find(h, "survival", 8));
|
|
zval *zb = zend_hash_str_find(h, "bytes_last", 10);
|
|
cnum bytes = Z_TYPE_P(zb) == IS_DOUBLE ? cn_dbl(Z_DVAL_P(zb)) : cn_long(Z_LVAL_P(zb));
|
|
h_row(&rows, s, "growth", 50 + (zend_long) (survival * 50),
|
|
h_msg("objects allocated at ", site, " grow at every snapshot (" ZEND_LONG_FMT " survivors)", surv),
|
|
CRYO_NONE, zend_string_copy(site), bytes);
|
|
} ZEND_HASH_FOREACH_END();
|
|
zval_ptr_dtor(&drows);
|
|
}
|
|
for (uint32_t i = 0; i < loaded; i++) snap_free(&snaps[i]);
|
|
efree(snaps);
|
|
if (!ok) {
|
|
zval_ptr_dtor(&rows);
|
|
RETURN_THROWS();
|
|
}
|
|
}
|
|
|
|
/* stable sort by score desc */
|
|
uint32_t nr = zend_hash_num_elements(Z_ARRVAL(rows)), i = 0;
|
|
zval *items = safe_emalloc(nr ? nr : 1, sizeof(zval), 0), *e;
|
|
sort_key *ord = safe_emalloc(nr ? nr : 1, sizeof(sort_key), 0);
|
|
ZEND_HASH_FOREACH_VAL(Z_ARRVAL(rows), e) {
|
|
ZVAL_COPY(&items[i], e);
|
|
ord[i].k1 = Z_LVAL_P(zend_hash_str_find(Z_ARRVAL_P(e), "score", 5));
|
|
ord[i].k2 = 0;
|
|
ord[i].idx = i;
|
|
i++;
|
|
} ZEND_HASH_FOREACH_END();
|
|
if (nr) qsort(ord, nr, sizeof(sort_key), sort_key_cmp);
|
|
array_init_size(return_value, nr);
|
|
for (i = 0; i < nr; i++) add_next_index_zval(return_value, &items[ord[i].idx]);
|
|
efree(ord);
|
|
efree(items);
|
|
zval_ptr_dtor(&rows);
|
|
}
|
|
|
|
/* ------------------------------------------------------------------------ */
|
|
/* Live analysis and diff (functions, see cryo_analysis.h) */
|
|
/* ------------------------------------------------------------------------ */
|
|
|
|
/* cryo_roots_of(object $o, int $max = 5): up to $max shortest paths from the
|
|
* roots to $o in the running process (the variable passed is not a root). */
|
|
ZEND_FUNCTION(cryo_roots_of)
|
|
{
|
|
zend_object *obj;
|
|
zend_long max = 5;
|
|
ZEND_PARSE_PARAMETERS_START(1, 2)
|
|
Z_PARAM_OBJ(obj)
|
|
Z_PARAM_OPTIONAL
|
|
Z_PARAM_LONG(max)
|
|
ZEND_PARSE_PARAMETERS_END();
|
|
uint32_t id = CRYO_NONE;
|
|
zend_string *data = cryo_snapshot_mem(NULL, obj, &id);
|
|
cryo_snap s;
|
|
if (snap_parse(&s, data, "(memory)") == FAILURE) {
|
|
RETURN_THROWS();
|
|
}
|
|
array_init(return_value);
|
|
if (id != CRYO_NONE) {
|
|
snap_paths(return_value, &s, id, max);
|
|
}
|
|
snap_free(&s);
|
|
}
|
|
|
|
/* cryo_orphans(): objects of the running process kept alive only by
|
|
* uncollected cycles (no cycle collection first), as node records. */
|
|
ZEND_FUNCTION(cryo_orphans)
|
|
{
|
|
ZEND_PARSE_PARAMETERS_NONE();
|
|
zend_string *data = cryo_snapshot_mem(NULL, NULL, NULL);
|
|
cryo_snap s;
|
|
if (snap_parse(&s, data, "(memory)") == FAILURE) {
|
|
RETURN_THROWS();
|
|
}
|
|
snap_orphans(return_value, &s);
|
|
snap_free(&s);
|
|
}
|
|
|
|
typedef struct { zend_string *cls; zend_long count[2]; cnum bytes[2]; cnum delta; uint32_t order; } diff_row;
|
|
|
|
static int diff_cmp(const void *a, const void *b)
|
|
{
|
|
const diff_row *x = a, *y = b;
|
|
zend_long cx = x->count[1] - x->count[0], cy = y->count[1] - y->count[0];
|
|
int c = cn_cmp(y->delta, x->delta);
|
|
if (c) return c;
|
|
if (cx != cy) return cx < cy ? 1 : -1;
|
|
return x->order < y->order ? -1 : 1;
|
|
}
|
|
|
|
/* cryo_diff(string $a, string $b): objects per class in both snapshots,
|
|
* by bytes_delta desc, count_delta desc (classes in first-seen order). */
|
|
ZEND_FUNCTION(cryo_diff)
|
|
{
|
|
zend_string *files[2];
|
|
ZEND_PARSE_PARAMETERS_START(2, 2)
|
|
Z_PARAM_PATH_STR(files[0])
|
|
Z_PARAM_PATH_STR(files[1])
|
|
ZEND_PARSE_PARAMETERS_END();
|
|
cryo_snap snaps[2];
|
|
if (snap_load(&snaps[0], files[0]) == FAILURE) {
|
|
RETURN_THROWS();
|
|
}
|
|
if (snap_load(&snaps[1], files[1]) == FAILURE) {
|
|
snap_free(&snaps[0]);
|
|
RETURN_THROWS();
|
|
}
|
|
HashTable idx;
|
|
zend_hash_init(&idx, 64, NULL, NULL, 0);
|
|
diff_row *rows = NULL;
|
|
uint32_t nrows = 0, cap = 0;
|
|
for (int f = 0; f < 2; f++) {
|
|
for (uint32_t k = 0; k < snaps[f].nnodes; k++) {
|
|
const cryo_node_rec *nd = &snaps[f].nodes[k];
|
|
if (nd->type != CRYO_NODE_OBJECT) continue;
|
|
zend_string *cls = snap_str(&snaps[f], nd->class_str);
|
|
zval *z = zend_hash_find(&idx, cls);
|
|
uint32_t at;
|
|
if (z) {
|
|
at = (uint32_t) Z_LVAL_P(z);
|
|
zend_string_release(cls);
|
|
} else {
|
|
if (nrows == cap) { cap = cap ? cap * 2 : 32; rows = erealloc(rows, cap * sizeof(diff_row)); }
|
|
at = nrows++;
|
|
memset(&rows[at], 0, sizeof(diff_row)); /* zeros = int 0 */
|
|
rows[at].cls = cls;
|
|
rows[at].order = at;
|
|
zval zz; ZVAL_LONG(&zz, at);
|
|
zend_hash_add_new(&idx, cls, &zz);
|
|
}
|
|
rows[at].count[f]++;
|
|
rows[at].bytes[f] = cn_add(rows[at].bytes[f], cn_u64(nd->size));
|
|
}
|
|
}
|
|
for (uint32_t i = 0; i < nrows; i++) rows[i].delta = cn_sub(rows[i].bytes[1], rows[i].bytes[0]);
|
|
if (nrows) qsort(rows, nrows, sizeof(diff_row), diff_cmp);
|
|
array_init(return_value);
|
|
for (uint32_t i = 0; i < nrows; i++) {
|
|
zval row;
|
|
array_init(&row);
|
|
add_assoc_str(&row, "class", rows[i].cls);
|
|
add_assoc_long(&row, "count_a", rows[i].count[0]);
|
|
add_assoc_long(&row, "count_b", rows[i].count[1]);
|
|
add_assoc_long(&row, "count_delta", rows[i].count[1] - rows[i].count[0]);
|
|
add_assoc_cn(&row, "bytes_a", rows[i].bytes[0]);
|
|
add_assoc_cn(&row, "bytes_b", rows[i].bytes[1]);
|
|
add_assoc_cn(&row, "bytes_delta", rows[i].delta);
|
|
add_next_index_zval(return_value, &row);
|
|
}
|
|
if (rows) efree(rows);
|
|
zend_hash_destroy(&idx);
|
|
snap_free(&snaps[0]);
|
|
snap_free(&snaps[1]);
|
|
}
|
|
|
|
/* ------------------------------------------------------------------------ */
|
|
|
|
ZEND_BEGIN_ARG_INFO_EX(arginfo_reader_construct, 0, 0, 0)
|
|
ZEND_END_ARG_INFO()
|
|
|
|
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_reader_open, 0, 1, IS_STATIC, 0)
|
|
ZEND_ARG_TYPE_INFO(0, file, IS_STRING, 0)
|
|
ZEND_END_ARG_INFO()
|
|
|
|
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_reader_info, 0, 0, IS_ARRAY, 0)
|
|
ZEND_END_ARG_INFO()
|
|
|
|
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_reader_top, 0, 0, IS_ARRAY, 0)
|
|
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, by, IS_STRING, 0, "\"retained\"")
|
|
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, limit, IS_LONG, 0, "20")
|
|
ZEND_END_ARG_INFO()
|
|
|
|
ZEND_BEGIN_ARG_WITH_RETURN_OBJ_INFO_EX(arginfo_reader_delta, 0, 1, CryoDelta, 0)
|
|
ZEND_ARG_TYPE_INFO(0, files, IS_ARRAY, 0)
|
|
ZEND_ARG_TYPE_MASK(0, opts, MAY_BE_ARRAY | MAY_BE_STRING, "[]")
|
|
ZEND_END_ARG_INFO()
|
|
|
|
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_delta_view, 0, 0, IS_ARRAY, 0)
|
|
ZEND_END_ARG_INFO()
|
|
|
|
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_delta_explain, 0, 1, IS_ARRAY, 1)
|
|
ZEND_ARG_TYPE_MASK(0, site, MAY_BE_LONG | MAY_BE_STRING, NULL)
|
|
ZEND_END_ARG_INFO()
|
|
|
|
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_delta_count, 0, 0, IS_LONG, 0)
|
|
ZEND_END_ARG_INFO()
|
|
|
|
ZEND_BEGIN_ARG_WITH_RETURN_OBJ_INFO_EX(arginfo_delta_iter, 0, 0, Iterator, 0)
|
|
ZEND_END_ARG_INFO()
|
|
|
|
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_delta_exists, 0, 1, _IS_BOOL, 0)
|
|
ZEND_ARG_TYPE_INFO(0, offset, IS_MIXED, 0)
|
|
ZEND_END_ARG_INFO()
|
|
|
|
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_delta_get, 0, 1, IS_MIXED, 0)
|
|
ZEND_ARG_TYPE_INFO(0, offset, IS_MIXED, 0)
|
|
ZEND_END_ARG_INFO()
|
|
|
|
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_delta_set, 0, 2, IS_VOID, 0)
|
|
ZEND_ARG_TYPE_INFO(0, offset, IS_MIXED, 0)
|
|
ZEND_ARG_TYPE_INFO(0, value, IS_MIXED, 0)
|
|
ZEND_END_ARG_INFO()
|
|
|
|
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_delta_unset, 0, 1, IS_VOID, 0)
|
|
ZEND_ARG_TYPE_INFO(0, offset, IS_MIXED, 0)
|
|
ZEND_END_ARG_INFO()
|
|
|
|
static const zend_function_entry cryo_delta_methods[] = {
|
|
ZEND_ME(CryoDelta, __construct, arginfo_reader_construct, ZEND_ACC_PRIVATE)
|
|
ZEND_ME(CryoDelta, bySite, arginfo_delta_view, ZEND_ACC_PUBLIC)
|
|
ZEND_ME(CryoDelta, byClass, arginfo_delta_view, ZEND_ACC_PUBLIC)
|
|
ZEND_ME(CryoDelta, byStack, arginfo_delta_view, ZEND_ACC_PUBLIC)
|
|
ZEND_ME(CryoDelta, timeline, arginfo_delta_view, ZEND_ACC_PUBLIC)
|
|
ZEND_ME(CryoDelta, explain, arginfo_delta_explain, ZEND_ACC_PUBLIC)
|
|
ZEND_ME(CryoDelta, count, arginfo_delta_count, ZEND_ACC_PUBLIC)
|
|
ZEND_ME(CryoDelta, getIterator, arginfo_delta_iter, ZEND_ACC_PUBLIC)
|
|
ZEND_ME(CryoDelta, offsetExists, arginfo_delta_exists, ZEND_ACC_PUBLIC)
|
|
ZEND_ME(CryoDelta, offsetGet, arginfo_delta_get, ZEND_ACC_PUBLIC)
|
|
ZEND_ME(CryoDelta, offsetSet, arginfo_delta_set, ZEND_ACC_PUBLIC)
|
|
ZEND_ME(CryoDelta, offsetUnset, arginfo_delta_unset, ZEND_ACC_PUBLIC)
|
|
ZEND_FE_END
|
|
};
|
|
|
|
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_reader_path, 0, 1, IS_ARRAY, 0)
|
|
ZEND_ARG_TYPE_INFO(0, node, IS_LONG, 0)
|
|
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, max, IS_LONG, 0, "5")
|
|
ZEND_END_ARG_INFO()
|
|
|
|
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_reader_limit, 0, 0, IS_ARRAY, 0)
|
|
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, limit, IS_LONG, 0, "20")
|
|
ZEND_END_ARG_INFO()
|
|
|
|
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_reader_query, 0, 1, IS_ARRAY, 0)
|
|
ZEND_ARG_TYPE_MASK(0, expr, MAY_BE_ARRAY | MAY_BE_STRING, NULL)
|
|
ZEND_END_ARG_INFO()
|
|
|
|
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_reader_node, 0, 1, IS_ARRAY, 1)
|
|
ZEND_ARG_TYPE_INFO(0, id, IS_LONG, 0)
|
|
ZEND_END_ARG_INFO()
|
|
|
|
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_reader_heuristics, 0, 0, IS_ARRAY, 0)
|
|
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, files, IS_ARRAY, 0, "[]")
|
|
ZEND_END_ARG_INFO()
|
|
|
|
static const zend_function_entry cryo_reader_methods[] = {
|
|
ZEND_ME(CryoReader, __construct, arginfo_reader_construct, ZEND_ACC_PRIVATE)
|
|
ZEND_ME(CryoReader, open, arginfo_reader_open, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC)
|
|
ZEND_ME(CryoReader, info, arginfo_reader_info, ZEND_ACC_PUBLIC)
|
|
ZEND_ME(CryoReader, top, arginfo_reader_top, ZEND_ACC_PUBLIC)
|
|
ZEND_ME(CryoReader, roots, arginfo_reader_info, ZEND_ACC_PUBLIC)
|
|
ZEND_ME(CryoReader, delta, arginfo_reader_delta, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC)
|
|
ZEND_ME(CryoReader, path, arginfo_reader_path, ZEND_ACC_PUBLIC)
|
|
ZEND_ME(CryoReader, dominators, arginfo_reader_limit, ZEND_ACC_PUBLIC)
|
|
ZEND_ME(CryoReader, orphans, arginfo_reader_info, ZEND_ACC_PUBLIC)
|
|
ZEND_ME(CryoReader, dupes, arginfo_reader_limit, ZEND_ACC_PUBLIC)
|
|
ZEND_ME(CryoReader, query, arginfo_reader_query, ZEND_ACC_PUBLIC)
|
|
ZEND_ME(CryoReader, node, arginfo_reader_node, ZEND_ACC_PUBLIC)
|
|
ZEND_ME(CryoReader, heuristics, arginfo_reader_heuristics, ZEND_ACC_PUBLIC)
|
|
ZEND_FE_END
|
|
};
|
|
|
|
void cryo_reader_minit(void)
|
|
{
|
|
zend_class_entry ce;
|
|
INIT_CLASS_ENTRY(ce, "CryoReader", cryo_reader_methods);
|
|
cryo_ce_reader = zend_register_internal_class(&ce);
|
|
cryo_ce_reader->ce_flags |= ZEND_ACC_FINAL | ZEND_ACC_NO_DYNAMIC_PROPERTIES | ZEND_ACC_NOT_SERIALIZABLE;
|
|
cryo_ce_reader->create_object = reader_create;
|
|
|
|
memcpy(&cryo_reader_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
|
|
cryo_reader_handlers.offset = XtOffsetOf(cryo_reader_obj, std);
|
|
cryo_reader_handlers.free_obj = reader_free;
|
|
cryo_reader_handlers.clone_obj = NULL;
|
|
|
|
INIT_CLASS_ENTRY(ce, "CryoDelta", cryo_delta_methods);
|
|
cryo_ce_delta = zend_register_internal_class(&ce);
|
|
cryo_ce_delta->ce_flags |= ZEND_ACC_FINAL | ZEND_ACC_NO_DYNAMIC_PROPERTIES | ZEND_ACC_NOT_SERIALIZABLE;
|
|
cryo_ce_delta->create_object = delta_create;
|
|
zend_class_implements(cryo_ce_delta, 3, zend_ce_aggregate, zend_ce_countable, zend_ce_arrayaccess);
|
|
memcpy(&cryo_delta_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
|
|
cryo_delta_handlers.offset = XtOffsetOf(cryo_delta_obj, std);
|
|
cryo_delta_handlers.free_obj = delta_free;
|
|
cryo_delta_handlers.clone_obj = NULL;
|
|
}
|