Skip to content

Commit

Permalink
clkmgr: Add chrony polling interval
Browse files Browse the repository at this point in the history
Signed-off-by: Lai Peter Jun Ann <[email protected]>
  • Loading branch information
Lai Peter Jun Ann authored and yoongsiang2 committed Jan 13, 2025
1 parent 49e0431 commit b88815a
Show file tree
Hide file tree
Showing 10 changed files with 67 additions and 26 deletions.
31 changes: 23 additions & 8 deletions clkmgr/TEST_clkmgr.md
Original file line number Diff line number Diff line change
Expand Up @@ -178,20 +178,26 @@ Options:
Default: -100000 ns
-i idle time (s)
Default: 1 s
-m chrony offset upper limit (ns)
Default: 100000 ns
-n chrony offset lower limit (ns)
Default: -100000 ns
-t timeout in waiting notification event (s)
Default: 10 s
```

Example output of c++ sample application (clkmgr_test):
```bash
~/libptpmgmt/clkmgr/sample# ./run_clkmgr_test.sh -l -10 -u 10
~/libptpmgmt/clkmgr/sample# ./run_clkmgr_test.sh -n 0 -m 5 -t 0
[clkmgr] Connected. Session ID : 0
[clkmgr] set subscribe event : 0xf
[clkmgr] set composite event : 0x7
GM Offset upper limit: 10 ns
GM Offset lower limit: -10 ns
GM Offset upper limit: 100000 ns
GM Offset lower limit: -100000 ns
Chrony Offset upper limit: 5 ns
Chrony Offset lower limit: 0 ns
[clkmgr][174.381] Obtained data from Subscription Event:
[clkmgr][360038.267] Obtained data from Subscription Event:
+---------------------------+------------------------+
| Event | Event Status |
+---------------------------+------------------------+
Expand All @@ -210,8 +216,16 @@ GM Offset lower limit: -10 ns
| - as_capable | |
+---------------------------+------------------------+
[clkmgr][175.381] Waiting for Notification Event...
[clkmgr][177.045] Obtained data from Notification Event:
+---------------------------+------------------------+
| chrony offset_in_range | 0 |
+---------------------------+------------------------+
| chrony clock_offset | 5 ns |
| chrony clock_reference_id | 50484330 |
| chrony polling interval | 500000 us |
+---------------------------+------------------------+
[clkmgr][360039.268] Waiting for Notification Event...
[clkmgr][360039.268] Obtained data from Notification Event:
+---------------------------+--------------+-------------+
| Event | Event Status | Event Count |
+---------------------------+--------------+-------------+
Expand All @@ -231,10 +245,11 @@ GM Offset lower limit: -10 ns
+---------------------------+--------------+-------------+
+---------------------------+----------------------------+
| chrony offset_in_range | 1 | 0 |
| chrony offset_in_range | 1 | 1 |
+---------------------------+----------------------------+
| chrony clock_offset | 4 ns |
| chrony clock_offset | 3 ns |
| chrony clock_reference_id | 50484330 |
| chrony polling_interval | 500000 us |
+---------------------------+----------------------------+
```

Expand Down
2 changes: 2 additions & 0 deletions clkmgr/client/clockmanager_c.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ bool clkmgr_c_subscribe(clkmgr_c_client_ptr client_ptr,
current_state->chrony_clock_offset = state.chrony_clock_offset;
current_state->chrony_reference_id = state.chrony_reference_id;
current_state->chrony_offset_in_range = state.chrony_offset_in_range;
current_state->polling_interval = state.polling_interval;
}
return ret;
}
Expand Down Expand Up @@ -95,6 +96,7 @@ int clkmgr_c_status_wait(clkmgr_c_client_ptr client_ptr, int timeout,
current_state->chrony_clock_offset = state.chrony_clock_offset;
current_state->chrony_reference_id = state.chrony_reference_id;
current_state->chrony_offset_in_range = state.chrony_offset_in_range;
current_state->polling_interval = state.polling_interval;
if(ret > 0) {
current_count->as_capable_event_count = eventCount.as_capable_event_count;
current_count->composite_event_count = eventCount.composite_event_count;
Expand Down
3 changes: 3 additions & 0 deletions clkmgr/client/notification_msg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,9 @@ PROCESS_MESSAGE_TYPE(ClientNotificationMessage::processMessage)
client_ptp_data->chrony_reference_id = proxy_data.chrony_reference_id;
clkmgrCurrentState.chrony_reference_id =
client_ptp_data->chrony_reference_id;
client_ptp_data->polling_interval = proxy_data.polling_interval;
clkmgrCurrentState.polling_interval =
client_ptp_data->polling_interval;
// Update Event_count
Event_count clkmgrCurrentEventCount =
currentClientState->get_eventStateCount();
Expand Down
3 changes: 2 additions & 1 deletion clkmgr/client/subscribe_msg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -148,12 +148,13 @@ PARSE_RXBUFFER_TYPE(ClientSubscribeMessage::parseBuffer)
if(currentClientState->get_eventSub().in_range(thresholdChronyOffset,
client_data->chrony_offset))
client_data->chrony_offset_in_range = true;
client_data->chrony_reference_id = data.chrony_reference_id;
clkmgrCurrentState->chrony_clock_offset = client_data->chrony_offset;
clkmgrCurrentState->chrony_offset_in_range =
client_data->chrony_offset_in_range;
client_data->chrony_reference_id = data.chrony_reference_id;
clkmgrCurrentState->chrony_reference_id = client_data->chrony_reference_id;
client_data->polling_interval = data.polling_interval;
clkmgrCurrentState->polling_interval = client_data->polling_interval;
return true;
}

Expand Down
4 changes: 3 additions & 1 deletion clkmgr/common/ptp_event.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ struct ptp_event {
bool synced_to_primary_clock;
uint8_t ptp4l_id;
int64_t chrony_offset;
uint64_t chrony_reference_id;
uint32_t chrony_reference_id;
int64_t polling_interval;
};

struct client_ptp_event {
Expand All @@ -46,6 +47,7 @@ struct client_ptp_event {
std::atomic<int> composite_event_count{};
int64_t chrony_offset;
uint32_t chrony_reference_id;
int64_t polling_interval;
bool chrony_offset_in_range;
std::atomic<int> chrony_offset_in_range_event_count{};
};
Expand Down
3 changes: 2 additions & 1 deletion clkmgr/common/types.m4
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ struct Nm(Event_state) {
bool composite_event; /**< Composite event */
int64_t chrony_clock_offset; /**< Chrony clock offset */
bool chrony_offset_in_range; /**< Chrony_clock offset in range */
uint64_t chrony_reference_id; /**< Chrony reference ID */
uint32_t chrony_reference_id; /**< Chrony reference ID */
uint32_t polling_interval; /**< Chrony polling interval */
};

/**
Expand Down
23 changes: 14 additions & 9 deletions clkmgr/proxy/connect_chrony.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include <sys/epoll.h>
#include <cstring>
#include <unistd.h>
#include <cmath>

__CLKMGR_NAMESPACE_USE;

Expand All @@ -32,9 +33,13 @@ using namespace std;
chrony_session *s;
int fd;
int report_index = 0;
int polling_interval;
extern ptp_event pe;

struct ThreadArgs {
chrony_session *s;
int report_index;
};

void chrony_notify_client()
{
PrintDebug("[clkmgr]::notify_client");
Expand Down Expand Up @@ -106,18 +111,18 @@ static int subscribe_to_chronyd(chrony_session *s, int report_index)
}
if(field_name != nullptr && strcmp(field_name, "Reference ID") == 0)
pe.chrony_reference_id = chrony_get_field_uinteger(s, j);
if(field_name != nullptr && strcmp(field_name, "Poll") == 0)
polling_interval = chrony_get_field_integer(s, j);
if(field_name != nullptr && strcmp(field_name, "Poll") == 0) {
int32_t interval = static_cast<int32_t>
(static_cast<int16_t>(chrony_get_field_integer(s, j)));
pe.polling_interval = std::pow(2.0, interval) * 1000000;
//printf("CHRONY polling_interval = %d us\n",
// pe.polling_interval);}
}
}
chrony_notify_client();
return CHRONY_OK;
}

struct ThreadArgs {
chrony_session *s;
int report_index;
};

void *monitor_chronyd(void *arg)
{
ThreadArgs *args = (ThreadArgs *)arg;
Expand All @@ -128,7 +133,7 @@ void *monitor_chronyd(void *arg)
subscribe_to_chronyd(s, i);
}
// Sleep duration is based on chronyd polling interval
usleep(polling_interval);
usleep(pe.polling_interval);
}
}

Expand Down
2 changes: 1 addition & 1 deletion clkmgr/proxy/connect_chrony.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ __CLKMGR_NAMESPACE_BEGIN

class ConnectChrony
{
public:
public:
static void connect_chrony();
};

Expand Down
8 changes: 6 additions & 2 deletions clkmgr/sample/clkmgr_c_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -226,8 +226,10 @@ int main(int argc, char *argv[])
printf("+---------------------------+------------------------+\n");
printf("| %-25s | %-19ld ns |\n",
"chrony clock_offset", event_state.chrony_clock_offset);
printf("| %-25s | %-19lX |\n",
printf("| %-25s | %-19X |\n",
"chrony clock_reference_id", event_state.chrony_reference_id);
printf("| %-25s | %-19d us |\n",
"chrony polling interval", event_state.polling_interval);
printf("+---------------------------+------------------------+\n\n");

sleep(1);
Expand Down Expand Up @@ -309,8 +311,10 @@ int main(int argc, char *argv[])
printf("+---------------------------+----------------------------+\n");
printf("| %-25s | %-19ld ns |\n",
"chrony clock_offset", event_state.chrony_clock_offset);
printf("| %-25s | %-19lX |\n",
printf("| %-25s | %-19X |\n",
"chrony clock_reference_id", event_state.chrony_reference_id);
printf("| %-25s | %-19d us |\n",
"chrony polling interval", event_state.polling_interval);
printf("+---------------------------+----------------------------+\n\n");

printf("[clkmgr][%.3f] sleep for %d seconds...\n\n",
Expand Down
14 changes: 11 additions & 3 deletions clkmgr/sample/clkmgr_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,10 @@ int main(int argc, char *argv[])
" Default: " << gmOffsetLowerLimit << " ns\n"
" -i idle time (s)\n"
" Default: " << idleTime << " s\n"
" -m chrony offset upper limit (ns)\n"
" Default: " << std::dec << chronyGmOffsetUpperLimit << " ns\n"
" -n chrony offset lower limit (ns)\n"
" Default: " << chronyGmOffsetLowerLimit << " ns\n"
" -t timeout in waiting notification event (s)\n"
" Default: " << timeout << " s\n";
return EXIT_SUCCESS;
Expand Down Expand Up @@ -182,7 +186,7 @@ int main(int argc, char *argv[])
std::cout << "[clkmgr] set composite event : 0x"
<< std::hex << subscription.get_composite_event_mask() << "\n";
std::cout << "GM Offset upper limit: " << std::dec << gmOffsetUpperLimit << " ns\n";
std::cout << "GM Offset lower limit: " << std::dec << gmOffsetLowerLimit << " ns\n\n";
std::cout << "GM Offset lower limit: " << std::dec << gmOffsetLowerLimit << " ns\n";
std::cout << "Chrony Offset upper limit: " << std::dec << chronyGmOffsetUpperLimit << " ns\n";
std::cout << "Chrony Offset lower limit: " << std::dec << chronyGmOffsetLowerLimit << " ns\n\n";

Expand Down Expand Up @@ -247,8 +251,10 @@ int main(int argc, char *argv[])
printf("+---------------------------+------------------------+\n");
printf("| %-25s | %-19ld ns |\n",
"chrony clock_offset", eventState.chrony_clock_offset);
printf("| %-25s | %-19lX |\n",
printf("| %-25s | %-19X |\n",
"chrony clock_reference_id", eventState.chrony_reference_id);
printf("| %-25s | %-19d us |\n",
"chrony polling interval", eventState.polling_interval);
printf("+---------------------------+------------------------+\n\n");

sleep(1);
Expand Down Expand Up @@ -330,8 +336,10 @@ int main(int argc, char *argv[])
printf("+---------------------------+----------------------------+\n");
printf("| %-25s | %-19ld ns |\n",
"chrony clock_offset", eventState.chrony_clock_offset);
printf("| %-25s | %-19lX |\n",
printf("| %-25s | %-19X |\n",
"chrony clock_reference_id", eventState.chrony_reference_id);
printf("| %-25s | %-19d us |\n",
"chrony polling_interval", eventState.polling_interval);
printf("+---------------------------+----------------------------+\n\n");

printf("[clkmgr][%.3f] sleep for %d seconds...\n\n",
Expand Down

0 comments on commit b88815a

Please sign in to comment.