Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added lost usage of USE_AWS_MEMORY_MANAGEMENT #2424

Merged
merged 2 commits into from
Jan 5, 2024

Conversation

sdavtaker
Copy link
Contributor

@sdavtaker sdavtaker commented Mar 31, 2023

Issue #, if available:

Description of changes:

Check all that applies:

  • Did a review by yourself.
  • Added proper tests to cover this PR. (If tests are not applicable, explain.)
  • Checked if this PR is a breaking (APIs have been changed) change.
  • Checked if this PR will not introduce cross-platform inconsistent behavior.
  • Checked if this PR would require a ReadMe/Wiki update.

Check which platforms you have built SDK on to verify the correctness of this PR.

  • Linux
  • Windows
  • Android
  • MacOS
  • IOS
  • Other Platforms

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

@jeking3
Copy link
Contributor

jeking3 commented Mar 31, 2023

If you do not add CUSTOM_MEMORY_MANAGEMENT test support to RunTests, not sure this will get tested. When I made similar changes earlier today, the unit tests in aws-sdk-cpp bombed out. Here's an interesting RunTests.cpp... but any CI needs to have a test run with -DCUSTOM_MEMORY_MANAGEMENT=ON enabled as one of the jobs to make this useful.

/**
 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
 * SPDX-License-Identifier: Apache-2.0.
 */

#include <gtest/gtest.h>
#include <aws/core/Aws.h>
#include <aws/core/utils/logging/LogMacros.h>
#include <aws/testing/TestingEnvironment.h>
#include <aws/testing/platform/PlatformTesting.h>
#include <aws/testing/MemoryTesting.h>

#if defined(HAS_UMASK)
#include <sys/stat.h>
#endif

Aws::SDKOptions options;

#ifdef USE_AWS_MEMORY_MANAGEMENT
class TestMemoryManager : public Aws::Utils::Memory::MemorySystemInterface
{
public:
    void *AllocateMemory(std::size_t blockSize, std::size_t alignment,
                         const char *allocationTag) override
    {
        (void)alignment;
        (void)allocationTag;
        return malloc(blockSize);
    }

    void FreeMemory(void* memoryPtr) override
    {
        free(memoryPtr);
    }

    void Begin() override
    {
    }

    void End() override
    {
    }
};
#endif // USE_AWS_MEMORY_MANAGEMENT

int main(int argc, char** argv)
{
#if defined(HAS_UMASK)
    // In order to fix github issue at https://github.com/aws/aws-sdk-cpp/issues/232
    // Created dir by this process will be set with mode 0777, so that multiple users can build on the same machine
    umask(0);
#endif

    Aws::Testing::RedirectHomeToTempIfAppropriate();

    options.loggingOptions.logLevel = Aws::Utils::Logging::LogLevel::Trace;
    options.httpOptions.installSigPipeHandler = true;
#ifdef USE_AWS_MEMORY_MANAGEMENT
    TestMemoryManager tmm;
    options.memoryManagementOptions.memoryManager = &tmm;
#endif // USE_AWS_MEMORY_MANAGEMENT

    AWS_BEGIN_MEMORY_TEST_EX(options, 1024, 128);

    Aws::Testing::InitPlatformTest(options);
    Aws::InitAPI(options);
    // Disable EC2 metadata in client configuration to avoid requests retrieving EC2 metadata in unit tests.
    Aws::Testing::SaveEnvironmentVariable("AWS_EC2_METADATA_DISABLED");
    Aws::Environment::SetEnv("AWS_EC2_METADATA_DISABLED", "true", 1/*override*/);
    ::testing::InitGoogleTest(&argc, argv);
    int retVal = RUN_ALL_TESTS();
    Aws::Testing::RestoreEnvironmentVariables();
    Aws::ShutdownAPI(options);
    AWS_END_MEMORY_TEST_EX;

    Aws::Testing::ShutdownPlatformTest(options);

    return retVal;
}

TEST(InitShutdown, Repeatable)
{
    for (unsigned ii = 0; ii < 5; ++ii)
    {
        // queue up some work for the logger, enough for it to be live until shutdown
        for (unsigned jj = 0; jj < 10000; ++jj) {
            AWS_LOG_WARN("InitShutdown.Repeatable", "test warn level");
        }

        Aws::ShutdownAPI(options);
        Aws::InitAPI(options);
    }
}

@SergeyRyabinin SergeyRyabinin force-pushed the fix_custom_mem_management branch 2 times, most recently from 9d17fa2 to 733b5fd Compare January 4, 2024 22:01
@SergeyRyabinin SergeyRyabinin merged commit bc80bf8 into main Jan 5, 2024
4 checks passed
@SergeyRyabinin SergeyRyabinin deleted the fix_custom_mem_management branch January 5, 2024 17:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants