Advertisement
teknoraver

test_memcpy

Jun 26th, 2021 (edited)
1,106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.52 KB | None | 0 0
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. #include <linux/printk.h>
  3. #include <linux/slab.h>
  4. #include <linux/mm.h>
  5. #include <linux/timekeeping.h>
  6. #include <linux/string.h>
  7.  
  8. #ifdef CONFIG_X86
  9. #include <asm/cacheflush.h>
  10. #endif
  11.  
  12. #define NUM_PAGES   (1 << (MAX_ORDER + 5))
  13. #define PG_SIZE     (PAGE_SIZE * NUM_PAGES)
  14.  
  15. static __init int memcpy_align_selftest(void)
  16. {
  17.     struct page *page1, *page2;
  18.     char *dst, *src;
  19.     ktime_t t0, t1;
  20.     int err = 0;
  21.     int i, j;
  22.  
  23.     page1 = alloc_contig_pages(NUM_PAGES, GFP_KERNEL, NUMA_NO_NODE, 0);
  24.     if (!page1)
  25.         return 1;
  26.  
  27.     page2 = alloc_contig_pages(NUM_PAGES, GFP_KERNEL, NUMA_NO_NODE, 0);
  28.     if (!page2) {
  29.         err = 1;
  30.         goto free1;
  31.     }
  32.  
  33.     printk("Strings selftest: memcpy testing with size: %lu Mb\n", PG_SIZE >> 20);
  34.  
  35.     src = page_to_virt(page1);
  36.     dst = page_to_virt(page2);
  37.  
  38.     preempt_disable();
  39.  
  40.     for (i = 0; i < sizeof(void*); i++) {
  41. #ifdef CONFIG_X86
  42.         for (j = 0; j < PG_SIZE; j += L1_CACHE_BYTES) {
  43.             clflush(src + j);
  44.             clflush(dst + j);
  45.             cond_resched();
  46.         }
  47. #endif
  48.         for (j = 0; j < sizeof(void*); j++) {
  49.             t0 = ktime_get();
  50.             memcpy(dst + i, src + j, PG_SIZE - max(i, j));
  51.             t1 = ktime_get();
  52.             printk("Strings selftest: memcpy(dst+%d, src+%d), distance %lu: %llu Mb/s\n",
  53.                    i, j, (j - i) % sizeof(long),
  54.                    PG_SIZE * (1000000000l / 1048576l) / (t1-t0));
  55.             cond_resched();
  56.         }
  57.         printk("\n");
  58.     }
  59.  
  60.     preempt_enable();
  61.  
  62.     free_contig_range(page_to_pfn(page2), NUM_PAGES);
  63.  
  64. free1:
  65.     free_contig_range(page_to_pfn(page1), NUM_PAGES);
  66.  
  67.     return err;
  68. }
  69.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement