Using check_new doesn't work for padlist_size. Mark weakrefs in link name.
[p5sagit/Devel-Size.git] / Size.xs
diff --git a/Size.xs b/Size.xs
index 01a91f8..20d6616 100644 (file)
--- a/Size.xs
+++ b/Size.xs
@@ -94,13 +94,15 @@ 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];
+    int min_recurse_threshold;
     /* 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);
+    void (*free_state_cb)(pTHX_ struct state *st);
     UV seqn;
     void *state_cb_data; /* free'd by free_state() after free_state_cb() call */
     /* this stuff wil be moved to state_cb_data later */
-    FILE *node_stream;
+    FILE *node_stream_fh;
+    char *node_stream_name;
 };
 
 #define ADD_SIZE(st, leafname, bytes) (NPathAddSizeCb(st, leafname, bytes) (st)->total_size += (bytes))
@@ -108,27 +110,26 @@ struct state {
 #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 dNPathNodes and NPathPushNode leaves NP pointing
+/* A subtle point here is that dNPathNodes and NPathPushNode leave 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.
+ * seqn==0 indicates the node is new (hasn't been output yet)
  */
 #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->seqn = NP->type = 0; NP->id = Nullch; /* safety/debug */ \
             NP->prev = prev_np
 #define NPathPushNode(nodeid, nodetype) \
             NP->id = nodeid; \
             NP->type = nodetype; \
+            NP->seqn = 0; \
             if(0)fprintf(stderr,"NPathPushNode (%p <-) %p <- [%d %s]\n", NP->prev, NP, nodetype,(char*)nodeid);\
             NP++; \
-            NP->id="?+?"; /* DEBUG */ \
+            NP->id = Nullch; /* safety/debug */ \
             NP->seqn = 0; \
             NP->prev = (NP-1)
 #define NPathSetNode(nodeid, nodetype) \
@@ -136,6 +137,8 @@ struct state {
             (NP-1)->type = nodetype; \
             if(0)fprintf(stderr,"NPathSetNode (%p <-) %p <- [%d %s]\n", (NP-1)->prev, (NP-1), nodetype,(char*)nodeid);\
             (NP-1)->seqn = 0;
+#define NPathPopNode \
+            --NP
 
 /* dNPathUseParent points NP directly the the parents' name_path_nodes array
  * So the function can only safely call ADD_*() but not NPathLink, unless the
@@ -149,8 +152,19 @@ struct state {
 #define NPtype_MAGIC    0x04
 #define NPtype_OP       0x05
 
-#define NPathLink(nodeid, nodetype)   ((NP->id = nodeid), (NP->type = nodetype), (NP->seqn = 0), NP)
+#define NPattr_LEAFSIZE 0x00
+#define NPattr_NAME     0x01
+#define NPattr_PADFAKE  0x02
+#define NPattr_PADNAME  0x03
+#define NPattr_PADTMP   0x04
+#define NPattr_NOTE     0x05
+
+#define _NPathLink(np, nid, ntype)   (((np)->id=nid), ((np)->type=ntype), ((np)->seqn=0))
+#define NPathLink(nid)               (_NPathLink(NP, nid, NPtype_LINK), NP)
+/* add a link and a name node to the path - a special case for op_size */
+#define NPathLinkAndNode(nid, nid2)  (_NPathLink(NP, nid, NPtype_LINK), _NPathLink(NP+1, nid2, NPtype_NAME), ((NP+1)->prev=NP), (NP+1))
 #define NPathOpLink  (NPathArg)
+#define NPathAddSizeCb(st, name, bytes) (st->add_attr_cb && st->add_attr_cb(st, NP-1, NPattr_LEAFSIZE, (name), (bytes))),
 #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
@@ -212,7 +226,7 @@ np_print_node_name(FILE *fp, npath_node_t *npath_node)
         break;
     }
     case NPtype_LINK:
-        fprintf(fp, "%s->", npath_node->id);
+        fprintf(fp, "%s", npath_node->id);
         break;
     case NPtype_NAME:
         fprintf(fp, "%s", npath_node->id);
@@ -231,59 +245,86 @@ np_dump_indent(int depth) {
 }
 
 int
-dump_formatted_node(struct state *st, npath_node_t *npath_node) {
-    np_dump_indent(npath_node->depth);
-    np_print_node_name(stderr, npath_node);
-    fprintf(stderr, "\t\t[#%ld @%u] ", npath_node->seqn, npath_node->depth);
-    fprintf(stderr, "\n");
-    return 0;
-}
-
-void
-np_walk_new_nodes(struct state *st, npath_node_t *npath_node, int (*cb)(struct state *st, npath_node_t *npath_node))
+np_walk_new_nodes(struct state *st,
+    npath_node_t *npath_node,
+    npath_node_t *npath_node_deeper,
+    int (*cb)(struct state *st, npath_node_t *npath_node, npath_node_t *npath_node_deeper))
 {
     if (npath_node->seqn) /* node already output */
-        return;
+        return 0;
 
     if (npath_node->prev) {
-        np_walk_new_nodes(st, npath_node->prev, cb); /* recurse */
+        np_walk_new_nodes(st, npath_node->prev, npath_node, 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);
+    if (cb) {
+        if (cb(st, npath_node, npath_node_deeper)) {
+            /* ignore this node */
+            assert(npath_node->prev);
+            assert(npath_node->depth);
+            assert(npath_node_deeper);
+            npath_node->depth--;
+            npath_node->seqn = --st->seqn;
+            npath_node_deeper->prev = npath_node->prev;
+        }
+    }
 
-    return;
+    return 0;
+}
+
+int
+np_dump_formatted_node(struct state *st, npath_node_t *npath_node, npath_node_t *npath_node_deeper) {
+    if (0 && npath_node->type == NPtype_LINK)
+        return 1;
+    np_dump_indent(npath_node->depth);
+    np_print_node_name(stderr, npath_node);
+    if (npath_node->type == NPtype_LINK)
+        fprintf(stderr, "->"); /* cosmetic */
+    fprintf(stderr, "\t\t[#%ld @%u] ", npath_node->seqn, npath_node->depth);
+    fprintf(stderr, "\n");
+    return 0;
 }
 
 int
 np_dump_node_path_info(struct state *st, npath_node_t *npath_node, UV attr_type, const char *attr_name, UV attr_value)
 {
-    if (!attr_type && !attr_value)
+    if (attr_type == NPattr_LEAFSIZE && !attr_value)
         return 0; /* ignore zero sized leaf items */
-    np_walk_new_nodes(st, npath_node, dump_formatted_node);
+    np_walk_new_nodes(st, npath_node, NULL, np_dump_formatted_node);
     np_dump_indent(npath_node->depth+1);
-    if (attr_type) {
+    switch (attr_type) {
+    case NPattr_LEAFSIZE:
+        fprintf(stderr, "+%ld %s =%ld", attr_value, attr_name, attr_value+st->total_size);
+        break;
+    case NPattr_NAME:
         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);
+        break;
+    case NPattr_NOTE:
+        fprintf(stderr, "~note %s %lu", attr_name, attr_value);
+        break;
+    case NPattr_PADTMP:
+    case NPattr_PADNAME:
+    case NPattr_PADFAKE:
+        fprintf(stderr, "~pad%lu %s %lu", attr_type, attr_name, attr_value);
+        break;
+    default:
+        fprintf(stderr, "~??? %s %lu", attr_name, attr_value);
+        break;
     }
     fprintf(stderr, "\n");
     return 0;
 }
 
 int
-np_stream_formatted_node(struct state *st, npath_node_t *npath_node) {
-    fprintf(st->node_stream, "N %lu %u ", npath_node->seqn,
-        (unsigned)npath_node->depth /* just to aid debugging */
+np_stream_formatted_node(struct state *st, npath_node_t *npath_node, npath_node_t *npath_node_deeper) {
+    fprintf(st->node_stream_fh, "-%u %lu %u ",
+        npath_node->type, npath_node->seqn, (unsigned)npath_node->depth
     );
-    np_print_node_name(st->node_stream, npath_node);
-    fprintf(st->node_stream, "\n");
+    np_print_node_name(st->node_stream_fh, npath_node);
+    fprintf(st->node_stream_fh, "\n");
     return 0;
 }
 
@@ -292,17 +333,18 @@ np_stream_node_path_info(struct state *st, npath_node_t *npath_node, UV attr_typ
 {
     if (!attr_type && !attr_value)
         return 0; /* ignore zero sized leaf items */
-    np_walk_new_nodes(st, npath_node, np_stream_formatted_node);
-    if (attr_type) {
-        fprintf(st->node_stream, "A %lu ", npath_node->seqn);   /* Attribute name and value */
+    np_walk_new_nodes(st, npath_node, NULL, np_stream_formatted_node);
+    if (attr_type) { /* Attribute type, name and value */
+        fprintf(st->node_stream_fh, "%lu %lu ", attr_type, npath_node->seqn);
     }
-    else {
-        fprintf(st->node_stream, "L %lu ", npath_node->seqn);   /* Leaf name and memory size */
+    else { /* Leaf name and memory size */
+        fprintf(st->node_stream_fh, "L %lu ", npath_node->seqn);
     }
-    fprintf(st->node_stream, "%lu %s\n", attr_value, attr_name);
+    fprintf(st->node_stream_fh, "%lu %s\n", attr_value, attr_name);
     return 0;
 }
 
+
 #endif /* PATH_TRACKING */
 
 
@@ -579,6 +621,17 @@ magic_size(pTHX_ const SV * const thing, struct state *st, pPATH) {
   dNPathNodes(1, NPathArg);
   MAGIC *magic_pointer = SvMAGIC(thing);
 
+  if (!magic_pointer)
+    return;
+
+  if (!SvMAGICAL(thing)) {
+    if (0) {
+        warn("Ignoring suspect magic on this SV\n");
+        sv_dump((SV*)thing);
+    }
+    return;
+  }
+
   /* push a dummy node for NPathSetNode to update inside the while loop */
   NPathPushNode("dummy", NPtype_NAME);
 
@@ -594,9 +647,10 @@ magic_size(pTHX_ const SV * const thing, struct state *st, pPATH) {
 
 
     TRY_TO_CATCH_SEGV {
-       sv_size(aTHX_ st, NPathLink("mg_obj", NPtype_LINK), magic_pointer->mg_obj, TOTAL_SIZE_RECURSION);
+        /* XXX only chase mg_obj if mg->mg_flags & MGf_REFCOUNTED ? */
+       sv_size(aTHX_ st, NPathLink("mg_obj"), magic_pointer->mg_obj, TOTAL_SIZE_RECURSION);
        if (magic_pointer->mg_len == HEf_SVKEY) {
-           sv_size(aTHX_ st, NPathLink("mg_ptr", NPtype_LINK), (SV *)magic_pointer->mg_ptr, TOTAL_SIZE_RECURSION);
+           sv_size(aTHX_ st, NPathLink("mg_ptr"), (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) {
@@ -605,7 +659,9 @@ magic_size(pTHX_ const SV * const thing, struct state *st, pPATH) {
            }
        }
 #endif
+        /* XXX also handle mg->mg_type == PERL_MAGIC_utf8 ? */
        else if (magic_pointer->mg_len > 0) {
+            if(0)do_magic_dump(0, Perl_debug_log, magic_pointer, 0, 0, FALSE, 0);
            if (check_new(st, magic_pointer->mg_ptr)) {
                ADD_SIZE(st, "mg_len", magic_pointer->mg_len);
            }
@@ -655,9 +711,10 @@ regex_size(const REGEXP * const baseregex, struct state *st, pPATH) {
 static void
 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.
+    /* op_size recurses to follow the chain of opcodes.  For the node path we
+     * don't want the chain to be 'nested' in the path so we use dNPathUseParent().
+     * Also, to avoid a link-to-a-link the caller should use NPathLinkAndNode()
+     * instead of NPathLink().
      */
     dNPathUseParent(NPathArg);
 
@@ -710,9 +767,9 @@ op_size(pTHX_ const OP * const baseop, struct state *st, pPATH)
            /* 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, NPathLink("PM_GETRE", NPtype_LINK));
+           regex_size(PM_GETRE((PMOP *)baseop), st, NPathLink("PM_GETRE"));
 #else
-           regex_size(((PMOP *)baseop)->op_pmregexp, st, NPathLink("op_pmregexp", NPtype_LINK));
+           regex_size(((PMOP *)baseop)->op_pmregexp, st, NPathLink("op_pmregexp"));
 #endif
            TAG;break;
        case OPc_SVOP: TAG;
@@ -720,7 +777,7 @@ op_size(pTHX_ const OP * const baseop, struct state *st, pPATH)
            if (!(baseop->op_type == OP_AELEMFAST
                  && baseop->op_flags & OPf_SPECIAL)) {
                /* not an OP_PADAV replacement */
-               sv_size(aTHX_ st, NPathLink("SVOP", NPtype_LINK), ((SVOP *)baseop)->op_sv, SOME_RECURSION);
+               sv_size(aTHX_ st, NPathLink("SVOP"), ((SVOP *)baseop)->op_sv, SOME_RECURSION);
            }
            TAG;break;
 #ifdef OA_PADOP
@@ -731,11 +788,11 @@ op_size(pTHX_ const OP * const baseop, struct state *st, pPATH)
 #ifdef OA_GVOP
       case OPc_GVOP: TAG;
          ADD_SIZE(st, "gvop", sizeof(struct gvop));
-         sv_size(aTHX_ st, NPathLink("GVOP", NPtype_LINK), ((GVOP *)baseop)->op_gv, SOME_RECURSION);
+         sv_size(aTHX_ st, NPathLink("GVOP"), ((GVOP *)baseop)->op_gv, SOME_RECURSION);
          TAG;break;
 #endif
        case OPc_PVOP: TAG;
-           check_new_and_strlen(st, ((PVOP *)baseop)->op_pv, NPathLink("op_pv", NPtype_LINK));
+           check_new_and_strlen(st, ((PVOP *)baseop)->op_pv, NPathLink("op_pv"));
            TAG;break;
        case OPc_LOOP: TAG;
            ADD_SIZE(st, "loop", sizeof(struct loop));
@@ -759,14 +816,15 @@ op_size(pTHX_ const OP * const baseop, struct state *st, pPATH)
           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, NPathLink("cop_label", NPtype_LINK));
+          check_new_and_strlen(st, basecop->cop_label, NPathLink("cop_label"));
 #endif
 #ifdef USE_ITHREADS
-          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));
+          check_new_and_strlen(st, basecop->cop_file, NPathLink("cop_file"));
+          check_new_and_strlen(st, basecop->cop_stashpv, NPathLink("cop_stashpv"));
 #else
-         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);
+          if (SvREFCNT(basecop->cop_stash) == 1) /* XXX hack? */
+            sv_size(aTHX_ st, NPathLink("cop_stash"), (SV *)basecop->cop_stash, SOME_RECURSION);
+         sv_size(aTHX_ st, NPathLink("cop_filegv"), (SV *)basecop->cop_filegv, SOME_RECURSION);
 #endif
 
         }
@@ -784,10 +842,12 @@ op_size(pTHX_ const OP * const baseop, struct state *st, pPATH)
 static void
 hek_size(pTHX_ struct state *st, HEK *hek, U32 shared, pPATH)
 {
-    dNPathUseParent(NPathArg);
+    dNPathNodes(1, NPathArg);
+
     /* Hash keys can be shared. Have we seen this before? */
     if (!check_new(st, hek))
        return;
+    NPathPushNode("hek", NPtype_NAME);
     ADD_SIZE(st, "hek_len", HEK_BASESIZE + hek->hek_len
 #if PERL_VERSION < 8
        + 1 /* No hash key flags prior to 5.8.0  */
@@ -907,19 +967,21 @@ const U8 body_sizes[SVt_LAST] = {
 };
 
 
+/* based on Perl_do_dump_pad() - wraps sv_size and adds ADD_ATTR calls for the pad names */
 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) {
+    if (!padlist)
         return;
-    }
+    if( 0 && !check_new(st, padlist))
+        return;
+
     pad_name = MUTABLE_AV(*av_fetch(MUTABLE_AV(padlist), 0, FALSE));
     pname = AvARRAY(pad_name);
 
@@ -929,13 +991,14 @@ padlist_size(pTHX_ struct state *const st, pPATH, PADLIST *padlist,
             namesv = NULL;
         }
         if (namesv) {
+            /* SvFAKE: On a pad name SV, that slot in the frame AV is a REFCNT'ed reference to a lexical from "outside" */
             if (SvFAKE(namesv))
-                ADD_ATTR(st, 1, SvPVX_const(namesv), ix);
+                ADD_ATTR(st, NPattr_PADFAKE, SvPVX_const(namesv), ix);
             else
-                ADD_ATTR(st, 1, SvPVX_const(namesv), ix);
+                ADD_ATTR(st, NPattr_PADNAME, SvPVX_const(namesv), ix);
         }
         else {
-            ADD_ATTR(st, 1, "SVs_PADTMP", ix);
+            ADD_ATTR(st, NPattr_PADTMP, "SVs_PADTMP", ix);
         }
 
     }
@@ -961,10 +1024,6 @@ sv_size(pTHX_ struct state *const st, pPATH, const SV * const orig_thing,
   NPathPushNode(thing, NPtype_SV);
   ADD_SIZE(st, "sv", sizeof(SV) + body_sizes[type]);
 
-  if (type >= SVt_PVMG) {
-      magic_size(aTHX_ thing, st, NPathLink("MG", NPtype_LINK));
-  }
-
   switch (type) {
 #if (PERL_VERSION < 11)
     /* Is it a reference? */
@@ -972,8 +1031,8 @@ sv_size(pTHX_ struct state *const st, pPATH, const SV * const orig_thing,
 #else
   case SVt_IV: TAG;
 #endif
-    if(recurse && SvROK(thing))
-       sv_size(aTHX_ st, NPathLink("RV", NPtype_LINK), SvRV_const(thing), recurse);
+    if(recurse && SvROK(thing)) /* XXX maybe don't follow weakrefs */
+       sv_size(aTHX_ st, (SvWEAKREF(thing) ? NPathLink("weakRV") : NPathLink("RV")), SvRV_const(thing), recurse);
     TAG;break;
 
   case SVt_PVAV: TAG;
@@ -983,11 +1042,11 @@ sv_size(pTHX_ struct state *const st, pPATH, const SV * const orig_thing,
       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) {
+      if (recurse >= st->min_recurse_threshold) {
          SSize_t i = AvFILLp(thing) + 1;
 
          while (i--)
-             sv_size(aTHX_ st, NPathLink("AVelem", NPtype_LINK), AvARRAY(thing)[i], recurse);
+             sv_size(aTHX_ st, NPathLink("AVelem"), AvARRAY(thing)[i], recurse);
       }
     }
     /* Add in the bits on the other side of the beginning */
@@ -1005,33 +1064,35 @@ sv_size(pTHX_ struct state *const st, pPATH, const SV * const orig_thing,
        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, NPathLink("ARYLEN", NPtype_LINK), AvARYLEN(thing), recurse);
+    sv_size(aTHX_ st, NPathLink("ARYLEN"), AvARYLEN(thing), recurse);
 #endif
     TAG;break;
+
   case SVt_PVHV: TAG;
     /* Now the array of buckets */
     ADD_SIZE(st, "hv_max", (sizeof(HE *) * (HvMAX(thing) + 1)));
     if (HvENAME(thing)) {
-        ADD_ATTR(st, 1, HvENAME(thing), 0);
+        ADD_ATTR(st, NPattr_NAME, HvENAME(thing), 0);
     }
     /* Now walk the bucket chain */
     if (HvARRAY(thing)) {
       HE *cur_entry;
       UV cur_bucket = 0;
-      NPathPushNode("HvARRAY", NPtype_LINK);
       for (cur_bucket = 0; cur_bucket <= HvMAX(thing); cur_bucket++) {
         cur_entry = *(HvARRAY(thing) + cur_bucket);
         while (cur_entry) {
+/* XXX a HE should probably be a node so the keys and values are seen as pairs */
           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) {
+         hek_size(aTHX_ st, cur_entry->hent_hek, HvSHAREKEYS(thing), NPathLink("hent_hek"));
+         if (recurse >= st->min_recurse_threshold) {
 /* I've seen a PL_strtab HeVAL == 0xC and 0x40C etc
  * just running perl -Mblib -Mstrict -MDevel::Size=:all -MCarp -e 'warn perl_size()'
  * but it seemed like a corruption - it would change come and go with irrelevant code changes.
  * so we protect against that here, but I'd like to know the cause.
  */
 if (PTR2UV(HeVAL(cur_entry)) > 0xFFF)
-             sv_size(aTHX_ st, NPathLink("HeVAL", NPtype_LINK), HeVAL(cur_entry), recurse);
+             sv_size(aTHX_ st, NPathLink("HeVAL"), HeVAL(cur_entry), recurse);
+else warn("skipped suspect HeVAL %p", HeVAL(cur_entry));
          }
           cur_entry = cur_entry->hent_next;
         }
@@ -1050,39 +1111,39 @@ if (PTR2UV(HeVAL(cur_entry)) > 0xFFF)
            if (count < 0)
                count = -count;
            while (--count)
-               hek_size(aTHX_ st, names[count], 1, NPathLink("HvAUXelem", NPtype_LINK));
+               hek_size(aTHX_ st, names[count], 1, NPathLink("HvAUXelem"));
        }
        else
 #endif
        {
-           hek_size(aTHX_ st, HvNAME_HEK(thing), 1, NPathLink("HvNAME_HEK", NPtype_LINK));
+           hek_size(aTHX_ st, HvNAME_HEK(thing), 1, NPathLink("HvNAME_HEK"));
        }
 
        ADD_SIZE(st, "xpvhv_aux", sizeof(struct xpvhv_aux));
        if (meta) {
            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);
+           sv_size(aTHX_ st, NPathLink("mro_nextmethod"), (SV *)meta->mro_nextmethod, TOTAL_SIZE_RECURSION);
 #if PERL_VERSION > 10 || (PERL_VERSION == 10 && PERL_SUBVERSION > 0)
-           sv_size(aTHX_ st, NPathLink("isa", NPtype_LINK), (SV *)meta->isa, TOTAL_SIZE_RECURSION);
+           sv_size(aTHX_ st, NPathLink("isa"), (SV *)meta->isa, TOTAL_SIZE_RECURSION);
 #endif
 #if PERL_VERSION > 10
-           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);
+           sv_size(aTHX_ st, NPathLink("mro_linear_all"), (SV *)meta->mro_linear_all, TOTAL_SIZE_RECURSION);
+           sv_size(aTHX_ st, NPathLink("mro_linear_current"), meta->mro_linear_current, TOTAL_SIZE_RECURSION);
 #else
-           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);
+           sv_size(aTHX_ st, NPathLink("mro_linear_dfs"), (SV *)meta->mro_linear_dfs, TOTAL_SIZE_RECURSION);
+           sv_size(aTHX_ st, NPathLink("mro_linear_c3"), (SV *)meta->mro_linear_c3, TOTAL_SIZE_RECURSION);
 #endif
        }
     }
 #else
-    check_new_and_strlen(st, HvNAME_get(thing), NPathLink("HvNAME", NPtype_LINK));
+    check_new_and_strlen(st, HvNAME_get(thing), NPathLink("HvNAME"));
 #endif
     TAG;break;
 
 
   case SVt_PVFM: TAG;
-    padlist_size(aTHX_ st, NPathLink("CvPADLIST", NPtype_LINK), CvPADLIST(thing), SOME_RECURSION);
-    sv_size(aTHX_ st, NPathLink("CvOUTSIDE", NPtype_LINK), (SV *)CvOUTSIDE(thing), recurse);
+    padlist_size(aTHX_ st, NPathLink("CvPADLIST"), CvPADLIST(thing), recurse);
+    sv_size(aTHX_ st, NPathLink("CvOUTSIDE"), (SV *)CvOUTSIDE(thing), recurse);
 
     if (st->go_yell && !st->fm_whine) {
       carp("Devel::Size: Calculated sizes for FMs are incomplete");
@@ -1091,28 +1152,28 @@ if (PTR2UV(HeVAL(cur_entry)) > 0xFFF)
     goto freescalar;
 
   case SVt_PVCV: TAG;
-    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);
+    /* not CvSTASH, per https://rt.cpan.org/Ticket/Display.html?id=79366 */
+    sv_size(aTHX_ st, NPathLink("SvSTASH"), (SV *)SvSTASH(thing), SOME_RECURSION);
+    sv_size(aTHX_ st, NPathLink("CvGV"), (SV *)CvGV(thing), SOME_RECURSION);
+    padlist_size(aTHX_ st, NPathLink("CvPADLIST"), CvPADLIST(thing), recurse);
+    sv_size(aTHX_ st, NPathLink("CvOUTSIDE"), (SV *)CvOUTSIDE(thing), recurse);
     if (CvISXSUB(thing)) {
-       sv_size(aTHX_ st, NPathLink("cv_const_sv", NPtype_LINK), cv_const_sv((CV *)thing), recurse);
+       sv_size(aTHX_ st, NPathLink("cv_const_sv"), cv_const_sv((CV *)thing), recurse);
     } else {
-       op_size(aTHX_ CvSTART(thing), st, NPathLink("CvSTART", NPtype_LINK));
-       op_size(aTHX_ CvROOT(thing), st, NPathLink("CvROOT", NPtype_LINK));
+       if(1)op_size(aTHX_ CvSTART(thing), st, NPathLinkAndNode("CvSTART", "OPs")); /* XXX ? */
+       op_size(aTHX_ CvROOT(thing), st, NPathLinkAndNode("CvROOT", "OPs"));
     }
     goto freescalar;
 
   case SVt_PVIO: TAG;
     /* Some embedded char pointers */
-    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));
+    check_new_and_strlen(st, ((XPVIO *) SvANY(thing))->xio_top_name, NPathLink("xio_top_name"));
+    check_new_and_strlen(st, ((XPVIO *) SvANY(thing))->xio_fmt_name, NPathLink("xio_fmt_name"));
+    check_new_and_strlen(st, ((XPVIO *) SvANY(thing))->xio_bottom_name, NPathLink("xio_bottom_name"));
     /* Throw the GVs on the list to be walked if they're not-null */
-    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);
+    sv_size(aTHX_ st, NPathLink("xio_top_gv"), (SV *)((XPVIO *) SvANY(thing))->xio_top_gv, recurse);
+    sv_size(aTHX_ st, NPathLink("xio_bottom_gv"), (SV *)((XPVIO *) SvANY(thing))->xio_bottom_gv, recurse);
+    sv_size(aTHX_ st, NPathLink("xio_fmt_gv"), (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
@@ -1131,13 +1192,13 @@ if (PTR2UV(HeVAL(cur_entry)) > 0xFFF)
   case SVt_PVGV: TAG;
     if(isGV_with_GP(thing)) {
 #ifdef GvNAME_HEK
-       hek_size(aTHX_ st, GvNAME_HEK(thing), 1, NPathLink("GvNAME_HEK", NPtype_LINK));
+       hek_size(aTHX_ st, GvNAME_HEK(thing), 1, NPathLink("GvNAME_HEK"));
 #else  
        ADD_SIZE(st, "GvNAMELEN", GvNAMELEN(thing));
 #endif
-        ADD_ATTR(st, 1, GvNAME_get(thing), 0);
+        ADD_ATTR(st, NPattr_NAME, GvNAME_get(thing), 0);
 #ifdef GvFILE_HEK
-       hek_size(aTHX_ st, GvFILE_HEK(thing), 1, NPathLink("GvFILE_HEK", NPtype_LINK));
+       hek_size(aTHX_ st, GvFILE_HEK(thing), 1, NPathLink("GvFILE_HEK"));
 #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
@@ -1147,18 +1208,18 @@ if (PTR2UV(HeVAL(cur_entry)) > 0xFFF)
           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), NPathLink("GvFILE", NPtype_LINK));
+       check_new_and_strlen(st, GvFILE(thing), NPathLink("GvFILE"));
 #  endif
 #endif
        /* Is there something hanging off the glob? */
        if (check_new(st, GvGP(thing))) {
            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);
+           sv_size(aTHX_ st, NPathLink("gp_sv"), (SV *)(GvGP(thing)->gp_sv), recurse);
+           sv_size(aTHX_ st, NPathLink("gp_av"), (SV *)(GvGP(thing)->gp_av), recurse);
+           sv_size(aTHX_ st, NPathLink("gp_hv"), (SV *)(GvGP(thing)->gp_hv), recurse);
+           sv_size(aTHX_ st, NPathLink("gp_cv"), (SV *)(GvGP(thing)->gp_cv), recurse);
+           sv_size(aTHX_ st, NPathLink("gp_egv"), (SV *)(GvGP(thing)->gp_egv), recurse);
+           sv_size(aTHX_ st, NPathLink("gp_form"), (SV *)(GvGP(thing)->gp_form), recurse);
        }
 #if (PERL_VERSION >= 9)
        TAG; break;
@@ -1173,9 +1234,9 @@ if (PTR2UV(HeVAL(cur_entry)) > 0xFFF)
   case SVt_PV: TAG;
   freescalar:
     if(recurse && SvROK(thing))
-       sv_size(aTHX_ st, NPathLink("RV", NPtype_LINK), SvRV_const(thing), recurse);
+       sv_size(aTHX_ st, NPathLink("RV"), SvRV_const(thing), recurse);
     else if (SvIsCOW_shared_hash(thing))
-       hek_size(aTHX_ st, SvSHARED_HEK_FROM_PV(SvPVX(thing)), 1, NPathLink("SvSHARED_HEK_FROM_PV", NPtype_LINK));
+       hek_size(aTHX_ st, SvSHARED_HEK_FROM_PV(SvPVX(thing)), 1, NPathLink("SvSHARED_HEK_FROM_PV"));
     else
        ADD_SIZE(st, "SvLEN", SvLEN(thing));
 
@@ -1187,9 +1248,29 @@ if (PTR2UV(HeVAL(cur_entry)) > 0xFFF)
     TAG;break;
 
   }
+
+  if (type >= SVt_PVMG) {
+      magic_size(aTHX_ thing, st, NPathLink("MG"));
+  }
+
   return;
 }
 
+static void
+free_memnode_state(pTHX_ struct state *st)
+{
+    if (st->node_stream_fh && st->node_stream_name) {
+        if (*st->node_stream_name == '|') {
+            if (pclose(st->node_stream_fh))
+                warn("%s exited with an error status\n", st->node_stream_name);
+        }
+        else {
+            if (fclose(st->node_stream_fh))
+                warn("Error closing %s: %s\n", st->node_stream_name, strerror(errno));
+        }
+    }
+}
+
 static struct state *
 new_state(pTHX)
 {
@@ -1198,6 +1279,7 @@ new_state(pTHX)
 
     Newxz(st, 1, struct state);
     st->go_yell = TRUE;
+    st->min_recurse_threshold = TOTAL_SIZE_RECURSION;
     if (NULL != (warn_flag = perl_get_sv("Devel::Size::warn", FALSE))) {
        st->dangle_whine = st->go_yell = SvIV(warn_flag) ? TRUE : FALSE;
     }
@@ -1211,16 +1293,56 @@ new_state(pTHX)
     check_new(st, &PL_sv_placeholder);
 #endif
 #ifdef PATH_TRACKING
-if (getenv("M"))
-st->node_stream = stdout;
-    if (st->node_stream)
+    if (getenv("MEMVIEW") && *getenv("MEMVIEW")) { /* XXX quick hack */
+        st->node_stream_name = getenv("MEMVIEW");
+        if (*st->node_stream_name == '|')
+            st->node_stream_fh = popen(st->node_stream_name+1, "w");
+        else
+            st->node_stream_fh = fopen(st->node_stream_name, "wb");
+        if (!st->node_stream_fh)
+            croak("Can't open '%s' for writing: %s", st->node_stream_name, strerror(errno));
+        setlinebuf(st->node_stream_fh); /* XXX temporary for debugging */
         st->add_attr_cb = np_stream_node_path_info;
-    else
+    }
+    else 
         st->add_attr_cb = np_dump_node_path_info;
+    st->free_state_cb = free_memnode_state;
 #endif
     return st;
 }
 
+/* XXX based on S_visit() in sv.c */
+static void
+unseen_sv_size(pTHX_ struct state *st, pPATH)
+{
+    dVAR;
+    SV* sva;
+    I32 visited = 0;
+    dNPathNodes(1, NPathArg);
+
+    NPathPushNode("unseen", NPtype_NAME);
+
+    /* by this point we should have visited all the SVs
+     * so now we'll run through all the SVs via the arenas
+     * in order to find any thet we've missed for some reason.
+     * Once the rest of the code is finding all the SVs then any
+     * found here will be leaks.
+     */
+    for (sva = PL_sv_arenaroot; sva; sva = MUTABLE_SV(SvANY(sva))) {
+        const SV * const svend = &sva[SvREFCNT(sva)];
+        SV* sv;
+        for (sv = sva + 1; sv < svend; ++sv) {
+            if (SvTYPE(sv) != (svtype)SVTYPEMASK && SvREFCNT(sv)) {
+                sv_size(aTHX_ st, NPathLink("arena"), sv, TOTAL_SIZE_RECURSION);
+            }
+            else if (check_new(st, sv)) { /* sanity check */
+                warn("unseen_sv_size encountered freed SV unexpectedly");
+                sv_dump(sv);
+            }
+        }
+    }
+}
+
 MODULE = Devel::Size        PACKAGE = Devel::Size       
 
 PROTOTYPES: DISABLE
@@ -1252,39 +1374,82 @@ UV
 perl_size()
 CODE:
 {
-  dNPathNodes(1, NULL);
   struct state *st = new_state(aTHX);
+  dNPathNodes(3, NULL);
+
+  st->min_recurse_threshold = NO_RECURSION; /* so always recurse */
+
   NPathPushNode("perl_size", NPtype_NAME); /* provide a root node */
-  
-  /* start with PL_defstash to get everything reachable from \%main::
-   * this seems to include PL_defgv, PL_incgv etc but I've listed them anyway
-   */
-  sv_size(aTHX_ st, NPathLink("PL_defstash", NPtype_LINK), (SV*)PL_defstash, TOTAL_SIZE_RECURSION);
-  sv_size(aTHX_ st, NPathLink("PL_defgv", NPtype_LINK), (SV*)PL_defgv, TOTAL_SIZE_RECURSION);
-  sv_size(aTHX_ st, NPathLink("PL_incgv", NPtype_LINK), (SV*)PL_incgv, TOTAL_SIZE_RECURSION);
-  sv_size(aTHX_ st, NPathLink("PL_rs", NPtype_LINK), (SV*)PL_rs, TOTAL_SIZE_RECURSION);
-  sv_size(aTHX_ st, NPathLink("PL_fdpid", NPtype_LINK), (SV*)PL_fdpid, TOTAL_SIZE_RECURSION);
-  sv_size(aTHX_ st, NPathLink("PL_modglobal", NPtype_LINK), (SV*)PL_modglobal, TOTAL_SIZE_RECURSION);
-  sv_size(aTHX_ st, NPathLink("PL_errors", NPtype_LINK), (SV*)PL_errors, TOTAL_SIZE_RECURSION);
-  sv_size(aTHX_ st, NPathLink("PL_stashcache", NPtype_LINK), (SV*)PL_stashcache, TOTAL_SIZE_RECURSION);
-  sv_size(aTHX_ st, NPathLink("PL_patchlevel", NPtype_LINK), (SV*)PL_patchlevel, TOTAL_SIZE_RECURSION);
-  sv_size(aTHX_ st, NPathLink("PL_apiversion", NPtype_LINK), (SV*)PL_apiversion, TOTAL_SIZE_RECURSION);
-  sv_size(aTHX_ st, NPathLink("PL_registered_mros", NPtype_LINK), (SV*)PL_registered_mros, TOTAL_SIZE_RECURSION);
+  /* start with PL_defstash to get everything reachable from \%main:: */
+  sv_size(aTHX_ st, NPathLink("PL_defstash"), (SV*)PL_defstash, TOTAL_SIZE_RECURSION);
+
+  NPathPushNode("others", NPtype_NAME); /* group these (typically much smaller) items */
+  sv_size(aTHX_ st, NPathLink("PL_defgv"), (SV*)PL_defgv, TOTAL_SIZE_RECURSION);
+  sv_size(aTHX_ st, NPathLink("PL_incgv"), (SV*)PL_incgv, TOTAL_SIZE_RECURSION);
+  sv_size(aTHX_ st, NPathLink("PL_rs"), (SV*)PL_rs, TOTAL_SIZE_RECURSION);
+  sv_size(aTHX_ st, NPathLink("PL_fdpid"), (SV*)PL_fdpid, TOTAL_SIZE_RECURSION);
+  sv_size(aTHX_ st, NPathLink("PL_modglobal"), (SV*)PL_modglobal, TOTAL_SIZE_RECURSION);
+  sv_size(aTHX_ st, NPathLink("PL_errors"), (SV*)PL_errors, TOTAL_SIZE_RECURSION);
+  sv_size(aTHX_ st, NPathLink("PL_stashcache"), (SV*)PL_stashcache, TOTAL_SIZE_RECURSION);
+  sv_size(aTHX_ st, NPathLink("PL_patchlevel"), (SV*)PL_patchlevel, TOTAL_SIZE_RECURSION);
+  sv_size(aTHX_ st, NPathLink("PL_apiversion"), (SV*)PL_apiversion, TOTAL_SIZE_RECURSION);
+  sv_size(aTHX_ st, NPathLink("PL_registered_mros"), (SV*)PL_registered_mros, TOTAL_SIZE_RECURSION);
 #ifdef USE_ITHREADS
-  sv_size(aTHX_ st, NPathLink("PL_regex_padav", NPtype_LINK), (SV*)PL_regex_padav, TOTAL_SIZE_RECURSION);
+  sv_size(aTHX_ st, NPathLink("PL_regex_padav"), (SV*)PL_regex_padav, TOTAL_SIZE_RECURSION);
 #endif
+  sv_size(aTHX_ st, NPathLink("PL_warnhook"), (SV*)PL_warnhook, TOTAL_SIZE_RECURSION);
+  sv_size(aTHX_ st, NPathLink("PL_diehook"), (SV*)PL_diehook, TOTAL_SIZE_RECURSION);
+  sv_size(aTHX_ st, NPathLink("PL_endav"), (SV*)PL_endav, TOTAL_SIZE_RECURSION);
+  sv_size(aTHX_ st, NPathLink("PL_main_cv"), (SV*)PL_main_cv, TOTAL_SIZE_RECURSION);
+  sv_size(aTHX_ st, NPathLink("PL_main_root"), (SV*)PL_main_root, TOTAL_SIZE_RECURSION);
+  sv_size(aTHX_ st, NPathLink("PL_main_start"), (SV*)PL_main_start, TOTAL_SIZE_RECURSION);
   /* TODO PL_pidstatus */
   /* TODO PL_stashpad */
-
-  /* in theory we shouldn't have any elements in PL_strtab that haven't been seen yet */
-  sv_size(aTHX_ st, NPathLink("PL_strtab", NPtype_LINK), (SV*)PL_strtab, TOTAL_SIZE_RECURSION);
+  /* TODO PL_compiling? COP */
 
   /* TODO stacks: cur, main, tmps, mark, scope, save */
-  /* TODO unused space in arenas */
-  /* TODO unused space in malloc, for whichever mallocs support it */
+  /* TODO PL_exitlist */
+  /* TODO PL_reentrant_buffers etc */
+  /* TODO environ */
+  /* TODO PerlIO? PL_known_layers PL_def_layerlist PL_perlio_fd_refcnt etc */
   /* TODO threads? */
   /* TODO anything missed? */
 
+  /* --- by this point we should have seen all reachable SVs --- */
+
+  /* in theory we shouldn't have any elements in PL_strtab that haven't been seen yet */
+  sv_size(aTHX_ st, NPathLink("PL_strtab"), (SV*)PL_strtab, TOTAL_SIZE_RECURSION);
+
+  /* unused space in sv head arenas */
+  if (PL_sv_root) {
+    SV *p = PL_sv_root;
+    UV free_heads = 1;
+#  define SvARENA_CHAIN(sv)     SvANY(sv) /* XXX */
+    while ((p = MUTABLE_SV(SvARENA_CHAIN(p)))) {
+        if (!check_new(st, p)) /* sanity check */
+            warn("Free'd SV head unexpectedly already seen");
+        ++free_heads;
+    }
+    NPathPushNode("unused_sv_heads", NPtype_NAME);
+    ADD_SIZE(st, "sv", free_heads * sizeof(SV));
+    NPathPopNode;
+  }
+  /* XXX iterate over bodies_by_type and crawl the free chains for each */
+
+  /* iterate over all SVs to find any we've not accounted for yet */
+  /* once the code above is visiting all SVs, any found here have been leaked */
+  unseen_sv_size(aTHX_ st, NPathLink("unaccounted"));
+
+  if (1) {
+    struct mstats ms = mstats();
+    NPathSetNode("unused malloc space", NPtype_NAME);
+    ADD_SIZE(st, "bytes_free", ms.bytes_free);
+    ADD_ATTR(st, NPattr_NOTE, "bytes_total", ms.bytes_total);
+    ADD_ATTR(st, NPattr_NOTE, "bytes_used",  ms.bytes_used);
+    ADD_ATTR(st, NPattr_NOTE, "chunks_used", ms.chunks_used);
+    ADD_ATTR(st, NPattr_NOTE, "chunks_free", ms.chunks_free);
+  }
+
   RETVAL = st->total_size;
   free_state(st);
 }