Debian Codegraph @

Source/Utilities/Memory/CCentralizedAllocationConfigProvidedDependendLimitation.cpp

raw
1 /*
2 * Copyright (C) 2013-2015, 2019 by the Konclude Developer Team.
3 *
4 * This file is part of the reasoning system Konclude.
5 * For details and support, see <http://konclude.com/>.
6 *
7 * Konclude is free software: you can redistribute it and/or modify
8 * it under the terms of version 3 of the GNU Lesser General Public
9 * License (LGPLv3) as published by the Free Software Foundation.
10 *
11 * Konclude is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU (Lesser) General Public License for more details.
15 *
16 * You should have received a copy of the GNU (Lesser) General Public
17 * License along with Konclude. If not, see <http://www.gnu.org/licenses/>.
18 *
19 */
20
21 #include "CCentralizedAllocationConfigProvidedDependendLimitation.h"
22
23
24 namespace Konclude {
25
26 namespace Utilities {
27
28 namespace Memory {
29
30
31 CCentralizedAllocationConfigProvidedDependendLimitation::CCentralizedAllocationConfigProvidedDependendLimitation(CConfigurationProvider* configProvider, const QString& configString) {
32 mConfigString = configString;
33 mConfigProvider = configProvider;
34 mMaxMemAllocSize = 0;
35 mCurrMemAllocSize = 0;
36 mAddMemAllocSize = 1024*1024*512;
37 mCriticalMemAllocSize = 1024*1024*1024;
38 mMemAllocLimited = false;
39 mLimitatorLinker = nullptr;
40 mLastConfig = nullptr;
41 mStatMaxAllocatedMemSize = 0;
42 }
43
44 CCentralizedAllocationConfigProvidedDependendLimitation::~CCentralizedAllocationConfigProvidedDependendLimitation() {
45 }
46
47
48 CCentralizedAllocationLimitator* CCentralizedAllocationConfigProvidedDependendLimitation::getLimitator() {
49 QMutexLocker locker(&mSyncMutex);
50 CCentralizedAllocationLimitator* limitator = new CCentralizedAllocationLimitator(this);
51 mLimitatorLinker = limitator->append(mLimitatorLinker);
52 return limitator;
53 }
54
55
56 cint64 CCentralizedAllocationConfigProvidedDependendLimitation::getAllocatedMemorySize() {
57 cint64 allocatedMemSize = 0;
58 CCentralizedAllocationLimitator* limIt = mLimitatorLinker;
59 while (limIt) {
60 allocatedMemSize += limIt->getMemoryAllocatedSize();
61 limIt = limIt->getNext();
62 }
63 return allocatedMemSize;
64 }
65
66
67 cint64 CCentralizedAllocationConfigProvidedDependendLimitation::getAllocatedMemorySizeMaximum() {
68 mStatMaxAllocatedMemSize = qMax(mStatMaxAllocatedMemSize,getAllocatedMemorySize());
69 return mStatMaxAllocatedMemSize;
70 }
71
72 cint64 CCentralizedAllocationConfigProvidedDependendLimitation::getReservedMemorySizeMaximum() {
73 return mCurrMemAllocSize;
74 }
75
76 bool CCentralizedAllocationConfigProvidedDependendLimitation::updateMaxMemoryAllocationSize() {
77 CConfiguration* currConfig = mConfigProvider->getCurrentConfiguration();
78 if (mLastConfig != currConfig) {
79 QMutexLocker locker(&mSyncMutex);
80 if (mLastConfig != currConfig) {
81 mLastConfig = currConfig;
82 mAddMemAllocSize = CConfigDataReader::readConfigInteger(currConfig,mConfigString+".IncreaseAllocationSize",1024*1024*512 /*512 MByte*/);
83
84
85 bool configReadError = false;
86 bool limited = CConfigDataReader::readConfigBoolean(currConfig,mConfigString+".AllocationLimitation",false,&configReadError);
87 if (!configReadError && limited) {
88 configReadError = false;
89 mCriticalMemAllocSize = CConfigDataReader::readConfigInteger(currConfig,mConfigString+".CriticalAllocationSize",1024*1024*1024 /*1 GByte*/);
90 cint64 configMaxMemAllocSize = CConfigDataReader::readConfigInteger(currConfig,mConfigString+".MaximumAllocationSize",0,&configReadError);
91 if (configReadError) {
92 mMemAllocLimited = false;
93 } else {
94 mMemAllocLimited = true;
95 mMaxMemAllocSize = configMaxMemAllocSize;
96 return true;
97 }
98 } else {
99 mMemAllocLimited = false;
100 }
101 }
102 }
103
104 return false;
105 }
106
107
108
109 bool CCentralizedAllocationConfigProvidedDependendLimitation::requestLimitatorMemoryAllocationSizeUpdate() {
110 CCentralizedAllocationLimitator* limIt = mLimitatorLinker;
111 while (limIt) {
112 if (limIt->getRemainingMemoryAllocationSize() > 2*mAddMemAllocSize) {
113 limIt->notifyRequireUpdateLimit();
114 }
115 limIt = limIt->getNext();
116 }
117 return true;
118 }
119
120
121 cint64 CCentralizedAllocationConfigProvidedDependendLimitation::getUpdatedMemoryAllocationLimit(CAllocationLimitator* limitator) {
122 mStatMaxAllocatedMemSize = qMax(mStatMaxAllocatedMemSize,getAllocatedMemorySize());
123 bool updated = updateMaxMemoryAllocationSize();
124 cint64 remSize = limitator->getRemainingMemoryAllocationSize();
125 if (remSize < mAddMemAllocSize) {
126 // add some memory
127 QMutexLocker locker(&mSyncMutex);
128 if (!mMemAllocLimited) {
129 mCurrMemAllocSize += mAddMemAllocSize;
130 return limitator->getMaximumMemoryAllocationSize() + mAddMemAllocSize;
131 } else {
132 cint64 tmpAlloc = mMaxMemAllocSize-mCurrMemAllocSize;
133 tmpAlloc = qMin(tmpAlloc,mAddMemAllocSize);
134 tmpAlloc = qMax(tmpAlloc,(cint64)0);
135 mCurrMemAllocSize += tmpAlloc;
136
137 if (mCurrMemAllocSize > mCriticalMemAllocSize || tmpAlloc != mAddMemAllocSize) {
138 requestLimitatorMemoryAllocationSizeUpdate();
139 }
140
141 return limitator->getMaximumMemoryAllocationSize() + tmpAlloc;
142 }
143 } else if (remSize > 2*mAddMemAllocSize) {
144 // get some memory
145 QMutexLocker locker(&mSyncMutex);
146 cint64 tmpAllocGet = remSize-mAddMemAllocSize;
147 mCurrMemAllocSize -= tmpAllocGet;
148 return limitator->getMaximumMemoryAllocationSize() - tmpAllocGet;
149 }
150 return limitator->getMaximumMemoryAllocationSize();
151 }
152
153
154 }; // end namespace Memory
155
156 }; // end namespace Utilities
157
158 }; // end namespace Konclude
159