diff options
| author | Denys Vlasenko <vda.linux@googlemail.com> | 2020-12-14 18:49:23 +0100 |
|---|---|---|
| committer | Denys Vlasenko <vda.linux@googlemail.com> | 2020-12-14 18:54:30 +0100 |
| commit | c7b858ff8d2e8b2d785f74b2d319bc9c839f4faa (patch) | |
| tree | 9ad109d5999444dfb3a281678b6b63d2570a76cd | |
| parent | fd3c512f88d43e6633bd3c3110cfa0bb321adaa8 (diff) | |
| download | busybox-w32-c7b858ff8d2e8b2d785f74b2d319bc9c839f4faa.tar.gz busybox-w32-c7b858ff8d2e8b2d785f74b2d319bc9c839f4faa.tar.bz2 busybox-w32-c7b858ff8d2e8b2d785f74b2d319bc9c839f4faa.zip | |
libbb: add and use infrastructure for fixed page size optimization
function old new delta
procps_scan 1121 1118 -3
getpagesize 6 - -6
rpm_main 1037 1027 -10
rpm2cpio_main 120 110 -10
ptok 38 21 -17
time_main 1282 1261 -21
mkswap_main 317 278 -39
------------------------------------------------------------------------------
(add/remove: 0/2 grow/shrink: 0/6 up/down: 0/-106) Total: -106 bytes
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
| -rw-r--r-- | archival/rpm.c | 10 | ||||
| -rw-r--r-- | include/libbb.h | 18 | ||||
| -rw-r--r-- | libbb/procps.c | 4 | ||||
| -rw-r--r-- | miscutils/devmem.c | 2 | ||||
| -rw-r--r-- | miscutils/hexedit.c | 14 | ||||
| -rw-r--r-- | miscutils/time.c | 7 | ||||
| -rw-r--r-- | util-linux/mkswap.c | 2 |
7 files changed, 37 insertions, 20 deletions
diff --git a/archival/rpm.c b/archival/rpm.c index a4d850b46..af8db99a6 100644 --- a/archival/rpm.c +++ b/archival/rpm.c | |||
| @@ -83,7 +83,9 @@ struct globals { | |||
| 83 | void *map; | 83 | void *map; |
| 84 | rpm_index *mytags; | 84 | rpm_index *mytags; |
| 85 | int tagcount; | 85 | int tagcount; |
| 86 | unsigned mapsize, pagesize; | 86 | unsigned mapsize; |
| 87 | IF_VARIABLE_ARCH_PAGESIZE(unsigned pagesize;) | ||
| 88 | #define G_pagesize cached_pagesize(G.pagesize) | ||
| 87 | } FIX_ALIASING; | 89 | } FIX_ALIASING; |
| 88 | #define G (*(struct globals*)bb_common_bufsiz1) | 90 | #define G (*(struct globals*)bb_common_bufsiz1) |
| 89 | #define INIT_G() do { setup_common_bufsiz(); } while (0) | 91 | #define INIT_G() do { setup_common_bufsiz(); } while (0) |
| @@ -141,7 +143,7 @@ static int rpm_gettags(const char *filename) | |||
| 141 | G.mytags = tags; | 143 | G.mytags = tags; |
| 142 | 144 | ||
| 143 | /* Map the store */ | 145 | /* Map the store */ |
| 144 | storepos = (storepos + G.pagesize) & -(int)G.pagesize; | 146 | storepos = (storepos + G_pagesize) & -(int)G_pagesize; |
| 145 | /* remember size for munmap */ | 147 | /* remember size for munmap */ |
| 146 | G.mapsize = storepos; | 148 | G.mapsize = storepos; |
| 147 | /* some NOMMU systems prefer MAP_PRIVATE over MAP_SHARED */ | 149 | /* some NOMMU systems prefer MAP_PRIVATE over MAP_SHARED */ |
| @@ -356,7 +358,7 @@ int rpm_main(int argc, char **argv) | |||
| 356 | int opt, func = 0; | 358 | int opt, func = 0; |
| 357 | 359 | ||
| 358 | INIT_G(); | 360 | INIT_G(); |
| 359 | G.pagesize = getpagesize(); | 361 | INIT_PAGESIZE(G.pagesize); |
| 360 | 362 | ||
| 361 | while ((opt = getopt(argc, argv, "iqpldc")) != -1) { | 363 | while ((opt = getopt(argc, argv, "iqpldc")) != -1) { |
| 362 | switch (opt) { | 364 | switch (opt) { |
| @@ -523,7 +525,7 @@ int rpm2cpio_main(int argc UNUSED_PARAM, char **argv) | |||
| 523 | int rpm_fd; | 525 | int rpm_fd; |
| 524 | 526 | ||
| 525 | INIT_G(); | 527 | INIT_G(); |
| 526 | G.pagesize = getpagesize(); | 528 | INIT_PAGESIZE(G.pagesize); |
| 527 | 529 | ||
| 528 | rpm_fd = rpm_gettags(argv[1]); | 530 | rpm_fd = rpm_gettags(argv[1]); |
| 529 | 531 | ||
diff --git a/include/libbb.h b/include/libbb.h index a74b3119f..db5bf7a51 100644 --- a/include/libbb.h +++ b/include/libbb.h | |||
| @@ -391,6 +391,24 @@ void *mmap_read(int fd, size_t size) FAST_FUNC; | |||
| 391 | void *mmap_anon(size_t size) FAST_FUNC; | 391 | void *mmap_anon(size_t size) FAST_FUNC; |
| 392 | void *xmmap_anon(size_t size) FAST_FUNC; | 392 | void *xmmap_anon(size_t size) FAST_FUNC; |
| 393 | 393 | ||
| 394 | #if defined(__x86_64__) || defined(i386) | ||
| 395 | # define BB_ARCH_FIXED_PAGESIZE 4096 | ||
| 396 | #else /* if defined(ARCH) */ | ||
| 397 | /* add you favorite arch today! */ | ||
| 398 | #endif | ||
| 399 | |||
| 400 | #if defined BB_ARCH_FIXED_PAGESIZE | ||
| 401 | # define IF_VARIABLE_ARCH_PAGESIZE(...) /*nothing*/ | ||
| 402 | # define bb_getpagesize() BB_ARCH_FIXED_PAGESIZE | ||
| 403 | # define INIT_PAGESIZE(var) ((void)0) | ||
| 404 | # define cached_pagesize(var) BB_ARCH_FIXED_PAGESIZE | ||
| 405 | #else | ||
| 406 | # define IF_VARIABLE_ARCH_PAGESIZE(...) __VA_ARGS__ | ||
| 407 | # define bb_getpagesize() getpagesize() | ||
| 408 | # define INIT_PAGESIZE(var) ((var) = getpagesize()) | ||
| 409 | # define cached_pagesize(var) (var) | ||
| 410 | #endif | ||
| 411 | |||
| 394 | 412 | ||
| 395 | //TODO: supply a pointer to char[11] buffer (avoid statics)? | 413 | //TODO: supply a pointer to char[11] buffer (avoid statics)? |
| 396 | extern const char *bb_mode_string(mode_t mode) FAST_FUNC; | 414 | extern const char *bb_mode_string(mode_t mode) FAST_FUNC; |
diff --git a/libbb/procps.c b/libbb/procps.c index 4cc3cb2a1..975e0d4dc 100644 --- a/libbb/procps.c +++ b/libbb/procps.c | |||
| @@ -94,15 +94,15 @@ static int read_to_buf(const char *filename, void *buf) | |||
| 94 | 94 | ||
| 95 | static procps_status_t* FAST_FUNC alloc_procps_scan(void) | 95 | static procps_status_t* FAST_FUNC alloc_procps_scan(void) |
| 96 | { | 96 | { |
| 97 | unsigned n = getpagesize(); | ||
| 98 | procps_status_t* sp = xzalloc(sizeof(procps_status_t)); | 97 | procps_status_t* sp = xzalloc(sizeof(procps_status_t)); |
| 99 | sp->dir = xopendir("/proc"); | 98 | unsigned n = bb_getpagesize(); |
| 100 | while (1) { | 99 | while (1) { |
| 101 | n >>= 1; | 100 | n >>= 1; |
| 102 | if (!n) break; | 101 | if (!n) break; |
| 103 | sp->shift_pages_to_bytes++; | 102 | sp->shift_pages_to_bytes++; |
| 104 | } | 103 | } |
| 105 | sp->shift_pages_to_kb = sp->shift_pages_to_bytes - 10; | 104 | sp->shift_pages_to_kb = sp->shift_pages_to_bytes - 10; |
| 105 | sp->dir = xopendir("/proc"); | ||
| 106 | return sp; | 106 | return sp; |
| 107 | } | 107 | } |
| 108 | 108 | ||
diff --git a/miscutils/devmem.c b/miscutils/devmem.c index e8dce5225..f9f0276bc 100644 --- a/miscutils/devmem.c +++ b/miscutils/devmem.c | |||
| @@ -75,7 +75,7 @@ int devmem_main(int argc UNUSED_PARAM, char **argv) | |||
| 75 | bb_show_usage(); /* one of bb_strtouXX failed */ | 75 | bb_show_usage(); /* one of bb_strtouXX failed */ |
| 76 | 76 | ||
| 77 | fd = xopen("/dev/mem", argv[3] ? (O_RDWR | O_SYNC) : (O_RDONLY | O_SYNC)); | 77 | fd = xopen("/dev/mem", argv[3] ? (O_RDWR | O_SYNC) : (O_RDONLY | O_SYNC)); |
| 78 | mapped_size = page_size = getpagesize(); | 78 | mapped_size = page_size = bb_getpagesize(); |
| 79 | offset_in_page = (unsigned)target & (page_size - 1); | 79 | offset_in_page = (unsigned)target & (page_size - 1); |
| 80 | if (offset_in_page + width > page_size) { | 80 | if (offset_in_page + width > page_size) { |
| 81 | /* This access spans pages. | 81 | /* This access spans pages. |
diff --git a/miscutils/hexedit.c b/miscutils/hexedit.c index 898d77376..f8ff9b62b 100644 --- a/miscutils/hexedit.c +++ b/miscutils/hexedit.c | |||
| @@ -31,7 +31,8 @@ struct globals { | |||
| 31 | int fd; | 31 | int fd; |
| 32 | unsigned height; | 32 | unsigned height; |
| 33 | unsigned row; | 33 | unsigned row; |
| 34 | unsigned pagesize; | 34 | IF_VARIABLE_ARCH_PAGESIZE(unsigned pagesize;) |
| 35 | #define G_pagesize cached_pagesize(G.pagesize) | ||
| 35 | uint8_t *baseaddr; | 36 | uint8_t *baseaddr; |
| 36 | uint8_t *current_byte; | 37 | uint8_t *current_byte; |
| 37 | uint8_t *eof_byte; | 38 | uint8_t *eof_byte; |
| @@ -46,15 +47,6 @@ struct globals { | |||
| 46 | SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \ | 47 | SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \ |
| 47 | } while (0) | 48 | } while (0) |
| 48 | 49 | ||
| 49 | //TODO: move to libbb | ||
| 50 | #if defined(__x86_64__) || defined(i386) | ||
| 51 | # define G_pagesize 4096 | ||
| 52 | # define INIT_PAGESIZE() ((void)0) | ||
| 53 | #else | ||
| 54 | # define G_pagesize (G.pagesize) | ||
| 55 | # define INIT_PAGESIZE() ((void)(G.pagesize = getpagesize())) | ||
| 56 | #endif | ||
| 57 | |||
| 58 | /* hopefully there aren't arches with PAGE_SIZE > 64k */ | 50 | /* hopefully there aren't arches with PAGE_SIZE > 64k */ |
| 59 | #define G_mapsize (64*1024) | 51 | #define G_mapsize (64*1024) |
| 60 | 52 | ||
| @@ -262,7 +254,7 @@ int hexedit_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; | |||
| 262 | int hexedit_main(int argc UNUSED_PARAM, char **argv) | 254 | int hexedit_main(int argc UNUSED_PARAM, char **argv) |
| 263 | { | 255 | { |
| 264 | INIT_G(); | 256 | INIT_G(); |
| 265 | INIT_PAGESIZE(); | 257 | INIT_PAGESIZE(G.pagesize); |
| 266 | 258 | ||
| 267 | get_terminal_width_height(-1, NULL, &G.height); | 259 | get_terminal_width_height(-1, NULL, &G.height); |
| 268 | if (1) { | 260 | if (1) { |
diff --git a/miscutils/time.c b/miscutils/time.c index d15d363f3..0006c59d8 100644 --- a/miscutils/time.c +++ b/miscutils/time.c | |||
| @@ -111,6 +111,10 @@ static void printargv(char *const *argv) | |||
| 111 | This is funky since the pagesize could be less than 1K. | 111 | This is funky since the pagesize could be less than 1K. |
| 112 | Note: Some machines express getrusage statistics in terms of K, | 112 | Note: Some machines express getrusage statistics in terms of K, |
| 113 | others in terms of pages. */ | 113 | others in terms of pages. */ |
| 114 | #ifdef BB_ARCH_FIXED_PAGESIZE | ||
| 115 | # define pagesize BB_ARCH_FIXED_PAGESIZE | ||
| 116 | # define ptok(pagesize, pages) ptok(pages) | ||
| 117 | #endif | ||
| 114 | static unsigned long ptok(const unsigned pagesize, const unsigned long pages) | 118 | static unsigned long ptok(const unsigned pagesize, const unsigned long pages) |
| 115 | { | 119 | { |
| 116 | unsigned long tmp; | 120 | unsigned long tmp; |
| @@ -124,6 +128,7 @@ static unsigned long ptok(const unsigned pagesize, const unsigned long pages) | |||
| 124 | tmp = pages * pagesize; /* Larger first, */ | 128 | tmp = pages * pagesize; /* Larger first, */ |
| 125 | return tmp / 1024; /* then smaller. */ | 129 | return tmp / 1024; /* then smaller. */ |
| 126 | } | 130 | } |
| 131 | #undef pagesize | ||
| 127 | 132 | ||
| 128 | /* summarize: Report on the system use of a command. | 133 | /* summarize: Report on the system use of a command. |
| 129 | 134 | ||
| @@ -177,7 +182,7 @@ static void summarize(const char *fmt, char **command, resource_t *resp) | |||
| 177 | { | 182 | { |
| 178 | unsigned vv_ms; /* Elapsed virtual (CPU) milliseconds */ | 183 | unsigned vv_ms; /* Elapsed virtual (CPU) milliseconds */ |
| 179 | unsigned cpu_ticks; /* Same, in "CPU ticks" */ | 184 | unsigned cpu_ticks; /* Same, in "CPU ticks" */ |
| 180 | unsigned pagesize = getpagesize(); | 185 | unsigned pagesize = bb_getpagesize(); |
| 181 | 186 | ||
| 182 | /* Impossible: we do not use WUNTRACED flag in wait()... | 187 | /* Impossible: we do not use WUNTRACED flag in wait()... |
| 183 | if (WIFSTOPPED(resp->waitstatus)) | 188 | if (WIFSTOPPED(resp->waitstatus)) |
diff --git a/util-linux/mkswap.c b/util-linux/mkswap.c index 9e51a1dcc..8fe5d0293 100644 --- a/util-linux/mkswap.c +++ b/util-linux/mkswap.c | |||
| @@ -128,7 +128,7 @@ int mkswap_main(int argc UNUSED_PARAM, char **argv) | |||
| 128 | 128 | ||
| 129 | /* Figure out how big the device is */ | 129 | /* Figure out how big the device is */ |
| 130 | len = get_volume_size_in_bytes(fd, argv[1], 1024, /*extend:*/ 1); | 130 | len = get_volume_size_in_bytes(fd, argv[1], 1024, /*extend:*/ 1); |
| 131 | pagesize = getpagesize(); | 131 | pagesize = bb_getpagesize(); |
| 132 | len -= pagesize; | 132 | len -= pagesize; |
| 133 | 133 | ||
| 134 | /* Announce our intentions */ | 134 | /* Announce our intentions */ |
