From e7995f213c90ec4e0b1159cb630d6ca9761ee35a Mon Sep 17 00:00:00 2001 From: Ara Ayvazyan Date: Wed, 4 Apr 2018 17:37:40 -0700 Subject: [PATCH 1/2] Avoid low severity log message construction --- include/grpc/support/log.h | 2 ++ src/core/lib/gpr/log.cc | 10 ++++++++-- src/core/lib/gpr/log_android.cc | 4 ++++ src/core/lib/gpr/log_linux.cc | 4 ++++ src/core/lib/gpr/log_posix.cc | 4 ++++ src/core/lib/gpr/log_windows.cc | 5 +++++ 6 files changed, 27 insertions(+), 2 deletions(-) diff --git a/include/grpc/support/log.h b/include/grpc/support/log.h index 2236703db35..7521453b80e 100644 --- a/include/grpc/support/log.h +++ b/include/grpc/support/log.h @@ -61,6 +61,8 @@ GPRAPI const char* gpr_log_severity_string(gpr_log_severity severity); GPRAPI void gpr_log(const char* file, int line, gpr_log_severity severity, const char* format, ...) GPR_PRINT_FORMAT_CHECK(4, 5); +GPRAPI int gpr_should_log(gpr_log_severity severity); + GPRAPI void gpr_log_message(const char* file, int line, gpr_log_severity severity, const char* message); diff --git a/src/core/lib/gpr/log.cc b/src/core/lib/gpr/log.cc index 72787ab7240..01ef112fb31 100644 --- a/src/core/lib/gpr/log.cc +++ b/src/core/lib/gpr/log.cc @@ -44,10 +44,16 @@ const char* gpr_log_severity_string(gpr_log_severity severity) { GPR_UNREACHABLE_CODE(return "UNKNOWN"); } +int gpr_should_log(gpr_log_severity severity) { + return static_cast(severity) >= + gpr_atm_no_barrier_load(&g_min_severity_to_print) + ? 1 + : 0; +} + void gpr_log_message(const char* file, int line, gpr_log_severity severity, const char* message) { - if (static_cast(severity) < - gpr_atm_no_barrier_load(&g_min_severity_to_print)) { + if (gpr_should_log(severity) == 0) { return; } diff --git a/src/core/lib/gpr/log_android.cc b/src/core/lib/gpr/log_android.cc index 0d3ac0fe522..40ef4c640da 100644 --- a/src/core/lib/gpr/log_android.cc +++ b/src/core/lib/gpr/log_android.cc @@ -41,6 +41,10 @@ static android_LogPriority severity_to_log_priority(gpr_log_severity severity) { void gpr_log(const char* file, int line, gpr_log_severity severity, const char* format, ...) { + /* Avoid message construction if gpr_log_message won't log */ + if (gpr_should_log(severity) == 0) { + return; + } char* message = NULL; va_list args; va_start(args, format); diff --git a/src/core/lib/gpr/log_linux.cc b/src/core/lib/gpr/log_linux.cc index e4417d9d5d4..561276f0c20 100644 --- a/src/core/lib/gpr/log_linux.cc +++ b/src/core/lib/gpr/log_linux.cc @@ -44,6 +44,10 @@ static long gettid(void) { return syscall(__NR_gettid); } void gpr_log(const char* file, int line, gpr_log_severity severity, const char* format, ...) { + /* Avoid message construction if gpr_log_message won't log */ + if (gpr_should_log(severity) == 0) { + return; + } char* message = nullptr; va_list args; va_start(args, format); diff --git a/src/core/lib/gpr/log_posix.cc b/src/core/lib/gpr/log_posix.cc index 6f93cdefcdf..0acb2255724 100644 --- a/src/core/lib/gpr/log_posix.cc +++ b/src/core/lib/gpr/log_posix.cc @@ -34,6 +34,10 @@ static intptr_t gettid(void) { return (intptr_t)pthread_self(); } void gpr_log(const char* file, int line, gpr_log_severity severity, const char* format, ...) { + /* Avoid message construction if gpr_log_message won't log */ + if (gpr_should_log(severity) == 0) { + return; + } char buf[64]; char* allocated = nullptr; char* message = nullptr; diff --git a/src/core/lib/gpr/log_windows.cc b/src/core/lib/gpr/log_windows.cc index caaa973f5a8..060be572b80 100644 --- a/src/core/lib/gpr/log_windows.cc +++ b/src/core/lib/gpr/log_windows.cc @@ -34,6 +34,11 @@ void gpr_log(const char* file, int line, gpr_log_severity severity, const char* format, ...) { + /* Avoid message construction if gpr_log_message won't log */ + if (gpr_should_log(severity) == 0) { + return; + } + char* message = NULL; va_list args; int ret; From 3095e350ac96a7fa043dd645e04834a80d165c13 Mon Sep 17 00:00:00 2001 From: Jan Tattermusch Date: Thu, 5 Apr 2018 09:41:03 +0200 Subject: [PATCH 2/2] regenerate projects using tools/buildgen/generate_projects.sh --- grpc.def | 1 + src/ruby/ext/grpc/rb_grpc_imports.generated.c | 2 ++ src/ruby/ext/grpc/rb_grpc_imports.generated.h | 3 +++ test/core/surface/public_headers_must_be_c89.c | 1 + 4 files changed, 7 insertions(+) diff --git a/grpc.def b/grpc.def index d64629f1297..6bdfab06ab9 100644 --- a/grpc.def +++ b/grpc.def @@ -185,6 +185,7 @@ EXPORTS gpr_cpu_current_cpu gpr_log_severity_string gpr_log + gpr_should_log gpr_log_message gpr_set_log_verbosity gpr_log_verbosity_init diff --git a/src/ruby/ext/grpc/rb_grpc_imports.generated.c b/src/ruby/ext/grpc/rb_grpc_imports.generated.c index a12819e87cb..f22979857e0 100644 --- a/src/ruby/ext/grpc/rb_grpc_imports.generated.c +++ b/src/ruby/ext/grpc/rb_grpc_imports.generated.c @@ -208,6 +208,7 @@ gpr_cpu_num_cores_type gpr_cpu_num_cores_import; gpr_cpu_current_cpu_type gpr_cpu_current_cpu_import; gpr_log_severity_string_type gpr_log_severity_string_import; gpr_log_type gpr_log_import; +gpr_should_log_type gpr_should_log_import; gpr_log_message_type gpr_log_message_import; gpr_set_log_verbosity_type gpr_set_log_verbosity_import; gpr_log_verbosity_init_type gpr_log_verbosity_init_import; @@ -449,6 +450,7 @@ void grpc_rb_load_imports(HMODULE library) { gpr_cpu_current_cpu_import = (gpr_cpu_current_cpu_type) GetProcAddress(library, "gpr_cpu_current_cpu"); gpr_log_severity_string_import = (gpr_log_severity_string_type) GetProcAddress(library, "gpr_log_severity_string"); gpr_log_import = (gpr_log_type) GetProcAddress(library, "gpr_log"); + gpr_should_log_import = (gpr_should_log_type) GetProcAddress(library, "gpr_should_log"); gpr_log_message_import = (gpr_log_message_type) GetProcAddress(library, "gpr_log_message"); gpr_set_log_verbosity_import = (gpr_set_log_verbosity_type) GetProcAddress(library, "gpr_set_log_verbosity"); gpr_log_verbosity_init_import = (gpr_log_verbosity_init_type) GetProcAddress(library, "gpr_log_verbosity_init"); diff --git a/src/ruby/ext/grpc/rb_grpc_imports.generated.h b/src/ruby/ext/grpc/rb_grpc_imports.generated.h index 089cb8a61a7..4ae05483866 100644 --- a/src/ruby/ext/grpc/rb_grpc_imports.generated.h +++ b/src/ruby/ext/grpc/rb_grpc_imports.generated.h @@ -599,6 +599,9 @@ extern gpr_log_severity_string_type gpr_log_severity_string_import; typedef void(*gpr_log_type)(const char* file, int line, gpr_log_severity severity, const char* format, ...) GPR_PRINT_FORMAT_CHECK(4, 5); extern gpr_log_type gpr_log_import; #define gpr_log gpr_log_import +typedef int(*gpr_should_log_type)(gpr_log_severity severity); +extern gpr_should_log_type gpr_should_log_import; +#define gpr_should_log gpr_should_log_import typedef void(*gpr_log_message_type)(const char* file, int line, gpr_log_severity severity, const char* message); extern gpr_log_message_type gpr_log_message_import; #define gpr_log_message gpr_log_message_import diff --git a/test/core/surface/public_headers_must_be_c89.c b/test/core/surface/public_headers_must_be_c89.c index 866bee5b2f6..b39ab352c6b 100644 --- a/test/core/surface/public_headers_must_be_c89.c +++ b/test/core/surface/public_headers_must_be_c89.c @@ -243,6 +243,7 @@ int main(int argc, char **argv) { printf("%lx", (unsigned long) gpr_cpu_current_cpu); printf("%lx", (unsigned long) gpr_log_severity_string); printf("%lx", (unsigned long) gpr_log); + printf("%lx", (unsigned long) gpr_should_log); printf("%lx", (unsigned long) gpr_log_message); printf("%lx", (unsigned long) gpr_set_log_verbosity); printf("%lx", (unsigned long) gpr_log_verbosity_init);