From 830659fc5c9e467cc72ec79f3d2190e3a1edd254 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc-Andr=C3=A9=20Moreau?= Date: Mon, 26 May 2014 12:30:58 -0400 Subject: [PATCH] libfreerdp-core: add new protocol metrics module --- include/freerdp/freerdp.h | 5 ++- include/freerdp/metrics.h | 40 +++++++++++++++++++++++ libfreerdp/core/CMakeLists.txt | 2 ++ libfreerdp/core/bulk.c | 46 +++++++++++--------------- libfreerdp/core/bulk.h | 2 -- libfreerdp/core/freerdp.c | 4 +++ libfreerdp/core/metrics.c | 59 ++++++++++++++++++++++++++++++++++ libfreerdp/core/peer.c | 5 +-- 8 files changed, 130 insertions(+), 33 deletions(-) create mode 100644 include/freerdp/metrics.h create mode 100644 libfreerdp/core/metrics.c diff --git a/include/freerdp/freerdp.h b/include/freerdp/freerdp.h index caddcc629..b306fd0a6 100644 --- a/include/freerdp/freerdp.h +++ b/include/freerdp/freerdp.h @@ -26,6 +26,7 @@ typedef struct rdp_rail rdpRail; typedef struct rdp_cache rdpCache; typedef struct rdp_channels rdpChannels; typedef struct rdp_graphics rdpGraphics; +typedef struct rdp_metrics rdpMetrics; typedef struct rdp_freerdp freerdp; typedef struct rdp_context rdpContext; @@ -39,6 +40,7 @@ typedef RDP_CLIENT_ENTRY_POINTS_V1 RDP_CLIENT_ENTRY_POINTS; #include #include #include +#include #include #include @@ -117,7 +119,8 @@ struct rdp_context ALIGN64 rdpInput* input; /* 38 */ ALIGN64 rdpUpdate* update; /* 39 */ ALIGN64 rdpSettings* settings; /* 40 */ - UINT64 paddingC[64 - 41]; /* 41 */ + ALIGN64 rdpMetrics* metrics; /* 41 */ + UINT64 paddingC[64 - 42]; /* 42 */ UINT64 paddingD[96 - 64]; /* 64 */ UINT64 paddingE[128 - 96]; /* 96 */ diff --git a/include/freerdp/metrics.h b/include/freerdp/metrics.h new file mode 100644 index 000000000..5b453ceb1 --- /dev/null +++ b/include/freerdp/metrics.h @@ -0,0 +1,40 @@ +/** + * FreeRDP: A Remote Desktop Protocol Implementation + * Protocol Metrics + * + * Copyright 2014 Marc-Andre Moreau + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef FREERDP_METRICS_H +#define FREERDP_METRICS_H + +#include + +struct rdp_metrics +{ + rdpContext* context; + + UINT64 TotalCompressedBytes; + UINT64 TotalUncompressedBytes; + double TotalCompressionRatio; +}; + +FREERDP_API double metrics_write_bytes(rdpMetrics* metrics, UINT32 UncompressedBytes, UINT32 CompressedBytes); + +FREERDP_API rdpMetrics* metrics_new(rdpContext* context); +FREERDP_API void metrics_free(rdpMetrics* metrics); + +#endif /* FREERDP_METRICS_H */ + diff --git a/libfreerdp/core/CMakeLists.txt b/libfreerdp/core/CMakeLists.txt index cb202439c..33e311534 100644 --- a/libfreerdp/core/CMakeLists.txt +++ b/libfreerdp/core/CMakeLists.txt @@ -79,6 +79,8 @@ set(${MODULE_PREFIX}_SRCS client.h server.c server.h + metrics.c + metrics.h capabilities.c capabilities.h certificate.c diff --git a/libfreerdp/core/bulk.c b/libfreerdp/core/bulk.c index 5a2d45540..5a833ec88 100644 --- a/libfreerdp/core/bulk.c +++ b/libfreerdp/core/bulk.c @@ -23,7 +23,7 @@ #include "bulk.h" -#define WITH_BULK_DEBUG 1 +//#define WITH_BULK_DEBUG 1 const char* bulk_get_compression_flags_string(UINT32 flags) { @@ -117,8 +117,12 @@ int bulk_decompress(rdpBulk* bulk, BYTE* pSrcData, UINT32 SrcSize, BYTE** ppDstD { UINT32 type; int status = -1; + rdpMetrics* metrics; UINT32 CompressedBytes; UINT32 UncompressedBytes; + double CompressionRatio; + + metrics = bulk->context->metrics; bulk_compression_max_size(bulk); type = flags & BULK_COMPRESSION_TYPE_MASK; @@ -162,21 +166,15 @@ int bulk_decompress(rdpBulk* bulk, BYTE* pSrcData, UINT32 SrcSize, BYTE** ppDstD CompressedBytes = SrcSize; UncompressedBytes = *pDstSize; - bulk->TotalUncompressedBytes += UncompressedBytes; - bulk->TotalCompressedBytes += CompressedBytes; + CompressionRatio = metrics_write_bytes(metrics, UncompressedBytes, CompressedBytes); #ifdef WITH_BULK_DEBUG { - double CompressionRatio; - double TotalCompressionRatio; - - CompressionRatio = ((double) CompressedBytes) / ((double) UncompressedBytes); - TotalCompressionRatio = ((double) bulk->TotalCompressedBytes) / ((double) bulk->TotalUncompressedBytes); - - printf("Decompress Type: %d Flags: %s (0x%04X) Compression Ratio: %f (%d / %d), Total: %f (%d / %d)\n", + printf("Decompress Type: %d Flags: %s (0x%04X) Compression Ratio: %f (%d / %d), Total: %f (%u / %u)\n", type, bulk_get_compression_flags_string(flags), flags, CompressionRatio, CompressedBytes, UncompressedBytes, - TotalCompressionRatio, bulk->TotalCompressedBytes, bulk->TotalUncompressedBytes); + metrics->TotalCompressionRatio, (UINT32) metrics->TotalCompressedBytes, + (UINT32) metrics->TotalUncompressedBytes); } #endif } @@ -191,8 +189,12 @@ int bulk_decompress(rdpBulk* bulk, BYTE* pSrcData, UINT32 SrcSize, BYTE** ppDstD int bulk_compress(rdpBulk* bulk, BYTE* pSrcData, UINT32 SrcSize, BYTE** ppDstData, UINT32* pDstSize, UINT32* pFlags) { int status = -1; + rdpMetrics* metrics; UINT32 CompressedBytes; UINT32 UncompressedBytes; + double CompressionRatio; + + metrics = bulk->context->metrics; if ((SrcSize <= 50) || (SrcSize >= 16384)) { @@ -231,24 +233,15 @@ int bulk_compress(rdpBulk* bulk, BYTE* pSrcData, UINT32 SrcSize, BYTE** ppDstDat CompressedBytes = *pDstSize; UncompressedBytes = SrcSize; - bulk->TotalUncompressedBytes += UncompressedBytes; - bulk->TotalCompressedBytes += CompressedBytes; + CompressionRatio = metrics_write_bytes(metrics, UncompressedBytes, CompressedBytes); #ifdef WITH_BULK_DEBUG { - UINT32 type; - double CompressionRatio; - double TotalCompressionRatio; - - type = bulk->CompressionLevel; - - CompressionRatio = ((double) CompressedBytes) / ((double) UncompressedBytes); - TotalCompressionRatio = ((double) bulk->TotalCompressedBytes) / ((double) bulk->TotalUncompressedBytes); - - printf("Compress Type: %d Flags: %s (0x%04X) Compression Ratio: %f (%d / %d), Total: %f (%d / %d)\n", - type, bulk_get_compression_flags_string(*pFlags), *pFlags, + printf("Compress Type: %d Flags: %s (0x%04X) Compression Ratio: %f (%d / %d), Total: %f (%u / %u)\n", + bulk->CompressionLevel, bulk_get_compression_flags_string(*pFlags), *pFlags, CompressionRatio, CompressedBytes, UncompressedBytes, - TotalCompressionRatio, bulk->TotalCompressedBytes, bulk->TotalUncompressedBytes); + metrics->TotalCompressionRatio, (UINT32) metrics->TotalCompressedBytes, + (UINT32) metrics->TotalUncompressedBytes); } #endif } @@ -293,9 +286,6 @@ rdpBulk* bulk_new(rdpContext* context) bulk->xcrushSend = xcrush_context_new(TRUE); bulk->CompressionLevel = context->settings->CompressionLevel; - - bulk->TotalCompressedBytes = 0; - bulk->TotalUncompressedBytes = 0; } return bulk; diff --git a/libfreerdp/core/bulk.h b/libfreerdp/core/bulk.h index 72916edb2..d05b61aa1 100644 --- a/libfreerdp/core/bulk.h +++ b/libfreerdp/core/bulk.h @@ -40,8 +40,6 @@ struct rdp_bulk XCRUSH_CONTEXT* xcrushRecv; XCRUSH_CONTEXT* xcrushSend; BYTE OutputBuffer[65536]; - UINT64 TotalCompressedBytes; - UINT64 TotalUncompressedBytes; }; #define BULK_COMPRESSION_FLAGS_MASK 0xE0 diff --git a/libfreerdp/core/freerdp.c b/libfreerdp/core/freerdp.c index e8104a866..94deaae30 100644 --- a/libfreerdp/core/freerdp.c +++ b/libfreerdp/core/freerdp.c @@ -405,6 +405,8 @@ int freerdp_context_new(freerdp* instance) context->pubSub = PubSub_New(TRUE); PubSub_AddEventTypes(context->pubSub, FreeRDP_Events, sizeof(FreeRDP_Events) / sizeof(wEventType)); + context->metrics = metrics_new(context); + rdp = rdp_new(context); instance->input = rdp->input; instance->update = rdp->update; @@ -458,6 +460,8 @@ void freerdp_context_free(freerdp* instance) PubSub_Free(instance->context->pubSub); + metrics_free(instance->context->metrics); + free(instance->context); instance->context = NULL; } diff --git a/libfreerdp/core/metrics.c b/libfreerdp/core/metrics.c new file mode 100644 index 000000000..708776f36 --- /dev/null +++ b/libfreerdp/core/metrics.c @@ -0,0 +1,59 @@ +/** + * FreeRDP: A Remote Desktop Protocol Implementation + * Protocol Metrics + * + * Copyright 2014 Marc-Andre Moreau + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include "rdp.h" + +double metrics_write_bytes(rdpMetrics* metrics, UINT32 UncompressedBytes, UINT32 CompressedBytes) +{ + double CompressionRatio; + + metrics->TotalUncompressedBytes += UncompressedBytes; + metrics->TotalCompressedBytes += CompressedBytes; + + CompressionRatio = ((double) CompressedBytes) / ((double) UncompressedBytes); + metrics->TotalCompressionRatio = ((double) metrics->TotalCompressedBytes) / ((double) metrics->TotalUncompressedBytes); + + return CompressionRatio; +} + +rdpMetrics* metrics_new(rdpContext* context) +{ + rdpMetrics* metrics; + + metrics = (rdpMetrics*) calloc(1, sizeof(rdpMetrics)); + + if (metrics) + { + metrics->context = context; + } + + return metrics; +} + +void metrics_free(rdpMetrics* metrics) +{ + if (!metrics) + return; + + free(metrics); +} diff --git a/libfreerdp/core/peer.c b/libfreerdp/core/peer.c index bc7431f47..2dde3d1a5 100644 --- a/libfreerdp/core/peer.c +++ b/libfreerdp/core/peer.c @@ -436,6 +436,8 @@ void freerdp_peer_context_new(freerdp_peer* client) client->context->ServerMode = TRUE; + client->context->metrics = metrics_new(client->context); + rdp = rdp_new(client->context); client->input = rdp->input; @@ -474,8 +476,7 @@ freerdp_peer* freerdp_peer_new(int sockfd) { freerdp_peer* client; - client = (freerdp_peer*) malloc(sizeof(freerdp_peer)); - ZeroMemory(client, sizeof(freerdp_peer)); + client = (freerdp_peer*) calloc(1, sizeof(freerdp_peer)); freerdp_tcp_set_no_delay(sockfd, TRUE);