Files
Aurora.3/code/controllers/Processes/garbage.dm
skull132 e9b688e1f6 Merge branch 'development-2' into development
Christ on a stick. SO MANY ERRORS. REEE.

# Conflicts:
#	baystation12.dme
#	code/_helpers/lists.dm
#	code/_helpers/logging.dm
#	code/_helpers/text.dm
#	code/_onclick/click.dm
#	code/controllers/configuration.dm
#	code/controllers/master_controller.dm
#	code/datums/supplypacks.dm
#	code/game/antagonist/antagonist.dm
#	code/game/antagonist/antagonist_print.dm
#	code/game/antagonist/outsider/commando.dm
#	code/game/antagonist/outsider/ninja.dm
#	code/game/area/areas.dm
#	code/game/gamemodes/cult/cult_items.dm
#	code/game/gamemodes/game_mode.dm
#	code/game/jobs/access.dm
#	code/game/machinery/atmoalter/canister.dm
#	code/game/machinery/autolathe.dm
#	code/game/machinery/doors/airlock.dm
#	code/game/machinery/recharger.dm
#	code/game/machinery/suit_storage_unit.dm
#	code/game/mecha/mech_fabricator.dm
#	code/game/mecha/mecha.dm
#	code/game/objects/effects/spiders.dm
#	code/game/objects/items.dm
#	code/game/objects/items/devices/PDA/PDA.dm
#	code/game/objects/items/devices/flash.dm
#	code/game/objects/items/devices/lightreplacer.dm
#	code/game/objects/items/devices/paicard.dm
#	code/game/objects/items/devices/scanners.dm
#	code/game/objects/items/devices/suit_cooling.dm
#	code/game/objects/items/devices/uplink.dm
#	code/game/objects/items/robot/robot_upgrades.dm
#	code/game/objects/items/toys.dm
#	code/game/objects/items/weapons/cards_ids.dm
#	code/game/objects/items/weapons/handcuffs.dm
#	code/game/objects/items/weapons/manuals.dm
#	code/game/objects/items/weapons/material/kitchen.dm
#	code/game/objects/items/weapons/material/misc.dm
#	code/game/objects/items/weapons/material/swords.dm
#	code/game/objects/items/weapons/melee/energy.dm
#	code/game/objects/items/weapons/melee/misc.dm
#	code/game/objects/items/weapons/scrolls.dm
#	code/game/objects/items/weapons/storage/belt.dm
#	code/game/objects/items/weapons/stunbaton.dm
#	code/game/objects/items/weapons/tools.dm
#	code/game/objects/objs.dm
#	code/game/objects/structures/crates_lockers/closets.dm
#	code/game/objects/structures/crates_lockers/closets/secure/security.dm
#	code/game/objects/structures/janicart.dm
#	code/game/sound.dm
#	code/game/turfs/simulated.dm
#	code/game/verbs/ooc.dm
#	code/global.dm
#	code/modules/admin/verbs/debug.dm
#	code/modules/admin/verbs/modifyvariables.dm
#	code/modules/client/client procs.dm
#	code/modules/client/preferences.dm
#	code/modules/clothing/clothing.dm
#	code/modules/clothing/head/hardhat.dm
#	code/modules/clothing/head/helmet.dm
#	code/modules/clothing/head/jobs.dm
#	code/modules/clothing/head/misc_special.dm
#	code/modules/clothing/shoes/jobs.dm
#	code/modules/clothing/spacesuits/alien.dm
#	code/modules/clothing/spacesuits/captain.dm
#	code/modules/clothing/spacesuits/miscellaneous.dm
#	code/modules/clothing/spacesuits/rig/rig_pieces.dm
#	code/modules/clothing/spacesuits/rig/suits/alien.dm
#	code/modules/clothing/spacesuits/spacesuits.dm
#	code/modules/clothing/spacesuits/void/merc.dm
#	code/modules/clothing/spacesuits/void/void.dm
#	code/modules/clothing/suits/armor.dm
#	code/modules/clothing/suits/jobs.dm
#	code/modules/clothing/suits/storage.dm
#	code/modules/clothing/suits/utility.dm
#	code/modules/clothing/suits/wiz_robe.dm
#	code/modules/clothing/under/jobs/security.dm
#	code/modules/economy/Events.dm
#	code/modules/economy/Events_Mundane.dm
#	code/modules/economy/economy_misc.dm
#	code/modules/events/blob.dm
#	code/modules/events/event.dm
#	code/modules/events/event_container.dm
#	code/modules/events/event_manager.dm
#	code/modules/events/money_lotto.dm
#	code/modules/events/prison_break.dm
#	code/modules/events/spacevine.dm
#	code/modules/hydroponics/trays/tray.dm
#	code/modules/mob/dead/observer/observer.dm
#	code/modules/mob/emote.dm
#	code/modules/mob/holder.dm
#	code/modules/mob/language/station.dm
#	code/modules/mob/living/bot/cleanbot.dm
#	code/modules/mob/living/carbon/alien/diona/diona.dm
#	code/modules/mob/living/carbon/alien/diona/diona_attacks.dm
#	code/modules/mob/living/carbon/give.dm
#	code/modules/mob/living/carbon/human/emote.dm
#	code/modules/mob/living/carbon/human/human.dm
#	code/modules/mob/living/carbon/human/human_defense.dm
#	code/modules/mob/living/carbon/human/inventory.dm
#	code/modules/mob/living/carbon/human/life.dm
#	code/modules/mob/living/carbon/human/species/outsider/vox.dm
#	code/modules/mob/living/carbon/human/species/station/golem.dm
#	code/modules/mob/living/carbon/human/species/station/station.dm
#	code/modules/mob/living/carbon/human/update_icons.dm
#	code/modules/mob/living/carbon/metroid/metroid.dm
#	code/modules/mob/living/living.dm
#	code/modules/mob/living/living_defense.dm
#	code/modules/mob/living/living_defines.dm
#	code/modules/mob/living/silicon/ai/ai.dm
#	code/modules/mob/living/silicon/pai/admin.dm
#	code/modules/mob/living/silicon/pai/pai.dm
#	code/modules/mob/living/silicon/robot/drone/drone.dm
#	code/modules/mob/living/silicon/robot/drone/drone_manufacturer.dm
#	code/modules/mob/living/silicon/robot/emote.dm
#	code/modules/mob/living/silicon/robot/robot_items.dm
#	code/modules/mob/living/silicon/robot/robot_modules.dm
#	code/modules/mob/living/silicon/silicon.dm
#	code/modules/mob/living/simple_animal/bees.dm
#	code/modules/mob/living/simple_animal/friendly/cat.dm
#	code/modules/mob/living/simple_animal/friendly/corgi.dm
#	code/modules/mob/living/simple_animal/friendly/farm_animals.dm
#	code/modules/mob/living/simple_animal/friendly/mouse.dm
#	code/modules/mob/living/simple_animal/friendly/spiderbot.dm
#	code/modules/mob/living/simple_animal/hostile/hostile.dm
#	code/modules/mob/living/simple_animal/simple_animal.dm
#	code/modules/mob/logout.dm
#	code/modules/mob/mob.dm
#	code/modules/mob/mob_grab_specials.dm
#	code/modules/mob/mob_helpers.dm
#	code/modules/mob/new_player/sprite_accessories.dm
#	code/modules/organs/organ.dm
#	code/modules/organs/organ_alien.dm
#	code/modules/organs/organ_external.dm
#	code/modules/paperwork/faxmachine.dm
#	code/modules/projectiles/ammunition/boxes.dm
#	code/modules/projectiles/ammunition/bullets.dm
#	code/modules/projectiles/guns/energy/nuclear.dm
#	code/modules/projectiles/guns/energy/rifle.dm
#	code/modules/projectiles/guns/energy/special.dm
#	code/modules/projectiles/guns/projectile.dm
#	code/modules/projectiles/guns/projectile/automatic.dm
#	code/modules/projectiles/guns/projectile/pistol.dm
#	code/modules/projectiles/guns/projectile/revolver.dm
#	code/modules/projectiles/guns/projectile/shotgun.dm
#	code/modules/projectiles/projectile/bullets.dm
#	code/modules/projectiles/projectile/special.dm
#	code/modules/reagents/reagent_containers.dm
#	code/modules/reagents/reagent_containers/food/drinks.dm
#	code/modules/research/designs.dm
#	code/modules/research/destructive_analyzer.dm
#	code/modules/research/rdconsole.dm
#	code/modules/spells/artifacts.dm
#	code/modules/spells/spellbook.dm
#	code/modules/tables/tables.dm
#	code/world.dm
#	config/example/config.txt
#	icons/mob/items_lefthand.dmi
#	icons/mob/items_righthand.dmi
#	icons/obj/lighting.dmi
2016-10-31 00:01:52 +02:00

