C++ 的 runtime exception是没有扩展性的

Stella981
• 阅读 690

https://groups.google.com/forum/#!topic/seastar-dev/RuK-OajeqHk

https://www.google.com/search?ei=gTH-Wtr5O4WQsAW-io7wCQ&q=dl\_iterate\_phdr+gcc+exception&oq=dl\_iterate\_phdr+gcc+exception&gs\_l=psy-ab.3...5055.7797.0.8041.10.8.0.0.0.0.382.997.3-3.3.0....0...1.1.64.psy-ab..7.1.306...33i160k1.0.dDZSKQ-pAA4

g++/glibc combination provides a c++ runtime with non scaleable 
c++ exception. This is dues to global locks in stack unwinding code. 
    
To summarize all the locks we have now and their purpose: 
 1. There is a lock in _Unwind_Find_FDE (libgcc) that protects 
    list of FDEs registered with __register_frame* functions. 
    The catch is that dynamically linked binary do not do that, 
    so all that it protects is checking that a certain list is empty. 
    This lock no longer relevant in gcc7 since there is a path there 
    checks that list is empty outside of the lock. 
 2. The lock in dl_iterate_phdr (glibc) that protects loaded object 
    list against runtime object loading/unloading. 

To get rid of the first lock one has to use gcc7. 

To get rid of the second one we can use the fact that we do not 
load/unload objects dynamically (at least for now). To do that we 
can mirror all elf header information in seastar and provide our 
own dl_iterate_phdr symbol which uses this mirror without locking. 
    
Unfortunately there is another gotcha in this approach: dl_iterate_phdr 
supplied by glibc never calls more then one callback simultaneously as an 
unintended consequences of the lock there, but unfortunately libgcc relies 
on that to maintain small cache of translations. The access to the cache is 
not protected by any lock since up until now only one callback could have 
run at a time. But luckily libgcc cannot use the cache if older version 
of dl_phdr_info is provided to the callback because the older version 
did not have an indication that loaded object list may have changed, 
so libgcc does not know when cache should be invalidated and disables it 
entirely. By calling the callback with old version of dl_phdr_info from 
our dl_iterate_phdr we can effectively make libgcc callback thread safe. 

diff --git a/configure.py b/configure.py 
index facdd8f..33b310b 100755 
--- a/configure.py 
+++ b/configure.py 
@@ -297,6 +297,8 @@ arg_parser.add_argument('--static-boost', dest = 'staticboost', action = 'store_ 
 add_tristate(arg_parser, name = 'hwloc', dest = 'hwloc', help = 'hwloc support') 
 arg_parser.add_argument('--enable-gcc6-concepts', dest='gcc6_concepts', action='store_true', default=False, 
                         help='enable experimental support for C++ Concepts as implemented in GCC 6') 
+arg_parser.add_argument('--disable-exception-scalability-workaround', dest='exception_workaround', action='store_true', default=False, 
+        help='disabling override of dl_iterate_phdr symbol to workaround C++ exception scalability issues') 
 args = arg_parser.parse_args() 
  
 libnet = [ 
@@ -337,6 +339,7 @@ core = [ 
     'net/inet_address.cc', 
     'rpc/rpc.cc', 
     'rpc/lz4_compressor.cc', 
+    'core/exception_hacks.cc', 
     ] 
  
 protobuf = [ 
@@ -392,6 +395,9 @@ if args.gcc6_concepts: 
     defines.append('HAVE_GCC6_CONCEPTS') 
     args.user_cflags += ' -fconcepts' 
  
+if args.exception_workaround: 
+    defines.append('NO_EXCEPTION_HACK') 

 if args.staticcxx: 
     libs = libs.replace('-lstdc++', '') 
     libs += ' -static-libgcc -static-libstdc++' 
diff --git a/core/exception_hacks.cc b/core/exception_hacks.cc 
new file mode 100644 
index 0000000..6f04630 
--- /dev/null 
+++ b/core/exception_hacks.cc 
@@ -0,0 +1,108 @@ 
+/* 
+ * This file is open source software, licensed to you under the terms 
+ * of the Apache License, Version 2.0 (the "License").  See the NOTICE file 
+ * distributed with this work for additional information regarding copyright 
+ * ownership.  You may not use this file except in compliance with the License. 
+ * 
+ * You may obtain a copy of the License at 
+ * 
+ *   http://www.apache.org/licenses/LICENSE-2.0 
+ * 
+ * Unless required by applicable law or agreed to in writing, 
+ * software distributed under the License is distributed on an 
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 
+ * KIND, either express or implied.  See the License for the 
+ * specific language governing permissions and limitations 
+ * under the License. 
+ */ 
+/* 
+ * Copyright (C) 2017 ScyllaDB 
+ */ 

+// The purpose of the hacks here is to workaround C++ exception scalability problem 
+// with gcc and glibc. For the best result gcc-7 is required. 
+// 
+// To summarize all the locks we have now and their purpose: 
+// 1. There is a lock in _Unwind_Find_FDE (libgcc) that protects 
+//    list of FDEs registered with __register_frame* functions. 
+//    The catch is that dynamically linked binary do not do that, 
+//    so all that it protects is checking that a certain list is empty. 
+//    This lock no longer relevant in gcc-7 since there is a path there 
+//    that checks that list is empty outside of the lock and it will be 
+//    always true for us. 
+// 2. The lock in dl_iterate_phdr (glibc) that protects loaded object 
+//    list against runtime object loading/unloading. 
+// 
+// To get rid of the first lock using gcc-7 is required. 
+// 
+// To get rid of the second one we can use the fact that we do not 
+// load/unload objects dynamically (at least for now). To do that we 
+// can mirror all elf header information in seastar and provide our 
+// own dl_iterate_phdr symbol which uses this mirror without locking. 
+// 
+// Unfortunately there is another gotcha in this approach: dl_iterate_phdr 
+// supplied by glibc never calls more then one callback simultaneously as an 
+// unintended consequences of the lock there, but unfortunately libgcc relies 
+// on that to maintain small cache of translations. The access to the cache is 
+// not protected by any lock since up until now only one callback could have 
+// run at a time. But luckily libgcc cannot use the cache if older version 
+// of dl_phdr_info is provided to the callback because the older version 
+// did not have an indication that loaded object list may have changed, 
+// so libgcc does not know when cache should be invalidated and disables it 
+// entirely. By calling the callback with old version of dl_phdr_info from 
+// our dl_iterate_phdr we can effectively make libgcc callback thread safe. 

+#ifndef NO_EXCEPTION_HACK 
+#include <link.h> 
+#include <dlfcn.h> 
+#include <assert.h> 
+#include  
+#include  
+#include "exception_hacks.hh" 

+namespace seastar { 
+using dl_iterate_fn = int (*) (int (*callback) (struct dl_phdr_info *info, size_t size, void *data), void *data); 

+static dl_iterate_fn dl_iterate_phdr_org() { 
+    static dl_iterate_fn org = [] { 
+        auto org = (dl_iterate_fn)dlsym (RTLD_NEXT, "dl_iterate_phdr"); 
+        assert(org); 
+        return org; 
+    }(); 
+    return org; 
+} 

+static std::vector<dl_phdr_info> phdrs_cache; 

+void init_phdr_cache() { 
+    // Fill out elf header cache for access without locking. 
+    // This assumes no dynamic object loading/unloading after this point 
+    dl_iterate_phdr_org()([] (struct dl_phdr_info *info, size_t size, void *data) { 
+        phdrs_cache.push_back(*info); 
+        return 0; 
+    }, nullptr); 
+} 
+} 

+extern "C" 
+[[gnu::visibility("default")]] 
+[[gnu::externally_visible]] 
+int dl_iterate_phdr(int (*callback) (struct dl_phdr_info *info, size_t size, void *data), void *data) { 
+    if (!seastar::phdrs_cache.size()) { 
+        // Cache is not yet populated, pass through to original function 
+        return seastar::dl_iterate_phdr_org()(callback, data); 
+    } 
+    int r = 0; 
+    for (auto h : seastar::phdrs_cache) { 
+        // Pass dl_phdr_info size that does not include dlpi_adds and dlpi_subs. 
+        // This forces libgcc to disable caching which is not thread safe and 
+        // requires dl_iterate_phdr to serialize calls to callback. Since we do 
+        // not serialize here we have to disable caching. 
+        r = callback(&h, offsetof(dl_phdr_info, dlpi_adds), data); 
+        if (r) { 
+            break; 
+        } 
+    } 
+    return r; 
+} 
+#endif 
diff --git a/core/exception_hacks.hh b/core/exception_hacks.hh 
new file mode 100644 
index 0000000..f5ff51f 
--- /dev/null 
+++ b/core/exception_hacks.hh 
@@ -0,0 +1,24 @@ 
+/* 
+ * This file is open source software, licensed to you under the terms 
+ * of the Apache License, Version 2.0 (the "License").  See the NOTICE file 
+ * distributed with this work for additional information regarding copyright 
+ * ownership.  You may not use this file except in compliance with the License. 
+ * 
+ * You may obtain a copy of the License at 
+ * 
+ *   http://www.apache.org/licenses/LICENSE-2.0 
+ * 
+ * Unless required by applicable law or agreed to in writing, 
+ * software distributed under the License is distributed on an 
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 
+ * KIND, either express or implied.  See the License for the 
+ * specific language governing permissions and limitations 
+ * under the License. 
+ */ 
+/* 
+ * Copyright (C) 2017 ScyllaDB 
+ */ 

+namespace seastar { 
+void init_phdr_cache(); 
+} 
diff --git a/core/reactor.cc b/core/reactor.cc 
index 2beef59..b4b4e8d 100644 
--- a/core/reactor.cc 
+++ b/core/reactor.cc 
@@ -93,6 +93,7 @@ 
 #include "util/defer.hh" 
 #include "core/metrics.hh" 
 #include "execution_stage.hh" 
+#include "exception_hacks.hh" 
  
 namespace seastar { 
  
@@ -3369,6 +3370,9 @@ smp::get_options_description() 
         ("max-io-requests", bpo::value(), "Maximum amount of concurrent requests to be sent to the disk. Defaults to 128 times the number of processors") 
 #endif 
         ("mbind", bpo::value()->default_value(true), "enable mbind") 
+#ifndef NO_EXCEPTION_HACK 
+        ("enable-glibc-exception-scaling-workaround", bpo::value()->default_value(true), "enable workaround for glibc/gcc c++ exception scalablity problem") 
+#endif 
         ; 
     return opts; 
 } 
@@ -3514,6 +3518,12 @@ static void sigabrt_action() noexcept { 
  
 void smp::configure(boost::program_options::variables_map configuration) 
 { 
+#ifndef NO_EXCEPTION_HACK 
+    if (configuration["enable-glibc-exception-scaling-workaround"].as()) { 
+        init_phdr_cache(); 
+    } 
+#endif 

     // Mask most, to prevent threads (esp. dpdk helper threads) 
     // from servicing a signal.  Individual reactors will unmask signals 
     // as they become prepared to handle them. 
-- 
                        Gleb.

点赞
收藏
评论区
推荐文章
blmius blmius
3年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
Wesley13 Wesley13
3年前
java将前端的json数组字符串转换为列表
记录下在前端通过ajax提交了一个json数组的字符串,在后端如何转换为列表。前端数据转化与请求varcontracts{id:'1',name:'yanggb合同1'},{id:'2',name:'yanggb合同2'},{id:'3',name:'yang
皕杰报表之UUID
​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为
待兔 待兔
4个月前
手写Java HashMap源码
HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22
Jacquelyn38 Jacquelyn38
3年前
2020年前端实用代码段,为你的工作保驾护航
有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )
Wesley13 Wesley13
3年前
mysql设置时区
mysql设置时区mysql\_query("SETtime\_zone'8:00'")ordie('时区设置失败,请联系管理员!');中国在东8区所以加8方法二:selectcount(user\_id)asdevice,CONVERT\_TZ(FROM\_UNIXTIME(reg\_time),'08:00','0
Wesley13 Wesley13
3年前
00:Java简单了解
浅谈Java之概述Java是SUN(StanfordUniversityNetwork),斯坦福大学网络公司)1995年推出的一门高级编程语言。Java是一种面向Internet的编程语言。随着Java技术在web方面的不断成熟,已经成为Web应用程序的首选开发语言。Java是简单易学,完全面向对象,安全可靠,与平台无关的编程语言。
Stella981 Stella981
3年前
Django中Admin中的一些参数配置
设置在列表中显示的字段,id为django模型默认的主键list_display('id','name','sex','profession','email','qq','phone','status','create_time')设置在列表可编辑字段list_editable
Wesley13 Wesley13
3年前
MySQL部分从库上面因为大量的临时表tmp_table造成慢查询
背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_
Python进阶者 Python进阶者
10个月前
Excel中这日期老是出来00:00:00,怎么用Pandas把这个去除
大家好,我是皮皮。一、前言前几天在Python白银交流群【上海新年人】问了一个Pandas数据筛选的问题。问题如下:这日期老是出来00:00:00,怎么把这个去除。二、实现过程后来【论草莓如何成为冻干莓】给了一个思路和代码如下:pd.toexcel之前把这