comparison base/StorageAdviser.cpp @ 1276:adbd16d2c1e8 3.0-integration

More informative debug output from StorageAdviser
author Chris Cannam
date Tue, 22 Nov 2016 16:39:29 +0000
parents 393134235fa0
children 91231350ee22
comparison
equal deleted inserted replaced
1275:ad2d3e0a8b7c 1276:adbd16d2c1e8
20 20
21 #include "system/System.h" 21 #include "system/System.h"
22 22
23 #include <iostream> 23 #include <iostream>
24 24
25 //#define DEBUG_STORAGE_ADVISER 1 25 QString
26 StorageAdviser::criteriaToString(int criteria)
27 {
28 QStringList labels;
29 if (criteria & SpeedCritical) labels.push_back("SpeedCritical");
30 if (criteria & PrecisionCritical) labels.push_back("PrecisionCritical");
31 if (criteria & LongRetentionLikely) labels.push_back("LongRetentionLikely");
32 if (criteria & FrequentLookupLikely) labels.push_back("FrequentLookupLikely");
33 if (labels.empty()) return "None";
34 else return labels.join("+");
35 }
36
37 QString
38 StorageAdviser::recommendationToString(int recommendation)
39 {
40 QStringList labels;
41 if (recommendation & UseMemory) labels.push_back("UseMemory");
42 if (recommendation & PreferMemory) labels.push_back("PreferMemory");
43 if (recommendation & PreferDisc) labels.push_back("PreferDisc");
44 if (recommendation & UseDisc) labels.push_back("UseDisc");
45 if (recommendation & ConserveSpace) labels.push_back("ConserveSpace");
46 if (recommendation & UseAsMuchAsYouLike) labels.push_back("UseAsMuchAsYouLike");
47 if (labels.empty()) return "None";
48 else return labels.join("+");
49 }
50
51 QString
52 StorageAdviser::storageStatusToString(StorageStatus status)
53 {
54 if (status == Insufficient) return "Insufficient";
55 if (status == Marginal) return "Marginal";
56 if (status == Sufficient) return "Sufficient";
57 return "Unknown";
58 }
26 59
27 size_t StorageAdviser::m_discPlanned = 0; 60 size_t StorageAdviser::m_discPlanned = 0;
28 size_t StorageAdviser::m_memoryPlanned = 0; 61 size_t StorageAdviser::m_memoryPlanned = 0;
29 62
30 StorageAdviser::Recommendation 63 StorageAdviser::Recommendation
33 StorageAdviser::Recommendation 66 StorageAdviser::Recommendation
34 StorageAdviser::recommend(Criteria criteria, 67 StorageAdviser::recommend(Criteria criteria,
35 size_t minimumSize, 68 size_t minimumSize,
36 size_t maximumSize) 69 size_t maximumSize)
37 { 70 {
38 #ifdef DEBUG_STORAGE_ADVISER 71 SVDEBUG << "StorageAdviser::recommend: criteria " << criteria
39 cerr << "StorageAdviser::recommend: Criteria " << criteria 72 << " (" + criteriaToString(criteria) + ")"
40 << ", minimumSize " << minimumSize 73 << ", minimumSize " << minimumSize
41 << ", maximumSize " << maximumSize << endl; 74 << ", maximumSize " << maximumSize << endl;
42 #endif
43 75
44 if (m_baseRecommendation != NoRecommendation) { 76 if (m_baseRecommendation != NoRecommendation) {
77 SVDEBUG << "StorageAdviser::recommend: Returning fixed recommendation "
78 << m_baseRecommendation << " ("
79 << recommendationToString(m_baseRecommendation) << ")" << endl;
45 return m_baseRecommendation; // for now 80 return m_baseRecommendation; // for now
46 } 81 }
47 82
48 QString path; 83 QString path;
49 try { 84 try {
50 path = TempDirectory::getInstance()->getPath(); 85 path = TempDirectory::getInstance()->getPath();
51 } catch (std::exception e) { 86 } catch (std::exception e) {
52 cerr << "StorageAdviser::recommend: ERROR: Failed to get temporary directory path: " << e.what() << endl; 87 SVDEBUG << "StorageAdviser::recommend: ERROR: Failed to get temporary directory path: " << e.what() << endl;
53 return Recommendation(UseMemory | ConserveSpace); 88 int r = UseMemory | ConserveSpace;
89 SVDEBUG << "StorageAdviser: returning fallback " << r
90 << " (" << recommendationToString(r) << ")" << endl;
91 return Recommendation(r);
54 } 92 }
55 ssize_t discFree = GetDiscSpaceMBAvailable(path.toLocal8Bit()); 93 ssize_t discFree = GetDiscSpaceMBAvailable(path.toLocal8Bit());
56 ssize_t memoryFree, memoryTotal; 94 ssize_t memoryFree, memoryTotal;
57 GetRealMemoryMBAvailable(memoryFree, memoryTotal); 95 GetRealMemoryMBAvailable(memoryFree, memoryTotal);
96
97 SVDEBUG << "StorageAdviser: disc space: " << discFree
98 << "M, memory free: " << memoryFree
99 << "M, memory total: " << memoryTotal << "M" << endl;
100 SVDEBUG << "StorageAdviser: disc planned: " << (m_discPlanned / 1024)
101 << "K, memory planned: " << (m_memoryPlanned / 1024) << "K" << endl;
102 SVDEBUG << "StorageAdviser: min requested: " << minimumSize
103 << "K, max requested: " << maximumSize << "K" << endl;
58 104
59 if (discFree > ssize_t(m_discPlanned / 1024 + 1)) { 105 if (discFree > ssize_t(m_discPlanned / 1024 + 1)) {
60 discFree -= m_discPlanned / 1024 + 1; 106 discFree -= m_discPlanned / 1024 + 1;
61 } else if (discFree > 0) { // can also be -1 for unknown 107 } else if (discFree > 0) { // can also be -1 for unknown
62 discFree = 0; 108 discFree = 0;
66 memoryFree -= m_memoryPlanned / 1024 + 1; 112 memoryFree -= m_memoryPlanned / 1024 + 1;
67 } else if (memoryFree > 0) { // can also be -1 for unknown 113 } else if (memoryFree > 0) { // can also be -1 for unknown
68 memoryFree = 0; 114 memoryFree = 0;
69 } 115 }
70 116
71 #ifdef DEBUG_STORAGE_ADVISER
72 cerr << "Disc space: " << discFree << ", memory free: " << memoryFree << ", memory total: " << memoryTotal << ", min " << minimumSize << ", max " << maximumSize << endl;
73 #endif
74
75 //!!! We have a potentially serious problem here if multiple 117 //!!! We have a potentially serious problem here if multiple
76 //recommendations are made in advance of any of the resulting 118 //recommendations are made in advance of any of the resulting
77 //allocations, as the allocations that have been recommended for 119 //allocations, as the allocations that have been recommended for
78 //won't be taken into account in subsequent recommendations. 120 //won't be taken into account in subsequent recommendations.
79
80 enum StorageStatus {
81 Unknown,
82 Insufficient,
83 Marginal,
84 Sufficient
85 };
86 121
87 StorageStatus memoryStatus = Unknown; 122 StorageStatus memoryStatus = Unknown;
88 StorageStatus discStatus = Unknown; 123 StorageStatus discStatus = Unknown;
89 124
90 ssize_t minmb = ssize_t(minimumSize / 1024 + 1); 125 ssize_t minmb = ssize_t(minimumSize / 1024 + 1);
103 else if (minmb > (discFree * 3) / 4) discStatus = Insufficient; 138 else if (minmb > (discFree * 3) / 4) discStatus = Insufficient;
104 else if (maxmb > (discFree / 4)) discStatus = Marginal; 139 else if (maxmb > (discFree / 4)) discStatus = Marginal;
105 else if (minmb > (discFree / 10)) discStatus = Marginal; 140 else if (minmb > (discFree / 10)) discStatus = Marginal;
106 else discStatus = Sufficient; 141 else discStatus = Sufficient;
107 142
108 #ifdef DEBUG_STORAGE_ADVISER 143 SVDEBUG << "StorageAdviser: memory status: " << memoryStatus
109 cerr << "Memory status: " << memoryStatus << ", disc status " 144 << " (" << storageStatusToString(memoryStatus) << ")"
110 << discStatus << endl; 145 << ", disc status " << discStatus
111 #endif 146 << " (" << storageStatusToString(discStatus) << ")" << endl;
112 147
113 int recommendation = NoRecommendation; 148 int recommendation = NoRecommendation;
114 149
115 if (memoryStatus == Insufficient || memoryStatus == Unknown) { 150 if (memoryStatus == Insufficient || memoryStatus == Unknown) {
116 151
179 recommendation |= UseAsMuchAsYouLike; 214 recommendation |= UseAsMuchAsYouLike;
180 } 215 }
181 } 216 }
182 } 217 }
183 218
184 #ifdef DEBUG_STORAGE_ADVISER 219 SVDEBUG << "StorageAdviser: returning recommendation " << recommendation
185 cerr << "StorageAdviser: returning recommendation " << recommendation << endl; 220 << " (" << recommendationToString(recommendation) << ")" << endl;
186 #endif
187 221
188 return Recommendation(recommendation); 222 return Recommendation(recommendation);
189 } 223 }
190 224
191 void 225 void
192 StorageAdviser::notifyPlannedAllocation(AllocationArea area, size_t size) 226 StorageAdviser::notifyPlannedAllocation(AllocationArea area, size_t size)
193 { 227 {
194 if (area == MemoryAllocation) m_memoryPlanned += size; 228 if (area == MemoryAllocation) m_memoryPlanned += size;
195 else if (area == DiscAllocation) m_discPlanned += size; 229 else if (area == DiscAllocation) m_discPlanned += size;
196 // cerr << "storage planned up: memory: " << m_memoryPlanned << ", disc " 230 SVDEBUG << "StorageAdviser: storage planned up: now memory: " << m_memoryPlanned << ", disc "
197 // << m_discPlanned << endl; 231 << m_discPlanned << endl;
198 } 232 }
199 233
200 void 234 void
201 StorageAdviser::notifyDoneAllocation(AllocationArea area, size_t size) 235 StorageAdviser::notifyDoneAllocation(AllocationArea area, size_t size)
202 { 236 {
205 else m_memoryPlanned = 0; 239 else m_memoryPlanned = 0;
206 } else if (area == DiscAllocation) { 240 } else if (area == DiscAllocation) {
207 if (m_discPlanned > size) m_discPlanned -= size; 241 if (m_discPlanned > size) m_discPlanned -= size;
208 else m_discPlanned = 0; 242 else m_discPlanned = 0;
209 } 243 }
210 // cerr << "storage planned down: memory: " << m_memoryPlanned << ", disc " 244 SVDEBUG << "StorageAdviser: storage planned down: now memory: " << m_memoryPlanned << ", disc "
211 // << m_discPlanned << endl; 245 << m_discPlanned << endl;
212 } 246 }
213 247
214 void 248 void
215 StorageAdviser::setFixedRecommendation(Recommendation recommendation) 249 StorageAdviser::setFixedRecommendation(Recommendation recommendation)
216 { 250 {