Blame memkind-1.10.0/test/allocator_perf_tool/StressIncreaseToMax.cpp

Packit 345191
/*
Packit 345191
* Copyright (C) 2015 - 2019 Intel Corporation.
Packit 345191
* All rights reserved.
Packit 345191
*
Packit 345191
* Redistribution and use in source and binary forms, with or without
Packit 345191
* modification, are permitted provided that the following conditions are met:
Packit 345191
* 1. Redistributions of source code must retain the above copyright notice(s),
Packit 345191
*    this list of conditions and the following disclaimer.
Packit 345191
* 2. Redistributions in binary form must reproduce the above copyright notice(s),
Packit 345191
*    this list of conditions and the following disclaimer in the documentation
Packit 345191
*    and/or other materials provided with the distribution.
Packit 345191
*
Packit 345191
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY EXPRESS
Packit 345191
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
Packit 345191
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO
Packit 345191
* EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
Packit 345191
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
Packit 345191
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
Packit 345191
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
Packit 345191
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
Packit 345191
* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
Packit 345191
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Packit 345191
*/
Packit 345191
Packit 345191
#include "StressIncreaseToMax.h"
Packit 345191
Packit 345191
Packit 345191
void StressIncreaseToMax::run()
Packit 345191
{
Packit 345191
    //Generate constant allocation sizes.
Packit 345191
    VectorIterator<size_t> allocation_sizes =
Packit 345191
        AllocationSizes::generate_random_sizes(task_conf.allocation_sizes_conf,
Packit 345191
                                               task_conf.seed);
Packit 345191
    //Generate only mallocs.
Packit 345191
    VectorIterator<int> func_calls =
Packit 345191
        FunctionCalls::generate_random_allocator_func_calls(task_conf.n, task_conf.seed,
Packit 345191
                                                            task_conf.func_calls);
Packit 345191
Packit 345191
    unsigned type;
Packit 345191
    for (type = 0; type < AllocatorTypes::NUM_OF_ALLOCATOR_TYPES; type++) {
Packit 345191
        if(task_conf.allocators_types.is_enabled(type))
Packit 345191
            break; //Assume that there is only one type.
Packit 345191
    }
Packit 345191
Packit 345191
Packit 345191
    AllocatorFactory allocator_factory;
Packit 345191
    VectorIterator<Allocator *> allocators_calls =
Packit 345191
        allocator_factory.generate_random_allocator_calls(task_conf.n, task_conf.seed,
Packit 345191
                                                          task_conf.allocators_types);
Packit 345191
Packit 345191
    ScenarioWorkload scenario_workload(
Packit 345191
        &allocators_calls,
Packit 345191
        &allocation_sizes,
Packit 345191
        &func_calls
Packit 345191
    );
Packit 345191
Packit 345191
    scenario_workload.enable_touch_memory_on_allocation(task_conf.touch_memory);
Packit 345191
Packit 345191
    test_status.is_allocation_error = false;
Packit 345191
Packit 345191
    size_t requested_memory = 0;
Packit 345191
    bool has_reach_memory_request_limit = false;
Packit 345191
Packit 345191
    while (!has_reach_memory_request_limit &&
Packit 345191
           !test_status.is_allocation_error &&
Packit 345191
           (test_status.has_next_memory_operation = scenario_workload.run())) {
Packit 345191
        memory_operation data = scenario_workload.get_allocations_info().back();
Packit 345191
        test_status.is_allocation_error = (data.error_code == ENOMEM) ||
Packit 345191
                                          (data.ptr == NULL);
Packit 345191
Packit 345191
        if(data.allocation_method != FunctionCalls::FREE) {
Packit 345191
            requested_memory += data.size_of_allocation;
Packit 345191
            has_reach_memory_request_limit = requested_memory >= req_mem_limit;
Packit 345191
        }
Packit 345191
    }
Packit 345191
Packit 345191
    if(!(scenario_workload.get_allocations_info().size() < task_conf.n) &&
Packit 345191
       !has_reach_memory_request_limit)
Packit 345191
        printf("\nWARNING: Too few memory operations to reach the limit.\n");
Packit 345191
    if(test_status.is_allocation_error) printf("\nWARNING: Allocation error. \n");
Packit 345191
Packit 345191
    results = scenario_workload.get_allocations_info();
Packit 345191
}
Packit 345191
Packit 345191
std::vector<iteration_result> StressIncreaseToMax::execute_test_iterations(
Packit 345191
    const TaskConf &task_conf,
Packit 345191
    unsigned time,
Packit 345191
    size_t requested_memory_limit)
Packit 345191
{
Packit 345191
    TimerSysTime timer;
Packit 345191
    unsigned itr = 0;
Packit 345191
    std::vector<iteration_result> results;
Packit 345191
Packit 345191
    std::ofstream csv_file;
Packit 345191
Packit 345191
    csv::Row row;
Packit 345191
    row.append("Iteration");
Packit 345191
    row.append("Allocated memory (MB)");
Packit 345191
    row.append("Elapsed time (seconds)");
Packit 345191
    if(task_conf.is_csv_log_enabled) {
Packit 345191
        csv_file.open("stress_test_increase_to_max.csv");
Packit 345191
        csv_file << row.export_row();
Packit 345191
    }
Packit 345191
    printf("%s", row.export_row().c_str());
Packit 345191
Packit 345191
    timer.start();
Packit 345191
Packit 345191
    while (timer.getElapsedTime() < time) {
Packit 345191
        StressIncreaseToMax stress_test(task_conf, requested_memory_limit);
Packit 345191
        stress_test.run();
Packit 345191
        float elapsed_time = timer.getElapsedTime();
Packit 345191
Packit 345191
        TimeStats stats;
Packit 345191
        stats += stress_test.get_results();
Packit 345191
Packit 345191
        results.push_back(stress_test.get_test_status());
Packit 345191
Packit 345191
        //Log every iteration of StressIncreaseToMax test.
Packit 345191
        csv::Row row;
Packit 345191
        row.append(itr);
Packit 345191
        row.append(convert_bytes_to_mb(stats.get_allocated()));
Packit 345191
        row.append(elapsed_time);
Packit 345191
        if(task_conf.is_csv_log_enabled) {
Packit 345191
            csv_file << row.export_row();
Packit 345191
        }
Packit 345191
        printf("%s", row.export_row().c_str());
Packit 345191
        fflush(stdout);
Packit 345191
Packit 345191
        itr++;
Packit 345191
    }
Packit 345191
Packit 345191
    printf("\nStress test (StressIncreaseToMax) finish in time %f.\n",
Packit 345191
           timer.getElapsedTime());
Packit 345191
Packit 345191
    csv_file.close();
Packit 345191
Packit 345191
    return results;
Packit 345191
}