| 1 | /* | 
|---|
| 2 | * Project: MoleCuilder | 
|---|
| 3 | * Description: creates and alters molecular systems | 
|---|
| 4 | * Copyright (C)  2010-2012 University of Bonn. All rights reserved. | 
|---|
| 5 | * Please see the LICENSE file or "Copyright notice" in builder.cpp for details. | 
|---|
| 6 | */ | 
|---|
| 7 |  | 
|---|
| 8 | /* | 
|---|
| 9 | * cleanUp.cpp | 
|---|
| 10 | * | 
|---|
| 11 | *  Created on: Oct 28, 2011 | 
|---|
| 12 | *      Author: heber | 
|---|
| 13 | */ | 
|---|
| 14 |  | 
|---|
| 15 | // include config.h | 
|---|
| 16 | #ifdef HAVE_CONFIG_H | 
|---|
| 17 | #include <config.h> | 
|---|
| 18 | #endif | 
|---|
| 19 |  | 
|---|
| 20 | #include <iostream> | 
|---|
| 21 | #include <iomanip> | 
|---|
| 22 |  | 
|---|
| 23 | #include "CodePatterns/MemDebug.hpp" | 
|---|
| 24 |  | 
|---|
| 25 | #include "cleanUp.hpp" | 
|---|
| 26 |  | 
|---|
| 27 | #include "Atom/AtomObserver.hpp" | 
|---|
| 28 |  | 
|---|
| 29 | #include "CodePatterns/Chronos.hpp" | 
|---|
| 30 | #include "CodePatterns/errorlogger.hpp" | 
|---|
| 31 | #include "CodePatterns/logger.hpp" | 
|---|
| 32 |  | 
|---|
| 33 | #include "Actions/ActionHistory.hpp" | 
|---|
| 34 | #include "Actions/ActionRegistry.hpp" | 
|---|
| 35 | #include "Actions/OptionRegistry.hpp" | 
|---|
| 36 | #include "Actions/ValueStorage.hpp" | 
|---|
| 37 |  | 
|---|
| 38 | #include "RandomNumbers/RandomNumberDistributionFactory.hpp" | 
|---|
| 39 | #include "RandomNumbers/RandomNumberEngineFactory.hpp" | 
|---|
| 40 | #include "RandomNumbers/RandomNumberGeneratorFactory.hpp" | 
|---|
| 41 |  | 
|---|
| 42 | #include "Parser/ChangeTracker.hpp" | 
|---|
| 43 | #include "Parser/FormatParserStorage.hpp" | 
|---|
| 44 |  | 
|---|
| 45 | #include "UIElements/CommandLineUI/CommandLineParser.hpp" | 
|---|
| 46 | #include "UIElements/Menu/MenuDescription.hpp" | 
|---|
| 47 | #include "UIElements/UIFactory.hpp" | 
|---|
| 48 | #include "World.hpp" | 
|---|
| 49 |  | 
|---|
| 50 | /** In this function all dynamicly allocated member variables to static/global | 
|---|
| 51 | * variables are added to the ignore list of Memory/MemDebug. | 
|---|
| 52 | * | 
|---|
| 53 | * Use this to prevent their listing in the Memory::getState() at the end of the | 
|---|
| 54 | * program. Check with valgrind that truely no memory leak occurs! | 
|---|
| 55 | */ | 
|---|
| 56 | void AddStaticEntitiestoIgnoreList() | 
|---|
| 57 | { | 
|---|
| 58 | // zeroVec and unitVec are global variables (on the stack) but vectorContent | 
|---|
| 59 | // within is situated on the heap and has to be ignored | 
|---|
| 60 | Memory::ignore(zeroVec.get()); | 
|---|
| 61 | Memory::ignore(unitVec[0].get()); | 
|---|
| 62 | Memory::ignore(unitVec[1].get()); | 
|---|
| 63 | Memory::ignore(unitVec[2].get()); | 
|---|
| 64 | } | 
|---|
| 65 |  | 
|---|
| 66 | /** purges all static (singleton) instances in correct order of dependency. | 
|---|
| 67 | * | 
|---|
| 68 | */ | 
|---|
| 69 | void purgeStaticInstances() | 
|---|
| 70 | { | 
|---|
| 71 | Chronos::purgeInstance(); | 
|---|
| 72 | RandomNumberDistributionFactory::purgeInstance(); | 
|---|
| 73 | RandomNumberEngineFactory::purgeInstance(); | 
|---|
| 74 | RandomNumberGeneratorFactory::purgeInstance(); | 
|---|
| 75 | FormatParserStorage::purgeInstance(); | 
|---|
| 76 | ChangeTracker::purgeInstance(); | 
|---|
| 77 | World::purgeInstance(); | 
|---|
| 78 | AtomObserver::purgeInstance(); | 
|---|
| 79 | MenuDescription::purgeInstance(); | 
|---|
| 80 | UIFactory::purgeInstance(); | 
|---|
| 81 | ValueStorage::purgeInstance(); | 
|---|
| 82 | CommandLineParser::purgeInstance(); | 
|---|
| 83 | MoleCuilder::ActionRegistry::purgeInstance(); | 
|---|
| 84 | MoleCuilder::OptionRegistry::purgeInstance(); | 
|---|
| 85 | MoleCuilder::ActionHistory::purgeInstance(); | 
|---|
| 86 | logger::purgeInstance(); | 
|---|
| 87 | errorLogger::purgeInstance(); | 
|---|
| 88 | } | 
|---|
| 89 |  | 
|---|
| 90 | /** Cleans all singleton instances in an orderly fashion. | 
|---|
| 91 | * C++ does not guarantee any specific sequence of removal of single instances | 
|---|
| 92 | * which have static/global variables. Some singletons depend on others hence we | 
|---|
| 93 | * acertain a specific ordering here, which is is used via the atexit() hook. | 
|---|
| 94 | */ | 
|---|
| 95 | void cleanUp() | 
|---|
| 96 | { | 
|---|
| 97 | // give timings per Action | 
|---|
| 98 | printTimings(); | 
|---|
| 99 | // purge static instances from memory | 
|---|
| 100 | purgeStaticInstances(); | 
|---|
| 101 | // put some static variables' dynamic contents on the Memory::ignore map to avoid their | 
|---|
| 102 | // admonishing lateron | 
|---|
| 103 | AddStaticEntitiestoIgnoreList(); | 
|---|
| 104 | #ifdef LOG_OBSERVER | 
|---|
| 105 | cout << observerLog().getLog(); | 
|---|
| 106 | #endif | 
|---|
| 107 | Memory::getState(); | 
|---|
| 108 | } | 
|---|
| 109 |  | 
|---|
| 110 | /** We give a list of all times per action and a total time. | 
|---|
| 111 | * | 
|---|
| 112 | */ | 
|---|
| 113 | void printTimings() | 
|---|
| 114 | { | 
|---|
| 115 | const MoleCuilder::ActionRegistry &AR = MoleCuilder::ActionRegistry::getInstance(); | 
|---|
| 116 | const Chronos &Chron = Chronos::getInstance(); | 
|---|
| 117 | std::cout << "(Non-zero) Times used per Action [seconds]:" << std::endl; | 
|---|
| 118 | for (MoleCuilder::ActionRegistry::const_iterator iter = AR.getBeginIter(); iter != AR.getEndIter(); ++iter) | 
|---|
| 119 | if (Chron.getTime(iter->first) != 0.) { // dont give if action has not been used | 
|---|
| 120 | std::cout << "  " << setiosflags(ios::left) << setw(24) << setfill('.') << iter->first; | 
|---|
| 121 | std::cout << setiosflags(ios::left) << setprecision(6) << fixed << Chron.getTime(iter->first) << std::endl; | 
|---|
| 122 | } | 
|---|
| 123 | std::cout << "Total Time: " << Chron.SumUpTotalTime() << " seconds" << std::endl; | 
|---|
| 124 | std::cout << "Total Actions called: " << Chron.SumUpTotalFunctions() << std::endl; | 
|---|
| 125 | } | 
|---|
| 126 |  | 
|---|
| 127 | /** Dump current memory chunks. | 
|---|
| 128 | * | 
|---|
| 129 | */ | 
|---|
| 130 | void dumpMemory() | 
|---|
| 131 | { | 
|---|
| 132 | ofstream ost("molecuilder.memdump"); | 
|---|
| 133 | Memory::dumpMemory(ost); | 
|---|
| 134 | } | 
|---|
| 135 |  | 
|---|
| 136 | /** Save the current World to output files and exit. | 
|---|
| 137 | * | 
|---|
| 138 | * @return retrieved from World::getExitFlag() | 
|---|
| 139 | */ | 
|---|
| 140 | int saveAll() | 
|---|
| 141 | { | 
|---|
| 142 | FormatParserStorage::getInstance().SaveAll(); | 
|---|
| 143 | ChangeTracker::getInstance().saveStatus(); | 
|---|
| 144 |  | 
|---|
| 145 | int ExitFlag = World::getInstance().getExitFlag(); | 
|---|
| 146 | return (ExitFlag == 1 ? 0 : ExitFlag); | 
|---|
| 147 | } | 
|---|