Blame test/allocator_perf_tool/Tests.hpp

Packit 345191
/*
Packit 345191
* Copyright (C) 2015 - 2018 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
/*
Packit 345191
This file contain self-tests for Allocator Perf Tool.
Packit 345191
*/
Packit 345191
#pragma once
Packit 345191
Packit 345191
#include <unistd.h>
Packit 345191
Packit 345191
#include "Configuration.hpp"
Packit 345191
#include "AllocatorFactory.hpp"
Packit 345191
#include "AllocationSizes.hpp"
Packit 345191
#include "VectorIterator.hpp"
Packit 345191
#include "StandardAllocatorWithTimer.hpp"
Packit 345191
#include "ScenarioWorkload.h"
Packit 345191
#include "TaskFactory.hpp"
Packit 345191
#include "Task.hpp"
Packit 345191
Packit 345191
//Basic test for allocators. Allocate 32 bytes by malloc, then deallocate it by free.
Packit 345191
void test_allocator(Allocator &allocator, const char *allocator_name)
Packit 345191
{
Packit 345191
    printf("\n============= Allocator (%s) test ============= \n",allocator_name);
Packit 345191
Packit 345191
    memory_operation data = allocator.wrapped_malloc(32);
Packit 345191
    printf("malloc: %p \n",data.ptr);
Packit 345191
Packit 345191
    assert(data.ptr!=NULL);
Packit 345191
Packit 345191
    allocator.wrapped_free(data.ptr);
Packit 345191
    printf("free: %p \n",data.ptr);
Packit 345191
Packit 345191
    printf("\n==================================================== \n");
Packit 345191
}
Packit 345191
Packit 345191
//Test for the workload classes. This test count and validate the number or runs.
Packit 345191
void test_workload(Workload &workload, const int N, const char *workload_name)
Packit 345191
{
Packit 345191
    printf("\n============= Work load (%s) test ============= \n",workload_name);
Packit 345191
Packit 345191
    assert(workload.run()); // Do "single" memory operation (allocate or deallocate).
Packit 345191
Packit 345191
    int i;
Packit 345191
    for (i=1; workload.run(); i++); // Do the rest of operations.
Packit 345191
    assert(i == N);
Packit 345191
Packit 345191
    printf("\n==================================================== \n");
Packit 345191
}
Packit 345191
Packit 345191
//Simulate time consuming memory operation.
Packit 345191
memory_operation time_consuming_memory_operation(unsigned int seconds)
Packit 345191
{
Packit 345191
    size_t size;
Packit 345191
Packit 345191
    START_TEST(0,0)
Packit 345191
    sleep(seconds);
Packit 345191
    END_TEST
Packit 345191
}
Packit 345191
Packit 345191
//This test check if timer has measured time.
Packit 345191
void test_timer()
Packit 345191
{
Packit 345191
    float total_time = time_consuming_memory_operation(2).total_time;
Packit 345191
Packit 345191
    bool test_res = (total_time >=  2.0) && (total_time < 2.2);
Packit 345191
    if(!test_res)
Packit 345191
        printf("test_timer(): unexpected timing %f", total_time);
Packit 345191
    assert(test_res);
Packit 345191
}
Packit 345191
Packit 345191
//Test for iterators. Check the number of iterations, such as:
Packit 345191
//N == number of iterations.
Packit 345191
template<class T>
Packit 345191
void test_iterator(Iterator<T> &it, const int N)
Packit 345191
{
Packit 345191
    printf("\n================= Iteartor test ==================== \n");
Packit 345191
Packit 345191
    assert(it.size() == N);
Packit 345191
    int i;
Packit 345191
    for (i=0; it.has_next(); i++) {
Packit 345191
        it.next();
Packit 345191
    }
Packit 345191
    printf("iterations=%d/%d\n",i,N);
Packit 345191
    assert(i == N);
Packit 345191
Packit 345191
    printf("\n==================================================== \n");
Packit 345191
}
Packit 345191
Packit 345191
//This test validate range of values generated by random generators.
Packit 345191
template<class T, class C>
Packit 345191
void test_iterator_values(VectorIterator<T> &it, const C from, const C to)
Packit 345191
{
Packit 345191
    for (int i=0; it.has_next(); i++) {
Packit 345191
        T val = it.next();
Packit 345191
Packit 345191
        if(val < from) std::cout << "ivalid value: actual=" << val << ", expected= >="
Packit 345191
                                     << from << std::endl;
Packit 345191
Packit 345191
        if(val > to) std::cout << "ivalid value: actual=" << val << ", expected= <=" <<
Packit 345191
                                   to << std::endl;
Packit 345191
    }
Packit 345191
}
Packit 345191
Packit 345191
//Test time counting instrumentation in StandardAllocatorWithTimer.
Packit 345191
//The instrumentation is made with START_TEST and END_TEST macros from WrapperMacros.h.
Packit 345191
void test_allocator_with_timer(int N, int seed)
Packit 345191
{
Packit 345191
    printf("\n============= Allocator with timer test ============= \n");
Packit 345191
Packit 345191
    StandardAllocatorWithTimer allocator;
Packit 345191
    VectorIterator<size_t> allocation_sizes =
Packit 345191
        AllocationSizes::generate_random_sizes(N, 32, 2048, seed);
Packit 345191
    memory_operation data;
Packit 345191
Packit 345191
    double elaspsed_time = 0;
Packit 345191
    for (int i=0; i
Packit 345191
        data = allocator.wrapped_malloc(allocation_sizes.next());
Packit 345191
        elaspsed_time += data.total_time;
Packit 345191
        allocator.wrapped_free(data.ptr);
Packit 345191
    }
Packit 345191
Packit 345191
    printf("%d allocations and frees done in time: %f \n", N, elaspsed_time);
Packit 345191
Packit 345191
    printf("\n==================================================== \n");
Packit 345191
}
Packit 345191
Packit 345191
//Test behavior of TypesConfiguration class, by enabling and disabling types.
Packit 345191
void test_types_conf()
Packit 345191
{
Packit 345191
    TypesConf types;
Packit 345191
Packit 345191
    for (unsigned i=0; i
Packit 345191
        types.enable_type(i);
Packit 345191
        assert(types.is_enabled(i));
Packit 345191
        types.disable_type(i);
Packit 345191
        assert(!types.is_enabled(i));
Packit 345191
    }
Packit 345191
}
Packit 345191
Packit 345191
void execute_self_tests()
Packit 345191
{
Packit 345191
    const int N = 10000;
Packit 345191
    const size_t size_from = 32, size_to = 2048;
Packit 345191
    const unsigned seed = 11;
Packit 345191
Packit 345191
    test_types_conf();
Packit 345191
Packit 345191
    {
Packit 345191
        VectorIterator<size_t> it = AllocationSizes::generate_random_sizes(N, size_from,
Packit 345191
                                                                           size_to,seed);
Packit 345191
        test_iterator_values(it, size_from, size_to);
Packit 345191
    }
Packit 345191
Packit 345191
    {
Packit 345191
        TypesConf enable_func_calls;
Packit 345191
        enable_func_calls.enable_type(FunctionCalls::MALLOC);
Packit 345191
Packit 345191
        VectorIterator<int> it = FunctionCalls::generate_random_allocator_func_calls(N,
Packit 345191
                                                                                     seed, enable_func_calls);
Packit 345191
        test_iterator(it, N);
Packit 345191
    }
Packit 345191
Packit 345191
//Timer implementation __cplusplus < 201100L is based on CPU clocks counting and it will fail on this test.
Packit 345191
#if __cplusplus > 201100L
Packit 345191
    test_timer();
Packit 345191
#endif
Packit 345191
Packit 345191
    printf("Test completed! (press ENTER to continue)\n");
Packit 345191
}
Packit 345191