static void   clear_cleaner_statistics   (Heap* heap)  {
    //        ============================
    //
    ZERO_BIGCOUNTER( &heap->total_bytes_allocated );
    //
    for     (int age = 0;  age < MAX_AGEGROUPS;   ++age) {
	for (int ilk = 0;  ilk < MAX_PLAIN_ILKS;  ++ilk) {
	    //
	    ZERO_BIGCOUNTER( &heap->total_bytes_copied_to_sib[ age ][ ilk ] );
	}
    }
}
void   set_up_heap   (			// Create and initialize the heap.
    // ===========
    //
    Task*              task,
    Bool               is_boot,
    Heapcleaner_Args*  params
) {
    int		ratio;
    int		max_size = 0;		// Initialized only to suppress a gcc -Wall warning.

    Heap*	heap;
    Agegroup*	ag;

    Multipage_Ram_Region*  multipage_ram_region;

    Val* agegroup0_buffer;

    // Default any parameters unspecified by user:
    //
    if (params->agegroup0_buffer_bytesize == 0)  params->agegroup0_buffer_bytesize  = DEFAULT_AGEGROUP0_BUFFER_BYTESIZE;		// From   src/c/h/runtime-configuration.h
    if (params->active_agegroups           < 0)  params->active_agegroups           = DEFAULT_ACTIVE_AGEGROUPS;				// From   src/c/h/runtime-configuration.h

    if (params->oldest_agegroup_keeping_idle_fromspace_buffers < 0) {
        params->oldest_agegroup_keeping_idle_fromspace_buffers =  DEFAULT_OLDEST_AGEGROUP_KEEPING_IDLE_FROMSPACE_BUFFERS;		// From   src/c/h/runtime-configuration.h
    }

    // First we initialize the underlying memory system:
    //
    set_up_multipage_ram_region_os_interface ();				// set_up_multipage_ram_region_os_interface	def in   src/c/ram/get-multipage-ram-region-from-mach.c
										// set_up_multipage_ram_region_os_interface	def in   src/c/ram/get-multipage-ram-region-from-mmap.c
										// set_up_multipage_ram_region_os_interface	def in   src/c/ram/get-multipage-ram-region-from-win32.c

    // Allocate a ram region to hold
    // the book_to_sibid__global and agegroup0 buffer:
    //
    {   long	book2sibid_bytesize;

	#ifdef TWO_LEVEL_MAP
	    #error two level map not supported
	#else
		book2sibid_bytesize = BOOK2SIBID_TABLE_SIZE_IN_SLOTS * sizeof( Sibid );
	#endif

	multipage_ram_region
	    =
            obtain_multipage_ram_region_from_os(
		//
		MAX_PTHREADS * params->agegroup0_buffer_bytesize
                +
                book2sibid_bytesize
           );

	if (multipage_ram_region == NULL) 	   die ("Unable to allocate ram region for book_to_sibid__global");

	book_to_sibid__global = (Sibid*) BASE_ADDRESS_OF_MULTIPAGE_RAM_REGION( multipage_ram_region );

	agegroup0_buffer = (Val*) (((Punt)book_to_sibid__global) + book2sibid_bytesize);
    }

    // Initialize the book_to_sibid__global:
    //
    #ifdef TWO_LEVEL_MAP
        #error two level map not supported
    #else
	for (int i = 0;  i < BOOK2SIBID_TABLE_SIZE_IN_SLOTS;  i++) {
	    //
	    book_to_sibid__global[ i ] = UNMAPPED_BOOK_SIBID;
	}
    #endif

    // Initialize heap descriptor:
    //
    heap = MALLOC_CHUNK(Heap);
    //
    memset ((char*)heap, 0, sizeof(Heap));
    //
    for (int age = 0;  age < MAX_AGEGROUPS;  age++) {
	//
	ratio = DfltRatios[age];

	if (age == 0) {   max_size = MAX_SZ1( params->agegroup0_buffer_bytesize * MAX_PTHREADS );
	} else {          max_size = (5 * max_size)/2;
	    //
	    if (max_size > 64 * ONE_MEG_BINARY)  {				// WTF? This silliness probably needs to Just Die.  XXX BUGGO FIXME. -- 2011-11-01 CrT
                max_size = 64 * ONE_MEG_BINARY;
	    }
	}

	ag		      =
	heap->agegroup[age]   =  MALLOC_CHUNK( Agegroup );

	ag->heap	= heap;
	ag->age	        = age+1;
	ag->cleanings	= 0;
	ag->ratio	= ratio;
	//
	ag->last_cleaning_count_of_younger_agegroup
	    =
	    0;
	//
	ag->tospace_ram_region			= NULL;
	ag->fromspace_ram_region		= NULL;
	ag->saved_fromspace_ram_region		= NULL;
	ag->coarse_inter_agegroup_pointers_map	= NULL;

	for (int ilk = 0;  ilk < MAX_PLAIN_ILKS;  ilk++) {			// MAX_PLAIN_ILKS		def in    src/c/h/sibid.h
	    //
	    ag->sib[ ilk ] = MALLOC_CHUNK( Sib );
	    //
	    ag->sib[ ilk ]->tospace_bytesize              = 0;
	    ag->sib[ ilk ]->requested_sib_buffer_bytesize = 0;
	    ag->sib[ ilk ]->soft_max_bytesize             = max_size;
	    //
	    ag->sib[ ilk ]->id =   MAKE_SIBID( age+1, ilk+1, 0);
	}
	for (int ilk = 0;  ilk < MAX_HUGE_ILKS;  ilk++) {			// MAX_HUGE_ILKS		def in    src/c/h/sibid.h
	    //
	    ag->hugechunks[ ilk ] = NULL;					// ilk = 0 == CODE__HUGE_ILK	def in    src/c/h/sibid.h
	}
    }

    for (int age = 0;   age < params->active_agegroups;   age++) {
	//
	int k = (age == params->active_agegroups -1)
                     ?  age
                     :  age+1;

	for (int ilk = 0;  ilk < MAX_PLAIN_ILKS;  ilk++) {
	    //
	    heap->agegroup[ age ]->sib[ ilk ]->sib_for_promoted_chunks
                =
                heap->agegroup[ k ]->sib[ ilk ];
	}
    }

    heap->oldest_agegroup_keeping_idle_fromspace_buffers
	=
	params->oldest_agegroup_keeping_idle_fromspace_buffers;

    heap->active_agegroups                    = params->active_agegroups;
    //
    heap->agegroup0_cleanings_done            = 0;
    heap->hugechunk_ramregion_count	      = 0;
    heap->hugechunk_ramregions		      = NULL;
    //
    heap->hugechunk_freelist		      = MALLOC_CHUNK( Hugechunk );
    heap->hugechunk_freelist->chunk	      = (Punt)0;
    //
    heap->hugechunk_freelist->bytesize   = 0;
    heap->hugechunk_freelist->hugechunk_state = FREE_HUGECHUNK;
    heap->hugechunk_freelist->prev	      = heap->hugechunk_freelist;
    //
    heap->hugechunk_freelist->next	      = heap->hugechunk_freelist;
    heap->weak_pointers_forwarded_during_cleaning		    = NULL;

    // Initialize new space:
    //
    heap->multipage_ram_region       =  multipage_ram_region;
    //
    heap->agegroup0_buffer           =  agegroup0_buffer;
    //
    heap->agegroup0_buffer_bytesize  =  params->agegroup0_buffer_bytesize
				      * MAX_PTHREADS;				// "* MAX_PTHREADS" because it gets partitioned into MAX_PTHREADS buffers by
										// partition_agegroup0_buffer_between_pthreads() in   src/c/heapcleaner/pthread-heapcleaner-stuff.c
    //
    set_book2sibid_entries_for_range (
	//
	book_to_sibid__global,
	(Val*) book_to_sibid__global,
	BYTESIZE_OF_MULTIPAGE_RAM_REGION( heap->multipage_ram_region ),
	NEWSPACE_SIBID
    );

    #ifdef VERBOSE
	debug_say ("NewSpace = [%#x, %#x:%#x), %d bytes\n",
	    heap->agegroup0_buffer, HEAP_ALLOCATION_LIMIT( heap ),
	    (Val_Sized_Unt)(heap->agegroup0_buffer)+params->agegroup0_buffer_bytesize, params->agegroup0_buffer_bytesize);
    #endif

    clear_cleaner_statistics( heap );										// clear_cleaner_statistics		def in   src/c/heapcleaner/heapcleaner-initialization.c


    //
    if (heapcleaner_statistics_fd__global > 0) {
	//	
      Cleaner_Statistics_Header   header;									// Cleaner_Statistics_Header		is from   src/c/h/heapcleaner-statistics-2.h
	//
	ZERO_BIGCOUNTER( &heap->total_bytes_allocated );
	//
	header.mask = STATMASK_ALLOC
		    | STATMASK_NGENS
		    | STATMASK_START
		    | STATMASK_STOP;

	header.is_new_runtime = 1;
	//
	header.agegroup0_buffer_bytesize = params->agegroup0_buffer_bytesize;
	header.active_agegroups        = params->active_agegroups;
	//
	{   struct timeval tv;
	    //
	    gettimeofday ( &tv, NULL);
	    //
	    header.start_time.seconds  =  tv.tv_sec;	
	    header.start_time.uSeconds =  tv.tv_usec;	
	};
	//
	write( heapcleaner_statistics_fd__global, (char*)&header, sizeof( Cleaner_Statistics_Header ) );
    }


    if (is_boot) {
	//
	// Create agegroup 1's to-space:
	//
        for (int ilk = 0;  ilk < MAX_PLAIN_ILKS;  ilk++) {
	    //
	    heap->agegroup[ 0 ]->sib[ ilk ]->tospace_bytesize
                =
                BOOKROUNDED_BYTESIZE( 2 * heap->agegroup0_buffer_bytesize );
	}

	if (allocate_and_partition_an_agegroup( heap->agegroup[0] ) == FAILURE)	    die ("unable to allocate initial agegroup 1 buffer\n");

	for (int ilk = 0;  ilk < MAX_PLAIN_ILKS;  ilk++) {
	    //
	    heap->agegroup[ 0 ]->sib[ ilk ]->end_of_fromspace_oldstuff
		=
		heap->agegroup[ 0 ]->sib[ ilk ]->tospace;
	}
    }

    // Initialize the cleaner-related
    // parts of the Mythryl state:
    //
    task->heap	                  =  heap;
    task->heap_allocation_pointer =  (Val*) task->heap->agegroup0_buffer;

    #if NEED_SOFTWARE_GENERATED_PERIODIC_EVENTS
	//
	reset_heap_allocation_limit_for_software_generated_periodic_events( task );
    #else
	task->heap_allocation_limit = HEAP_ALLOCATION_LIMIT( heap );
    #endif
}										// fun set_up_heap
Exemplo n.º 3
0
void   set_up_heap   (			// Create and initialize the heap.
    // ===========
    //
    Task*              task,
    Bool               is_boot,
    Heapcleaner_Args*  params
) {
    // We are called (only) from
    //
    //     make_task()
    // in
    //     src/c/main/runtime-state.c

    int		max_size = 0;		// Initialized only to suppress a gcc -Wall warning.

    Heap*	heap;
    Agegroup*	ag;

    Quire*  quire;

    Val* agegroup0_master_buffer;

    // Default any parameters unspecified by user:
    //
    if (params->agegroup0_buffer_bytesize == 0)  params->agegroup0_buffer_bytesize  = DEFAULT_AGEGROUP0_BUFFER_BYTESIZE;		// From   src/c/h/runtime-configuration.h
    if (params->active_agegroups           < 0)  params->active_agegroups           = DEFAULT_ACTIVE_AGEGROUPS;				// From   src/c/h/runtime-configuration.h

    if (params->oldest_agegroup_retaining_fromspace_sibs_between_heapcleanings < 0) {
        params->oldest_agegroup_retaining_fromspace_sibs_between_heapcleanings =  DEFAULT_OLDEST_AGEGROUP_RETAINING_FROMSPACE_SIBS_BETWEEN_HEAPCLEANINGS;		// From   src/c/h/runtime-configuration.h
    }

    // First we initialize the underlying memory system:
    //
    set_up_quire_os_interface ();						// set_up_quire_os_interface	def in   src/c/ram/get-quire-from-mach.c
										// set_up_quire_os_interface	def in   src/c/ram/get-quire-from-mmap.c
										// set_up_quire_os_interface	def in   src/c/ram/get-quire-from-win32.c

    // Allocate a ram region to hold
    // the book_to_sibid__global and agegroup0 buffer:
    //
    {   long	book2sibid_bytesize;

	#ifdef TWO_LEVEL_MAP
	    #error two level map not supported
	#else
		book2sibid_bytesize = BOOK2SIBID_TABLE_SIZE_IN_SLOTS * sizeof( Sibid );
	#endif

	quire = obtain_quire_from_os(
		    //
		    MAX_HOSTTHREADS * params->agegroup0_buffer_bytesize
		    +
		    book2sibid_bytesize
	       );

	if (quire == NULL) 	   die ("Unable to allocate ram region for book_to_sibid__global");

	book_to_sibid__global = (Sibid*) BASE_ADDRESS_OF_QUIRE( quire );

	agegroup0_master_buffer = (Val*) (((Vunt)book_to_sibid__global) + book2sibid_bytesize);
    }

    // Initialize the book_to_sibid__global:
    //
    #ifdef TWO_LEVEL_MAP
        #error two level map not supported
    #else
	for (int i = 0;  i < BOOK2SIBID_TABLE_SIZE_IN_SLOTS;  i++) {
	    //
	    book_to_sibid__global[ i ] = UNMAPPED_BOOK_SIBID;
	}
    #endif

    // Initialize heap descriptor:
    //
    heap = MALLOC_CHUNK(Heap);
    //
    memset ((char*)heap, 0, sizeof(Heap));
    //
    for (int age = 0;  age < MAX_AGEGROUPS;  age++) {
	//
	if (age == 0) {   max_size = MAX_SZ1( params->agegroup0_buffer_bytesize * MAX_HOSTTHREADS );	// MAX_SZ1 just multiplies by 6 (why??) -- def in  src/c/h/runtime-configuration.h
	} else {          max_size = (5 * max_size)/2;
	    //
	    if (max_size > 64 * ONE_MEG_BINARY)  {				// WTF? This silliness probably needs to Just Die.  XXX BUGGO FIXME. -- 2011-11-01 CrT
                max_size = 64 * ONE_MEG_BINARY;
	    }
	}

	ag			=
	heap->agegroup[ age ]	=  MALLOC_CHUNK( Agegroup );

	ag->heap		=  heap;
	ag->age			=  age + 1;
	ag->heapcleanings_count	=  0;

	ag->target_heapcleaning_frequency_ratio
	    =
	    default_agegroup_size_ratio__local[ age ];

	ag->heapcleanings_count_of_younger_agegroup_during_last_heapcleaning
	    =
	    0;
	//
	ag->tospace_quire			= NULL;
	ag->fromspace_quire		= NULL;
	ag->retained_fromspace_quire		= NULL;
	ag->coarse_inter_agegroup_pointers_map	= NULL;

	for (int s = 0;  s < MAX_PLAIN_SIBS;  s++) {				// MAX_PLAIN_SIBS		def in    src/c/h/sibid.h
	    //
	    ag->sib[ s ] = MALLOC_CHUNK( Sib );
	    //
	    ag->sib[ s ]->tospace.bytesize              = 0;
	    ag->sib[ s ]->requested_extra_free_bytes = 0;
	    ag->sib[ s ]->soft_max_bytesize             = max_size;
	    //
	    ag->sib[ s ]->id =   MAKE_SIBID( age+1, s+1, 0);
	}
	for (int s = 0;  s < MAX_HUGE_SIBS;  s++) {				// MAX_HUGE_SIBS		def in    src/c/h/sibid.h
	    //
	    ag->hugechunks[ s ] = NULL;						// s = 0 == CODE__HUGE_SIB	def in    src/c/h/sibid.h
	}
    }

    for (int a = 0;   a < params->active_agegroups;   a++) {
	//
	int k = (a == params->active_agegroups -1)
                     ?  a
                     :  a+1;

	for (int s = 0;  s < MAX_PLAIN_SIBS;  s++) {
	    //
	    heap->agegroup[ a ]->sib[ s ]->sib_for_promoted_chunks
                =
                heap->agegroup[ k ]->sib[ s ];

	    heap->agegroup[ a ]->sib[ s ]->current_bytes_in_use  = 0;		// This field is only for display/debugging; it plays no algorithmic role.
	    heap->agegroup[ a ]->sib[ s ]->previous_bytes_in_use = 0;		// This field is only for display/debugging; it plays no algorithmic role.
	}
    }

    heap->oldest_agegroup_retaining_fromspace_sibs_between_heapcleanings
	=
	params->oldest_agegroup_retaining_fromspace_sibs_between_heapcleanings;

    heap->active_agegroups			= params->active_agegroups;
    //
    heap->agegroup0_heapcleanings_count		= 0;
    heap->hugechunk_quire_count			= 0;
    heap->hugechunk_quires			= NULL;
    //
    heap->hugechunk_freelist			= MALLOC_CHUNK( Hugechunk );
    heap->hugechunk_freelist->chunk		= (Vunt)0;
    //
    heap->hugechunk_freelist->bytesize		= 0;
    heap->hugechunk_freelist->hugechunk_state	= FREE_HUGECHUNK;
    heap->hugechunk_freelist->prev		= heap->hugechunk_freelist;
    //
    heap->hugechunk_freelist->next		= heap->hugechunk_freelist;
    //
    heap->weakrefs_forwarded_during_heapcleaning		= NULL;

    // Initialize new space:
    //
    heap->quire       =  quire;
    //
    heap->agegroup0_master_buffer    =  agegroup0_master_buffer;
    //
    heap->agegroup0_master_buffer_bytesize
        =
        params->agegroup0_buffer_bytesize * MAX_HOSTTHREADS;			// "* MAX_HOSTTHREADS" because it gets partitioned into MAX_HOSTTHREADS separate buffers by
										// partition_agegroup0_buffer_between_hostthreads() in   src/c/heapcleaner/hostthread-heapcleaner-stuff.c
    //
    set_book2sibid_entries_for_range (
	//
	book_to_sibid__global,
	(Val*) book_to_sibid__global,
	BYTESIZE_OF_QUIRE( heap->quire ),
	AGEGROUP0_SIBID
    );


    clear_heapcleaner_statistics( heap );


    //
    if (heapcleaner_statistics_fd__global > 0) {
	//	
      Cleaner_Statistics_Header   header;									// Cleaner_Statistics_Header		is from   src/c/h/heapcleaner-statistics-2.h
	//
	ZERO_BIGCOUNTER( &heap->total_bytes_allocated );
	//
	header.mask = STATMASK_ALLOC
		    | STATMASK_NGENS
		    | STATMASK_START
		    | STATMASK_STOP;

	header.is_new_runtime = 1;
	//
	header.agegroup0_buffer_bytesize = params->agegroup0_buffer_bytesize;
	header.active_agegroups        = params->active_agegroups;
	//
	{   struct timeval tv;
	    //
	    gettimeofday ( &tv, NULL);
	    //
	    header.start_time.seconds  =  tv.tv_sec;	
	    header.start_time.uSeconds =  tv.tv_usec;	
	};
	//
	write( heapcleaner_statistics_fd__global, (char*)&header, sizeof( Cleaner_Statistics_Header ) );
    }


    if (is_boot) {
	//
	// Create agegroup 1's to-space:
	//
        for (int s = 0;  s < MAX_PLAIN_SIBS;  s++) {
	    //
	    heap->agegroup[ 0 ]->sib[ s ]->tospace.bytesize
                =
                BOOKROUNDED_BYTESIZE( 2 * (heap->agegroup0_master_buffer_bytesize / MAX_HOSTTHREADS) );
	}

	if (set_up_tospace_sib_buffers_for_agegroup( heap->agegroup[0] ) == FALSE)	    die ("unable to allocate initial agegroup 1 buffer\n");

	for (int s = 0;  s < MAX_PLAIN_SIBS;  s++) {
	    //
	    heap->agegroup[ 0 ]->sib[ s ]->fromspace.seniorchunks_end
		=
		heap->agegroup[ 0 ]->sib[ s ]->tospace.start;
	}
    }

    // Initialize the cleaner-related
    // parts of the Mythryl state:
    //
    task->heap	                          =  heap;
    task->heap_allocation_buffer          =  task->heap->agegroup0_master_buffer;
    task->heap_allocation_pointer         =  task->heap->agegroup0_master_buffer;
    task->heap_allocation_buffer_bytesize =  heap->agegroup0_master_buffer_bytesize / MAX_HOSTTHREADS; 

    #if NEED_SOFTWARE_GENERATED_PERIODIC_EVENTS
	//
	reset_heap_allocation_limit_for_software_generated_periodic_events( task );
    #else
	task->heap_allocation_limit
	    =
	    HEAP_ALLOCATION_LIMIT( task );
    #endif
}										// fun set_up_heap