A função malloc (memory allocation) aloca espaço para um bloco de bytes consecutivos na memória RAM do computador e devolve o endereço deste bloco. Esta é uma função da biblioteca padrão das linguagens C e C++.

O programa MallocIO utiliza esta função para mensurar a latência de alocação de um bloco de memória e da alocação em disco, retornando os respectivos tempos e informando a viabilidade para o Protheus, com os retornos Ótimo, Bom, Ruim e Péssimo.

Este programa foi desenvolvido pelo time de tecnologia, na linguagem C++, em virtude de diversos problemas em cenários de virtualização, onde a farm estava com configurações default ou com problemas de firmware. 


#include "pch.h"
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <iostream>

using namespace std;
int main()
{
    void *var = 0;

    time_t timer;
    time_t timer2;
    struct tm y2k = { 0 };
    double seconds;
    string msgret;

    printf("TESTE 1\n");
    printf("ALOCANDO BLOCO DE MEMORIA\n");

    y2k.tm_hour = 0;   y2k.tm_min = 0; y2k.tm_sec = 0;
    y2k.tm_year = 100; y2k.tm_mon = 0; y2k.tm_mday = 1;
    timer = time(NULL);

    for (int i = 0; i < 300000000; i++)
    {
        var = malloc(100);
        free(var);
    }

    timer2 = time(NULL);
    seconds = timer2 - timer;

    printf("TEMPO LEVADO PARA ALOCAR MEMORIA: %f segundos\n", seconds);

    printf("TESTE 2\n");
    printf("LEITURA EM DISCO\n");

    y2k.tm_hour = 0;   y2k.tm_min = 0; y2k.tm_sec = 0;
    y2k.tm_year = 100; y2k.tm_mon = 0; y2k.tm_mday = 1;
    timer = time(NULL);

    char * buffer;
    FILE *pFile = fopen("teste.txt", "rb");
    long lSize = ftell(pFile);
    size_t result;
    buffer = (char*)malloc(sizeof(char)*lSize);
    for (int i = 0; i < 7000000; i++)
    {
        fseek(pFile, 0, SEEK_END);
        result = fread(buffer, 1, lSize, pFile);
    }
    fclose(pFile);
    free(buffer);
    timer2 = time(NULL);
    seconds = timer2 - timer;

    printf("TEMPO LEVADO PARA REALIZAR A LEITURA EM DISCO: %f segundos\n", seconds);
}