| 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 |
▶ | if (found) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (found) { |
618 |
202 |
820 |
|
| 13 |
618 |
| SV* ref = SvRV(*found); |
| 14 |
|
| /* value not a valid hashref? bail out */ |
| 15 |
618 |
▶ | if (SvTYPE(ref) != SVt_PVHV) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (SvTYPE(ref) != SVt_PVHV) { |
0 |
618 |
618 |
|
| 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 |
▶ | if (hv_store(duk->stats, category, clen, ref, 0)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (hv_store(duk->stats, category, clen, ref, 0)) { |
0 |
202 |
202 |
|
| 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 |
▶ | if (hv_store(data, name, nlen, pvalue, 0)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (hv_store(data, name, nlen, pvalue, 0)) { |
820 |
0 |
820 |
|
| 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 |
▶ | if (!(duk->flags & DUK_OPT_FLAG_GATHER_STATS)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!(duk->flags & DUK_OPT_FLAG_GATHER_STATS)) { |
5004060 |
410 |
5004470 |
|
| 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 |
▶ | if (!(duk->flags & DUK_OPT_FLAG_GATHER_STATS)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!(duk->flags & DUK_OPT_FLAG_GATHER_STATS)) { |
5004056 |
410 |
5004466 |
|
| 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 |
|
| } |