Created
January 19, 2022 17:00
-
-
Save mathis-m/7ce5cf9f1fc791e52167261c3549155f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define _CRT_SECURE_NO_WARNINGS | |
#include <unistd.h> | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <sys/time.h> | |
#include <stdbool.h> | |
double getDifferenceTimeOfDay(struct timeval *begin, struct timeval *end) { | |
return ((end->tv_sec - begin->tv_sec) + ((end->tv_usec - begin->tv_usec)/(1000.0*1000.0))); | |
} | |
int main(int argc, char *argv[]) { | |
int count = atoi(argv[2]); | |
bool logToConsole = atoi(argv[1]); | |
FILE *f = NULL; | |
FILE *f2 = NULL; | |
if(logToConsole == false) { | |
f = fopen("result.csv", "w"); | |
if (f == NULL) | |
{ | |
printf("Error opening file!\n"); | |
exit(1); | |
} | |
fprintf(f, "%s,%s\n", "Size", "Durchsatz"); | |
fclose(f); | |
f2 = fopen("result2.csv", "w"); | |
if (f2 == NULL) | |
{ | |
printf("Error opening file!\n"); | |
exit(1); | |
} | |
fprintf(f2, "%s,%s\n", "Size", "Time"); | |
fclose(f2); | |
} else { | |
printf("%s\t\t %s\t\t %s\n", "Size", "Durchsatz", "Total Time"); | |
} | |
for (unsigned long size = 16; size <= 16777216; size *= 4) { | |
int pipefd[2] = {0}; | |
int pipefdBack[2] = {0}; | |
int i, n, m; | |
char *buf; | |
char *buf2; | |
buf = malloc(size); | |
buf2 = malloc(size); | |
double mbPerS, totalTimeTaken; | |
if (buf == NULL) { | |
perror("malloc"); | |
return 1; | |
} | |
if (pipe(pipefd) == -1) { | |
perror("pipe"); | |
return 1; | |
} | |
if (pipe(pipefdBack) == -1) { | |
perror("pipe"); | |
return 1; | |
} | |
if (fork() == 0) { | |
// child Prozess | |
unsigned long sum = 0; | |
for (i = 0; i < count; i++) { | |
n = read(pipefd[0], buf, size); | |
if (n == -1) { | |
perror("read"); | |
return 1; | |
} | |
sum += n; | |
} | |
if (sum != count * size) { | |
fprintf(stderr, "sum error: %ld != %ld\n", sum, count * size); | |
return 1; | |
} | |
for (i = 0; i < count; i++) { | |
size_t bytesWritten = write(pipefdBack[1], buf2, size); | |
if (bytesWritten != size) { | |
fprintf(stderr, "write size did not match: %zu bytes were written.\n", bytesWritten); | |
return 1; | |
} | |
} | |
return 0; | |
} else { | |
// process | |
// send data to child process | |
struct timeval begin, end, totalBegin, endTotal; | |
gettimeofday(&totalBegin, NULL); | |
gettimeofday(&begin, NULL); | |
for (i = 0; i < count; i++) { | |
size_t bytesWritten = write(pipefd[1], buf, size); | |
if (bytesWritten != size) { | |
fprintf(stderr, "write size did not match: %zu bytes were written.\n", bytesWritten); | |
return 1; | |
} | |
} | |
gettimeofday(&end, NULL); | |
double timeTaken = getDifferenceTimeOfDay(&begin, &end); | |
mbPerS = (count * size * 1.0) / (timeTaken * 1024 * 1024); | |
unsigned long sum2 = 0; | |
for (int x = 0; x < count; x++) { | |
m = read(pipefdBack[0], buf2, size); | |
if (m == -1) { | |
perror("read"); | |
return 1; | |
} | |
sum2 += m; | |
} | |
gettimeofday(&endTotal, NULL); | |
if (sum2 != count * size) { | |
fprintf(stderr, "sum2 error: %ld != %ld\n", sum2, count * size); | |
return 1; | |
} | |
totalTimeTaken = getDifferenceTimeOfDay(&totalBegin, &endTotal) / count; | |
} | |
if (logToConsole == true) { | |
printf("%ld byte\t\t %.0f MiB/s\t\t\t %f\n", size, mbPerS, totalTimeTaken); | |
} else { | |
f = fopen("result.csv", "a"); | |
if (f == NULL) | |
{ | |
printf("Error opening file!\n"); | |
exit(1); | |
} | |
fprintf(f, "%ld,%.0f\n", size, mbPerS); | |
fclose(f); | |
f2 = fopen("result2.csv", "a"); | |
if (f2 == NULL) | |
{ | |
printf("Error opening file!\n"); | |
exit(1); | |
} | |
fprintf(f2, "%ld,%f\n", size, totalTimeTaken); | |
fclose(f2); | |
} | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment