Hugintrunk  0.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Utils.cpp
Go to the documentation of this file.
1 /*
2 * Copyright (C) 2007-2008 Anael Orlinski
3 *
4 * This file is part of Panomatic.
5 *
6 * Panomatic is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * Panomatic is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with Panomatic; if not, write to the Free Software
18 * <http://www.gnu.org/licenses/>.
19 */
20 
21 #include "Utils.h"
22 #if defined(HW_NCPU) || defined(__APPLE__)
23 #include <sys/sysctl.h>
24 #endif
25 #ifdef _WIN32
26 #include <windows.h>
27 #include <algorithm>
28 #elif defined __APPLE__
29 #include <CoreServices/CoreServices.h> //for gestalt
30 #else
31 #include <unistd.h>
32 #endif
33 
34 #ifdef _WIN32
35 unsigned long long utils::getTotalMemory()
36 {
37  MEMORYSTATUSEX status;
38  status.dwLength = sizeof(status);
39  GlobalMemoryStatusEx(&status);
40 #ifndef _WIN64
41  // when compiled as 32 bit version, we can only use about 2 GB
42  // even if we have more memory available on a 64 bit system
43  return std::min<unsigned long long>(status.ullTotalPhys, 1500*1024*1024);
44 #else
45  return status.ullTotalPhys;
46 #endif
47 };
48 #elif defined __APPLE__
49 unsigned long long utils::getTotalMemory()
50 {
51  SInt32 ramSize;
52  if(Gestalt(gestaltPhysicalRAMSizeInMegabytes, &ramSize)==noErr)
53  {
54  unsigned long long _ramSize = ramSize;
55  return _ramSize * 1024 * 1024;
56  }
57  else
58  {
59  // if query was not successful return 1 GB,
60  // return 0 would result in crash in calling function
61  return 1024*1024*1024;
62  }
63 };
64 #else
65 unsigned long long utils::getTotalMemory()
66 {
67  long pages = sysconf(_SC_PHYS_PAGES);
68  long page_size = sysconf(_SC_PAGE_SIZE);
69  return pages * page_size;
70 }
71 #endif
72 
73 #if defined _WIN32
74 unsigned int utils::GetPhysicalCPUCount()
75 {
76  // on Windows use GetLogicalProcessorInformationEx to get physical core count
77  PSYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX procInfoTotal = NULL;
78  DWORD length = 0;
79  while (1)
80  {
81  if (GetLogicalProcessorInformationEx(RelationProcessorCore, procInfoTotal, &length))
82  {
83  break;
84  };
85  if (GetLastError() != ERROR_INSUFFICIENT_BUFFER)
86  {
87  return 1;
88  };
89  PSYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX tmpprocInfoTotal = (PSYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX)realloc(procInfoTotal, length);
90  if (!tmpprocInfoTotal)
91  {
92  free(procInfoTotal);
93  return 1;
94  }
95  procInfoTotal = tmpprocInfoTotal;
96  };
97 
98  unsigned int cpuCount = 0;
99  for (PSYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX procInfo = procInfoTotal;
100  (void*)procInfo < (void*)((uintptr_t)procInfoTotal + length);
101  procInfo = (PSYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX)((uintptr_t)procInfo + procInfo->Size))
102  {
103  if (procInfo->Relationship == RelationProcessorCore)
104  {
105  ++cpuCount;
106  };
107  };
108  free(procInfoTotal);
109  return cpuCount > 0 ? cpuCount : 1;
110 }
111 #elif defined(__APPLE__) || defined(__FreeBSD__)
112 #include <sys/sysctl.h>
113 
114 unsigned int utils::GetPhysicalCPUCount()
115 {
116  uint32_t num_cores = 0;
117  size_t num_cores_len = sizeof(num_cores);
118  sysctlbyname("hw.physicalcpu", &num_cores, &num_cores_len, 0, 0);
119  return num_cores;
120 }
121 #else
122 // for Linux, read from /proc/cpuinfo
123 #include <cstring>
124 #include <stdio.h>
125 
127 {
128  char str[256];
129  int cpuCount = 0;
130  FILE* fp;
131  if ((fp = fopen("/proc/cpuinfo", "r")))
132  {
133  while (fgets(str, sizeof(str), fp))
134  {
135  if (memcmp(str, "core id", 7) == 0)
136  {
137  ++cpuCount;
138  };
139  };
140  fclose(fp);
141  };
142  return cpuCount > 0 ? cpuCount : 1;
143 }
144 #endif
unsigned long long getTotalMemory()
returns the total memory in byte
Definition: Utils.cpp:65
unsigned int GetPhysicalCPUCount()
return the number of physical cpu cores
Definition: Utils.cpp:126