File: Profiling.pmod

package info (click to toggle)
pike8.0 8.0.1956-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 60,580 kB
  • sloc: ansic: 259,734; xml: 36,320; makefile: 3,748; sh: 1,713; cpp: 1,349; awk: 1,036; lisp: 655; javascript: 468; asm: 242; objc: 240; pascal: 157; sed: 34
file content (329 lines) | stat: -rw-r--r-- 9,719 bytes parent folder | download | duplicates (6)
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
/* -*- Mode: pike; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
#pike __REAL_VERSION__
#require constant(get_profiling_info)

private multiset(program) object_programs()
{
    multiset x = (<>);
    int orig_enabled = Pike.gc_parameters()->enabled;
    Pike.gc_parameters( (["enabled":0]) );
    program prog = this_program;

    void add_traverse( program start, function traverse )
    {
        while(1)
        {
            x[start] = 1;
            program next_prog;
            if(catch(next_prog=traverse(start)))
                break;
            start = next_prog;
        }
    };

    add_traverse( _next(prog), _next );
    add_traverse( prog, _prev );
    Pike.gc_parameters( (["enabled":orig_enabled]) );

    return x;
}

private string remove_cwd(string from)
{
    if( !from ) return 0;

    if( has_prefix( from, getcwd() ) )
        from = from[strlen(getcwd())..];
    return from;
}

private string normalize_name( string from )
{
    from = remove_cwd( from );
    from = replace( from, ".pmod/", "." );
    from = replace( from, ".pmod", "" );
    from = replace( from, ".pike", "" );
    from = replace( from, ".so", "" );
    from = replace( from, "()->", "." );
    return from;
}

private array (program) all_modules()
{
    return (array)object_programs();
}

private string filter_obj_name( string x )
{
    string a, b;
    while( sscanf( x, "%s(%*s)%s", a, b ) == 3 )
        x = a+b;
    return x;
}

private mapping(program|object:string) pnc = set_weak_flag(([]),Pike.WEAK_INDICES);
private string program_name(program|object what)
{
    if( pnc[what] ) return pnc[what];
    string p;
    if( (p = search(master()->programs,what)) && p != "main") 
        return pnc[what] = normalize_name(p);
    return pnc[what] = filter_obj_name(sprintf("%O",what)-".pike");
}

private string program_function_file( program prog, string key )
{
    return remove_cwd(Program.defined(prog, key)||"-");
}

private mapping get_prof(bool avoid_overhead)
{
    mapping res = ([]);
    foreach(all_modules(), program prog)
    {
        if( prog && programp( prog ) )
        {
            string n = pnc[prog] || program_name(prog);
            int cnt = 1;
            array pinf = get_profiling_info( prog );
            /* clones, ([ info }) */
            if( !avoid_overhead )
                foreach( pinf[1]; string key; array row )
                    pinf[1][key] += ({program_function_file(prog,key)});
            while( res[n] )
                n=pnc[prog]+"<"+(++cnt)+">";
            res[n] = pinf;
        }
    }
    return res;
}


private string trim_obj( string x )
{
    sscanf( x, "/mod/%s", x );
    sscanf( x, "/modules/%s", x );
    if( has_prefix( x, "/main." ) )
        x = "Backend"+x[5..];

    int l;
    if( sscanf( x, "%s__lambda_%*s_line_%d", x, l ) )
        x = x+" @ "+l;
    x = replace( x, "().", "." );
    return x;
};

private mapping oi = ([]);
private int last_cpu_used;
private mapping(string:mapping(string:string)) nc = ([]);

private mapping low_get_prof_info( bool avoid_overhead )
{
    mapping as_functions = ([]);
    mapping tmp = get_prof(avoid_overhead);

    foreach(indices(tmp), string c)
    {
        // if( has_prefix( c, "/master" ) ) 
        //     continue;
        if( has_value( c, "Profiling" ) )
            continue;
        mapping g = tmp[c][1];
        mapping ce = nc[c] || (nc[c]=([]));

        foreach(indices(g), string f)
        {
            string fn = ce[f];
            if( !fn )
            {
                fn = c+"."+f;
                if( has_prefix( f, "__lambda" ) )
                    fn = c+".lambda @ "+(f/"_")[-1];
                switch( f )
                {
                    case "cast":   fn = "(cast)"+c; break;
                    case "__INIT": fn = c;          break;
                    case "create": case "`()": fn = c+"()"; break;
                    case "`->":    fn = c+"->"; break;
                    case "`[]":    fn = c+"[]"; break;
                }
                fn = replace( fn, "()->", ".");
                fn = replace( fn, "->", ".");
                fn = replace( fn, "_static_modules.", "" );
                fn = replace( fn, ".Builtin", "" );
                fn = trim_obj( fn );
                fn = replace( fn, "\0", "");
                ce[f] = fn;
            }
            if( !has_suffix(fn,"Program.defined" ) )
                as_functions[fn] = ({ g[f][2],g[f][0],g[f][1],g[f][-1] });
        }
    }
    return as_functions;
}


