File Coverage

pl_stats.c
Criterion Covered Total %
statement 29 32 90.6
branch 9 12 75.0
condition n/a
subroutine n/a
pod n/a
total 38 44 86.3


line stmt bran cond sub pod time code
1             #include "pl_util.h"
2             #include "pl_stats.h"
3             #include "ppport.h"
4              
5 820           static void save_stat(pTHX_ Duk* duk, const char* category, const char* name, double value)
6             {
7 820           STRLEN clen = strlen(category);
8 820           STRLEN nlen = strlen(name);
9 820           HV* data = 0;
10 820           SV* pvalue = 0;
11 820           SV** found = hv_fetch(duk->stats, category, clen, 0);
12 820 100         if (found) {
13 618           SV* ref = SvRV(*found);
14             /* value not a valid hashref? bail out */
15 618 50         if (SvTYPE(ref) != SVt_PVHV) {
16 0           croak("Found category %s in stats but it is not a hashref\n", category);
17             return;
18             }
19 618           data = (HV*) ref;
20             } else {
21 202           SV* ref = 0;
22 202           data = newHV();
23 202           ref = newRV_noinc((SV*) data);
24 202 50         if (hv_store(duk->stats, category, clen, ref, 0)) {
25             #if 0
26             /* Not needed as ref is already 1 */
27             SvREFCNT_inc(ref);
28             #endif
29             }
30             else {
31 0           croak("Could not create category %s in stats\n", category);
32             }
33             }
34              
35 820           pvalue = sv_2mortal(newSVnv(value));
36 820 50         if (hv_store(data, name, nlen, pvalue, 0)) {
37 820           SvREFCNT_inc(pvalue);
38             }
39             else {
40 0           croak("Could not create entry %s for category %s in stats\n", name, category);
41             }
42             }
43              
44 5004470           void pl_stats_start(pTHX_ Duk* duk, Stats* stats)
45             {
46 5004470 100         if (!(duk->flags & DUK_OPT_FLAG_GATHER_STATS)) {
47 5004060           return;
48             }
49 410           stats->t0 = now_us();
50 410           stats->m0 = total_memory_pages() * duk->pagesize_bytes;
51             }
52              
53 5004466           void pl_stats_stop(pTHX_ Duk* duk, Stats* stats, const char* name)
54             {
55 5004466 100         if (!(duk->flags & DUK_OPT_FLAG_GATHER_STATS)) {
56 5004056           return;
57             }
58 410           stats->t1 = now_us();
59 410           stats->m1 = total_memory_pages() * duk->pagesize_bytes;
60              
61 410           save_stat(aTHX_ duk, name, "elapsed_us", stats->t1 - stats->t0);
62 410           save_stat(aTHX_ duk, name, "memory_bytes", stats->m1 - stats->m0);
63             }