~ubuntu-branches/debian/squeeze/erlang/squeeze

« back to all changes in this revision

Viewing changes to erts/emulator/beam/erl_instrument.c

  • Committer: Bazaar Package Importer
  • Author(s): Erlang Packagers, Sergei Golovan
  • Date: 2006-12-03 17:07:44 UTC
  • mfrom: (2.1.11 feisty)
  • Revision ID: james.westby@ubuntu.com-20061203170744-rghjwupacqlzs6kv
Tags: 1:11.b.2-4
[ Sergei Golovan ]
Fixed erlang-base and erlang-base-hipe prerm scripts.

Show diffs side-by-side

added added

removed removed

Lines of Context:
23
23
#include "global.h"
24
24
#include "big.h"
25
25
#include "erl_instrument.h"
 
26
#include "erl_threads.h"
26
27
 
27
28
typedef union { long l; double d; } Align_t;
28
29
 
55
56
    Uint max_blocks_ever;
56
57
} Stat_t;
57
58
 
58
 
#ifdef USE_THREADS
59
 
#define ERL_THREADS_EMU_INTERNAL__
60
 
#include "erl_threads.h"
61
 
static erts_mutex_t instr_mutex;
62
 
static erts_mutex_t instr_x_mutex;
63
 
#  ifdef DEBUG
64
 
#    define LOCK(MTX)   ASSERT(0 == erts_mutex_lock((MTX)))
65
 
#    define UNLOCK(MTX) ASSERT(0 == erts_mutex_unlock((MTX)))
66
 
#  else
67
 
#    define LOCK(MTX)   ((void) erts_mutex_lock((MTX)))
68
 
#    define UNLOCK(MTX) ((void) erts_mutex_unlock((MTX)))
69
 
#  endif
70
 
#else
71
 
#  define LOCK(MTX)
72
 
#  define UNLOCK(MTX)
73
 
#endif
 
59
static erts_mtx_t instr_mutex;
 
60
static erts_mtx_t instr_x_mutex;
74
61
 
75
62
int erts_instr_memory_map;
76
63
int erts_instr_stat;
77
64
 
78
 
static Eterm current_pid;
79
 
 
80
65
static ErtsAllocatorFunctions_t real_allctrs[ERTS_ALC_A_MAX+1];
81
66
 
82
67
struct stats_ {
289
274
    Uint ssize;
290
275
    void *res;
291
276
 
292
 
    LOCK(instr_mutex);
 
277
    erts_mtx_lock(&instr_mutex);
293
278
 
294
279
    ssize = size + STAT_BLOCK_HEADER_SIZE;
295
280
    res = (*real_af->alloc)(n, real_af->extra, ssize);
299
284
        res = (void *) ((StatBlock_t *) res)->mem;
300
285
    }
301
286
 
302
 
    UNLOCK(instr_mutex);
 
287
    erts_mtx_unlock(&instr_mutex);
303
288
 
304
289
    return res;
305
290
}
313
298
    void *sptr;
314
299
    void *res;
315
300
 
316
 
    LOCK(instr_mutex);
 
301
    erts_mtx_lock(&instr_mutex);
317
302
 
318
303
    if (ptr) {
319
304
        sptr = (void *) (((char *) ptr) - STAT_BLOCK_HEADER_SIZE);
332
317
        res = (void *) ((StatBlock_t *) res)->mem;
333
318
    }
334
319
 
335
 
    UNLOCK(instr_mutex);
 
320
    erts_mtx_unlock(&instr_mutex);
336
321
 
337
322
    return res;
338
323
}
343
328
    ErtsAllocatorFunctions_t *real_af = (ErtsAllocatorFunctions_t *) extra;
344
329
    void *sptr;
345
330
 
346
 
    LOCK(instr_mutex);
 
331
    erts_mtx_lock(&instr_mutex);
347
332
 
348
333
    if (ptr) {
349
334
        sptr = (void *) (((char *) ptr) - STAT_BLOCK_HEADER_SIZE);
355
340
 
356
341
    (*real_af->free)(n, real_af->extra, sptr);
357
342
 
358
 
    UNLOCK(instr_mutex);
 
343
    erts_mtx_unlock(&instr_mutex);
359
344
 
360
345
}
361
346
 
370
355
    Uint msize;
371
356
    void *res;
372
357
 
373
 
    LOCK(instr_mutex);
 
358
    erts_mtx_lock(&instr_mutex);
374
359
 
