Windows 下使用 tcmalloc

https://github.com/gperftools/gperftools
使用VS编译项目,将 libtcmalloc_minimal.lib 放到项目的lib目录下

添加库目录:
$(ProjectDir)lib

附加依赖项:
libtcmalloc_minimal.lib

强制符号引用:
__tcmalloc

测试代码

#include <Windows.h>
#include <iostream>
//#include <tbb/tbbmalloc_proxy.h> //这是tbb包含的头文件, win32系统默认malloc是只要注释该头文件引用
using namespace std;
// 执行多少轮
#define MAX_SCALE (4000)
//每轮生成 2 4 8 16     32 64 126 256   512  1024 2048 4096   。。。。共16种规格的内存
#define MAX_SIZE  16

DWORD WINAPI worker(LPVOID lpThreadParameter){
	int iterations = MAX_SCALE;
	void* ps[MAX_SCALE][MAX_SIZE];

	while(iterations-- ) {
		for (int i = 0; i < MAX_SIZE; i ++){
			int size = (int)pow(2.0, i);
			void * p = malloc(size);
			memset(p,0,size);
			ps[iterations][i] = p;
		}
	}

	iterations = MAX_SCALE;
	while(iterations-- ) {
		for (int i = 0; i < MAX_SIZE; i ++){
			free(ps[iterations][i]);
		}
	}
	return NULL;
}

void main(){
	int scale = 4;
	cout << "start " << endl;
	HANDLE* handles = new HANDLE[scale];
	cout << "HANDLE newed" << endl;
	DWORD start = ::GetTickCount();
	for (int i = 0; i < scale; i ++){
		DWORD threadID;
		handles[i] = CreateThread(0, 0x1000000000, worker, NULL, NULL, &threadID);
	}


	WaitForMultipleObjects(scale, handles, true, INFINITE);
	DWORD end = ::GetTickCount();
	cout << end-start << endl;
}

测试结果(毫秒)
tcmalloc 4 thread
1155
905
1045
952

msvcrt 4 thread
1997
2714
2777
2496

tbb 4 thread
905
889
920
858

tcmalloc 1 thread
796
780
843
858

msvcrt 1 thread
686
686
702
686

tbb 1 thread
734
718
686
702

tbb 4t 8000loop (crash!!!)
1732
1825
1794
1841

ms 4t 8000loop
16660
23462
21855
11013

tc 4t 8000loop
2200
2262
2200
2059

CentOS 7 安装 TCP BBR

升级内核:
rpm --import https://www.elrepo.org/RPM-GPG-KEY-elrepo.org
yum install https://www.elrepo.org/elrepo-release-7.0-4.el7.elrepo.noarch.rpm
yum --enablerepo=elrepo-kernel install kernel-ml

查看内核:
awk -F\' '$1=="menuentry " {print i++ " : " $2}' /etc/grub2.cfg
0 : Red Hat Enterprise Linux Server (4.10.0-1.el7.elrepo.x86_64) 7.3 (Maipo)
1 : Red Hat Enterprise Linux Server 7.3 Rescue a294b6df936641c2a363bf9f7d90646a (3.10.0-514.6.1.el7.x86_64)
2 : Red Hat Enterprise Linux Server (3.10.0-514.6.1.el7.x86_64) 7.3 (Maipo)
3 : Red Hat Enterprise Linux Server (3.10.0-514.el7.x86_64) 7.3 (Maipo)
4 : Red Hat Enterprise Linux Server (0-rescue-be7a44b4b98e404a8cdcd062c4733c10) 7.3 (Maipo)

修改启动项:
grub2-set-default 0
reboot


开启 TCP BBR
修改系统变量:
echo "net.core.default_qdisc=fq" >> /etc/sysctl.conf
echo "net.ipv4.tcp_congestion_control=bbr" >> /etc/sysctl.conf
保存生效
sysctl -p
执行
sysctl net.ipv4.tcp_available_congestion_control
如果结果是这样
sysctl net.ipv4.tcp_available_congestion_control
net.ipv4.tcp_available_congestion_control = bbr cubic reno
就开启了。

执行  lsmod | grep bbr,以检测 BBR 是否开启。
如果结果类似tcp_bbr  16384  15 则成功

默认启用此 yum 源:
vi /etc/yum.repos.d/elrepo.repo
修改:
[elrepo-kernel]
...
enabled=1

Amazon AWS 安装 epel

注意:不要在AWS上安装 iptables-service,否则会导致无法恢复的网络异常

允许ping
安全组,入站
添加规则:自定义 ICMP 规则 - IPv4,任何位置

chmod 400 aws.pem
通过其 公有 DNS 连接,默认账户名ec2-user
ssh -i "aws.pem" ec2-user@ec2-52-221-186-41.ap-southeast-1.compute.amazonaws.com

sudo passwd root

su root

vi /etc/ssh/sshd_config
PasswordAuthentication yes
PermitRootLogin no

sudo systemctl restart sshd

passwd ec2-user

vi /etc/sysconfig/selinux
SELINUX=disabled

添加新用户:
adduser username
passwd username
Login as new username:
su root
userdel -r ec2-user



yum-config-manager --enable rhui-REGION-rhel-server-extras rhui-REGION-rhel-server-optional

yum install wget
wget http://dl.fedoraproject.org/pub/epel/7/x86_64/Packages/e/epel-release-7-11.noarch.rpm
rpm -vih epel-release-7-11.noarch.rpm

vi /etc/yum.repos.d/nginx.repo
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/rhel/7/$basearch/
gpgcheck=0
enabled=1

vi /etc/yum.repos.d/mongodb-org-3.4.repo
[mongodb-org-3.4]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/3.4/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-3.4.asc