source: src/memoryusageobserver.cpp@ e138de

Action_Thermostats Add_AtomRandomPerturbation Add_FitFragmentPartialChargesAction Add_RotateAroundBondAction Add_SelectAtomByNameAction Added_ParseSaveFragmentResults AddingActions_SaveParseParticleParameters Adding_Graph_to_ChangeBondActions Adding_MD_integration_tests Adding_ParticleName_to_Atom Adding_StructOpt_integration_tests AtomFragments Automaking_mpqc_open AutomationFragmentation_failures Candidate_v1.5.4 Candidate_v1.6.0 Candidate_v1.6.1 ChangeBugEmailaddress ChangingTestPorts ChemicalSpaceEvaluator CombiningParticlePotentialParsing Combining_Subpackages Debian_Package_split Debian_package_split_molecuildergui_only Disabling_MemDebug Docu_Python_wait EmpiricalPotential_contain_HomologyGraph EmpiricalPotential_contain_HomologyGraph_documentation Enable_parallel_make_install Enhance_userguide Enhanced_StructuralOptimization Enhanced_StructuralOptimization_continued Example_ManyWaysToTranslateAtom Exclude_Hydrogens_annealWithBondGraph FitPartialCharges_GlobalError Fix_BoundInBox_CenterInBox_MoleculeActions Fix_ChargeSampling_PBC Fix_ChronosMutex Fix_FitPartialCharges Fix_FitPotential_needs_atomicnumbers Fix_ForceAnnealing Fix_IndependentFragmentGrids Fix_ParseParticles Fix_ParseParticles_split_forward_backward_Actions Fix_PopActions Fix_QtFragmentList_sorted_selection Fix_Restrictedkeyset_FragmentMolecule Fix_StatusMsg Fix_StepWorldTime_single_argument Fix_Verbose_Codepatterns Fix_fitting_potentials Fixes ForceAnnealing_goodresults ForceAnnealing_oldresults ForceAnnealing_tocheck ForceAnnealing_with_BondGraph ForceAnnealing_with_BondGraph_continued ForceAnnealing_with_BondGraph_continued_betteresults ForceAnnealing_with_BondGraph_contraction-expansion FragmentAction_writes_AtomFragments FragmentMolecule_checks_bonddegrees GeometryObjects Gui_Fixes Gui_displays_atomic_force_velocity ImplicitCharges IndependentFragmentGrids IndependentFragmentGrids_IndividualZeroInstances IndependentFragmentGrids_IntegrationTest IndependentFragmentGrids_Sole_NN_Calculation JobMarket_RobustOnKillsSegFaults JobMarket_StableWorkerPool JobMarket_unresolvable_hostname_fix MoreRobust_FragmentAutomation ODR_violation_mpqc_open PartialCharges_OrthogonalSummation PdbParser_setsAtomName PythonUI_with_named_parameters QtGui_reactivate_TimeChanged_changes Recreated_GuiChecks Rewrite_FitPartialCharges RotateToPrincipalAxisSystem_UndoRedo SaturateAtoms_findBestMatching SaturateAtoms_singleDegree StoppableMakroAction Subpackage_CodePatterns Subpackage_JobMarket Subpackage_LinearAlgebra Subpackage_levmar Subpackage_mpqc_open Subpackage_vmg Switchable_LogView ThirdParty_MPQC_rebuilt_buildsystem TrajectoryDependenant_MaxOrder TremoloParser_IncreasedPrecision TremoloParser_MultipleTimesteps TremoloParser_setsAtomName Ubuntu_1604_changes stable
Last change on this file since e138de was e138de, checked in by Frederik Heber <heber@…>, 15 years ago

Huge change from ofstream * (const) out --> Log().

  • first shift was done via regular expressions
  • then via error messages from the code
  • note that class atom, class element and class molecule kept in parts their output stream, was they print to file.
  • make check runs fine
  • MISSING: Verbosity is not fixed for everything (i.e. if no endl; is present and next has Verbose(0) ...)