375
360
    msize = size + MAP_STAT_BLOCK_HEADER_SIZE;
376
361
    res = (*real_af->alloc)(n, real_af->extra, msize);
380
365
 
381
366
        mb->size = size;
382
367
        mb->type_no = n;
383
 
        mb->pid = current_pid;
 
368
        mb->pid = erts_get_current_pid();
384
369
 
385
370
        mb->prev = NULL;
386
371
        mb->next = mem_anchor;
391
376
        res = (void *) mb->mem;
392
377
    }
393
378
 
394
 
    UNLOCK(instr_mutex);
 
379
    erts_mtx_unlock(&instr_mutex);
395
380
 
396
381
    return res;
397
382
}
405
390
    void *mptr;
406
391
    void *res;
407
392
 
408
 
    LOCK(instr_x_mutex);
409
 
    LOCK(instr_mutex);
 
393
    erts_mtx_lock(&instr_x_mutex);
 
394
    erts_mtx_lock(&instr_mutex);
410
395
 
411
396
    if (ptr) {
412
397
        mptr = (void *) (((char *) ptr) - MAP_STAT_BLOCK_HEADER_SIZE);
424
409
 
425
410
        mb->size = size;
426
411
        mb->type_no = n;
427
 
        mb->pid = current_pid;
 
412
        mb->pid = erts_get_current_pid();
428
413
 
429
414
        stat_upd_realloc(n, size, old_size);
430
415
 
453
438
        res = (void *) mb->mem;
454
439
    }
455
440
 
456
 
    UNLOCK(instr_mutex);
457
 
    UNLOCK(instr_x_mutex);
 
441
    erts_mtx_unlock(&instr_mutex);
 
442
    erts_mtx_unlock(&instr_x_mutex);
458
443
 
459
444
    return res;
460
445
}
465
450
    ErtsAllocatorFunctions_t *real_af = (ErtsAllocatorFunctions_t *) extra;
466
451
    void *mptr;
467
452
 
468
 
    LOCK(instr_x_mutex);
469
 
    LOCK(instr_mutex);
 
453
    erts_mtx_lock(&instr_x_mutex);
 
454
    erts_mtx_lock(&instr_mutex);
470
455
 
471
456
    if (ptr) {
472
457
        MapStatBlock_t *mb;
489
474
 
490
475
    (*real_af->free)(n, real_af->extra, mptr);
491
476
 
492
 
    UNLOCK(instr_mutex);
493
 
    UNLOCK(instr_x_mutex);
494
 
 
495
 
}
496
 
 
497
 
void
498
 
erts_instr_set_curr_proc(Eterm pid)
499
 
{
500
 
    LOCK(instr_mutex);
501
 
    current_pid = pid;
502
 
    UNLOCK(instr_mutex);
503
 
}
504
 
 
505
 
void
506
 
erts_instr_reset_curr_proc(void)
507
 
{
508
 
    LOCK(instr_mutex);
509
 
    current_pid = THE_NON_VALUE;
510
 
    UNLOCK(instr_mutex);
 
477
    erts_mtx_unlock(&instr_mutex);
 
478
    erts_mtx_unlock(&instr_x_mutex);
 
479
 
511
480
}
512
481
 
513
482
static void dump_memory_map_to_stream(FILE *fp)
514
483
{
515
484
    ErtsAlcType_t n;
516
485
    MapStatBlock_t *bp;
517
 
 
518
 
    if (!erts_writing_erl_crash_dump) {
519
 
        LOCK(instr_mutex);
520
 
    }
 
486
    int lock = !ERTS_IS_CRASH_DUMPING;
 
487
    if (lock)
 
488
        erts_mtx_lock(&instr_mutex);
521
489
 
522
490
    /* Write header */
523
491
 
574
542
                    (Uint) bp->size);
575
543
    }
576
544
 
577
 
    if (!erts_writing_erl_crash_dump) {
578
 
        UNLOCK(instr_mutex);
579
 
    }
 
545
    if (lock)
 
546
        erts_mtx_unlock(&instr_mutex);
580
547
}
581
548
 
582
549
int erts_instr_dump_memory_map_to_fd(int fd)
639
606
    if (!am_a)
640
607
        init_am_a();
641
608
 
642
 
    LOCK(instr_x_mutex);
643
 
    LOCK(instr_mutex);
 
609
    erts_mtx_lock(&instr_x_mutex);
 
610
    erts_mtx_lock(&instr_mutex);
644
611
 
645
612
    /* Header size */
