зеркало из https://github.com/mozilla/gecko-dev.git
Bug 1675491 - Count number of bytes send and received over a socket. r=necko-reviewers,valentin
This needs to count encrypted data, therefore it is implemented as a NSPR layer right above the PR_NSPR_IO_LAYER layer. Differential Revision: https://phabricator.services.mozilla.com/D96083
This commit is contained in:
Родитель
0210714e2a
Коммит
a15f3e2f6d
|
@ -0,0 +1,143 @@
|
|||
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
||||
/* vim:set ts=2 sw=2 sts=2 et cindent: */
|
||||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
#include "NetworkDataCountLayer.h"
|
||||
#include "nsSocketTransportService2.h"
|
||||
#include "prmem.h"
|
||||
#include "prio.h"
|
||||
|
||||
namespace mozilla {
|
||||
namespace net {
|
||||
|
||||
static PRDescIdentity sNetworkDataCountLayerIdentity;
|
||||
static PRIOMethods sNetworkDataCountLayerMethods;
|
||||
static PRIOMethods* sNetworkDataCountLayerMethodsPtr = nullptr;
|
||||
|
||||
class NetworkDataCountSecret {
|
||||
public:
|
||||
NetworkDataCountSecret() {}
|
||||
|
||||
uint64_t mSentBytes = 0;
|
||||
uint64_t mReceivedBytes = 0;
|
||||
};
|
||||
|
||||
static PRInt32 NetworkDataCountSend(PRFileDesc* fd, const void* buf,
|
||||
PRInt32 amount, PRIntn flags,
|
||||
PRIntervalTime timeout) {
|
||||
MOZ_RELEASE_ASSERT(fd->identity == sNetworkDataCountLayerIdentity);
|
||||
MOZ_ASSERT(OnSocketThread(), "not on socket thread");
|
||||
|
||||
NetworkDataCountSecret* secret =
|
||||
reinterpret_cast<NetworkDataCountSecret*>(fd->secret);
|
||||
|
||||
PRInt32 rv =
|
||||
(fd->lower->methods->send)(fd->lower, buf, amount, flags, timeout);
|
||||
if (rv > 0) {
|
||||
secret->mSentBytes += rv;
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
|
||||
static PRInt32 NetworkDataCountWrite(PRFileDesc* fd, const void* buf,
|
||||
PRInt32 amount) {
|
||||
return NetworkDataCountSend(fd, buf, amount, 0, PR_INTERVAL_NO_WAIT);
|
||||
}
|
||||
|
||||
static PRInt32 NetworkDataCountRecv(PRFileDesc* fd, void* buf, PRInt32 amount,
|
||||
PRIntn flags, PRIntervalTime timeout) {
|
||||
MOZ_RELEASE_ASSERT(fd->identity == sNetworkDataCountLayerIdentity);
|
||||
|
||||
NetworkDataCountSecret* secret =
|
||||
reinterpret_cast<NetworkDataCountSecret*>(fd->secret);
|
||||
|
||||
PRInt32 rv =
|
||||
(fd->lower->methods->recv)(fd->lower, buf, amount, flags, timeout);
|
||||
if (rv > 0) {
|
||||
secret->mReceivedBytes += rv;
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
|
||||
static PRInt32 NetworkDataCountRead(PRFileDesc* fd, void* buf, PRInt32 amount) {
|
||||
return NetworkDataCountRecv(fd, buf, amount, 0, PR_INTERVAL_NO_WAIT);
|
||||
}
|
||||
|
||||
static PRStatus NetworkDataCountClose(PRFileDesc* fd) {
|
||||
if (!fd) {
|
||||
return PR_FAILURE;
|
||||
}
|
||||
|
||||
PRFileDesc* layer = PR_PopIOLayer(fd, PR_TOP_IO_LAYER);
|
||||
|
||||
MOZ_RELEASE_ASSERT(layer && layer->identity == sNetworkDataCountLayerIdentity,
|
||||
"NetworkDataCount Layer not on top of stack");
|
||||
|
||||
NetworkDataCountSecret* secret =
|
||||
reinterpret_cast<NetworkDataCountSecret*>(layer->secret);
|
||||
layer->secret = nullptr;
|
||||
layer->dtor(layer);
|
||||
delete secret;
|
||||
return fd->methods->close(fd);
|
||||
}
|
||||
|
||||
nsresult AttachNetworkDataCountLayer(PRFileDesc* fd) {
|
||||
MOZ_ASSERT(OnSocketThread(), "not on socket thread");
|
||||
|
||||
if (!sNetworkDataCountLayerMethodsPtr) {
|
||||
sNetworkDataCountLayerIdentity =
|
||||
PR_GetUniqueIdentity("NetworkDataCount Layer");
|
||||
sNetworkDataCountLayerMethods = *PR_GetDefaultIOMethods();
|
||||
sNetworkDataCountLayerMethods.send = NetworkDataCountSend;
|
||||
sNetworkDataCountLayerMethods.write = NetworkDataCountWrite;
|
||||
sNetworkDataCountLayerMethods.recv = NetworkDataCountRecv;
|
||||
sNetworkDataCountLayerMethods.read = NetworkDataCountRead;
|
||||
sNetworkDataCountLayerMethods.close = NetworkDataCountClose;
|
||||
sNetworkDataCountLayerMethodsPtr = &sNetworkDataCountLayerMethods;
|
||||
}
|
||||
|
||||
PRFileDesc* layer = PR_CreateIOLayerStub(sNetworkDataCountLayerIdentity,
|
||||
sNetworkDataCountLayerMethodsPtr);
|
||||
|
||||
if (!layer) {
|
||||
return NS_ERROR_FAILURE;
|
||||
}
|
||||
|
||||
NetworkDataCountSecret* secret = new NetworkDataCountSecret();
|
||||
|
||||
layer->secret = reinterpret_cast<PRFilePrivate*>(secret);
|
||||
|
||||
PRStatus status = PR_PushIOLayer(fd, PR_NSPR_IO_LAYER, layer);
|
||||
|
||||
if (status == PR_FAILURE) {
|
||||
delete secret;
|
||||
PR_Free(layer); // PR_CreateIOLayerStub() uses PR_Malloc().
|
||||
return NS_ERROR_FAILURE;
|
||||
}
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
void NetworkDataCountSent(PRFileDesc* fd, uint64_t& sentBytes) {
|
||||
PRFileDesc* ndcFd = PR_GetIdentitiesLayer(fd, sNetworkDataCountLayerIdentity);
|
||||
MOZ_RELEASE_ASSERT(ndcFd);
|
||||
MOZ_ASSERT(OnSocketThread(), "not on socket thread");
|
||||
|
||||
NetworkDataCountSecret* secret =
|
||||
reinterpret_cast<NetworkDataCountSecret*>(ndcFd->secret);
|
||||
sentBytes = secret->mSentBytes;
|
||||
}
|
||||
|
||||
void NetworkDataCountReceived(PRFileDesc* fd, uint64_t& receivedBytes) {
|
||||
PRFileDesc* ndcFd = PR_GetIdentitiesLayer(fd, sNetworkDataCountLayerIdentity);
|
||||
MOZ_RELEASE_ASSERT(ndcFd);
|
||||
MOZ_ASSERT(OnSocketThread(), "not on socket thread");
|
||||
|
||||
NetworkDataCountSecret* secret =
|
||||
reinterpret_cast<NetworkDataCountSecret*>(ndcFd->secret);
|
||||
receivedBytes = secret->mReceivedBytes;
|
||||
}
|
||||
|
||||
} // namespace net
|
||||
} // namespace mozilla
|
|
@ -0,0 +1,26 @@
|
|||
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
||||
/* vim:set ts=2 sw=2 sts=2 et cindent: */
|
||||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
#ifndef NetworkDataCountLayer_h__
|
||||
#define NetworkDataCountLayer_h__
|
||||
|
||||
#include "prerror.h"
|
||||
|
||||
namespace mozilla {
|
||||
namespace net {
|
||||
|
||||
nsresult AttachNetworkDataCountLayer(PRFileDesc* fd);
|
||||
|
||||
// Get amount of sent bytes.
|
||||
void NetworkDataCountSent(PRFileDesc* fd, uint64_t& sentBytes);
|
||||
|
||||
// Get amount of received bytes.
|
||||
void NetworkDataCountReceived(PRFileDesc* fd, uint64_t& receivedBytes);
|
||||
|
||||
} // namespace net
|
||||
} // namespace mozilla
|
||||
|
||||
#endif // NetworkDataCountLayer_h__
|
|
@ -188,6 +188,7 @@ UNIFIED_SOURCES += [
|
|||
"LoadInfo.cpp",
|
||||
"MemoryDownloader.cpp",
|
||||
"NetworkConnectivityService.cpp",
|
||||
"NetworkDataCountLayer.cpp",
|
||||
"nsAsyncRedirectVerifyHelper.cpp",
|
||||
"nsAsyncStreamCopier.cpp",
|
||||
"nsAuthInformationHolder.cpp",
|
||||
|
|
|
@ -34,6 +34,7 @@
|
|||
#include "nsIDNSRecord.h"
|
||||
#include "nsIDNSByTypeRecord.h"
|
||||
#include "nsICancelable.h"
|
||||
#include "NetworkDataCountLayer.h"
|
||||
#include "QuicSocketControl.h"
|
||||
#include "TCPFastOpenLayer.h"
|
||||
#include <algorithm>
|
||||
|
@ -1612,6 +1613,15 @@ nsresult nsSocketTransport::InitiateSocket() {
|
|||
}
|
||||
}
|
||||
|
||||
if (Telemetry::CanRecordPrereleaseData()) {
|
||||
if (NS_FAILED(AttachNetworkDataCountLayer(fd))) {
|
||||
SOCKET_LOG(
|
||||
("nsSocketTransport::InitiateSocket "
|
||||
"AttachNetworkDataCountLayer failed [this=%p]\n",
|
||||
this));
|
||||
}
|
||||
}
|
||||
|
||||
bool connectCalled = true; // This is only needed for telemetry.
|
||||
status = PR_Connect(fd, &prAddr, NS_SOCKET_CONNECT_TIMEOUT);
|
||||
PRErrorCode code = PR_GetError();
|
||||
|
|
Загрузка…
Ссылка в новой задаче