Squashed commit of all initial work on the 'name path' mechanism.
Tim Bunce [Mon, 10 Sep 2012 18:59:39 +0000 (19:59 +0100)]
.gitignore [new file with mode: 0644]
CHANGES
Makefile.PL
Size.xs

diff --git a/.gitignore b/.gitignore
new file mode 100644 (file)
index 0000000..9453686
--- /dev/null
@@ -0,0 +1,9 @@
+MYMETA.json
+MYMETA.yml
+Makefile
+Makefile.old
+Size.bs
+Size.c
+Size.o
+blib/
+pm_to_blib
diff --git a/CHANGES b/CHANGES
index c99fc5a..a7debaa 100644 (file)
--- a/CHANGES
+++ b/CHANGES
@@ -1,6 +1,7 @@
 Revision history for Perl extension Devel::Size.
 
  * Spelling fix to docs from gregor herrmann [CPAN #78766]
+ * Fix measurement of struct svop [Tim]
 
 0.78 2011-07-26 nicholas
  [no changes]
index 73d3874..7067f9c 100644 (file)
@@ -9,6 +9,7 @@ use Config;
 my $ptr_bits = length $1;
 
 WriteMakefile(
+    OPTIMIZE => "-g",
              NAME => 'Devel::Size',
              VERSION_FROM => 'lib/Devel/Size.pm',
              DEFINE => "-DALIGN_BITS=$ptr_bits",
diff --git a/Size.xs b/Size.xs
index 252dab2..a8fb349 100644 (file)
--- a/Size.xs
+++ b/Size.xs
@@ -1,5 +1,8 @@
 /* -*- mode: C -*- */
 
+#undef NDEBUG /* XXX */
+#include <assert.h>
+
 #define PERL_NO_GET_CONTEXT
 
 #include "EXTERN.h"
 #define LEAF_BITS   (16 - BYTE_BITS)
 #define LEAF_MASK   0x1FFF
 
+typedef struct npath_node_st npath_node_t;
+struct npath_node_st {
+    npath_node_t *prev;
+    const void *id;
+    U8 type;
+    U8 flags;
+    UV seqn;
+    U16 depth;
+};
+
 struct state {
     UV total_size;
     bool regex_whine;
@@ -81,8 +94,185 @@ struct state {
        start with 0 bits, hence the start of this array will be hot, and the
        end unused. So put the flags next to the hot end.  */
     void *tracking[256];
+    /* callback hooks and data */
+    int (*add_attr_cb)(struct state *st, npath_node_t *npath_node, UV attr_type, const char *name, UV value);
+    void (*free_state_cb)(struct state *st);
+    UV seqn;
+    void *state_cb_data; /* free'd by free_state() after free_state_cb() call */
 };
 
+#define ADD_SIZE(st, leafname, bytes) (NPathAddSizeCb(st, leafname, bytes) (st)->total_size += (bytes))
+
+#define PATH_TRACKING
+#ifdef PATH_TRACKING
+
+#define NPathAddSizeCb(st, name, bytes) (st->add_attr_cb && st->add_attr_cb(st, NP-1, 0, (name), (bytes))),
+#define pPATH npath_node_t *NPathArg
+
+/* A subtle point here is that each dNPathSetNode leaves NP pointing to
+ * the next unused slot (though with prev already filled in)
+ * whereas NPathLink leaves NP unchanged, it just fills in the slot NP points
+ * to and passes that NP value to the function being called.
+ */
+#define dNPathNodes(nodes, prev_np) \
+            npath_node_t name_path_nodes[nodes+1]; /* +1 for NPathLink */ \
+            npath_node_t *NP = &name_path_nodes[0]; \
+            NP->seqn = 0; \
+            NP->type = 0; \
+            NP->id = "?0?"; /* DEBUG */ \
+            NP->prev = prev_np
+#define dNPathSetNode(nodeid, nodetype) \
+            NP->id = nodeid; \
+            NP->type = nodetype; \
+            if(0)fprintf(stderr,"dNPathSetNode (%p <-) %p <- [%d %s]\n", NP->prev, NP, nodetype,(char*)nodeid);\
+            NP++; \
+            NP->id="?+?"; /* DEBUG */ \
+            NP->seqn = 0; \
+            NP->prev = (NP-1)
+
+/* dNPathUseParent points NP directly the the parents' name_path_nodes array
+ * So the function can only safely call ADD_*() but not NPathLink, unless the
+ * caller has spare nodes in its name_path_nodes.
+ */
+#define dNPathUseParent(prev_np) npath_node_t *NP = (((prev_np+1)->prev = prev_np), prev_np+1)
+
+#define NPtype_NAME     0x01
+#define NPtype_LINK     0x02
+#define NPtype_SV       0x03
+#define NPtype_MAGIC    0x04
+#define NPtype_OP       0x05
+
+#define NPathLink(nodeid, nodetype)   ((NP->id = nodeid), (NP->type = nodetype), (NP->seqn = 0), NP)
+#define NPathOpLink  (NPathArg)
+#define ADD_ATTR(st, attr_type, attr_name, attr_value) (st->add_attr_cb && st->add_attr_cb(st, NP-1, attr_type, attr_name, attr_value))
+
+#else
+
+#define NPathAddSizeCb(st, name, bytes)
+#define pPATH void *npath_dummy /* XXX ideally remove */
+#define dNPathNodes(nodes, prev_np)  dNOOP
+#define NPathLink(nodeid, nodetype)  NULL
+#define NPathOpLink NULL
+#define ADD_ATTR(st, attr_type, attr_name, attr_value) NOOP
+
+#endif /* PATH_TRACKING */
+
+
+
+
+#ifdef PATH_TRACKING
+
+static const char *svtypenames[SVt_LAST] = {
+#if PERL_VERSION < 9
+  "NULL", "IV", "NV", "RV", "PV", "PVIV", "PVNV", "PVMG", "PVBM", "PVLV", "PVAV", "PVHV", "PVCV", "PVGV", "PVFM", "PVIO",
+#elif PERL_VERSION == 10 && PERL_SUBVERSION == 0
+  "NULL", "BIND", "IV", "NV", "RV", "PV", "PVIV", "PVNV", "PVMG", "PVGV", "PVLV", "PVAV", "PVHV", "PVCV", "PVFM", "PVIO",
+#elif PERL_VERSION == 10 && PERL_SUBVERSION == 1
+  "NULL", "BIND", "IV", "NV", "RV", "PV", "PVIV", "PVNV", "PVMG", "PVGV", "PVLV", "PVAV", "PVHV", "PVCV", "PVFM", "PVIO",
+#elif PERL_VERSION < 13
+  "NULL", "BIND", "IV", "NV", "PV", "PVIV", "PVNV", "PVMG", "REGEXP", "PVGV", "PVLV", "PVAV", "PVHV", "PVCV", "PVFM", "PVIO",
+#else
+  "NULL", "BIND", "IV", "NV", "PV", "PVIV", "PVNV", "PVMG", "REGEXP", "PVGV", "PVLV", "PVAV", "PVHV", "PVCV", "PVFM", "PVIO",
+#endif
+};
+
+int
+print_node_name(npath_node_t *npath_node)
+{
+    char buf[1024]; /* XXX */
+
+    switch (npath_node->type) {
+    case NPtype_SV: { /* id is pointer to the SV sv_size was called on */
+        const SV *sv = (SV*)npath_node->id;
+        int type = SvTYPE(sv);
+        char *typename = (type == SVt_IV && SvROK(sv)) ? "RV" : svtypenames[type];
+        fprintf(stderr, "SV(%s)", typename);
+        switch(type) {  /* add some useful details */
+        case SVt_PVAV: fprintf(stderr, " fill=%d/%ld", av_len((AV*)sv), AvMAX((AV*)sv)); break;
+        case SVt_PVHV: fprintf(stderr, " fill=%ld/%ld", HvFILL((HV*)sv), HvMAX((HV*)sv)); break;
+        }
+        break;
+    }
+    case NPtype_OP: { /* id is pointer to the OP op_size was called on */
+        const OP *op = (OP*)npath_node->id;
+        fprintf(stderr, "OP(%s)", OP_NAME(op));
+        break;
+    }
+    case NPtype_MAGIC: { /* id is pointer to the MAGIC struct */
+        MAGIC *magic_pointer = (MAGIC*)npath_node->id;
+        /* XXX it would be nice if we could reuse mg_names.c [sigh] */
+        fprintf(stderr, "MAGIC(%c)", magic_pointer->mg_type ? magic_pointer->mg_type : '0');
+        break;
+    }
+    case NPtype_LINK:
+        fprintf(stderr, "%s->", npath_node->id);
+        break;
+    case NPtype_NAME:
+        fprintf(stderr, "%s", npath_node->id);
+        break;
+    default:    /* assume id is a string pointer */
+        fprintf(stderr, "UNKNOWN(%d,%p)", npath_node->type, npath_node->id);
+        break;
+    }
+    return 0;
+}
+
+void
+print_indent(int depth) {
+    while (depth-- > 0)
+        fprintf(stderr, ":   ");
+}
+
+int
+print_formatted_node(struct state *st, npath_node_t *npath_node) {
+    print_indent(npath_node->depth);
+    print_node_name(npath_node);
+    fprintf(stderr, "\t\t[#%ld @%u] ", npath_node->seqn, npath_node->depth);
+    fprintf(stderr, "\n");
+    return 0;
+}
+
+void
+walk_new_nodes(struct state *st, npath_node_t *npath_node, int (*cb)(struct state *st, npath_node_t *npath_node))
+{
+    if (npath_node->seqn) /* node already output */
+        return;
+
+    if (npath_node->prev) {
+        walk_new_nodes(st, npath_node->prev, cb); /* recurse */
+        npath_node->depth = npath_node->prev->depth + 1;
+    }
+    else npath_node->depth = 0;
+    npath_node->seqn = ++st->seqn;
+
+    if (cb)
+        cb(st, npath_node);
+
+    return;
+}
+
+int
+dump_path(struct state *st, npath_node_t *npath_node, UV attr_type, const char *attr_name, UV attr_value)
+{
+    if (!attr_type && !attr_value)
+        return 0;
+    walk_new_nodes(st, npath_node, print_formatted_node);
+    print_indent(npath_node->depth+1);
+    if (attr_type) {
+        fprintf(stderr, "~NAMED('%s') %lu", attr_name, attr_value);
+    }
+    else {
+        fprintf(stderr, "+%ld ", attr_value);
+        fprintf(stderr, "%s ", attr_name);
+        fprintf(stderr, "=%ld ", attr_value+st->total_size);
+    }
+    fprintf(stderr, "\n");
+    return 0;
+}
+
+#endif /* PATH_TRACKING */
+
+
 /* 
     Checks to see if thing is in the bitstring. 
     Returns true or false, and
@@ -176,6 +366,10 @@ static void
 free_state(struct state *st)
 {
     const int top_level = (sizeof(void *) * 8 - LEAF_BITS - BYTE_BITS) / 8;
+    if (st->free_state_cb)
+        st->free_state_cb(st);
+    if (st->state_cb_data)
+        Safefree(st->state_cb_data);
     free_tracking_at((void **)st->tracking, top_level);
     Safefree(st);
 }
@@ -194,7 +388,7 @@ free_state(struct state *st)
 #define SOME_RECURSION 1
 #define TOTAL_SIZE_RECURSION 2
 
-static void sv_size(pTHX_ struct state *, const SV *const, const int recurse);
+static void sv_size(pTHX_ struct state *, pPATH, const SV *const, const int recurse);
 
 typedef enum {
     OPc_NULL,   /* 0 */
@@ -348,32 +542,36 @@ cc_opclass(const OP * const o)
 /* Figure out how much magic is attached to the SV and return the
    size */
 static void
-magic_size(pTHX_ const SV * const thing, struct state *st) {
+magic_size(pTHX_ const SV * const thing, struct state *st, pPATH) {
+  dNPathNodes(1, NPathArg);
   MAGIC *magic_pointer = SvMAGIC(thing);
 
   /* Have we seen the magic pointer?  (NULL has always been seen before)  */
   while (check_new(st, magic_pointer)) {
-    st->total_size += sizeof(MAGIC);
+
+    dNPathSetNode(magic_pointer, NPtype_MAGIC);
+
+    ADD_SIZE(st, "mg", sizeof(MAGIC));
     /* magic vtables aren't freed when magic is freed, so don't count them.
        (They are static structures. Anything that assumes otherwise is buggy.)
     */
 
 
     TRY_TO_CATCH_SEGV {
-       sv_size(aTHX_ st, magic_pointer->mg_obj, TOTAL_SIZE_RECURSION);
+       sv_size(aTHX_ st, NPathLink("mg_obj", NPtype_LINK), magic_pointer->mg_obj, TOTAL_SIZE_RECURSION);
        if (magic_pointer->mg_len == HEf_SVKEY) {
-           sv_size(aTHX_ st, (SV *)magic_pointer->mg_ptr, TOTAL_SIZE_RECURSION);
+           sv_size(aTHX_ st, NPathLink("mg_ptr", NPtype_LINK), (SV *)magic_pointer->mg_ptr, TOTAL_SIZE_RECURSION);
        }
 #if defined(PERL_MAGIC_utf8) && defined (PERL_MAGIC_UTF8_CACHESIZE)
        else if (magic_pointer->mg_type == PERL_MAGIC_utf8) {
            if (check_new(st, magic_pointer->mg_ptr)) {
-               st->total_size += PERL_MAGIC_UTF8_CACHESIZE * 2 * sizeof(STRLEN);
+               ADD_SIZE(st, "PERL_MAGIC_utf8", PERL_MAGIC_UTF8_CACHESIZE * 2 * sizeof(STRLEN));
            }
        }
 #endif
        else if (magic_pointer->mg_len > 0) {
            if (check_new(st, magic_pointer->mg_ptr)) {
-               st->total_size += magic_pointer->mg_len;
+               ADD_SIZE(st, "mg_len", magic_pointer->mg_len);
            }
        }
 
@@ -388,24 +586,29 @@ magic_size(pTHX_ const SV * const thing, struct state *st) {
 }
 
 static void
-check_new_and_strlen(struct state *st, const char *const p) {
-    if(check_new(st, p))
-       st->total_size += 1 + strlen(p);
+check_new_and_strlen(struct state *st, const char *const p, pPATH) {
+    dNPathNodes(1, NPathArg->prev);
+    if(check_new(st, p)) {
+        dNPathSetNode(NPathArg->id, NPtype_NAME);
+       ADD_SIZE(st, NPathArg->id, 1 + strlen(p));
+    }
 }
 
 static void
-regex_size(const REGEXP * const baseregex, struct state *st) {
+regex_size(const REGEXP * const baseregex, struct state *st, pPATH) {
+    dNPathNodes(1, NPathArg);
     if(!check_new(st, baseregex))
        return;
-  st->total_size += sizeof(REGEXP);
+  dNPathSetNode("regex_size", NPtype_NAME);
+  ADD_SIZE(st, "REGEXP", sizeof(REGEXP));
 #if (PERL_VERSION < 11)     
   /* Note the size of the paren offset thing */
-  st->total_size += sizeof(I32) * baseregex->nparens * 2;
-  st->total_size += strlen(baseregex->precomp);
+  ADD_SIZE(st, "nparens", sizeof(I32) * baseregex->nparens * 2);
+  ADD_SIZE(st, "precomp", strlen(baseregex->precomp));
 #else
-  st->total_size += sizeof(struct regexp);
-  st->total_size += sizeof(I32) * SvANY(baseregex)->nparens * 2;
-  /*st->total_size += strlen(SvANY(baseregex)->subbeg);*/
+  ADD_SIZE(st, "regexp", sizeof(struct regexp));
+  ADD_SIZE(st, "nparens", sizeof(I32) * SvANY(baseregex)->nparens * 2);
+  /*ADD_SIZE(st, strlen(SvANY(baseregex)->subbeg));*/
 #endif
   if (st->go_yell && !st->regex_whine) {
     carp("Devel::Size: Calculated sizes for compiled regexes are incompatible, and probably always will be");
@@ -414,97 +617,103 @@ regex_size(const REGEXP * const baseregex, struct state *st) {
 }
 
 static void
-op_size(pTHX_ const OP * const baseop, struct state *st)
+op_size(pTHX_ const OP * const baseop, struct state *st, pPATH)
 {
+    /* op_size recurses to follow the chain of opcodes.
+     * For the 'path' we don't want the chain to be 'nested' in the path so we
+     * use ->prev in dNPathNodes.
+     */
+    dNPathUseParent(NPathArg);
+
     TRY_TO_CATCH_SEGV {
        TAG;
        if(!check_new(st, baseop))
            return;
        TAG;
-       op_size(aTHX_ baseop->op_next, st);
+       op_size(aTHX_ baseop->op_next, st, NPathOpLink);
        TAG;
        switch (cc_opclass(baseop)) {
        case OPc_BASEOP: TAG;
-           st->total_size += sizeof(struct op);
+           ADD_SIZE(st, "op", sizeof(struct op));
            TAG;break;
        case OPc_UNOP: TAG;
-           st->total_size += sizeof(struct unop);
-           op_size(aTHX_ ((UNOP *)baseop)->op_first, st);
+           ADD_SIZE(st, "unop", sizeof(struct unop));
+           op_size(aTHX_ ((UNOP *)baseop)->op_first, st, NPathOpLink);
            TAG;break;
        case OPc_BINOP: TAG;
-           st->total_size += sizeof(struct binop);
-           op_size(aTHX_ ((BINOP *)baseop)->op_first, st);
-           op_size(aTHX_ ((BINOP *)baseop)->op_last, st);
+           ADD_SIZE(st, "binop", sizeof(struct binop));
+           op_size(aTHX_ ((BINOP *)baseop)->op_first, st, NPathOpLink);
+           op_size(aTHX_ ((BINOP *)baseop)->op_last, st, NPathOpLink);
            TAG;break;
        case OPc_LOGOP: TAG;
-           st->total_size += sizeof(struct logop);
-           op_size(aTHX_ ((BINOP *)baseop)->op_first, st);
-           op_size(aTHX_ ((LOGOP *)baseop)->op_other, st);
+           ADD_SIZE(st, "logop", sizeof(struct logop));
+           op_size(aTHX_ ((BINOP *)baseop)->op_first, st, NPathOpLink);
+           op_size(aTHX_ ((LOGOP *)baseop)->op_other, st, NPathOpLink);
            TAG;break;
 #ifdef OA_CONDOP
        case OPc_CONDOP: TAG;
-           st->total_size += sizeof(struct condop);
-           op_size(aTHX_ ((BINOP *)baseop)->op_first, st);
-           op_size(aTHX_ ((CONDOP *)baseop)->op_true, st);
-           op_size(aTHX_ ((CONDOP *)baseop)->op_false, st);
+           ADD_SIZE(st, "condop", sizeof(struct condop));
+           op_size(aTHX_ ((BINOP *)baseop)->op_first, st, NPathOpLink);
+           op_size(aTHX_ ((CONDOP *)baseop)->op_true, st, NPathOpLink);
+           op_size(aTHX_ ((CONDOP *)baseop)->op_false, st, NPathOpLink);
            TAG;break;
 #endif
        case OPc_LISTOP: TAG;
-           st->total_size += sizeof(struct listop);
-           op_size(aTHX_ ((LISTOP *)baseop)->op_first, st);
-           op_size(aTHX_ ((LISTOP *)baseop)->op_last, st);
+           ADD_SIZE(st, "listop", sizeof(struct listop));
+           op_size(aTHX_ ((LISTOP *)baseop)->op_first, st, NPathOpLink);
+           op_size(aTHX_ ((LISTOP *)baseop)->op_last, st, NPathOpLink);
            TAG;break;
        case OPc_PMOP: TAG;
-           st->total_size += sizeof(struct pmop);
-           op_size(aTHX_ ((PMOP *)baseop)->op_first, st);
-           op_size(aTHX_ ((PMOP *)baseop)->op_last, st);
+           ADD_SIZE(st, "pmop", sizeof(struct pmop));
+           op_size(aTHX_ ((PMOP *)baseop)->op_first, st, NPathOpLink);
+           op_size(aTHX_ ((PMOP *)baseop)->op_last, st, NPathOpLink);
 #if PERL_VERSION < 9 || (PERL_VERSION == 9 && PERL_SUBVERSION < 5)
-           op_size(aTHX_ ((PMOP *)baseop)->op_pmreplroot, st);
-           op_size(aTHX_ ((PMOP *)baseop)->op_pmreplstart, st);
+           op_size(aTHX_ ((PMOP *)baseop)->op_pmreplroot, st, NPathOpLink);
+           op_size(aTHX_ ((PMOP *)baseop)->op_pmreplstart, st, NPathOpLink);
 #endif
            /* This is defined away in perl 5.8.x, but it is in there for
               5.6.x */
 #ifdef PM_GETRE
-           regex_size(PM_GETRE((PMOP *)baseop), st);
+           regex_size(PM_GETRE((PMOP *)baseop), st, NPathLink("PM_GETRE", NPtype_LINK));
 #else
-           regex_size(((PMOP *)baseop)->op_pmregexp, st);
+           regex_size(((PMOP *)baseop)->op_pmregexp, st, NPathLink("op_pmregexp", NPtype_LINK));
 #endif
            TAG;break;
        case OPc_SVOP: TAG;
-           st->total_size += sizeof(struct pmop);
+           ADD_SIZE(st, "svop", sizeof(struct svop));
            if (!(baseop->op_type == OP_AELEMFAST
                  && baseop->op_flags & OPf_SPECIAL)) {
                /* not an OP_PADAV replacement */
-               sv_size(aTHX_ st, ((SVOP *)baseop)->op_sv, SOME_RECURSION);
+               sv_size(aTHX_ st, NPathLink("SVOP", NPtype_LINK), ((SVOP *)baseop)->op_sv, SOME_RECURSION);
            }
            TAG;break;
 #ifdef OA_PADOP
       case OPc_PADOP: TAG;
-         st->total_size += sizeof(struct padop);
+         ADD_SIZE(st, "padop", sizeof(struct padop));
          TAG;break;
 #endif
 #ifdef OA_GVOP
       case OPc_GVOP: TAG;
-         st->total_size += sizeof(struct gvop);
-         sv_size(aTHX_ st, ((GVOP *)baseop)->op_gv, SOME_RECURSION);
+         ADD_SIZE(st, "gvop", sizeof(struct gvop));
+         sv_size(aTHX_ st, NPathLink("GVOP", NPtype_LINK), ((GVOP *)baseop)->op_gv, SOME_RECURSION);
          TAG;break;
 #endif
        case OPc_PVOP: TAG;
-           check_new_and_strlen(st, ((PVOP *)baseop)->op_pv);
+           check_new_and_strlen(st, ((PVOP *)baseop)->op_pv, NPathLink("op_pv", NPtype_LINK));
            TAG;break;
        case OPc_LOOP: TAG;
-           st->total_size += sizeof(struct loop);
-           op_size(aTHX_ ((LOOP *)baseop)->op_first, st);
-           op_size(aTHX_ ((LOOP *)baseop)->op_last, st);
-           op_size(aTHX_ ((LOOP *)baseop)->op_redoop, st);
-           op_size(aTHX_ ((LOOP *)baseop)->op_nextop, st);
-           op_size(aTHX_ ((LOOP *)baseop)->op_lastop, st);
+           ADD_SIZE(st, "loop", sizeof(struct loop));
+           op_size(aTHX_ ((LOOP *)baseop)->op_first, st, NPathOpLink);
+           op_size(aTHX_ ((LOOP *)baseop)->op_last, st, NPathOpLink);
+           op_size(aTHX_ ((LOOP *)baseop)->op_redoop, st, NPathOpLink);
+           op_size(aTHX_ ((LOOP *)baseop)->op_nextop, st, NPathOpLink);
+           op_size(aTHX_ ((LOOP *)baseop)->op_lastop, st, NPathOpLink);
            TAG;break;
        case OPc_COP: TAG;
         {
           COP *basecop;
           basecop = (COP *)baseop;
-          st->total_size += sizeof(struct cop);
+          ADD_SIZE(st, "cop", sizeof(struct cop));
 
           /* Change 33656 by nicholas@mouse-mill on 2008/04/07 11:29:51
           Eliminate cop_label from struct cop by storing a label as the first
@@ -514,14 +723,14 @@ op_size(pTHX_ const OP * const baseop, struct state *st)
           before 5.11 @33656, but later than 5.10, producing slightly too
           small memory sizes on these Perls. */
 #if (PERL_VERSION < 11)
-          check_new_and_strlen(st, basecop->cop_label);
+          check_new_and_strlen(st, basecop->cop_label, NPathLink("cop_label", NPtype_LINK));
 #endif
 #ifdef USE_ITHREADS
-          check_new_and_strlen(st, basecop->cop_file);
-          check_new_and_strlen(st, basecop->cop_stashpv);
+          check_new_and_strlen(st, basecop->cop_file, NPathLink("cop_file", NPtype_LINK));
+          check_new_and_strlen(st, basecop->cop_stashpv, NPathLink("cop_stashpv", NPtype_LINK));
 #else
-         sv_size(aTHX_ st, (SV *)basecop->cop_stash, SOME_RECURSION);
-         sv_size(aTHX_ st, (SV *)basecop->cop_filegv, SOME_RECURSION);
+         sv_size(aTHX_ st, NPathLink("cop_stash", NPtype_LINK), (SV *)basecop->cop_stash, SOME_RECURSION);
+         sv_size(aTHX_ st, NPathLink("cop_filegv", NPtype_LINK), (SV *)basecop->cop_filegv, SOME_RECURSION);
 #endif
 
         }
@@ -537,23 +746,24 @@ op_size(pTHX_ const OP * const baseop, struct state *st)
 }
 
 static void
-hek_size(pTHX_ struct state *st, HEK *hek, U32 shared)
+hek_size(pTHX_ struct state *st, HEK *hek, U32 shared, pPATH)
 {
+    dNPathUseParent(NPathArg);
     /* Hash keys can be shared. Have we seen this before? */
     if (!check_new(st, hek))
        return;
-    st->total_size += HEK_BASESIZE + hek->hek_len
+    ADD_SIZE(st, "hek_len", HEK_BASESIZE + hek->hek_len
 #if PERL_VERSION < 8
        + 1 /* No hash key flags prior to 5.8.0  */
 #else
        + 2
 #endif
-       ;
+       );
     if (shared) {
 #if PERL_VERSION < 10
-       st->total_size += sizeof(struct he);
+       ADD_SIZE(st, "he", sizeof(struct he));
 #else
-       st->total_size += STRUCT_OFFSET(struct shared_he, shared_he_hek);
+       ADD_SIZE(st, "shared_he", STRUCT_OFFSET(struct shared_he, shared_he_hek));
 #endif
     }
 }
@@ -660,13 +870,51 @@ const U8 body_sizes[SVt_LAST] = {
 #endif
 };
 
+
+static void
+padlist_size(pTHX_ struct state *const st, pPATH, PADLIST *padlist,
+       const int recurse)
+{
+    dNPathUseParent(NPathArg);
+    /* based on Perl_do_dump_pad() */
+    const AV *pad_name;
+    SV **pname;
+    I32 ix;              
+
+    if (!padlist) {
+        return;
+    }
+    pad_name = MUTABLE_AV(*av_fetch(MUTABLE_AV(padlist), 0, FALSE));
+    pname = AvARRAY(pad_name);
+
+    for (ix = 1; ix <= AvFILLp(pad_name); ix++) {
+        const SV *namesv = pname[ix];
+        if (namesv && namesv == &PL_sv_undef) {
+            namesv = NULL;
+        }
+        if (namesv) {
+            if (SvFAKE(namesv))
+                ADD_ATTR(st, 1, SvPVX_const(namesv), ix);
+            else
+                ADD_ATTR(st, 1, SvPVX_const(namesv), ix);
+        }
+        else {
+            ADD_ATTR(st, 1, "SVs_PADTMP", ix);
+        }
+
+    }
+    sv_size(aTHX_ st, NPathArg, (SV*)padlist, recurse);
+}
+
+
 static void
-sv_size(pTHX_ struct state *const st, const SV * const orig_thing,
+sv_size(pTHX_ struct state *const st, pPATH, const SV * const orig_thing,
        const int recurse) {
   const SV *thing = orig_thing;
+  dNPathNodes(3, NPathArg);
   U32 type;
 
-  if(!check_new(st, thing))
+  if(!check_new(st, orig_thing))
       return;
 
   type = SvTYPE(thing);
@@ -674,10 +922,11 @@ sv_size(pTHX_ struct state *const st, const SV * const orig_thing,
       warn("Devel::Size: Unknown variable type: %d encountered\n", type);
       return;
   }
-  st->total_size += sizeof(SV) + body_sizes[type];
+  dNPathSetNode(thing, NPtype_SV);
+  ADD_SIZE(st, "sv", sizeof(SV) + body_sizes[type]);
 
   if (type >= SVt_PVMG) {
-      magic_size(aTHX_ thing, st);
+      magic_size(aTHX_ thing, st, NPathLink(NULL, 0));
   }
 
   switch (type) {
@@ -688,55 +937,59 @@ sv_size(pTHX_ struct state *const st, const SV * const orig_thing,
   case SVt_IV: TAG;
 #endif
     if(recurse && SvROK(thing))
-       sv_size(aTHX_ st, SvRV_const(thing), recurse);
+       sv_size(aTHX_ st, NPathLink("RV", NPtype_LINK), SvRV_const(thing), recurse);
     TAG;break;
 
   case SVt_PVAV: TAG;
     /* Is there anything in the array? */
     if (AvMAX(thing) != -1) {
       /* an array with 10 slots has AvMax() set to 9 - te 2007-04-22 */
-      st->total_size += sizeof(SV *) * (AvMAX(thing) + 1);
+      ADD_SIZE(st, "av_max", sizeof(SV *) * (AvMAX(thing) + 1));
       dbg_printf(("total_size: %li AvMAX: %li av_len: $i\n", st->total_size, AvMAX(thing), av_len((AV*)thing)));
 
       if (recurse >= TOTAL_SIZE_RECURSION) {
          SSize_t i = AvFILLp(thing) + 1;
 
          while (i--)
-             sv_size(aTHX_ st, AvARRAY(thing)[i], recurse);
+             sv_size(aTHX_ st, NPathLink("AVelem", NPtype_LINK), AvARRAY(thing)[i], recurse);
       }
     }
     /* Add in the bits on the other side of the beginning */
 
     dbg_printf(("total_size %li, sizeof(SV *) %li, AvARRAY(thing) %li, AvALLOC(thing)%li , sizeof(ptr) %li \n", 
-    st->total_size, sizeof(SV*), AvARRAY(thing), AvALLOC(thing), sizeof( thing )));
+        st->total_size, sizeof(SV*), AvARRAY(thing), AvALLOC(thing), sizeof( thing )));
 
     /* under Perl 5.8.8 64bit threading, AvARRAY(thing) was a pointer while AvALLOC was 0,
        resulting in grossly overstated sized for arrays. Technically, this shouldn't happen... */
     if (AvALLOC(thing) != 0) {
-      st->total_size += (sizeof(SV *) * (AvARRAY(thing) - AvALLOC(thing)));
+      ADD_SIZE(st, "AvALLOC", (sizeof(SV *) * (AvARRAY(thing) - AvALLOC(thing))));
       }
 #if (PERL_VERSION < 9)
     /* Is there something hanging off the arylen element?
        Post 5.9.something this is stored in magic, so will be found there,
        and Perl_av_arylen_p() takes a non-const AV*, hence compilers rightly
        complain about AvARYLEN() passing thing to it.  */
-    sv_size(aTHX_ st, AvARYLEN(thing), recurse);
+    sv_size(aTHX_ st, NPathLink("ARYLEN", NPtype_LINK), AvARYLEN(thing), recurse);
 #endif
     TAG;break;
   case SVt_PVHV: TAG;
     /* Now the array of buckets */
-    st->total_size += (sizeof(HE *) * (HvMAX(thing) + 1));
+    ADD_SIZE(st, "hv_max", (sizeof(HE *) * (HvMAX(thing) + 1)));
+    if (HvENAME(thing)) {
+        ADD_ATTR(st, 1, HvENAME(thing), 0);
+    }
     /* Now walk the bucket chain */
     if (HvARRAY(thing)) {
       HE *cur_entry;
       UV cur_bucket = 0;
+      dNPathSetNode("HvARRAY", NPtype_LINK);
       for (cur_bucket = 0; cur_bucket <= HvMAX(thing); cur_bucket++) {
         cur_entry = *(HvARRAY(thing) + cur_bucket);
         while (cur_entry) {
-          st->total_size += sizeof(HE);
-         hek_size(aTHX_ st, cur_entry->hent_hek, HvSHAREKEYS(thing));
+          ADD_SIZE(st, "he", sizeof(HE));
+         hek_size(aTHX_ st, cur_entry->hent_hek, HvSHAREKEYS(thing), NPathLink("hent_hek", NPtype_LINK));
          if (recurse >= TOTAL_SIZE_RECURSION)
-             sv_size(aTHX_ st, HeVAL(cur_entry), recurse);
+             sv_size(aTHX_ st, NPathLink("HeVAL", NPtype_LINK), HeVAL(cur_entry), recurse);
           cur_entry = cur_entry->hent_next;
         }
       }
@@ -754,39 +1007,39 @@ sv_size(pTHX_ struct state *const st, const SV * const orig_thing,
            if (count < 0)
                count = -count;
            while (--count)
-               hek_size(aTHX_ st, names[count], 1);
+               hek_size(aTHX_ st, names[count], 1, NPathLink("HvAUXelem", NPtype_LINK));
        }
        else
 #endif
        {
-           hek_size(aTHX_ st, HvNAME_HEK(thing), 1);
+           hek_size(aTHX_ st, HvNAME_HEK(thing), 1, NPathLink("HvNAME_HEK", NPtype_LINK));
        }
 
-       st->total_size += sizeof(struct xpvhv_aux);
+       ADD_SIZE(st, "xpvhv_aux", sizeof(struct xpvhv_aux));
        if (meta) {
-           st->total_size += sizeof(struct mro_meta);
-           sv_size(aTHX_ st, (SV *)meta->mro_nextmethod, TOTAL_SIZE_RECURSION);
+           ADD_SIZE(st, "mro_meta", sizeof(struct mro_meta));
+           sv_size(aTHX_ st, NPathLink("mro_nextmethod", NPtype_LINK), (SV *)meta->mro_nextmethod, TOTAL_SIZE_RECURSION);
 #if PERL_VERSION > 10 || (PERL_VERSION == 10 && PERL_SUBVERSION > 0)
-           sv_size(aTHX_ st, (SV *)meta->isa, TOTAL_SIZE_RECURSION);
+           sv_size(aTHX_ st, NPathLink("isa", NPtype_LINK), (SV *)meta->isa, TOTAL_SIZE_RECURSION);
 #endif
 #if PERL_VERSION > 10
-           sv_size(aTHX_ st, (SV *)meta->mro_linear_all, TOTAL_SIZE_RECURSION);
-           sv_size(aTHX_ st, meta->mro_linear_current, TOTAL_SIZE_RECURSION);
+           sv_size(aTHX_ st, NPathLink("mro_linear_all", NPtype_LINK), (SV *)meta->mro_linear_all, TOTAL_SIZE_RECURSION);
+           sv_size(aTHX_ st, NPathLink("mro_linear_current", NPtype_LINK), meta->mro_linear_current, TOTAL_SIZE_RECURSION);
 #else
-           sv_size(aTHX_ st, (SV *)meta->mro_linear_dfs, TOTAL_SIZE_RECURSION);
-           sv_size(aTHX_ st, (SV *)meta->mro_linear_c3, TOTAL_SIZE_RECURSION);
+           sv_size(aTHX_ st, NPathLink("mro_linear_dfs", NPtype_LINK), (SV *)meta->mro_linear_dfs, TOTAL_SIZE_RECURSION);
+           sv_size(aTHX_ st, NPathLink("mro_linear_c3", NPtype_LINK), (SV *)meta->mro_linear_c3, TOTAL_SIZE_RECURSION);
 #endif
        }
     }
 #else
-    check_new_and_strlen(st, HvNAME_get(thing));
+    check_new_and_strlen(st, HvNAME_get(thing), NPathLink("HvNAME", NPtype_LINK));
 #endif
     TAG;break;
 
 
   case SVt_PVFM: TAG;
-    sv_size(aTHX_ st, (SV *)CvPADLIST(thing), SOME_RECURSION);
-    sv_size(aTHX_ st, (SV *)CvOUTSIDE(thing), recurse);
+    padlist_size(aTHX_ st, NPathLink("CvPADLIST", NPtype_LINK), CvPADLIST(thing), SOME_RECURSION);
+    sv_size(aTHX_ st, NPathLink("CvOUTSIDE", NPtype_LINK), (SV *)CvOUTSIDE(thing), recurse);
 
     if (st->go_yell && !st->fm_whine) {
       carp("Devel::Size: Calculated sizes for FMs are incomplete");
@@ -795,28 +1048,28 @@ sv_size(pTHX_ struct state *const st, const SV * const orig_thing,
     goto freescalar;
 
   case SVt_PVCV: TAG;
-    sv_size(aTHX_ st, (SV *)CvSTASH(thing), SOME_RECURSION);
-    sv_size(aTHX_ st, (SV *)SvSTASH(thing), SOME_RECURSION);
-    sv_size(aTHX_ st, (SV *)CvGV(thing), SOME_RECURSION);
-    sv_size(aTHX_ st, (SV *)CvPADLIST(thing), SOME_RECURSION);
-    sv_size(aTHX_ st, (SV *)CvOUTSIDE(thing), recurse);
+    sv_size(aTHX_ st, NPathLink("CvSTASH", NPtype_LINK), (SV *)CvSTASH(thing), SOME_RECURSION);
+    sv_size(aTHX_ st, NPathLink("SvSTASH", NPtype_LINK), (SV *)SvSTASH(thing), SOME_RECURSION);
+    sv_size(aTHX_ st, NPathLink("CvGV", NPtype_LINK), (SV *)CvGV(thing), SOME_RECURSION);
+    padlist_size(aTHX_ st, NPathLink("CvPADLIST", NPtype_LINK), CvPADLIST(thing), SOME_RECURSION);
+    sv_size(aTHX_ st, NPathLink("CvOUTSIDE", NPtype_LINK), (SV *)CvOUTSIDE(thing), recurse);
     if (CvISXSUB(thing)) {
-       sv_size(aTHX_ st, cv_const_sv((CV *)thing), recurse);
+       sv_size(aTHX_ st, NPathLink("cv_const_sv", NPtype_LINK), cv_const_sv((CV *)thing), recurse);
     } else {
-       op_size(aTHX_ CvSTART(thing), st);
-       op_size(aTHX_ CvROOT(thing), st);
+       op_size(aTHX_ CvSTART(thing), st, NPathLink("CvSTART", NPtype_LINK));
+       op_size(aTHX_ CvROOT(thing), st, NPathLink("CvROOT", NPtype_LINK));
     }
     goto freescalar;
 
   case SVt_PVIO: TAG;
     /* Some embedded char pointers */
-    check_new_and_strlen(st, ((XPVIO *) SvANY(thing))->xio_top_name);
-    check_new_and_strlen(st, ((XPVIO *) SvANY(thing))->xio_fmt_name);
-    check_new_and_strlen(st, ((XPVIO *) SvANY(thing))->xio_bottom_name);
+    check_new_and_strlen(st, ((XPVIO *) SvANY(thing))->xio_top_name, NPathLink("xio_top_name", NPtype_LINK));
+    check_new_and_strlen(st, ((XPVIO *) SvANY(thing))->xio_fmt_name, NPathLink("xio_fmt_name", NPtype_LINK));
+    check_new_and_strlen(st, ((XPVIO *) SvANY(thing))->xio_bottom_name, NPathLink("xio_bottom_name", NPtype_LINK));
     /* Throw the GVs on the list to be walked if they're not-null */
-    sv_size(aTHX_ st, (SV *)((XPVIO *) SvANY(thing))->xio_top_gv, recurse);
-    sv_size(aTHX_ st, (SV *)((XPVIO *) SvANY(thing))->xio_bottom_gv, recurse);
-    sv_size(aTHX_ st, (SV *)((XPVIO *) SvANY(thing))->xio_fmt_gv, recurse);
+    sv_size(aTHX_ st, NPathLink("xio_top_gv", NPtype_LINK), (SV *)((XPVIO *) SvANY(thing))->xio_top_gv, recurse);
+    sv_size(aTHX_ st, NPathLink("xio_bottom_gv", NPtype_LINK), (SV *)((XPVIO *) SvANY(thing))->xio_bottom_gv, recurse);
+    sv_size(aTHX_ st, NPathLink("xio_fmt_gv", NPtype_LINK), (SV *)((XPVIO *) SvANY(thing))->xio_fmt_gv, recurse);
 
     /* Only go trotting through the IO structures if they're really
        trottable. If USE_PERLIO is defined we can do this. If
@@ -835,12 +1088,13 @@ sv_size(pTHX_ struct state *const st, const SV * const orig_thing,
   case SVt_PVGV: TAG;
     if(isGV_with_GP(thing)) {
 #ifdef GvNAME_HEK
-       hek_size(aTHX_ st, GvNAME_HEK(thing), 1);
+       hek_size(aTHX_ st, GvNAME_HEK(thing), 1, NPathLink("GvNAME_HEK", NPtype_LINK));
 #else  
-       st->total_size += GvNAMELEN(thing);
+       ADD_SIZE(st, "GvNAMELEN", GvNAMELEN(thing));
 #endif
+        ADD_ATTR(st, 1, GvNAME_get(thing), 0);
 #ifdef GvFILE_HEK
-       hek_size(aTHX_ st, GvFILE_HEK(thing), 1);
+       hek_size(aTHX_ st, GvFILE_HEK(thing), 1, NPathLink("GvFILE_HEK", NPtype_LINK));
 #elif defined(GvFILE)
 #  if !defined(USE_ITHREADS) || (PERL_VERSION > 8 || (PERL_VERSION == 8 && PERL_SUBVERSION > 8))
        /* With itreads, before 5.8.9, this can end up pointing to freed memory
@@ -850,18 +1104,18 @@ sv_size(pTHX_ struct state *const st, const SV * const orig_thing,
           of cases. 5.9.something added a proper fix, by converting the GP to
           use a shared hash key (porperly reference counted), instead of a
           char * (owned by who knows? possibly no-one now) */
-       check_new_and_strlen(st, GvFILE(thing));
+       check_new_and_strlen(st, GvFILE(thing), NPathLink("GvFILE", NPtype_LINK));
 #  endif
 #endif
        /* Is there something hanging off the glob? */
        if (check_new(st, GvGP(thing))) {
-           st->total_size += sizeof(GP);
-           sv_size(aTHX_ st, (SV *)(GvGP(thing)->gp_sv), recurse);
-           sv_size(aTHX_ st, (SV *)(GvGP(thing)->gp_form), recurse);
-           sv_size(aTHX_ st, (SV *)(GvGP(thing)->gp_av), recurse);
-           sv_size(aTHX_ st, (SV *)(GvGP(thing)->gp_hv), recurse);
-           sv_size(aTHX_ st, (SV *)(GvGP(thing)->gp_egv), recurse);
-           sv_size(aTHX_ st, (SV *)(GvGP(thing)->gp_cv), recurse);
+           ADD_SIZE(st, "GP", sizeof(GP));
+           sv_size(aTHX_ st, NPathLink("gp_sv", NPtype_LINK), (SV *)(GvGP(thing)->gp_sv), recurse);
+           sv_size(aTHX_ st, NPathLink("gp_form", NPtype_LINK), (SV *)(GvGP(thing)->gp_form), recurse);
+           sv_size(aTHX_ st, NPathLink("gp_av", NPtype_LINK), (SV *)(GvGP(thing)->gp_av), recurse);
+           sv_size(aTHX_ st, NPathLink("gp_hv", NPtype_LINK), (SV *)(GvGP(thing)->gp_hv), recurse);
+           sv_size(aTHX_ st, NPathLink("gp_egv", NPtype_LINK), (SV *)(GvGP(thing)->gp_egv), recurse);
+           sv_size(aTHX_ st, NPathLink("gp_cv", NPtype_LINK), (SV *)(GvGP(thing)->gp_cv), recurse);
        }
 #if (PERL_VERSION >= 9)
        TAG; break;
@@ -876,16 +1130,16 @@ sv_size(pTHX_ struct state *const st, const SV * const orig_thing,
   case SVt_PV: TAG;
   freescalar:
     if(recurse && SvROK(thing))
-       sv_size(aTHX_ st, SvRV_const(thing), recurse);
+       sv_size(aTHX_ st, NPathLink("RV", NPtype_LINK), SvRV_const(thing), recurse);
     else if (SvIsCOW_shared_hash(thing))
-       hek_size(aTHX_ st, SvSHARED_HEK_FROM_PV(SvPVX(thing)), 1);
+       hek_size(aTHX_ st, SvSHARED_HEK_FROM_PV(SvPVX(thing)), 1, NPathLink("SvSHARED_HEK_FROM_PV", NPtype_LINK));
     else
-       st->total_size += SvLEN(thing);
+       ADD_SIZE(st, "SvLEN", SvLEN(thing));
 
     if(SvOOK(thing)) {
        STRLEN len;
        SvOOK_offset(thing, len);
-       st->total_size += len;
+       ADD_SIZE(st, "SvOOK", len);
     }
     TAG;break;
 
@@ -935,8 +1189,12 @@ CODE:
   if (SvROK(thing)) {
     thing = SvRV(thing);
   }
-
-  sv_size(aTHX_ st, thing, ix);
+#ifdef PATH_TRACKING
+  st->add_attr_cb = dump_path;
+  if (st->add_attr_cb)
+    sv_dump(thing);
+#endif
+  sv_size(aTHX_ st, NULL, thing, ix);
   RETVAL = st->total_size;
   free_state(st);
 }