private void hide_compile_time()
{
    get_prof_info();
}
private mixed __when_pike_backend_started = call_out( hide_compile_time, 0.0 );

//! Collect profiling data.
//!
//! This will return the CPU usage, by function, since the last time
//! the function was called.
//!
//! The returned array contains the following entries per entry:
//! @array
//! @elem string name
//!   The name of the function
//! @elem float number_of_calls
//!   The number of calls
//! @elem float self_time
//!   The self CPU time
//! @elem float cpu_time
//!   The self CPU time, including children.
//! @elem float self_time_pct
//!   The self CPU time as percentage of total time.
//! @elem float cpu_time_pct
//!   The self CPU time, including children, as percentage of total
//!   time.
//! @elem string function_line
//!   This is the location in the source of the start of the function
//! @endarray
array(array(string|float|int)) get_prof_info(string|array(string)|void include,
                                             string|array(string)|void exclude)
{
    array res = ({});
    int time_passed = gethrvtime()-last_cpu_used;
    mapping as_functions = low_get_prof_info( false );
    foreach( as_functions; string key; array v )
    {
        if( oi[key] )
        {
            v[0] -= oi[key][0];
            v[1] -= oi[key][1];
            v[2] -= oi[key][2];
        }
        if( v[1] && (v[0]||v[2]) )
        {
            res +=
                ({
                    ({key,
                      v[1],
                      (float)v[0],
                      (float)v[2],
                      (float)(v[0])/v[1] * 1000,
                      (float)(v[2])/v[1] * 1000,
                      (int)((v[0]*100) / (time_passed/1000.0)),
                      (int)((v[2]*100) / (time_passed/1000.0)),
                      v[-1],
                    })
                });
        }
    }
    if( include && sizeof(include) )
        res = filter( res, lambda( array q ) { return glob(include,q[0]); });

    if( exclude && sizeof(exclude) )
        res = filter( res, lambda( array q ) { return !glob(exclude,q[0]); });

    /* In order to avoid counting the act of profiling, get information again: */
    oi = low_get_prof_info( true );
    last_cpu_used = gethrvtime();
    return res;
}

int name_column_width = 32;

private string format_number(string|int|float x )
{
    if( stringp( x ) )
    {
        if( x[0..6] == "/master" )
            x = x[1..];
        x = replace((x-" "),"@",":");
        if( strlen( x ) > name_column_width )
            x = x[..5] + "[..]" + x[<(name_column_width-11)..];
        return x;
    }
    if( intp( x ) )
    {
        if( x > 100000 )
            return x/1000+"k";
        return (string)x;
    }

    constant units = ({ "k", "M", "G", "T", "" });
    int unit = -1;
    while( x >= 1000.0 )
    {
        x/= 1000.0;
        unit++;
    }

    if( (float)(int)x == x && (int)x == 0 )
        return "-";

    return sprintf("%.1f%s",x,units[unit] );
}


private void output_result( array rows, int percentage_column )
{
    string line = "-"*(79-32+name_column_width)+"\n";
    werror(line);
    werror("%-"+name_column_width+"s %7s %7s %7s %5s %7s %7s\n",
           "Function", "Calls", "Time","+Child","%", "/call", "ch/call" );
    werror("%-"+name_column_width+"s %7s %7s %7s %5s %7s %7s\n",
           "", "", "ms", "ms", "", "us", "us" );
    werror(line);
    foreach( rows, array r )
        werror( "%[0]-"+name_column_width+"s %[1]7s %[2]7s %[3]7s"
                " %["+percentage_column+"]5s %[4]7s %[5]7s\n",
                @format_number(r[*]) );
    werror(line);
}


//! Show profiling information in a more-or-less readable manner.
//! This only works if pike has been compiled with profiling support.
//!
//! The function will print to stderr using werror.
//!
//! This is mainly here for use from the @[Debug.Watchdog] class, if
//! you want to do your own formatting or output to some other channel
//! use @[get_prof_info] instead.
void display(int|void num,
             string|array(string)|void pattern,
             string|array(string)|void exclude)
{
    // Show top num.
    if( !exclude )
    {
        // Do not count ourselves by default.
        exclude = ({"Debug.*",
                    "/master.describe_program",
                    "/master.describe_module",
                    "/master.describe_function",
                    "/master.describe_backtrace",
                    "/master.program_path_to_name",
                    "/master.describe_object"
                  });
    }
    array rows = get_prof_info( pattern,exclude );
    sort( (array(float))column(rows,1+(__MINOR__==6?0:2)), rows );
    if( pattern )
        rows = filter( rows, lambda(array row) {
                                 return glob(pattern, row[0]);
                             });
    werror("Most CPU-consuming functions "
           "sorted by total (self + child) times.\n");
    output_result( reverse(rows)[..num||99], 7 );

    werror("\n"
           "Most CPU-consuming functions sorted by self-time.\n");
    sort( (array(float))column(rows,1+(__MINOR__==6?0:1)), rows );
    output_result( reverse(rows)[..num||99], 6 );
}