Skip to content

Commit

Permalink
clkmgr: Add chrony support
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 committed Dec 6, 2024
1 parent 80c8591 commit 4706eff
Show file tree
Hide file tree
Showing 10 changed files with 194 additions and 9 deletions.
2 changes: 1 addition & 1 deletion clkmgr/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ CLKMGR_PROXY:=$(CLKMGR_PROXY_DIR)/clkmgr_proxy
CLKMGR_GEN_CPP=$(Q_GEN)$(M4) -I $(CLKMGR_COMMON_DIR) -D lang=cpp $< > $@
CLKMGR_GEN_C=$(Q_GEN)$(M4) -I $(CLKMGR_COMMON_DIR) -D lang=c $< > $@

CLKMGR_LDLIBS:=$(foreach lib,pthread rt rtpi,-l$(lib))
CLKMGR_LDLIBS:=$(foreach lib,pthread rt rtpi chrony,-l$(lib))
CLKMGR_CXXFLAGS:=-I$(CLKMGR_DIR) -I$(CLKMGR_PUB_DIR)

CLKMGR_COMMON_SRCS:=$(wildcard $(CLKMGR_COMMON_DIR)/*.cpp)
Expand Down
3 changes: 2 additions & 1 deletion clkmgr/client/clockmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,8 @@ int ClockManager::clkmgr_status_wait(int timeout,
eventCount.as_capable_event_count ||
eventCount.synced_to_gm_event_count ||
eventCount.composite_event_count ||
eventCount.gm_changed_event_count) {
eventCount.gm_changed_event_count ||
eventCount.chrony_offset_in_range_event_count) {
event_changes_detected = true;
break;
}
Expand Down
27 changes: 27 additions & 0 deletions clkmgr/client/notification_msg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,33 @@ PROCESS_MESSAGE_TYPE(ClientNotificationMessage::processMessage)
client_ptp_data->gm_changed_event_count;
clkmgrCurrentEventCount.composite_event_count =
client_ptp_data->composite_event_count;
printf("notification: proxy chronyd offset = %ld\n",proxy_data.chrony_offset );
if(proxy_data.chrony_offset != client_ptp_data->chrony_offset) {
client_ptp_data->chrony_offset = proxy_data.chrony_offset;
if(currentClientState->get_eventSub().in_range(thresholdGMOffset,
client_ptp_data->chrony_offset)) {
if(!(client_ptp_data->chrony_offset_in_range)) {
client_ptp_data->chrony_offset_in_range = true;
client_ptp_data->chrony_offset_in_range_event_count.fetch_add(1,
std::memory_order_relaxed);
}
} else {
if((client_ptp_data->chrony_offset_in_range)) {
client_ptp_data->chrony_offset_in_range = false;
client_ptp_data->chrony_offset_in_range_event_count.fetch_add(1,
std::memory_order_relaxed);
}
}
}
//client_ptp_data->chrony_offset = proxy_data.chrony_offset;
clkmgrCurrentState.chrony_clock_offset = client_ptp_data->chrony_offset;
clkmgrCurrentEventCount.chrony_offset_in_range_event_count =
client_ptp_data->chrony_offset_in_range_event_count;
clkmgrCurrentState.chrony_offset_in_range =
client_ptp_data->chrony_offset_in_range;
client_ptp_data->chrony_reference_id = proxy_data.chrony_reference_id;
clkmgrCurrentState.chrony_reference_id = client_ptp_data->chrony_reference_id;
printf("notification: client chronyd offset = %ld\n",clkmgrCurrentState.chrony_clock_offset);
}
return true;
}
Expand Down
6 changes: 6 additions & 0 deletions clkmgr/client/subscribe_msg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,12 @@ PARSE_RXBUFFER_TYPE(ClientSubscribeMessage::parseBuffer)
clkmgrCurrentState->notification_timestamp += last_notification_time.tv_nsec;
memcpy(clkmgrCurrentState->gm_identity, client_data->gm_identity,
sizeof(client_data->gm_identity));
client_data->chrony_offset = data.chrony_offset;
client_data->chrony_reference_id = data.chrony_reference_id;
//printf("client_data->chrony_offset =%ld, data.chrony_offset = %ld \n",client_data->chrony_offset,data.chrony_offset);
clkmgrCurrentState->chrony_clock_offset = client_data->chrony_offset;
clkmgrCurrentState->chrony_offset_in_range =
client_data->chrony_offset_in_range;
return true;
}

Expand Down
6 changes: 6 additions & 0 deletions clkmgr/common/ptp_event.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ struct ptp_event {
bool as_capable; /* [email protected] Capable */
bool synced_to_primary_clock;
uint8_t ptp4l_id;
int64_t chrony_offset;
uint64_t chrony_reference_id;
};

struct client_ptp_event {
Expand All @@ -42,6 +44,10 @@ struct client_ptp_event {
std::atomic<int> synced_to_gm_event_count{};
std::atomic<int> gm_changed_event_count{};
std::atomic<int> composite_event_count{};
int64_t chrony_offset;
uint64_t chrony_reference_id;
bool chrony_offset_in_range;
std::atomic<int> chrony_offset_in_range_event_count{};
};

__CLKMGR_NAMESPACE_END
Expand Down
123 changes: 116 additions & 7 deletions clkmgr/proxy/connect_ptp4l.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include <string>
#include <sys/epoll.h>
#include <unistd.h>
#include <chrony.h>

__CLKMGR_NAMESPACE_USE

Expand All @@ -41,7 +42,11 @@ static SockBase *sk;
static std::unique_ptr<SockBase> m_sk;

SUBSCRIBE_EVENTS_NP_t d;
ptp_event pe = { 0, {0, 0, 0, 0, 0, 0, 0, 0}, 0, 0, 0};
ptp_event pe = { 0, {0, 0, 0, 0, 0, 0, 0, 0}, 0, 0, 0, 0};
chrony_session *s;
int fd;
int report_index = 0;
int polling_interval;

void notify_client()
{
Expand Down Expand Up @@ -78,12 +83,14 @@ void event_handle()
memcpy(pe.gm_identity, timeStatus->gmIdentity.v,
sizeof(pe.gm_identity));
/* Uncomment for debug data printing */
//printf("master_offset = %ld, synced_to_primary_clock = %d\n",
// pe.master_offset, pe.synced_to_primary_clock);
//printf("gm_identity = %02x%02x%02x.%02x%02x.%02x%02x%02x\n\n",
// pe.gm_identity[0], pe.gm_identity[1],pe.gm_identity[2],
// pe.gm_identity[3], pe.gm_identity[4],
// pe.gm_identity[5], pe.gm_identity[6],pe.gm_identity[7]);
/*
printf("master_offset = %ld, synced_to_primary_clock = %d\n",
pe.master_offset, pe.synced_to_primary_clock);
printf("gm_identity = %02x%02x%02x.%02x%02x.%02x%02x%02x\n\n",
pe.gm_identity[0], pe.gm_identity[1],pe.gm_identity[2],
pe.gm_identity[3], pe.gm_identity[4],
pe.gm_identity[5], pe.gm_identity[6],pe.gm_identity[7]);
*/
break;
}
case PORT_PROPERTIES_NP: /* Get initial port state when Proxy starts */
Expand Down Expand Up @@ -200,6 +207,99 @@ bool event_subscription(struct clkmgr_handle **handle)
return ret;
}

static chrony_err process_chronyd_data(chrony_session *s)
{
struct pollfd pfd = { .fd = chrony_get_fd(s), .events = POLLIN };
int n, timeout;
chrony_err r;
timeout = 1000;
while (chrony_needs_response(s)) {
n = poll(&pfd, 1, timeout);
if (n < 0) {
perror("poll");
// return -1;
} else if (n == 0) {
fprintf(stderr, "No valid response received\n");
// return -1;
}
r = chrony_process_response(s);
if (r != CHRONY_OK)
return r;
}
return CHRONY_OK;
}

static int subscribe_to_chronyd(chrony_session *s, int report_index)
{
chrony_field_content content;
const char *report_name;
chrony_err r;
int i, j;
report_name = chrony_get_report_name(report_index);
i = 0;
r = chrony_request_record(s, report_name, i);
if (r != CHRONY_OK)
return r;
r = process_chronyd_data(s);
if (r != CHRONY_OK)
return r;
for (j = 0; j < chrony_get_record_number_fields(s); j++) {
content = chrony_get_field_content(s, j);
if (content == CHRONY_CONTENT_NONE)
continue;
const char *field_name = chrony_get_field_name(s, j);
if (field_name != NULL && strcmp(field_name, "Last offset") == 0) {
float second = (chrony_get_field_float(s, j) * 1e9);
pe.chrony_offset = (int)second;
printf("offset (chrony): %ld nanosecond \n\n", pe.chrony_offset);
}
if (field_name != NULL && strcmp(field_name, "Reference ID") == 0) {
pe.chrony_reference_id = chrony_get_field_uinteger(s, j);
printf("Reference ID: %lX\n", pe.chrony_reference_id);
}
if (field_name != NULL && strcmp(field_name, "Poll") == 0) {
polling_interval = chrony_get_field_integer(s, j);
//printf("Poll: %d\n", pe.chrony_poll);
printf("Pollng interval: %ld s\n",polling_interval);
}
}
return CHRONY_OK;
}


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

void* monitor_chronyd(void* arg)
{
ThreadArgs* args = (ThreadArgs*)arg;
chrony_session *s = args->s;
int report_index = args->report_index;
while (true) {
if (chrony_init_session(&s, fd) == CHRONY_OK) {
for (int i = 0; i < 2; i++) {
subscribe_to_chronyd(s, i);
// chrony_deinit_session(s);
}
}
usleep(polling_interval); // Sleep duration is based on chronyd polling interval
}
return NULL;
}

void start_monitor_thread(chrony_session *s, int report_index)
{
pthread_t thread_id;
ThreadArgs* args = new ThreadArgs{s, report_index};
if (pthread_create(&thread_id, NULL, monitor_chronyd, args) != 0) {
fprintf(stderr, "Failed to create thread\n");
exit(EXIT_FAILURE);
}
}

/**
* @brief Runs the main event loop for handling PTP (Precision Time Protocol)
* events.
Expand Down Expand Up @@ -302,6 +402,15 @@ int Connect::connect(uint8_t transport_specific)
prms.transportSpecific = transport_specific;
msg.updateParams(prms);
sk = m_sk.get();

/* connect to chronyd unix socket*/
fd = chrony_open_socket("/var/run/chrony/chronyd.sock");
chrony_session *s;
if (chrony_init_session(&s, fd) == CHRONY_OK) {
start_monitor_thread(s, report_index);
chrony_deinit_session(s);
}

handle_connect();
return 0;
}
Expand Down
2 changes: 2 additions & 0 deletions clkmgr/pub/clkmgr/clockmanager_c.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ struct clkmgr_c_event_state {
bool gm_changed; /**< Primary clock UUID changed */
bool composite_event; /**< Composite event */
bool reserved[27]; /**< Reserved for future */
int64_t chrony_clock_offset; /**< Chrony clock offset */
uint64_t chrony_reference_id; /**< Chrony reference ID */
};

/**
Expand Down
4 changes: 4 additions & 0 deletions clkmgr/pub/clkmgr/event_state.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ struct clkmgr_event_state {
bool gm_changed; /**< Primary clock UUID changed */
bool composite_event; /**< Composite event */
bool reserved[27]; /**< Reserved for future */
int64_t chrony_clock_offset; /**< Chrony clock offset */
uint64_t chrony_reference_id; /**< Chrony reference ID */
bool chrony_offset_in_range; /**< Chrony clock offset in range */
};

/**
Expand All @@ -45,6 +48,7 @@ struct clkmgr_event_count {
uint32_t gm_changed_event_count; /**< Primary clock UUID changed */
uint32_t composite_event_count; /**< Composite event */
uint32_t reserved[27]; /**< Reserved for future */
uint32_t chrony_offset_in_range_event_count; /**< Chrony clock offset in range */
};

__CLKMGR_NAMESPACE_END
Expand Down
12 changes: 12 additions & 0 deletions clkmgr/sample/clkmgr_c_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,12 @@ int main(int argc, char *argv[])
} else {
printf("\n");
}
printf("+---------------------------+------------------------+\n");
printf("| %-25s | %-19ld ns |\n",
"chrony clock_offset", event_state.chrony_clock_offset);
printf("| %-25s | %-19lX |\n",
"chrony clock_reference_id", event_state.chrony_reference_id);
printf("+---------------------------+------------------------+\n\n");

sleep(1);

Expand Down Expand Up @@ -277,6 +283,12 @@ int main(int argc, char *argv[])
} else {
printf("\n");
}
printf("+---------------------------+----------------------------+\n");
printf("| %-25s | %-19ld ns |\n",
"chrony clock_offset", event_state.chrony_clock_offset);
printf("| %-25s | %-19lX |\n",
"chrony clock_reference_id", event_state.chrony_reference_id);
printf("+---------------------------+----------------------------+\n\n");

printf("[clkmgr][%.3f] sleep for %d seconds...\n\n",
getMonotonicTime(), idle_time);
Expand Down
18 changes: 18 additions & 0 deletions clkmgr/sample/clkmgr_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,15 @@ int main(int argc, char *argv[])
} else {
printf("\n");
}
printf("+---------------------------+------------------------+\n");
printf("| %-25s | %-22d |\n", "chrony offset_in_range",
eventState.chrony_offset_in_range);
printf("+---------------------------+----------------------------+\n");
printf("| %-25s | %-19ld ns |\n",
"chrony clock_offset", eventState.chrony_clock_offset);
printf("| %-25s | %-19lX |\n",
"chrony clock_reference_id", eventState.chrony_reference_id);
printf("+---------------------------+------------------------+\n\n");

sleep(1);

Expand Down Expand Up @@ -296,6 +305,15 @@ int main(int argc, char *argv[])
} else {
printf("\n");
}
printf("+---------------------------+----------------------------+\n");
printf("| %-25s | %-12d | %-11d |\n", "chrony offset_in_range",
eventState.chrony_offset_in_range, eventCount.chrony_offset_in_range_event_count);
printf("+---------------------------+----------------------------+\n");
printf("| %-25s | %-19ld ns |\n",
"chrony clock_offset", eventState.chrony_clock_offset);
printf("| %-25s | %-19lX |\n",
"chrony clock_reference_id", eventState.chrony_reference_id);
printf("+---------------------------+----------------------------+\n\n");

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

0 comments on commit 4706eff

Please sign in to comment.