aboutsummaryrefslogtreecommitdiff
path: root/lib/trace-cmd/trace-timesync-ptp.c
blob: 20e6e6f1ab59f1b2b52561e204107e1c540a0646 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
// SPDX-License-Identifier: LGPL-2.1
/*
 * Copyright (C) 2019, VMware, Tzvetomir Stoyanov tz.stoyanov@gmail.com>
 *
 */

#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <linux/types.h>
#include <time.h>
#include <sched.h>
#include <limits.h>

#include "trace-cmd.h"
#include "trace-cmd-private.h"
#include "tracefs.h"
#include "trace-tsync-local.h"
#include "trace-msg.h"
#include "trace-cmd-local.h"

typedef __be32 be32;
typedef __u64 u64;
typedef __s64 s64;

#define PTP_SYNC_LOOP	339

#define PTP_SYNC_PKT_START	1
#define PTP_SYNC_PKT_PROBE	2
#define PTP_SYNC_PKT_PROBES	3
#define PTP_SYNC_PKT_OFFSET	4
#define PTP_SYNC_PKT_END	5

/* print time sync debug messages */
/* #define TSYNC_DEBUG */

struct ptp_clock_sync {
	struct tep_handle	*tep;
	struct tep_format_field	*id;
	int			raw_id;
	int			marker_fd;
	int			series_id;
	int			flags;
	int			debug_fd;
};

enum {
/*
 * Consider only the probe with fastest response time,
 * otherwise make a histogram from all probes.
 */
	PTP_FLAG_FASTEST_RESPONSE		= (1 << 0),
/*
 * Use trace marker to get the clock,
 * otherwise use the system clock directly.
 */
	PTP_FLAG_USE_MARKER			= (1 << 1),
};
static int ptp_flags = PTP_FLAG_FASTEST_RESPONSE | PTP_FLAG_USE_MARKER;

/*
 * Calculated using formula [CPU rate]*[calculated offset deviation]
 * tested on 3GHz CPU, with x86-tsc trace clock and compare the calculated
 * offset with /sys/kernel/debug/kvm/<VM ID>/vcpu0/tsc-offset
 * measured 2000ns deviation
 * using PTP flags PTP_FLAG_FASTEST_RESPONSE | PTP_FLAG_USE_MARKER
 */
#define PTP_ACCURACY	6000
#define PTP_NAME	"ptp"

struct ptp_clock_start_msg {
	be32	series_id;
	be32	flags;
} __packed;

struct ptp_clock_sample {
	s64		ts;
	be32		id;
} __packed;

struct ptp_clock_result_msg {
	be32			series_id;
	be32			count;
	struct ptp_clock_sample	samples[2*PTP_SYNC_LOOP];
} __packed;

struct ptp_clock_offset_msg {
	s64	ts;
	s64	offset;
};

struct ptp_markers_context {
	struct clock_sync_context	*clock;
	struct ptp_clock_sync		*ptp;
	struct ptp_clock_result_msg	msg;
	int				size;
};

struct ptp_marker_buf {
	int local_id;
	int remote_id;
	int count;
	int packet_id;
} __packed;

struct ptp_marker {
	int series_id;
	struct ptp_marker_buf data;
} __packed;

static int ptp_clock_sync_init(struct tracecmd_time_sync *tsync)
{
	const char *systems[] = {"ftrace", NULL};
	struct clock_sync_context *clock_context;
	struct ptp_clock_sync *ptp;
	struct tep_event *raw;
	char *path;

	if (!tsync || !tsync->context)
		return -1;
	clock_context = (struct clock_sync_context *)tsync->context;
	if (clock_context->proto_data)
		return 0;

	ptp = calloc(1, sizeof(struct ptp_clock_sync));
	if (!ptp)
		return -1;

	ptp->marker_fd = -1;
	ptp->debug_fd = -1;

	path = tracefs_instance_get_dir(clock_context->instance);
	if (!path)
		goto error;
	ptp->tep = tracefs_local_events_system(path, systems);
	tracefs_put_tracing_file(path);
	if (!ptp->tep)
		goto error;
	raw = tep_find_event_by_name(ptp->tep, "ftrace", "raw_data");
	if (!raw)
		goto error;
	ptp->id = tep_find_field(raw, "id");
	if (!ptp->id)
		goto error;
	ptp->raw_id = raw->id;

	tep_set_file_bigendian(ptp->tep, tracecmd_host_bigendian());
	tep_set_local_bigendian(ptp->tep, tracecmd_host_bigendian());

	path = tracefs_instance_get_file(clock_context->instance, "trace_marker_raw");
	if (!path)
		goto error;
	ptp->marker_fd = open(path, O_WRONLY);
	tracefs_put_tracing_file(path);

	clock_context->proto_data = ptp;

#ifdef TSYNC_DEBUG
	if (clock_context->is_server) {
		char buff[256];
		int res_fd;

		sprintf(buff, "res-id%d.txt", clock_context->remote_id);

		res_fd = open(buff, O_CREAT|O_WRONLY|O_TRUNC, 0644);
		if (res_fd > 0)
			close(res_fd);
	}
#endif

	return 0;

error:
	if (ptp) {
		tep_free(ptp->tep);
		if (ptp->marker_fd >= 0)
			close(ptp->marker_fd);
	}
	free(ptp);
	return -1;
}

static int ptp_clock_sync_free(struct tracecmd_time_sync *tsync)
{
	struct clock_sync_context *clock_context;
	struct ptp_clock_sync *ptp;

	if (!tsync || !tsync->context)
		return -1;
	clock_context = (struct clock_sync_context *)tsync->context;

	if (clock_context && clock_context->proto_data) {
		ptp = (struct ptp_clock_sync *)clock_context->proto_data;
		tep_free(ptp->tep);
		if (ptp->marker_fd >= 0)
			close(ptp->marker_fd);
		if (ptp->debug_fd >= 0)
			close(ptp->debug_fd);
		free(clock_context->proto_data);
		clock_context->proto_data = NULL;
	}
	return 0;
}

/* Save the timestamps of sent ('s') and returned ('r') probes in the
 * ctx->msg.samples[] array. Depending of the context (server or client), there
 * may be only returned probes, or both sent and returned probes. The returned
 * probes are saved first in the array, after them are the sent probes.
 * Depending of the context, the array can be with size:
 *  [0 .. max data.count] - holds only returned probes
 *  [0 .. 2 * max data.count] - holds both returned and sent probes
 */
static void ptp_probe_store(struct ptp_markers_context *ctx,
			    struct ptp_marker *marker,
			    unsigned long long ts)
{
	int index = -1;

	if (marker->data.packet_id == 'r' &&
	    marker->data.count <= ctx->size) {
		index = marker->data.count - 1;
	} else if (marker->data.packet_id == 's' &&
		  marker->data.count * 2 <= ctx->size){
		index = ctx->size / 2 + marker->data.count - 1;
	}

	if (index >= 0) {
		ctx->msg.samples[index].id = marker->data.count;
		ctx->msg.samples[index].ts = ts;
		ctx->msg.count++;
	}
}

static int ptp_marker_find(struct tep_event *event, struct tep_record *record,
			   int cpu, void *context)
{
	struct ptp_markers_context *ctx;
	struct ptp_marker *marker;

	ctx = (struct ptp_markers_context *)context;

	/* Make sure this is our event */
	if (event->id != ctx->ptp->raw_id || !ctx->ptp->id)
		return 0;
	if (record->size >= (ctx->ptp->id->offset + sizeof(struct ptp_marker))) {
		marker = (struct ptp_marker *)(record->data + ctx->ptp->id->offset);
		if (marker->data.local_id == ctx->clock->local_id &&
		    marker->data.remote_id == ctx->clock->remote_id &&
		    marker->series_id == ctx->ptp->series_id &&
		    marker->data.count)
			ptp_probe_store(ctx, marker, record->ts);
	}

	return 0;
}

static inline bool good_probe(struct ptp_clock_sample *server_sample,
			      struct ptp_clock_sample *send_sample,
			      struct ptp_clock_sample *client_sample,
			      int *bad_probes)
{
	if (server_sample->ts && send_sample->ts && client_sample->ts &&
	    server_sample->id == send_sample->id &&
	    server_sample->id == client_sample->id)
		return true;
	(*bad_probes)++;
	return false;
}

static int ptp_calc_offset_fastest(struct clock_sync_context *clock,
			   struct ptp_clock_result_msg *server,
			   struct ptp_clock_result_msg *client,
			   long long *offset_ret, long long *ts_ret,
			   int *bad_probes)
{
	struct ptp_clock_sample *sample_send;
	long long delta_min = LLONG_MAX;
	long long offset = 0;
	long long delta = 0;
	long long ts = 0;
	int max_i;
	int i;

	*bad_probes = 0;
	sample_send = server->samples + (server->count / 2);
	max_i = server->count / 2 < client->count ?
		server->count / 2 : client->count;
	for (i = 0; i < max_i; i++) {
		if (!good_probe(&server->samples[i], &sample_send[i],
		    &client->samples[i], bad_probes))
			continue;
		ts = (sample_send[i].ts + server->samples[i].ts) / 2;
		offset = client->samples[i].ts - ts;

		delta = server->samples[i].ts - sample_send[i].ts;
		if (delta_min > delta) {
			delta_min = delta;
			*offset_ret = offset;
			*ts_ret = ts;
		}
#ifdef TSYNC_DEBUG
		{
			struct ptp_clock_sync *ptp;

			ptp = (struct ptp_clock_sync *)clock->proto_data;
			if (ptp && ptp->debug_fd > 0) {
				char buff[256];

				sprintf(buff, "%lld %lld %lld\n",
					ts, client->samples[i].ts, offset);
				write(ptp->debug_fd, buff, strlen(buff));
			}
		}
#endif
	}

	return 0;
}

static int ptp_calc_offset_hist(struct clock_sync_context *clock,
			   struct ptp_clock_result_msg *server,
			   struct ptp_clock_result_msg *client,
			   long long *offset_ret, long long *ts_ret,
			   int *bad_probes)
{
	struct ptp_clock_sample *sample_send;
	long long timestamps[PTP_SYNC_LOOP];
	long long offsets[PTP_SYNC_LOOP];
	long long offset_min = LLONG_MAX;
	long long offset_max = 0;
	int hist[PTP_SYNC_LOOP];
	int ind, max = 0;
	long long bin;
	int i, k = 0;

	*bad_probes = 0;
	memset(hist, 0, sizeof(int) * PTP_SYNC_LOOP);
	sample_send = server->samples + (server->count / 2);
	for (i = 0; i * 2 < server->count && i < client->count; i++) {
		if (!good_probe(&server->samples[i], &sample_send[i],
		    &client->samples[i], bad_probes))
			continue;
		timestamps[k] = (sample_send[i].ts + server->samples[i].ts) / 2;
		offsets[k] = client->samples[i].ts - timestamps[k];
		if (offset_max < llabs(offsets[k]))
			offset_max = llabs(offsets[k]);
		if (offset_min > llabs(offsets[k]))
			offset_min = llabs(offsets[k]);
#ifdef TSYNC_DEBUG
		{
			struct ptp_clock_sync *ptp;

			ptp = (struct ptp_clock_sync *)clock->proto_data;

			if (ptp && ptp->debug_fd > 0) {
				char buff[256];

				sprintf(buff, "%lld %lld %lld\n",
					timestamps[k],
					client->samples[i].ts, offsets[k]);
				write(ptp->debug_fd, buff, strlen(buff));
			}
		}
#endif
		k++;
	}

	bin = (offset_max - offset_min) / PTP_SYNC_LOOP;
	for (i = 0; i < k; i++) {
		ind = (llabs(offsets[i]) - offset_min) / bin;
		if (ind < PTP_SYNC_LOOP) {
			hist[ind]++;
			if (max < hist[ind]) {
				max = hist[ind];
				*offset_ret = offsets[i];
				*ts_ret = timestamps[i];
			}
		}
	}

	return 0;
}

static void ntoh_ptp_results(struct ptp_clock_result_msg *msg)
{
	int i;

	msg->count = ntohl(msg->count);
	for (i = 0; i < msg->count; i++) {
		msg->samples[i].id = ntohl(msg->samples[i].id);
		msg->samples[i].ts = ntohll(msg->samples[i].ts);
	}
	msg->series_id = ntohl(msg->series_id);
}


