Spinning Topp Logo BlackTopp Studios
inc
systemcalls.cpp
Go to the documentation of this file.
1 // The DAGFrameScheduler is a Multi-Threaded lock free and wait free scheduling library.
2 // © Copyright 2010 - 2016 BlackTopp Studios Inc.
3 /* This file is part of The DAGFrameScheduler.
4 
5  The DAGFrameScheduler is free software: you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation, either version 3 of the License, or
8  (at your option) any later version.
9 
10  The DAGFrameScheduler is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with The DAGFrameScheduler. If not, see <http://www.gnu.org/licenses/>.
17 */
18 /* The original authors have included a copy of the license specified above in the
19  'doc' folder. See 'gpl.txt'
20 */
21 /* We welcome the use of the DAGFrameScheduler to anyone, including companies who wish to
22  Build professional software and charge for their product.
23 
24  However there are some practical restrictions, so if your project involves
25  any of the following you should contact us and we will try to work something
26  out:
27  - DRM or Copy Protection of any kind(except Copyrights)
28  - Software Patents You Do Not Wish to Freely License
29  - Any Kind of Linking to Non-GPL licensed Works
30  - Are Currently In Violation of Another Copyright Holder's GPL License
31  - If You want to change our code and not add a few hundred MB of stuff to
32  your distribution
33 
34  These and other limitations could cause serious legal problems if you ignore
35  them, so it is best to simply contact us or the Free Software Foundation, if
36  you have any questions.
37 
38  Joseph Toppi - toppij@gmail.com
39  John Blackwood - makoenergy02@gmail.com
40 */
41 #ifndef _systemcalls_cpp
42 #define _systemcalls_cpp
43 
44 /// @file
45 /// @brief This file defines some platform specifc functions
46 
47 #include "datatypes.h"
48 #include "systemcalls.h"
49 #include "crossplatformincludes.h"
50 
51 #ifdef _MEZZ_CPP11_
52  #include<chrono>
53 #else
54  #ifdef _MEZZ_THREAD_WIN32_
55  #include <windows.h>
56  #else
57  #ifdef _MEZZ_THREAD_APPLE_
58  #include <sys/sysctl.h>
59  #endif
60  #include <sys/time.h>
61  #include <unistd.h>
62  #endif
63 #endif
64 
65 namespace Mezzanine
66 {
67  #ifdef _MEZZ_CPP11_
69  { return std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::high_resolution_clock::now()).count(); }
70  #else
71  #ifdef _MEZZ_THREAD_WIN32_
72  namespace
73  {
74  /// @internal
75  class Timer
76  {
77  public:
78  LARGE_INTEGER frequency;
79 
80  Timer()
81  { QueryPerformanceFrequency(&frequency); }
82 
84  {
85  LARGE_INTEGER Current;
86  QueryPerformanceCounter(&Current);
87  return MaxInt(Current.QuadPart * (1000000.0 / frequency.QuadPart));
88  }
89  };
90 
91  static Timer ATimer;
92  }
93 
95  { return ATimer.GetTimeStamp(); }
96 
98  { return Whole(ATimer.frequency.QuadPart/1000); }
99 
100  #else
102  {
103  timeval Now;
104  gettimeofday(&Now, NULL); // Posix says this must return 0, so it seems it can't fail
105  return (Now.tv_sec * 1000000) + Now.tv_usec;
106  }
107 
109  { return 1; } // barring kernel bugs
110 
111  #endif
112  #endif
113 
115  {
116  #ifdef _MEZZ_THREAD_WIN32_
117  SYSTEM_INFO sysinfo;
118  GetSystemInfo( &sysinfo );
119 
120  return sysinfo.dwNumberOfProcessors;
121  #else
122  return sysconf( _SC_NPROCESSORS_ONLN );
123  #endif
124  }
125 
127  {
128  #ifdef _MEZZ_THREAD_WIN32_
129  #ifdef _MSC_VER
130  size_t Size = 0;
131  DWORD buffer_size = 0;
132  DWORD i = 0;
133  SYSTEM_LOGICAL_PROCESSOR_INFORMATION * buffer = 0;
134 
135  GetLogicalProcessorInformation(0, &buffer_size);
136  buffer = (SYSTEM_LOGICAL_PROCESSOR_INFORMATION *)malloc(buffer_size);
137  GetLogicalProcessorInformation(&buffer[0], &buffer_size);
138 
139  for (i = 0; i != buffer_size / sizeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION); ++i) {
140  if (buffer[i].Relationship == RelationCache && buffer[i].Cache.Level == 1) {
141  Size = buffer[i].Cache.Size;
142  break;
143  }
144  }
145 
146  free(buffer);
147  return Size;
148  #else
149  //mingw can't do this, see bug http://sourceforge.net/p/mingw/bugs/1626/
150  // assume 32k
151  return 32768;
152  #endif
153  #else
154  #ifdef _MEZZ_THREAD_APPLE_
155  size_t CacheSize;
156  size_t CSSize = sizeof(CacheSize);
157  if(0==sysctlbyname("hw.l4icachesize", &CacheSize, &CSSize, NULL, 0))
158  {
159  return CacheSize;
160  }else{
161  if(0==sysctlbyname("hw.l3icachesize", &CacheSize, &CSSize, NULL, 0))
162  {
163  return CacheSize;
164  }else{
165  if(0==sysctlbyname("hw.l2icachesize", &CacheSize, &CSSize, NULL, 0))
166  {
167  return CacheSize;
168  }else{
169  if(0==sysctlbyname("hw.l1icachesize", &CacheSize, &CSSize, NULL, 0))
170  {
171  return CacheSize;
172  }else{
173  return 0;
174  }
175  }
176  }
177  }
178  #else
179  Whole CSSize = sysconf(_SC_LEVEL4_CACHE_SIZE);
180  if(!CSSize)
181  {
182  CSSize = sysconf(_SC_LEVEL3_CACHE_SIZE);
183  if(!CSSize)
184  {
185  CSSize = sysconf(_SC_LEVEL2_CACHE_SIZE);
186  if(!CSSize)
187  { CSSize = sysconf(_SC_LEVEL1_DCACHE_SIZE); }
188  }
189  }
190  return CSSize;
191  #endif
192  #endif
193  }
194 
196  {
197  #ifdef _MEZZ_THREAD_WIN32_
198  #ifdef _MSC_VER
199  size_t Size = 0;
200  DWORD buffer_size = 0;
201  DWORD i = 0;
202  SYSTEM_LOGICAL_PROCESSOR_INFORMATION * buffer = 0;
203 
204  GetLogicalProcessorInformation(0, &buffer_size);
205  buffer = (SYSTEM_LOGICAL_PROCESSOR_INFORMATION *)malloc(buffer_size);
206  GetLogicalProcessorInformation(&buffer[0], &buffer_size);
207 
208  for (i = 0; i != buffer_size / sizeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION); ++i)
209  {
210  if (buffer[i].Relationship == RelationCache && buffer[i].Cache.Level == 1)
211  {
212  Size = buffer[i].Cache.LineSize;
213  break;
214  }
215  }
216 
217  free(buffer);
218  return Size;
219  #else
220  //mingw can't do this, see bug http://sourceforge.net/p/mingw/bugs/1626/
221  // assume 64
222  return 64;
223  #endif
224  #else
225  #ifdef _MEZZ_THREAD_APPLE_
226  size_t line_size = 0;
227  size_t sizeof_line_size = sizeof(line_size);
228  sysctlbyname("hw.cachelinesize", &line_size, &sizeof_line_size, 0, 0);
229  return line_size;
230  #else
231  Whole Size = sysconf(_SC_LEVEL1_DCACHE_LINESIZE);
232  if(!Size)
233  {
234  Size = sysconf(_SC_LEVEL2_CACHE_LINESIZE);
235  if(!Size)
236  {
237  Size = sysconf(_SC_LEVEL3_CACHE_LINESIZE);
238  if(!Size)
239  { Size = sysconf(_SC_LEVEL4_CACHE_LINESIZE); }
240  }
241  }
242  return Size;
243  #endif
244  #endif
245  }
246 
247 }//Mezzanine
248 
249 #endif
MaxInt GetTimeStamp()
Get a timestamp, in microseconds. This will generally be some multiple of the GetTimeStampResolution ...
Timer()
Class Constructor.
Definition: timer.cpp:64
This file determines what features are offered by the compiler that this library can take advantage o...
All the definitions for datatypes as well as some basic conversion functions are defined here...
Whole GetCachelineSize()
Get the size of one unit of storage on the CPU cache for purposes of tuning alogrithms.
Whole MEZZ_LIB GetCPUCount()
Get the amount of logical processors, a reasononable amount to use for thread creation.
MaxInt Now()
Get a timestamp, in microseconds. This will generally be some multiple of the GetTimeStampResolution ...
Whole GetCacheSize()
How much storage can be used before RAM must be used.
This file defines a minimalistic cross-platform thread that the scheduler uses to schedule tasks...
#define MEZZ_LIB
Some platforms require special decorations to denote what is exported/imported in a share library...
long long MaxInt
A large integer type suitable for compile time math and long term microsecond time keeping...
Definition: datatypes.h:190
The bulk of the engine components go in this namspace.
Definition: actor.cpp:56
unsigned long Whole
Whole is an unsigned integer, it will be at least 32bits in size.
Definition: datatypes.h:151
Whole GetTimeStampResolution()
Get the resolution of the timestamp in microseconds. This is the smallest amount of time that the Get...