使用计时器逐块读取文件

时间:2018-10-20 10:21:13

标签: c cpu delay disk

我的任务是测量CPU和磁盘之间的延迟/延迟。任务是获取时间哪些磁盘需要为写入或读取块/文件做好准备。

磁盘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;
}

0 个答案:

没有答案