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