zsh-workers
 help / color / mirror / code / Atom feed
2d8ed4f684cc433b4b9efe522c27186fbfa56870 blob 8989 bytes (raw)

  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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
 
/*
 * zprof.c - a shell function profiling module for zsh
 *
 * This file is part of zsh, the Z shell.
 *
 * Copyright (c) 1996-1997 Sven Wischnowsky
 * All rights reserved.
 *
 * Permission is hereby granted, without written agreement and without
 * license or royalty fees, to use, copy, modify, and distribute this
 * software and to distribute modified versions of this software for any
 * purpose, provided that the above copyright notice and the following
 * two paragraphs appear in all copies of this software.
 *
 * In no event shall Sven Wischnowsky or the Zsh Development Group be liable
 * to any party for direct, indirect, special, incidental, or consequential
 * damages arising out of the use of this software and its documentation,
 * even if Sven Wischnowsky and the Zsh Development Group have been advised of
 * the possibility of such damage.
 *
 * Sven Wischnowsky and the Zsh Development Group specifically disclaim any
 * warranties, including, but not limited to, the implied warranties of
 * merchantability and fitness for a particular purpose.  The software
 * provided hereunder is on an "as is" basis, and Sven Wischnowsky and the
 * Zsh Development Group have no obligation to provide maintenance,
 * support, updates, enhancements, or modifications.
 *
 */

#include "zprof.mdh"
#include "zprof.pro"

#include <sys/time.h>
#include <unistd.h>

typedef struct pfunc *Pfunc;

struct pfunc {
    Pfunc next;
    char *name;
    long calls;
    double time;
    double self;
    long num;
};

typedef struct sfunc *Sfunc;

struct sfunc {
    Pfunc p;
    Sfunc prev;
    double beg;
};

typedef struct parc *Parc;

struct parc {
    Parc next;
    Pfunc from;
    Pfunc to;
    long calls;
    double time;
    double self;
};

static Pfunc calls;
static int ncalls;
static Parc arcs;
static int narcs;
static Sfunc stack;
static Module zprof_module;

static void
freepfuncs(Pfunc f)
{
    Pfunc n;

    for (; f; f = n) {
	n = f->next;
	zsfree(f->name);
	zfree(f, sizeof(*f));
    }
}

static void
freeparcs(Parc a)
{
    Parc n;

    for (; a; a = n) {
	n = a->next;
	zfree(a, sizeof(*a));
    }
}

static Pfunc
findpfunc(char *name)
{
    Pfunc f;

    for (f = calls; f; f = f->next)
	if (!strcmp(name, f->name))
	    return f;

    return NULL;
}

static Parc
findparc(Pfunc f, Pfunc t)
{
    Parc a;

    for (a = arcs; a; a = a->next)
	if (a->from == f && a->to == t)
	    return a;

    return NULL;
}

static int
cmpsfuncs(Pfunc *a, Pfunc *b)
{
    return ((*a)->self > (*b)->self ? -1 : ((*a)->self != (*b)->self));
}

static int
cmptfuncs(Pfunc *a, Pfunc *b)
{
    return ((*a)->time > (*b)->time ? -1 : ((*a)->time != (*b)->time));
}

static int
cmpparcs(Parc *a, Parc *b)
{
    return ((*a)->time > (*b)->time ? -1 : ((*a)->time != (*b)->time));
}

