include/dbil_obs.h

filter: Statementstmt 90.0% 54 / 60 Branchbran 56.0% 37 / 66 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 72.2% 91 / 126 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/dbil_abi_impl.h ↑ Summary include/dbil_txn.h »
1 /* dbil_obs.h - the statement observer registry (dbil_abi v2).
2 *
3 * DBIx::Loop had one choke point every statement passes through, dbil_exec,
4 * and no way to watch it. That is the seam a slow-query log, a per-query
5 * metric or a database span all need, and none of them could be built
6 * without wrapping the Perl API and giving up the C path.
7 *
8 * Included after dbil_future.h (it attaches to a future) and before
9 * dbil_loop.h (which fires it). dbil_abi_impl.h, compiled later, registers
10 * into the same table.
11 *
12 * WHAT AN OBSERVER IS NOT GIVEN: the bind values. They are the literal data -
13 * names, tokens, card numbers - and an observer wanting the query text gets
14 * the prepared SQL, which has placeholders exactly where the sensitive parts
15 * would be. That is the correct thing to record anyway, and making it the
16 * only thing on offer means nobody has to remember the rule.
17 */
18  
19 #ifndef DBIL_OBS_H
20 #define DBIL_OBS_H
21  
22 #include "dbil_abi.h"
23  
24 static struct { dbil_obs_start_cb start; dbil_obs_done_cb done; void *ud; }
25 DBIL_OBS[DBIL_ABI_MAX_OBSERVERS];
26 static int DBIL_OBS_N = 0;
27  
28 typedef struct { void *tok[DBIL_ABI_MAX_OBSERVERS]; int n; } dbil_obs_tokens;
29  
30 /* The future struct without the croak. dbil_abi_impl.h has the same thing
31 * under its own name, but it is compiled after the site that fires this, so
32 * this file carries its own rather than reordering the whole include chain
33 * for an observer nobody may have registered. */
34 28 static dbil_future *dbil_obs_peek(pTHX_ SV *sv) {
35 MAGIC *mg;
36 28 ▶ if (!sv || !SvROK(sv)) return NULL;
Branch: 2 branches
BranchTrueFalseTotal
if (!sv || !SvROK(sv)) return NULL; 28 0 28
if (!sv || !SvROK(sv)) return NULL; 0 28 28
37 28 mg = mg_findext(SvRV(sv), PERL_MAGIC_ext, &dbil_future_vtbl);
38 28 ▶ return mg ? (dbil_future *)mg->mg_ptr : NULL;
Branch: 1 branch
BranchTrueFalseTotal
return mg ? (dbil_future *)mg->mg_ptr : NULL; 28 0 28
39 }
40  
41 3 static int dbil_obs_add(pTHX_ dbil_obs_start_cb start, dbil_obs_done_cb done,
42 void *ud) {
43 PERL_UNUSED_CONTEXT;
44 3 ▶ if (!start || DBIL_OBS_N >= DBIL_ABI_MAX_OBSERVERS) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (!start || DBIL_OBS_N >= DBIL_ABI_MAX_OBSERVERS) return 0; 3 0 3
if (!start || DBIL_OBS_N >= DBIL_ABI_MAX_OBSERVERS) return 0; 0 3 3
45 3 DBIL_OBS[DBIL_OBS_N].start = start;
46 3 DBIL_OBS[DBIL_OBS_N].done = done;
47 3 DBIL_OBS[DBIL_OBS_N].ud = ud;
48 3 DBIL_OBS_N++;
49 3 return 1;
50 }
51  
52 /* Fire the start half. Returns the tokens, or NULL when nobody is listening -
53 * which is the branch every uninstrumented statement takes. */
54 701 static dbil_obs_tokens *dbil_obs_start(pTHX_ int is_query, SV *sql,
55 AV *bind) {
56 dbil_obs_tokens *t;
57 701 STRLEN sl = 0;
58 1402 ▶ const char *sp = (sql && SvOK(sql)) ? SvPV_const(sql, sl) : "";
Branch: 2 branches
BranchTrueFalseTotal
const char *sp = (sql && SvOK(sql)) ? SvPV_const(sql, sl) : ""; 701 0 701
const char *sp = (sql && SvOK(sql)) ? SvPV_const(sql, sl) : ""; 701 0 701
59 701 ▶ int nbind = (bind && av_len(bind) >= 0) ? (int)(av_len(bind) + 1) : 0;
Branch: 2 branches
BranchTrueFalseTotal
int nbind = (bind && av_len(bind) >= 0) ? (int)(av_len(bind) + 1) : 0; 696 5 701
int nbind = (bind && av_len(bind) >= 0) ? (int)(av_len(bind) + 1) : 0; 581 115 696
60 int i;
61 701 ▶ if (!DBIL_OBS_N) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!DBIL_OBS_N) return NULL; 687 14 701
62 14 Newxz(t, 1, dbil_obs_tokens);
63 14 t->n = DBIL_OBS_N;
64 39 ▶ for (i = 0; i < DBIL_OBS_N; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < DBIL_OBS_N; i++) 25 14 39
65 25 t->tok[i] = DBIL_OBS[i].start(aTHX_ is_query, sp, sl, nbind,
66 DBIL_OBS[i].ud);
67 14 return t;
68 }
69  
70 14 static void dbil_obs_fire_done(pTHX_ dbil_obs_tokens *t, SV *res, SV *err) {
71 int i;
72 14 ▶ if (!t) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!t) return; 0 14 14
73 39 ▶ for (i = 0; i < t->n && i < DBIL_OBS_N; i++)
Branch: 2 branches
BranchTrueFalseTotal
for (i = 0; i < t->n && i < DBIL_OBS_N; i++) 25 14 39
for (i = 0; i < t->n && i < DBIL_OBS_N; i++) 25 0 25
74 25 ▶ if (DBIL_OBS[i].done) DBIL_OBS[i].done(aTHX_ t->tok[i], res, err,
Branch: 1 branch
BranchTrueFalseTotal
if (DBIL_OBS[i].done) DBIL_OBS[i].done(aTHX_ t->tok[i], res, err, 25 0 25
75 DBIL_OBS[i].ud);
76 14 Safefree(t);
77 }
78  
79 /* The settle half, as a future continuation. Attached to whatever dbil_exec
80 * returns, so it runs for all three backends - the native fd path, the forked
81 * pool and the plain synchronous handle - without any of them knowing. */
82 XS_INTERNAL(dbil_xs_obs_done);
83 14 XS_INTERNAL(dbil_xs_obs_done) {
84 14 dXSARGS;
85 14 AV *cap = dbil_clos_cap(aTHX_ cv);
86 SV **tp;
87 dbil_obs_tokens *t;
88 dbil_future *p;
89 14 ▶ if (!cap || items < 1) XSRETURN_EMPTY;
Branch: 2 branches
BranchTrueFalseTotal
if (!cap || items < 1) XSRETURN_EMPTY; 14 0 14
if (!cap || items < 1) XSRETURN_EMPTY; 0 14 14
90 14 tp = av_fetch(cap, 0, 0);
91 14 ▶ if (!(tp && *tp)) XSRETURN_EMPTY;
Branch: 2 branches
BranchTrueFalseTotal
if (!(tp && *tp)) XSRETURN_EMPTY; 14 0 14
if (!(tp && *tp)) XSRETURN_EMPTY; 0 14 14
92 14 t = INT2PTR(dbil_obs_tokens *, SvIV(*tp));
93 14 p = dbil_obs_peek(aTHX_ ST(0));
94 14 ▶ if (!p) { dbil_obs_fire_done(aTHX_ t, NULL, NULL); XSRETURN_EMPTY; }
Branch: 1 branch
BranchTrueFalseTotal
if (!p) { dbil_obs_fire_done(aTHX_ t, NULL, NULL); XSRETURN_EMPTY; } 0 14 14
95 14 ▶ if (p->state == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (p->state == 1) { 12 2 14
96 /* done: result is an arrayref of values, and the statement's own
97 * result is the first of them */
98 12 SV *v = NULL;
99 12 ▶ if (p->result && SvROK(p->result)
Branch: 2 branches
BranchTrueFalseTotal
if (p->result && SvROK(p->result) 12 0 12
if (p->result && SvROK(p->result) 12 0 12
100 12 ▶ && SvTYPE(SvRV(p->result)) == SVt_PVAV) {
Branch: 1 branch
BranchTrueFalseTotal
&& SvTYPE(SvRV(p->result)) == SVt_PVAV) { 12 0 12
101 12 AV *av = (AV *)SvRV(p->result);
102 12 ▶ SV **e = (av_len(av) >= 0) ? av_fetch(av, 0, 0) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV **e = (av_len(av) >= 0) ? av_fetch(av, 0, 0) : NULL; 12 0 12
103 12 ▶ v = (e && *e) ? *e : NULL;
Branch: 2 branches
BranchTrueFalseTotal
v = (e && *e) ? *e : NULL; 12 0 12
v = (e && *e) ? *e : NULL; 12 0 12
104 }
105 12 dbil_obs_fire_done(aTHX_ t, v, NULL);
106 }
107 2 else dbil_obs_fire_done(aTHX_ t, NULL, p->error);
108 14 XSRETURN_EMPTY;
109 }
110  
111 /* Attach the settle half to a statement's future. Mirrors what
112 * dbil_abi_future_on_ready does, open-coded because that lives in
113 * dbil_abi_impl.h and is compiled after the site that fires this. */
114 701 static void dbil_obs_attach(pTHX_ SV *fut, dbil_obs_tokens *t) {
115 dbil_future *p;
116 AV *cap;
117 SV *cv;
118 701 ▶ if (!t) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!t) return; 687 14 701
119 14 p = dbil_obs_peek(aTHX_ fut);
120 14 ▶ if (!p) { dbil_obs_fire_done(aTHX_ t, NULL, NULL); return; }
Branch: 1 branch
BranchTrueFalseTotal
if (!p) { dbil_obs_fire_done(aTHX_ t, NULL, NULL); return; } 0 14 14
121 14 cap = newAV();
122 14 av_push(cap, newSViv(PTR2IV(t)));
123 14 cv = dbil_closure(aTHX_ dbil_xs_obs_done, cap);
124 14 ▶ if (p->state) { /* already settled - fire now, never queue */
Branch: 1 branch
BranchTrueFalseTotal
if (p->state) { /* already settled - fire now, never queue */ 0 14 14
125 0 dSP;
126 0 ENTER; SAVETMPS;
127 0 ▶ PUSHMARK(SP); EXTEND(SP, 1); PUSHs(fut); PUTBACK;
Branch: 2 branches
BranchTrueFalseTotal
PUSHMARK(SP); EXTEND(SP, 1); PUSHs(fut); PUTBACK; 0 0 0
PUSHMARK(SP); EXTEND(SP, 1); PUSHs(fut); PUTBACK; 0 0 0
128 0 call_sv(cv, G_DISCARD | G_EVAL);
129 0 ▶ SPAGAIN; PUTBACK; FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
SPAGAIN; PUTBACK; FREETMPS; LEAVE; 0 0 0
130 0 SvREFCNT_dec(cv);
131 }
132 14 else av_push(p->cbs, cv); /* the queue owns it */
133 }
134  
135 #endif /* DBIL_OBS_H */
« include/dbil_abi_impl.h ↑ Summary include/dbil_txn.h »