annotate system/System.cpp @ 220:e08cb0158e81

...
author Chris Cannam
date Thu, 25 Jan 2007 15:26:15 +0000
parents 91fdc752e540
children 07a96a1931c0
rev   line source
Chris@150 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@150 2
Chris@150 3 /*
Chris@150 4 Sonic Visualiser
Chris@150 5 An audio file viewer and annotation editor.
Chris@150 6 Centre for Digital Music, Queen Mary, University of London.
Chris@202 7 This file copyright 2006 Chris Cannam and QMUL.
Chris@150 8
Chris@150 9 This program is free software; you can redistribute it and/or
Chris@150 10 modify it under the terms of the GNU General Public License as
Chris@150 11 published by the Free Software Foundation; either version 2 of the
Chris@150 12 License, or (at your option) any later version. See the file
Chris@150 13 COPYING included with this distribution for more information.
Chris@150 14 */
Chris@150 15
Chris@150 16 #include "System.h"
Chris@150 17
Chris@168 18 #include <QStringList>
Chris@168 19 #include <QString>
Chris@168 20
Chris@170 21 #include <stdint.h>
Chris@170 22
Chris@150 23 #ifndef _WIN32
Chris@150 24 #include <signal.h>
Chris@168 25 #include <sys/statvfs.h>
Chris@150 26 #endif
Chris@150 27
Martin@196 28 #ifdef __APPLE__
Martin@196 29 #include <sys/param.h>
Martin@196 30 #include <sys/sysctl.h>
Martin@196 31 #endif
Martin@196 32
Chris@150 33 #include <iostream>
Chris@150 34
Chris@150 35 #ifdef _WIN32
Chris@150 36
Chris@150 37 extern "C" {
Chris@150 38
Chris@150 39 void usleep(unsigned long usec)
Chris@150 40 {
Chris@150 41 ::Sleep(usec / 1000);
Chris@150 42 }
Chris@150 43
Chris@150 44 void gettimeofday(struct timeval *tv, void *tz)
Chris@150 45 {
Chris@150 46 union {
Chris@150 47 long long ns100;
Chris@150 48 FILETIME ft;
Chris@150 49 } now;
Chris@150 50
Chris@150 51 ::GetSystemTimeAsFileTime(&now.ft);
Chris@150 52 tv->tv_usec = (long)((now.ns100 / 10LL) % 1000000LL);
Chris@150 53 tv->tv_sec = (long)((now.ns100 - 116444736000000000LL) / 10000000LL);
Chris@150 54 }
Chris@150 55
Chris@150 56 }
Chris@150 57
Chris@150 58 #endif
Chris@150 59
Chris@150 60 ProcessStatus
Chris@150 61 GetProcessStatus(int pid)
Chris@150 62 {
Chris@150 63 #ifdef _WIN32
Chris@150 64 HANDLE handle = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, pid);
Chris@150 65 if (!handle) {
Chris@150 66 return ProcessNotRunning;
Chris@150 67 } else {
Chris@150 68 CloseHandle(handle);
Chris@150 69 return ProcessRunning;
Chris@150 70 }
Chris@150 71 #else
Chris@150 72 if (kill(getpid(), 0) == 0) {
Chris@150 73 if (kill(pid, 0) == 0) {
Chris@150 74 return ProcessRunning;
Chris@150 75 } else {
Chris@150 76 return ProcessNotRunning;
Chris@150 77 }
Chris@150 78 } else {
Chris@150 79 return UnknownProcessStatus;
Chris@150 80 }
Chris@150 81 #endif
Chris@150 82 }
Chris@150 83
Chris@170 84 void
Chris@170 85 GetRealMemoryMBAvailable(int &available, int &total)
Chris@168 86 {
Chris@170 87 available = -1;
Chris@170 88 total = -1;
Chris@170 89
Chris@171 90 #ifdef _WIN32
Chris@171 91
Chris@171 92 MEMORYSTATUSEX status;
Chris@171 93 status.dwLength = sizeof(status);
Chris@171 94 if (!GlobalMemoryStatusEx(&status)) {
Chris@171 95 std::cerr << "WARNING: GetDiskFreeSpaceEx failed: error code "
Chris@171 96 << GetLastError() << std::endl;
Chris@171 97 return;
Chris@171 98 }
Chris@171 99 DWORDLONG size = status.ullAvailPhys / 1048576;
Chris@171 100 if (size > INT_MAX) size = INT_MAX;
Chris@171 101 available = int(size);
Chris@171 102
Chris@171 103 size = status.ullTotalPhys / 1048576;
Chris@171 104 if (size > INT_MAX) size = INT_MAX;
Chris@171 105 total = int(size);
Chris@171 106
Chris@171 107 return;
Chris@171 108
Chris@171 109 #else
Chris@171 110 #ifdef __APPLE__
Chris@171 111
Chris@171 112 unsigned int val;
Chris@171 113 int mib[2];
Chris@171 114 size_t size_sys;
Chris@171 115
Chris@171 116 mib[0] = CTL_HW;
Chris@171 117
Chris@171 118 mib[1] = HW_PHYSMEM;
Chris@171 119 size_sys = sizeof(val);
Chris@171 120 sysctl(mib, 2, &val, &size_sys, NULL, 0);
Chris@171 121 if (val) total = val / 1048576;
Chris@171 122
Chris@171 123 mib[1] = HW_USERMEM;
Chris@171 124 size_sys = sizeof(val);
Chris@171 125 sysctl(mib, 2, &val, &size_sys, NULL, 0);
Chris@171 126 if (val) available = val / 1048576;
Chris@171 127
Chris@171 128 return;
Chris@171 129
Chris@171 130 #else
Chris@171 131
Chris@169 132 FILE *meminfo = fopen("/proc/meminfo", "r");
Chris@170 133 if (!meminfo) return;
Chris@169 134
Chris@169 135 char buf[256];
Chris@169 136 while (!feof(meminfo)) {
Chris@169 137 fgets(buf, 256, meminfo);
Chris@170 138 bool isMemFree = (strncmp(buf, "MemFree:", 8) == 0);
Chris@170 139 bool isMemTotal = (!isMemFree && (strncmp(buf, "MemTotal:", 9) == 0));
Chris@170 140 if (isMemFree || isMemTotal) {
Chris@169 141 QString line = QString(buf).trimmed();
Chris@169 142 QStringList elements = line.split(' ', QString::SkipEmptyParts);
Chris@169 143 QString unit = "kB";
Chris@169 144 if (elements.size() > 2) unit = elements[2];
Chris@169 145 int size = elements[1].toInt();
Chris@169 146 // std::cerr << "have size \"" << size << "\", unit \""
Chris@169 147 // << unit.toStdString() << "\"" << std::endl;
Chris@170 148 if (unit.toLower() == "gb") size = size * 1024;
Chris@170 149 else if (unit.toLower() == "mb") size = size;
Chris@170 150 else if (unit.toLower() == "kb") size = size / 1024;
Chris@170 151 else size = size / 1048576;
Chris@170 152
Chris@170 153 if (isMemFree) available = size;
Chris@170 154 else total = size;
Chris@170 155 }
Chris@170 156 if (available != -1 && total != -1) {
Chris@170 157 fclose(meminfo);
Chris@170 158 return;
Chris@168 159 }
Chris@168 160 }
Chris@169 161 fclose(meminfo);
Chris@171 162
Chris@171 163 return;
Chris@171 164
Chris@171 165 #endif
Chris@171 166 #endif
Chris@168 167 }
Chris@168 168
Chris@168 169 int
Chris@168 170 GetDiscSpaceMBAvailable(const char *path)
Chris@168 171 {
Chris@168 172 #ifdef _WIN32
Chris@168 173 __int64 available, total, totalFree;
Chris@168 174 if (GetDiskFreeSpaceEx(path, &available, &total, &totalFree)) {
Chris@168 175 available /= 1048576;
Chris@170 176 if (available > INT_MAX) available = INT_MAX;
Chris@168 177 return int(available);
Chris@168 178 } else {
Chris@168 179 std::cerr << "WARNING: GetDiskFreeSpaceEx failed: error code "
Chris@168 180 << GetLastError() << std::endl;
Chris@168 181 return -1;
Chris@168 182 }
Chris@168 183 #else
Chris@168 184 struct statvfs buf;
Chris@168 185 if (!statvfs(path, &buf)) {
Chris@168 186 // do the multiplies and divides in this order to reduce the
Chris@168 187 // likelihood of arithmetic overflow
Chris@168 188 std::cerr << "statvfs(" << path << ") says available: " << buf.f_bavail << ", block size: " << buf.f_bsize << std::endl;
Chris@170 189 uint64_t available = ((buf.f_bavail / 1024) * buf.f_bsize) / 1024;
Chris@170 190 if (available > INT_MAX) available = INT_MAX;
Chris@170 191 return int(available);
Chris@168 192 } else {
Chris@168 193 perror("statvfs failed");
Chris@168 194 return -1;
Chris@168 195 }
Chris@168 196 #endif
Chris@168 197 }
Chris@168 198
Chris@168 199
Chris@150 200 double mod(double x, double y) { return x - (y * floor(x / y)); }
Chris@150 201 float modf(float x, float y) { return x - (y * floorf(x / y)); }
Chris@150 202
Chris@150 203 double princarg(double a) { return mod(a + M_PI, -2 * M_PI) + M_PI; }
Chris@150 204 float princargf(float a) { return modf(a + M_PI, -2 * M_PI) + M_PI; }
Chris@150 205