static int
bin_zprof(UNUSED(char *nam), UNUSED(char **args), Options ops, UNUSED(int func))
{
    if (OPT_ISSET(ops,'c')) {
	freepfuncs(calls);
	calls = NULL;
	ncalls = 0;
	freeparcs(arcs);
	arcs = NULL;
	narcs = 0;
    } else {
	VARARR(Pfunc, fs, (ncalls + 1));
	Pfunc f, *fp;
	VARARR(Parc, as, (narcs + 1));
	Parc a, *ap;
	long i;
	double total;

	for (total = 0.0, f = calls, fp = fs; f; f = f->next, fp++) {
	    *fp = f;
	    total += f->self;
	}
	*fp = NULL;
	for (a = arcs, ap = as; a; a = a->next, ap++)
	    *ap = a;
	*ap = NULL;

	qsort(fs, ncalls, sizeof(f),
	      (int (*) _((const void *, const void *))) cmpsfuncs);
	qsort(as, narcs, sizeof(a),
	      (int (*) _((const void *, const void *))) cmpparcs);

	printf("num  calls                time                       self            name\n-----------------------------------------------------------------------------------\n");
	for (fp = fs, i = 1; *fp; fp++, i++) {
	    printf("%2ld) %4ld       %8.2f %8.2f  %6.2f%%  %8.2f %8.2f  %6.2f%%  %s\n",
		   ((*fp)->num = i),
		   (*fp)->calls,
		   (*fp)->time, (*fp)->time / ((double) (*fp)->calls),
		   ((*fp)->time / total) * 100.0,
		   (*fp)->self, (*fp)->self / ((double) (*fp)->calls),
		   ((*fp)->self / total) * 100.0,
		   (*fp)->name);
	}
	qsort(fs, ncalls, sizeof(f),
	      (int (*) _((const void *, const void *))) cmptfuncs);

	for (fp = fs; *fp; fp++) {
	    printf("\n-----------------------------------------------------------------------------------\n\n");
	    for (ap = as; *ap; ap++)
		if ((*ap)->to == *fp) {
		    printf("    %4ld/%-4ld  %8.2f %8.2f  %6.2f%%  %8.2f %8.2f             %s [%ld]\n",
			   (*ap)->calls, (*fp)->calls,
			   (*ap)->time, (*ap)->time / ((double) (*ap)->calls),
			   ((*ap)->time / total) * 100.0,
			   (*ap)->self, (*ap)->self / ((double) (*ap)->calls),
			   (*ap)->from->name, (*ap)->from->num);
		}
	    printf("%2ld) %4ld       %8.2f %8.2f  %6.2f%%  %8.2f %8.2f  %6.2f%%  %s\n",
		   (*fp)->num, (*fp)->calls,
		   (*fp)->time, (*fp)->time / ((double) (*fp)->calls),
		   ((*fp)->time / total) * 100.0,
		   (*fp)->self, (*fp)->self / ((double) (*fp)->calls),
		   ((*fp)->self / total) * 100.0,
		   (*fp)->name);
	    for (ap = as + narcs - 1; ap >= as; ap--)
		if ((*ap)->from == *fp) {
		    printf("    %4ld/%-4ld  %8.2f %8.2f  %6.2f%%  %8.2f %8.2f             %s [%ld]\n",
			   (*ap)->calls, (*ap)->to->calls,
			   (*ap)->time, (*ap)->time / ((double) (*ap)->calls),
			   ((*ap)->time / total) * 100.0,
			   (*ap)->self, (*ap)->self / ((double) (*ap)->calls),
			   (*ap)->to->name, (*ap)->to->num);
		}
	}
    }
    return 0;
}

static char *
name_for_anonymous_function(char *name)
{
    char lineno[DIGBUFSIZE];
    char *parts[7];

    convbase(lineno, funcstack[0].flineno, 10);

    parts[0] = name;
    parts[1] = " [";
    parts[2] = funcstack[0].filename ? funcstack[0].filename : "";
    parts[3] = ":";
    parts[4] = lineno;
    parts[5] = "]";
    parts[6] = NULL;

    return sepjoin(parts, "", 0);
}

