| 1 |
|
| /* You may distribute under the terms of either the GNU General Public License |
| 2 |
|
| * or the Artistic License (the same terms as Perl itself) |
| 3 |
|
| * |
| 4 |
|
| * (C) Paul Evans, 2022 -- leonerd@leonerd.org.uk |
| 5 |
|
| */ |
| 6 |
|
| #include "EXTERN.h" |
| 7 |
|
| #include "perl.h" |
| 8 |
|
| #include "XSUB.h" |
| 9 |
|
| |
| 10 |
|
| #include "AsyncAwait.h" |
| 11 |
|
| |
| 12 |
8 |
| static SV *S_call_metrics_method(pTHX_ U8 gimme, SV *metrics, const char *method, SV *arg1, SV *arg2) |
| 13 |
|
| { |
| 14 |
8 |
| dSP; |
| 15 |
8 |
| ENTER; |
| 16 |
8 |
| SAVETMPS; |
| 17 |
|
| |
| 18 |
8 |
▶ | EXTEND(SP, 3); |
Branch: 1 branch
| Branch | True | False | Total |
| EXTEND(SP, 3); |
0 |
8 |
8 |
|
| 19 |
8 |
▶ | PUSHMARK(SP); |
Branch: 1 branch
| Branch | True | False | Total |
| PUSHMARK(SP); |
0 |
8 |
8 |
|
| 20 |
8 |
| PUSHs(metrics); |
| 21 |
8 |
| mPUSHs(arg1); |
| 22 |
8 |
▶ | if(arg2) |
Branch: 1 branch
| Branch | True | False | Total |
| if(arg2) |
7 |
1 |
8 |
|
| 23 |
7 |
| mPUSHs(arg2); |
| 24 |
8 |
| PUTBACK; |
| 25 |
|
| |
| 26 |
8 |
| call_method(method, gimme); |
| 27 |
|
| |
| 28 |
|
| SV *ret; |
| 29 |
|
| |
| 30 |
8 |
▶ | if(gimme > G_VOID) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(gimme > G_VOID) { |
1 |
7 |
8 |
|
| 31 |
1 |
| SPAGAIN; |
| 32 |
1 |
| ret = POPs; |
| 33 |
1 |
| SvREFCNT_inc(ret); |
| 34 |
1 |
| PUTBACK; |
| 35 |
|
| } |
| 36 |
|
| |
| 37 |
8 |
▶ | FREETMPS; |
Branch: 1 branch
| Branch | True | False | Total |
| FREETMPS; |
8 |
0 |
8 |
|
| 38 |
8 |
| LEAVE; |
| 39 |
|
| |
| 40 |
8 |
| return ret; |
| 41 |
|
| } |
| 42 |
|
| |
| 43 |
|
| #define call_metrics_method_pvn(gimme, metrics, method, pv, len) \ |
| 44 |
|
| S_call_metrics_method(aTHX_ gimme, metrics, method, newSVpvn(pv, len), NULL) |
| 45 |
|
| #define call_metrics_method_pvn_iv(gimme, metrics, method, pv, len, iv) \ |
| 46 |
|
| S_call_metrics_method(aTHX_ gimme, metrics, method, newSVpvn(pv, len), newSViv(iv)) |
| 47 |
|
| |
| 48 |
|
| struct FAAMetricsState |
| 49 |
|
| { |
| 50 |
|
| SV *metrics; |
| 51 |
|
| |
| 52 |
|
| bool use_batch_mode; |
| 53 |
|
| |
| 54 |
|
| UV states_created_counter; |
| 55 |
|
| UV suspends_counter; |
| 56 |
|
| UV resumes_counter; |
| 57 |
|
| UV states_destroyed_counter; |
| 58 |
|
| |
| 59 |
|
| IV current_states_gauge; |
| 60 |
|
| IV current_subs_gauge; |
| 61 |
|
| }; |
| 62 |
|
| |
| 63 |
|
| XS_INTERNAL(flush_metrics); |
| 64 |
|
| |
| 65 |
8 |
| static struct FAAMetricsState *S_get_state(pTHX) |
| 66 |
|
| { |
| 67 |
|
| struct FAAMetricsState *state; |
| 68 |
|
| |
| 69 |
8 |
| SV **svp = hv_fetchs(PL_modglobal, "Future::AsyncAwait::Metrics/state", GV_ADD); |
| 70 |
8 |
▶ | if(SvOK(*svp)) |
Branch: 1 branch
| Branch | True | False | Total |
| if(SvOK(*svp)) |
7 |
1 |
8 |
|
| 71 |
7 |
| state = INT2PTR(struct FAAMetricsState *, SvUV(*svp)); |
| 72 |
|
| else { |
| 73 |
1 |
| Newx(state, 1, struct FAAMetricsState); |
| 74 |
1 |
| sv_setuv(*svp, PTR2UV(state)); |
| 75 |
|
| |
| 76 |
1 |
| state->metrics = get_sv("Future::AsyncAwait::Metrics::metrics", 0); |
| 77 |
|
| |
| 78 |
1 |
| SV *r = S_call_metrics_method(aTHX_ G_SCALAR, state->metrics, |
| 79 |
|
| "add_batch_mode_callback", |
| 80 |
1 |
| newRV_noinc((SV *)newXS_flags("flush_metrics", flush_metrics, __FILE__, NULL, 0)), |
| 81 |
|
| NULL |
| 82 |
|
| ); |
| 83 |
|
| |
| 84 |
1 |
▶ | if(r && SvTRUE(r)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if(r && SvTRUE(r)) { |
1 |
0 |
1 |
| if(r && SvTRUE(r)) { |
1 |
0 |
1 |
|
| 85 |
1 |
| state->use_batch_mode = TRUE; |
| 86 |
|
| |
| 87 |
1 |
| state->states_created_counter = 0; |
| 88 |
1 |
| state->suspends_counter = 0; |
| 89 |
1 |
| state->resumes_counter = 0; |
| 90 |
1 |
| state->states_destroyed_counter = 0; |
| 91 |
|
| |
| 92 |
1 |
| state->current_states_gauge = 0; |
| 93 |
1 |
| state->current_subs_gauge = 0; |
| 94 |
|
| } |
| 95 |
|
| else |
| 96 |
0 |
| state->use_batch_mode = FALSE; |
| 97 |
|
| } |
| 98 |
|
| |
| 99 |
8 |
| return state; |
| 100 |
|
| } |
| 101 |
|
| #define get_state() S_get_state(aTHX) |
| 102 |
|
| |
| 103 |
3 |
| XS_INTERNAL(flush_metrics) |
| 104 |
|
| { |
| 105 |
3 |
| struct FAAMetricsState *state = get_state(); |
| 106 |
|
| |
| 107 |
|
| #define FLUSH_COUNTER(name) \ |
| 108 |
|
| if(state->name##_counter) { \ |
| 109 |
|
| call_metrics_method_pvn_iv(G_VOID, state->metrics, "inc_counter_by", \ |
| 110 |
|
| "" #name "", sizeof(#name)-1, state->name##_counter); \ |
| 111 |
|
| state->name##_counter = 0; \ |
| 112 |
|
| } |
| 113 |
|
| |
| 114 |
3 |
▶ | FLUSH_COUNTER(states_created); |
Branch: 1 branch
| Branch | True | False | Total |
| FLUSH_COUNTER(states_created); |
1 |
2 |
3 |
|
| 115 |
3 |
▶ | FLUSH_COUNTER(suspends); |
Branch: 1 branch
| Branch | True | False | Total |
| FLUSH_COUNTER(suspends); |
2 |
1 |
3 |
|
| 116 |
3 |
▶ | FLUSH_COUNTER(resumes); |
Branch: 1 branch
| Branch | True | False | Total |
| FLUSH_COUNTER(resumes); |
1 |
2 |
3 |
|
| 117 |
3 |
▶ | FLUSH_COUNTER(states_destroyed); |
Branch: 1 branch
| Branch | True | False | Total |
| FLUSH_COUNTER(states_destroyed); |
0 |
3 |
3 |
|
| 118 |
|
| |
| 119 |
|
| #define FLUSH_GAUGE(name) \ |
| 120 |
|
| if(state->name##_gauge) { \ |
| 121 |
|
| call_metrics_method_pvn_iv(G_VOID, state->metrics, "inc_gauge_by", \ |
| 122 |
|
| "" #name "", sizeof(#name)-1, state->name##_gauge); \ |
| 123 |
|
| state->name##_gauge = 0; \ |
| 124 |
|
| } |
| 125 |
|
| |
| 126 |
3 |
▶ | FLUSH_GAUGE(current_states); |
Branch: 1 branch
| Branch | True | False | Total |
| FLUSH_GAUGE(current_states); |
1 |
2 |
3 |
|
| 127 |
3 |
▶ | FLUSH_GAUGE(current_subs); |
Branch: 1 branch
| Branch | True | False | Total |
| FLUSH_GAUGE(current_subs); |
2 |
1 |
3 |
|
| 128 |
3 |
| } |
| 129 |
|
| |
| 130 |
|
| #define INC_COUNTER(name) \ |
| 131 |
|
| if(state->use_batch_mode) \ |
| 132 |
|
| state->name##_counter++; \ |
| 133 |
|
| else \ |
| 134 |
|
| call_metrics_method_pvn(G_VOID, state->metrics, "inc_counter", "" #name "", sizeof(#name)-1) |
| 135 |
|
| |
| 136 |
|
| #define INC_GAUGE(name) \ |
| 137 |
|
| if(state->use_batch_mode) \ |
| 138 |
|
| state->name##_gauge++; \ |
| 139 |
|
| else \ |
| 140 |
|
| call_metrics_method_pvn(G_VOID, state->metrics, "inc_gauge", "" #name "", sizeof(#name)-1) |
| 141 |
|
| #define DEC_GAUGE(name) \ |
| 142 |
|
| if(state->use_batch_mode) \ |
| 143 |
|
| state->name##_gauge--; \ |
| 144 |
|
| else \ |
| 145 |
|
| call_metrics_method_pvn(G_VOID, state->metrics, "dec_gauge", "" #name "", sizeof(#name)-1) |
| 146 |
|
| |
| 147 |
1 |
| static void hook_post_cvcopy(pTHX_ CV *runcv, CV *cv, HV *modhookdata, void *hookdata) |
| 148 |
|
| { |
| 149 |
1 |
| struct FAAMetricsState *state = get_state(); |
| 150 |
|
| |
| 151 |
1 |
▶ | INC_COUNTER(states_created); |
Branch: 1 branch
| Branch | True | False | Total |
| INC_COUNTER(states_created); |
1 |
0 |
1 |
|
| 152 |
1 |
▶ | INC_GAUGE(current_states); |
Branch: 1 branch
| Branch | True | False | Total |
| INC_GAUGE(current_states); |
1 |
0 |
1 |
|
| 153 |
1 |
| } |
| 154 |
|
| |
| 155 |
2 |
| static void hook_post_suspend(pTHX_ CV *cv, HV *modhookdata, void *hookdata) |
| 156 |
|
| { |
| 157 |
2 |
| struct FAAMetricsState *state = get_state(); |
| 158 |
|
| |
| 159 |
2 |
▶ | INC_COUNTER(suspends); |
Branch: 1 branch
| Branch | True | False | Total |
| INC_COUNTER(suspends); |
2 |
0 |
2 |
|
| 160 |
2 |
▶ | INC_GAUGE(current_subs); |
Branch: 1 branch
| Branch | True | False | Total |
| INC_GAUGE(current_subs); |
2 |
0 |
2 |
|
| 161 |
2 |
| } |
| 162 |
|
| |
| 163 |
2 |
| static void hook_pre_resume(pTHX_ CV *cv, HV *modhookdata, void *hookdata) |
| 164 |
|
| { |
| 165 |
2 |
| struct FAAMetricsState *state = get_state(); |
| 166 |
|
| |
| 167 |
2 |
▶ | INC_COUNTER(resumes); |
Branch: 1 branch
| Branch | True | False | Total |
| INC_COUNTER(resumes); |
2 |
0 |
2 |
|
| 168 |
2 |
▶ | DEC_GAUGE(current_subs); |
Branch: 1 branch
| Branch | True | False | Total |
| DEC_GAUGE(current_subs); |
2 |
0 |
2 |
|
| 169 |
2 |
| } |
| 170 |
|
| |
| 171 |
0 |
| static void hook_free(pTHX_ CV *cv, HV *modhookdata, void *hookdata) |
| 172 |
|
| { |
| 173 |
0 |
| struct FAAMetricsState *state = get_state(); |
| 174 |
|
| |
| 175 |
0 |
▶ | INC_COUNTER(states_destroyed); |
Branch: 1 branch
| Branch | True | False | Total |
| INC_COUNTER(states_destroyed); |
0 |
0 |
0 |
|
| 176 |
0 |
▶ | DEC_GAUGE(current_states); |
Branch: 1 branch
| Branch | True | False | Total |
| DEC_GAUGE(current_states); |
0 |
0 |
0 |
|
| 177 |
0 |
| } |
| 178 |
|
| |
| 179 |
|
| static const struct AsyncAwaitHookFuncs hooks = { |
| 180 |
|
| .post_cv_copy = &hook_post_cvcopy, |
| 181 |
|
| .post_suspend = &hook_post_suspend, |
| 182 |
|
| .pre_resume = &hook_pre_resume, |
| 183 |
|
| .free = &hook_free, |
| 184 |
|
| }; |
| 185 |
|
| |
| 186 |
|
| MODULE = Future::AsyncAwait::Metrics PACKAGE = Future::AsyncAwait::Metrics |
| 187 |
|
| |
| 188 |
|
| BOOT: |
| 189 |
2 |
| boot_future_asyncawait(0.60); |
| 190 |
|
| |
| 191 |
2 |
| register_future_asyncawait_hook(&hooks, NULL); |