source: src/memoryusageobserver.cpp@ 5f612ee

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 5f612ee was a67d19, checked in by Frederik Heber <heber@…>, 15 years ago

Huge change: Log() << Verbose(.) --> DoLog(.) && (Log() << Verbose(.) << ...);

Most of the files are affected, but this is necessary as if DoLog() says verbosity is not enough, all the stream operators won"t get executed which saves substantial amount of computation time.

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