getting_started.md 9.18 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

11
## Ubuntu/LinuxMint/WSL
12 13
### Prepare deps

14
Install common deps:
15
```
16
$ sudo apt-get install git g++ make libssl-dev
17 18
```

19
Install [gflags](https://github.com/gflags/gflags), [protobuf](https://github.com/google/protobuf), [leveldb](https://github.com/google/leveldb):
20
```
21
$ sudo apt-get install realpath libgflags-dev libprotobuf-dev libprotoc-dev protobuf-compiler libleveldb-dev
22 23
```

24
If you need to statically link leveldb:
25
```
26
$ sudo apt-get install libsnappy-dev
27
```
28

29
### Compile brpc with config_brpc.sh
30
git clone brpc, cd into the repo and run
31
```
32
$ sh config_brpc.sh --headers=/usr/include --libs=/usr/lib
33
$ make
34
```
35 36 37
To change compiler to clang, add `--cxx=clang++ --cc=clang`.

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

39
**Run example**
40

41
```
42 43 44 45
$ cd example/echo_c++
$ make
$ ./echo_server &
$ ./echo_client
46
```
47

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

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

52
**Run tests**
53

54
Install and compile libgtest-dev (which is not compiled yet):
55

gejun's avatar
gejun committed
56
```shell
57
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
58 59 60 61 62
```

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`
63

64 65 66 67 68 69
### 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++.

70
To not link debugging symbols, use `cmake -DWITH_DEBUG_SYMBOLS=OFF ..` and compiled binaries will be much smaller.
71 72 73 74 75 76 77 78 79 80 81

**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**
82 83 84 85 86 87 88 89

Install gtest like just written above.

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

90 91 92 93
## Fedora/CentOS

### Prepare deps

94 95 96 97 98
CentOS needs to install EPEL generally otherwise many packages are not available by default.
```
sudo yum install epel-release
```

99 100
Install common deps:
```
101
sudo yum install git gcc-c++ make openssl-devel
102
```
103

104 105 106 107
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
```
108
### Compile brpc with config_brpc.sh
109

110
git clone brpc, cd into the repo and run
111 112 113 114 115

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

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

120
**Run example**
121 122 123 124 125 126 127

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

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

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

133 134 135 136 137 138 139 140 141 142 143 144
**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++.

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

**Run example**

149 150 151 152 153 154 155 156
```
$ 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 ..`

157
**Run tests**
158 159 160 161 162 163

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

164 165 166 167 168 169
## 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.

170 171 172 173 174
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
```
175 176 177

### Compile brpc

178
Keep on with the gflags example, let `../gflags_dev` be where gflags is cloned.
179 180 181 182 183 184 185 186

git clone brpc. cd into the repo and run

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

187 188 189
To change compiler to clang, add `--cxx=clang++ --cc=clang`.

To not link debugging symbols, add `--nodebugsymbols` and compiled binaries will be much smaller.
190 191 192 193 194 195 196 197 198 199

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
```
200

201 202 203 204 205 206 207 208 209 210
### 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++.

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

213
# Supported deps
214

215
## GCC: 4.8-7.1
216

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

219
The over-aligned issues in GCC7 is suppressed temporarily now.
220

221
Using other versions of gcc may generate warnings, contact us to fix.
222

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

gejun's avatar
gejun committed
225
## Clang: 3.5-4.0
226

gejun's avatar
gejun committed
227
no known issues.
228

gejun's avatar
gejun committed
229
## glibc: 2.12-2.25
230

231
no known issues.
232

233
## protobuf: 2.4-3.4
234

235
Be compatible with pb 3.x and pb 2.x with the same file:
236
Don't use new types in proto3 and start the proto file with `syntax="proto2";`
gejun's avatar
gejun committed
237
[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
238 239

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

241
## gflags: 2.0-2.21
242

243
no known issues.
244

gejun's avatar
gejun committed
245
## openssl: 0.97-1.1
246

247
required by https.
248

gejun's avatar
gejun committed
249
## tcmalloc: 1.7-2.5
250

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

gejun's avatar
gejun committed
253
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.
254

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

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

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

261
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.
262

gejun's avatar
gejun committed
263
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.
264

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

267 268
## glog: 3.3+

269
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 `-DBRPC_WITH_GLOG=ON` to cmake.
270

271
## valgrind: 3.8+
272

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

gejun's avatar
gejun committed
275
# Track instances
276

gejun's avatar
gejun committed
277
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.