260 lines
8.0 KiB
Plaintext

// The time a datum was destroyed by the GC, or null if it hasn't been
/datum/var/gcDestroyed
#define GC_COLLECTIONS_PER_RUN 300
#define GC_COLLECTION_TIMEOUT (30 SECONDS)
#define GC_FORCE_DEL_PER_RUN 30
var/datum/controller/process/garbage_collector/garbage_collector
var/list/delayed_garbage = list()
/datum/controller/process/garbage_collector
var/garbage_collect = 1 // Whether or not to actually do work
var/total_dels = 0 // number of total del()'s
var/tick_dels = 0 // number of del()'s we've done this tick
var/soft_dels = 0
var/hard_dels = 0 // number of hard dels in total
var/list/destroyed = list() // list of refID's of things that should be garbage collected
// refID's are associated with the time at which they time out and need to be manually del()
// we do this so we aren't constantly locating them and preventing them from being gc'd
var/list/logging = list() // list of all types that have failed to GC associated with the number of times that's happened.
// the types are stored as strings
/datum/controller/process/garbage_collector/setup()
name = "garbage"
schedule_interval = 5 SECONDS
start_delay = 3
if(!garbage_collector)
garbage_collector = src
for(var/garbage in delayed_garbage)
qdel(garbage)
delayed_garbage.Cut()
delayed_garbage = null
#ifdef GC_FINDREF
world/loop_checks = 0
#endif
/datum/controller/process/garbage_collector/doWork()
if(!garbage_collect)
return
tick_dels = 0
var/time_to_kill = world.time - GC_COLLECTION_TIMEOUT
var/checkRemain = GC_COLLECTIONS_PER_RUN
var/remaining_force_dels = GC_FORCE_DEL_PER_RUN
while(destroyed.len && --checkRemain >= 0)
if(remaining_force_dels <= 0)
#ifdef GC_DEBUG
testing("GC: Reached max force dels per tick [dels] vs [maxDels]")
#endif
break // Server's already pretty pounded, everything else can wait 2 seconds
var/refID = destroyed[1]
var/GCd_at_time = destroyed[refID]
if(GCd_at_time > time_to_kill)
#ifdef GC_DEBUG
testing("GC: [refID] not old enough, breaking at [world.time] for [GCd_at_time - time_to_kill] deciseconds until [GCd_at_time + collection_timeout]")
#endif
break // Everything else is newer, skip them
var/datum/A = locate(refID)
#ifdef GC_DEBUG
testing("GC: [refID] old enough to test: GCd_at_time: [GCd_at_time] time_to_kill: [time_to_kill] current: [world.time]")
#endif
if(A && A.gcDestroyed == GCd_at_time) // So if something else coincidently gets the same ref, it's not deleted by mistake
// Something's still referring to the qdel'd object. Kill it.
log_hard_delete(A)
logging["[A.type]"]++
del(A)
hard_dels++
remaining_force_dels--
else
#ifdef GC_DEBUG
testing("GC: [refID] properly GC'd at [world.time] with timeout [GCd_at_time]")
#endif
soft_dels++
tick_dels++
total_dels++
destroyed.Cut(1, 2)
SCHECK
#undef GC_FORCE_DEL_PER_TICK
#undef GC_COLLECTION_TIMEOUT
#undef GC_COLLECTIONS_PER_TICK
#ifdef GC_FINDREF
/datum/controller/process/garbage_collector/proc/LookForRefs(var/datum/D, var/list/targ)
. = 0
for(var/V in D.vars)
if(V == "contents")
continue
if(istype(D.vars[V], /atom))
var/atom/A = D.vars[V]
if(A in targ)
testing("GC: [A] | [A.type] referenced by [D] | [D.type], var [V]")
. += 1
else if(islist(D.vars[V]))
. += LookForListRefs(D.vars[V], targ, D, V)
/datum/controller/process/garbage_collector/proc/LookForListRefs(var/list/L, var/list/targ, var/datum/D, var/V)
. = 0
for(var/F in L)
if(istype(F, /atom))
var/atom/A = F
if(A in targ)
testing("GC: [A] | [A.type] referenced by [D] | [D.type], list [V]")
. += 1
if(islist(F))
. += LookForListRefs(F, targ, D, "[F] in list [V]")
#endif
/datum/controller/process/garbage_collector/proc/AddTrash(datum/A)
if(!istype(A) || !isnull(A.gcDestroyed))
return
#ifdef GC_DEBUG
testing("GC: AddTrash(\ref[A] - [A.type])")
#endif
A.gcDestroyed = world.time
destroyed -= "\ref[A]" // Removing any previous references that were GC'd so that the current object will be at the end of the list.
destroyed["\ref[A]"] = world.time
/datum/controller/process/garbage_collector/statProcess()
..()
stat(null, "[garbage_collect ? "On" : "Off"], [destroyed.len] queued")
stat(null, "Dels: [total_dels], [soft_dels] soft, [hard_dels] hard, [tick_dels] last run")
// Tests if an atom has been deleted.
/proc/deleted(atom/A)
return !A || !isnull(A.gcDestroyed)
// Should be treated as a replacement for the 'del' keyword.
// Datums passed to this will be given a chance to clean up references to allow the GC to collect them.
/proc/qdel(var/datum/A)
if(!A)
return
if(!istype(A))
warning("qdel() passed object of type [A.type]. qdel() can only handle /datum types.")
crash_with("qdel() passed object of type [A.type]. qdel() can only handle /datum types.")
del(A)
if(garbage_collector)
garbage_collector.total_dels++
garbage_collector.hard_dels++
else if(isnull(A.gcDestroyed))
// Let our friend know they're about to get collected
. = !A.Destroy()
if(. && A)
A.finalize_qdel()
/datum/proc/finalize_qdel()
if(IsPooled(src))
PlaceInPool(src)
else
del(src)
/atom/finalize_qdel()
if(IsPooled(src))
PlaceInPool(src)
else
if(garbage_collector)
garbage_collector.AddTrash(src)
else
delayed_garbage |= src
/icon/finalize_qdel()
del(src)
/image/finalize_qdel()
del(src)
/mob/finalize_qdel()
del(src)
/turf/finalize_qdel()
del(src)
// Default implementation of clean-up code.
// This should be overridden to remove all references pointing to the object being destroyed.
// Return true if the the GC controller should allow the object to continue existing. (Useful if pooling objects.)
/datum/proc/Destroy()
nanomanager.close_uis(src)
tag = null
return
#ifdef TESTING
/client/var/running_find_references
/mob/verb/create_thing()
set category = "Debug"
set name = "Create Thing"
var/path = input("Enter path")
var/atom/thing = new path(loc)
thing.find_references()
/atom/verb/find_references()
set category = "Debug"
set name = "Find References"
set background = 1
set src in world
if(!usr || !usr.client)
return
if(usr.client.running_find_references)
testing("CANCELLED search for references to a [usr.client.running_find_references].")
usr.client.running_find_references = null
return
if(alert("Running this will create a lot of lag until it finishes. You can cancel it by running it again. Would you like to begin the search?", "Find References", "Yes", "No") == "No")
return
// Remove this object from the list of things to be auto-deleted.
if(garbage_collector)
garbage_collector.destroyed -= "\ref[src]"
usr.client.running_find_references = type
testing("Beginning search for references to a [type].")
var/list/things = list()
for(var/client/thing)
things += thing
for(var/datum/thing)
things += thing
for(var/atom/thing)
things += thing
testing("Collected list of things in search for references to a [type]. ([things.len] Thing\s)")
for(var/datum/thing in things)
if(!usr.client.running_find_references) return
for(var/varname in thing.vars)
var/variable = thing.vars[varname]
if(variable == src)
testing("Found [src.type] \ref[src] in [thing.type]'s [varname] var.")
else if(islist(variable))
if(src in variable)
testing("Found [src.type] \ref[src] in [thing.type]'s [varname] list var.")
testing("Completed search for references to a [type].")
usr.client.running_find_references = null
/client/verb/purge_all_destroyed_objects()
set category = "Debug"
if(garbage_collector)
while(garbage_collector.destroyed.len)
var/datum/o = locate(garbage_collector.destroyed[1])
if(istype(o) && o.gcDestroyed)
del(o)
garbage_collector.dels++
garbage_collector.destroyed.Cut(1, 2)
#endif
#ifdef GC_DEBUG
#undef GC_DEBUG
#endif
#ifdef GC_FINDREF
#undef GC_FINDREF
#endif