]> git.sesse.net Git - stockfish/blob - src/misc.cpp
Destroy all locks before to exit
[stockfish] / src / misc.cpp
1 /*
2   Stockfish, a UCI chess playing engine derived from Glaurung 2.1
3   Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
4   Copyright (C) 2008-2009 Marco Costalba
5
6   Stockfish 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 3 of the License, or
9   (at your option) any later version.
10
11   Stockfish 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 this program.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20
21 ////
22 //// Includes
23 ////
24
25 #if !defined(_MSC_VER)
26
27 #  include <sys/time.h>
28 #  include <sys/types.h>
29 #  include <unistd.h>
30 #  if defined(__hpux)
31 #     include <sys/pstat.h>
32 #  endif
33
34 #else
35
36 #define _CRT_SECURE_NO_DEPRECATE
37 #include <windows.h>
38 #include <sys/timeb.h>
39
40 #endif
41
42 #include <cassert>
43 #include <cstdio>
44 #include <iomanip>
45 #include <iostream>
46 #include <sstream>
47
48 #include "bitcount.h"
49 #include "misc.h"
50
51 using namespace std;
52
53 /// Version number. If this is left empty, the current date (in the format
54 /// YYMMDD) is used as a version number.
55
56 static const string EngineVersion = "";
57 static const string AppName = "Stockfish";
58 static const string AppTag  = "";
59
60
61 ////
62 //// Variables
63 ////
64
65 bool Chess960;
66
67 uint64_t dbg_cnt0 = 0;
68 uint64_t dbg_cnt1 = 0;
69
70 bool dbg_show_mean = false;
71 bool dbg_show_hit_rate = false;
72
73
74 ////
75 //// Functions
76 ////
77
78 void dbg_hit_on(bool b) {
79
80     assert(!dbg_show_mean);
81     dbg_show_hit_rate = true;
82     dbg_cnt0++;
83     if (b)
84         dbg_cnt1++;
85 }
86
87 void dbg_hit_on_c(bool c, bool b) {
88
89     if (c)
90         dbg_hit_on(b);
91 }
92
93 void dbg_before() {
94
95     assert(!dbg_show_mean);
96     dbg_show_hit_rate = true;
97     dbg_cnt0++;
98 }
99
100 void dbg_after() {
101
102     assert(!dbg_show_mean);
103     dbg_show_hit_rate = true;
104     dbg_cnt1++;
105 }
106
107 void dbg_mean_of(int v) {
108
109     assert(!dbg_show_hit_rate);
110     dbg_show_mean = true;
111     dbg_cnt0++;
112     dbg_cnt1 += v;
113 }
114
115 void dbg_print_hit_rate() {
116
117   cout << "Total " << dbg_cnt0 << " Hit " << dbg_cnt1
118        << " hit rate (%) " << (dbg_cnt1*100)/(dbg_cnt0 ? dbg_cnt0 : 1) << endl;
119 }
120
121 void dbg_print_mean() {
122
123   cout << "Total " << dbg_cnt0 << " Mean "
124        << (float)dbg_cnt1 / (dbg_cnt0 ? dbg_cnt0 : 1) << endl;
125 }
126
127 void dbg_print_hit_rate(ofstream& logFile) {
128
129   logFile << "Total " << dbg_cnt0 << " Hit " << dbg_cnt1
130           << " hit rate (%) " << (dbg_cnt1*100)/(dbg_cnt0 ? dbg_cnt0 : 1) << endl;
131 }
132
133 void dbg_print_mean(ofstream& logFile) {
134
135   logFile << "Total " << dbg_cnt0 << " Mean "
136           << (float)dbg_cnt1 / (dbg_cnt0 ? dbg_cnt0 : 1) << endl;
137 }
138
139 /// engine_name() returns the full name of the current Stockfish version.
140 /// This will be either "Stockfish YYMMDD" (where YYMMDD is the date when the
141 /// program was compiled) or "Stockfish <version number>", depending on whether
142 /// the constant EngineVersion (defined in misc.h) is empty.
143
144 const string engine_name() {
145
146   const string cpu64(CpuHas64BitPath ? " 64bit" : "");
147
148   if (!EngineVersion.empty())
149       return AppName+ " " + EngineVersion + cpu64;
150
151   string date(__DATE__); // From compiler, format is "Sep 21 2008"
152   string months("Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec");
153
154   size_t mon = 1 + months.find(date.substr(0, 3)) / 4;
155
156   stringstream s;
157   string day = (date[4] == ' ' ? date.substr(5, 1) : date.substr(4, 2));
158
159   string name = AppName + " " + AppTag + " ";
160
161   s << name << date.substr(date.length() - 2) << setfill('0')
162     << setw(2) << mon << setw(2) << day << cpu64;
163
164   return s.str();
165 }
166
167
168 /// get_system_time() returns the current system time, measured in
169 /// milliseconds.
170
171 int get_system_time() {
172
173 #if defined(_MSC_VER)
174     struct _timeb t;
175     _ftime(&t);
176     return int(t.time*1000 + t.millitm);
177 #else
178     struct timeval t;
179     gettimeofday(&t, NULL);
180     return t.tv_sec*1000 + t.tv_usec/1000;
181 #endif
182 }
183
184
185 /// cpu_count() tries to detect the number of CPU cores.
186
187 #if !defined(_MSC_VER)
188
189 #  if defined(_SC_NPROCESSORS_ONLN)
190 int cpu_count() {
191   return Min(sysconf(_SC_NPROCESSORS_ONLN), 8);
192 }
193 #  elif defined(__hpux)
194 int cpu_count() {
195   struct pst_dynamic psd;
196   if (pstat_getdynamic(&psd, sizeof(psd), (size_t)1, 0) == -1)
197       return 1;
198
199   return Min(psd.psd_proc_cnt, 8);
200 }
201 #  else
202 int cpu_count() {
203   return 1;
204 }
205 #  endif
206
207 #else
208
209 int cpu_count() {
210   SYSTEM_INFO s;
211   GetSystemInfo(&s);
212   return Min(s.dwNumberOfProcessors, 8);
213 }
214
215 #endif
216
217
218 /*
219   From Beowulf, from Olithink
220 */
221 #ifndef _WIN32
222 /* Non-windows version */
223 int Bioskey()
224 {
225   fd_set          readfds;
226   struct timeval  timeout;
227
228   FD_ZERO(&readfds);
229   FD_SET(fileno(stdin), &readfds);
230   /* Set to timeout immediately */
231   timeout.tv_sec = 0;
232   timeout.tv_usec = 0;
233   select(16, &readfds, 0, 0, &timeout);
234
235   return (FD_ISSET(fileno(stdin), &readfds));
236 }
237
238 #else
239 /* Windows-version */
240 #include <windows.h>
241 #include <conio.h>
242 int Bioskey()
243 {
244     static int      init = 0,
245                     pipe;
246     static HANDLE   inh;
247     DWORD           dw;
248     /* If we're running under XBoard then we can't use _kbhit() as the input
249      * commands are sent to us directly over the internal pipe */
250
251 #if defined(FILE_CNT)
252     if (stdin->_cnt > 0)
253         return stdin->_cnt;
254 #endif
255     if (!init) {
256         init = 1;
257         inh = GetStdHandle(STD_INPUT_HANDLE);
258         pipe = !GetConsoleMode(inh, &dw);
259         if (!pipe) {
260             SetConsoleMode(inh, dw & ~(ENABLE_MOUSE_INPUT | ENABLE_WINDOW_INPUT));
261             FlushConsoleInputBuffer(inh);
262         }
263     }
264     if (pipe) {
265         if (!PeekNamedPipe(inh, NULL, 0, NULL, &dw, NULL))
266             return 1;
267         return dw;
268     } else {
269         // Count the number of unread input records, including keyboard,
270         // mouse, and window-resizing input records.
271         GetNumberOfConsoleInputEvents(inh, &dw);
272         if (dw <= 0)
273             return 0;
274
275         // Read data from console without removing it from the buffer
276         INPUT_RECORD rec[256];
277         DWORD recCnt;
278         if (!PeekConsoleInput(inh, rec, Min(dw, 256), &recCnt))
279             return 0;
280
281         // Search for at least one keyboard event
282         for (DWORD i = 0; i < recCnt; i++)
283             if (rec[i].EventType == KEY_EVENT)
284                 return 1;
285
286         return 0;
287     }
288 }
289 #endif