annotate base/StorageAdviser.cpp @ 192:900ac6667eab

...
author Chris Cannam
date Tue, 17 Oct 2006 18:54:04 +0000
parents 058a82e8bc3c
children 91fdc752e540
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@168 7 This file copyright 2006 Chris Cannam.
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@168 25 StorageAdviser::Recommendation
Chris@168 26 StorageAdviser::recommend(Criteria criteria,
Chris@168 27 int minimumSize,
Chris@168 28 int maximumSize)
Chris@168 29 {
Chris@170 30 std::cerr << "StorageAdviser::recommend: Criteria " << criteria
Chris@170 31 << ", minimumSize " << minimumSize
Chris@170 32 << ", maximumSize " << maximumSize << std::endl;
Chris@170 33
Chris@168 34 QString path = TempDirectory::getInstance()->getPath();
Chris@170 35 int discFree = GetDiscSpaceMBAvailable(path.toLocal8Bit());
Chris@170 36 int memoryFree, memoryTotal;
Chris@170 37 GetRealMemoryMBAvailable(memoryFree, memoryTotal);
Chris@168 38
Chris@170 39 std::cerr << "Disc space: " << discFree << ", memory free: " << memoryFree << ", memory total: " << memoryTotal << std::endl;
Chris@168 40
Chris@192 41 //!!! We have a potentially serious problem here if multiple
Chris@192 42 //recommendations are made in advance of any of the resulting
Chris@192 43 //allocations, as the allocations that have been recommended for
Chris@192 44 //won't be taken into account in subsequent recommendations.
Chris@192 45
Chris@170 46 enum StorageStatus {
Chris@170 47 Unknown,
Chris@170 48 Insufficient,
Chris@170 49 Marginal,
Chris@170 50 Sufficient
Chris@170 51 };
Chris@168 52
Chris@170 53 StorageStatus memoryStatus = Unknown;
Chris@170 54 StorageStatus discStatus = Unknown;
Chris@170 55
Chris@170 56 int minmb = minimumSize / 1024 + 1;
Chris@170 57 int maxmb = maximumSize / 1024 + 1;
Chris@170 58
Chris@170 59 if (memoryFree == -1) memoryStatus = Unknown;
Chris@170 60 else if (minmb > (memoryFree * 3) / 4) memoryStatus = Insufficient;
Chris@170 61 else if (maxmb > (memoryFree * 3) / 4) memoryStatus = Marginal;
Chris@170 62 else if (minmb > (memoryFree / 3)) memoryStatus = Marginal;
Chris@170 63 else if (memoryTotal == -1 ||
Chris@170 64 minmb > (memoryTotal / 10)) memoryStatus = Marginal;
Chris@170 65 else memoryStatus = Sufficient;
Chris@170 66
Chris@170 67 if (discFree == -1) discStatus = Unknown;
Chris@170 68 else if (minmb > (discFree * 3) / 4) discStatus = Insufficient;
Chris@173 69 else if (maxmb > (discFree / 4)) discStatus = Marginal;
Chris@173 70 else if (minmb > (discFree / 10)) discStatus = Marginal;
Chris@170 71 else discStatus = Sufficient;
Chris@170 72
Chris@170 73 std::cerr << "Memory status: " << memoryStatus << ", disc status "
Chris@170 74 << discStatus << std::endl;
Chris@170 75
Chris@170 76 int recommendation = NoRecommendation;
Chris@170 77
Chris@170 78 if (memoryStatus == Insufficient || memoryStatus == Unknown) {
Chris@170 79
Chris@170 80 recommendation |= UseDisc;
Chris@170 81
Chris@170 82 if (discStatus == Insufficient && minmb > discFree) {
Chris@170 83 throw InsufficientDiscSpace(path, minmb, discFree);
Chris@170 84 }
Chris@170 85
Chris@170 86 if (discStatus == Insufficient || discStatus == Marginal) {
Chris@170 87 recommendation |= ConserveSpace;
Chris@170 88 } else if (discStatus == Unknown && !(criteria & PrecisionCritical)) {
Chris@170 89 recommendation |= ConserveSpace;
Chris@170 90 } else {
Chris@170 91 recommendation |= UseAsMuchAsYouLike;
Chris@170 92 }
Chris@170 93
Chris@170 94 } else if (memoryStatus == Marginal) {
Chris@170 95
Chris@170 96 if (((criteria & SpeedCritical) ||
Chris@170 97 (criteria & FrequentLookupLikely)) &&
Chris@170 98 !(criteria & PrecisionCritical) &&
Chris@170 99 !(criteria & LongRetentionLikely)) {
Chris@170 100
Chris@170 101 // requirements suggest a preference for memory
Chris@170 102
Chris@170 103 if (discStatus != Insufficient) {
Chris@170 104 recommendation |= PreferMemory;
Chris@170 105 } else {
Chris@170 106 recommendation |= UseMemory;
Chris@170 107 }
Chris@170 108
Chris@170 109 recommendation |= ConserveSpace;
Chris@170 110
Chris@170 111 } else {
Chris@170 112
Chris@170 113 if (discStatus == Insufficient) {
Chris@170 114 recommendation |= (UseMemory | ConserveSpace);
Chris@170 115 } else if (discStatus == Marginal) {
Chris@170 116 recommendation |= (PreferMemory | ConserveSpace);
Chris@170 117 } else if (discStatus == Unknown) {
Chris@170 118 recommendation |= (PreferDisc | ConserveSpace);
Chris@170 119 } else {
Chris@170 120 recommendation |= (UseDisc | UseAsMuchAsYouLike);
Chris@170 121 }
Chris@170 122 }
Chris@170 123
Chris@170 124 } else {
Chris@170 125
Chris@170 126 if (discStatus == Insufficient) {
Chris@170 127 recommendation |= (UseMemory | ConserveSpace);
Chris@170 128 } else if (discStatus != Sufficient) {
Chris@170 129 recommendation |= (PreferMemory | ConserveSpace);
Chris@170 130 } else {
Chris@170 131
Chris@170 132 if ((criteria & SpeedCritical) ||
Chris@170 133 (criteria & FrequentLookupLikely)) {
Chris@170 134 recommendation |= PreferMemory;
Chris@170 135 if (criteria & PrecisionCritical) {
Chris@170 136 recommendation |= UseAsMuchAsYouLike;
Chris@170 137 } else {
Chris@170 138 recommendation |= ConserveSpace;
Chris@170 139 }
Chris@170 140 } else {
Chris@170 141 recommendation |= PreferDisc;
Chris@170 142 recommendation |= UseAsMuchAsYouLike;
Chris@170 143 }
Chris@170 144 }
Chris@170 145 }
Chris@170 146
Chris@170 147 return Recommendation(recommendation);
Chris@168 148 }
Chris@168 149