keep_data_small.txt 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. Keeping data small
  2. When many applets are compiled into busybox, all rw data and
  3. bss for each applet are concatenated. Including those from libc,
  4. if static busybox is built. When busybox is started, _all_ this data
  5. is allocated, not just that one part for selected applet.
  6. What "allocated" exactly means, depends on arch.
  7. On NOMMU it's probably bites the most, actually using real
  8. RAM for rwdata and bss. On i386, bss is lazily allocated
  9. by COWed zero pages. Not sure about rwdata - also COW?
  10. In order to keep busybox NOMMU and small-mem systems friendly
  11. we should avoid large global data in our applets, and should
  12. minimize usage of libc functions which implicitly use
  13. such structures.
  14. Small experiment to measure "parasitic" bbox memory consumption:
  15. here we start 1000 "busybox sleep 10" in parallel.
  16. busybox binary is practically allyesconfig static one,
  17. built against uclibc. Run on x86-64 machine with 64-bit kernel:
  18. bash-3.2# nmeter '%t %c %m %p %[pn]'
  19. 23:17:28 .......... 168M 0 147
  20. 23:17:29 .......... 168M 0 147
  21. 23:17:30 U......... 168M 1 147
  22. 23:17:31 SU........ 181M 244 391
  23. 23:17:32 SSSSUUU... 223M 757 1147
  24. 23:17:33 UUU....... 223M 0 1147
  25. 23:17:34 U......... 223M 1 1147
  26. 23:17:35 .......... 223M 0 1147
  27. 23:17:36 .......... 223M 0 1147
  28. 23:17:37 S......... 223M 0 1147
  29. 23:17:38 .......... 223M 1 1147
  30. 23:17:39 .......... 223M 0 1147
  31. 23:17:40 .......... 223M 0 1147
  32. 23:17:41 .......... 210M 0 906
  33. 23:17:42 .......... 168M 1 147
  34. 23:17:43 .......... 168M 0 147
  35. This requires 55M of memory. Thus 1 trivial busybox applet
  36. takes 55k of memory on 64-bit x86 kernel.
  37. On 32-bit kernel we need ~26k per applet.
  38. Script:
  39. i=1000; while test $i != 0; do
  40. echo -n .
  41. busybox sleep 30 &
  42. i=$((i - 1))
  43. done
  44. echo
  45. wait
  46. (Data from NOMMU arches are sought. Provide 'size busybox' output too)
  47. Example 1
  48. One example how to reduce global data usage is in
  49. archival/libunarchive/decompress_unzip.c:
  50. /* This is somewhat complex-looking arrangement, but it allows
  51. * to place decompressor state either in bss or in
  52. * malloc'ed space simply by changing #defines below.
  53. * Sizes on i386:
  54. * text data bss dec hex
  55. * 5256 0 108 5364 14f4 - bss
  56. * 4915 0 0 4915 1333 - malloc
  57. */
  58. #define STATE_IN_BSS 0
  59. #define STATE_IN_MALLOC 1
  60. (see the rest of the file to get the idea)
  61. This example completely eliminates globals in that module.
  62. Required memory is allocated in unpack_gz_stream() [its main module]
  63. and then passed down to all subroutines which need to access 'globals'
  64. as a parameter.
  65. Example 2
  66. In case you don't want to pass this additional parameter everywhere,
  67. take a look at archival/gzip.c. Here all global data is replaced by
  68. single global pointer (ptr_to_globals) to allocated storage.
  69. In order to not duplicate ptr_to_globals in every applet, you can
  70. reuse single common one. It is defined in libbb/messages.c
  71. as struct globals *const ptr_to_globals, but the struct globals is
  72. NOT defined in libbb.h. You first define your own struct:
  73. struct globals { int a; char buf[1000]; };
  74. and then declare that ptr_to_globals is a pointer to it:
  75. #define G (*ptr_to_globals)
  76. ptr_to_globals is declared as constant pointer.
  77. This helps gcc understand that it won't change, resulting in noticeably
  78. smaller code. In order to assign it, use SET_PTR_TO_GLOBALS macro:
  79. SET_PTR_TO_GLOBALS(xzalloc(sizeof(G)));
  80. Typically it is done in <applet>_main().
  81. Now you can reference "globals" by G.a, G.buf and so on, in any function.
  82. bb_common_bufsiz1
  83. There is one big common buffer in bss - bb_common_bufsiz1. It is a much
  84. earlier mechanism to reduce bss usage. Each applet can use it for
  85. its needs. Library functions are prohibited from using it.
  86. 'G.' trick can be done using bb_common_bufsiz1 instead of malloced buffer:
  87. #define G (*(struct globals*)&bb_common_bufsiz1)
  88. Be careful, though, and use it only if globals fit into bb_common_bufsiz1.
  89. Since bb_common_bufsiz1 is BUFSIZ + 1 bytes long and BUFSIZ can change
  90. from one libc to another, you have to add compile-time check for it:
  91. if (sizeof(struct globals) > sizeof(bb_common_bufsiz1))
  92. BUG_<applet>_globals_too_big();
  93. Drawbacks
  94. You have to initialize it by hand. xzalloc() can be helpful in clearing
  95. allocated storage to 0, but anything more must be done by hand.
  96. All global variables are prefixed by 'G.' now. If this makes code
  97. less readable, use #defines:
  98. #define dev_fd (G.dev_fd)
  99. #define sector (G.sector)
  100. Word of caution
  101. If applet doesn't use much of global data, converting it to use
  102. one of above methods is not worth the resulting code obfuscation.
  103. If you have less than ~300 bytes of global data - don't bother.
  104. gcc's data alignment problem
  105. The following attribute added in vi.c:
  106. static int tabstop;
  107. static struct termios term_orig __attribute__ ((aligned (4)));
  108. static struct termios term_vi __attribute__ ((aligned (4)));
  109. reduces bss size by 32 bytes, because gcc sometimes aligns structures to
  110. ridiculously large values. asm output diff for above example:
  111. tabstop:
  112. .zero 4
  113. .section .bss.term_orig,"aw",@nobits
  114. - .align 32
  115. + .align 4
  116. .type term_orig, @object
  117. .size term_orig, 60
  118. term_orig:
  119. .zero 60
  120. .section .bss.term_vi,"aw",@nobits
  121. - .align 32
  122. + .align 4
  123. .type term_vi, @object
  124. .size term_vi, 60
  125. gcc doesn't seem to have options for altering this behaviour.
  126. gcc 3.4.3 and 4.1.1 tested:
  127. char c = 1;
  128. // gcc aligns to 32 bytes if sizeof(struct) >= 32
  129. struct {
  130. int a,b,c,d;
  131. int i1,i2,i3;
  132. } s28 = { 1 }; // struct will be aligned to 4 bytes
  133. struct {
  134. int a,b,c,d;
  135. int i1,i2,i3,i4;
  136. } s32 = { 1 }; // struct will be aligned to 32 bytes
  137. // same for arrays
  138. char vc31[31] = { 1 }; // unaligned
  139. char vc32[32] = { 1 }; // aligned to 32 bytes
  140. -fpack-struct=1 reduces alignment of s28 to 1 (but probably
  141. will break layout of many libc structs) but s32 and vc32
  142. are still aligned to 32 bytes.
  143. I will try to cook up a patch to add a gcc option for disabling it.
  144. Meanwhile, this is where it can be disabled in gcc source:
  145. gcc/config/i386/i386.c
  146. int
  147. ix86_data_alignment (tree type, int align)
  148. {
  149. #if 0
  150. if (AGGREGATE_TYPE_P (type)
  151. && TYPE_SIZE (type)
  152. && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
  153. && (TREE_INT_CST_LOW (TYPE_SIZE (type)) >= 256
  154. || TREE_INT_CST_HIGH (TYPE_SIZE (type))) && align < 256)
  155. return 256;
  156. #endif
  157. Result (non-static busybox built against glibc):
  158. # size /usr/srcdevel/bbox/fix/busybox.t0/busybox busybox
  159. text data bss dec hex filename
  160. 634416 2736 23856 661008 a1610 busybox
  161. 632580 2672 22944 658196 a0b14 busybox_noalign
  162. Keeping code small
  163. Set CONFIG_EXTRA_CFLAGS="-fno-inline-functions-called-once",
  164. produce "make bloatcheck", see the biggest auto-inlined functions.
  165. Now, set CONFIG_EXTRA_CFLAGS back to "", but add NOINLINE
  166. to some of these functions. In 1.16.x timeframe, the results were
  167. (annotated "make bloatcheck" output):
  168. function old new delta
  169. expand_vars_to_list - 1712 +1712 win
  170. lzo1x_optimize - 1429 +1429 win
  171. arith_apply - 1326 +1326 win
  172. read_interfaces - 1163 +1163 loss, leave w/o NOINLINE
  173. logdir_open - 1148 +1148 win
  174. check_deps - 1148 +1148 loss
  175. rewrite - 1039 +1039 win
  176. run_pipe 358 1396 +1038 win
  177. write_status_file - 1029 +1029 almost the same, leave w/o NOINLINE
  178. dump_identity - 987 +987 win
  179. mainQSort3 - 921 +921 win
  180. parse_one_line - 916 +916 loss
  181. summarize - 897 +897 almost the same
  182. do_shm - 884 +884 win
  183. cpio_o - 863 +863 win
  184. subCommand - 841 +841 loss
  185. receive - 834 +834 loss
  186. 855 bytes saved in total.
  187. scripts/mkdiff_obj_bloat may be useful to automate this process: run
  188. "scripts/mkdiff_obj_bloat NORMALLY_BUILT_TREE FORCED_NOINLINE_TREE"
  189. and select modules which shrank.