1
0

measure-anything.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /*
  2. * measure-anything.c version 20090223
  3. * D. J. Bernstein
  4. * Public domain.
  5. */
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include <time.h>
  10. #include <sys/time.h>
  11. #include <sys/types.h>
  12. #include <sys/resource.h>
  13. #include "cpucycles.h"
  14. #include "cpuid.h"
  15. typedef int uint32;
  16. static uint32 seed[32] = { 3,1,4,1,5,9,2,6,5,3,5,8,9,7,9,3,2,3,8,4,6,2,6,4,3,3,8,3,2,7,9,5 } ;
  17. static uint32 in[12];
  18. static uint32 out[8];
  19. static int outleft = 0;
  20. #define ROTATE(x,b) (((x) << (b)) | ((x) >> (32 - (b))))
  21. #define MUSH(i,b) x = t[i] += (((x ^ seed[i]) + sum) ^ ROTATE(x,b));
  22. static void surf(void)
  23. {
  24. uint32 t[12]; uint32 x; uint32 sum = 0;
  25. int r; int i; int loop;
  26. for (i = 0;i < 12;++i) t[i] = in[i] ^ seed[12 + i];
  27. for (i = 0;i < 8;++i) out[i] = seed[24 + i];
  28. x = t[11];
  29. for (loop = 0;loop < 2;++loop) {
  30. for (r = 0;r < 16;++r) {
  31. sum += 0x9e3779b9;
  32. MUSH(0,5) MUSH(1,7) MUSH(2,9) MUSH(3,13)
  33. MUSH(4,5) MUSH(5,7) MUSH(6,9) MUSH(7,13)
  34. MUSH(8,5) MUSH(9,7) MUSH(10,9) MUSH(11,13)
  35. }
  36. for (i = 0;i < 8;++i) out[i] ^= t[i + 4];
  37. }
  38. }
  39. void randombytes(unsigned char *x,unsigned long long xlen)
  40. {
  41. while (xlen > 0) {
  42. if (!outleft) {
  43. if (!++in[0]) if (!++in[1]) if (!++in[2]) ++in[3];
  44. surf();
  45. outleft = 8;
  46. }
  47. *x = out[--outleft];
  48. ++x;
  49. --xlen;
  50. }
  51. }
  52. extern const char *primitiveimplementation;
  53. extern const char *implementationversion;
  54. extern const char *sizenames[];
  55. extern const long long sizes[];
  56. extern void preallocate(void);
  57. extern void allocate(void);
  58. extern void measure(void);
  59. static void printword(const char *s)
  60. {
  61. if (!*s) putchar('-');
  62. while (*s) {
  63. if (*s == ' ') putchar('_');
  64. else if (*s == '\t') putchar('_');
  65. else if (*s == '\r') putchar('_');
  66. else if (*s == '\n') putchar('_');
  67. else putchar(*s);
  68. ++s;
  69. }
  70. putchar(' ');
  71. }
  72. static void printnum(long long x)
  73. {
  74. printf("%lld ",x);
  75. }
  76. static void fail(const char *why)
  77. {
  78. fprintf(stderr,"measure: fatal: %s\n",why);
  79. exit(111);
  80. }
  81. unsigned char *alignedcalloc(unsigned long long len)
  82. {
  83. unsigned char *x = (unsigned char *) calloc(1,len + 128);
  84. if (!x) fail("out of memory");
  85. /* will never deallocate so shifting is ok */
  86. x += 63 & (-(unsigned long) x);
  87. return x;
  88. }
  89. static long long cyclespersecond;
  90. static void printimplementations(void)
  91. {
  92. int i;
  93. printword("implementation");
  94. printword(primitiveimplementation);
  95. printword(implementationversion);
  96. printf("\n"); fflush(stdout);
  97. for (i = 0;sizenames[i];++i) {
  98. printword(sizenames[i]);
  99. printnum(sizes[i]);
  100. printf("\n"); fflush(stdout);
  101. }
  102. printword("cpuid");
  103. printword(cpuid);
  104. printf("\n"); fflush(stdout);
  105. printword("cpucycles_persecond");
  106. printnum(cyclespersecond);
  107. printf("\n"); fflush(stdout);
  108. printword("cpucycles_implementation");
  109. printword(cpucycles_implementation);
  110. printf("\n"); fflush(stdout);
  111. printword("compiler");
  112. printword(COMPILER);
  113. #if defined(__VERSION__) && !defined(__ICC)
  114. printword(__VERSION__);
  115. #elif defined(__xlc__)
  116. printword(__xlc__);
  117. #elif defined(__ICC)
  118. {
  119. char buf[256];
  120. sprintf(buf, "%d.%d.%d", __ICC/100, __ICC%100,
  121. __INTEL_COMPILER_BUILD_DATE);
  122. printword(buf);
  123. }
  124. #elif defined(__PGIC__)
  125. {
  126. char buf[256];
  127. sprintf(buf, "%d.%d.%d", __PGIC__, __PGIC_MINOR__, __PGIC_PATCHLEVEL__);
  128. printword(buf);
  129. }
  130. #elif defined(__SUNPRO_C)
  131. {
  132. char buf[256];
  133. int major, minor, micro;
  134. micro = __SUNPRO_C & 0xf;
  135. minor = (__SUNPRO_C >> 4) & 0xf;
  136. major = (__SUNPRO_C >> 8) & 0xf;
  137. if (micro)
  138. sprintf(buf, "%d.%d.%d", major, minor, micro);
  139. else
  140. sprintf(buf, "%d.%d", major, minor);
  141. printword(buf);
  142. }
  143. #else
  144. printword("unknown compiler version");
  145. #endif
  146. printf("\n"); fflush(stdout);
  147. }
  148. void printentry(long long mbytes,const char *measuring,long long *m,long long mlen)
  149. {
  150. long long i;
  151. long long j;
  152. long long belowj;
  153. long long abovej;
  154. printword(measuring);
  155. if (mbytes >= 0) printnum(mbytes); else printword("");
  156. if (mlen > 0) {
  157. for (j = 0;j + 1 < mlen;++j) {
  158. belowj = 0;
  159. for (i = 0;i < mlen;++i) if (m[i] < m[j]) ++belowj;
  160. abovej = 0;
  161. for (i = 0;i < mlen;++i) if (m[i] > m[j]) ++abovej;
  162. if (belowj * 2 < mlen && abovej * 2 < mlen) break;
  163. }
  164. printnum(m[j]);
  165. if (mlen > 1) {
  166. for (i = 0;i < mlen;++i) printnum(m[i]);
  167. }
  168. }
  169. printf("\n"); fflush(stdout);
  170. }
  171. void limits()
  172. {
  173. #ifdef RLIM_INFINITY
  174. struct rlimit r;
  175. r.rlim_cur = 0;
  176. r.rlim_max = 0;
  177. #ifdef RLIMIT_NOFILE
  178. setrlimit(RLIMIT_NOFILE,&r);
  179. #endif
  180. #ifdef RLIMIT_NPROC
  181. setrlimit(RLIMIT_NPROC,&r);
  182. #endif
  183. #ifdef RLIMIT_CORE
  184. setrlimit(RLIMIT_CORE,&r);
  185. #endif
  186. #endif
  187. }
  188. int main()
  189. {
  190. cyclespersecond = cpucycles_persecond();
  191. preallocate();
  192. limits();
  193. printimplementations();
  194. allocate();
  195. measure();
  196. return 0;
  197. }