comparison base/StorageAdviser.cpp @ 0:fc9323a41f5a

start base : Sonic Visualiser sv1-1.0rc1
author lbajardsilogic
date Fri, 11 May 2007 09:08:14 +0000
parents
children be6f263fa0ab
comparison
equal deleted inserted replaced
-1:000000000000 0:fc9323a41f5a
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
2
3 /*
4 Sonic Visualiser
5 An audio file viewer and annotation editor.
6 Centre for Digital Music, Queen Mary, University of London.
7 This file copyright 2006 QMUL.
8
9 This program is free software; you can redistribute it and/or
10 modify it under the terms of the GNU General Public License as
11 published by the Free Software Foundation; either version 2 of the
12 License, or (at your option) any later version. See the file
13 COPYING included with this distribution for more information.
14 */
15
16 #include "StorageAdviser.h"
17
18 #include "Exceptions.h"
19 #include "TempDirectory.h"
20
21 #include "system/System.h"
22
23 #include <iostream>
24
25 long StorageAdviser::m_discPlanned = 0;
26 long StorageAdviser::m_memoryPlanned = 0;
27
28 StorageAdviser::Recommendation
29 StorageAdviser::recommend(Criteria criteria,
30 int minimumSize,
31 int maximumSize)
32 {
33 std::cerr << "StorageAdviser::recommend: Criteria " << criteria
34 << ", minimumSize " << minimumSize
35 << ", maximumSize " << maximumSize << std::endl;
36
37 QString path = TempDirectory::getInstance()->getPath();
38 int discFree = GetDiscSpaceMBAvailable(path.toLocal8Bit());
39 int memoryFree, memoryTotal;
40 GetRealMemoryMBAvailable(memoryFree, memoryTotal);
41
42 if (discFree > m_discPlanned / 1024 + 1) {
43 discFree -= m_discPlanned / 1024 + 1;
44 } else if (discFree > 0) { // can also be -1 for unknown
45 discFree = 0;
46 }
47
48 if (memoryFree > m_memoryPlanned / 1024 + 1) {
49 memoryFree -= m_memoryPlanned / 1024 + 1;
50 } else if (memoryFree > 0) { // can also be -1 for unknown
51 memoryFree = 0;
52 }
53
54 std::cerr << "Disc space: " << discFree << ", memory free: " << memoryFree << ", memory total: " << memoryTotal << std::endl;
55
56 //!!! We have a potentially serious problem here if multiple
57 //recommendations are made in advance of any of the resulting
58 //allocations, as the allocations that have been recommended for
59 //won't be taken into account in subsequent recommendations.
60
61 enum StorageStatus {
62 Unknown,
63 Insufficient,
64 Marginal,
65 Sufficient
66 };
67
68 StorageStatus memoryStatus = Unknown;
69 StorageStatus discStatus = Unknown;
70
71 int minmb = minimumSize / 1024 + 1;
72 int maxmb = maximumSize / 1024 + 1;
73
74 if (memoryFree == -1) memoryStatus = Unknown;
75 else if (minmb > (memoryFree * 3) / 4) memoryStatus = Insufficient;
76 else if (maxmb > (memoryFree * 3) / 4) memoryStatus = Marginal;
77 else if (minmb > (memoryFree / 3)) memoryStatus = Marginal;
78 else if (memoryTotal == -1 ||
79 minmb > (memoryTotal / 10)) memoryStatus = Marginal;
80 else memoryStatus = Sufficient;
81
82 if (discFree == -1) discStatus = Unknown;
83 else if (minmb > (discFree * 3) / 4) discStatus = Insufficient;
84 else if (maxmb > (discFree / 4)) discStatus = Marginal;
85 else if (minmb > (discFree / 10)) discStatus = Marginal;
86 else discStatus = Sufficient;
87
88 std::cerr << "Memory status: " << memoryStatus << ", disc status "
89 << discStatus << std::endl;
90
91 int recommendation = NoRecommendation;
92
93 if (memoryStatus == Insufficient || memoryStatus == Unknown) {
94
95 recommendation |= UseDisc;
96
97 if (discStatus == Insufficient && minmb > discFree) {
98 throw InsufficientDiscSpace(path, minmb, discFree);
99 }
100
101 if (discStatus == Insufficient || discStatus == Marginal) {
102 recommendation |= ConserveSpace;
103 } else if (discStatus == Unknown && !(criteria & PrecisionCritical)) {
104 recommendation |= ConserveSpace;
105 } else {
106 recommendation |= UseAsMuchAsYouLike;
107 }
108
109 } else if (memoryStatus == Marginal) {
110
111 if (((criteria & SpeedCritical) ||
112 (criteria & FrequentLookupLikely)) &&
113 !(criteria & PrecisionCritical) &&
114 !(criteria & LongRetentionLikely)) {
115
116 // requirements suggest a preference for memory
117
118 if (discStatus != Insufficient) {
119 recommendation |= PreferMemory;
120 } else {
121 recommendation |= UseMemory;
122 }
123
124 recommendation |= ConserveSpace;
125
126 } else {
127
128 if (discStatus == Insufficient) {
129 recommendation |= (UseMemory | ConserveSpace);
130 } else if (discStatus == Marginal) {
131 recommendation |= (PreferMemory | ConserveSpace);
132 } else if (discStatus == Unknown) {
133 recommendation |= (PreferDisc | ConserveSpace);
134 } else {
135 recommendation |= (UseDisc | UseAsMuchAsYouLike);
136 }
137 }
138
139 } else {
140
141 if (discStatus == Insufficient) {
142 recommendation |= (UseMemory | ConserveSpace);
143 } else if (discStatus != Sufficient) {
144 recommendation |= (PreferMemory | ConserveSpace);
145 } else {
146
147 if ((criteria & SpeedCritical) ||
148 (criteria & FrequentLookupLikely)) {
149 recommendation |= PreferMemory;
150 if (criteria & PrecisionCritical) {
151 recommendation |= UseAsMuchAsYouLike;
152 } else {
153 recommendation |= ConserveSpace;
154 }
155 } else {
156 recommendation |= PreferDisc;
157 recommendation |= UseAsMuchAsYouLike;
158 }
159 }
160 }
161
162 return Recommendation(recommendation);
163 }
164
165 void
166 StorageAdviser::notifyPlannedAllocation(AllocationArea area, int size)
167 {
168 if (area == MemoryAllocation) m_memoryPlanned += size;
169 else if (area == DiscAllocation) m_discPlanned += size;
170 // std::cerr << "storage planned up: memory: " << m_memoryPlanned << ", disc "
171 // << m_discPlanned << std::endl;
172 }
173
174 void
175 StorageAdviser::notifyDoneAllocation(AllocationArea area, int size)
176 {
177 if (area == MemoryAllocation) {
178 if (m_memoryPlanned > size) m_memoryPlanned -= size;
179 else m_memoryPlanned = 0;
180 } else if (area == DiscAllocation) {
181 if (m_discPlanned > size) m_discPlanned -= size;
182 else m_discPlanned = 0;
183 }
184 // std::cerr << "storage planned down: memory: " << m_memoryPlanned << ", disc "
185 // << m_discPlanned << std::endl;
186 }
187