16 #include "mysys_priv.h"
17 #include "my_static.h"
18 #include "mysys_err.h"
28 #pragma comment(lib, "ws2_32")
31 static void my_win_init(
void);
32 static my_bool win32_init_tcp_ip();
38 #define SCALE_USEC 10000
40 my_bool my_init_done= 0;
41 ulong my_thread_stack_size= 65536;
43 static ulong atoi_octal(
const char *str)
46 while (*str && my_isspace(&my_charset_latin1, *str))
49 (*str ==
'0' ? 8 : 10),
78 if ((str= getenv(
"UMASK")) != 0)
79 my_umask= (int) (atoi_octal(str) | 0600);
81 if ((str= getenv(
"UMASK_DIR")) != 0)
82 my_umask_dir= (
int) (atoi_octal(str) | 0700);
86 instrumented_stdin.
m_file= stdin;
87 instrumented_stdin.
m_psi= NULL;
88 mysql_stdin= & instrumented_stdin;
90 if (my_thread_global_init())
93 #if defined(SAFE_MUTEX)
94 safe_mutex_global_init();
97 #if defined(MY_PTHREAD_FASTMUTEX) && !defined(SAFE_MUTEX)
98 fastmutex_global_init();
102 if ((home_dir= getenv(
"HOME")) != 0)
103 home_dir= intern_filename(home_dir_buff, home_dir);
106 DBUG_ENTER(
"my_init");
107 DBUG_PROCESS((
char*) (my_progname ? my_progname :
"unknown"));
109 DBUG_PRINT(
"exit", (
"home: '%s'", home_dir));
120 void my_end(
int infoflag)
127 FILE *info_file= DBUG_FILE;
137 DBUG_PRINT(
"info",(
"Shutting down: infoflag: %d print_info: %d",
138 infoflag, print_info));
145 if ((infoflag & MY_CHECK_ERROR) || print_info)
148 if (my_file_opened | my_stream_opened)
151 my_snprintf(ebuff,
sizeof(ebuff), EE(EE_OPEN_WARNING),
152 my_file_opened, my_stream_opened);
153 my_message_stderr(EE_OPEN_WARNING, ebuff, ME_BELL);
154 DBUG_PRINT(
"error", (
"%s", ebuff));
155 my_print_open_files();
159 my_error_unregister_all();
162 if ((infoflag & MY_GIVE_INFO) || print_info)
164 #ifdef HAVE_GETRUSAGE
168 memset(&rus, 0,
sizeof(rus));
170 if (!getrusage(RUSAGE_SELF, &rus))
171 fprintf(info_file,
"\n\
172 User time %.2f, System time %.2f\n\
173 Maximum resident set size %ld, Integral resident set size %ld\n\
174 Non-physical pagefaults %ld, Physical pagefaults %ld, Swaps %ld\n\
175 Blocks in %ld out %ld, Messages in %ld out %ld, Signals %ld\n\
176 Voluntary context switches %ld, Involuntary context switches %ld\n",
177 (rus.ru_utime.tv_sec * SCALE_SEC +
178 rus.ru_utime.tv_usec / SCALE_USEC) / 100.0,
179 (rus.ru_stime.tv_sec * SCALE_SEC +
180 rus.ru_stime.tv_usec / SCALE_USEC) / 100.0,
181 rus.ru_maxrss, rus.ru_idrss,
182 rus.ru_minflt, rus.ru_majflt,
183 rus.ru_nswap, rus.ru_inblock, rus.ru_oublock,
184 rus.ru_msgsnd, rus.ru_msgrcv, rus.ru_nsignals,
185 rus.ru_nvcsw, rus.ru_nivcsw);
187 #if defined(__WIN__) && defined(_MSC_VER)
188 _CrtSetReportMode( _CRT_WARN, _CRTDBG_MODE_FILE );
189 _CrtSetReportFile( _CRT_WARN, _CRTDBG_FILE_STDERR );
190 _CrtSetReportMode( _CRT_ERROR, _CRTDBG_MODE_FILE );
191 _CrtSetReportFile( _CRT_ERROR, _CRTDBG_FILE_STDERR );
192 _CrtSetReportMode( _CRT_ASSERT, _CRTDBG_MODE_FILE );
193 _CrtSetReportFile( _CRT_ASSERT, _CRTDBG_FILE_STDERR );
195 _CrtDumpMemoryLeaks();
199 if (!(infoflag & MY_DONT_FREE_DBUG))
205 my_thread_global_end();
206 #if defined(SAFE_MUTEX)
211 safe_mutex_end((infoflag & (MY_GIVE_INFO | MY_CHECK_ERROR)) ? stderr :
236 void my_parameter_handler(
const wchar_t * expression,
const wchar_t *
function,
237 const wchar_t *
file,
unsigned int line,
240 DBUG_PRINT(
"my",(
"Expression: %s function: %s file: %s, line: %d",
241 expression,
function, file, line));
245 #ifdef __MSVC_RUNTIME_CHECKS
249 #pragma runtime_checks("", off)
256 int handle_rtc_failure(
int err_type,
const char *file,
int line,
257 const char* module,
const char *format, ...)
260 va_start(args, format);
261 fprintf(stderr,
"Error:");
262 vfprintf(stderr, format, args);
263 fprintf(stderr,
" At %s:%d\n", file, line);
265 (void) fflush(stderr);
269 #pragma runtime_checks("", restore)
272 #define OFFSET_TO_EPOC ((__int64) 134774 * 24 * 60 * 60 * 1000 * 1000 * 10)
275 static void win_init_time(
void)
279 LARGE_INTEGER li, t_cnt;
281 DBUG_ASSERT(
sizeof(LARGE_INTEGER) ==
sizeof(query_performance_frequency));
283 if (QueryPerformanceFrequency((LARGE_INTEGER *)&query_performance_frequency) == 0)
284 query_performance_frequency= 0;
287 GetSystemTimeAsFileTime(&ft);
288 li.LowPart= ft.dwLowDateTime;
289 li.HighPart= ft.dwHighDateTime;
290 query_performance_offset= li.QuadPart-OFFSET_TO_EPOC;
291 QueryPerformanceCounter(&t_cnt);
292 query_performance_offset-= (t_cnt.QuadPart /
293 query_performance_frequency * MS +
295 query_performance_frequency * MS /
296 query_performance_frequency);
305 static void win_init_registry(
void)
309 if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, (LPCTSTR)
"SOFTWARE\\MySQL",
310 0, KEY_READ, &key_handle) == ERROR_SUCCESS)
315 char key_name[256], key_data[1024];
316 DWORD key_name_len=
sizeof(key_name) - 1;
317 DWORD key_data_len=
sizeof(key_data) - 1;
319 while ((ret= RegEnumValue(key_handle, index++,
320 key_name, &key_name_len,
321 NULL, &type, (LPBYTE)&key_data,
322 &key_data_len)) != ERROR_NO_MORE_ITEMS)
324 char env_string[
sizeof(key_name) +
sizeof(key_data) + 2];
326 if (ret == ERROR_MORE_DATA)
329 DBUG_PRINT(
"error", (
"Skipped registry value that was too large"));
331 else if (ret == ERROR_SUCCESS)
335 strxmov(env_string, key_name,
"=", key_data, NullS);
338 putenv(strdup(env_string)) ;
347 key_name_len=
sizeof(key_name) - 1;
348 key_data_len=
sizeof(key_data) - 1;
351 RegCloseKey(key_handle);
356 static void my_win_init(
void)
358 DBUG_ENTER(
"my_win_init");
360 #if defined(_MSC_VER)
370 _set_invalid_parameter_handler(my_parameter_handler);
374 #ifdef __MSVC_RUNTIME_CHECKS
379 _RTC_SetErrorFunc(handle_rtc_failure);
398 #define TCPIPKEY "SYSTEM\\CurrentControlSet\\Services\\Tcpip\\Parameters"
399 #define WINSOCK2KEY "SYSTEM\\CurrentControlSet\\Services\\Winsock2\\Parameters"
400 #define WINSOCKKEY "SYSTEM\\CurrentControlSet\\Services\\Winsock\\Parameters"
402 static my_bool win32_have_tcpip(
void)
405 if (RegOpenKeyEx ( HKEY_LOCAL_MACHINE, TCPIPKEY, 0, KEY_READ,
406 &hTcpipRegKey) != ERROR_SUCCESS)
408 if (RegOpenKeyEx ( HKEY_LOCAL_MACHINE, WINSOCK2KEY, 0, KEY_READ,
409 &hTcpipRegKey) != ERROR_SUCCESS)
411 if (RegOpenKeyEx ( HKEY_LOCAL_MACHINE, WINSOCKKEY, 0, KEY_READ,
412 &hTcpipRegKey) != ERROR_SUCCESS)
413 if (!getenv(
"HAVE_TCPIP") || have_tcpip)
417 RegCloseKey ( hTcpipRegKey);
422 static my_bool win32_init_tcp_ip()
424 if (win32_have_tcpip())
426 WORD wVersionRequested = MAKEWORD( 2, 2 );
430 if (WSAStartup( wVersionRequested, &wsaData ))
435 if (!WSAStartup( wVersionRequested, &wsaData ))
440 if (wsaData.wVersion != wVersionRequested)
444 if (!WSAStartup( wVersionRequested, &wsaData ))
456 {0,
"Waiting for table level lock", 0};
458 #ifdef HAVE_PSI_INTERFACE
460 #if !defined(HAVE_PREAD) && !defined(_WIN32)
461 PSI_mutex_key key_my_file_info_mutex;
464 #if !defined(HAVE_LOCALTIME_R) || !defined(HAVE_GMTIME_R)
465 PSI_mutex_key key_LOCK_localtime_r;
468 PSI_mutex_key key_BITMAP_mutex, key_IO_CACHE_append_buffer_lock,
469 key_IO_CACHE_SHARE_mutex, key_KEY_CACHE_cache_lock, key_LOCK_alarm,
470 key_my_thread_var_mutex, key_THR_LOCK_charset, key_THR_LOCK_heap,
471 key_THR_LOCK_lock, key_THR_LOCK_malloc,
472 key_THR_LOCK_mutex, key_THR_LOCK_myisam, key_THR_LOCK_net,
473 key_THR_LOCK_open, key_THR_LOCK_threads,
474 key_TMPDIR_mutex, key_THR_LOCK_myisam_mmap;
476 static PSI_mutex_info all_mysys_mutexes[]=
478 #if !defined(HAVE_PREAD) && !defined(_WIN32)
479 { &key_my_file_info_mutex,
"st_my_file_info:mutex", 0},
481 #if !defined(HAVE_LOCALTIME_R) || !defined(HAVE_GMTIME_R)
482 { &key_LOCK_localtime_r,
"LOCK_localtime_r", PSI_FLAG_GLOBAL},
484 { &key_BITMAP_mutex,
"BITMAP::mutex", 0},
485 { &key_IO_CACHE_append_buffer_lock,
"IO_CACHE::append_buffer_lock", 0},
486 { &key_IO_CACHE_SHARE_mutex,
"IO_CACHE::SHARE_mutex", 0},
487 { &key_KEY_CACHE_cache_lock,
"KEY_CACHE::cache_lock", 0},
488 { &key_LOCK_alarm,
"LOCK_alarm", PSI_FLAG_GLOBAL},
489 { &key_my_thread_var_mutex,
"my_thread_var::mutex", 0},
490 { &key_THR_LOCK_charset,
"THR_LOCK_charset", PSI_FLAG_GLOBAL},
491 { &key_THR_LOCK_heap,
"THR_LOCK_heap", PSI_FLAG_GLOBAL},
492 { &key_THR_LOCK_lock,
"THR_LOCK_lock", PSI_FLAG_GLOBAL},
493 { &key_THR_LOCK_malloc,
"THR_LOCK_malloc", PSI_FLAG_GLOBAL},
494 { &key_THR_LOCK_mutex,
"THR_LOCK::mutex", 0},
495 { &key_THR_LOCK_myisam,
"THR_LOCK_myisam", PSI_FLAG_GLOBAL},
496 { &key_THR_LOCK_net,
"THR_LOCK_net", PSI_FLAG_GLOBAL},
497 { &key_THR_LOCK_open,
"THR_LOCK_open", PSI_FLAG_GLOBAL},
498 { &key_THR_LOCK_threads,
"THR_LOCK_threads", PSI_FLAG_GLOBAL},
499 { &key_TMPDIR_mutex,
"TMPDIR_mutex", PSI_FLAG_GLOBAL},
500 { &key_THR_LOCK_myisam_mmap,
"THR_LOCK_myisam_mmap", PSI_FLAG_GLOBAL}
503 PSI_cond_key key_COND_alarm, key_IO_CACHE_SHARE_cond,
504 key_IO_CACHE_SHARE_cond_writer, key_my_thread_var_suspend,
505 key_THR_COND_threads;
507 static PSI_cond_info all_mysys_conds[]=
509 { &key_COND_alarm,
"COND_alarm", PSI_FLAG_GLOBAL},
510 { &key_IO_CACHE_SHARE_cond,
"IO_CACHE_SHARE::cond", 0},
511 { &key_IO_CACHE_SHARE_cond_writer,
"IO_CACHE_SHARE::cond_writer", 0},
512 { &key_my_thread_var_suspend,
"my_thread_var::suspend", 0},
513 { &key_THR_COND_threads,
"THR_COND_threads", 0}
516 #ifdef USE_ALARM_THREAD
517 PSI_thread_key key_thread_alarm;
519 static PSI_thread_info all_mysys_threads[]=
521 { &key_thread_alarm,
"alarm", PSI_FLAG_GLOBAL}
525 #ifdef HUGETLB_USE_PROC_MEMINFO
526 PSI_file_key key_file_proc_meminfo;
528 PSI_file_key key_file_charset, key_file_cnf;
530 static PSI_file_info all_mysys_files[]=
532 #ifdef HUGETLB_USE_PROC_MEMINFO
533 { &key_file_proc_meminfo,
"proc_meminfo", 0},
535 { &key_file_charset,
"charset", 0},
536 { &key_file_cnf,
"cnf", 0}
541 & stage_waiting_for_table_level_lock
544 void my_init_mysys_psi_keys()
546 const char* category=
"mysys";
549 count=
sizeof(all_mysys_mutexes)/
sizeof(all_mysys_mutexes[0]);
552 count=
sizeof(all_mysys_conds)/
sizeof(all_mysys_conds[0]);
555 #ifdef USE_ALARM_THREAD
556 count=
sizeof(all_mysys_threads)/
sizeof(all_mysys_threads[0]);
560 count=
sizeof(all_mysys_files)/
sizeof(all_mysys_files[0]);
563 count= array_elements(all_mysys_stages);