trace-cmd: Fix trace-cmd stream
cover trace-cmd: Fix trace-cmd stream
From: "Steven Rostedt (Google)" <[email protected]>
Looking at https://bugzilla.kernel.org/show_bug.cgi?id=216577 and analyzing the fix, I found a bunch of other issues with trace-cmd stream.
This series is to address them.
Steven Rostedt (Google) (10):
trace-cmd library: Set recorder to nonblock when finished trace-cmd stream: Do not block when stopping threads trace-cmd stream: Add a flush signal to kick the output trace-cmd stream: Do one last flush when finished trace-cmd library: Fix read_data() with error from tracefs_cpu_read()
trace-cmd: Have trace_stream_read() use poll()
trace-cmd stream: Set default sleep time to half a second trace-cmd library: Return the result of tracefs_cpu_stop() trace-cmd record: Set sleep_time to zero at end of recording trace-cmd record: Keep stopping the recording when finished
.../include/private/trace-cmd-private.h | 4 +- lib/trace-cmd/trace-recorder.c | 33 ++++++--- tracecmd/include/trace-local.h | 2 +- tracecmd/trace-record.c | 72 +++++++++++++++---- tracecmd/trace-stream.c | 34 ++++----- 5 files changed, 99 insertions(+), 46 deletions(-)
Signature · 1 line
2.35.1
PATCH 1/10 trace-cmd library: Set recorder to nonblock when finished
From: "Steven Rostedt (Google)" <[email protected]>
When the recorder is signaled to stop, make sure it only reads in non-blocking mode.
--- lib/trace-cmd/trace-recorder.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-)
lib/trace-cmd/trace-recorder.c +6 -3
@@ -317,11 +317,12 @@ static inline void update_fd(struct tracecmd_recorder *recorder, int size)*/ static long read_data (struct tracecmd_recorder *recorder) { +bool nonblock = recorder->stop;char buf[recorder->subbuf_size];long left;long r, w; - r =tracefs_cpu_read (recorder->tcpu, buf, false); + r =tracefs_cpu_read (recorder->tcpu, buf, nonblock); left = r; do {
@@ -344,11 +345,13 @@ static long read_data(struct tracecmd_recorder *recorder)*/ static long direct_splice_data (struct tracecmd_recorder *recorder) { -return tracefs_cpu_pipe (recorder->tcpu, recorder->fd, false); +bool nonblock = recorder->stop; +return tracefs_cpu_pipe (recorder->tcpu, recorder->fd, nonblock); }static long move_data (struct tracecmd_recorder *recorder) { +bool nonblock = recorder->stop;long ret;if (recorder->flags &TRACECMD_RECORD_NOSPLICE )
@@ -357,7 +360,7 @@ static long move_data(struct tracecmd_recorder *recorder)if (recorder->flags &TRACECMD_RECORD_NOBRASS )return direct_splice_data (recorder); - ret =tracefs_cpu_write (recorder->tcpu, recorder->fd, false); + ret =tracefs_cpu_write (recorder->tcpu, recorder->fd, nonblock);if (ret >0 )update_fd (recorder, ret);return ret;
Signature · 1 line
2.35.1
PATCH 2/10 trace-cmd stream: Do not block when stopping threads
In reply to Steven RostedtFrom: "Steven Rostedt (Google)" <[email protected]>
When the trace-cmd stream is exiting, it flushes the data. But it does so in a blocking way, where it can block when there's nothing to read. Set the timeout of trace_stream_read() to zero in order to return immediately.
--- tracecmd/trace-record.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
tracecmd/trace-record.c +2 -1
@@ -798,7 +798,8 @@ static void stop_threads(enum trace_type type)/* Flush out the pipes */ if (type &TRACE_TYPE_STREAM ) {do { - ret =trace_stream_read (pids, recorder_threads,NULL ); +struct timeval tv = {0 ,0 }; + ret =trace_stream_read (pids, recorder_threads, &tv); }while (ret >0 ); } }
Signature · 1 line
2.35.1
PATCH 3/10 trace-cmd stream: Add a flush signal to kick the output
In reply to Steven RostedtFrom: "Steven Rostedt (Google)" <[email protected]>
Have trace-cmd stream periodically signal the recorder tasks to flush some output, otherwise it will look empty for a long time (especially with the default setting of a 50 percent buffering, where the ring buffers will not return read until they are half full).
Add a "finish" flag to tracecmd_flush_recording() that will keep the old behavior when set, but when not set, it will only do a single tracefs_cpu_flush() that will output some data, but not all of it.
Use the SIGUSR2 to signal the recorder tasks to do the flush.
--- .../include/private/trace-cmd-private.h | 2 +- lib/trace-cmd/trace-recorder.c | 10 ++++- tracecmd/trace-record.c | 41 +++++++++++++++---- 3 files changed, 43 insertions(+), 10 deletions(-)
lib/trace-cmd/include/private/trace-cmd-private.h +1 -1
@@ -378,7 +378,7 @@ struct tracecmd_recorder *tracecmd_create_buffer_recorder_maxkb(const char *fileint tracecmd_start_recording (struct tracecmd_recorder *recorder,unsigned long sleep);void tracecmd_stop_recording (struct tracecmd_recorder *recorder); -long tracecmd_flush_recording (struct tracecmd_recorder *recorder); +long tracecmd_flush_recording (struct tracecmd_recorder *recorder,bool finish);enum tracecmd_msg_flags {TRACECMD_MSG_FL_USE_TCP =1 <<0 ,
lib/trace-cmd/trace-recorder.c +8 -2
@@ -366,13 +366,19 @@ static long move_data(struct tracecmd_recorder *recorder)return ret; } -long tracecmd_flush_recording (struct tracecmd_recorder *recorder) +long tracecmd_flush_recording (struct tracecmd_recorder *recorder,bool finish) {char buf[recorder->subbuf_size];long total =0 ;long wrote =0 ;long ret; +if (!recorder) +return 0 ; + +if (!finish) +return tracefs_cpu_flush_write (recorder->tcpu, recorder->fd); +set_nonblock (recorder); do {
@@ -421,7 +427,7 @@ int tracecmd_start_recording(struct tracecmd_recorder *recorder, unsigned long s }while (!recorder->stop);/* Flush out the rest */ - ret =tracecmd_flush_recording (recorder); + ret =tracecmd_flush_recording (recorder, true);if (ret <0 )return ret;
tracecmd/trace-record.c +34 -7
80 lines not on this page · raw message
Signature · 1 line
2.35.1
PATCH 4/10 trace-cmd stream: Do one last flush when finished
In reply to Steven RostedtFrom: "Steven Rostedt (Google)" <[email protected]>
When trace-cmd stream is finished, the trace_or_sleep() will exit before waiting for the pipes to flush. Call trace_stream_read() one more time, and with indefinite blocking, as the pipes should finish everything and then close (which will stop the polling).
--- tracecmd/trace-record.c | 3 +++ 1 file changed, 3 insertions(+)
tracecmd/trace-record.c +3 -0
@@ -7019,6 +7019,9 @@ static void record_trace(int argc, char **argv, }while (!finished && wait_indefinitely)trace_or_sleep (type, pwait); +/* Streams need to be flushed one more time */ +if (type &TRACE_TYPE_STREAM ) +trace_stream_read (pids, recorder_threads,NULL ); }tell_guests_to_stop (ctx);
Signature · 1 line
2.35.1
PATCH 5/10 trace-cmd library: Fix read_data() with error from tracefs_cpu_read()
In reply to Steven RostedtFrom: "Steven Rostedt (Google)" <[email protected]>
If tracefs_cpu_read() has an error, it will return a negative value. But the value is then used as an index. An error check is required to make sure that if an error is returned from tracefs_cpu_read() the function exits with that error and does not continue further.
--- lib/trace-cmd/trace-recorder.c | 2 ++ 1 file changed, 2 insertions(+)
lib/trace-cmd/trace-recorder.c +2 -0
@@ -323,6 +323,8 @@ static long read_data(struct tracecmd_recorder *recorder)long r, w; r =tracefs_cpu_read (recorder->tcpu, buf, nonblock); +if (r <0 ) +return r; left = r; do {
Signature · 1 line
2.35.1
PATCH 6/10 trace-cmd: Have trace_stream_read() use poll()
In reply to Steven RostedtFrom: "Steven Rostedt (Google)" <[email protected]>
Replace the select() that is used in trace_stream_read() with poll() as that is the more robust function, as select() is limited to fds that are less than 1024, and today's kernels can have fds bigger than that.
As a side-effect, since poll() uses milliseconds for its timeout, the granularity of the timeout is no longer microseconds, even though that is what is passed in on the command line. We may need to changes this.
If 1us is passed in, it will be rounded up to 1ms. But zero and negative numbers shall remain the same.
--- tracecmd/include/trace-local.h | 2 +- tracecmd/trace-record.c | 13 +++++-------- tracecmd/trace-stream.c | 34 +++++++++++++++------------------- 3 files changed, 21 insertions(+), 28 deletions(-)
tracecmd/include/trace-local.h +1 -1
@@ -137,7 +137,7 @@ struct tracecmd_input * trace_stream_init(struct buffer_instance *instance, int cpu, int fd, int cpus, struct hook_list *hooks, tracecmd_handle_init_func handle_init, int global); -int trace_stream_read (struct pid_record_data *pids,int nr_pids,struct timeval *tv); +int trace_stream_read (struct pid_record_data *pids,int nr_pids,long sleep_us);void trace_show_data (struct tracecmd_input *handle,struct tep_record *record);
tracecmd/trace-record.c +5 -8
@@ -73,7 +73,7 @@ static int rt_prio;static int keep;static int latency; -static int sleep_time =1000 ; +static long sleep_time =1000 ;static int recorder_threads;static struct pid_record_data *pids;static int buffers;
@@ -798,8 +798,7 @@ static void stop_threads(enum trace_type type)/* Flush out the pipes */ if (type &TRACE_TYPE_STREAM ) {do { -struct timeval tv = {0 ,0 }; - ret =trace_stream_read (pids, recorder_threads, &tv); + ret =trace_stream_read (pids, recorder_threads,0 ); }while (ret >0 ); } }
@@ -1307,7 +1306,6 @@ static void update_task_filter(void)static pid_t trace_waitpid (enum trace_type type,pid_t pid,int *status,int options) { -struct timeval tv = {1 ,0 };int ret;if (type &TRACE_TYPE_STREAM )
@@ -1319,7 +1317,7 @@ static pid_t trace_waitpid(enum trace_type type, pid_t pid, int *status, int optreturn ret;if (type &TRACE_TYPE_STREAM ) -trace_stream_read (pids, recorder_threads, &tv); +trace_stream_read (pids, recorder_threads, sleep_time); }while (1 ); }
@@ -1640,14 +1638,13 @@ static inline void ptrace_attach(struct buffer_instance *instance, int pid) { }static void trace_or_sleep (enum trace_type type,bool pwait) { -struct timeval tv = {1 ,0 };int i;if (pwait)ptrace_wait (type);else if (type &TRACE_TYPE_STREAM ) {/* Returns zero if it did not read anything (and did a sleep) */ -if (trace_stream_read (pids, recorder_threads, &tv) >0 ) +if (trace_stream_read (pids, recorder_threads, sleep_time) >0 )return ;/* Force a flush if nothing was read (including on errors) */ for (i =0 ; i < recorder_threads; i++) {
@@ -7021,7 +7018,7 @@ static void record_trace(int argc, char **argv,trace_or_sleep (type, pwait);/* Streams need to be flushed one more time */ if (type &TRACE_TYPE_STREAM ) -trace_stream_read (pids, recorder_threads,NULL ); +trace_stream_read (pids, recorder_threads,-1 ); }tell_guests_to_stop (ctx);
tracecmd/trace-stream.c +15 -19
@@ -4,6 +4,7 @@* */ #include <stdio.h> +#include <poll.h> #include <unistd.h> #include <fcntl.h> #include <errno.h>
@@ -83,17 +84,19 @@ trace_stream_init(struct buffer_instance *instance, int cpu, int fd, int cpus,return NULL ; } -int trace_stream_read (struct pid_record_data *pids,int nr_pids,struct timeval *tv) +int trace_stream_read (struct pid_record_data *pids,int nr_pids,long sleep_us) { -struct tep_record *record; -struct pid_record_data *pid;struct pid_record_data *last_pid; -fd_set rfds; -int top_rfd =0 ; -int nr_fd; +struct pid_record_data *pid; +struct tep_record *record; +struct pollfd pollfd[nr_pids]; +long sleep_ms = sleep_us >0 ? (sleep_us +999 ) /1000 : sleep_us;int ret;int i; +if (!nr_pids) +return 0 ; + last_pid =NULL ; again:
@@ -118,25 +121,18 @@ int trace_stream_read(struct pid_record_data *pids, int nr_pids, struct timevalreturn 1 ; } - nr_fd =0 ; -FD_ZERO (&rfds); -for (i =0 ; i < nr_pids; i++) {/* Do not process closed pipes */ -if (pids[i].closed) +if (pids[i].closed) { +memset (pollfd + i,0 ,sizeof (*pollfd));continue ; - nr_fd++; -if (pids[i].brass[0 ] > top_rfd) - top_rfd = pids[i].brass[0 ]; + } -FD_SET (pids[i].brass[0 ], &rfds); + pollfd[i].fd = pids[i].brass[0 ]; + pollfd[i].events =POLLIN ; } -if (!nr_fd) -return 0 ; - - ret =select (top_rfd +1 , &rfds,NULL ,NULL , tv); - + ret =poll (pollfd, nr_pids, sleep_ms);if (ret >0 ) goto again;
Signature · 1 line
2.35.1
PATCH 7/10 trace-cmd stream: Set default sleep time to half a second
In reply to Steven RostedtFrom: "Steven Rostedt (Google)" <[email protected]>
When in streaming mode, set the default to half a second instead of 1 millisecond (which should be changed for record as well).
--- tracecmd/trace-record.c | 4 ++++ 1 file changed, 4 insertions(+)
tracecmd/trace-record.c +4 -0
@@ -7161,11 +7161,15 @@ void trace_stream(int argc, char **argv) {struct common_record_context ctx; +/* Default sleep time is half a second for streaming */ + sleep_time =500000 ; +parse_record_options (argc, argv, CMD_stream, &ctx);record_trace_command (argc, argv, &ctx);exit (0 ); } +void trace_profile (int argc,char **argv) {struct common_record_context ctx;
Signature · 1 line
2.35.1
PATCH 8/10 trace-cmd library: Return the result of tracefs_cpu_stop()
In reply to Steven RostedtFrom: "Steven Rostedt (Google)" <[email protected]>
Ideally, tracefs_cpu_stop() is suppose to return zero if it guaranteed to stop the recorders (but this may not be true if called from a signal handler). Return the result of tracefs_cpu_stop() in tracecmd_stop_recording().
--- lib/trace-cmd/include/private/trace-cmd-private.h | 2 +- lib/trace-cmd/trace-recorder.c | 12 ++++++------ 2 files changed, 7 insertions(+), 7 deletions(-)
lib/trace-cmd/include/private/trace-cmd-private.h +1 -1
@@ -377,7 +377,7 @@ struct tracecmd_recorder *tracecmd_create_buffer_recorder(const char *file, intstruct tracecmd_recorder *tracecmd_create_buffer_recorder_maxkb (const char *file,int cpu,unsigned flags,struct tracefs_instance *instance,int maxkb);int tracecmd_start_recording (struct tracecmd_recorder *recorder,unsigned long sleep); -void tracecmd_stop_recording (struct tracecmd_recorder *recorder); +int tracecmd_stop_recording (struct tracecmd_recorder *recorder);long tracecmd_flush_recording (struct tracecmd_recorder *recorder,bool finish);enum tracecmd_msg_flags {
lib/trace-cmd/trace-recorder.c +6 -6
@@ -102,9 +102,9 @@ void tracecmd_free_recorder(struct tracecmd_recorder *recorder)free (recorder); } -static void set_nonblock (struct tracecmd_recorder *recorder) +static int set_nonblock (struct tracecmd_recorder *recorder) { -tracefs_cpu_stop (recorder->tcpu); +return tracefs_cpu_stop (recorder->tcpu); } static struct tracecmd_recorder *
@@ -437,12 +437,12 @@ int tracecmd_start_recording(struct tracecmd_recorder *recorder, unsigned long sreturn 0 ; } -void tracecmd_stop_recording (struct tracecmd_recorder *recorder) +int tracecmd_stop_recording (struct tracecmd_recorder *recorder) {if (!recorder) -return ; - -set_nonblock (recorder); +return -1 ; recorder->stop =1 ; + +return set_nonblock (recorder); }
Signature · 1 line
2.35.1
PATCH 9/10 trace-cmd record: Set sleep_time to zero at end of recording
In reply to Steven RostedtFrom: "Steven Rostedt (Google)" <[email protected]>
When the recording (of trace-cmd record) or streaming (of trace-cmd stream) is finished, set sleep_time to zero. This will cause various calls to read the buffer to not block, as the sleep_time is used in some instances to determine how long to wait if there's no data.
--- tracecmd/trace-record.c | 2 ++ 1 file changed, 2 insertions(+)
tracecmd/trace-record.c +2 -0
@@ -3164,6 +3164,8 @@ static void expand_event_list(void)static void finish (void ) { + sleep_time =0 ; +/* all done */ if (recorder)tracecmd_stop_recording (recorder);
Signature · 1 line
2.35.1
PATCH 10/10 trace-cmd record: Keep stopping the recording when finished
In reply to Steven RostedtFrom: "Steven Rostedt (Google)" <[email protected]>
The tracecmd_stop_recording() is suppose to force the recorders to finish, but because it is called from a signal handler, that may not necessarily be the case (due to race conditions and such). Set an alarm to try again in a second, then after two seconds, and then three, and so on.
--- tracecmd/trace-record.c | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-)
tracecmd/trace-record.c +13 -1
@@ -3164,11 +3164,21 @@ static void expand_event_list(void)static void finish (void ) { +static int secs =1 ; + sleep_time =0 ;/* all done */ -if (recorder) +if (recorder) {tracecmd_stop_recording (recorder); +/* +* We could just call the alarm if the above returned non zero, +* as zero is suppose to guarantee that the reader woke up. +* But as this is called from a signal handler, that may not +* necessarily be the case. +*/ +alarm (secs++); + } finished =1 ; }
@@ -3181,6 +3191,7 @@ static void flush(void)static void do_sig (int sig) {switch (sig) { +case SIGALRM :case SIGUSR1 :case SIGINT :return finish ();
@@ -3432,6 +3443,7 @@ static int create_recorder(struct buffer_instance *instance, int cpu,signal (SIGINT ,SIG_IGN );signal (SIGUSR1 , do_sig);signal (SIGUSR2 , do_sig); +signal (SIGALRM , do_sig);if (rt_prio)set_prio (rt_prio);
Signature · 1 line
2.35.1