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
rpm -Uvh http://www.elrepo.org/elrepo-release-7.0-2.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

在 CentOS 7 下安装配置 shadowsocks

安装 pip
pip是 python 的包管理工具。在本文中将使用 python 版本的 shadowsocks,此版本的 shadowsocks 已发布到 pip 上,因此我们需要通过 pip 命令来安装。

在控制台执行以下命令安装 pip:
$ curl "https://bootstrap.pypa.io/get-pip.py" -o "get-pip.py"
$ python get-pip.py

安装配置 shadowsocks
在控制台执行以下命令安装 shadowsocks:
$ pip install --upgrade pip
$ pip install shadowsocks

安装完成后,需要创建配置文件/etc/shadowsocks.json,内容如下:
{
  "server": "0.0.0.0",
  "server_port": 8388,
  "password": "asdfghjkl",
  "method": "aes-256-cfb"
}
说明:
method为加密方法,可选aes-128-cfb, aes-192-cfb, aes-256-cfb, bf-cfb, cast5-cfb, des-cfb, rc4-md5, chacha20, salsa20, rc4, table
server_port为服务监听端口
password为密码,可使用密码生成工具生成一个随机密码
以上三项信息在配置 shadowsocks 客户端时需要配置一致,具体说明可查看 shadowsocks 的帮助文档。

设置防火墙开放8388端口

配置自启动
新建启动脚本文件/etc/systemd/system/shadowsocks.service,内容如下:
[Unit]
Description=Shadowsocks

[Service]
TimeoutStartSec=0
ExecStart=/usr/bin/ssserver -c /etc/shadowsocks.json

[Install]
WantedBy=multi-user.target

执行以下命令启动 shadowsocks 服务:
$ systemctl enable shadowsocks
$ systemctl start shadowsocks

为了检查 shadowsocks 服务是否已成功启动,可以执行以下命令查看服务的状态:
$ systemctl status shadowsocks -l

如果服务启动成功,则控制台显示的信息可能类似这样:
● shadowsocks.service - Shadowsocks
   Loaded: loaded (/etc/systemd/system/shadowsocks.service; enabled; vendor preset: disabled)
   Active: active (running) since Mon 2015-12-21 23:51:48 CST; 11min ago
 Main PID: 19334 (ssserver)
   CGroup: /system.slice/shadowsocks.service
           └─19334 /usr/bin/python /usr/bin/ssserver -c /etc/shadowsocks.json
Dec 21 23:51:48 morning.work systemd[1]: Started Shadowsocks.
Dec 21 23:51:48 morning.work systemd[1]: Starting Shadowsocks...
Dec 21 23:51:48 morning.work ssserver[19334]: INFO: loading config from /etc/shadowsocks.json
Dec 21 23:51:48 morning.work ssserver[19334]: 2015-12-21 23:51:48 INFO     loading libcrypto from libcrypto.so.10
Dec 21 23:51:48 morning.work ssserver[19334]: 2015-12-21 23:51:48 INFO     starting server at 0.0.0.0:8388


注意:Android 最新版本的ShadowSocks客户端可能会出现连接之后无数据的情况,建议选择低一些的稳定版本

可选项:

安装 shadowsocksr
wget https://github.com/shadowsocksrr/shadowsocksr/archive/3.2.2.tar.gz
cd shadowsocksr-3.2.2/
bash initcfg.sh

vi userapiconfig.py
API_INTERFACE = 'mudbjson'

使用脚本mujson_mgr.py添加端口及相应的加密、协议、混淆等配置
查看该脚本的说明及提示:
python mujson_mgr.py
查看用户列表:
python mujson_mgr.py -l
添加用户:
python mujson_mgr.py -a -u username -p 8388 -k password -m aes-256-cfb -O auth_aes128_md5 -o http_simple
注意在客户端要选择对应的参数:
加密方法 aes-256-cfb
协议:auth_aes128_md5
混淆方式:http_simple
否则可能会导致无法正常访问网络

后台运行(无log,ssh窗口关闭后也继续运行)
./run.sh

后台运行(输出log,ssh窗口关闭后也继续运行)
./logrun.sh

后台运行时查看运行情况
./tail.sh

停止运行
./stop.sh

查看log:
tail -f shadowsocks.log

注意时区,时间要同步