Linux-Trace-Devel Archive on lore.kernel.org
 help / color / Atom feed
From: Steven Rostedt <rostedt@goodmis.org>
To: "Tzvetomir Stoyanov (VMware)" <tz.stoyanov@gmail.com>
Cc: linux-trace-devel@vger.kernel.org
Subject: Re: [PATCH v29 5/5] trace-cmd [POC]: Add KVM timestamp synchronization plugin
Date: Tue, 23 Feb 2021 16:36:32 -0500
Message-ID: <20210223163632.438071c7@gandalf.local.home> (raw)
In-Reply-To: <20210219101457.2345089-6-tz.stoyanov@gmail.com>

On Fri, 19 Feb 2021 12:14:57 +0200
"Tzvetomir Stoyanov (VMware)" <tz.stoyanov@gmail.com> wrote:

> diff --git a/lib/trace-cmd/trace-timesync.c b/lib/trace-cmd/trace-timesync.c
> index fb18075b..aa3e3fc1 100644
> --- a/lib/trace-cmd/trace-timesync.c
> +++ b/lib/trace-cmd/trace-timesync.c
> @@ -63,6 +63,7 @@ static struct tsync_proto *tsync_proto_find(const char *proto_name)
>  void tracecmd_tsync_init(void)
>  {
>  	ptp_clock_sync_register();
> +	kvm_clock_sync_register();
>  }
>  
>  int tracecmd_tsync_proto_register(const char *proto_name, int accuracy, int roles,
> @@ -433,6 +434,7 @@ void tracecmd_tsync_free(struct tracecmd_time_sync *tsync)
>  	}
>  	pthread_mutex_destroy(&tsync->lock);
>  	pthread_cond_destroy(&tsync->cond);
> +	pthread_barrier_destroy(&tsync->first_sync);

I'm guessing that this was suppose to be added as a separate patch? If not,
it should be.