static void hton_ptp_results(struct ptp_clock_result_msg *msg)
{
	int i;

	for (i = 0; i < msg->count; i++) {
		msg->samples[i].id = htonl(msg->samples[i].id);
		msg->samples[i].ts = htonll(msg->samples[i].ts);
	}
	msg->series_id = htonl(msg->series_id);
	msg->count = htonl(msg->count);
}

static inline void ptp_track_clock(struct ptp_markers_context *ctx,
				   struct ptp_marker *marker)
{
	if (ctx->ptp->flags & PTP_FLAG_USE_MARKER) {
		write(ctx->ptp->marker_fd, marker, sizeof(struct ptp_marker));
	} else {
		struct timespec clock;
		unsigned long long ts;

		clock_gettime(CLOCK_MONOTONIC_RAW, &clock);
		ts = clock.tv_sec * 1000000000LL;
		ts += clock.tv_nsec;
		ptp_probe_store(ctx, marker, ts);
	}
}

static int ptp_clock_client(struct tracecmd_time_sync *tsync,
			    long long *offset, long long *timestamp)
{
	char sync_proto[TRACECMD_TSYNC_PNAME_LENGTH];
	struct clock_sync_context *clock_context;
	struct ptp_clock_offset_msg res_offset;
	struct ptp_clock_start_msg start;
	struct ptp_markers_context ctx;
	struct ptp_clock_sync *ptp;
	struct ptp_marker marker;
	unsigned int sync_msg;
	unsigned int size;
	char *msg;
	int count;
	int ret;

	if (!tsync || !tsync->context || !tsync->msg_handle)
		return -1;

	clock_context = (struct clock_sync_context *)tsync->context;
	if (clock_context->proto_data == NULL)
		return -1;

	ptp = (struct ptp_clock_sync *)clock_context->proto_data;
	size = sizeof(start);
	msg = (char *)&start;
	ret = tracecmd_msg_recv_time_sync(tsync->msg_handle,
					  sync_proto, &sync_msg,
					  &size, &msg);
	if (ret || strncmp(sync_proto, PTP_NAME, TRACECMD_TSYNC_PNAME_LENGTH) ||
	    sync_msg != PTP_SYNC_PKT_START)
		return -1;
	ret = tracecmd_msg_send_time_sync(tsync->msg_handle, PTP_NAME,
					  PTP_SYNC_PKT_START, sizeof(start),
					  (char *)&start);
	marker.data.local_id = clock_context->local_id;
	marker.data.remote_id = clock_context->remote_id;
	marker.series_id = ntohl(start.series_id);
	marker.data.packet_id = 'r';
	ptp->series_id = marker.series_id;
	ptp->flags = ntohl(start.flags);
	msg = (char *)&count;
	size = sizeof(count);
	ctx.msg.count = 0;
	ctx.size = PTP_SYNC_LOOP;
	ctx.ptp = ptp;
	ctx.clock = clock_context;
	ctx.msg.series_id = ptp->series_id;
	while (true) {
		count = 0;
		ret = tracecmd_msg_recv_time_sync(tsync->msg_handle,
						  sync_proto, &sync_msg,
						  &size, &msg);
		if (ret || strncmp(sync_proto, PTP_NAME, TRACECMD_TSYNC_PNAME_LENGTH) ||
		    sync_msg != PTP_SYNC_PKT_PROBE || !ntohl(count))
			break;
		marker.data.count = ntohl(count);
		ptp_track_clock(&ctx, &marker);
		ret = tracecmd_msg_send_time_sync(tsync->msg_handle, PTP_NAME,
						  PTP_SYNC_PKT_PROBE,
						  sizeof(count), (char *)&count);
		if (ret)
			break;
	}

	if (strncmp(sync_proto, PTP_NAME, TRACECMD_TSYNC_PNAME_LENGTH) ||
	    sync_msg != PTP_SYNC_PKT_END)
		return -1;

	if (ptp->flags & PTP_FLAG_USE_MARKER)
		tracefs_iterate_raw_events(ptp->tep, clock_context->instance,
					   NULL, 0, ptp_marker_find, &ctx);

	hton_ptp_results(&ctx.msg);
	ret = tracecmd_msg_send_time_sync(tsync->msg_handle, PTP_NAME,
					  PTP_SYNC_PKT_PROBES,
					  sizeof(ctx.msg), (char *)&ctx.msg);

	msg = (char *)&res_offset;
	size = sizeof(res_offset);
	ret = tracecmd_msg_recv_time_sync(tsync->msg_handle,
					  sync_proto, &sync_msg,
					  &size, (char **)&msg);
	if (ret || strncmp(sync_proto, PTP_NAME, TRACECMD_TSYNC_PNAME_LENGTH) ||
	    sync_msg != PTP_SYNC_PKT_OFFSET)
		return -1;

	*offset = ntohll(res_offset.offset);
	*timestamp = ntohll(res_offset.ts);

	return 0;
}