646
613
    hsz = 5 + 1 + (ERTS_ALC_N_MAX+1-ERTS_ALC_N_MIN)*(1 + 4);
672
639
    org_mem_anchor = mem_anchor;
673
640
    mem_anchor = NULL;
674
641
 
675
 
    UNLOCK(instr_mutex);
 
642
    erts_mtx_unlock(&instr_mutex);
676
643
 
677
644
    hp = HAlloc(proc, hsz); /* May end up calling map_stat_alloc() */
678
645
 
679
 
    LOCK(instr_mutex);
 
646
    erts_mtx_lock(&instr_mutex);
680
647
 
681
648
#ifdef DEBUG
682
649
    end_hp = hp + hsz;
806
773
        mem_anchor = org_mem_anchor;
807
774
    }
808
775
 
809
 
    UNLOCK(instr_mutex);
810
 
    UNLOCK(instr_x_mutex);
 
776
    erts_mtx_unlock(&instr_mutex);
 
777
    erts_mtx_unlock(&instr_x_mutex);
811
778
 
812
779
    return res;
813
780
}
845
812
erts_instr_get_stat(Process *proc, Eterm what, int begin_max_period)
846
813
{
847
814
    int i, len, max, min, allctr;
848
 
    byte *bufp;
849
815
    Eterm *names, *values, res;
850
 
    Uint arr_size, buf_size, stat_size, hsz, *hszp, *hp, **hpp;
 
816
    Uint arr_size, stat_size, hsz, *hszp, *hp, **hpp;
851
817
    Stat_t *stat_src, *stat;
852
818
 
853
819
    if (!erts_instr_stat)
900
866
        return THE_NON_VALUE;
901
867
    }
902
868
 
903
 
    bufp = tmp_buf;
904
 
    buf_size = TMP_BUF_SIZE;
905
 
 
906
 
    if (buf_size >= stat_size) {
907
 
        stat = (Stat_t *) bufp;
908
 
        bufp += stat_size;
909
 
        buf_size -= stat_size;
910
 
        ASSERT(stat_size % sizeof(Uint) == 0);
911
 
    }
912
 
    else
913
 
        stat = (Stat_t *) erts_alloc(ERTS_ALC_T_TMP, stat_size);
 
869
    stat = (Stat_t *) erts_alloc(ERTS_ALC_T_TMP, stat_size);
914
870
 
915
871
    arr_size = (max - min + 1)*sizeof(Eterm);
916
872
 
917
 
    if (allctr) {
918
 
        if (buf_size >= arr_size) {
919
 
            names = (Eterm *) bufp;
920
 
            bufp += arr_size;
921
 
            buf_size -= arr_size;
922
 
            ASSERT(arr_size % sizeof(Uint) == 0);
923
 
        }
924
 
        else
925
 
            names = (Eterm *) erts_alloc(ERTS_ALC_T_TMP, arr_size);
926
 
    }
927
 
 
928
 
    if (buf_size >= arr_size) {
929
 
        values = (Eterm *) bufp;
930
 
        bufp += arr_size;
931
 
        buf_size -= arr_size;
932
 
        ASSERT(arr_size % sizeof(Uint) == 0);
933
 
    }
934
 
    else
935
 
        values = (Eterm *) erts_alloc(ERTS_ALC_T_TMP, arr_size);
936
 
 
937
 
    LOCK(instr_mutex);
 
873
    if (allctr)
 
874
        names = (Eterm *) erts_alloc(ERTS_ALC_T_TMP, arr_size);
 
875
 
 
876
    values = (Eterm *) erts_alloc(ERTS_ALC_T_TMP, arr_size);
 
877
 
 
878
    erts_mtx_lock(&instr_mutex);
938
879
 
939
880
    update_max_ever_values(stat_src, min, max);
940
881
 
943
884
    if (begin_max_period)
944
885
        begin_new_max_period(stat_src, min, max);
945
886
 
946
 
    UNLOCK(instr_mutex);
 
887
    erts_mtx_unlock(&instr_mutex);
947
888
 
948
889
    hsz = 0;
949
890
    hszp = &hsz;
986
927
        goto restart_bld;
987
928
    }
988
929
 
989
 
    if ((byte *) stat > tmp_buf + TMP_BUF_SIZE || (byte *) stat < tmp_buf)
990
 
        erts_free(ERTS_ALC_T_TMP, (void *) stat);
991
 
    if ((byte *) values > tmp_buf + TMP_BUF_SIZE || (byte *) values < tmp_buf)
