comparison system/System.cpp @ 168:04baa690f90d

* Start adding StorageAdviser class to determine whether caches should be on disc or in memory
author Chris Cannam
date Mon, 25 Sep 2006 13:44:05 +0000
parents 4b2ea82fd0ed
children 603991c63ff6
comparison
equal deleted inserted replaced
167:665342c6ec57 168:04baa690f90d
13 COPYING included with this distribution for more information. 13 COPYING included with this distribution for more information.
14 */ 14 */
15 15
16 #include "System.h" 16 #include "System.h"
17 17
18 #include <QFile>
19 #include <QTextStream>
20 #include <QStringList>
21 #include <QString>
22
18 #ifndef _WIN32 23 #ifndef _WIN32
19 #include <signal.h> 24 #include <signal.h>
25 #include <sys/statvfs.h>
20 #endif 26 #endif
21 27
22 #include <iostream> 28 #include <iostream>
23 29
24 #ifdef _WIN32 30 #ifdef _WIN32
68 return UnknownProcessStatus; 74 return UnknownProcessStatus;
69 } 75 }
70 #endif 76 #endif
71 } 77 }
72 78
79 int
80 GetRealMemoryMBAvailable()
81 {
82 // ugh
83 QFile meminfo("/proc/meminfo");
84 if (meminfo.open(QFile::ReadOnly)) {
85 std::cerr << "opened meminfo" << std::endl;
86 QTextStream in(&meminfo);
87 while (!in.atEnd()) {
88 QString line = in.readLine(256);
89 std::cerr << "read: \"" << line.toStdString() << "\"" << std::endl;
90 if (line.startsWith("MemFree:")) {
91 QStringList elements = line.split(' ', QString::SkipEmptyParts);
92 QString unit = "kB";
93 if (elements.size() > 2) unit = elements[2];
94 int size = elements[1].toInt();
95 std::cerr << "have size \"" << size << "\", unit \""
96 << unit.toStdString() << "\"" << std::endl;
97 if (unit.toLower() == "gb") return size * 1024;
98 if (unit.toLower() == "mb") return size;
99 if (unit.toLower() == "kb") return size / 1024;
100 return size / 1048576;
101 }
102 }
103 }
104 return -1;
105 }
106
107 int
108 GetDiscSpaceMBAvailable(const char *path)
109 {
110 #ifdef _WIN32
111 __int64 available, total, totalFree;
112 if (GetDiskFreeSpaceEx(path, &available, &total, &totalFree)) {
113 available /= 1048576;
114 return int(available);
115 } else {
116 std::cerr << "WARNING: GetDiskFreeSpaceEx failed: error code "
117 << GetLastError() << std::endl;
118 return -1;
119 }
120 #else
121 struct statvfs buf;
122 if (!statvfs(path, &buf)) {
123 // do the multiplies and divides in this order to reduce the
124 // likelihood of arithmetic overflow
125 std::cerr << "statvfs(" << path << ") says available: " << buf.f_bavail << ", block size: " << buf.f_bsize << std::endl;
126 return ((buf.f_bavail / 1024) * buf.f_bsize) / 1024;
127 } else {
128 perror("statvfs failed");
129 return -1;
130 }
131 #endif
132 }
133
134
73 double mod(double x, double y) { return x - (y * floor(x / y)); } 135 double mod(double x, double y) { return x - (y * floor(x / y)); }
74 float modf(float x, float y) { return x - (y * floorf(x / y)); } 136 float modf(float x, float y) { return x - (y * floorf(x / y)); }
75 137
76 double princarg(double a) { return mod(a + M_PI, -2 * M_PI) + M_PI; } 138 double princarg(double a) { return mod(a + M_PI, -2 * M_PI) + M_PI; }
77 float princargf(float a) { return modf(a + M_PI, -2 * M_PI) + M_PI; } 139 float princargf(float a) { return modf(a + M_PI, -2 * M_PI) + M_PI; }