From bdee4267583a7daaf9ea5284d2ff6d4bbb782229 Mon Sep 17 00:00:00 2001 From: Rob Hughes Date: Tue, 7 Jan 2020 17:05:24 +0000 Subject: Add thin abstraction layer for processes and filesystem This is used instead of some hardcoded Unix calls and means this code now works on Windows (This is a rework of a previous patch which used boost, now that I have been informed that we are trying to move towards removing boost). Change-Id: Ib0d11055279bbd7b710f086e9890369e3ecbfe9a Signed-off-by: Robert Hughes --- src/armnnUtils/Filesystem.cpp | 50 ++++++++++++++++++++++ src/armnnUtils/Filesystem.hpp | 18 ++++++++ src/armnnUtils/Processes.cpp | 30 +++++++++++++ src/armnnUtils/Processes.hpp | 16 +++++++ src/profiling/SendCounterPacket.cpp | 4 +- .../test/FileOnlyProfilingDecoratorTests.cpp | 7 ++- src/profiling/test/SendCounterPacketTests.cpp | 4 +- 7 files changed, 123 insertions(+), 6 deletions(-) create mode 100644 src/armnnUtils/Filesystem.cpp create mode 100644 src/armnnUtils/Filesystem.hpp create mode 100644 src/armnnUtils/Processes.cpp create mode 100644 src/armnnUtils/Processes.hpp (limited to 'src') diff --git a/src/armnnUtils/Filesystem.cpp b/src/armnnUtils/Filesystem.cpp new file mode 100644 index 0000000000..08c447b3f6 --- /dev/null +++ b/src/armnnUtils/Filesystem.cpp @@ -0,0 +1,50 @@ +// +// Copyright © 2020 Arm Ltd. All rights reserved. +// SPDX-License-Identifier: MIT +// + +#include "Filesystem.hpp" + +#if defined(__unix__) +#include +#include +#elif defined(_MSC_VER) +#define WIN32_LEAN_AND_MEAN +#include +#endif + +namespace armnnUtils +{ +namespace Filesystem +{ + +long GetFileSize(const char* path) +{ +#if defined(__unix__) + struct stat statusBuffer; + if (stat(path, & statusBuffer) != 0) + { + return -1; + } + return statusBuffer.st_size; +#elif defined(_MSC_VER) + WIN32_FILE_ATTRIBUTE_DATA attr; + if (::GetFileAttributesEx(path, GetFileExInfoStandard, &attr) == 0) + { + return -1; + } + return attr.nFileSizeLow; +#endif +} + +bool Remove(const char* path) +{ +#if defined(__unix__) + return remove(path) == 0; +#elif defined(_MSC_VER) + return ::DeleteFile(path); +#endif +} + +} +} diff --git a/src/armnnUtils/Filesystem.hpp b/src/armnnUtils/Filesystem.hpp new file mode 100644 index 0000000000..d6dc5b97fd --- /dev/null +++ b/src/armnnUtils/Filesystem.hpp @@ -0,0 +1,18 @@ +// +// Copyright © 2020 Arm Ltd. All rights reserved. +// SPDX-License-Identifier: MIT +// + +#pragma once + +namespace armnnUtils +{ +namespace Filesystem +{ + +long GetFileSize(const char* path); + +bool Remove(const char* path); + +} +} diff --git a/src/armnnUtils/Processes.cpp b/src/armnnUtils/Processes.cpp new file mode 100644 index 0000000000..0e43e8cecd --- /dev/null +++ b/src/armnnUtils/Processes.cpp @@ -0,0 +1,30 @@ +// +// Copyright © 2020 Arm Ltd. All rights reserved. +// SPDX-License-Identifier: MIT +// + +#include "Processes.hpp" + +#if defined(__unix__) +#include +#elif defined(_MSC_VER) +#define WIN32_LEAN_AND_MEAN +#include +#endif + +namespace armnnUtils +{ +namespace Processes +{ + +int GetCurrentId() +{ +#if defined(__unix__) + return getpid(); +#elif defined(_MSC_VER) + return ::GetCurrentProcessId(); +#endif +} + +} +} diff --git a/src/armnnUtils/Processes.hpp b/src/armnnUtils/Processes.hpp new file mode 100644 index 0000000000..0f1d955bb1 --- /dev/null +++ b/src/armnnUtils/Processes.hpp @@ -0,0 +1,16 @@ +// +// Copyright © 2020 Arm Ltd. All rights reserved. +// SPDX-License-Identifier: MIT +// + +#pragma once + +namespace armnnUtils +{ +namespace Processes +{ + +int GetCurrentId(); + +} +} diff --git a/src/profiling/SendCounterPacket.cpp b/src/profiling/SendCounterPacket.cpp index 5128331c46..4d305af951 100644 --- a/src/profiling/SendCounterPacket.cpp +++ b/src/profiling/SendCounterPacket.cpp @@ -9,6 +9,7 @@ #include #include +#include #include #include @@ -90,7 +91,8 @@ void SendCounterPacket::SendStreamMetaDataPacket() offset += sizeUint32; WriteUint32(writeBuffer, offset, MAX_METADATA_PACKET_LENGTH); // max_data_length offset += sizeUint32; - WriteUint32(writeBuffer, offset, numeric_cast(getpid())); // pid + int pid = armnnUtils::Processes::GetCurrentId(); + WriteUint32(writeBuffer, offset, numeric_cast(pid)); // pid offset += sizeUint32; uint32_t poolOffset = bodySize; WriteUint32(writeBuffer, offset, infoSize ? poolOffset : 0); // offset_info diff --git a/src/profiling/test/FileOnlyProfilingDecoratorTests.cpp b/src/profiling/test/FileOnlyProfilingDecoratorTests.cpp index 26634704ae..4112dbac03 100644 --- a/src/profiling/test/FileOnlyProfilingDecoratorTests.cpp +++ b/src/profiling/test/FileOnlyProfilingDecoratorTests.cpp @@ -7,6 +7,7 @@ #include #include +#include #include #include @@ -94,12 +95,10 @@ BOOST_AUTO_TEST_CASE(DumpOutgoingValidFileEndToEnd) profilingService.ResetExternalProfilingOptions(options, true); // The output file size should be greater than 0. - struct stat statusBuffer; - BOOST_CHECK(stat(tempPath.c_str(), &statusBuffer) == 0); - BOOST_CHECK(statusBuffer.st_size > 0); + BOOST_CHECK(armnnUtils::Filesystem::GetFileSize(tempPath.string().c_str()) > 0); // Delete the tmp file. - BOOST_CHECK(remove(tempPath.c_str()) == 0); + BOOST_CHECK(armnnUtils::Filesystem::Remove(tempPath.string().c_str())); } BOOST_AUTO_TEST_SUITE_END() diff --git a/src/profiling/test/SendCounterPacketTests.cpp b/src/profiling/test/SendCounterPacketTests.cpp index 19423165a9..83bffe4686 100644 --- a/src/profiling/test/SendCounterPacketTests.cpp +++ b/src/profiling/test/SendCounterPacketTests.cpp @@ -10,6 +10,7 @@ #include #include #include +#include #include #include @@ -335,7 +336,8 @@ BOOST_AUTO_TEST_CASE(SendStreamMetaDataPacketTest) offset += sizeUint32; BOOST_TEST(ReadUint32(readBuffer2, offset) == MAX_METADATA_PACKET_LENGTH); // max_data_len offset += sizeUint32; - BOOST_TEST(ReadUint32(readBuffer2, offset) == numeric_cast(getpid())); // pid + int pid = armnnUtils::Processes::GetCurrentId(); + BOOST_TEST(ReadUint32(readBuffer2, offset) == numeric_cast(pid)); offset += sizeUint32; uint32_t poolOffset = 10 * sizeUint32; BOOST_TEST(ReadUint32(readBuffer2, offset) == (infoSize ? poolOffset : 0)); // offset_info -- cgit v1.2.1