992
 
        erts_free(ERTS_ALC_T_TMP, (void *) values);
993
 
    if (allctr
994
 
        && ((byte *) names > tmp_buf + TMP_BUF_SIZE
995
 
            || (byte *) names < tmp_buf))
 
930
    erts_free(ERTS_ALC_T_TMP, (void *) stat);
 
931
    erts_free(ERTS_ALC_T_TMP, (void *) values);
 
932
    if (allctr)
996
933
        erts_free(ERTS_ALC_T_TMP, (void *) names);
997
934
 
998
935
    return res;
1003
940
{
1004
941
    ErtsAlcType_t i, a_max, a_min;
1005
942
 
1006
 
    LOCK(instr_mutex);
 
943
    erts_mtx_lock(&instr_mutex);
1007
944
 
1008
945
    fprintf(fp,
1009
946
            "{instr_vsn,%lu}.\n",
1092
1029
        begin_new_max_period(stats->n, ERTS_ALC_N_MIN, ERTS_ALC_N_MAX);
1093
1030
    }
1094
1031
 
1095
 
    UNLOCK(instr_mutex);
 
1032
    erts_mtx_unlock(&instr_mutex);
1096
1033
 
1097
1034
}
1098
1035
 
1164
1101
    if (!am_c)
1165
1102
        init_am_c();
1166
1103
 
1167
 
    if (TMP_BUF_SIZE >= (ERTS_ALC_N_MAX-ERTS_ALC_N_MIN+1)*sizeof(Eterm))
1168
 
        tpls = (Eterm *) tmp_buf;
1169
 
    else
1170
 
        tpls = (Eterm *) erts_alloc(ERTS_ALC_T_TMP,
1171
 
                                    (ERTS_ALC_N_MAX-ERTS_ALC_N_MIN+1)
1172
 
                                    * sizeof(Eterm));
 
1104
    tpls = (Eterm *) erts_alloc(ERTS_ALC_T_TMP,
 
1105
                                (ERTS_ALC_N_MAX-ERTS_ALC_N_MIN+1)
 
1106
                                * sizeof(Eterm));
1173
1107
    hsz = 0;
1174
1108
    hszp = &hsz;
1175
1109
    hpp = NULL;
1201
1135
        goto restart_bld;
1202
1136
    }
1203
1137
 
1204
 
    if (tpls != (Eterm *) tmp_buf)
1205
 
        erts_free(ERTS_ALC_T_TMP, tpls);
 
1138
    erts_free(ERTS_ALC_T_TMP, tpls);
1206
1139
 
1207
1140
    return res;
1208
1141
}
1226
1159
 
1227
1160
    stats = erts_alloc(ERTS_ALC_T_INSTR_INFO, sizeof(struct stats_));
1228
1161
 
1229
 
#ifdef USE_THREADS
1230
 
    instr_mutex = erts_mutex_sys(ERTS_MUTEX_SYS_INSTR);
1231
 
    if(!instr_mutex || erts_mutex_set_default_atfork(instr_mutex))
1232
 
        erl_exit(1, "Failed to initialize instr mutex\n");
1233
 
#endif
 
1162
    erts_mtx_init(&instr_mutex, "instr");
 
1163
    erts_mtx_set_forksafe(&instr_mutex);
1234
1164
 
1235
1165
    mem_anchor = NULL;
1236
 
    current_pid = THE_NON_VALUE;
1237
1166
 
1238
1167
    /* Install instrumentation functions */
1239
1168
    ASSERT(sizeof(erts_allctrs) == sizeof(real_allctrs));
1253
1182
    }
1254
1183
 
1255
1184
    if (map_stat) {
1256
 
#ifdef USE_THREADS
1257
 
        instr_x_mutex = erts_mutex_sys(ERTS_MUTEX_SYS_INSTR_X);
1258
 
        if(!instr_x_mutex || erts_mutex_set_default_atfork(instr_x_mutex))
1259
 
            erl_exit(1, "Failed to initialize instr x mutex\n");
1260
 
#endif
 
1185
 
 
1186
        erts_mtx_init(&instr_x_mutex, "instr_x");
 
1187
        erts_mtx_set_forksafe(&instr_x_mutex);
 
1188
 
1261
1189
        erts_instr_memory_map = 1;
1262
1190
        erts_instr_stat = 1;
1263
1191
        for (i = ERTS_ALC_A_MIN; i <= ERTS_ALC_A_MAX; i++) {