Skip to content

Commit a3cc647

Browse files
author
Venkatesh Duggirala
committed
Bug #18808072 MYSQLBINLOG USES LOCALTIME() TO PRINT EVENTS, CAUSES KERNEL MUTEX CONTENTION
Problem: For every event read, mysqlbinlog calls localtime() which in turn calls stat(/etc/localtime) which is causing kernel mutex contention. Analysis and Fix: localtime() calls stat(/etc/localtime) for every instance of the call where as localtime_r() the reentrant version was optimized to store the read only tz internal structure. Hence it will not call stat(/etc/localtime). It will call only once at the beginning. The mysql server is calling localtime_r() and mysqlbinlog tool is one place where we are still using localtime(). Once the process (mysqlbinlog) is started if timezone is changed it will be not picked up the the process and it will continue with the same values as the beginning of the process. This behavior is in-lined with mysql server. Also adding localtime_r() and gmtime_r() support for windows.
1 parent 0d0c59f commit a3cc647

File tree

5 files changed

+19
-35
lines changed

5 files changed

+19
-35
lines changed

client/mysqlbinlog.cc

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
2+
Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.
33
44
This program is free software; you can redistribute it and/or modify
55
it under the terms of the GNU General Public License as published by
@@ -2102,6 +2102,7 @@ int main(int argc, char** argv)
21022102
DBUG_PROCESS(argv[0]);
21032103

21042104
my_init_time(); // for time functions
2105+
tzset(); // set tzname
21052106

21062107
if (load_defaults("my", load_default_groups, &argc, &argv))
21072108
exit(1);

cmake/os/WindowsCache.cmake

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright (c) 2010, 2011, Oracle and/or its affiliates. All rights reserved.
1+
# Copyright (c) 2010, 2014, Oracle and/or its affiliates. All rights reserved.
22
#
33
# This program is free software; you can redistribute it and/or modify
44
# it under the terms of the GNU General Public License as published by
@@ -88,7 +88,7 @@ SET(HAVE_GETRLIMIT CACHE INTERNAL "")
8888
SET(HAVE_GETRUSAGE CACHE INTERNAL "")
8989
SET(HAVE_GETTIMEOFDAY CACHE INTERNAL "")
9090
SET(HAVE_GETWD CACHE INTERNAL "")
91-
SET(HAVE_GMTIME_R CACHE INTERNAL "")
91+
SET(HAVE_GMTIME_R 1 CACHE INTERNAL "")
9292
SET(HAVE_GRP_H CACHE INTERNAL "")
9393
SET(HAVE_IA64INTRIN_H CACHE INTERNAL "")
9494
SET(HAVE_IEEEFP_H CACHE INTERNAL "")
@@ -109,7 +109,7 @@ SET(HAVE_LANGINFO_H CACHE INTERNAL "")
109109
SET(HAVE_LDIV 1 CACHE INTERNAL "")
110110
SET(HAVE_LIMITS_H 1 CACHE INTERNAL "")
111111
SET(HAVE_LOCALE_H 1 CACHE INTERNAL "")
112-
SET(HAVE_LOCALTIME_R CACHE INTERNAL "")
112+
SET(HAVE_LOCALTIME_R 1 CACHE INTERNAL "")
113113
SET(HAVE_LOG2 CACHE INTERNAL "")
114114
SET(HAVE_LONGJMP 1 CACHE INTERNAL "")
115115
SET(HAVE_LRAND48 CACHE INTERNAL "")

include/my_pthread.h

+13-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
1+
/* Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.
22
33
This program is free software; you can redistribute it and/or modify
44
it under the terms of the GNU General Public License as published by
@@ -142,8 +142,18 @@ int pthread_attr_init(pthread_attr_t *connect_att);
142142
int pthread_attr_setstacksize(pthread_attr_t *connect_att,DWORD stack);
143143
int pthread_attr_destroy(pthread_attr_t *connect_att);
144144
int my_pthread_once(my_pthread_once_t *once_control,void (*init_routine)(void));
145-
struct tm *localtime_r(const time_t *timep,struct tm *tmp);
146-
struct tm *gmtime_r(const time_t *timep,struct tm *tmp);
145+
146+
static inline struct tm *localtime_r(const time_t *timep, struct tm *tmp)
147+
{
148+
localtime_s(tmp, timep);
149+
return tmp;
150+
}
151+
152+
static inline struct tm *gmtime_r(const time_t *clock, struct tm *res)
153+
{
154+
gmtime_s(res, clock);
155+
return res;
156+
}
147157

148158
void pthread_exit(void *a);
149159
int pthread_join(pthread_t thread, void **value_ptr);

mysys/my_wincond.c

+1-23
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
1+
/* Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.
22
33
This program is free software; you can redistribute it and/or modify
44
it under the terms of the GNU General Public License as published by
@@ -344,26 +344,4 @@ int pthread_attr_destroy(pthread_attr_t *connect_att)
344344
return 0;
345345
}
346346

347-
/****************************************************************************
348-
** Fix localtime_r() to be a bit safer
349-
****************************************************************************/
350-
351-
struct tm *localtime_r(const time_t *timep,struct tm *tmp)
352-
{
353-
if (*timep == (time_t) -1) /* This will crash win32 */
354-
{
355-
bzero(tmp,sizeof(*tmp));
356-
}
357-
else
358-
{
359-
struct tm *res=localtime(timep);
360-
if (!res) /* Wrong date */
361-
{
362-
bzero(tmp,sizeof(*tmp)); /* Keep things safe */
363-
return 0;
364-
}
365-
*tmp= *res;
366-
}
367-
return tmp;
368-
}
369347
#endif /* __WIN__ */

sql/log_event.cc

-5
Original file line numberDiff line numberDiff line change
@@ -2162,13 +2162,8 @@ void Log_event::print_timestamp(IO_CACHE* file, time_t* ts)
21622162
DBUG_ENTER("Log_event::print_timestamp");
21632163
if (!ts)
21642164
ts = &when;
2165-
#ifdef MYSQL_SERVER // This is always false
21662165
struct tm tm_tmp;
21672166
localtime_r(ts,(res= &tm_tmp));
2168-
#else
2169-
res=localtime(ts);
2170-
#endif
2171-
21722167
my_b_printf(file,"%02d%02d%02d %2d:%02d:%02d",
21732168
res->tm_year % 100,
21742169
res->tm_mon+1,

0 commit comments

Comments
 (0)