Simple.xs

filter: Statementstmt 89.1% 371 / 416 Branchbran 55.4% 284 / 512 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 70.5% 655 / 928 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
↑ Summary blib/lib/Graphite/Simple.pm »
1 // PERL_NO_GET_CONTEXT is not used here, so it's OK to define it after inculding these files
2 #include "EXTERN.h"
3 #include "perl.h"
4  
5 // There are a lot of macro about threads: USE_ITHREADS, USE_5005THREADS, I_PTHREAD, I_MACH_CTHREADS, OLD_PTHREADS_API
6 // This symbol, if defined, indicates that Perl should be built to use the interpreter-based threading implementation.
7 #ifndef USE_ITHREADS
8 # define PERL_NO_GET_CONTEXT
9 #endif
10  
11 #include "XSUB.h"
12  
13 #ifdef I_PTHREAD
14 # include "pthread.h"
15 #endif
16  
17 #ifdef I_MACH_CTHREADS
18 # include "mach/cthreads.h"
19 #endif
20  
21 #include <errno.h>
22 #include <string>
23 #include <string_view>
24 #include <netdb.h>
25 #include <sys/socket.h>
26 #include <sys/un.h>
27  
28 #ifdef WIN32
29 # include <windows.h>
30 # include <winsock2.h>
31 #else
32 # include <netinet/in.h>
33 # include <arpa/inet.h>
34 # include <sys/types.h>
35 #endif
36  
37 #include <time.h>
38 #include <fcntl.h>
39  
40 using namespace std;
41  
42 struct xs_state {
43 uint32_t invalid_key_counter;
44 HV* bulk_hv;
45 HV* avg_hv;
46 HV* invalid_hv;
47 REGEXP* block_re;
48 SV* hostname;
49 SV* global_prefix;
50 SV* sender_name;
51 SV* sock_path;
52 uint32_t port;
53 int sock_fd;
54 bool is_sock_stream;
55 bool is_enabled;
56 bool is_connected;
57 bool use_global_storage;
58 bool store_invalid_metrics;
59 sockaddr_in sock_addr_inet;
60 sockaddr_un sock_addr_unix;
61 };
62  
63 typedef struct xs_state GraphiteXS_Object;
64  
65 static SV* sv_use_sock_stream_key;
66 static SV* sv_store_invalid_metrics_key;
67 static SV* sv_use_global_storage_key;
68 static SV* sv_sender_key;
69 static SV* sv_block_re_key;
70 static SV* sv_sock_path_key;
71 static SV* sv_host_key;
72 static SV* sv_port_key;
73 static SV* sv_enabled_key;
74 static SV* sv_prefix_key;
75 static uint32_t MAX_CHUNK_SIZE;
76  
77 81 inline void increment_hash_value_by ( HV* hv, SV* key, NV value ) {
78  
79 81 HE *ent = hv_fetch_ent( hv, key, 0, 0 );
80  
81 81 ▶ if (ent == NULL) // adding the new key
Branch: 1 branch
BranchTrueFalseTotal
if (ent == NULL) // adding the new key 65 16 81
82 65 ▶ hv_store_ent( hv, move( key ), move( newSVnv( value ) ), 0 );
Branch: 2 branches
BranchTrueFalseTotal
hv_store_ent( hv, move( key ), move( newSVnv( value ) ), 0 ); 65 0 65
hv_store_ent( hv, move( key ), move( newSVnv( value ) ), 0 ); 65 0 65
83 else { // updating the existed key
84  
85 16 SV* he_value = HeVAL(ent);
86 16 ▶ NV nv_value = SvNV( he_value ) + (double) value;
Branch: 1 branch
BranchTrueFalseTotal
NV nv_value = SvNV( he_value ) + (double) value; 16 0 16
87  
88 16 ▶ if (!SvREADONLY( he_value ))
Branch: 1 branch
BranchTrueFalseTotal
if (!SvREADONLY( he_value )) 16 0 16
89 16 ▶ sv_setnv( he_value, move( nv_value ) );
Branch: 1 branch
BranchTrueFalseTotal
sv_setnv( he_value, move( nv_value ) ); 16 0 16
90 else
91 0 ▶ hv_store_ent( hv, move(key), move( newSVnv( nv_value ) ), move( HeHASH(ent) ) );
Branch: 2 branches
BranchTrueFalseTotal
hv_store_ent( hv, move(key), move( newSVnv( nv_value ) ), move( HeHASH(ent) ) ); 0 0 0
hv_store_ent( hv, move(key), move( newSVnv( nv_value ) ), move( HeHASH(ent) ) ); 0 0 0
92 }
93 81 }
94  
95 61 inline bool is_metric_blocked_ (GraphiteXS_Object* graphite, SV* key) {
96 61 bool is_blocked = false;
97 61 ▶ if ( NULL != graphite->block_re ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( NULL != graphite->block_re ) { 26 35 61
98 26 char* ckey = SvPVX(key);
99 26 ▶ if ( pregexec( graphite->block_re, ckey, SvEND(key), move(ckey), 1, move(key), 1 ) )
Branch: 2 branches
BranchTrueFalseTotal
if ( pregexec( graphite->block_re, ckey, SvEND(key), move(ckey), 1, move(key), 1 ) ) 26 0 26
if ( pregexec( graphite->block_re, ckey, SvEND(key), move(ckey), 1, move(key), 1 ) ) 2 24 26
100 2 is_blocked = true;
101 }
102 61 return is_blocked;
103 }
104  
105 78 static inline bool is_valid_key_ (GraphiteXS_Object* graphite, SV* key) {
106  
107 // https://www.linux.org.ru/forum/development/5908806
108 // pregcomp pregexec pregfree pregfree2 (man perlapi)
109  
110 78 bool is_valid = true;
111 static REGEXP* key_re;
112  
113 78 ▶ if ( key_re == NULL ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( key_re == NULL ) { 1 77 78
114 1 const char* pattern = "[^a-zA-Z0-9_\\.-]";
115 1 ▶ SV* svpat = sv_2mortal(newSVpv(pattern, strlen(pattern)));
Branch: 2 branches
BranchTrueFalseTotal
SV* svpat = sv_2mortal(newSVpv(pattern, strlen(pattern))); 1 0 1
SV* svpat = sv_2mortal(newSVpv(pattern, strlen(pattern))); 1 0 1
116 1 ▶ key_re = pregcomp(move(svpat), 0);
Branch: 1 branch
BranchTrueFalseTotal
key_re = pregcomp(move(svpat), 0); 1 0 1
117 }
118  
119 // if (! defined($key) || "${key}" eq '' || !utf8::valid($key) || $key =~ m#[^a-zA-Z0-9\-_\.]#) {
120 78 ▶ if ( SVt_NULL == SvTYPE(key) )
Branch: 1 branch
BranchTrueFalseTotal
if ( SVt_NULL == SvTYPE(key) ) 1 77 78
121 1 is_valid = false;
122 else {
123 77 char* ckey = SvPVX(key);
124 77 ▶ if ( 0 == strlen(ckey) || ! SvPOK_only_UTF8(key) || pregexec( key_re, ckey, SvEND(key), move(ckey), 1, move(key), 1 ) )
Branch: 5 branches
BranchTrueFalseTotal
if ( 0 == strlen(ckey) || ! SvPOK_only_UTF8(key) || pregexec( key_re, ckey, SvEND(key), move(ckey), 1, move(key), 1 ) ) 68 9 77
if ( 0 == strlen(ckey) || ! SvPOK_only_UTF8(key) || pregexec( key_re, ckey, SvEND(key), move(ckey), 1, move(key), 1 ) ) 68 0 68
if ( 0 == strlen(ckey) || ! SvPOK_only_UTF8(key) || pregexec( key_re, ckey, SvEND(key), move(ckey), 1, move(key), 1 ) ) 68 0 68
if ( 0 == strlen(ckey) || ! SvPOK_only_UTF8(key) || pregexec( key_re, ckey, SvEND(key), move(ckey), 1, move(key), 1 ) ) 4 64 68
if ( 0 == strlen(ckey) || ! SvPOK_only_UTF8(key) || pregexec( key_re, ckey, SvEND(key), move(ckey), 1, move(key), 1 ) ) 13 64 77
125 13 is_valid = false;
126 }
127  
128 78 ▶ if ( ! is_valid )
Branch: 1 branch
BranchTrueFalseTotal
if ( ! is_valid ) 14 64 78
129 14 graphite->invalid_key_counter++;
130  
131 78 return is_valid;
132 }
133  
134 68 static inline void increment_metric_ (GraphiteXS_Object* graphite, SV* key, NV value) {
135 68 bool is_valid = is_valid_key_(graphite, key);
136 68 ▶ if ( is_valid && ! is_metric_blocked_(graphite, key) ) {
Branch: 3 branches
BranchTrueFalseTotal
if ( is_valid && ! is_metric_blocked_(graphite, key) ) { 61 7 68
if ( is_valid && ! is_metric_blocked_(graphite, key) ) { 59 2 61
if ( is_valid && ! is_metric_blocked_(graphite, key) ) { 59 9 68
137 59 increment_hash_value_by( move(graphite->bulk_hv), key, move(value) );
138 61 ▶ static string key_matcher ('\0', 128);
Branch: 4 branches
BranchTrueFalseTotal
static string key_matcher ('\0', 128); 1 58 59
static string key_matcher ('\0', 128); 1 0 1
static string key_matcher ('\0', 128); 1 0 1
static string key_matcher ('\0', 128); 0 0 0
139 59 key_matcher.assign(SvPVX( key ));
140 59 ▶ if ( 0 == key_matcher.find( "avg." ) )
Branch: 1 branch
BranchTrueFalseTotal
if ( 0 == key_matcher.find( "avg." ) ) 18 41 59
141 18 increment_hash_value_by( move(graphite->avg_hv), move(key), 1 );
142 9 ▶ } else if (! is_valid && graphite->store_invalid_metrics) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (! is_valid && graphite->store_invalid_metrics) { 7 2 9
} else if (! is_valid && graphite->store_invalid_metrics) { 1 6 7
143 1 increment_hash_value_by( graphite->invalid_hv, move(key), move(value) );
144 }
145 68 }
146  
147 24 inline void clear_bulk_(GraphiteXS_Object* graphite) {
148 24 ▶ if (graphite->store_invalid_metrics)
Branch: 1 branch
BranchTrueFalseTotal
if (graphite->store_invalid_metrics) 1 23 24
149 1 hv_clear(graphite->invalid_hv);
150 24 hv_clear(graphite->bulk_hv);
151 24 hv_clear(graphite->avg_hv);
152 24 graphite->invalid_key_counter = 0;
153 24 }
154  
155 10 inline void set_blocked_metrics_re_ (GraphiteXS_Object* graphite, SV* block_re) {
156 10 ▶ if (!block_re || SVt_NULL == SvTYPE(block_re)) {
Branch: 2 branches
BranchTrueFalseTotal
if (!block_re || SVt_NULL == SvTYPE(block_re)) { 10 0 10
if (!block_re || SVt_NULL == SvTYPE(block_re)) { 4 6 10
157 4 ▶ if (graphite->block_re) // pregfree(graphite->block_re)
Branch: 1 branch
BranchTrueFalseTotal
if (graphite->block_re) // pregfree(graphite->block_re) 3 1 4
158 3 SvREFCNT_dec_NN(graphite->block_re);
159 4 graphite->block_re = NULL;
160 }
161 else {
162 // if (SvREADONLY(block_re)) // can be set via perl call of Internals::SvREADONLY($var => 1);
163  
164 6 ▶ if (graphite->block_re) // pregfree
Branch: 1 branch
BranchTrueFalseTotal
if (graphite->block_re) // pregfree 3 3 6
165 3 ▶ SvREFCNT_dec_NN(graphite->block_re);
Branch: 1 branch
BranchTrueFalseTotal
SvREFCNT_dec_NN(graphite->block_re); 3 0 3
166  
167 6 char* pattern = SvPV_nolen(block_re);
168 6 ▶ SV* svpat = sv_2mortal(newSVpv(pattern, strlen(pattern)));
Branch: 2 branches
BranchTrueFalseTotal
SV* svpat = sv_2mortal(newSVpv(pattern, strlen(pattern))); 6 0 6
SV* svpat = sv_2mortal(newSVpv(pattern, strlen(pattern))); 6 0 6
169 6 ▶ graphite->block_re = pregcomp(move(svpat), 0);
Branch: 1 branch
BranchTrueFalseTotal
graphite->block_re = pregcomp(move(svpat), 0); 6 0 6
170 // sv_dump((SV *) graphite->block_re);
171 // graphite->block_re = SvRX(block_re); // "NULL" will be returned if a REGEXP* is not found.
172 }
173 10 }
174  
175 3 inline void calculate_result_metrics_ (GraphiteXS_Object* graphite, HV* res_hv) {
176  
177 3 HE* bulk_entry = NULL;
178  
179 3 ▶ hv_iterinit(graphite->bulk_hv);
Branch: 1 branch
BranchTrueFalseTotal
hv_iterinit(graphite->bulk_hv); 3 0 3
180  
181 21 ▶ while (bulk_entry = hv_iternext(graphite->bulk_hv)) {
Branch: 2 branches
BranchTrueFalseTotal
while (bulk_entry = hv_iternext(graphite->bulk_hv)) { 21 0 21
while (bulk_entry = hv_iternext(graphite->bulk_hv)) { 18 3 21
182  
183 18 ▶ SV* sv_bulk_key = hv_iterkeysv(bulk_entry);
Branch: 1 branch
BranchTrueFalseTotal
SV* sv_bulk_key = hv_iterkeysv(bulk_entry); 18 0 18
184 18 ▶ SV* sv_bulk_value = hv_iterval(graphite->bulk_hv, move(bulk_entry));
Branch: 1 branch
BranchTrueFalseTotal
SV* sv_bulk_value = hv_iterval(graphite->bulk_hv, move(bulk_entry)); 18 0 18
185 18 ▶ HE* avg_entry = hv_fetch_ent(graphite->avg_hv, sv_bulk_key, NULL, 0);
Branch: 1 branch
BranchTrueFalseTotal
HE* avg_entry = hv_fetch_ent(graphite->avg_hv, sv_bulk_key, NULL, 0); 18 0 18
186 18 ▶ HE* res_entry = hv_fetch_ent(res_hv, move(sv_bulk_key), NULL, 0);
Branch: 1 branch
BranchTrueFalseTotal
HE* res_entry = hv_fetch_ent(res_hv, move(sv_bulk_key), NULL, 0); 18 0 18
187 SV* sv_result_value;
188  
189 18 ▶ if (avg_entry) {
Branch: 1 branch
BranchTrueFalseTotal
if (avg_entry) { 5 13 18
190 5 ▶ SV* sv_avg_value = hv_iterval(graphite->avg_hv, move(avg_entry));
Branch: 1 branch
BranchTrueFalseTotal
SV* sv_avg_value = hv_iterval(graphite->avg_hv, move(avg_entry)); 5 0 5
191 5 ▶ sv_result_value = sv_2mortal(newSV(0));
Branch: 2 branches
BranchTrueFalseTotal
sv_result_value = sv_2mortal(newSV(0)); 5 0 5
sv_result_value = sv_2mortal(newSV(0)); 5 0 5
192 5 ▶ sv_setnv( sv_result_value, SvNV(sv_bulk_value) / SvNV(sv_avg_value) );
Branch: 3 branches
BranchTrueFalseTotal
sv_setnv( sv_result_value, SvNV(sv_bulk_value) / SvNV(sv_avg_value) ); 5 0 5
sv_setnv( sv_result_value, SvNV(sv_bulk_value) / SvNV(sv_avg_value) ); 5 0 5
sv_setnv( sv_result_value, SvNV(sv_bulk_value) / SvNV(sv_avg_value) ); 5 0 5
193 }
194 else
195 13 ▶ sv_result_value = sv_mortalcopy(move(sv_bulk_value));
Branch: 1 branch
BranchTrueFalseTotal
sv_result_value = sv_mortalcopy(move(sv_bulk_value)); 13 0 13
196  
197 18 SvREFCNT_inc_simple_void_NN(sv_result_value);
198 // HeHASH(res_entry) wasn't calcualted previuously due to it's an empty hash
199 18 ▶ hv_store_ent(res_hv, move(sv_bulk_key), move(sv_result_value), 0);
Branch: 1 branch
BranchTrueFalseTotal
hv_store_ent(res_hv, move(sv_bulk_key), move(sv_result_value), 0); 18 0 18
200 }
201 3 }
202  
203 10 void calculate_result_metrics_ (GraphiteXS_Object* graphite) {
204  
205 10 HE* avg_entry = NULL;
206  
207 10 ▶ hv_iterinit(graphite->avg_hv);
Branch: 1 branch
BranchTrueFalseTotal
hv_iterinit(graphite->avg_hv); 10 0 10
208  
209 17 ▶ while (avg_entry = hv_iternext(graphite->avg_hv)) {
Branch: 2 branches
BranchTrueFalseTotal
while (avg_entry = hv_iternext(graphite->avg_hv)) { 17 0 17
while (avg_entry = hv_iternext(graphite->avg_hv)) { 7 10 17
210  
211 7 ▶ SV* sv_avg_key = hv_iterkeysv(avg_entry);
Branch: 1 branch
BranchTrueFalseTotal
SV* sv_avg_key = hv_iterkeysv(avg_entry); 7 0 7
212 7 ▶ SV* sv_avg_value = hv_iterval(graphite->bulk_hv, move(avg_entry));
Branch: 1 branch
BranchTrueFalseTotal
SV* sv_avg_value = hv_iterval(graphite->bulk_hv, move(avg_entry)); 7 0 7
213 7 ▶ HE* bulk_entry = hv_fetch_ent(graphite->bulk_hv, sv_avg_key, NULL, 0);
Branch: 1 branch
BranchTrueFalseTotal
HE* bulk_entry = hv_fetch_ent(graphite->bulk_hv, sv_avg_key, NULL, 0); 7 0 7
214  
215 7 ▶ if (!bulk_entry)
Branch: 1 branch
BranchTrueFalseTotal
if (!bulk_entry) 0 7 7
216 0 croak("No bulk entry found for key '%s'", SvPV_nolen(sv_avg_key));
217  
218 7 ▶ SV* sv_bulk_value = hv_iterval(graphite->bulk_hv, move(bulk_entry));
Branch: 1 branch
BranchTrueFalseTotal
SV* sv_bulk_value = hv_iterval(graphite->bulk_hv, move(bulk_entry)); 7 0 7
219 7 ▶ sv_setnv( sv_bulk_value, SvNV(sv_bulk_value) / SvNV(sv_avg_value) );
Branch: 3 branches
BranchTrueFalseTotal
sv_setnv( sv_bulk_value, SvNV(sv_bulk_value) / SvNV(sv_avg_value) ); 7 0 7
sv_setnv( sv_bulk_value, SvNV(sv_bulk_value) / SvNV(sv_avg_value) ); 7 0 7
sv_setnv( sv_bulk_value, SvNV(sv_bulk_value) / SvNV(sv_avg_value) ); 7 0 7
220  
221 7 SvREFCNT_inc_simple_void_NN(sv_bulk_value);
222 7 ▶ hv_store_ent(graphite->bulk_hv, move(sv_avg_key), move(sv_bulk_value), move(HeHASH(bulk_entry)));
Branch: 1 branch
BranchTrueFalseTotal
hv_store_ent(graphite->bulk_hv, move(sv_avg_key), move(sv_bulk_value), move(HeHASH(bulk_entry))); 7 0 7
223 }
224 10 }
225  
226 13 inline void apply_store_invalid_metrics_key_ (GraphiteXS_Object* graphite, HV* opts) {
227 13 HE* entry = hv_fetch_ent(opts, sv_store_invalid_metrics_key, NULL, 0);
228 13 ▶ if (entry)
Branch: 1 branch
BranchTrueFalseTotal
if (entry) 1 12 13
229 1 graphite->store_invalid_metrics = SvIVx(hv_iterval(opts, entry)) ? true : false;
230 13 }
231  
232 13 inline void apply_sender_key_ (GraphiteXS_Object* graphite, HV* opts) {
233 13 HE* entry = entry = hv_fetch_ent(opts, sv_sender_key, NULL, 0);
234 13 ▶ if (entry) {
Branch: 1 branch
BranchTrueFalseTotal
if (entry) { 7 6 13
235 7 graphite->sender_name = move(HeVAL(entry));
236  
237 7 ▶ if ( !graphite->sender_name )
Branch: 1 branch
BranchTrueFalseTotal
if ( !graphite->sender_name ) 0 7 7
238 0 croak("Sender sub name can't be undefined. Specify its name with package. Example: My::Package::my_sub");
239  
240 // Let's check that the sub exists
241 7 ▶ if (!(CV *) get_cv(SvPVX(graphite->sender_name), 0))
Branch: 1 branch
BranchTrueFalseTotal
if (!(CV *) get_cv(SvPVX(graphite->sender_name), 0)) 0 7 7
242 0 croak("Can't find sender sub: %s", SvPVX(graphite->sender_name));
243  
244 7 SvREFCNT_inc_simple_void_NN(graphite->sender_name);
245 }
246 13 }
247  
248 13 inline void apply_use_global_storage_key_ (GraphiteXS_Object* graphite, HV* opts) {
249 13 HE* entry = hv_fetch_ent(opts, sv_use_global_storage_key, NULL, 0);
250 13 ▶ if (entry)
Branch: 1 branch
BranchTrueFalseTotal
if (entry) 1 12 13
251 1 graphite->use_global_storage = SvIVx(hv_iterval(opts, entry)) ? true : false;
252  
253 13 ▶ if (graphite->use_global_storage) {
Branch: 1 branch
BranchTrueFalseTotal
if (graphite->use_global_storage) { 1 12 13
254 1 graphite->bulk_hv = get_hv("Graphite::Simple::bulk", GV_ADD);
255 1 graphite->avg_hv = get_hv("Graphite::Simple::avg_counters", GV_ADD);
256 1 graphite->invalid_hv = get_hv("Graphite::Simple::invalid", GV_ADD);
257 } else {
258 12 graphite->bulk_hv = newHV();
259 12 graphite->avg_hv = newHV();
260 12 graphite->invalid_hv = newHV();
261 }
262 13 }
263  
264 13 inline void apply_block_re_key_ (GraphiteXS_Object* graphite, HV* opts) {
265 13 HE* entry = hv_fetch_ent(opts, sv_block_re_key, NULL, 0);
266 13 ▶ if (entry)
Branch: 1 branch
BranchTrueFalseTotal
if (entry) 3 10 13
267 3 set_blocked_metrics_re_(graphite, hv_iterval(opts, entry));
268 13 }
269  
270 13 inline void apply_enabled_key_ (GraphiteXS_Object* graphite, HV* opts) {
271 13 HE* entry = hv_fetch_ent(opts, sv_enabled_key, NULL, 0);
272 13 ▶ if (entry != NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (entry != NULL) 9 4 13
273 9 graphite->is_enabled = SvNVx(hv_iterval(opts, entry)) ? true : false; // by default: "enabled" => false
274 13 }
275  
276 9 inline void apply_prefix_key_ (GraphiteXS_Object* graphite, HV* opts) {
277 9 STRLEN prefix_len = 0;
278 9 HE* entry = hv_fetch_ent(opts, sv_prefix_key, NULL, 0);
279  
280 9 ▶ if (entry != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (entry != NULL) { 9 0 9
281 9 graphite->global_prefix = move(HeVAL(entry));
282 9 const char *prefix = SvPVX(graphite->global_prefix);
283  
284 9 ▶ if ((prefix_len = SvCUR(graphite->global_prefix)) > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ((prefix_len = SvCUR(graphite->global_prefix)) > 0) { 9 0 9
285 9 ▶ if (prefix[prefix_len - 1] != '.')
Branch: 1 branch
BranchTrueFalseTotal
if (prefix[prefix_len - 1] != '.') 9 0 9
286 9 sv_catpvn(graphite->global_prefix, ".", 1);
287 9 SvREFCNT_inc_simple_void_NN(graphite->global_prefix);
288 }
289 }
290  
291 9 ▶ if (!prefix_len)
Branch: 1 branch
BranchTrueFalseTotal
if (!prefix_len) 0 9 9
292 0 croak("You have to setup project name (global prefix)");
293 9 }
294  
295 9 inline void apply_sock_path_key_ (GraphiteXS_Object* graphite, HV* opts) {
296 9 HE* entry = hv_fetch_ent(opts, sv_sock_path_key, NULL, 0);
297 9 ▶ if (entry != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (entry != NULL) { 4 5 9
298  
299 4 graphite->sock_path = move(HeVAL(entry));
300  
301 4 ▶ if (!sv_len(graphite->sock_path))
Branch: 1 branch
BranchTrueFalseTotal
if (!sv_len(graphite->sock_path)) 0 4 4
302 0 croak("'path' can't be an empty string");
303  
304 4 graphite->sock_addr_unix.sun_family = AF_LOCAL;
305 4 strcpy(graphite->sock_addr_unix.sun_path, SvPVX(graphite->sock_path));
306 }
307 9 }
308  
309 9 inline void apply_host_and_port_keys_ (GraphiteXS_Object* graphite, HV* opts) {
310  
311 // Initially we treat hostname as host, and only in case of failure we treat it as IP
312 9 in_addr_t addr = 0;
313 9 HE* entry = hv_fetch_ent(opts, sv_host_key, NULL, 0);
314  
315 9 ▶ if (graphite->sock_path && entry != NULL)
Branch: 2 branches
BranchTrueFalseTotal
if (graphite->sock_path && entry != NULL) 4 5 9
if (graphite->sock_path && entry != NULL) 0 4 4
316 0 croak("At once can be specified either 'path' or 'host' and 'port'.");
317  
318 9 ▶ if (entry != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (entry != NULL) { 5 4 9
319  
320 struct hostent *host;
321  
322 5 graphite->hostname = move(HeVAL(entry));
323  
324 5 SvREFCNT_inc_simple_void_NN(graphite->hostname);
325  
326 5 char* hostname = SvPVX(graphite->hostname);
327  
328 5 ▶ if ((host = gethostbyname(hostname)) != NULL)
Branch: 1 branch
BranchTrueFalseTotal
if ((host = gethostbyname(hostname)) != NULL) 5 0 5
329 5 addr = *(long *)(host->h_addr_list[0]);
330 0 ▶ else if ( (addr = inet_addr(hostname)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
else if ( (addr = inet_addr(hostname)) < 0) 0 0 0
331 0 croak("Invalid hostname '%s' was given", hostname);
332 }
333  
334 9 ▶ if (graphite->sock_path) // do nothing
Branch: 1 branch
BranchTrueFalseTotal
if (graphite->sock_path) // do nothing 4 5 9
335 4 return(void());
336  
337 5 ▶ if (addr <= 0)
Branch: 1 branch
BranchTrueFalseTotal
if (addr <= 0) 0 5 5
338 0 croak("Neither host nor ip was given");
339  
340 5 ▶ if ((entry = hv_fetch_ent(opts, sv_port_key, NULL, 0)) != NULL)
Branch: 1 branch
BranchTrueFalseTotal
if ((entry = hv_fetch_ent(opts, sv_port_key, NULL, 0)) != NULL) 5 0 5
341 5 graphite->port = (uint32_t) SvIVx(hv_iterval(opts, entry));
342  
343 5 ▶ if (!graphite->port)
Branch: 1 branch
BranchTrueFalseTotal
if (!graphite->port) 0 5 5
344 0 croak("No port number was given");
345  
346 5 graphite->sock_addr_inet.sin_addr.s_addr = addr; //*(long *)(host->h_addr_list[0]);
347 5 graphite->sock_addr_inet.sin_port = htons(graphite->port);
348 5 graphite->sock_addr_inet.sin_family = AF_INET;
349 }
350  
351 9 inline void apply_use_sock_stream_key_ (GraphiteXS_Object* graphite, HV* opts) {
352 9 HE* entry = hv_fetch_ent(opts, sv_use_sock_stream_key, NULL, 0);
353 9 ▶ if (entry)
Branch: 1 branch
BranchTrueFalseTotal
if (entry) 0 9 9
354 0 graphite->is_sock_stream = SvIVx(hv_iterval(opts, entry)) ? true : false;
355 9 }
356  
357 13 inline void apply_constructor_options_ (GraphiteXS_Object* graphite, HV* opts) {
358  
359 13 apply_store_invalid_metrics_key_(graphite, opts);
360 13 apply_sender_key_(graphite, opts);
361 13 apply_use_global_storage_key_(graphite, opts);
362 13 apply_block_re_key_(graphite, opts);
363 13 apply_enabled_key_(graphite, opts);
364  
365 13 ▶ if (graphite->is_enabled) {
Branch: 1 branch
BranchTrueFalseTotal
if (graphite->is_enabled) { 9 4 13
366 9 apply_prefix_key_(graphite, opts);
367 9 apply_sock_path_key_(graphite, opts);
368 9 apply_host_and_port_keys_(graphite, opts);
369 9 apply_use_sock_stream_key_(graphite, opts);
370 }
371 13 }
372  
373 9 inline void connect_ (GraphiteXS_Object* graphite) {
374 9 ▶ if (graphite->is_connected)
Branch: 1 branch
BranchTrueFalseTotal
if (graphite->is_connected) 0 9 9
375 0 return(void());
376  
377 9 ▶ if (graphite->sock_path) {
Branch: 1 branch
BranchTrueFalseTotal
if (graphite->sock_path) { 4 5 9
378 // AF_UNIX -> AF_LOCAL
379 4 ▶ if ((graphite->sock_fd = socket(AF_LOCAL, graphite->is_sock_stream ? SOCK_STREAM : SOCK_DGRAM, 0)) == -1)
Branch: 2 branches
BranchTrueFalseTotal
if ((graphite->sock_fd = socket(AF_LOCAL, graphite->is_sock_stream ? SOCK_STREAM : SOCK_DGRAM, 0)) == -1) 0 4 4
if ((graphite->sock_fd = socket(AF_LOCAL, graphite->is_sock_stream ? SOCK_STREAM : SOCK_DGRAM, 0)) == -1) 0 4 4
380 0 croak("Error: can't create socket: %s\n", strerror(errno));
381  
382 4 ▶ if (fcntl(graphite->sock_fd, F_SETFL, O_CLOEXEC) == -1)
Branch: 1 branch
BranchTrueFalseTotal
if (fcntl(graphite->sock_fd, F_SETFL, O_CLOEXEC) == -1) 0 4 4
383 0 croak("Error: can't set socket flags: %s\n", strerror(errno));
384  
385 4 ▶ if(connect(graphite->sock_fd, (struct sockaddr *) &graphite->sock_addr_unix, sizeof(graphite->sock_addr_unix)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if(connect(graphite->sock_fd, (struct sockaddr *) &graphite->sock_addr_unix, sizeof(graphite->sock_addr_unix)) < 0) 4 0 4
386 4 croak("Error: connection is failed to %s: %s\n", SvPVX(graphite->sock_path), strerror(errno));
387 }
388 5 ▶ else if (graphite->hostname && graphite->port) {
Branch: 2 branches
BranchTrueFalseTotal
else if (graphite->hostname && graphite->port) { 5 0 5
else if (graphite->hostname && graphite->port) { 5 0 5
389 5 ▶ if((graphite->sock_fd = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
Branch: 1 branch
BranchTrueFalseTotal
if((graphite->sock_fd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) 0 5 5
390 0 croak("Error: can't create socket: %s\n", strerror(errno));
391  
392 5 ▶ if (fcntl(graphite->sock_fd, F_SETFL, O_NONBLOCK | O_ASYNC | O_CLOEXEC) == -1)
Branch: 1 branch
BranchTrueFalseTotal
if (fcntl(graphite->sock_fd, F_SETFL, O_NONBLOCK | O_ASYNC | O_CLOEXEC) == -1) 0 5 5
393 0 croak("Error: can't set socket flags: %s\n", strerror(errno));
394  
395 5 ▶ if(connect(graphite->sock_fd, (struct sockaddr *) &graphite->sock_addr_inet, sizeof(graphite->sock_addr_inet)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if(connect(graphite->sock_fd, (struct sockaddr *) &graphite->sock_addr_inet, sizeof(graphite->sock_addr_inet)) < 0) 0 5 5
396 0 croak("Error: connection is failed to %s:%i: %s\n", SvPVX(graphite->hostname), graphite->port, strerror(errno));
397 }
398  
399 5 ▶ if (graphite->sock_fd)
Branch: 1 branch
BranchTrueFalseTotal
if (graphite->sock_fd) 5 0 5
400 5 graphite->is_connected = true;
401 }
402  
403 22 inline void disconnect_ (GraphiteXS_Object* graphite) {
404 22 ▶ if (graphite->is_connected) {
Branch: 1 branch
BranchTrueFalseTotal
if (graphite->is_connected) { 5 17 22
405 5 close(graphite->sock_fd);
406 5 graphite->is_connected = false;
407 5 graphite->sock_fd = 0;
408 }
409 22 }
410  
411  
412 MODULE = Graphite::Simple PACKAGE = Graphite::Simple
413  
414 PROTOTYPES: DISABLE
415  
416 BOOT:
417 {
418 1 ▶ sv_use_sock_stream_key = move(newSVpv("use_sock_stream", strlen("use_sock_stream")));
Branch: 1 branch
BranchTrueFalseTotal
sv_use_sock_stream_key = move(newSVpv("use_sock_stream", strlen("use_sock_stream"))); 1 0 1
419 1 ▶ sv_store_invalid_metrics_key = move(newSVpv("store_invalid_metrics", strlen("store_invalid_metrics")));
Branch: 1 branch
BranchTrueFalseTotal
sv_store_invalid_metrics_key = move(newSVpv("store_invalid_metrics", strlen("store_invalid_metrics"))); 1 0 1
420 1 ▶ sv_use_global_storage_key = move(newSVpv("use_common_storage", strlen("use_global_storage")));
Branch: 1 branch
BranchTrueFalseTotal
sv_use_global_storage_key = move(newSVpv("use_common_storage", strlen("use_global_storage"))); 1 0 1
421 1 ▶ sv_sender_key = move(newSVpv("sender_name", strlen("sender_name")));
Branch: 1 branch
BranchTrueFalseTotal
sv_sender_key = move(newSVpv("sender_name", strlen("sender_name"))); 1 0 1
422 1 ▶ sv_block_re_key = move(newSVpv("block_metrics_re", strlen("block_metrics_re")));
Branch: 1 branch
BranchTrueFalseTotal
sv_block_re_key = move(newSVpv("block_metrics_re", strlen("block_metrics_re"))); 1 0 1
423 1 ▶ sv_sock_path_key = move(newSVpv("path", strlen("path")));
Branch: 1 branch
BranchTrueFalseTotal
sv_sock_path_key = move(newSVpv("path", strlen("path"))); 1 0 1
424 1 ▶ sv_host_key = move(newSVpv("host", strlen("host")));
Branch: 1 branch
BranchTrueFalseTotal
sv_host_key = move(newSVpv("host", strlen("host"))); 1 0 1
425 1 ▶ sv_port_key = move(newSVpv("port", strlen("port")));
Branch: 1 branch
BranchTrueFalseTotal
sv_port_key = move(newSVpv("port", strlen("port"))); 1 0 1
426 1 ▶ sv_enabled_key = move(newSVpv("enabled", strlen("enabled")));
Branch: 1 branch
BranchTrueFalseTotal
sv_enabled_key = move(newSVpv("enabled", strlen("enabled"))); 1 0 1
427 1 ▶ sv_prefix_key = move(newSVpv("project", strlen("project")));
Branch: 1 branch
BranchTrueFalseTotal
sv_prefix_key = move(newSVpv("project", strlen("project"))); 1 0 1
428 1 MAX_CHUNK_SIZE = 1460;
429 }
430  
431 GraphiteXS_Object* new (char* class_name, HV* opts)
432 CODE:
433  
434 13 GraphiteXS_Object *self = (GraphiteXS_Object *) safemalloc(sizeof(GraphiteXS_Object));
435  
436 13 self->invalid_key_counter = 0;
437 13 self->bulk_hv = nullptr;
438 13 self->avg_hv = nullptr;
439 13 self->invalid_hv = nullptr;
440 13 self->block_re = NULL;
441 13 self->is_enabled = false;
442 13 self->is_connected = false;
443 13 self->is_sock_stream = false;
444 13 self->use_global_storage = false;
445 13 self->store_invalid_metrics = false;
446 13 self->global_prefix = nullptr;
447 13 self->sock_path = nullptr;
448 13 self->sender_name = nullptr;
449 13 self->hostname = nullptr;
450 13 self->port = 0;
451 13 self->sock_fd = 0;
452  
453 13 memset(&self->sock_addr_unix, 0, sizeof(sockaddr_un));
454 13 memset(&self->sock_addr_inet, 0, sizeof(sockaddr_in));
455 13 apply_constructor_options_(self, opts);
456  
457 13 RETVAL = self;
458 OUTPUT:
459 RETVAL
460  
461  
462 IV is_connected (GraphiteXS_Object *self)
463 PPCODE:
464 6 ▶ mXPUSHi(self->is_connected ? 1 : 0);
Branch: 2 branches
BranchTrueFalseTotal
mXPUSHi(self->is_connected ? 1 : 0); 0 6 6
mXPUSHi(self->is_connected ? 1 : 0); 2 4 6
465 6 XSRETURN(1);
466  
467  
468 IV connect (GraphiteXS_Object *self)
469 PPCODE:
470 7 connect_(self);
471 3 ▶ mXPUSHi(self->is_connected ? 1 : 0);
Branch: 2 branches
BranchTrueFalseTotal
mXPUSHi(self->is_connected ? 1 : 0); 0 3 3
mXPUSHi(self->is_connected ? 1 : 0); 3 0 3
472 3 XSRETURN(1);
473  
474  
475 IV reconnect (GraphiteXS_Object *self)
476 PPCODE:
477 2 disconnect_(self);
478 2 connect_(self);
479 2 ▶ mXPUSHi(self->is_connected ? 1 : 0);
Branch: 2 branches
BranchTrueFalseTotal
mXPUSHi(self->is_connected ? 1 : 0); 0 2 2
mXPUSHi(self->is_connected ? 1 : 0); 2 0 2
480 2 XSRETURN(1);
481  
482  
483 void disconnect (GraphiteXS_Object *self)
484 PPCODE:
485 7 disconnect_(self);
486 7 XSRETURN_EMPTY;
487  
488  
489 HV* get_bulk_metrics (GraphiteXS_Object *self)
490 CODE:
491 //ST(0) = sv_mortalcopy((SV *) self->bulk_hv); // mXPUSHs((SV *) self->bulk_hv );
492 //ST(0) = sv_2mortal(newRV_noinc( (SV *) self->bulk_hv ));
493 //XSRETURN(1);
494 14 RETVAL = self->bulk_hv;
495 OUTPUT:
496 RETVAL
497  
498  
499 HV* get_invalid_metrics (GraphiteXS_Object *self)
500 CODE:
501 3 RETVAL = self->invalid_hv;
502 OUTPUT:
503 RETVAL
504  
505  
506 SV* send_bulk (GraphiteXS_Object* self)
507 PPCODE:
508  
509 3 IV is_success = 1;
510  
511 3 ▶ if (self->is_connected) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->is_connected) { 3 0 3
512  
513 3 ▶ SSize_t keys_cnt = hv_iterinit(self->bulk_hv);
Branch: 1 branch
BranchTrueFalseTotal
SSize_t keys_cnt = hv_iterinit(self->bulk_hv); 3 0 3
514  
515 3 ▶ if (keys_cnt) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys_cnt) { 3 0 3
516  
517 // writes results into self->bulk_hv
518 3 ▶ calculate_result_metrics_(self);
Branch: 1 branch
BranchTrueFalseTotal
calculate_result_metrics_(self); 3 0 3
519  
520 3 STRLEN len, key_len = 0;
521 3 const char *prefix = SvPVX(self->global_prefix);
522 3 HE *entry = NULL;
523  
524 static string_view suffix;
525 5 ▶ static string data ("");
Branch: 4 branches
BranchTrueFalseTotal
static string data (""); 1 2 3
static string data (""); 1 0 1
static string data (""); 1 0 1
static string data (""); 0 0 0
526 static bool is_first_time = true;
527 static int send_flags = MSG_NOSIGNAL; // we don't set MSG_DONTWAIT flag because O_NONBLOCK is already set via fcntl
528  
529 3 ▶ if (is_first_time) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_first_time) { 1 2 3
530 1 ▶ data.reserve(MAX_CHUNK_SIZE + 100);
Branch: 1 branch
BranchTrueFalseTotal
data.reserve(MAX_CHUNK_SIZE + 100); 1 0 1
531 1 is_first_time = false;
532 }
533  
534 3 ▶ suffix = move( string_view(move( " " + to_string(time(NULL)).append("\n") )) );
Branch: 3 branches
BranchTrueFalseTotal
suffix = move( string_view(move( " " + to_string(time(NULL)).append("\n") )) ); 3 0 3
suffix = move( string_view(move( " " + to_string(time(NULL)).append("\n") )) ); 3 0 3
suffix = move( string_view(move( " " + to_string(time(NULL)).append("\n") )) ); 3 0 3
535  
536 15 ▶ while (entry = hv_iternext(self->bulk_hv)) {
Branch: 2 branches
BranchTrueFalseTotal
while (entry = hv_iternext(self->bulk_hv)) { 12 0 12
while (entry = hv_iternext(self->bulk_hv)) { 9 3 12
537  
538 9 ▶ data.append(prefix);
Branch: 1 branch
BranchTrueFalseTotal
data.append(prefix); 9 0 9
539 9 ▶ data.append(move(HePV(entry, key_len)));
Branch: 2 branches
BranchTrueFalseTotal
data.append(move(HePV(entry, key_len))); 0 9 9
data.append(move(HePV(entry, key_len))); 9 0 9
540 9 ▶ data.append(" ");
Branch: 1 branch
BranchTrueFalseTotal
data.append(" "); 9 0 9
541 18 ▶ data.append(move(SvPV_nolen(HeVAL(entry))));
Branch: 1 branch
BranchTrueFalseTotal
data.append(move(SvPV_nolen(HeVAL(entry)))); 9 0 9
542 9 ▶ data.append(suffix);
Branch: 1 branch
BranchTrueFalseTotal
data.append(suffix); 9 0 9
543  
544 9 ▶ if (( len = data.length() ) >= MAX_CHUNK_SIZE) {
Branch: 1 branch
BranchTrueFalseTotal
if (( len = data.length() ) >= MAX_CHUNK_SIZE) { 0 9 9
545 //warn("data:\n%s", data.c_str());
546 0 ▶ if (send(self->sock_fd, data.c_str(), move(len), send_flags) == -1) {
Branch: 2 branches
BranchTrueFalseTotal
if (send(self->sock_fd, data.c_str(), move(len), send_flags) == -1) { 0 0 0
if (send(self->sock_fd, data.c_str(), move(len), send_flags) == -1) { 0 0 0
547 0 ▶ if (self->sock_path) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->sock_path) { 0 0 0
548 0 ▶ disconnect_(self);
Branch: 1 branch
BranchTrueFalseTotal
disconnect_(self); 0 0 0
549 0 data.clear();
550 0 croak("Error: can't send. %s\n", strerror(errno));
551 }
552 0 is_success = 0;
553 }
554 0 data.clear();
555 }
556 }
557  
558 3 ▶ if ((len = data.length()) > 0) { // if we have only one key, then "len" iz zero here
Branch: 1 branch
BranchTrueFalseTotal
if ((len = data.length()) > 0) { // if we have only one key, then "len" iz zero here 3 0 3
559 //warn("data:\n%s", data.c_str());
560 3 ▶ if (send(self->sock_fd, data.c_str(), move(len), send_flags) == -1) {
Branch: 2 branches
BranchTrueFalseTotal
if (send(self->sock_fd, data.c_str(), move(len), send_flags) == -1) { 3 0 3
if (send(self->sock_fd, data.c_str(), move(len), send_flags) == -1) { 0 3 3
561 0 ▶ if (self->sock_path) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->sock_path) { 0 0 0
562 0 ▶ disconnect_(self);
Branch: 1 branch
BranchTrueFalseTotal
disconnect_(self); 0 0 0
563 0 data.clear();
564 0 croak("Error: can't send. %s\n", strerror(errno));
565 }
566 0 is_success = 0;
567 }
568 3 data.clear();
569 }
570 }
571 }
572 else
573 0 ▶ warn("Client is not connected to server");
Branch: 1 branch
BranchTrueFalseTotal
warn("Client is not connected to server"); 0 0 0
574  
575 3 ▶ clear_bulk_(self);
Branch: 1 branch
BranchTrueFalseTotal
clear_bulk_(self); 3 0 3
576 3 ▶ mXPUSHi(move(is_success));
Branch: 4 branches
BranchTrueFalseTotal
mXPUSHi(move(is_success)); 0 3 3
mXPUSHi(move(is_success)); 0 0 0
mXPUSHi(move(is_success)); 3 0 3
mXPUSHi(move(is_success)); 3 0 3
577 3 XSRETURN(1);
578  
579  
580 SV* send_bulk_delegate (GraphiteXS_Object *self)
581 PPCODE:
582  
583 7 const char *sender = SvPVX(self->sender_name);
584  
585 7 ▶ if (!sender)
Branch: 1 branch
BranchTrueFalseTotal
if (!sender) 0 7 7
586 0 croak("No sender was specified");
587  
588 7 calculate_result_metrics_(self);
589  
590 // see "man perlcall" for details
591  
592 7 IV status = 0;
593  
594 7 dSP; // Declares a local copy of perl's stack pointer for the XSUB, available via the "SP" macro. See "SP".
595 I32 ax;
596 I32 count;
597  
598 7 ENTER; // Opening bracket on a callback. See "LEAVE" and perlcall.
599 7 SAVETMPS; // pening bracket for temporaries on a callback. See "FREETMPS" and perlcall.
600 7 ▶ PUSHMARK(SP); // Opening bracket for arguments on a callback. See "PUTBACK" and perlcall.
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); // Opening bracket for arguments on a callback. See "PUTBACK" and perlcall. 0 7 7
601 7 ▶ EXTEND(SP, 1); // We are going to pass only one argument
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 1); // We are going to pass only one argument 0 7 7
602 7 PUSHs(sv_2mortal(newRV( (SV *) self->bulk_hv ))); // Push into outgoing stack an argument
603 7 PUTBACK; // Closing bracket for XSUB arguments. This is usually handled by "xsubpp". See "PUSHMARK" and perlcall for other uses.
604  
605 7 count = call_pv(sender, G_SCALAR);
606  
607 7 SPAGAIN; // Refetch the stack pointer. Used after a callback. See perlcall.
608 7 SP -= count;
609 7 ax = (SP - PL_stack_base) + 1;
610  
611 7 ▶ if (count != 1)
Branch: 1 branch
BranchTrueFalseTotal
if (count != 1) 0 7 7
612 0 croak("Unexpected size of returned stack from sender\n");
613  
614 7 uint32_t type = SvTYPE(ST(0));
615  
616 7 ▶ if (SVt_IV == type || SVt_NV == type)
Branch: 2 branches
BranchTrueFalseTotal
if (SVt_IV == type || SVt_NV == type) 4 3 7
if (SVt_IV == type || SVt_NV == type) 0 4 4
617 3 status = SvIV(ST(0)); // round the value
618 else
619 4 warn("The sender must return a number type of status. Using 0 as sender status now.");
620  
621 7 PUTBACK; // Closing bracket for XSUB arguments. This is usually handled by "xsubpp". See "PUSHMARK" and perlcall for other uses.
622 7 ▶ FREETMPS; // Closing bracket for temporaries on a callback. See "SAVETMPS" and perlcall.
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; // Closing bracket for temporaries on a callback. See "SAVETMPS" and perlcall. 7 0 7
623 7 LEAVE; // Closing bracket on a callback. See "ENTER" and perlcall.
624  
625 // warn("status: %d\n", status);
626 7 clear_bulk_(self);
627 7 ▶ mXPUSHi(status);
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHi(status); 0 7 7
628 7 XSRETURN(1);
629  
630  
631 HV* get_metrics (GraphiteXS_Object *self)
632 CODE:
633 3 ▶ HV* res_hv = (HV *) sv_2mortal((SV*) newHV());
Branch: 2 branches
BranchTrueFalseTotal
HV* res_hv = (HV *) sv_2mortal((SV*) newHV()); 3 0 3
HV* res_hv = (HV *) sv_2mortal((SV*) newHV()); 3 0 3
634 3 ▶ calculate_result_metrics_(self, res_hv);
Branch: 1 branch
BranchTrueFalseTotal
calculate_result_metrics_(self, res_hv); 3 0 3
635 3 ▶ RETVAL = move(res_hv);
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = move(res_hv); 3 0 3
636 OUTPUT:
637 RETVAL
638  
639  
640 HV* get_average_counters (GraphiteXS_Object *self)
641 CODE:
642 9 RETVAL = self->avg_hv;
643 OUTPUT:
644 RETVAL
645  
646  
647 void clear_bulk (GraphiteXS_Object *self)
648 PPCODE:
649 14 clear_bulk_(self);
650 14 XSRETURN_EMPTY;
651  
652  
653 void incr_bulk (GraphiteXS_Object *self, SV* key, NV value = 1)
654 PPCODE:
655 27 bool is_ok = false;
656 27 ▶ if (key != &PL_sv_undef) {
Branch: 1 branch
BranchTrueFalseTotal
if (key != &PL_sv_undef) { 27 0 27
657 27 uint32_t key_type = SvTYPE(key);
658 27 ▶ if (key_type == SVt_PVLV || key_type == SVt_PVMG) {
Branch: 2 branches
BranchTrueFalseTotal
if (key_type == SVt_PVLV || key_type == SVt_PVMG) { 27 0 27
if (key_type == SVt_PVLV || key_type == SVt_PVMG) { 0 27 27
659 // SVt_PVLV can be returned from substr
660 // SVt_PVMG can be returned from RegExp
661 STRLEN key_len;
662 0 char *ch = SvPVx(move(key), key_len);
663 0 ▶ key = newSVpv(move(ch), move(key_len));
Branch: 1 branch
BranchTrueFalseTotal
key = newSVpv(move(ch), move(key_len)); 0 0 0
664 }
665 27 ▶ if (SvTYPE(key) == SVt_PV) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvTYPE(key) == SVt_PV) { 26 1 27
666 26 is_ok = true;
667 26 ▶ increment_metric_( self, move(key), move(value) );
Branch: 1 branch
BranchTrueFalseTotal
increment_metric_( self, move(key), move(value) ); 26 0 26
668 }
669 }
670  
671 27 ▶ if (!is_ok)
Branch: 1 branch
BranchTrueFalseTotal
if (!is_ok) 1 26 27
672 1 croak("key must be a string");
673  
674 26 XSRETURN_EMPTY;
675  
676  
677 void append_bulk (GraphiteXS_Object *self, HV* hv, SV* prefix = &PL_sv_undef)
678 PPCODE:
679  
680 15 ▶ if (SvTIED_mg((SV *) hv, PERL_MAGIC_tied))
Branch: 3 branches
BranchTrueFalseTotal
if (SvTIED_mg((SV *) hv, PERL_MAGIC_tied)) 0 15 15
if (SvTIED_mg((SV *) hv, PERL_MAGIC_tied)) 0 0 0
if (SvTIED_mg((SV *) hv, PERL_MAGIC_tied)) 0 15 15
681 0 croak("Tied hashes are not supported");
682  
683 15 bool use_prefix = false;
684  
685 15 ▶ if (prefix && prefix != &PL_sv_undef) {
Branch: 2 branches
BranchTrueFalseTotal
if (prefix && prefix != &PL_sv_undef) { 15 0 15
if (prefix && prefix != &PL_sv_undef) { 2 13 15
686  
687 2 uint32_t type = SvTYPE(prefix);
688  
689 2 ▶ use_prefix = type == SVt_PV || type == SVt_PVLV;
Branch: 2 branches
BranchTrueFalseTotal
use_prefix = type == SVt_PV || type == SVt_PVLV; 0 2 2
use_prefix = type == SVt_PV || type == SVt_PVLV; 0 0 0
690  
691 2 ▶ if (!use_prefix && type != SVt_NULL)
Branch: 2 branches
BranchTrueFalseTotal
if (!use_prefix && type != SVt_NULL) 0 2 2
if (!use_prefix && type != SVt_NULL) 0 0 0
692 0 croak("prefix must be a string or undefined");
693 }
694  
695 15 HE* entry = NULL;
696 17 ▶ static string sprefix ('\0', 128); // preallocate 128 byte
Branch: 4 branches
BranchTrueFalseTotal
static string sprefix ('\0', 128); // preallocate 128 byte 1 14 15
static string sprefix ('\0', 128); // preallocate 128 byte 1 0 1
static string sprefix ('\0', 128); // preallocate 128 byte 1 0 1
static string sprefix ('\0', 128); // preallocate 128 byte 0 0 0
697  
698 15 ▶ if (use_prefix) {
Branch: 1 branch
BranchTrueFalseTotal
if (use_prefix) { 2 13 15
699 2 ▶ sprefix.assign( SvPVX(prefix) );
Branch: 1 branch
BranchTrueFalseTotal
sprefix.assign( SvPVX(prefix) ); 2 0 2
700 2 ▶ if (sprefix.back() != '.')
Branch: 1 branch
BranchTrueFalseTotal
if (sprefix.back() != '.') 1 1 2
701 1 ▶ sprefix.append(".");
Branch: 1 branch
BranchTrueFalseTotal
sprefix.append("."); 1 0 1
702 }
703  
704 15 ▶ hv_iterinit(hv);
Branch: 1 branch
BranchTrueFalseTotal
hv_iterinit(hv); 15 0 15
705  
706 60 ▶ while (entry = hv_iternext(hv)) {
Branch: 2 branches
BranchTrueFalseTotal
while (entry = hv_iternext(hv)) { 60 0 60
while (entry = hv_iternext(hv)) { 45 15 60
707  
708 45 ▶ SV* key = hv_iterkeysv(entry);
Branch: 1 branch
BranchTrueFalseTotal
SV* key = hv_iterkeysv(entry); 45 0 45
709 45 ▶ SV* value = hv_iterval(hv, move(entry));
Branch: 1 branch
BranchTrueFalseTotal
SV* value = hv_iterval(hv, move(entry)); 45 0 45
710 45 uint32_t value_type = SvTYPE(value);
711  
712 48 if (
713 45 ▶ SvROK(value) ||
Branch: 2 branches
BranchTrueFalseTotal
SvROK(value) || 43 2 45
SvROK(value) || 2 41 43
714 (
715 // value_type < SVt_IV || value_type > SVt_PVNV
716 2 ▶ SVt_IV != value_type &&
Branch: 1 branch
BranchTrueFalseTotal
SVt_IV != value_type && 2 0 2
717 2 ▶ SVt_NV != value_type &&
Branch: 1 branch
BranchTrueFalseTotal
SVt_NV != value_type && 1 1 2
718 1 ▶ SVt_PV != value_type &&
Branch: 1 branch
BranchTrueFalseTotal
SVt_PV != value_type && 1 0 1
719 1 ▶ SVt_PVNV != value_type &&
Branch: 1 branch
BranchTrueFalseTotal
SVt_PVNV != value_type && 1 0 1
720 SVt_PVIV != value_type
721 )
722 ) {
723 3 ▶ warn("Value type of key '%s' is not a number", SvPVX(key));
Branch: 1 branch
BranchTrueFalseTotal
warn("Value type of key '%s' is not a number", SvPVX(key)); 3 0 3
724 3 continue;
725 }
726  
727 42 ▶ if (use_prefix)
Branch: 1 branch
BranchTrueFalseTotal
if (use_prefix) 6 36 42
728 6 ▶ sv_setpv(key, ( sprefix + SvPVX(key) ).c_str());
Branch: 2 branches
BranchTrueFalseTotal
sv_setpv(key, ( sprefix + SvPVX(key) ).c_str()); 6 0 6
sv_setpv(key, ( sprefix + SvPVX(key) ).c_str()); 6 0 6
729  
730 42 ▶ increment_metric_( self, move(key), move(SvNVx(value)) );
Branch: 2 branches
BranchTrueFalseTotal
increment_metric_( self, move(key), move(SvNVx(value)) ); 42 0 42
increment_metric_( self, move(key), move(SvNVx(value)) ); 42 0 42
731 }
732  
733 15 XSRETURN_EMPTY;
734  
735  
736 SV* is_valid_key (GraphiteXS_Object *self, SV* key)
737 PPCODE:
738 10 ▶ bool is_valid = is_valid_key_(self, move(key));
Branch: 1 branch
BranchTrueFalseTotal
bool is_valid = is_valid_key_(self, move(key)); 10 0 10
739 10 ▶ mXPUSHi( move(is_valid ? 1 : 0) );
Branch: 5 branches
BranchTrueFalseTotal
mXPUSHi( move(is_valid ? 1 : 0) ); 0 10 10
mXPUSHi( move(is_valid ? 1 : 0) ); 0 0 0
mXPUSHi( move(is_valid ? 1 : 0) ); 3 7 10
mXPUSHi( move(is_valid ? 1 : 0) ); 10 0 10
mXPUSHi( move(is_valid ? 1 : 0) ); 10 0 10
740 10 XSRETURN(1);
741  
742  
743 IV get_invalid_key_counter (GraphiteXS_Object *self)
744 PPCODE:
745 5 ▶ mXPUSHi( self->invalid_key_counter );
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHi( self->invalid_key_counter ); 0 5 5
746 5 XSRETURN(1);
747  
748  
749 void check_and_bump_invalid_metric (GraphiteXS_Object *self, SV* key)
750 PPCODE:
751 3 ▶ if ( SVt_NULL != SvTYPE(key) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( SVt_NULL != SvTYPE(key) ) { 3 0 3
752 3 uint32_t counter = move(self->invalid_key_counter);
753 3 self->invalid_key_counter = 0;
754 3 ▶ increment_hash_value_by( self->bulk_hv, move(key), move(counter) );
Branch: 1 branch
BranchTrueFalseTotal
increment_hash_value_by( self->bulk_hv, move(key), move(counter) ); 3 0 3
755 }
756 3 XSRETURN_EMPTY;
757  
758  
759 IV is_metric_blocked (GraphiteXS_Object *self, SV* key)
760 PPCODE:
761 0 bool is_blocked = false;
762 0 ▶ if ( is_valid_key_(self, key) )
Branch: 2 branches
BranchTrueFalseTotal
if ( is_valid_key_(self, key) ) 0 0 0
if ( is_valid_key_(self, key) ) 0 0 0
763 0 ▶ is_blocked = is_metric_blocked_(self, move(key));
Branch: 1 branch
BranchTrueFalseTotal
is_blocked = is_metric_blocked_(self, move(key)); 0 0 0
764 0 ▶ mXPUSHi( move(is_blocked ? 1 : 0) );
Branch: 5 branches
BranchTrueFalseTotal
mXPUSHi( move(is_blocked ? 1 : 0) ); 0 0 0
mXPUSHi( move(is_blocked ? 1 : 0) ); 0 0 0
mXPUSHi( move(is_blocked ? 1 : 0) ); 0 0 0
mXPUSHi( move(is_blocked ? 1 : 0) ); 0 0 0
mXPUSHi( move(is_blocked ? 1 : 0) ); 0 0 0
765 0 XSRETURN(1);
766  
767  
768 void set_blocked_metrics_re (GraphiteXS_Object *self, SV* block_re = &PL_sv_undef)
769 PPCODE:
770 7 set_blocked_metrics_re_(self, block_re);
771 7 XSRETURN_EMPTY;
772  
773  
774 void DESTROY (...)
775 PPCODE:
776 13 ▶ GraphiteXS_Object *self = (GraphiteXS_Object *) SvUV(SvRV(ST(0)));
Branch: 1 branch
BranchTrueFalseTotal
GraphiteXS_Object *self = (GraphiteXS_Object *) SvUV(SvRV(ST(0))); 13 0 13
777 13 ▶ if (PL_dirty) // global destruction
Branch: 1 branch
BranchTrueFalseTotal
if (PL_dirty) // global destruction 0 13 13
778 0 return;
779 13 ▶ if (self->sender_name && SvREFCNT(self->sender_name)) // sv_clear
Branch: 2 branches
BranchTrueFalseTotal
if (self->sender_name && SvREFCNT(self->sender_name)) // sv_clear 7 6 13
if (self->sender_name && SvREFCNT(self->sender_name)) // sv_clear 7 0 7
780 7 ▶ SvREFCNT_dec_NN(self->sender_name);
Branch: 1 branch
BranchTrueFalseTotal
SvREFCNT_dec_NN(self->sender_name); 7 0 7
781 13 ▶ if (self->global_prefix && SvREFCNT(self->global_prefix)) // sv_clear
Branch: 2 branches
BranchTrueFalseTotal
if (self->global_prefix && SvREFCNT(self->global_prefix)) // sv_clear 9 4 13
if (self->global_prefix && SvREFCNT(self->global_prefix)) // sv_clear 9 0 9
782 9 ▶ SvREFCNT_dec_NN(self->global_prefix);
Branch: 1 branch
BranchTrueFalseTotal
SvREFCNT_dec_NN(self->global_prefix); 9 0 9
783 13 ▶ if (self->hostname && SvREFCNT(self->hostname)) // sv_clear
Branch: 2 branches
BranchTrueFalseTotal
if (self->hostname && SvREFCNT(self->hostname)) // sv_clear 5 8 13
if (self->hostname && SvREFCNT(self->hostname)) // sv_clear 5 0 5
784 5 ▶ SvREFCNT_dec_NN(self->hostname);
Branch: 1 branch
BranchTrueFalseTotal
SvREFCNT_dec_NN(self->hostname); 5 0 5
785 13 ▶ if (self->sock_path && SvREFCNT(self->sock_path)) // sv_clear
Branch: 2 branches
BranchTrueFalseTotal
if (self->sock_path && SvREFCNT(self->sock_path)) // sv_clear 4 9 13
if (self->sock_path && SvREFCNT(self->sock_path)) // sv_clear 0 4 4
786 0 ▶ SvREFCNT_dec_NN(self->sock_path);
Branch: 1 branch
BranchTrueFalseTotal
SvREFCNT_dec_NN(self->sock_path); 0 0 0
787 13 ▶ if (self->block_re && SvREFCNT(self->block_re))
Branch: 2 branches
BranchTrueFalseTotal
if (self->block_re && SvREFCNT(self->block_re)) 0 13 13
if (self->block_re && SvREFCNT(self->block_re)) 0 0 0
788 0 ▶ SvREFCNT_dec_NN(self->block_re);
Branch: 1 branch
BranchTrueFalseTotal
SvREFCNT_dec_NN(self->block_re); 0 0 0
789 13 ▶ if (!self->use_global_storage) {
Branch: 1 branch
BranchTrueFalseTotal
if (!self->use_global_storage) { 12 1 13
790 12 ▶ if (self->bulk_hv) // hv_undef
Branch: 1 branch
BranchTrueFalseTotal
if (self->bulk_hv) // hv_undef 12 0 12
791 12 ▶ SvREFCNT_dec_NN(self->bulk_hv);
Branch: 1 branch
BranchTrueFalseTotal
SvREFCNT_dec_NN(self->bulk_hv); 12 0 12
792 12 ▶ if (self->avg_hv) // hv_undef
Branch: 1 branch
BranchTrueFalseTotal
if (self->avg_hv) // hv_undef 12 0 12
793 12 ▶ SvREFCNT_dec_NN(self->avg_hv);
Branch: 1 branch
BranchTrueFalseTotal
SvREFCNT_dec_NN(self->avg_hv); 12 0 12
794 12 ▶ if (self->invalid_hv) // hv_undef
Branch: 1 branch
BranchTrueFalseTotal
if (self->invalid_hv) // hv_undef 12 0 12
795 12 ▶ SvREFCNT_dec_NN(self->invalid_hv);
Branch: 1 branch
BranchTrueFalseTotal
SvREFCNT_dec_NN(self->invalid_hv); 12 0 12
796 }
797 13 ▶ disconnect_(self);
Branch: 1 branch
BranchTrueFalseTotal
disconnect_(self); 13 0 13
798 //dump_all();
799 13 ▶ safefree(move(self));
Branch: 1 branch
BranchTrueFalseTotal
safefree(move(self)); 13 0 13
800 13 XSRETURN_EMPTY;
↑ Summary blib/lib/Graphite/Simple.pm »