Signed-off-by: Frederik Heber <heber@…>

  • Property mode set to 100644
File size: 2.9 KB
Line 
1/*
2 * \file memoryusageobserver.cpp
3 *
4 * This class represents a Singleton for observing memory usage.
5 */
6
7#include "log.hpp"
8#include "memoryusageobserver.hpp"
9#include "verbose.hpp"
10
11MemoryUsageObserver* MemoryUsageObserver::instance = NULL;
12
13/**
14 * Constructor. Do not use this function. Use getInstance() instead.
15 *
16 * \return memory usage observer instance
17 */
18MemoryUsageObserver::MemoryUsageObserver() {
19 instance = NULL;
20 maximumSize = 0;
21 totalSize = 0;
22}
23
24/**
25 * Destructor. Better use purgeInstance().
26 */
27MemoryUsageObserver::~MemoryUsageObserver() {
28 while (!memoryUsers.empty()) {
29 map<void*, size_t>::iterator current = memoryUsers.begin();
30 free(current->first);
31 memoryUsers.erase(current);
32 }
33
34 maximumSize = 0;
35 totalSize = 0;
36}
37
38/**
39 * Returns the singleton memory usage observer instance.
40 *
41 * \return memory usage observer instance
42 */
43MemoryUsageObserver* MemoryUsageObserver::getInstance() {
44 if (instance == NULL) {
45 instance = new MemoryUsageObserver;
46 }
47
48 return instance;
49}
50
51/**
52 * Purges the current memory usage observer instance.
53 */
54void MemoryUsageObserver::purgeInstance() {
55 if (instance != NULL) {
56 delete instance;
57 }
58
59 instance = NULL;
60}
61
62/**
63 * Adds memory.
64 *
65 * \param pointer to the allocated piece of memory
66 * \param size of the allocated memory
67 */
68void MemoryUsageObserver::addMemory(void* pointer, size_t size) {
69 // Memory might become reseized so we need to check whether the provided pointer is already tracked.
70 map<void*, size_t>::iterator current = memoryUsers.find(pointer);
71 if (current != memoryUsers.end()) {
72 totalSize -= current->second;
73 }
74
75 memoryUsers[pointer] = size;
76 totalSize += size;
77 maximumSize = (totalSize > maximumSize) ? totalSize : maximumSize;
78}
79
80/**
81 * Removes tracked memory. Prints a warning if untracked memory is to be released.
82 *
83 * \param pointer to the allocated piece of memory
84 * \param *msg optional error message
85 */
86void MemoryUsageObserver::removeMemory(void* pointer, const char *msg) {
87 map<void*, size_t>::iterator current = memoryUsers.find(pointer);
88
89 if (current == memoryUsers.end()) {
90 Log() << Verbose(0) << "WARNING: There is non-tracked memory to be freed. Pointer "
91 << pointer << " is not registered by MemoryUsageObserver: ";
92 if (msg != NULL)
93 Log() << Verbose(0) << *msg;
94 Log() << Verbose(0) << endl;
95 return;
96 }
97
98 totalSize -= current->second;
99 memoryUsers.erase(current);
100}
101
102/**
103 * Gets the size of currently allocated memory.
104 */
105size_t MemoryUsageObserver::getUsedMemorySize() {
106 return totalSize;
107}
108
109/**
110 * Gets the maximum size of allocated memory until now.
111 */
112size_t MemoryUsageObserver::getMaximumUsedMemory() {
113 return maximumSize;
114}
115
116/**
117 * Gets a map with pointers to the currently allocated memory ranges as keys and
118 * the allocated size as value.
119 */
120map<void*, size_t> MemoryUsageObserver::getPointersToAllocatedMemory() {
121 return memoryUsers;
122}
Note: See TracBrowser for help on using the repository browser.