[PATCH v1] Size reduction, cleanup, fixes, improvements for swapon/swapoff

Tito farmatito at tiscali.it
Sun Mar 30 19:55:51 UTC 2014


Hi to all,
this patch started as a personal fun and learning project on swapon/swapoff after
Matt Whitlock's patches. I was curios if there was a way to remove all the #ifdefs
from within the code as for my personal taste this unindented stuff in indented
code makes the code harder to read.
So I tried hard by using the preprocessor and compilers dead code elimination
and in the end I was able to achieve my goal: a ifdef free code.
During the process as side effects I spotted a few differences between
our swapon/swapoff inplementation and the one on my debian box
so I added this fixes/improvements to the code:

1)  real swapon/swapoff handles also devices on the commandline with -a;
2)  xstat(device)  in  swap_enable_disable aborts on error when cycling through
     fstab so some devices  are not handled;
3) duplicated code for ENABLE_FEATURE_SWAPON_DISCARD and
    ENABLE_FEATURE_SWAPON_PRI was moved to functions.
4) silence some error messages with -a;
5) minor cleanups and code refactoring reduced the size as per bloat-check:

./scripts/bloat-o-meter busybox_unstripped.old busybox_unstripped
function                                             old     new   delta
set_discard_flag                                       -     106    +106
set_priority_flag                                      -      75     +75
swap_enable_disable                                  149     219     +70
.rodata                                           141270  141260     -10
swap_on_off_main                                     638     325    -313
------------------------------------------------------------------------------
(add/remove: 2/0 grow/shrink: 1/2 up/down: 251/-323)          Total: -72 bytes

6) I also added support for /proc/swaps handling to swapoff: 

"When the -a flag is given, swapping is disabled on all known  swap  devices
 and  files  (as  found  in  /proc/swaps  or /etc/fstab)."

So now swapoff first cycles through  /proc/swaps and then through fstab
to swapoff all devices. 
With this additional feature bloat-check is:

./scripts/bloat-o-meter busybox_unstripped.old busybox_unstripped
function                                             old     new   delta
set_discard_flag                                       -     106    +106
set_priority_flag                                      -      75     +75
swap_enable_disable                                  149     219     +70
.rodata                                           141270  141272      +2
swap_on_off_main                                     638     416    -222
------------------------------------------------------------------------------
(add/remove: 2/0 grow/shrink: 2/1 up/down: 253/-222)           Total: 31 bytes

so it comes at almost no extra size cost.
I've decided not to add an extra config option for that, but it could
be  easily added if needed.

7) Something I'm forgettin...;

Sadly I cannot provide a patch series for the different changes but
only a allinone patch, therefore I also attach a drop-in replacement
file for swaponoff.c for review and testing by the list members.
I've tested it a little on my box with a few test cases I could think of,
but more testing is needed as there is a lot of code changed and bugs
can creep in.
Hint, critics and improvements are welcome.

Ciao,
Tito

-------------- next part --------------
A non-text attachment was scrubbed...
Name: swaponoff.patch
Type: text/x-patch
Size: 9480 bytes
Desc: not available
URL: <http://lists.busybox.net/pipermail/busybox/attachments/20140330/522bf110/attachment.bin>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: swaponoff.c
Type: text/x-csrc
Size: 7051 bytes
Desc: not available
URL: <http://lists.busybox.net/pipermail/busybox/attachments/20140330/522bf110/attachment.c>


More information about the busybox mailing list