annotate system/System.cpp @ 178:0e266fa2510f

* pthread_mutex -> QMutex
author Chris Cannam
date Thu, 05 Oct 2006 11:02:05 +0000
parents ebf55cf0d34d
children 28c38d1e5141
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@150 7 This file copyright 2006 Chris Cannam.
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
Chris@150 28 #include <iostream>
Chris@150 29
Chris@150 30 #ifdef _WIN32
Chris@150 31
Chris@150 32 extern "C" {
Chris@150 33
Chris@150 34 void usleep(unsigned long usec)
Chris@150 35 {
Chris@150 36 ::Sleep(usec / 1000);
Chris@150 37 }
Chris@150 38
Chris@150 39 void gettimeofday(struct timeval *tv, void *tz)
Chris@150 40 {
Chris@150 41 union {
Chris@150 42 long long ns100;
Chris@150 43 FILETIME ft;
Chris@150 44 } now;
Chris@150 45
Chris@150 46 ::GetSystemTimeAsFileTime(&now.ft);
Chris@150 47 tv->tv_usec = (long)((now.ns100 / 10LL) % 1000000LL);
Chris@150 48 tv->tv_sec = (long)((now.ns100 - 116444736000000000LL) / 10000000LL);
Chris@150 49 }
Chris@150 50
Chris@150 51 }
Chris@150 52
Chris@150 53 #endif
Chris@150 54
Chris@150 55 ProcessStatus
Chris@150 56 GetProcessStatus(int pid)
Chris@150 57 {
Chris@150 58 #ifdef _WIN32
Chris@150 59 HANDLE handle = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, pid);
Chris@150 60 if (!handle) {
Chris@150 61 return ProcessNotRunning;
Chris@150 62 } else {
Chris@150 63 CloseHandle(handle);
Chris@150 64 return ProcessRunning;
Chris@150 65 }
Chris@150 66 #else
Chris@150 67 if (kill(getpid(), 0) == 0) {
Chris@150 68 if (kill(pid, 0) == 0) {
Chris@150 69 return ProcessRunning;
Chris@150 70 } else {
Chris@150 71 return ProcessNotRunning;
Chris@150 72 }
Chris@150 73 } else {
Chris@150 74 return UnknownProcessStatus;
Chris@150 75 }
Chris@150 76 #endif
Chris@150 77 }
Chris@150 78
Chris@170 79 void
Chris@170 80 GetRealMemoryMBAvailable(int &available, int &total)
Chris@168 81 {
Chris@170 82 available = -1;
Chris@170 83 total = -1;
Chris@170 84
Chris@171 85 #ifdef _WIN32
Chris@171 86
Chris@171 87 MEMORYSTATUSEX status;
Chris@171 88 status.dwLength = sizeof(status);
Chris@171 89 if (!GlobalMemoryStatusEx(&status)) {
Chris@171 90 std::cerr << "WARNING: GetDiskFreeSpaceEx failed: error code "
Chris@171 91 << GetLastError() << std::endl;
Chris@171 92 return;
Chris@171 93 }
Chris@171 94 DWORDLONG size = status.ullAvailPhys / 1048576;
Chris@171 95 if (size > INT_MAX) size = INT_MAX;
Chris@171 96 available = int(size);
Chris@171 97
Chris@171 98 size = status.ullTotalPhys / 1048576;
Chris@171 99 if (size > INT_MAX) size = INT_MAX;
Chris@171 100 total = int(size);
Chris@171 101
Chris@171 102 return;
Chris@171 103
Chris@171 104 #else
Chris@171 105 #ifdef __APPLE__
Chris@171 106
Chris@171 107 unsigned int val;
Chris@171 108 int mib[2];
Chris@171 109 size_t size_sys;
Chris@171 110
Chris@171 111 mib[0] = CTL_HW;
Chris@171 112
Chris@171 113 mib[1] = HW_PHYSMEM;
Chris@171 114 size_sys = sizeof(val);
Chris@171 115 sysctl(mib, 2, &val, &size_sys, NULL, 0);
Chris@171 116 if (val) total = val / 1048576;
Chris@171 117
Chris@171 118 mib[1] = HW_USERMEM;
Chris@171 119 size_sys = sizeof(val);
Chris@171 120 sysctl(mib, 2, &val, &size_sys, NULL, 0);
Chris@171 121 if (val) available = val / 1048576;
Chris@171 122
Chris@171 123 return;
Chris@171 124
Chris@171 125 #else
Chris@171 126
Chris@169 127 FILE *meminfo = fopen("/proc/meminfo", "r");
Chris@170 128 if (!meminfo) return;
Chris@169 129
Chris@169 130 char buf[256];
Chris@169 131 while (!feof(meminfo)) {
Chris@169 132 fgets(buf, 256, meminfo);
Chris@170 133 bool isMemFree = (strncmp(buf, "MemFree:", 8) == 0);
Chris@170 134 bool isMemTotal = (!isMemFree && (strncmp(buf, "MemTotal:", 9) == 0));
Chris@170 135 if (isMemFree || isMemTotal) {
Chris@169 136 QString line = QString(buf).trimmed();
Chris@169 137 QStringList elements = line.split(' ', QString::SkipEmptyParts);
Chris@169 138 QString unit = "kB";
Chris@169 139 if (elements.size() > 2) unit = elements[2];
Chris@169 140 int size = elements[1].toInt();
Chris@169 141 // std::cerr << "have size \"" << size << "\", unit \""
Chris@169 142 // << unit.toStdString() << "\"" << std::endl;
Chris@170 143 if (unit.toLower() == "gb") size = size * 1024;
Chris@170 144 else if (unit.toLower() == "mb") size = size;
Chris@170 145 else if (unit.toLower() == "kb") size = size / 1024;
Chris@170 146 else size = size / 1048576;
Chris@170 147
Chris@170 148 if (isMemFree) available = size;
Chris@170 149 else total = size;
Chris@170 150 }
Chris@170 151 if (available != -1 && total != -1) {
Chris@170 152 fclose(meminfo);
Chris@170 153 return;
Chris@168 154 }
Chris@168 155 }
Chris@169 156 fclose(meminfo);
Chris@171 157
Chris@171 158 return;
Chris@171 159
Chris@171 160 #endif
Chris@171 161 #endif
Chris@168 162 }
Chris@168 163
Chris@168 164 int
Chris@168 165 GetDiscSpaceMBAvailable(const char *path)
Chris@168 166 {
Chris@168 167 #ifdef _WIN32
Chris@168 168 __int64 available, total, totalFree;
Chris@168 169 if (GetDiskFreeSpaceEx(path, &available, &total, &totalFree)) {
Chris@168 170 available /= 1048576;
Chris@170 171 if (available > INT_MAX) available = INT_MAX;
Chris@168 172 return int(available);
Chris@168 173 } else {
Chris@168 174 std::cerr << "WARNING: GetDiskFreeSpaceEx failed: error code "
Chris@168 175 << GetLastError() << std::endl;
Chris@168 176 return -1;
Chris@168 177 }
Chris@168 178 #else
Chris@168 179 struct statvfs buf;
Chris@168 180 if (!statvfs(path, &buf)) {
Chris@168 181 // do the multiplies and divides in this order to reduce the
Chris@168 182 // likelihood of arithmetic overflow
Chris@168 183 std::cerr << "statvfs(" << path << ") says available: " << buf.f_bavail << ", block size: " << buf.f_bsize << std::endl;
Chris@170 184 uint64_t available = ((buf.f_bavail / 1024) * buf.f_bsize) / 1024;
Chris@170 185 if (available > INT_MAX) available = INT_MAX;
Chris@170 186 return int(available);
Chris@168 187 } else {
Chris@168 188 perror("statvfs failed");
Chris@168 189 return -1;
Chris@168 190 }
Chris@168 191 #endif
Chris@168 192 }
Chris@168 193
Chris@168 194
Chris@150 195 double mod(double x, double y) { return x - (y * floor(x / y)); }
Chris@150 196 float modf(float x, float y) { return x - (y * floorf(x / y)); }
Chris@150 197
Chris@150 198 double princarg(double a) { return mod(a + M_PI, -2 * M_PI) + M_PI; }
Chris@150 199 float princargf(float a) { return modf(a + M_PI, -2 * M_PI) + M_PI; }
Chris@150 200