[Linuxptp-devel] [PATCH 11/14] phc2sys: autoconfiguration

2014-05-07 Thread Jiri Benc
Add automatic configuration option (-a).

Signed-off-by: Jiri Benc 
---
 phc2sys.c |  265 +
 1 files changed, 250 insertions(+), 15 deletions(-)

diff --git a/phc2sys.c b/phc2sys.c
index 731c2bb1af35..d102ca8e8d93 100644
--- a/phc2sys.c
+++ b/phc2sys.c
@@ -22,6 +22,7 @@
 #include 
 #include 
 #include 
+#include 
 #include 
 #include 
 #include 
@@ -105,9 +106,11 @@ struct node {
struct clock *master;
 };
 
-static int update_sync_offset(struct node *node);
+static int update_pmc(struct node *node, int subscribe);
 static int clock_handle_leap(struct node *node, struct clock *clock,
 int64_t offset, uint64_t ts, int do_leap);
+static int run_pmc_get_utc_offset(struct node *node, int timeout);
+static void run_pmc_events(struct node *node);
 
 static clockid_t clock_open(char *device)
 {
@@ -262,6 +265,78 @@ static struct port *port_add(struct node *node, unsigned 
int number,
return p;
 }
 
+static void clock_reinit(struct clock *clock)
+{
+   servo_reset(clock->servo);
+   clock->servo_state = SERVO_UNLOCKED;
+
+   if (clock->offset_stats) {
+   stats_reset(clock->offset_stats);
+   stats_reset(clock->freq_stats);
+   stats_reset(clock->delay_stats);
+   }
+}
+
+static void reconfigure(struct node *node)
+{
+   struct clock *c, *rt, *src;
+   int src_cnt = 0, dst_cnt = 0;
+
+   pr_info("reconfiguring after port state change");
+   node->state_changed = 0;
+
+   src = rt = NULL;
+   LIST_FOREACH(c, &node->clocks, list) {
+   if (c->clkid == CLOCK_REALTIME) {
+   rt = c;
+   continue;
+   }
+
+   if (c->new_state == PS_MASTER)
+   clock_reinit(c);
+
+   c->state = c->new_state;
+   c->new_state = 0;
+
+   if (c->state == PS_SLAVE) {
+   src = c;
+   src_cnt++;
+   } else if (c->state == PS_UNCALIBRATED) {
+   src_cnt++;
+   } else if (c->state == PS_MASTER) {
+   pr_info("selecting %s for synchronization", c->device);
+   dst_cnt++;
+   }
+   }
+   if (src_cnt > 1) {
+   pr_info("multiple master clocks available, postponing sync...");
+   node->master = NULL;
+   return;
+   }
+   if (src_cnt > 0 && !src) {
+   pr_info("master clock not ready, waiting...");
+   node->master = NULL;
+   return;
+   }
+   if (!src_cnt && !dst_cnt) {
+   pr_info("no PHC ready, waiting...");
+   node->master = NULL;
+   return;
+   }
+   if (!src_cnt) {
+   src = rt;
+   rt->state = PS_SLAVE;
+   } else {
+   if (rt->state != PS_MASTER) {
+   rt->state = PS_MASTER;
+   clock_reinit(rt);
+   }
+   pr_info("selecting %s for synchronization", rt->device);
+   }
+   node->master = src;
+   pr_info("selecting %s as the master clock", src->device);
+}
+
 static int read_phc(clockid_t clkid, clockid_t sysclk, int readings,
int64_t *offset, uint64_t *ts, int64_t *delay)
 {
@@ -460,7 +535,7 @@ static int do_pps_loop(struct node *node, struct clock 
*clock, int fd)
pps_offset = pps_ts - phc_ts;
}
 
-   do_leap = update_sync_offset(node);
+   do_leap = update_pmc(node, 0);
if (do_leap < 0)
continue;
update_clock(node, clock, pps_offset, pps_ts, -1, do_leap);
@@ -469,13 +544,12 @@ static int do_pps_loop(struct node *node, struct clock 
*clock, int fd)
return 0;
 }
 
-static int do_loop(struct node *node)
+static int do_loop(struct node *node, int subscriptions)
 {
struct timespec interval;
struct clock *clock;
uint64_t ts;
int64_t offset, delay;
-   int src_fd = CLOCKID_TO_FD(node->master->clkid);
int do_leap;
 
interval.tv_sec = node->phc_interval;
@@ -483,18 +557,34 @@ static int do_loop(struct node *node)
 
while (1) {
clock_nanosleep(CLOCK_MONOTONIC, 0, &interval, NULL);
-   do_leap = update_sync_offset(node);
+   do_leap = update_pmc(node, 0);
if (do_leap < 0)
continue;
 
+   if (subscriptions) {
+   run_pmc_events(node);
+   if (node->state_changed) {
+   /* force getting offset, as it may have
+* changed after the port state change */
+   if (run_pmc_get_utc_offset(node, 1000) <= 0) {
+ 

Re: [Linuxptp-devel] [PATCH 11/14] phc2sys: autoconfiguration

2014-05-12 Thread Miroslav Lichvar
On Wed, May 07, 2014 at 06:23:15PM +0200, Jiri Benc wrote:
> Add automatic configuration option (-a).

This seems to be working, but only when phc2sys is running for short
time. If it runs for more than few minutes, it seems to stop reacting
to port state changes.

> 
> Signed-off-by: Jiri Benc 
> ---
>  phc2sys.c |  265 
> +
>  1 files changed, 250 insertions(+), 15 deletions(-)

> +static int do_loop(struct node *node, int subscriptions)
> @@ -483,18 +557,34 @@ static int do_loop(struct node *node)
>  
>   while (1) {
>   clock_nanosleep(CLOCK_MONOTONIC, 0, &interval, NULL);
> - do_leap = update_sync_offset(node);
> + do_leap = update_pmc(node, 0);

Shouldn't the second parameter be "subscriptions > 0"?

Also, are you planning to implement some check that would abort/reinit
phc2sys when ptp4l is killed (update_pmc fails to update the UTC
offset or the resubscription fails)? I'm worried it's too easy to get
into a state where phc2sys is not receiving updates and the user is
wondering why it's not working as expected.

Thanks,

-- 
Miroslav Lichvar

--
"Accelerate Dev Cycles with Automated Cross-Browser Testing - For FREE
Instantly run your Selenium tests across 300+ browser/OS combos.
Get unparalleled scalability from the best Selenium testing platform available
Simple to use. Nothing to install. Get started now for free."
http://p.sf.net/sfu/SauceLabs
___
Linuxptp-devel mailing list
Linuxptp-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/linuxptp-devel


Re: [Linuxptp-devel] [PATCH 11/14] phc2sys: autoconfiguration

2014-05-14 Thread Jiri Benc
On Mon, 12 May 2014 17:25:51 +0200, Miroslav Lichvar wrote:
> On Wed, May 07, 2014 at 06:23:15PM +0200, Jiri Benc wrote:
> > +   do_leap = update_pmc(node, 0);
> 
> Shouldn't the second parameter be "subscriptions > 0"?

Yes. Seems it got incorrect during rework of the "Subscription time
limit" patch and apparently I haven't tested the new version long
enough (unlike the original version).

I'll fix it, thanks for catching this.

> Also, are you planning to implement some check that would abort/reinit
> phc2sys when ptp4l is killed (update_pmc fails to update the UTC
> offset or the resubscription fails)? I'm worried it's too easy to get
> into a state where phc2sys is not receiving updates and the user is
> wondering why it's not working as expected.

Yes. I have it on a todo list but as the problem exists with the
current code, too (albeit it's much less visible) I don't consider it
to be part of this set.

 Jiri

-- 
Jiri Benc

--
"Accelerate Dev Cycles with Automated Cross-Browser Testing - For FREE
Instantly run your Selenium tests across 300+ browser/OS combos.
Get unparalleled scalability from the best Selenium testing platform available
Simple to use. Nothing to install. Get started now for free."
http://p.sf.net/sfu/SauceLabs
___
Linuxptp-devel mailing list
Linuxptp-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/linuxptp-devel