static int ptp_clock_server(struct tracecmd_time_sync *tsync,
			    long long *offset, long long *timestamp)
{
	char sync_proto[TRACECMD_TSYNC_PNAME_LENGTH];
	struct ptp_clock_result_msg *results = NULL;
	struct clock_sync_context *clock_context;
	struct ptp_clock_offset_msg res_offset;
	struct ptp_clock_start_msg start;
	struct ptp_markers_context ctx;
	int sync_loop = PTP_SYNC_LOOP;
	struct ptp_clock_sync *ptp;
	struct ptp_marker marker;
	unsigned int sync_msg;
	unsigned int size;
	int bad_probes;
	int count = 1;
	int msg_count;
	int msg_ret;
	char *msg;
	int ret;

	if (!tsync || !tsync->context || !tsync->msg_handle)
		return -1;

	clock_context = (struct clock_sync_context *)tsync->context;
	if (clock_context->proto_data == NULL)
		return -1;

	ptp = (struct ptp_clock_sync *)clock_context->proto_data;
	ptp->flags = ptp_flags;
	memset(&start, 0, sizeof(start));
	start.series_id = htonl(ptp->series_id + 1);
	start.flags = htonl(ptp->flags);
	ret = tracecmd_msg_send_time_sync(tsync->msg_handle, PTP_NAME,
					 PTP_SYNC_PKT_START, sizeof(start),
					 (char *)&start);
	if (!ret)
		ret = tracecmd_msg_recv_time_sync(tsync->msg_handle,
						  sync_proto, &sync_msg,
						  NULL, NULL);
	if (ret || strncmp(sync_proto, PTP_NAME, TRACECMD_TSYNC_PNAME_LENGTH) ||
	    sync_msg != PTP_SYNC_PKT_START)
		return -1;

	tracefs_instance_file_write(clock_context->instance, "trace", "\0");

	ptp->series_id++;
	marker.data.local_id = clock_context->local_id;
	marker.data.remote_id = clock_context->remote_id;
	marker.series_id = ptp->series_id;
	msg = (char *)&msg_ret;
	size = sizeof(msg_ret);
	ctx.size = 2*PTP_SYNC_LOOP;
	ctx.ptp = ptp;
	ctx.clock = clock_context;
	ctx.msg.count = 0;
	ctx.msg.series_id = ptp->series_id;
	do {
		marker.data.count = count++;
		marker.data.packet_id = 's';
		msg_count = htonl(marker.data.count);
		ptp_track_clock(&ctx, &marker);
		ret = tracecmd_msg_send_time_sync(tsync->msg_handle, PTP_NAME,
						 PTP_SYNC_PKT_PROBE,
						 sizeof(msg_count),
						 (char *)&msg_count);
		if (!ret)
			ret = tracecmd_msg_recv_time_sync(tsync->msg_handle,
							  sync_proto, &sync_msg,
							  &size, &msg);

		marker.data.packet_id = 'r';
		ptp_track_clock(&ctx, &marker);
		if (ret || strncmp(sync_proto, PTP_NAME, TRACECMD_TSYNC_PNAME_LENGTH) ||
		    sync_msg != PTP_SYNC_PKT_PROBE ||
		    ntohl(msg_ret) != marker.data.count)
			break;
	} while (--sync_loop);

	if (sync_loop)
		return -1;

	ret = tracecmd_msg_send_time_sync(tsync->msg_handle, PTP_NAME,
					  PTP_SYNC_PKT_END, 0, NULL);

	size = 0;
	ret = tracecmd_msg_recv_time_sync(tsync->msg_handle,
					  sync_proto, &sync_msg,
					  &size, (char **)&results);
	if (ret || strncmp(sync_proto, PTP_NAME, TRACECMD_TSYNC_PNAME_LENGTH) ||
	    sync_msg != PTP_SYNC_PKT_PROBES || size == 0 || results == NULL)
		return -1;

	ntoh_ptp_results(results);
	if (ptp->flags & PTP_FLAG_USE_MARKER)
		tracefs_iterate_raw_events(ptp->tep, clock_context->instance,
					   NULL, 0, ptp_marker_find, &ctx);
	if (ptp->flags & PTP_FLAG_FASTEST_RESPONSE)
		ptp_calc_offset_fastest(clock_context, &ctx.msg, results, offset,
					timestamp, &bad_probes);
	else
		ptp_calc_offset_hist(clock_context, &ctx.msg, results, offset,
				     timestamp, &bad_probes);
#ifdef TSYNC_DEBUG
	{
		char buff[256];
		int res_fd;

		sprintf(buff, "res-id%d.txt", clock_context->remote_id);

		res_fd = open(buff, O_WRONLY|O_APPEND, 0644);
		if (res_fd > 0) {
			if (*offset && *timestamp) {
				sprintf(buff, "%d %lld %lld\n",
					ptp->series_id, *offset, *timestamp);
				write(res_fd, buff, strlen(buff));
			}
			close(res_fd);
		}

		printf("\n calculated offset %d: %lld, %d probes, filtered out %d, PTP flags 0x%X\n\r",
			ptp->series_id, *offset, results->count, bad_probes, ptp->flags);
		if (ptp && ptp->debug_fd > 0) {
			sprintf(buff, "%lld %lld 0\n", *offset, *timestamp);
			write(ptp->debug_fd, buff, strlen(buff));
			close(ptp->debug_fd);
			ptp->debug_fd = -1;
		}

	}
#endif

	res_offset.offset = htonll(*offset);
	res_offset.ts = htonll(*timestamp);
	ret = tracecmd_msg_send_time_sync(tsync->msg_handle, PTP_NAME,
					  PTP_SYNC_PKT_OFFSET,
					  sizeof(res_offset),
					  (char *)&res_offset);

	free(results);
	return 0;
}

static int ptp_clock_sync_calc(struct tracecmd_time_sync *tsync,
			       long long *offset, long long *scaling, long long *frac,
			       long long *timestamp, unsigned int cpu)
{
	struct clock_sync_context *clock_context;
	int ret;

	if (!tsync || !tsync->context)
		return -1;
	clock_context = (struct clock_sync_context *)tsync->context;

#ifdef TSYNC_DEBUG
	if (clock_context->is_server) {
		struct ptp_clock_sync *ptp;
		char buff[256];

		ptp = (struct ptp_clock_sync *)clock_context->proto_data;
		if (ptp->debug_fd > 0)
			close(ptp->debug_fd);
		sprintf(buff, "s-id%d_%d.txt",
				clock_context->remote_id, ptp->series_id+1);
		ptp->debug_fd = open(buff, O_CREAT|O_WRONLY|O_TRUNC, 0644);
	}
#endif

	if (scaling)
		*scaling = 1;
	if (frac)
		*frac = 0;
	if (clock_context->is_server)
		ret = ptp_clock_server(tsync, offset, timestamp);
	else
		ret = ptp_clock_client(tsync, offset, timestamp);

	return ret;
}

int ptp_clock_sync_register(void)
{
	return tracecmd_tsync_proto_register(PTP_NAME, PTP_ACCURACY,
					     TRACECMD_TIME_SYNC_ROLE_GUEST |
					     TRACECMD_TIME_SYNC_ROLE_HOST |
					     TRACECMD_TIME_SYNC_ROLE_CLIENT |
					     TRACECMD_TIME_SYNC_ROLE_SERVER,
					     0, TRACECMD_TSYNC_FLAG_INTERPOLATE,
					     ptp_clock_sync_init,
					     ptp_clock_sync_free,
					     ptp_clock_sync_calc);

}

int ptp_clock_sync_unregister(void)
{
	return tracecmd_tsync_proto_unregister(PTP_NAME);
}