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