comparison system/System.cpp @ 170:b23eea68357e

* flesh out StorageAdviser
author Chris Cannam
date Tue, 26 Sep 2006 12:58:12 +0000
parents 603991c63ff6
children ebf55cf0d34d
comparison
equal deleted inserted replaced
169:603991c63ff6 170:b23eea68357e
15 15
16 #include "System.h" 16 #include "System.h"
17 17
18 #include <QStringList> 18 #include <QStringList>
19 #include <QString> 19 #include <QString>
20
21 #include <stdint.h>
20 22
21 #ifndef _WIN32 23 #ifndef _WIN32
22 #include <signal.h> 24 #include <signal.h>
23 #include <sys/statvfs.h> 25 #include <sys/statvfs.h>
24 #endif 26 #endif
72 return UnknownProcessStatus; 74 return UnknownProcessStatus;
73 } 75 }
74 #endif 76 #endif
75 } 77 }
76 78
77 int 79 void
78 GetRealMemoryMBAvailable() 80 GetRealMemoryMBAvailable(int &available, int &total)
79 { 81 {
82 available = -1;
83 total = -1;
84
80 FILE *meminfo = fopen("/proc/meminfo", "r"); 85 FILE *meminfo = fopen("/proc/meminfo", "r");
81 if (!meminfo) return -1; 86 if (!meminfo) return;
82 87
83 char buf[256]; 88 char buf[256];
84 while (!feof(meminfo)) { 89 while (!feof(meminfo)) {
85 fgets(buf, 256, meminfo); 90 fgets(buf, 256, meminfo);
86 if (strncmp(buf, "MemFree:", 8)) { 91 bool isMemFree = (strncmp(buf, "MemFree:", 8) == 0);
87 fclose(meminfo); 92 bool isMemTotal = (!isMemFree && (strncmp(buf, "MemTotal:", 9) == 0));
93 if (isMemFree || isMemTotal) {
88 QString line = QString(buf).trimmed(); 94 QString line = QString(buf).trimmed();
89 QStringList elements = line.split(' ', QString::SkipEmptyParts); 95 QStringList elements = line.split(' ', QString::SkipEmptyParts);
90 QString unit = "kB"; 96 QString unit = "kB";
91 if (elements.size() > 2) unit = elements[2]; 97 if (elements.size() > 2) unit = elements[2];
92 int size = elements[1].toInt(); 98 int size = elements[1].toInt();
93 // std::cerr << "have size \"" << size << "\", unit \"" 99 // std::cerr << "have size \"" << size << "\", unit \""
94 // << unit.toStdString() << "\"" << std::endl; 100 // << unit.toStdString() << "\"" << std::endl;
95 if (unit.toLower() == "gb") return size * 1024; 101 if (unit.toLower() == "gb") size = size * 1024;
96 if (unit.toLower() == "mb") return size; 102 else if (unit.toLower() == "mb") size = size;
97 if (unit.toLower() == "kb") return size / 1024; 103 else if (unit.toLower() == "kb") size = size / 1024;
98 return size / 1048576; 104 else size = size / 1048576;
105
106 if (isMemFree) available = size;
107 else total = size;
108 }
109 if (available != -1 && total != -1) {
110 fclose(meminfo);
111 return;
99 } 112 }
100 } 113 }
101 fclose(meminfo); 114 fclose(meminfo);
102 return -1;
103 } 115 }
104 116
105 int 117 int
106 GetDiscSpaceMBAvailable(const char *path) 118 GetDiscSpaceMBAvailable(const char *path)
107 { 119 {
108 #ifdef _WIN32 120 #ifdef _WIN32
109 __int64 available, total, totalFree; 121 __int64 available, total, totalFree;
110 if (GetDiskFreeSpaceEx(path, &available, &total, &totalFree)) { 122 if (GetDiskFreeSpaceEx(path, &available, &total, &totalFree)) {
111 available /= 1048576; 123 available /= 1048576;
124 if (available > INT_MAX) available = INT_MAX;
112 return int(available); 125 return int(available);
113 } else { 126 } else {
114 std::cerr << "WARNING: GetDiskFreeSpaceEx failed: error code " 127 std::cerr << "WARNING: GetDiskFreeSpaceEx failed: error code "
115 << GetLastError() << std::endl; 128 << GetLastError() << std::endl;
116 return -1; 129 return -1;
119 struct statvfs buf; 132 struct statvfs buf;
120 if (!statvfs(path, &buf)) { 133 if (!statvfs(path, &buf)) {
121 // do the multiplies and divides in this order to reduce the 134 // do the multiplies and divides in this order to reduce the
122 // likelihood of arithmetic overflow 135 // likelihood of arithmetic overflow
123 std::cerr << "statvfs(" << path << ") says available: " << buf.f_bavail << ", block size: " << buf.f_bsize << std::endl; 136 std::cerr << "statvfs(" << path << ") says available: " << buf.f_bavail << ", block size: " << buf.f_bsize << std::endl;
124 return ((buf.f_bavail / 1024) * buf.f_bsize) / 1024; 137 uint64_t available = ((buf.f_bavail / 1024) * buf.f_bsize) / 1024;
138 if (available > INT_MAX) available = INT_MAX;
139 return int(available);
125 } else { 140 } else {
126 perror("statvfs failed"); 141 perror("statvfs failed");
127 return -1; 142 return -1;
128 } 143 }
129 #endif 144 #endif