Чтение файла блок за блоком с таймером - PullRequest
0 голосов
/ 20 октября 2018

Моя задача - измерить задержку / задержку между процессором и диском.Миссия состоит в том, чтобы получить время, которое необходимо подготовить для записи или чтения блока / файла.

Один блок на моем диске NTFS имеет размер 4096 байт.

Сценарий # 1: загрузка блок за блокомс диска на память проблема в том, что каждый раз, когда я получаю 0.0 миллисекунд для первого блока чтения.

#include <sys/time.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

float timedifference_msec(struct timeval t0, struct timeval t1)
{
    return (t1.tv_sec - t0.tv_sec) * 1000.0f + (t1.tv_usec - t0.tv_usec) / 1000.0f;
}

int main(void)
{
    struct timeval t0,t1,t2;
    float elapsed;
    size_t newLen;
    int i = 0;
    for (i = 0; i < 1; i++){
        FILE *fp0=fopen("D:\\Download\\foot.txt", "ab+");
        ftruncate(fileno(fp0), 1024*1024*4);
        fclose(fp0);

        gettimeofday(&t0, 0);
        char *source = NULL;
        FILE *fp = fopen("D:\\Download\\foot.txt", "rb");
        if (fp != NULL) {
            /* Go to the end of the file. */
            if (fseek(fp, 0L, SEEK_END) == 0) {
                /* Get the size of the file. */
                long bufsize = ftell(fp);
                if (bufsize == -1) { /* Error */ }

                /* Allocate our buffer to that size. */
                source = malloc(sizeof(char) * (bufsize + 1));

                /* Go back to the start of the file. */
                if (fseek(fp, 0L, SEEK_SET) != 0) { /* Handle error here */ }

                /* Read the entire file into memory. */
                gettimeofday(&t0, 0);
                while (newLen = fread(source, sizeof(char), 4096, fp) > 0) {
                    gettimeofday(&t2, 0);
                    if (newLen == 0) {
                        fputs("Error reading file", stderr);
                    } else {
                        //source[newLen] = '\0'; /* Just to be safe. */
                        i++;
                    }
                    gettimeofday(&t1, 0);
                    if (i == 0){
                        elapsed = timedifference_msec(t0, t1);
                    } else {
                        elapsed = timedifference_msec(t1, t2);
                    }


                    printf("%d Code executed in %f milliseconds.\n", i, elapsed);
                }
            }
            fclose(fp);
        }
        free(source);
    }

    return 0;
}

Сценарий № 2: запись блок за блоком, та же проблема

#include <sys/time.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#define O_DIRECT 040000
#include <malloc.h>
#include <windows.h>

float timedifference_msec(struct timeval t0, struct timeval t1)
{
    return (t1.tv_sec - t0.tv_sec) * 1000.0f + (t1.tv_usec - t0.tv_usec) / 1000.0f;
}

int main(void)
{
    struct timeval t0, t1, t2, t3;
    float elapsed;
    size_t newLen;
    char c [4096];
    char *source = __mingw_aligned_malloc(4096, 4096);
    int i = 0;
    int j = 0;

    int fdw = open("D:\\Download\\foot.txt", O_WRONLY |O_DIRECT, 00666);

    for(i=0; i<4096; i++){
        c[i] = 0;
    }

    memcpy(source, c, sizeof(c));
    for(j=0; j<1024; j++){
        gettimeofday(&t0, 0);
        write(fdw, source, a);
        gettimeofday(&t1, 0);
        elapsed = timedifference_msec(t0, t1);
        printf("j = %d, time in %f milliseconds.\n", j, elapsed);
    }

    close(fdw);
    free(source);

    return 0;
}
...