diff options
author | Denys Vlasenko <vda.linux@googlemail.com> | 2018-03-11 19:35:58 +0100 |
---|---|---|
committer | Denys Vlasenko <vda.linux@googlemail.com> | 2018-03-11 19:35:58 +0100 |
commit | 0e09ded2f1ab4b2e941cb29bed6c5dca7ecb9b53 (patch) | |
tree | 25a0a5f518e9543fc46b316fff65a225250d1054 /util-linux/readprofile.c | |
parent | 448fdcc17df39ac90c59d0588ccaf9ee0400eb7e (diff) | |
download | busybox-w32-0e09ded2f1ab4b2e941cb29bed6c5dca7ecb9b53.tar.gz busybox-w32-0e09ded2f1ab4b2e941cb29bed6c5dca7ecb9b53.tar.bz2 busybox-w32-0e09ded2f1ab4b2e941cb29bed6c5dca7ecb9b53.zip |
readprofile: code shrink
function old new delta
defaultpro 14 - -14
defaultmap 17 - -17
readprofile_main 1762 1719 -43
------------------------------------------------------------------------------
(add/remove: 0/2 grow/shrink: 0/1 up/down: 0/-74) Total: -74 bytes
text data bss dec hex filename
933081 473 6836 940390 e5966 busybox_old
933035 473 6836 940344 e5938 busybox_unstripped
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
Diffstat (limited to 'util-linux/readprofile.c')
-rw-r--r-- | util-linux/readprofile.c | 67 |
1 files changed, 33 insertions, 34 deletions
diff --git a/util-linux/readprofile.c b/util-linux/readprofile.c index 8fc33be3c..219c0c18c 100644 --- a/util-linux/readprofile.c +++ b/util-linux/readprofile.c | |||
@@ -61,27 +61,22 @@ | |||
61 | 61 | ||
62 | #define S_LEN 128 | 62 | #define S_LEN 128 |
63 | 63 | ||
64 | /* These are the defaults */ | ||
65 | static const char defaultmap[] ALIGN1 = "/boot/System.map"; | ||
66 | static const char defaultpro[] ALIGN1 = "/proc/profile"; | ||
67 | |||
68 | int readprofile_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; | 64 | int readprofile_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
69 | int readprofile_main(int argc UNUSED_PARAM, char **argv) | 65 | int readprofile_main(int argc UNUSED_PARAM, char **argv) |
70 | { | 66 | { |
71 | FILE *map; | 67 | FILE *map; |
72 | const char *mapFile, *proFile; | 68 | const char *mapFile, *proFile; |
73 | unsigned long indx = 1; | 69 | unsigned long indx; |
74 | size_t len; | 70 | size_t len; |
75 | uint64_t add0 = 0; | 71 | uint64_t add0; |
76 | unsigned int step; | 72 | unsigned int step; |
77 | unsigned int *buf, total, fn_len; | 73 | unsigned int *buf, total, fn_len; |
78 | unsigned long long fn_add, next_add; /* current and next address */ | 74 | unsigned long long fn_add, next_add; /* current and next address */ |
79 | char fn_name[S_LEN], next_name[S_LEN]; /* current and next name */ | 75 | char fn_name[S_LEN], next_name[S_LEN]; /* current and next name */ |
80 | char mapline[S_LEN]; | 76 | char mapline[S_LEN]; |
81 | char mode[8]; | 77 | char mode[8]; |
82 | int maplineno = 1; | 78 | int maplineno; |
83 | int header_printed; | 79 | int multiplier; |
84 | int multiplier = 0; | ||
85 | unsigned opt; | 80 | unsigned opt; |
86 | enum { | 81 | enum { |
87 | OPT_M = (1 << 0), | 82 | OPT_M = (1 << 0), |
@@ -106,8 +101,9 @@ int readprofile_main(int argc UNUSED_PARAM, char **argv) | |||
106 | 101 | ||
107 | #define next (current^1) | 102 | #define next (current^1) |
108 | 103 | ||
109 | proFile = defaultpro; | 104 | proFile = "/proc/profile"; |
110 | mapFile = defaultmap; | 105 | mapFile = "/boot/System.map"; |
106 | multiplier = 0; | ||
111 | 107 | ||
112 | opt = getopt32(argv, "M:+m:p:nabsirv", &multiplier, &mapFile, &proFile); | 108 | opt = getopt32(argv, "M:+m:p:nabsirv", &multiplier, &mapFile, &proFile); |
113 | 109 | ||
@@ -122,7 +118,7 @@ int readprofile_main(int argc UNUSED_PARAM, char **argv) | |||
122 | if (!optMult) | 118 | if (!optMult) |
123 | to_write = 1; /* sth different from sizeof(int) */ | 119 | to_write = 1; /* sth different from sizeof(int) */ |
124 | 120 | ||
125 | fd = xopen(defaultpro, O_WRONLY); | 121 | fd = xopen("/proc/profile", O_WRONLY); |
126 | xwrite(fd, &multiplier, to_write); | 122 | xwrite(fd, &multiplier, to_write); |
127 | close(fd); | 123 | close(fd); |
128 | return EXIT_SUCCESS; | 124 | return EXIT_SUCCESS; |
@@ -133,12 +129,13 @@ int readprofile_main(int argc UNUSED_PARAM, char **argv) | |||
133 | */ | 129 | */ |
134 | len = MAXINT(ssize_t); | 130 | len = MAXINT(ssize_t); |
135 | buf = xmalloc_xopen_read_close(proFile, &len); | 131 | buf = xmalloc_xopen_read_close(proFile, &len); |
132 | len /= sizeof(*buf); | ||
133 | |||
136 | if (!optNative) { | 134 | if (!optNative) { |
137 | int entries = len / sizeof(*buf); | 135 | int big = 0, small = 0; |
138 | int big = 0, small = 0, i; | ||
139 | unsigned *p; | 136 | unsigned *p; |
140 | 137 | ||
141 | for (p = buf+1; p < buf+entries; p++) { | 138 | for (p = buf+1; p < buf+len; p++) { |
142 | if (*p & ~0U << (sizeof(*buf)*4)) | 139 | if (*p & ~0U << (sizeof(*buf)*4)) |
143 | big++; | 140 | big++; |
144 | if (*p & ((1 << (sizeof(*buf)*4))-1)) | 141 | if (*p & ((1 << (sizeof(*buf)*4))-1)) |
@@ -147,15 +144,15 @@ int readprofile_main(int argc UNUSED_PARAM, char **argv) | |||
147 | if (big > small) { | 144 | if (big > small) { |
148 | bb_error_msg("assuming reversed byte order, " | 145 | bb_error_msg("assuming reversed byte order, " |
149 | "use -n to force native byte order"); | 146 | "use -n to force native byte order"); |
150 | for (p = buf; p < buf+entries; p++) | 147 | BUILD_BUG_ON(sizeof(*p) > 8); |
151 | for (i = 0; i < sizeof(*buf)/2; i++) { | 148 | for (p = buf; p < buf+len; p++) { |
152 | unsigned char *b = (unsigned char *) p; | 149 | if (sizeof(*p) == 2) |
153 | unsigned char tmp; | 150 | *p = bswap_16(*p); |
154 | 151 | if (sizeof(*p) == 4) | |
155 | tmp = b[i]; | 152 | *p = bswap_32(*p); |
156 | b[i] = b[sizeof(*buf)-i-1]; | 153 | if (sizeof(*p) == 8) |
157 | b[sizeof(*buf)-i-1] = tmp; | 154 | *p = bb_bswap_64(*p); |
158 | } | 155 | } |
159 | } | 156 | } |
160 | } | 157 | } |
161 | 158 | ||
@@ -165,10 +162,9 @@ int readprofile_main(int argc UNUSED_PARAM, char **argv) | |||
165 | return EXIT_SUCCESS; | 162 | return EXIT_SUCCESS; |
166 | } | 163 | } |
167 | 164 | ||
168 | total = 0; | ||
169 | |||
170 | map = xfopen_for_read(mapFile); | 165 | map = xfopen_for_read(mapFile); |
171 | 166 | add0 = 0; | |
167 | maplineno = 1; | ||
172 | while (fgets(mapline, S_LEN, map)) { | 168 | while (fgets(mapline, S_LEN, map)) { |
173 | if (sscanf(mapline, "%llx %s %s", &fn_add, mode, fn_name) != 3) | 169 | if (sscanf(mapline, "%llx %s %s", &fn_add, mode, fn_name) != 3) |
174 | bb_error_msg_and_die("%s(%i): wrong map line", | 170 | bb_error_msg_and_die("%s(%i): wrong map line", |
@@ -187,8 +183,11 @@ int readprofile_main(int argc UNUSED_PARAM, char **argv) | |||
187 | /* | 183 | /* |
188 | * Main loop. | 184 | * Main loop. |
189 | */ | 185 | */ |
186 | total = 0; | ||
187 | indx = 1; | ||
190 | while (fgets(mapline, S_LEN, map)) { | 188 | while (fgets(mapline, S_LEN, map)) { |
191 | unsigned int this = 0; | 189 | bool header_printed; |
190 | unsigned int this; | ||
192 | 191 | ||
193 | if (sscanf(mapline, "%llx %s %s", &next_add, mode, next_name) != 3) | 192 | if (sscanf(mapline, "%llx %s %s", &next_add, mode, next_name) != 3) |
194 | bb_error_msg_and_die("%s(%i): wrong map line", | 193 | bb_error_msg_and_die("%s(%i): wrong map line", |
@@ -198,17 +197,17 @@ int readprofile_main(int argc UNUSED_PARAM, char **argv) | |||
198 | 197 | ||
199 | /* ignore any LEADING (before a '[tT]' symbol is found) | 198 | /* ignore any LEADING (before a '[tT]' symbol is found) |
200 | Absolute symbols */ | 199 | Absolute symbols */ |
201 | if ((*mode == 'A' || *mode == '?') && total == 0) continue; | 200 | if ((mode[0] == 'A' || mode[0] == '?') && total == 0) |
202 | if (*mode != 'T' && *mode != 't' | 201 | continue; |
203 | && *mode != 'W' && *mode != 'w' | 202 | if ((mode[0]|0x20) != 't' && (mode[0]|0x20) != 'w') { |
204 | ) { | ||
205 | break; /* only text is profiled */ | 203 | break; /* only text is profiled */ |
206 | } | 204 | } |
207 | 205 | ||
208 | if (indx >= len / sizeof(*buf)) | 206 | if (indx >= len) |
209 | bb_error_msg_and_die("profile address out of range. " | 207 | bb_error_msg_and_die("profile address out of range. " |
210 | "Wrong map file?"); | 208 | "Wrong map file?"); |
211 | 209 | ||
210 | this = 0; | ||
212 | while (indx < (next_add-add0)/step) { | 211 | while (indx < (next_add-add0)/step) { |
213 | if (optBins && (buf[indx] || optAll)) { | 212 | if (optBins && (buf[indx] || optAll)) { |
214 | if (!header_printed) { | 213 | if (!header_printed) { |
@@ -256,7 +255,7 @@ int readprofile_main(int argc UNUSED_PARAM, char **argv) | |||
256 | } | 255 | } |
257 | 256 | ||
258 | /* clock ticks, out of kernel text - probably modules */ | 257 | /* clock ticks, out of kernel text - probably modules */ |
259 | printf("%6u %s\n", buf[len/sizeof(*buf)-1], "*unknown*"); | 258 | printf("%6u *unknown*\n", buf[len-1]); |
260 | 259 | ||
261 | /* trailer */ | 260 | /* trailer */ |
262 | if (optVerbose) | 261 | if (optVerbose) |