>  	free(tsync->clock_str);
>  }
>  
> @@ -630,23 +632,24 @@ static inline void get_ts_loop_delay(struct timespec *timeout, int delay_ms)
>   * It loops infinite, until the timesync semaphore is released
>   *
>   */
> -void tracecmd_tsync_with_guest(struct tracecmd_time_sync *tsync)
> +int tracecmd_tsync_with_guest(struct tracecmd_time_sync *tsync)
>  {
>  	struct tsync_probe_request_msg probe;
>  	int ts_array_size = CLOCK_TS_ARRAY;
>  	struct tsync_proto *proto;
>  	struct timespec timeout;
> +	bool first = true;
>  	bool end = false;
>  	int ret;
>  	int i;
>  
>  	proto = tsync_proto_find(tsync->proto_name);
>  	if (!proto || !proto->clock_sync_calc)
> -		return;
> +		return -1;
>  
>  	clock_context_init(tsync, false);
>  	if (!tsync->context)
> -		return;
> +		return -1;
>  
>  	if (tsync->loop_interval > 0 &&
>  	    tsync->loop_interval < (CLOCK_TS_ARRAY * 1000))
> @@ -664,6 +667,10 @@ void tracecmd_tsync_with_guest(struct tracecmd_time_sync *tsync)
>  			if (ret)
>  				break;
>  		}
> +		if (first) {
> +			first = false;
> +			pthread_barrier_wait(&tsync->first_sync);

As pthread_barrier_wait() and pthread_barrier_destroy() are used here, this
should not be in the library code. It should be in the trace-cmd code, or
the trace-cmd code should be in the library.

A pthread_barrier_wait() is dangerous and needs to be tightly coupled with
all use cases. Otherwise, you could end with a thread stuck in the barrier
and nothing wakes it up.

> +		}
>  		if (end || i < tsync->vcpu_count)
>  			break;
>  		if (tsync->loop_interval > 0) {
> @@ -685,4 +692,5 @@ void tracecmd_tsync_with_guest(struct tracecmd_time_sync *tsync)
>  				    TRACECMD_TSYNC_PROTO_NONE,
>  				    TRACECMD_TIME_SYNC_CMD_STOP,
>  				    0, NULL);
> +	return 0;
>  }
> diff --git a/tracecmd/trace-tsync.c b/tracecmd/trace-tsync.c
> index d7de8298..ec4b2d86 100644
> --- a/tracecmd/trace-tsync.c
> +++ b/tracecmd/trace-tsync.c
> @@ -61,14 +61,19 @@ error:
>  static void *tsync_host_thread(void *data)
>  {
>  	struct tracecmd_time_sync *tsync = NULL;
> +	int ret;
>  
>  	tsync = (struct tracecmd_time_sync *)data;
>  
> -	tracecmd_tsync_with_guest(tsync);
> +	ret = tracecmd_tsync_with_guest(tsync);
>  
>  	tracecmd_msg_handle_close(tsync->msg_handle);
>  	tsync->msg_handle = NULL;
>  
> +	/* tsync with guest failed, release the barrier */
> +	if (ret)
> +		pthread_barrier_wait(&tsync->first_sync);

This being needed here shows that the barrier logic needs to be separated
out. As this is in the trace-cmd proper, and its releasing the guest, and
this is exposing the internal logic of the lib/trace-cmd code, which is not
acceptable.

We probably want the guest logic moved here?

Either way, we need to make sure there's no path that could cause the guest
(or host) to get stuck in the barrier.

-- Steve


> +
>  	pthread_exit(0);
>  }
>  
> @@ -106,6 +111,7 @@ int tracecmd_host_tsync(struct buffer_instance *instance,
>  		instance->tsync.clock_str = strdup(top_instance.clock);
>  	pthread_mutex_init(&instance->tsync.lock, NULL);
>  	pthread_cond_init(&instance->tsync.cond, NULL);
> +	pthread_barrier_init(&instance->tsync.first_sync, NULL, 2);
>  
>  	pthread_attr_init(&attrib);
>  	pthread_attr_setdetachstate(&attrib, PTHREAD_CREATE_JOINABLE);
> @@ -117,6 +123,7 @@ int tracecmd_host_tsync(struct buffer_instance *instance,
>  		if (!get_first_cpu(&pin_mask, &mask_size))
>  			pthread_setaffinity_np(instance->tsync_thread, mask_size, pin_mask);
>  		instance->tsync_thread_running = true;
> +		pthread_barrier_wait(&instance->tsync.first_sync);
>  	}
>  
>  	if (pin_mask)


  reply index

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-02-19 10:14 [PATCH v29 0/5] Timestamp synchronization of host - guest tracing session Tzvetomir Stoyanov (VMware)
2021-02-19 10:14 ` [PATCH v29 1/5] trace-cmd: Add timestamp synchronization per vCPU Tzvetomir Stoyanov (VMware)
2021-02-19 10:14 ` [PATCH v29 2/5] trace-cmd: Add dummy function to initialize timestamp sync logic Tzvetomir Stoyanov (VMware)
2021-02-19 10:14 ` [PATCH v29 3/5] trace-cmd: [POC] PTP-like algorithm for host - guest timestamp synchronization Tzvetomir Stoyanov (VMware)
2021-02-19 10:14 ` [PATCH v29 4/5] trace-cmd: Debug scripts for " Tzvetomir Stoyanov (VMware)
2021-02-19 10:14 ` [PATCH v29 5/5] trace-cmd [POC]: Add KVM timestamp synchronization plugin Tzvetomir Stoyanov (VMware)
2021-02-23 21:36   ` Steven Rostedt [this message]
2021-02-24  6:22     ` Tzvetomir Stoyanov
2021-02-24 23:19       ` Steven Rostedt

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210223163632.438071c7@gandalf.local.home \
    --to=rostedt@goodmis.org \
    --cc=linux-trace-devel@vger.kernel.org \
    --cc=tz.stoyanov@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

Linux-Trace-Devel Archive on lore.kernel.org

Archives are clonable:
	git clone --mirror https://lore.kernel.org/linux-trace-devel/0 linux-trace-devel/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 linux-trace-devel linux-trace-devel/ https://lore.kernel.org/linux-trace-devel \
		linux-trace-devel@vger.kernel.org
	public-inbox-index linux-trace-devel

Example config snippet for mirrors

Newsgroup available over NNTP:
	nntp://nntp.lore.kernel.org/org.kernel.vger.linux-trace-devel


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git