annotate base/StorageAdviser.cpp @ 173:058a82e8bc3c

...
author Chris Cannam
date Wed, 27 Sep 2006 20:52:48 +0000
parents b23eea68357e
children 900ac6667eab
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@170 41 enum StorageStatus {
Chris@170 42 Unknown,
Chris@170 43 Insufficient,
Chris@170 44 Marginal,
Chris@170 45 Sufficient
Chris@170 46 };
Chris@168 47
Chris@170 48 StorageStatus memoryStatus = Unknown;
Chris@170 49 StorageStatus discStatus = Unknown;
Chris@170 50
Chris@170 51 int minmb = minimumSize / 1024 + 1;
Chris@170 52 int maxmb = maximumSize / 1024 + 1;
Chris@170 53
Chris@170 54 if (memoryFree == -1) memoryStatus = Unknown;
Chris@170 55 else if (minmb > (memoryFree * 3) / 4) memoryStatus = Insufficient;
Chris@170 56 else if (maxmb > (memoryFree * 3) / 4) memoryStatus = Marginal;
Chris@170 57 else if (minmb > (memoryFree / 3)) memoryStatus = Marginal;
Chris@170 58 else if (memoryTotal == -1 ||
Chris@170 59 minmb > (memoryTotal / 10)) memoryStatus = Marginal;
Chris@170 60 else memoryStatus = Sufficient;
Chris@170 61
Chris@170 62 if (discFree == -1) discStatus = Unknown;
Chris@170 63 else if (minmb > (discFree * 3) / 4) discStatus = Insufficient;
Chris@173 64 else if (maxmb > (discFree / 4)) discStatus = Marginal;
Chris@173 65 else if (minmb > (discFree / 10)) discStatus = Marginal;
Chris@170 66 else discStatus = Sufficient;
Chris@170 67
Chris@170 68 std::cerr << "Memory status: " << memoryStatus << ", disc status "
Chris@170 69 << discStatus << std::endl;
Chris@170 70
Chris@170 71 int recommendation = NoRecommendation;
Chris@170 72
Chris@170 73 if (memoryStatus == Insufficient || memoryStatus == Unknown) {
Chris@170 74
Chris@170 75 recommendation |= UseDisc;
Chris@170 76
Chris@170 77 if (discStatus == Insufficient && minmb > discFree) {
Chris@170 78 throw InsufficientDiscSpace(path, minmb, discFree);
Chris@170 79 }
Chris@170 80
Chris@170 81 if (discStatus == Insufficient || discStatus == Marginal) {
Chris@170 82 recommendation |= ConserveSpace;
Chris@170 83 } else if (discStatus == Unknown && !(criteria & PrecisionCritical)) {
Chris@170 84 recommendation |= ConserveSpace;
Chris@170 85 } else {
Chris@170 86 recommendation |= UseAsMuchAsYouLike;
Chris@170 87 }
Chris@170 88
Chris@170 89 } else if (memoryStatus == Marginal) {
Chris@170 90
Chris@170 91 if (((criteria & SpeedCritical) ||
Chris@170 92 (criteria & FrequentLookupLikely)) &&
Chris@170 93 !(criteria & PrecisionCritical) &&
Chris@170 94 !(criteria & LongRetentionLikely)) {
Chris@170 95
Chris@170 96 // requirements suggest a preference for memory
Chris@170 97
Chris@170 98 if (discStatus != Insufficient) {
Chris@170 99 recommendation |= PreferMemory;
Chris@170 100 } else {
Chris@170 101 recommendation |= UseMemory;
Chris@170 102 }
Chris@170 103
Chris@170 104 recommendation |= ConserveSpace;
Chris@170 105
Chris@170 106 } else {
Chris@170 107
Chris@170 108 if (discStatus == Insufficient) {
Chris@170 109 recommendation |= (UseMemory | ConserveSpace);
Chris@170 110 } else if (discStatus == Marginal) {
Chris@170 111 recommendation |= (PreferMemory | ConserveSpace);
Chris@170 112 } else if (discStatus == Unknown) {
Chris@170 113 recommendation |= (PreferDisc | ConserveSpace);
Chris@170 114 } else {
Chris@170 115 recommendation |= (UseDisc | UseAsMuchAsYouLike);
Chris@170 116 }
Chris@170 117 }
Chris@170 118
Chris@170 119 } else {
Chris@170 120
Chris@170 121 if (discStatus == Insufficient) {
Chris@170 122 recommendation |= (UseMemory | ConserveSpace);
Chris@170 123 } else if (discStatus != Sufficient) {
Chris@170 124 recommendation |= (PreferMemory | ConserveSpace);
Chris@170 125 } else {
Chris@170 126
Chris@170 127 if ((criteria & SpeedCritical) ||
Chris@170 128 (criteria & FrequentLookupLikely)) {
Chris@170 129 recommendation |= PreferMemory;
Chris@170 130 if (criteria & PrecisionCritical) {
Chris@170 131 recommendation |= UseAsMuchAsYouLike;
Chris@170 132 } else {
Chris@170 133 recommendation |= ConserveSpace;
Chris@170 134 }
Chris@170 135 } else {
Chris@170 136 recommendation |= PreferDisc;
Chris@170 137 recommendation |= UseAsMuchAsYouLike;
Chris@170 138 }
Chris@170 139 }
Chris@170 140 }
Chris@170 141
Chris@170 142 return Recommendation(recommendation);
Chris@168 143 }
Chris@168 144