annotate base/StorageAdviser.cpp @ 391:5858cc462d0a

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