wok view busybox/stuff/busybox-1.12.0-replay.u @ rev 3214

busybox: add replay, speedup unlzma
author Pascal Bellard <pascal.bellard@slitaz.org>
date Thu May 28 13:57:09 2009 +0200 (2009-05-28)
parents
children 9743505c66ac
line source
1 --- busybox-1.12.0/util-linux/script.c
2 +++ busybox-1.12.0/util-linux/script.c
3 @@ -36,6 +36,15 @@
4 const char *shell;
5 char shell_opt[] = "-i";
6 char *shell_arg = NULL;
7 + enum {
8 + OPT_a = (1 << 0),
9 + OPT_c = (1 << 1),
10 + OPT_f = (1 << 2),
11 + OPT_q = (1 << 3),
12 +#if ENABLE_REPLAY
13 + OPT_t = (1 << 4),
14 +#endif
15 + };
17 #if ENABLE_GETOPT_LONG
18 static const char getopt_longopts[] ALIGN1 =
19 @@ -43,25 +52,28 @@
20 "command\0" Required_argument "c"
21 "flush\0" No_argument "f"
22 "quiet\0" No_argument "q"
23 +# if ENABLE_REPLAY
24 + "timing\0" No_argument "t"
25 +# endif
26 ;
28 applet_long_options = getopt_longopts;
29 #endif
30 opt_complementary = "?1"; /* max one arg */
31 - opt = getopt32(argv, "ac:fq", &shell_arg);
32 + opt = getopt32(argv, "ac:fq" USE_REPLAY("t") , &shell_arg);
33 //argc -= optind;
34 argv += optind;
35 if (argv[0]) {
36 fname = argv[0];
37 }
38 mode = O_CREAT|O_TRUNC|O_WRONLY;
39 - if (opt & 1) {
40 + if (opt & OPT_a) {
41 mode = O_CREAT|O_APPEND|O_WRONLY;
42 }
43 - if (opt & 2) {
44 + if (opt & OPT_c) {
45 shell_opt[1] = 'c';
46 }
47 - if (!(opt & 8)) { /* not -q */
48 + if (!(opt & OPT_q)) {
49 printf("Script started, file is %s\n", fname);
50 }
51 shell = getenv("SHELL");
52 @@ -97,6 +109,10 @@
53 #define buf bb_common_bufsiz1
54 struct pollfd pfd[2];
55 int outfd, count, loop;
56 +#if ENABLE_REPLAY
57 + struct timeval tv;
58 + double oldtime=time(NULL), newtime;
59 +#endif
61 outfd = xopen(fname, mode);
62 pfd[0].fd = pty;
63 @@ -118,15 +134,27 @@
64 }
65 if (pfd[0].revents) {
66 errno = 0;
67 +#if ENABLE_REPLAY
68 + if (opt & OPT_t) {
69 + gettimeofday(&tv, NULL);
70 + }
71 +#endif
72 count = safe_read(pty, buf, sizeof(buf));
73 if (count <= 0 && errno != EAGAIN) {
74 /* err/eof from pty: exit */
75 goto restore;
76 }
77 if (count > 0) {
78 +#if ENABLE_REPLAY
79 + if (opt & OPT_t) {
80 + newtime = tv.tv_sec + (double) tv.tv_usec / 1000000;
81 + fprintf(stderr, "%f %i\n", newtime - oldtime, count);
82 + oldtime = newtime;
83 + }
84 +#endif
85 full_write(STDOUT_FILENO, buf, count);
86 full_write(outfd, buf, count);
87 - if (opt & 4) { /* -f */
88 + if (opt & OPT_f) {
89 fsync(outfd);
90 }
91 }
92 @@ -158,7 +186,7 @@
93 restore:
94 if (attr_ok == 0)
95 tcsetattr(0, TCSAFLUSH, &tt);
96 - if (!(opt & 8)) /* not -q */
97 + if (!(opt & OPT_q))
98 printf("Script done, file is %s\n", fname);
99 return EXIT_SUCCESS;
100 }
102 --- busybox-1.12.0/util-linux/Config.in
103 +++ busybox-1.12.0/util-linux/Config.in
104 @@ -719,6 +719,13 @@
105 help
106 This allows you to parse /proc/profile for basic profiling.
108 +config REPLAY
109 + bool "replay"
110 + default n
111 + help
112 + This program replays a typescript, using timing information
113 + given by script -t.
114 +
115 config RTCWAKE
116 bool "rtcwake"
117 default n
119 --- busybox-1.12.0/util-linux/Kbuild
120 +++ busybox-1.12.0/util-linux/Kbuild
121 @@ -28,6 +28,7 @@
122 lib-$(CONFIG_RDATE) += rdate.o
123 lib-$(CONFIG_RDEV) += rdev.o
124 lib-$(CONFIG_READPROFILE) += readprofile.o
125 +lib-$(CONFIG_REPLAY) += replay.o
126 lib-$(CONFIG_RTCWAKE) += rtcwake.o
127 lib-$(CONFIG_SCRIPT) += script.o
128 lib-$(CONFIG_SETARCH) += setarch.o
130 --- busybox-1.12.0/include/applets.h
131 USE_RM(APPLET_NOFORK(rm, rm, _BB_DIR_BIN, _BB_SUID_NEVER, rm))
132 +++ busybox-1.12.0/include/applets.h
133 @@ -294,6 +294,7 @@
134 USE_REALPATH(APPLET(realpath, _BB_DIR_USR_BIN, _BB_SUID_NEVER))
135 USE_HALT(APPLET_ODDNAME(reboot, halt, _BB_DIR_SBIN, _BB_SUID_NEVER, reboot))
136 USE_RENICE(APPLET(renice, _BB_DIR_USR_BIN, _BB_SUID_NEVER))
137 +USE_REPLAY(APPLET(replay, _BB_DIR_BIN, _BB_SUID_NEVER))
138 USE_RESET(APPLET(reset, _BB_DIR_USR_BIN, _BB_SUID_NEVER))
139 USE_RESIZE(APPLET(resize, _BB_DIR_USR_BIN, _BB_SUID_NEVER))
140 USE_RESTORECON(APPLET_ODDNAME(restorecon, setfiles, _BB_DIR_SBIN, _BB_SUID_NEVER, restorecon))
142 --- busybox-1.12.0/include/usage.h
143 +++ busybox-1.12.0/include/usage.h
144 @@ -3244,6 +3244,11 @@
145 "\n -g Process group id(s)" \
146 "\n -u Process user name(s) and/or id(s)" \
148 +#define replay_trivial_usage \
149 + "timingfile [typescript [divisor]]"
150 +#define replay_full_usage "\n\n" \
151 + "Play back typescripts, using timing information"
152 +
153 #define reset_trivial_usage \
154 ""
155 #define reset_full_usage "\n\n" \
156 @@ -3426,13 +3431,20 @@
158 #define script_trivial_usage \
159 "[-afq] [-c COMMAND] [OUTFILE]"
160 -#define script_full_usage "\n\n" \
161 +#define script_full_usage_base "\n\n" \
162 "Options:" \
163 "\n -a Append output" \
164 "\n -c Run COMMAND, not shell" \
165 "\n -f Flush output after each write" \
166 "\n -q Quiet" \
168 +#ifdef USE_REPLAY
169 +#define script_full_usage script_full_usage_base \
170 + "\n -t Send timing to stderr"
171 +#else
172 +#define script_full_usage script_full_usage_base
173 +#endif
174 +
175 #define sed_trivial_usage \
176 "[-efinr] pattern [files...]"
177 #define sed_full_usage "\n\n" \
179 --- busybox-1.12.0/util-linux/replay.c
180 +++ busybox-1.12.0/util-linux/replay.c
181 @@ -0,0 +1,41 @@
182 +/* vi: set sw=4 ts=4: */
183 +/*
184 + * replay - play back typescripts, using timing information
185 + *
186 + * pascal.bellard@ads-lu.com
187 + *
188 + * Licensed under GPLv2 or later, see file License in this tarball for details.
189 + *
190 + */
191 +
192 +#include "libbb.h"
193 +
194 +int replay_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
195 +int replay_main(int argc, char **argv)
196 +{
197 + const char *script = "typescript";
198 + double delay, factor = 1000000.0;
199 + int fd;
200 + long count;
201 + FILE *tfp;
202 +
203 + switch (argc) {
204 + case 4: factor /= atof(argv[3]);
205 + case 3: script = argv[2];
206 + case 2: break;
207 + default:
208 + bb_show_usage();
209 + }
210 +
211 + tfp = xfopen_for_read(argv[1]);
212 + fd = open(script, O_RDONLY);
213 + while (fscanf(tfp, "%lf %ld\n", &delay, &count) == 2) {
214 + usleep(delay * factor);
215 + bb_copyfd_exact_size(fd, STDOUT_FILENO, count);
216 + }
217 +#if ENABLE_FEATURE_CLEAN_UP
218 + close(fd);
219 + fclose(tfp);
220 +#endif
221 + return EXIT_SUCCESS;
222 +}