Created
January 30, 2025 08:47
-
-
Save teknoraver/ec3bb8b5616e0599684689d6f874546f to your computer and use it in GitHub Desktop.
This file contains 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
/* | |
* membench.c - simple memory benchmarking tool | |
* Copyright (C) 2020 Matteo Croce <[email protected]> | |
* | |
* This program is free software: you can redistribute it and/or modify | |
* it under the terms of the GNU General Public License as published by | |
* the Free Software Foundation, either version 3 of the License, or | |
* (at your option) any later version. | |
* | |
* This program is distributed in the hope that it will be useful, | |
* but WITHOUT ANY WARRANTY; without even the implied warranty of | |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
* GNU General Public License for more details. | |
* | |
* You should have received a copy of the GNU General Public License | |
* along with this program. If not, see <https://www.gnu.org/licenses/>. | |
*/ | |
#include <stdint.h> | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <unistd.h> | |
#include <string.h> | |
#include <sys/mman.h> | |
#include <time.h> | |
#define B 1000000000 | |
static void __attribute__ ((noreturn)) usage(char *argv0, int ret) | |
{ | |
fprintf(stderr, "usage: %s [-v] [-c count] -s size\n", argv0); | |
exit(ret); | |
} | |
static uint64_t time_sub(struct timespec *since, struct timespec *to) | |
{ | |
if (to->tv_sec == since->tv_sec) | |
return to->tv_nsec - since->tv_nsec; | |
return (to->tv_sec - since->tv_sec) * B + to->tv_nsec - since->tv_nsec; | |
} | |
int main(int argc, char *argv[]) | |
{ | |
struct timespec oldt, newt; | |
size_t size = 0; | |
int verbose = 0; | |
uint64_t delta; | |
int count = 1; | |
void *buf; | |
int i, c; | |
while ((c = getopt(argc, argv, "s:c:vh")) != -1) | |
switch (c) { | |
case 's': { | |
int len = strlen(optarg); | |
size = atoi(optarg); | |
if (!size) { | |
fprintf(stderr, "invalid size '%s'\n", optarg); | |
return 1; | |
} | |
switch (optarg[len-1]) { | |
case 't': | |
case 'T': | |
size *= 1024; | |
case 'g': | |
case 'G': | |
size *= 1024; | |
case 'm': | |
case 'M': | |
size *= 1024; | |
case 'k': | |
case 'K': | |
size *= 1024; | |
} | |
break; | |
} | |
case 'c': | |
count = atoi(optarg); | |
break; | |
case 'v': | |
verbose = 1; | |
break; | |
case 'h': | |
usage(argv[0], 0); | |
} | |
buf = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); | |
if (buf == MAP_FAILED) { | |
perror("mmap"); | |
return 1; | |
} | |
if (mlock(buf, size) == -1) { | |
perror("mlock"); | |
return 1; | |
} | |
if (verbose) | |
printf("Starting test with length %lu\n", size); | |
clock_gettime(CLOCK_MONOTONIC, &oldt); | |
for (i = 0; i < count; i++) { | |
if (verbose) | |
printf("writing... "); | |
memset(buf, i, size); | |
if (verbose) | |
puts("reading..."); | |
if (memcmp(buf, buf + size / 2, size / 2)) { | |
fputs("\nmemory differs!", stderr); | |
return 1; | |
} | |
} | |
clock_gettime(CLOCK_MONOTONIC, &newt); | |
delta = time_sub(&oldt, &newt); | |
printf("R/W %.0f mbyte/sec\n", (double)size * count * B / 1024 / 1024 / delta); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment