多线程下内存分配方式的比较(TCMalloc vs mt_alloc)

当软件性能优化到一定程度之后,用vturn查看hotspots,将会发现malloc/delete会花费很高比例的时间,此时如果是多线程程序,频繁的lock将会是一个瓶颈,这里有一篇oracle的文章,很好的介绍了这样的情况http://www.oracle.com/technetwork/articles/servers-storage-dev/mem-alloc-1557798.html

在linux平台,此时我们主要考虑到两种优化方案,各自都有很细节的文章可以参考
1. google的MTMalloc (http://goog-perftools.sourceforge.net/doc/tcmalloc.html)

2. gnu的__mt_alloc (http://gcc.gnu.org/onlinedocs/libstdc++/manual/bk01pt12ch32.html)


下面我做了一个简单的测试程序
// test app
void* vectormemorycost(void*){
    cout << "malloc test"<< endl;
    int blocksize = 512;
    for(int j=0;j<20; ++j){
        int sum=25000;
        char* p1[sum];
        for(int i=0; i<sum;++i){
            p1[i]=(char*)malloc(blocksize);
        }

        for(int i=0; i<sum;++i){
            free(p1[i]);
        }
    }

    return 0;
}

void* mtallocateMemorycost(void*){
    cout << "mt alloc test"<< endl;
    typedef pod value_type;
    typedef __gnu_cxx::__mt_alloc<value_type> allocator_type;
    typedef __gnu_cxx::__pool_base::_Tune tune_type;

///_M_align(__align)
///_M_max_bytes(__maxb)
///_M_min_bin(__minbin),
///_M_chunk_size(__chunk)
///_M_max_threads(__maxthreads),
///_M_freelist_headroom(__headroom)
///_M_force_new(__force)

    tune_type t_our(16, 510, 32, 5120, 20, 10, false);
    allocator_type a;);
    a._M_set_options(t_our);
    int blocksize = 512;
for(int j=0;j<20; ++j){
    int sum=25000;
    allocator_type::pointer p1[sum];
    for(int i=0; i<sum;++i){
    p1[i]=a.allocate(blocksize);
    }

    for(int i=0; i<sum;++i){
    a.deallocate(p1[i], blocksize);
    }
}
    return 0;
}




typedef void*(*pFoo)(void*);

void mtallocatePrivate(pFoo func){
    int i;
    int cores = 4;
    pthread_t threads[cores];
    /* Create threads to do the work. */
    for (i = 0; i < cores; ++i)
        // pthread_create (&(threads[i]), NULL, vectormemorycost, NULL);
        pthread_create (&(threads[i]), NULL, func, NULL);

    /* Wait for all threads to finish. */
    for (i = 0; i < cores; ++i)
        pthread_join (threads[i], NULL);
}

void mtallocate(){
    for(int i=0;i<5;++i){
        mtallocatePrivate(mtallocateMemorycost);
        mtallocatePrivate(vectormemorycost);
    }
}




改变的tune_type参数,发现gnu的最优的情况比malloc快一两倍,最糟糕的情况是反而比malloc慢十倍,很依赖具体的实现情况,另外tcmalloc使用很简单,我们只要改一下link的库,测试的结果是比不用tcmalloc有七八倍的性能提高,从结构上来讲gnu的在更高层次的代码上优化,想通过memory pool和thread id的比较来减少申请内存的次数和不同线程之间的竞争,它最终调用的还是malloc尤其是当申请的内存大于_M_max_bytes的时候就完全是调用malloc了,而TCMalloc是替换掉了系统的malloc,是更加底层的优化。

总结一下,gnu的使用麻烦,接口繁琐,还可能有负面效应,google的比gnu的速度快,使用也方便很多。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1:首先下载libunwind-1.1 # wget http://down.yunwei8.com/soft/linux/libunwind-1.1.tar.gz 然后安装 # tar zxvf libunwind-1.1.tar.gz # cd libunwind-1.1 # CFLAGS=-fPIC ./configure # make CFLAGS=-fPIC # make CFLAGS=-fPIC install 2:安装google-perftools: 首先下载gperftools-2.0 # wget http://down.yunwei8.com/soft/linux/gperftools-2.0.tar.gz 然后安装 # tar zxvf gperftools-2.0.tar.gz # cd gperftools-2.0/ # ./configure # make && make install # echo "/usr/local/lib" > /etc/ld.so.conf.d/usr_local_lib.conf # /sbin/ldconfig 3:重新编译nginx 不会安装的看这里 http://www.yunwei8.com/nginx124/ # cd /soft/nginx-1.2.4 # ./configure --user=www --group=www --prefix=/usr/local/server/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_sub_module --with-google_perftools_module # make && make install 安装参数说明看这里:http://www.yunwei8.com/nginxcs 4:为tcmalloc添加目录,并且赋予适当权限 # mkdir -p /tmp/tcmalloc/ # chown -R www:www /tmp/tcmalloc/ 5:修改 nginx.conf ,令nginx可以 google-perftools实现加速 # vi /usr/local/server/nginx/conf/nginx.conf 修改前面几行为: user www www; worker_processes 8; error_log /web/logs/nginx_error.log crit; pid logs/nginx.pid; google_perftools_profiles /tmp/tcmalloc/; events{ use epoll; worker_connections 65535; } 6:测试和运行 # /usr/local/server/nginx/sbin/nginx -t 如果显示下面信息,即表示配置没问题 nginx: the configuration file /opt/nginx/conf/nginx.conf syntax is ok nginx: configuration file /opt/nginx/conf/nginx.conf test is successful 输入代码运行nginx服务 # /usr/local/server/nginx/sbin/nginx # ps aux|grep [n]ginx 如果显以类似下面的信息,即表示nginx已经启动 root 22900 0.0 0.1 43216 1576 ? Ss 08:23 0:00 nginx: master process /usr/local/server/nginx/sbin/nginx www 23019 0.0 2.6 68816 27160 ? S 08:48 0:00 nginx: worker process www 23020 0.0 2.6 68816 26828 ? S 08:48 0:00 nginx: worker process www 23021 0.0 2.6 68816 26828 ? S 08:48 0:00 nginx: worker process www 23022 0.0 2.6 68816 26828 ? S 08:48 0:00 nginx: worker process www 23023 0.0 2.6 68816 26828 ? S 08:48 0:00 nginx: worker process www 23024 0.0 2.6 68816 26828 ? S 08:48 0:00 nginx: worker process www 23025 0.0 2.6 68816 26828 ? S 08:48 0:00 nginx: worker process www 23026 0.0 2.6 68816 26828 ? S 08:48 0:00 nginx: worker process 输入代码检测是否支持加速 # lsof -n | grep tcmalloc 如果显示类似下面的信息,即表示支持tcmalloc加速 (nginx八个线程都支持) nginx 31655 www 9w REG 8,1 0 479533 /tmp/tcmalloc/.31655 nginx 31656 www 11w REG 8,1 0 479534 /tmp/tcmalloc/.31656 nginx 31657 www 13w REG 8,1 0 479535 /tmp/tcmalloc/.31657 nginx 31658 www 15w REG 8,1 0 479536 /tmp/tcmalloc/.31658 nginx 31659 www 17w REG 8,1 0 479537 /tmp/tcmalloc/.31659 nginx 31660 www 19w REG 8,1 0 479540 /tmp/tcmalloc/.31660 nginx 31661 www 21w REG 8,1 0 479538 /tmp/tcmalloc/.31661 nginx 31662 www 23w REG 8,1 0 479539 /tmp/tcmalloc/.31662 优化完成

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值