mwrap-perl.git  about / heads / tags
LD_PRELOAD malloc wrapper + line stats for Perl
blob e51259ebe20f1cd23afce8eb98f0f8efab86cbd8 5319 bytes (raw)
$ git show HEAD:Mwrap.xs	# shows this blob on the CLI

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
 
/*
 * Copyright (C) mwrap hackers <mwrap-perl@80x24.org>
 * License: GPL-3.0+ <https://www.gnu.org/licenses/gpl-3.0.txt>
 * Disclaimer: I don't really know my way around XS or Perl internals well
 */
#define MWRAP_PERL 1
#include "mwrap_core.h"

/*
 * The Devel::Mwrap Perl API is probably no longer useful now that
 * the AF_UNIX HTTP interface exists.  It'll probably remain undocumented.
 */

/*
 * I hate typedefs, especially when they're hiding the fact that there's
 * a pointer, but XS needs this, apparently, and it does s/__/::/g
 */
typedef struct src_loc * Devel__Mwrap__SrcLoc;

/* keep this consistent with httpd.h write_loc_name */
static SV *location_string(struct src_loc *l)
{
	SV *ret = newSVpvs("");

	if (l->f) {
		sv_catpv(ret, l->f->fn);
		if (l->lineno == U24_MAX)
			sv_catpvs(ret, ":-");
		else
			sv_catpvf(ret, ":%u", l->lineno);
	}
	if (l->bt_len) {
		AUTO_FREE char **s = bt_syms(l->bt, l->bt_len);
		if (s) {
			if (l->f)
				sv_catpvs(ret, "\n");
			sv_catpv(ret, s[0]);
			for (uint32_t i = 1; i < l->bt_len; ++i)
				sv_catpvf(ret, "\n%s", s[i]);
		}
	}
	return ret;
}

MODULE = Devel::Mwrap	PACKAGE = Devel::Mwrap	PREFIX = mwrap_

BOOT:
#ifndef PERL_IMPLICIT_CONTEXT
	root_locating = &locating;
#endif

PROTOTYPES: ENABLE

size_t
mwrap_quiet(int on_off)
CODE:
	RETVAL = on_off ? locating++ : locating--;
OUTPUT:
	RETVAL

size_t
mwrap_current_age()
CODE:
	RETVAL = uatomic_read(&total_bytes_inc);
OUTPUT:
	RETVAL

size_t
mwrap_total_bytes_allocated()
CODE:
	RETVAL = uatomic_read(&total_bytes_inc);
OUTPUT:
	RETVAL

size_t
mwrap_total_bytes_freed()
CODE:
	RETVAL = uatomic_read(&total_bytes_dec);
OUTPUT:
	RETVAL

void
mwrap_each(min, cb, arg = &PL_sv_undef)
	size_t min;
	SV *cb;
	SV *arg;
PREINIT:
	struct cds_lfht *t;
	struct cds_lfht_iter iter;
	struct src_loc *l;
CODE:
	++locating;
	rcu_read_lock();
	t = CMM_LOAD_SHARED(totals);
	if (t) {
		bool err = false;

		cds_lfht_for_each_entry(t, &iter, l, hnode) {
			size_t total = uatomic_read(&l->total);

			if (total > min) {
				SV *loc;
				dSP;
				ENTER;
				SAVETMPS;

				PUSHMARK(SP);
				loc = sv_newmortal();
				sv_setref_pv(loc, "Devel::Mwrap::SrcLoc", l);
				XPUSHs(arg);
				XPUSHs(loc);
				PUTBACK;

				call_sv(cb, G_DISCARD|G_EVAL);

				SPAGAIN;
				if (SvTRUE(ERRSV))
					err = true;

				FREETMPS;
				LEAVE;
			}
			if (err)
				break;
			mwrap_assert(rcu_read_ongoing());
		}
	}
	if (SvTRUE(ERRSV))
		croak(NULL);
CLEANUP:
	rcu_read_unlock();
	--locating;


void
mwrap_reset()
CODE:
	mwrap_reset();

unsigned
mwrap_bt_depth(arg = &PL_sv_undef)
	SV *arg;
