Advertisement
teknoraver

membench.c

Jan 17th, 2020
1,549
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.80 KB | None | 0 0
  1. /*
  2.  * membench.c - simple memory benchmarking tool
  3.  * Copyright (C) 2020 Matteo Croce <mcroce@redhat.com>
  4.  *
  5.  * This program is free software: you can redistribute it and/or modify
  6.  * it under the terms of the GNU General Public License as published by
  7.  * the Free Software Foundation, either version 3 of the License, or
  8.  * (at your option) any later version.
  9.  *
  10.  * This program is distributed in the hope that it will be useful,
  11.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.  * GNU General Public License for more details.
  14.  *
  15.  * You should have received a copy of the GNU General Public License
  16.  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
  17.  */
  18.  
  19. #include <stdint.h>
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <unistd.h>
  23. #include <string.h>
  24. #include <sys/mman.h>
  25. #include <time.h>
  26.  
  27. #define B 1000000000
  28.  
  29. static void __attribute__ ((noreturn)) usage(char *argv0, int ret)
  30. {
  31.     fprintf(stderr, "usage: %s [-v] [-c count] -s size\n", argv0);
  32.     exit(ret);
  33. }
  34.  
  35. static uint64_t time_sub(struct timespec *since, struct timespec *to)
  36. {
  37.     if (to->tv_sec == since->tv_sec)
  38.         return to->tv_nsec - since->tv_nsec;
  39.  
  40.     return (to->tv_sec - since->tv_sec) * B + to->tv_nsec - since->tv_nsec;
  41. }
  42.  
  43. int main(int argc, char *argv[])
  44. {
  45.     struct timespec oldt, newt;
  46.     size_t size = 0;
  47.     int verbose = 0;
  48.     uint64_t delta;
  49.     int count = 1;
  50.     void *buf;
  51.     int i, c;
  52.  
  53.     while ((c = getopt(argc, argv, "s:c:vh")) != -1)
  54.         switch (c) {
  55.         case 's': {
  56.             int len = strlen(optarg);
  57.  
  58.             size = atoi(optarg);
  59.             if (!size) {
  60.                 fprintf(stderr, "invalid size '%s'\n", optarg);
  61.                 return 1;
  62.             }
  63.             switch (optarg[len-1]) {
  64.             case 't':
  65.             case 'T':
  66.                 size *= 1024;
  67.             case 'g':
  68.             case 'G':
  69.                 size *= 1024;
  70.             case 'm':
  71.             case 'M':
  72.                 size *= 1024;
  73.             case 'k':
  74.             case 'K':
  75.                 size *= 1024;
  76.             }
  77.             break;
  78.         }
  79.         case 'c':
  80.             count = atoi(optarg);
  81.             break;
  82.         case 'v':
  83.             verbose = 1;
  84.             break;
  85.         case 'h':
  86.             usage(argv[0], 0);
  87.         }
  88.  
  89.     buf = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
  90.     if (buf == MAP_FAILED) {
  91.         perror("mmap");
  92.         return 1;
  93.     }
  94.  
  95.     if (mlock(buf, size) == -1) {
  96.         perror("mlock");
  97.         return 1;
  98.     }
  99.  
  100.     if (verbose)
  101.         printf("Starting test with length %lu\n", size);
  102.  
  103.     clock_gettime(CLOCK_MONOTONIC, &oldt);
  104.     for (i = 0; i < count; i++) {
  105.         if (verbose)
  106.             printf("writing... ");
  107.         memset(buf, i, size);
  108.         if (verbose)
  109.             puts("reading...");
  110.         if (memcmp(buf, buf + size / 2, size / 2)) {
  111.             fputs("\nmemory differs!", stderr);
  112.             return 1;
  113.         }
  114.     }
  115.     clock_gettime(CLOCK_MONOTONIC, &newt);
  116.  
  117.     delta = time_sub(&oldt, &newt);
  118.     printf("R/W %.0f mbyte/sec\n", (double)size * count * B / 1024 / 1024 / delta);
  119.  
  120.     return 0;
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement