From 26a4b54d27c70f3cbbe86cf6b210ee1492b20bba Mon Sep 17 00:00:00 2001 From: Divya Sampath Kumar Date: Wed, 11 Oct 2023 16:37:35 -0700 Subject: [PATCH] Profile STUN DNS resolution time --- .../kinesis/video/webrtcclient/Stats.h | 1 + src/source/PeerConnection/PeerConnection.c | 19 +++++++++++++++++-- src/source/PeerConnection/PeerConnection.h | 1 + 3 files changed, 19 insertions(+), 2 deletions(-) diff --git a/src/include/com/amazonaws/kinesis/video/webrtcclient/Stats.h b/src/include/com/amazonaws/kinesis/video/webrtcclient/Stats.h index 39ac7cc438..0b04721ee0 100644 --- a/src/include/com/amazonaws/kinesis/video/webrtcclient/Stats.h +++ b/src/include/com/amazonaws/kinesis/video/webrtcclient/Stats.h @@ -612,6 +612,7 @@ typedef struct { UINT64 iceHolePunchingTime; //!< Time taken (ms) for ICE agent set up to complete UINT64 closePeerConnectionTime; //!< Time taken (ms) to close the peer connection UINT64 freePeerConnectionTime; //!< Time taken (ms) to free the peer connection object + UINT64 stunDnsResolutionTime; //!< Time taken (ms) to complete STUN DNS resolution on the thread } PeerConnectionStats, *PPeerConnectionStats; /** diff --git a/src/source/PeerConnection/PeerConnection.c b/src/source/PeerConnection/PeerConnection.c index 67b8316eb5..55f4412e24 100644 --- a/src/source/PeerConnection/PeerConnection.c +++ b/src/source/PeerConnection/PeerConnection.c @@ -26,7 +26,7 @@ STATUS createWebRtcClientInstance() CHK_WARN(!ATOMIC_LOAD_BOOL(&pWebRtcClientContext->isContextInitialized), retStatus, "WebRtc client context already initialized, nothing to do"); CHK_ERR(!IS_VALID_MUTEX_VALUE(pWebRtcClientContext->stunCtxlock), retStatus, "Mutex seems to have been created already"); - pWebRtcClientContext->stunCtxlock = MUTEX_CREATE(FALSE); + pWebRtcClientContext->stunCtxlock = MUTEX_CREATE(TRUE); CHK_ERR(IS_VALID_MUTEX_VALUE(pWebRtcClientContext->stunCtxlock), STATUS_NULL_ARG, "Mutex creation failed"); MUTEX_LOCK(pWebRtcClientContext->stunCtxlock); locked = TRUE; @@ -818,11 +818,11 @@ PVOID resolveStunIceServerIp(PVOID args) CHAR addressResolved[KVS_IP_ADDRESS_STRING_BUFFER_LEN + 1] = {'\0'}; PCHAR pRegion; PCHAR pHostnamePostfix; + UINT64 stunDnsResolutionStartTime = 0; if (ATOMIC_LOAD_BOOL(&pWebRtcClientContext->isContextInitialized)) { MUTEX_LOCK(pWebRtcClientContext->stunCtxlock); locked = TRUE; - if (pWebRtcClientContext->pStunIpAddrCtx == NULL) { DLOGE("Failed to resolve STUN IP address because webrtc client instance was not created"); } else { @@ -838,6 +838,7 @@ PVOID resolveStunIceServerIp(PVOID args) SNPRINTF(pWebRtcClientContext->pStunIpAddrCtx->hostname, SIZEOF(pWebRtcClientContext->pStunIpAddrCtx->hostname), KINESIS_VIDEO_STUN_URL_WITHOUT_PORT, pRegion, pHostnamePostfix); + stunDnsResolutionStartTime = GETTIME(); if (getStunAddr(pWebRtcClientContext->pStunIpAddrCtx) == STATUS_SUCCESS) { getIpAddrStr(&pWebRtcClientContext->pStunIpAddrCtx->kvsIpAddr, addressResolved, ARRAY_SIZE(addressResolved)); DLOGI("ICE Server address for %s with getaddrinfo: %s", pWebRtcClientContext->pStunIpAddrCtx->hostname, addressResolved); @@ -846,6 +847,8 @@ PVOID resolveStunIceServerIp(PVOID args) DLOGE("Failed to resolve %s", pWebRtcClientContext->pStunIpAddrCtx->hostname); } pWebRtcClientContext->pStunIpAddrCtx->startTime = GETTIME(); + PROFILE_WITH_START_TIME_OBJ(stunDnsResolutionStartTime, pWebRtcClientContext->pStunIpAddrCtx->stunDnsResolutionTime, + "STUN DNS resolution time taken"); } if (locked) { MUTEX_UNLOCK(pWebRtcClientContext->stunCtxlock); @@ -1742,11 +1745,22 @@ STATUS peerConnectionGetMetrics(PRtcPeerConnection pPeerConnection, PPeerConnect { STATUS retStatus = STATUS_SUCCESS; PKvsPeerConnection pKvsPeerConnection = (PKvsPeerConnection) pPeerConnection; + PWebRtcClientContext pWebRtcClientContext = getWebRtcClientInstance(); + CHK(pKvsPeerConnection != NULL && pPeerConnectionMetrics != NULL, STATUS_NULL_ARG); if (pPeerConnectionMetrics->version > PEER_CONNECTION_METRICS_CURRENT_VERSION) { DLOGW("Peer connection metrics object version invalid..setting to highest default version %d", PEER_CONNECTION_METRICS_CURRENT_VERSION); pPeerConnectionMetrics->version = PEER_CONNECTION_METRICS_CURRENT_VERSION; } + + MUTEX_LOCK(pWebRtcClientContext->stunCtxlock); + if (pWebRtcClientContext->isContextInitialized) { + if (pWebRtcClientContext->pStunIpAddrCtx->isIpInitialized) { + pPeerConnectionMetrics->peerConnectionStats.stunDnsResolutionTime = pWebRtcClientContext->pStunIpAddrCtx->stunDnsResolutionTime; + } + } + MUTEX_UNLOCK(pWebRtcClientContext->stunCtxlock); + pPeerConnectionMetrics->peerConnectionStats.peerConnectionCreationTime = pKvsPeerConnection->peerConnectionDiagnostics.peerConnectionCreationTime; pPeerConnectionMetrics->peerConnectionStats.dtlsSessionSetupTime = pKvsPeerConnection->peerConnectionDiagnostics.dtlsSessionSetupTime; pPeerConnectionMetrics->peerConnectionStats.iceHolePunchingTime = pKvsPeerConnection->peerConnectionDiagnostics.iceHolePunchingTime; @@ -1754,6 +1768,7 @@ STATUS peerConnectionGetMetrics(PRtcPeerConnection pPeerConnection, PPeerConnect pPeerConnectionMetrics->peerConnectionStats.closePeerConnectionTime = pKvsPeerConnection->peerConnectionDiagnostics.closePeerConnectionTime; pPeerConnectionMetrics->peerConnectionStats.freePeerConnectionTime = pKvsPeerConnection->peerConnectionDiagnostics.freePeerConnectionTime; CleanUp: + releaseHoldOnInstance(pWebRtcClientContext); return retStatus; } diff --git a/src/source/PeerConnection/PeerConnection.h b/src/source/PeerConnection/PeerConnection.h index e638767055..3a47d3eebd 100644 --- a/src/source/PeerConnection/PeerConnection.h +++ b/src/source/PeerConnection/PeerConnection.h @@ -157,6 +157,7 @@ typedef struct { KvsIpAddress kvsIpAddr; BOOL isIpInitialized; UINT64 startTime; + UINT64 stunDnsResolutionTime; UINT64 expirationDuration; STATUS status; } StunIpAddrContext, *PStunIpAddrContext;