mirror of
https://github.com/Mercury-Language/mercury.git
synced 2025-12-12 20:34:19 +00:00
Branches: main
Implement a new form of memory profiling, which tells the user what memory
is being retained during a program run. This is done by allocating an extra
word before each cell, which is used to "attribute" the cell to an
allocation site. The attribution, or "allocation id", is an address to an
MR_AllocSiteInfo structure generated by the Mercury compiler, giving the
procedure, filename and line number of the allocation, and the type
constructor and arity of the cell that it allocates.
The user must manually instrument the program with calls to
`benchmarking.report_memory_attribution', which forces a GC and summarises
the live objects on the heap using the attributions. The mprof tool is
extended with a new mode to parse and present that data.
Objects which are unattributed (e.g. by hand-written C code which hasn't
been updated) are still accounted for, but show up in profiles as "unknown".
Currently this profiling mode only works in conjunction with the Boehm
garbage collector, though in principle it can work with any memory allocator
for which we can access a list of the live objects. Since term size
profiling relies on the same technique of using an extra word per memory
cell, the two profiling modes are incompatible.
The output from `mprof -s' looks like this:
------ [1] some label ------
cells words cumul procedure / type (location)
14150 38872 total
* 1949/ 13.8% 4872/ 12.5% 12.5% <predicate `parser.parse_rest/7' mode 0>
975/ 6.9% 1950/ 5.0% list.list/1 (parser.m:502)
487/ 3.4% 1948/ 5.0% term.term/1 (parser.m:501)
487/ 3.4% 974/ 2.5% term.const/0 (parser.m:501)
* 1424/ 10.1% 4272/ 11.0% 23.5% <predicate `parser.parse_simple_term_2/6' mode 0>
708/ 5.0% 2832/ 7.3% term.term/1 (parser.m:643)
708/ 5.0% 1416/ 3.6% term.const/0 (parser.m:643)
...
boehm_gc/alloc.c:
boehm_gc/include/gc.h:
boehm_gc/misc.c:
boehm_gc/reclaim.c:
Add a callback function to be called for every live object after a GC.
Add a function to write out the GC_size_map array.
compiler/layout.m:
Define the alloc_site_info type which is equivalent to the
MR_AllocSiteInfo C structure.
Add alloc_site_array as a kind of "layout" array.
compiler/llds.m:
Add allocation sites to `cfile' structure.
Replace TypeMsg argument (which was also for profiling) on `incr_hp'
instructions by an allocation site identifier.
Add a new foreign_proc_component for allocation site ids.
compiler/code_info.m:
compiler/global_data.m:
compiler/proc_gen.m:
Keep the set of allocation sites in the code_info and global_data
structures.
compiler/unify_gen.m:
Add allocation sites to LLDS allocation instructions.
compiler/layout_out.m:
compiler/llds_out_file.m:
compiler/llds_out_instr.m:
Output MR_AllocSiteInfo arrays in generated C files.
Output code to register the MR_AllocSiteInfo array with the Mercury
runtime.
Output allocation site ids for memory allocation instructions.
compiler/llds_out_util.m:
Add allocation sites to llds_out_info.
compiler/pragma_c_gen.m:
compiler/ml_foreign_proc_gen.m:
Generate a macro MR_ALLOC_ID which resolves to an allocation site
structure, for every foreign_proc whose C code contains the string
"MR_ALLOC_ID". This is to be used by hand-written C code which
allocates memory.
MR_PROC_LABELs are retained for backwards compatibility. Though
they were introduced for profiling, they seem to have been co-opted
for printf-debugging since then.
compiler/ml_global_data.m:
Add allocation site structures to the MLDS global data.
compiler/mlds.m:
compiler/ml_unify_gen.m:
Add allocation site id to `new_object' instruction.
compiler/mlds_to_c.m:
Output allocation site arrays and allocation ids in high-level C code.
Output a call to register the allocation site array with the Mercury
runtime.
Delete an unused predicate.
compiler/exprn_aux.m:
compiler/jumpopt.m:
compiler/livemap.m:
compiler/mercury_compile_llds_back_end.m:
compiler/middle_rec.m:
compiler/ml_accurate_gc.m:
compiler/ml_elim_nested.m:
compiler/ml_optimize.m:
compiler/ml_util.m:
compiler/mlds_to_cs.m:
compiler/mlds_to_gcc.m:
compiler/mlds_to_il.m:
compiler/mlds_to_java.m:
compiler/mlds_to_managed.m:
compiler/opt_debug.m:
compiler/opt_util.m:
compiler/use_local_vars.m:
compiler/var_locn.m:
Conform to changes.
compiler/pickle.m:
compiler/prog_event.m:
compiler/timestamp.m:
Conform to changes in memory allocation macros.
library/benchmarking.m:
Add the `report_memory_attribution' instrumentation predicates.
Conform to changes to MR_memprof_record.
library/array.m:
library/bit_buffer.m:
library/bitmap.m:
library/construct.m:
library/deconstruct.m:
library/dir.m:
library/io.m:
library/mutvar.m:
library/store.m:
library/string.m:
library/thread.semaphore.m:
library/version_array.m:
Use attributed memory allocation throughout the standard library so
that objects don't show up in the memory profile as "unknown".
Replace MR_PROC_LABEL by MR_ALLOC_ID.
mdbcomp/program_representation.m:
mdbcomp/rtti_access.m:
Replace MR_PROC_LABEL by MR_ALLOC_ID.
profiler/Mercury.options:
profiler/globals.m:
profiler/mercury_profile.m:
profiler/options.m:
profiler/output.m:
profiler/snapshots.m:
Add a new mode to `mprof' to parse and present the data from
`Prof.Snapshots' files.
Add options for the new profiling mode.
profiler/process_file.m:
Fix a typo.
runtime/mercury_conf_param.h:
#define MR_MPROF_PROFILE_MEMORY_ATTRIBUTION if memory profiling
is enabled and we are using Boehm GC.
runtime/mercury.h:
Make MR_new_object take an allocation id argument.
Conform to changes in memory allocation macros.
runtime/mercury_memory.c:
runtime/mercury_memory.h:
runtime/mercury_types.h:
Define MR_AllocSiteInfo.
Add memory allocation functions and macros which take into the
account the additional word necessary for the new profiling mode.
These should be used in preferences to the raw memory allocation
functions wherever possible so that objects do not show up in the
profile as "unknown".
Add analogues of realloc/free which take into account the offset
introduced by the attribution word.
Add function versions of the MR_new_object macros, which can't be
written in standard C. They are only used when necessary.
Add built-in allocation site ids, to be used in the runtime and
other hand-written code when context-specific ids are unavailable.
runtime/mercury_heap.h:
Make MR_tag_offset_incr_hp_msg and MR_tag_offset_incr_hp_atomic_msg
allocate an extra word when memory attribution is desired, and store
the allocation id there.
Similarly for MR_create{1,2,3}_msg.
Replace proclabel arguments in allocation macros by alloc_id
arguments.
Replace MR_hp_alloc_atomic by MR_hp_alloc_atomic_msg. It was only
used for boxing floats.
Conform to change to MR_new_object macro.
runtime/mercury_bootstrap.h:
Delete obsolete macro hp_alloc_atomic.
runtime/mercury_heap_profile.c:
runtime/mercury_heap_profile.h:
Add the code to summarise the live objects on the Boehm GC heap and
writes out the data to `Prof.Snapshots', for display by mprof.
Don't store the procedure name in MR_memprof_record: the procedure
address is enough and faster to compare.
runtime/mercury_prof.c:
Finish and close the `Prof.Snapshots' file when the program
terminates.
Conform to changes in MR_memprof_record.
runtime/mercury_misc.h:
Add a macro to expand to the name of the allocation sites array
in LLDS grades.
runtime/mercury_bitmap.c:
runtime/mercury_bitmap.h:
Pass allocation id through bitmap allocation functions.
Delete unused function MR_string_to_bitmap.
runtime/mercury_string.h:
Add MR_make_aligned_string_copy_msg.
Make string allocation macros take allocation id arguments.
runtime/mercury.c:
runtime/mercury_array_macros.h:
runtime/mercury_context.c:
runtime/mercury_deconstruct.c:
runtime/mercury_deconstruct_macros.h:
runtime/mercury_dlist.c:
runtime/mercury_engine.c:
runtime/mercury_float.h:
runtime/mercury_hash_table.c:
runtime/mercury_ho_call.c:
runtime/mercury_label.c:
runtime/mercury_prof_mem.c:
runtime/mercury_stacks.c:
runtime/mercury_stm.c:
runtime/mercury_string.c:
runtime/mercury_thread.c:
runtime/mercury_trace_base.c:
runtime/mercury_trail.c:
runtime/mercury_type_desc.c:
runtime/mercury_type_info.c:
runtime/mercury_wsdeque.c:
Use attributed memory allocation throughout the runtime so that
objects don't show up in the profile as "unknown".
runtime/mercury_memory_zones.c:
Attribute memory zones to the Mercury runtime.
runtime/mercury_tabling.c:
runtime/mercury_tabling.h:
Use attributed memory allocation macros for tabling structures.
Delete unused MR_table_realloc_* and MR_table_copy_bytes macros.
runtime/mercury_deep_copy_body.h:
Try to retain the original attribution word when copying values.
runtime/mercury_ml_expand_body.h:
Conform to changes in memory allocation macros.
runtime/mercury_tags.h:
Replace proclabel arguments by alloc_id arguments in allocation macros.
runtime/mercury_wrapper.c:
If memory attribution is enabled, tell Boehm GC that pointers may be
displaced by an extra word.
trace/mercury_trace.c:
trace/mercury_trace_tables.c:
Conform to changes in memory allocation macros.
extras/net/tcp.m:
extras/solver_types/library/any_array.m:
extras/trailed_update/tr_array.m:
Conform to changes in memory allocation macros.
doc/user_guide.texi:
Document the new profiling mode.
doc/reference_manual.texi:
Update a commented out example.
295 lines
8.1 KiB
C
295 lines
8.1 KiB
C
/*
|
|
** vim: ts=4 sw=4 expandtab
|
|
*/
|
|
/*
|
|
** Copyright (C) 1997-2000, 2006-2008, 2011 The University of Melbourne.
|
|
** This file may only be copied under the terms of the GNU Library General
|
|
** Public License - see the file COPYING.LIB in the Mercury distribution.
|
|
*/
|
|
|
|
/*
|
|
** mercury_trail.c - code for the Mercury trail.
|
|
**
|
|
** The trail is used to record values that need to be restored on backtracking.
|
|
*/
|
|
|
|
#include "mercury_imp.h"
|
|
#include "mercury_trail.h"
|
|
#include "mercury_memory.h"
|
|
#include "mercury_memory_handlers.h"
|
|
#include "mercury_misc.h"
|
|
|
|
#ifdef MR_USE_TRAIL
|
|
|
|
#if !defined(MR_THREAD_SAFE)
|
|
MR_MemoryZone *MR_trail_zone;
|
|
MR_TrailEntry *MR_trail_ptr_var;
|
|
|
|
#if defined(MR_TRAIL_SEGMENTS)
|
|
MR_MemoryZones *MR_prev_trail_zones;
|
|
#endif
|
|
|
|
MR_Unsigned MR_ticket_counter_var = 1;
|
|
MR_Unsigned MR_ticket_high_water_var = 1;
|
|
#endif
|
|
|
|
#if defined(MR_TRAIL_SEGMENTS)
|
|
static void
|
|
MR_pop_trail_segment(void);
|
|
#endif
|
|
|
|
static void
|
|
MR_reset_trail_zone(void);
|
|
|
|
void
|
|
MR_untrail_to(MR_TrailEntry *old_trail_ptr, MR_untrail_reason reason)
|
|
{
|
|
MR_TrailEntry *tr_ptr;
|
|
MR_TrailEntry *tr_base;
|
|
|
|
/* Not needed, since MR_trail_ptr is never a real reg: */
|
|
/* MR_restore_transient_registers(); */
|
|
tr_ptr = MR_trail_ptr;
|
|
|
|
switch (reason) {
|
|
case MR_solve:
|
|
case MR_commit:
|
|
|
|
/* Just handle the function trail entries */
|
|
tr_base = MR_TRAIL_BASE;
|
|
while (tr_ptr != old_trail_ptr) {
|
|
tr_ptr--;
|
|
if (MR_get_trail_entry_kind(tr_ptr) == MR_func_entry) {
|
|
(*MR_get_trail_entry_untrail_func(tr_ptr))(
|
|
MR_get_trail_entry_datum(tr_ptr), reason);
|
|
}
|
|
|
|
/*
|
|
** When we are using trail segments it is possible that
|
|
** `old_trail_ptr' is not a location on the current trail segment.
|
|
** We need to walk backwards through all the previous segments
|
|
** (invoking function trail entires as we go) until we find it.
|
|
*/
|
|
#if defined(MR_TRAIL_SEGMENTS)
|
|
if (tr_ptr == tr_base
|
|
&& tr_ptr != old_trail_ptr)
|
|
{
|
|
MR_MemoryZones *prev_zones;
|
|
MR_MemoryZone *zone;
|
|
|
|
prev_zones = MR_PREV_TRAIL_ZONES;
|
|
zone = prev_zones->MR_zones_head;
|
|
tr_ptr = (MR_TrailEntry *) zone->MR_zone_end;
|
|
|
|
while (tr_ptr != old_trail_ptr) {
|
|
tr_ptr--;
|
|
if (MR_get_trail_entry_kind(tr_ptr) == MR_func_entry) {
|
|
(*MR_get_trail_entry_untrail_func(tr_ptr))(
|
|
MR_get_trail_entry_datum(tr_ptr), reason);
|
|
}
|
|
|
|
if (tr_ptr == (MR_TrailEntry *) zone->MR_zone_min
|
|
&& tr_ptr != old_trail_ptr)
|
|
{
|
|
prev_zones = prev_zones->MR_zones_tail;
|
|
zone = prev_zones->MR_zones_head;
|
|
tr_ptr = (MR_TrailEntry *) zone->MR_zone_end;
|
|
}
|
|
}
|
|
break;
|
|
}
|
|
#endif
|
|
}
|
|
/*
|
|
** NB. We do _not_ reset the trail pointer here. Doing so would be
|
|
** unsafe, for `mdi' modes, because we may still need to restore
|
|
** the value if/when we backtrack to a choicepoint prior to the one
|
|
** we're cutting away.
|
|
*/
|
|
break;
|
|
|
|
case MR_undo:
|
|
case MR_exception:
|
|
case MR_retry:
|
|
/* Handle both function and value trail entries */
|
|
tr_base = MR_TRAIL_BASE;
|
|
while (tr_ptr != old_trail_ptr) {
|
|
tr_ptr--;
|
|
if (MR_get_trail_entry_kind(tr_ptr) == MR_func_entry) {
|
|
(*MR_get_trail_entry_untrail_func(tr_ptr))(
|
|
MR_get_trail_entry_datum(tr_ptr), reason);
|
|
} else {
|
|
*MR_get_trail_entry_address(tr_ptr) =
|
|
MR_get_trail_entry_value(tr_ptr);
|
|
}
|
|
#if defined(MR_TRAIL_SEGMENTS)
|
|
if (tr_ptr == tr_base
|
|
&& tr_ptr != old_trail_ptr)
|
|
{
|
|
MR_pop_trail_segment();
|
|
tr_ptr = MR_trail_ptr;
|
|
tr_base = MR_TRAIL_BASE;
|
|
}
|
|
#endif
|
|
}
|
|
|
|
MR_trail_ptr = tr_ptr;
|
|
/* Not needed, since MR_trail_ptr is never a real reg: */
|
|
/* MR_save_transient_registers(); */
|
|
break;
|
|
|
|
default:
|
|
MR_fatal_error("unknown MR_untrail_reason");
|
|
}
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
MR_Unsigned
|
|
MR_num_trail_entries(void)
|
|
{
|
|
MR_Unsigned n_entries = 0;
|
|
|
|
#if defined(MR_TRAIL_SEGMENTS)
|
|
MR_MemoryZones *list;
|
|
MR_MemoryZone *zone;
|
|
|
|
list = MR_PREV_TRAIL_ZONES;
|
|
while (list != NULL) {
|
|
zone = list->MR_zones_head;
|
|
n_entries += (MR_TrailEntry *) zone->MR_zone_end
|
|
- (MR_TrailEntry *) zone->MR_zone_min;
|
|
list = list->MR_zones_tail;
|
|
}
|
|
#endif /* MR_TRAIL_SEGMENTS */
|
|
|
|
n_entries += MR_trail_ptr - MR_TRAIL_BASE;
|
|
|
|
return n_entries;
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
void
|
|
MR_reset_trail(void)
|
|
{
|
|
#if defined(MR_TRAIL_SEGMENTS)
|
|
while (MR_PREV_TRAIL_ZONES != NULL) {
|
|
MR_reset_trail_zone();
|
|
MR_pop_trail_segment();
|
|
}
|
|
#endif
|
|
|
|
MR_reset_trail_zone();
|
|
|
|
#if defined(MR_CONSERVATIVE_GC)
|
|
MR_clear_zone_for_GC(MR_TRAIL_ZONE, MR_trail_ptr);
|
|
#endif
|
|
|
|
MR_ticket_counter = 1;
|
|
MR_ticket_high_water = 1;
|
|
}
|
|
|
|
static void
|
|
MR_reset_trail_zone(void) {
|
|
|
|
MR_TrailEntry *tr_ptr;
|
|
MR_TrailEntry *tr_base;
|
|
|
|
tr_ptr = MR_trail_ptr;
|
|
tr_base = MR_TRAIL_BASE;
|
|
|
|
while (tr_ptr != tr_base) {
|
|
tr_ptr--;
|
|
if (MR_get_trail_entry_kind(tr_ptr) == MR_func_entry) {
|
|
(*MR_get_trail_entry_untrail_func(tr_ptr))(
|
|
MR_get_trail_entry_datum(tr_ptr), MR_gc);
|
|
}
|
|
}
|
|
MR_trail_ptr = tr_base;
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
#if defined(MR_TRAIL_SEGMENTS)
|
|
void
|
|
MR_new_trail_segment(void)
|
|
{
|
|
|
|
MR_MemoryZones *list;
|
|
MR_MemoryZone *new_zone;
|
|
MR_TrailEntry *old_trail_ptr;
|
|
|
|
old_trail_ptr = MR_trail_ptr;
|
|
|
|
/*
|
|
** We perform explicit overflow checks so redzones just waste space.
|
|
*/
|
|
new_zone = MR_create_or_reuse_zone("trail_segment", MR_trail_size, 0,
|
|
0, MR_default_handler);
|
|
|
|
list = MR_GC_malloc_uncollectable_attrib(sizeof(MR_MemoryZones),
|
|
MR_ALLOC_SITE_RUNTIME);
|
|
|
|
#if defined(MR_DEBUG_TRAIL_SEGMENTS)
|
|
printf("create new trail segment: old zone: %p, old trail_ptr %p\n",
|
|
MR_TRAIL_ZONE, MR_trail_ptr);
|
|
#endif
|
|
|
|
list->MR_zones_head = MR_TRAIL_ZONE;
|
|
list->MR_zones_tail = MR_PREV_TRAIL_ZONES;
|
|
MR_PREV_TRAIL_ZONES = list;
|
|
MR_TRAIL_ZONE = new_zone;
|
|
MR_trail_ptr = (MR_TrailEntry *) MR_TRAIL_ZONE->MR_zone_min;
|
|
|
|
#if defined(MR_DEBUG_TRAIL_SEGMENTS)
|
|
printf("create new trail segment: new zone: %p, new trail_ptr %p\n",
|
|
MR_TRAIL_ZONE, MR_trail_ptr);
|
|
#endif
|
|
}
|
|
|
|
static void
|
|
MR_pop_trail_segment(void)
|
|
{
|
|
|
|
MR_MemoryZones *list;
|
|
|
|
#if defined(MR_DEBUG_TRAIL_SEGMENTS)
|
|
printf("restore old trail segment: old zone %p, old trail_ptr %p\n",
|
|
MR_TRAIL_ZONE, MR_trail_ptr);
|
|
#endif
|
|
|
|
MR_release_zone(MR_TRAIL_ZONE);
|
|
|
|
list = MR_PREV_TRAIL_ZONES;
|
|
MR_TRAIL_ZONE = list->MR_zones_head;
|
|
MR_PREV_TRAIL_ZONES = list->MR_zones_tail;
|
|
MR_trail_ptr = (MR_TrailEntry *) MR_TRAIL_ZONE->MR_zone_end;
|
|
MR_GC_free(list);
|
|
|
|
#if defined(MR_DEBUG_TRAIL_SEGMENTS)
|
|
printf("restore old trail segment: new zone %p, new trail_ptr %p\n",
|
|
MR_TRAIL_ZONE, MR_trail_ptr);
|
|
#endif
|
|
}
|
|
|
|
MR_Unsigned
|
|
MR_num_trail_segments(void)
|
|
{
|
|
MR_Unsigned n_segments = 1;
|
|
MR_MemoryZones *list;
|
|
|
|
list = MR_PREV_TRAIL_ZONES;
|
|
while (list != NULL) {
|
|
n_segments++;
|
|
list = list->MR_zones_tail;
|
|
}
|
|
|
|
return n_segments;
|
|
}
|
|
|
|
#endif /* MR_TRAIL_SEGMENTS */
|
|
|
|
#endif /* MR_USE_TRAIL */
|
|
/*---------------------------------------------------------------------------*/
|