static int
zprof_wrapper(Eprog prog, FuncWrap w, char *name)
{
    int active = 0;
    struct sfunc sf, *sp;
    Pfunc f = NULL;
    Parc a = NULL;
    struct timeval tv;
    struct timezone dummy;
    double prev = 0, now;
    char *name_for_lookups;

    if (is_anonymous_function_name(name)) {
        name_for_lookups = name_for_anonymous_function(name);
    } else {
        name_for_lookups = ztrdup(name);
    }

    if (zprof_module && !(zprof_module->node.flags & MOD_UNLOAD)) {
        active = 1;
        if (!(f = findpfunc(name_for_lookups))) {
            f = (Pfunc) zalloc(sizeof(*f));
            f->name = name_for_lookups;
            f->calls = 0;
            f->time = f->self = 0.0;
            f->next = calls;
            calls = f;
            ncalls++;
        }
        if (stack) {
            if (!(a = findparc(stack->p, f))) {
                a = (Parc) zalloc(sizeof(*a));
                a->from = stack->p;
                a->to = f;
                a->calls = 0;
                a->time = a->self = 0.0;
                a->next = arcs;
                arcs = a;
                narcs++;
            }
        }
        sf.prev = stack;
        sf.p = f;
        stack = &sf;

        f->calls++;
        tv.tv_sec = tv.tv_usec = 0;
        gettimeofday(&tv, &dummy);
        sf.beg = prev = ((((double) tv.tv_sec) * 1000.0) +
                         (((double) tv.tv_usec) / 1000.0));
    }
    runshfunc(prog, w, name);
    if (active) {
        if (zprof_module && !(zprof_module->node.flags & MOD_UNLOAD)) {
            tv.tv_sec = tv.tv_usec = 0;
            gettimeofday(&tv, &dummy);

            now = ((((double) tv.tv_sec) * 1000.0) +
                   (((double) tv.tv_usec) / 1000.0));
            f->self += now - sf.beg;
            for (sp = sf.prev; sp && sp->p != f; sp = sp->prev);
            if (!sp)
                f->time += now - prev;
            if (a) {
                a->calls++;
                a->self += now - sf.beg;
            }
            stack = sf.prev;

            if (stack) {
                stack->beg += now - prev;
                if (a)
                    a->time += now - prev;
            }
        } else
            stack = sf.prev;
    }
    return 0;
}

static struct builtin bintab[] = {
    BUILTIN("zprof", 0, bin_zprof, 0, 0, 0, "c", NULL),
};

static struct funcwrap wrapper[] = {
    WRAPDEF(zprof_wrapper),
};

static struct features module_features = {
    bintab, sizeof(bintab)/sizeof(*bintab),
    NULL, 0,
    NULL, 0,
    NULL, 0,
    0
};

/**/
int
setup_(Module m)
{
    zprof_module = m;
    return 0;
}

/**/
int
features_(Module m, char ***features)
{
    *features = featuresarray(m, &module_features);
    return 0;
}

/**/
int
enables_(Module m, int **enables)
{
    return handlefeatures(m, &module_features, enables);
}

/**/
int
boot_(Module m)
{
    calls = NULL;
    ncalls = 0;
    arcs = NULL;
    narcs = 0;
    stack = NULL;
    return addwrapper(m, wrapper);
}

/**/
int
cleanup_(Module m)
{
    freepfuncs(calls);
    freeparcs(arcs);
    deletewrapper(m, wrapper);
    return setfeatureenables(m, &module_features, NULL);
}

/**/
int
finish_(UNUSED(Module m))
{
    return 0;
}
debug log:

solving 2d8ed4f68 ...
found 2d8ed4f68 in https://inbox.vuxu.org/zsh-workers/20200526220638.26711-1-danielsh@tarpaulin.shahaf.local2/
found bc97771c0 in https://git.vuxu.org/mirror/zsh/
preparing index
index prepared:
100644 bc97771c04269639c84b869dbc4a6f5e2c21d7ef	Src/Modules/zprof.c

applying [1/1] https://inbox.vuxu.org/zsh-workers/20200526220638.26711-1-danielsh@tarpaulin.shahaf.local2/
diff --git a/Src/Modules/zprof.c b/Src/Modules/zprof.c
index bc97771c0..2d8ed4f68 100644

Checking patch Src/Modules/zprof.c...
Applied patch Src/Modules/zprof.c cleanly.

index at:
100644 2d8ed4f684cc433b4b9efe522c27186fbfa56870	Src/Modules/zprof.c

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).