nitc.nitc :: memory_logger $ AbstractCompiler
Singleton that store the knowledge about the compilation processnitc :: memory_logger $ AbstractCompilerVisitor
A visitor on the AST of property definition that generate the C code.nitc :: memory_logger $ AbstractCompiler
Singleton that store the knowledge about the compilation processnitc :: memory_logger $ AbstractCompilerVisitor
A visitor on the AST of property definition that generate the C code.nitc :: astbuilder
Instantiation and transformation of semantic nodes in the AST of expressions and statementsSerializable::inspect to show more useful information
			nitc :: modelbuilder
more_collections :: more_collections
Highly specific, but useful, collections-related classes.serialization :: serialization_core
Abstract services to serialize Nit objects to different formatsnitc :: toolcontext
Common command-line tool infrastructure than handle options and error messagescore :: union_find
union–find algorithm using an efficient disjoint-set data structure
# Extension to inject memory-tracing instrumentation in code generated by `nitc`.
module memory_logger
import abstract_compiler
redef class ToolContext
	# --trace-memory
	var opt_trace_memory = new OptionBool("Enable dynamic measure of memory usage", "--trace-memory")
	init do
		self.option_context.add_option opt_trace_memory
	end
end
redef class AbstractCompiler
	redef fun compile_before_main(v)
	do
		super
		if not modelbuilder.toolcontext.opt_trace_memory.value then return
		header.add_decl("#include <time.h>")
		header.add_decl("extern FILE *mlog;")
		header.add_decl("extern struct timespec mlog_last;")
		header.add_decl("extern struct timespec mlog_time0;")
		v.add_decl("FILE *mlog;")
		v.add_decl("struct timespec mlog_last;")
		v.add_decl("struct timespec mlog_time0;")
	end
	redef fun compile_begin_main(v)
	do
		super
		if not modelbuilder.toolcontext.opt_trace_memory.value then return
		v.add("mlog = fopen(\"memory.log\", \"w\");")
		v.add("clock_gettime(CLOCK_MONOTONIC, &mlog_time0);")
	end
end
redef class AbstractCompilerVisitor
	redef fun nit_alloc(size, tag)
	do
		if not compiler.modelbuilder.toolcontext.opt_trace_memory.value then return super
		# Log time each 10ms (ie 1e7ns)
		var tw = get_name("mtw")
		add_decl("struct timespec {tw};")
		add("clock_gettime(CLOCK_MONOTONIC, &{tw});")
		add("if(mlog_last.tv_sec < {tw}.tv_sec || mlog_last.tv_nsec + 1e7 < {tw}.tv_nsec) \{")
		add("mlog_last = {tw};")
		add("fprintf(mlog, \"# %f\\n\", 1000.0*{tw}.tv_sec + 1e-6*{tw}.tv_nsec - (1000.0*mlog_time0.tv_sec + 1e-6*mlog_time0.tv_nsec));")
		add("\}")
		# Print size and tag the mlog
		var str = "\"+\\t%d\\t%s\\n\", {size}, \"{tag or else "?"}\""
		add("fprintf(mlog, {str});")
		return super
	end
end
src/compiler/memory_logger.nit:15,1--76,3