CODE:
	if (SvOK(arg)) {
		UV n = SvUVx(arg);
		if (n > MWRAP_BT_MAX)
			n = MWRAP_BT_MAX;
		CMM_STORE_SHARED(bt_req_depth, (uint32_t)n);
		RETVAL = n;
	} else {
		RETVAL = CMM_LOAD_SHARED(bt_req_depth);
	}
OUTPUT:
	RETVAL

Devel::Mwrap::SrcLoc
mwrap_get(loc)
	SV *loc;
PREINIT:
	STRLEN len;
	const char *str;
	struct src_loc *l;
CODE:
	++locating;
	if (!SvPOK(loc))
		XSRETURN_UNDEF;
	str = SvPV(loc, len);
	l = mwrap_get(str, len);
	if (!l)
		XSRETURN_UNDEF;
	RETVAL = l;
OUTPUT:
	RETVAL
CLEANUP:
	--locating;

MODULE = Devel::Mwrap	PACKAGE = Devel::Mwrap::SrcLoc	PREFIX = src_loc_

PROTOTYPES: ENABLE

size_t
src_loc_frees(self)
	Devel::Mwrap::SrcLoc self
PREINIT:
CODE:
	++locating;
	RETVAL = uatomic_read(&self->frees);
OUTPUT:
	RETVAL
CLEANUP:
	--locating;

size_t
src_loc_freed_bytes(self)
	Devel::Mwrap::SrcLoc self
PREINIT:
CODE:
	++locating;
	RETVAL = uatomic_read(&self->freed_bytes);
OUTPUT:
	RETVAL
CLEANUP:
	--locating;

size_t
src_loc_allocations(self)
	Devel::Mwrap::SrcLoc self
PREINIT:
CODE:
	++locating;
	RETVAL = uatomic_read(&self->allocations);
OUTPUT:
	RETVAL
CLEANUP:
	--locating;

size_t
src_loc_total(self)
	Devel::Mwrap::SrcLoc self
PREINIT:
CODE:
	++locating;
	RETVAL = uatomic_read(&self->total);
OUTPUT:
	RETVAL
CLEANUP:
	--locating;

double
src_loc_mean_lifespan(self)
	Devel::Mwrap::SrcLoc self
PREINIT:
	size_t tot, frees;
CODE:
	++locating;
	frees = uatomic_read(&self->frees);
	tot = uatomic_read(&self->age_total);
	RETVAL = frees ? ((double)tot/(double)frees) : HUGE_VAL;
OUTPUT:
	RETVAL
CLEANUP:
	--locating;

double
src_loc_max_lifespan(self)
	Devel::Mwrap::SrcLoc self
CODE:
	++locating;
	RETVAL = uatomic_read(&self->max_lifespan);
OUTPUT:
	RETVAL
CLEANUP:
	--locating;

SV *
src_loc_name(self)
	Devel::Mwrap::SrcLoc self
CODE:
	++locating;
	RETVAL = location_string(self);
OUTPUT:
	RETVAL
CLEANUP:
	--locating;

void
src_loc_each(self, min, cb, arg = &PL_sv_undef)
	Devel::Mwrap::SrcLoc self;
	size_t min;
	SV *cb;
	SV *arg;
PREINIT:
	struct alloc_hdr *h;
	bool err = false;
CODE:
	++locating;
	rcu_read_lock();
	cds_list_for_each_entry_rcu(h, &self->allocs, anode) {
		size_t size = uatomic_read(&h->size);
		if (size > min) {
			dSP;
			ENTER;
			SAVETMPS;

			PUSHMARK(SP);

			/*
			 * note: we MUST NOT expose alloc_hdr to Perl code
			 * since that opens us up to use-after-free
			 */
			XPUSHs(arg);
			XPUSHs(sv_2mortal(newSVuv(size)));
			XPUSHs(sv_2mortal(newSVuv(h->as.live.gen)));
			XPUSHs(sv_2mortal(newSVuv((uintptr_t)h->real)));
			PUTBACK;

			call_sv(cb, G_DISCARD|G_EVAL);

			SPAGAIN;
			if (SvTRUE(ERRSV))
				err = true;

			FREETMPS;
			LEAVE;
			if (err)
				break;
			mwrap_assert(rcu_read_ongoing());
		}
	}
	if (SvTRUE(ERRSV))
		croak(NULL);
CLEANUP:
	rcu_read_unlock();
	--locating;

git clone https://80x24.org/mwrap-perl.git