Upload packaging folder
[platform/upstream/iotjs.git] / tools / docs / devs / Memory-savings-with-libtuv.md
1 Memory usage with libtuv on iotjs is described here and compared to libuv.
2
3 * Compared with release version in i686.
4 * iotjs version: hash f8e8391d8c30a76c2f82644e454056c11a2bad1a
5
6 #### runtime memory usage compare with libuv running iotjs
7
8 1) how to build
9
10 1-1) with libuv
11 ```
12 ./tools/build.py --buildtype=release --nochecktest 
13 ```
14 1-2) with libtuv
15 ```
16 ./tools/build.py --buildtype=release --nochecktest --tuv
17 ```
18 2) memory usage measurement with valgrind running `test httpserver`
19 ```
20 valgrind ./build/i686-linux/release/iotjs/iotjs ./test/run_pass/test_httpserver.js
21 ```
22
23 2-1) with libuv
24 ```
25 ==5740== Memcheck, a memory error detector
26 ==5740== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
27 ==5740== Using Valgrind-3.10.0.SVN and LibVEX; rerun with -h for copyright info
28 ==5740== Command: ./build/i686-linux/release/iotjs/iotjs ./test/run_pass/test_httpserver.js
29 ==5740== 
30 ==5740== 
31 ==5740== HEAP SUMMARY:
32 ==5740==     in use at exit: 0 bytes in 0 blocks
33 ==5740==   total heap usage: 959 allocs, 959 frees, 482,669 bytes allocated
34 ==5740== 
35 ==5740== All heap blocks were freed -- no leaks are possible
36 ==5740== 
37 ==5740== For counts of detected and suppressed errors, rerun with: -v
38 ==5740== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
39 ```
40
41 2-2) with libtuv
42 ```
43 ==7584== Memcheck, a memory error detector
44 ==7584== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
45 ==7584== Using Valgrind-3.10.0.SVN and LibVEX; rerun with -h for copyright info
46 ==7584== Command: ./build/i686-linux/release/iotjs/iotjs ./test/run_pass/test_httpserver.js
47 ==7584== 
48 ==7584== 
49 ==7584== HEAP SUMMARY:
50 ==7584==     in use at exit: 0 bytes in 0 blocks
51 ==7584==   total heap usage: 955 allocs, 955 frees, 481,645 bytes allocated
52 ==7584== 
53 ==7584== All heap blocks were freed -- no leaks are possible
54 ==7584== 
55 ==7584== For counts of detected and suppressed errors, rerun with: -v
56 ==7584== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
57 ```
58
59 482,669 vs 481,645 = 1,024 bytes saved
60
61 #### binary size
62
63 1) build
64 ```
65 ./tools/build.py --buildtype=release --nochecktest --target-arch=arm --target-os=linux
66 ./tools/build.py --buildtype=release --nochecktest --target-arch=arm --target-os=linux --tuv
67 ```
68
69 2) binary size in i686-linux
70
71 * libuv : 213,130 / iotjs: 2,512,292, stripped: 782,152
72 * libtuv: 103,158 / iotjs: 2,460,357, stripped: 732,776
73 * itself: 109,972 smaller, iotjs: 51,935(striped 49,376) saved
74
75 3) binary size in arm-linux
76
77 * libuv : 176,614 / iotjs: 2,543,525, stripped: 536,460
78 * libtuv:  83,458 / iotjs: 2,506,455, stripped: 507,548
79 * itself:  93,156 smaller, iotjs:    37,070(stripped: 28,912) saved
80
81 #### libuv vs libtuv itself
82
83 1) use `tuvtester` as an application to compare.
84
85 2) codes changes to make both libuv.a and libtuv.a interchangeable. 
86
87 2-1) as libtuv uses c++ compiler, wrap all codes with
88 ```
89 extern "C" {
90 ...
91 }
92 ```
93 2-2) for libuv, give `libuv.a` by changing `tuvtest.cmake` file. use file from iotjs build.
94 ```
95 target_link_libraries(${TUVTESTNAME} LINK_PUBLIC
96                       #${TARGETLIBNAME}
97                       "/(absolute path to libuv)/libuv.a"
98                       ${TUV_LINK_LIBS})
99 ```
100
101 2-3) some functions that does not exist in libuv. add this bottom of in runner_main.cpp
102 ```
103 #if 1
104
105 #define uv__handle_deinit(h)                                                  \
106   do {                                                                        \
107     QUEUE_REMOVE(&(h)->handle_queue);                                         \
108     QUEUE_INIT(&(h)->handle_queue);                                           \
109   }                                                                           \
110   while (0)
111
112 void uv_deinit(uv_loop_t* loop, uv_handle_t* handle) {
113   QUEUE* q;
114   uv_handle_t* h;
115
116   QUEUE_FOREACH(q, &loop->handles_queue) {
117     h = QUEUE_DATA(q, uv_handle_t, handle_queue);
118     if (h == handle) {
119       uv__handle_deinit(handle);
120       break;
121     }
122   }
123 }
124
125 #endif
126 ```
127
128 2-4) remove test codes that does not run with libuv, tested codes are like this in runner_list.h
129 ```
130 #define TEST_LIST_ALL(TE)                                                     \
131   TE(idle_basic, 5000)                                                        \
132   TE(timer_init, 5000)                                                        \
133   \
134   TE(condvar_2, 5000)                                                         \
135   TE(condvar_3, 5000)                                                         \
136   TE(cwd, 5000)                                                               \
137   \
138   TE(fs_file_noent, 5000)                                                     \
139   TE(fs_file_sync, 5000)                                                      \
140   TE(fs_file_async, 5000)                                                     \
141   TE(fs_file_write_null_buffer, 5000)                                         \
142   TE(fs_stat_missing_path, 5000)                                              \
143   TE(fs_open_dir, 5000)                                                       \
144   TE(fs_file_open_append, 5000)                                               \
145   TE(fs_read_file_eof, 5000)                                                  \
146   \
147   TE(threadpool_queue_work_simple, 5000)                                      \
148
149
150 // shutdown_eof should be last of tcp test, it'll stop "echo_sevrer"
151
152 #if defined(__linux__) 
153 #define TEST_LIST_EXT(TE)                                                     \
154
155 #else
156 #define TEST_LIST_EXT(TE)                                                     \
157
158 #endif
159
160 #define HELPER_LIST_ALL(TE)                                                   \
161
162 ```
163
164 3) measure
165
166 run with valgrind
167 ```
168 valgrind ./build/i686-linux/release/bin/tuvtester
169 ```
170
171 3-1) with libuv
172 ```
173 ==24952== Memcheck, a memory error detector
174 ==24952== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
175 ==24952== Using Valgrind-3.10.0.SVN and LibVEX; rerun with -h for copyright info
176 ==24952== Command: ./build/i686-linux/release/bin/tuvtester
177 ==24952== 
178 Run Helpers...
179 [idle_basic                    ]...OK
180 [timer_init                    ]...OK
181 [condvar_2                     ]...OK
182 [condvar_3                     ]...OK
183 [cwd                           ]...OK
184 [fs_file_noent                 ]...OK
185 [fs_file_sync                  ]...OK
186 [fs_file_async                 ]...OK
187 [fs_file_write_null_buffer     ]...OK
188 [fs_stat_missing_path          ]...OK
189 [fs_open_dir                   ]...OK
190 [fs_file_open_append           ]...OK
191 [fs_read_file_eof              ]...OK
192 [threadpool_queue_work_simple  ]...OK
193 Waiting Helpers to end...
194 ==24952== 
195 ==24952== HEAP SUMMARY:
196 ==24952==     in use at exit: 0 bytes in 0 blocks
197 ==24952==   total heap usage: 44 allocs, 44 frees, 1,727 bytes allocated
198 ==24952== 
199 ==24952== All heap blocks were freed -- no leaks are possible
200 ==24952== 
201 ==24952== For counts of detected and suppressed errors, rerun with: -v
202 ==24952== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
203 ```
204
205 3-2) with libtuv
206 ```
207 ==26621== Memcheck, a memory error detector
208 ==26621== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
209 ==26621== Using Valgrind-3.10.0.SVN and LibVEX; rerun with -h for copyright info
210 ==26621== Command: ./build/i686-linux/release/bin/tuvtester
211 ==26621== 
212 Run Helpers...
213 [idle_basic                    ]...OK
214 [timer_init                    ]...OK
215 [condvar_2                     ]...OK
216 [condvar_3                     ]...OK
217 [cwd                           ]...OK
218 [fs_file_noent                 ]...OK
219 [fs_file_sync                  ]...OK
220 [fs_file_async                 ]...OK
221 [fs_file_write_null_buffer     ]...OK
222 [fs_stat_missing_path          ]...OK
223 [fs_open_dir                   ]...OK
224 [fs_file_open_append           ]...OK
225 [fs_read_file_eof              ]...OK
226 [threadpool_queue_work_simple  ]...OK
227 Waiting Helpers to end...
228 ==26621== 
229 ==26621== HEAP SUMMARY:
230 ==26621==     in use at exit: 0 bytes in 0 blocks
231 ==26621==   total heap usage: 40 allocs, 40 frees, 991 bytes allocated
232 ==26621== 
233 ==26621== All heap blocks were freed -- no leaks are possible
234 ==26621== 
235 ==26621== For counts of detected and suppressed errors, rerun with: -v
236 ```
237
238 3-3) result
239
240 * libuv: 1,727 bytes
241 * libtuv:  991 bytes