getting_started.md 11.3 KB
Newer Older
1
# BUILD
2

gejun's avatar
gejun committed
3
brpc prefers static linkages of deps, so that they don't have to be installed on every machine running the app.
4

5 6
brpc depends on following packages:

7 8 9
* [gflags](https://github.com/gflags/gflags): Extensively used to define global options.
* [protobuf](https://github.com/google/protobuf): Serializations of messages, interfaces of services.
* [leveldb](https://github.com/google/leveldb): Required by [/rpcz](rpcz.md) to record RPCs for tracing.
10

zhujiashun's avatar
zhujiashun committed
11
# Supported Environment
zhujiashun's avatar
zhujiashun committed
12

zhujiashun's avatar
zhujiashun committed
13 14
* [Ubuntu/LinuxMint/WSL](#ubuntulinuxmintwsl)
* [Fedora/CentOS](#fedoracentos)
zhujiashun's avatar
zhujiashun committed
15 16 17
* [Linux with self-built deps](#linux-with-self-built-deps)
* [MacOS](#macos)

18
## Ubuntu/LinuxMint/WSL
19 20
### Prepare deps

21
Install common deps:
22
```
23
$ sudo apt-get install git g++ make libssl-dev
24 25
```

26
Install [gflags](https://github.com/gflags/gflags), [protobuf](https://github.com/google/protobuf), [leveldb](https://github.com/google/leveldb):
27
```
28
$ sudo apt-get install libgflags-dev libprotobuf-dev libprotoc-dev protobuf-compiler libleveldb-dev
29 30
```

31
If you need to statically link leveldb:
32
```
33
$ sudo apt-get install libsnappy-dev
34
```
35

36
### Compile brpc with config_brpc.sh
37
git clone brpc, cd into the repo and run
38
```
39
$ sh config_brpc.sh --headers=/usr/include --libs=/usr/lib
40
$ make
41
```
42 43 44
To change compiler to clang, add `--cxx=clang++ --cc=clang`.

To not link debugging symbols, add `--nodebugsymbols` and compiled binaries will be much smaller.
45

46
**Run example**
47

48
```
49 50 51 52
$ cd example/echo_c++
$ make
$ ./echo_server &
$ ./echo_client
53
```
54

55
Examples link brpc statically, if you need to link the shared version, `make clean` and `LINK_SO=1 make`
56

gejun's avatar
gejun committed
57
To run examples with cpu/heap profilers, install `libgoogle-perftools-dev` and re-run `config_brpc.sh` before compiling.
58

59
**Run tests**
60

61
Install and compile libgtest-dev (which is not compiled yet):
62

gejun's avatar
gejun committed
63
```shell
64
sudo apt-get install libgtest-dev && cd /usr/src/gtest && sudo cmake . && sudo make && sudo mv libgtest* /usr/lib/ && cd -
gejun's avatar
gejun committed
65 66 67 68 69
```

The directory of gtest source code may be changed, try `/usr/src/googletest/googletest` if `/usr/src/gtest` is not there.

Rerun `config_brpc.sh`, `make` in test/, and `sh run_tests.sh`
70

71 72 73 74 75 76
### Compile brpc with cmake
```
$ mkdir build && cd build && cmake .. && make
```
To change compiler to clang, overwrite environment variable CC and CXX to clang and clang++.

77
To not link debugging symbols, use `cmake -DWITH_DEBUG_SYMBOLS=OFF ..` and compiled binaries will be much smaller.
78 79 80 81 82 83 84 85 86 87 88

**Run example with cmake**
```
$ cd example/echo_c++
$ mkdir build && cd build && cmake .. && make
$ ./echo_server &
$ ./echo_client
```
Examples link brpc statically, if you need to link the shared version, use `cmake -DEXAMPLE_LINK_SO=ON ..`

**Run tests**
89 90 91 92 93 94 95 96

Install gtest like just written above.

```
$ mkdir build && cd build && cmake -DBUILD_UNIT_TESTS=ON .. && make
$ cd test && sh run_tests.sh
```

97 98 99 100
## Fedora/CentOS

### Prepare deps

101 102 103 104 105
CentOS needs to install EPEL generally otherwise many packages are not available by default.
```
sudo yum install epel-release
```

106 107
Install common deps:
```
108
sudo yum install git gcc-c++ make openssl-devel
109
```
110

111 112 113 114
Install [gflags](https://github.com/gflags/gflags), [protobuf](https://github.com/google/protobuf), [leveldb](https://github.com/google/leveldb):
```
sudo yum install gflags-devel protobuf-devel protobuf-compiler leveldb-devel
```
115
### Compile brpc with config_brpc.sh
116

117
git clone brpc, cd into the repo and run
118 119 120 121 122

```
$ sh config_brpc.sh --headers=/usr/include --libs=/usr/lib64
$ make
```
123 124 125
To change compiler to clang, add `--cxx=clang++ --cc=clang`.

To not link debugging symbols, add `--nodebugsymbols` and compiled binaries will be much smaller.
126

127
**Run example**
128 129 130 131 132 133 134

```
$ cd example/echo_c++
$ make
$ ./echo_server &
$ ./echo_client
```
135

136 137
Examples link brpc statically, if you need to link the shared version, `make clean` and `LINK_SO=1 make`

gejun's avatar
gejun committed
138 139
To run examples with cpu/heap profilers, install `gperftools-devel` and re-run `config_brpc.sh` before compiling.

140 141 142 143 144 145 146 147 148 149 150 151
**Run tests**

Install gtest-devel.

Rerun `config_brpc.sh`, `make` in test/, and `sh run_tests.sh`

### Compile brpc with cmake
```
$ mkdir build && cd build && cmake .. && make
```
To change compiler to clang, overwrite environment variable CC and CXX to clang and clang++.

152
To not link debugging symbols, use `cmake -DWITH_DEBUG_SYMBOLS=OFF ..` and compiled binaries will be much smaller.
153 154 155

**Run example**

156 157 158 159 160 161 162 163
```
$ cd example/echo_c++
$ mkdir build && cd build && cmake .. && make
$ ./echo_server &
$ ./echo_client
```
Examples link brpc statically, if you need to link the shared version, use `cmake -DEXAMPLE_LINK_SO=ON ..`

164
**Run tests**
165 166 167 168 169 170

```
$ mkdir build && cd build && cmake -DBUILD_UNIT_TESTS=ON .. && make
$ cd test && sh run_tests.sh
```

171 172 173 174 175 176
## Linux with self-built deps

### Prepare deps

brpc builds itself to both static and shared libs by default, so it needs static and shared libs of deps to be built as well.

177 178 179 180 181
Take [gflags](https://github.com/gflags/gflags) as example, which does not build shared lib by default, you need to pass options to `cmake` to change the behavior:
```
cmake . -DBUILD_SHARED_LIBS=1 -DBUILD_STATIC_LIBS=1
make
```
182 183 184

### Compile brpc

185
Keep on with the gflags example, let `../gflags_dev` be where gflags is cloned.
186 187 188 189 190 191 192 193

git clone brpc. cd into the repo and run

```
$ sh config_brpc.sh --headers="../gflags_dev /usr/include" --libs="../gflags_dev /usr/lib64"
$ make
```

194 195 196
To change compiler to clang, add `--cxx=clang++ --cc=clang`.

To not link debugging symbols, add `--nodebugsymbols` and compiled binaries will be much smaller.
197 198 199 200 201 202 203 204 205 206

Here we pass multiple paths to `--headers` and `--libs` to make the script search for multiple places. You can also group all deps and brpc into one directory, then pass the directory to --headers/--libs which actually search all subdirectories recursively and will find necessary files.

```
$ ls my_dev
gflags_dev protobuf_dev leveldb_dev brpc_dev
$ cd brpc_dev
$ sh config_brpc.sh --headers=.. --libs=..
$ make
```
207

208 209 210 211 212 213 214 215 216 217
### Compile brpc with cmake

git clone brpc. cd into the repo and run

```
$ mkdir build && cd build && cmake -DCMAKE_INCLUDE_PATH="/path/to/dep1/include;/path/to/dep2/include" -DCMAKE_LIBRARY_PATH="/path/to/dep1/lib;/path/to/dep2/lib" .. && make
```

To change compiler to clang, overwrite environment variable CC and CXX to clang and clang++.

218
To not link debugging symbols, use `cmake -DWITH_DEBUG_SYMBOLS=OFF ..` and compiled binaries will be much smaller.
219

zhujiashun's avatar
zhujiashun committed
220
## MacOS
221 222 223

Note: In the same running environment, the performance of the current Mac version is about 2.5 times worse than the Linux version. If your service is performance-critical, do not use MacOS as your production environment.

zhujiashun's avatar
zhujiashun committed
224 225 226 227
### Prepare deps

Install common deps:
```
smart's avatar
smart committed
228
$ brew install openssl git gnu-getopt coreutils
zhujiashun's avatar
zhujiashun committed
229 230 231 232 233 234 235 236 237 238
```

Install [gflags](https://github.com/gflags/gflags), [protobuf](https://github.com/google/protobuf), [leveldb](https://github.com/google/leveldb):
```
$ brew install gflags protobuf leveldb
```

### Compile brpc with config_brpc.sh
git clone brpc, cd into the repo and run
```
zhujiashun's avatar
zhujiashun committed
239
$ sh config_brpc.sh --headers=/usr/local/include --libs=/usr/local/lib --cc=clang --cxx=clang++
zhujiashun's avatar
zhujiashun committed
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
$ make
```
To not link debugging symbols, add `--nodebugsymbols` and compiled binaries will be much smaller.

**Run example**

```
$ cd example/echo_c++
$ make
$ ./echo_server &
$ ./echo_client
```

Examples link brpc statically, if you need to link the shared version, `make clean` and `LINK_SO=1 make`

To run examples with cpu/heap profilers, install `gperftools` and re-run `config_brpc.sh` before compiling.

**Run tests**

Install and compile googletest (which is not compiled yet):

```shell
git clone https://github.com/google/googletest && cd googletest/googletest && mkdir build && cd build && cmake .. && make && sudo mv libgtest* /usr/lib/ && cd -
```

Rerun `config_brpc.sh`, `make` in test/, and `sh run_tests.sh`

### Compile brpc with cmake
```
$ mkdir build && cd build && cmake .. && make
```

To not link debugging symbols, use `cmake -DWITH_DEBUG_SYMBOLS=OFF ..` and compiled binaries will be much smaller.

**Run example with cmake**
```
$ cd example/echo_c++
$ mkdir build && cd build && cmake .. && make
$ ./echo_server &
$ ./echo_client
```
Examples link brpc statically, if you need to link the shared version, use `cmake -DEXAMPLE_LINK_SO=ON ..`

**Run tests**

Install gtest like just written above.

```
$ mkdir build && cd build && cmake -DBUILD_UNIT_TESTS=ON .. && make
$ cd test && sh run_tests.sh
```

292
# Supported deps
293

294
## GCC: 4.8-7.1
295

gejun's avatar
gejun committed
296
c++11 is turned on by default to remove dependencies on boost (atomic).
297

298
The over-aligned issues in GCC7 is suppressed temporarily now.
299

300
Using other versions of gcc may generate warnings, contact us to fix.
301

gejun's avatar
gejun committed
302
Adding `-D__const__=` to cxxflags in your makefiles is a must to avoid [errno issue in gcc4+](thread_local.md).
303

gejun's avatar
gejun committed
304
## Clang: 3.5-4.0
305

gejun's avatar
gejun committed
306
no known issues.
307

gejun's avatar
gejun committed
308
## glibc: 2.12-2.25
309

310
no known issues.
311

312
## protobuf: 2.4-3.4
313

314
Be compatible with pb 3.x and pb 2.x with the same file:
315
Don't use new types in proto3 and start the proto file with `syntax="proto2";`
gejun's avatar
gejun committed
316
[tools/add_syntax_equal_proto2_to_all.sh](https://github.com/brpc/brpc/blob/master/tools/add_syntax_equal_proto2_to_all.sh)can add `syntax="proto2"` to all proto files without it.
gejun's avatar
gejun committed
317 318

Arena in pb 3.x is not supported yet.
319

320
## gflags: 2.0-2.21
321

322
no known issues.
323

gejun's avatar
gejun committed
324
## openssl: 0.97-1.1
325

326
required by https.
327

gejun's avatar
gejun committed
328
## tcmalloc: 1.7-2.5
329

gejun's avatar
gejun committed
330
brpc does **not** link [tcmalloc](http://goog-perftools.sourceforge.net/doc/tcmalloc.html) by default. Users link tcmalloc on-demand.
331

gejun's avatar
gejun committed
332
Comparing to ptmalloc embedded in glibc, tcmalloc often improves performance. However different versions of tcmalloc may behave really differently. For example, tcmalloc 2.1 may make multi-threaded examples in brpc perform significantly worse(due to a spinlock in tcmalloc) than the one using tcmalloc 1.7 and 2.5. Even different minor versions may differ. When you program behave unexpectedly, remove tcmalloc or try another version.
333

gejun's avatar
gejun committed
334
Code compiled with gcc 4.8.2 and linked to a tcmalloc compiled with earlier GCC may crash or deadlock before main(), E.g:
335

gejun's avatar
gejun committed
336
![img](../images/tcmalloc_stuck.png)
337

gejun's avatar
gejun committed
338
When you meet the issue, compile tcmalloc with the same GCC.
339

340
Another common issue with tcmalloc is that it does not return memory to system as early as ptmalloc. So when there's an invalid memory access, the program may not crash directly, instead it crashes at a unrelated place, or even not crash. When you program has weird memory issues, try removing tcmalloc.
341

gejun's avatar
gejun committed
342
If you want to use [cpu profiler](cpu_profiler.md) or [heap profiler](heap_profiler.md), do link `libtcmalloc_and_profiler.a`. These two profilers are based on tcmalloc.[contention profiler](contention_profiler.md) does not require tcmalloc.
343

gejun's avatar
gejun committed
344
When you remove tcmalloc, not only remove the linkage with tcmalloc but also the macro `-DBRPC_ENABLE_CPU_PROFILER`.
345

346 347
## glog: 3.3+

wangxuefeng's avatar
wangxuefeng committed
348
brpc implements a default [logging utility](../../src/butil/logging.h) which conflicts with glog. To replace this with glog, add *--with-glog* to config_brpc.sh or add `-DWITH_GLOG=ON` to cmake.
349

350
## valgrind: 3.8+
351

352
brpc detects valgrind automatically (and registers stacks of bthread). Older valgrind(say 3.2) is not supported.
353

gejun's avatar
gejun committed
354
# Track instances
355

gejun's avatar
gejun committed
356
We provide a program to help you to track and monitor all brpc instances. Just run [trackme_server](https://github.com/brpc/brpc/tree/master/tools/trackme_server/) somewhere and launch need-to-be-tracked instances with -trackme_server=SERVER. The trackme_server will receive pings from instances periodically and print logs when it does. You can aggregate instance addresses from the log and call builtin services of the instances for further information.