ucmd_membench.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /* membench: ring-3 speed check. Times the CPU clock, an ALU loop, memcpy/
  2. * memset over a heap buffer (the libc arena; highmem frames on a >1 GB
  3. * machine) and over a BSS buffer (the ELF image), and the syscall rate of
  4. * the lock-free clock against a syscall that takes the big kernel lock.
  5. * Three rounds each, so a process moving between CPUs shows as spread.
  6. * Written to find why ring-3 code ran ~100x slower on one board. */
  7. #include "ucmd_lib.h"
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #define BUF_BYTES (1024u * 1024u)
  11. #define ROUND_MS 300u
  12. static unsigned char bss_a[64 * 1024], bss_b[64 * 1024];
  13. static volatile unsigned int sink;
  14. static unsigned long long rdtsc(void)
  15. {
  16. unsigned int lo, hi;
  17. __asm__ volatile ("rdtsc" : "=a"(lo), "=d"(hi));
  18. return ((unsigned long long)hi << 32) | lo;
  19. }
  20. static void put_rate(const char *what, unsigned long long n, unsigned int ms,
  21. const char *unit)
  22. {
  23. u_puts(what);
  24. u_putdec(ms ? (unsigned int)(n * 1000ull / ms) : 0);
  25. u_puts(unit);
  26. }
  27. /* Wait for the next millisecond edge so every round starts aligned. */
  28. static unsigned int ms_edge(void)
  29. {
  30. unsigned int t = u_ticks_ms();
  31. while (u_ticks_ms() == t)
  32. ;
  33. return t + 1;
  34. }
  35. static void round_tsc(void)
  36. {
  37. unsigned int t0 = ms_edge(), t1;
  38. unsigned long long c0 = rdtsc(), c1;
  39. while ((t1 = u_ticks_ms()) - t0 < ROUND_MS)
  40. ;
  41. c1 = rdtsc();
  42. put_rate(" tsc ", (c1 - c0) / 1000ull, t1 - t0, " kHz\n");
  43. }
  44. static void round_alu(void)
  45. {
  46. unsigned int t0 = ms_edge(), t1, x = 1, i;
  47. unsigned long long n = 0;
  48. do {
  49. for (i = 0; i < 10000; i++)
  50. x = x * 1103515245u + 12345u;
  51. n += 10000;
  52. } while ((t1 = u_ticks_ms()) - t0 < ROUND_MS);
  53. sink = x;
  54. put_rate(" alu ", n / 1000ull, t1 - t0, " k-ops/s\n");
  55. }
  56. static void round_copy(const char *what, unsigned char *dst,
  57. const unsigned char *src, unsigned int len, int set)
  58. {
  59. unsigned int t0 = ms_edge(), t1;
  60. unsigned long long bytes = 0;
  61. do {
  62. if (set)
  63. memset(dst, (int)bytes, len);
  64. else
  65. memcpy(dst, src, len);
  66. bytes += len;
  67. } while ((t1 = u_ticks_ms()) - t0 < ROUND_MS);
  68. sink = dst[len / 2];
  69. put_rate(what, bytes / 1024ull, t1 - t0, " KB/s\n");
  70. }
  71. static void round_sys(const char *what, int num)
  72. {
  73. unsigned int t0 = ms_edge(), t1;
  74. unsigned long long n = 0;
  75. do {
  76. u_syscall0(num);
  77. n++;
  78. } while ((t1 = u_ticks_ms()) - t0 < ROUND_MS);
  79. put_rate(what, n, t1 - t0, " calls/s\n");
  80. }
  81. void _start(const char *arg)
  82. {
  83. unsigned char *a, *b;
  84. int r;
  85. (void)arg;
  86. a = (unsigned char *)malloc(BUF_BYTES);
  87. b = (unsigned char *)malloc(BUF_BYTES);
  88. if (!a || !b) {
  89. u_puts("membench: out of memory\n");
  90. u_exit(1);
  91. }
  92. memset(a, 1, BUF_BYTES);
  93. memset(b, 2, BUF_BYTES);
  94. for (r = 1; r <= 3; r++) {
  95. u_puts("round ");
  96. u_putdec((unsigned int)r);
  97. u_puts("\n");
  98. round_tsc();
  99. round_alu();
  100. round_copy(" heap memcpy 1M ", a, b, BUF_BYTES, 0);
  101. round_copy(" heap memset 1M ", a, 0, BUF_BYTES, 1);
  102. round_copy(" bss memcpy 64K ", bss_a, bss_b, sizeof(bss_a), 0);
  103. round_sys(" sys ticks (no BKL) ", SYS_GET_TICKS_MS);
  104. round_sys(" sys getpid (BKL) ", SYS_GETPID);
  105. }
  106. u_exit(0);
  107. }