Mercurial > projects > dynamin
annotate dynamin/core/environment.d @ 103:73060bc3f004
Change license to Boost 1.0 and MPL 2.0.
author | Jordan Miner <jminer7@gmail.com> |
---|---|
date | Tue, 15 May 2012 22:06:02 -0500 |
parents | aa4efef0f0b1 |
children | acdbb30fee7e |
rev | line source |
---|---|
0 | 1 |
2 /* | |
103
73060bc3f004
Change license to Boost 1.0 and MPL 2.0.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
3 * Copyright Jordan Miner |
0 | 4 * |
103
73060bc3f004
Change license to Boost 1.0 and MPL 2.0.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
5 * Distributed under the Boost Software License, Version 1.0. |
73060bc3f004
Change license to Boost 1.0 and MPL 2.0.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
6 * (See accompanying file BOOST_LICENSE.txt or copy at |
73060bc3f004
Change license to Boost 1.0 and MPL 2.0.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
7 * http://www.boost.org/LICENSE_1_0.txt) |
0 | 8 * |
9 */ | |
10 | |
11 module dynamin.core.environment; | |
12 | |
13 import dynamin.all_core; | |
14 import dynamin.core_backend; | |
15 | |
16 /** | |
17 * Contains static methods to access information about the computer the | |
18 * application is running on. | |
19 */ | |
20 static class Environment { | |
21 static: | |
22 private: | |
23 mixin EnvironmentBackend; | |
24 public: | |
25 /** | |
26 * Returns the time in milliseconds since the program was started. | |
27 * On Windows XP, this time is updated every millisecond. | |
28 * On Linux, this time is usually updated every millisecond, but | |
29 * occasionally may take 5 to 10 milliseconds. | |
30 * This is the author's dream time function because | |
31 * | |
32 * $(OL | |
33 * $(LI It is accurate to 1 millisecond.) | |
34 * $(LI It works correctly on multiple core computers.) | |
35 * $(LI It is unaffected by changes to the system time.) | |
36 * $(LI It never wraps to zero.) | |
37 * ) | |
38 * | |
39 * On my 1.3 GHz celeron, this function can be called about 480 times | |
40 * in one millisecond under Windows and about 380 times in one millisecond | |
41 * under Linux. | |
42 * | |
43 * TODO: make sure it works with multiple cores, although I'm sure it does | |
44 */ | |
45 long runningTime() { | |
46 return backend_runningTime; | |
47 } | |
48 /** | |
49 * Returns the system time in milliseconds since January 1, 1970 UTC. | |
50 * On Windows XP, this time is only updated every 15.625 milliseconds. | |
51 * | |
52 * On my 1.3 GHz celeron, this function can be called about 12,000 times | |
53 * in one millisecond under Windows and about 460 times in one millisecond | |
54 * under Linux. | |
55 */ | |
56 long systemTime() { | |
57 return backend_systemTime; | |
58 } | |
59 /** | |
60 * Gets the number of logical processors on this computer. A logical | |
61 * processor can either be a different physical processor or simply | |
62 * another core in the same processor. Even a single core hyper-threaded | |
63 * processor is considered to have two logical processors. | |
64 * Returns: the number of logical processors | |
65 */ | |
66 int processorCount() { | |
67 return backend_processorCount; | |
68 } | |
69 /** | |
70 * The number returned by this method can be used to measure the | |
71 * time between two calls. This method uses the highest resolution | |
72 * timer available. | |
73 * | |
74 * On my 1.3 GHz celeron, this function can be called about 500 times | |
75 * in one millisecond under Windows. | |
76 * | |
77 * Returns: the current time in milliseconds | |
78 * | |
79 * Note: Under Windows, this is implemented using QueryPerformanceCounter(). | |
80 * QueryPerformanceCounter() gets the time counter from the processor. | |
81 * On processors with multiple cores (such as an Althon X2 or a Core 2 Duo), | |
82 * the time counter for each core may be a few milliseconds different. | |
83 * (Microsoft's documentation says this is due to bugs in the BIOS or HAL.) | |
84 * Since QueryPerformanceCounter() can get the time from either core, | |
85 * the time between two calls made within the same millisecond can be off. | |
86 * For example, on my Althon X2 computer, the difference between cores | |
87 * is usually 60 ms. If two calls to QueryPerformanceCounter() are made | |
88 * in the same millisecond, there is a possiblity that the second one | |
89 * will return a time 60 ms smaller than the first. | |
90 * Under Linux, this is implemented using gettimeofday(), which has no | |
91 * problems with multiple cores and is accurate. | |
92 * One way to fix this inaccuracy is by only allowing the thread to | |
93 * use one processor. Another problem is that this time will run slightly | |
94 * faster or slower than the system time. | |
95 */ | |
96 private long processorTime() { | |
97 return backend_processorTime; | |
98 } | |
99 } | |
100 | |
101 unittest { | |
102 auto startTime = Environment.runningTime; | |
103 assert(startTime > 0); | |
104 auto time = startTime; | |
105 const SAMPLE = 50; | |
106 // makes sure that RunningTime does not go backwards | |
107 for(int i = 0; i < SAMPLE;) { | |
108 auto time2 = Environment.runningTime; | |
109 assert(time2 >= time); | |
110 if(time2 > time) { | |
111 time = time2; | |
112 ++i; | |
113 } | |
114 } | |
115 //printf("avg accuracy: %.1f ms\n", (time-startTime)/cast(float)SAMPLE); | |
116 } | |
117 |