benchmark-value-pairs.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623
  1. /* SPDX-License-Identifier: GPL-3.0-or-later */
  2. #include "config.h"
  3. #include "libnetdata/libnetdata.h"
  4. #ifdef simple_hash
  5. #undef simple_hash
  6. #endif
  7. void netdata_cleanup_and_exit(int ret) {
  8. exit(ret);
  9. }
  10. #define simple_hash(name) ({ \
  11. register unsigned char *__hash_source = (unsigned char *)(name); \
  12. register uint32_t __hash_value = 0x811c9dc5; \
  13. while (*__hash_source) { \
  14. __hash_value *= 16777619; \
  15. __hash_value ^= (uint32_t) *__hash_source++; \
  16. } \
  17. __hash_value; \
  18. })
  19. static inline uint32_t simple_hash2(const char *name) {
  20. register unsigned char *s = (unsigned char *)name;
  21. register uint32_t hval = 0x811c9dc5;
  22. while (*s) {
  23. hval *= 16777619;
  24. hval ^= (uint32_t) *s++;
  25. }
  26. return hval;
  27. }
  28. static inline unsigned long long fast_strtoull(const char *s) {
  29. register unsigned long long n = 0;
  30. register char c;
  31. for(c = *s; c >= '0' && c <= '9' ; c = *(++s)) {
  32. n *= 10;
  33. n += c - '0';
  34. // n = (n << 1) + (n << 3) + (c - '0');
  35. }
  36. return n;
  37. }
  38. static uint32_t cache_hash = 0;
  39. static uint32_t rss_hash = 0;
  40. static uint32_t rss_huge_hash = 0;
  41. static uint32_t mapped_file_hash = 0;
  42. static uint32_t writeback_hash = 0;
  43. static uint32_t dirty_hash = 0;
  44. static uint32_t swap_hash = 0;
  45. static uint32_t pgpgin_hash = 0;
  46. static uint32_t pgpgout_hash = 0;
  47. static uint32_t pgfault_hash = 0;
  48. static uint32_t pgmajfault_hash = 0;
  49. static uint32_t inactive_anon_hash = 0;
  50. static uint32_t active_anon_hash = 0;
  51. static uint32_t inactive_file_hash = 0;
  52. static uint32_t active_file_hash = 0;
  53. static uint32_t unevictable_hash = 0;
  54. static uint32_t hierarchical_memory_limit_hash = 0;
  55. static uint32_t total_cache_hash = 0;
  56. static uint32_t total_rss_hash = 0;
  57. static uint32_t total_rss_huge_hash = 0;
  58. static uint32_t total_mapped_file_hash = 0;
  59. static uint32_t total_writeback_hash = 0;
  60. static uint32_t total_dirty_hash = 0;
  61. static uint32_t total_swap_hash = 0;
  62. static uint32_t total_pgpgin_hash = 0;
  63. static uint32_t total_pgpgout_hash = 0;
  64. static uint32_t total_pgfault_hash = 0;
  65. static uint32_t total_pgmajfault_hash = 0;
  66. static uint32_t total_inactive_anon_hash = 0;
  67. static uint32_t total_active_anon_hash = 0;
  68. static uint32_t total_inactive_file_hash = 0;
  69. static uint32_t total_active_file_hash = 0;
  70. static uint32_t total_unevictable_hash = 0;
  71. unsigned long long values1[50] = { 0 };
  72. unsigned long long values2[50] = { 0 };
  73. unsigned long long values3[50] = { 0 };
  74. unsigned long long values4[50] = { 0 };
  75. unsigned long long values5[50] = { 0 };
  76. unsigned long long values6[50] = { 0 };
  77. unsigned long long values7[50] = { 0 };
  78. unsigned long long values8[50] = { 0 };
  79. unsigned long long values9[50] = { 0 };
  80. struct pair {
  81. const char *name;
  82. const char *value;
  83. uint32_t hash;
  84. unsigned long long *collected8;
  85. unsigned long long *collected9;
  86. } pairs[] = {
  87. { "cache", "12345678901234", 0, &values8[0] ,&values9[0] },
  88. { "rss", "23456789012345", 0, &values8[1] ,&values9[1] },
  89. { "rss_huge", "34567890123456", 0, &values8[2] ,&values9[2] },
  90. { "mapped_file", "45678901234567", 0, &values8[3] ,&values9[3] },
  91. { "writeback", "56789012345678", 0, &values8[4] ,&values9[4] },
  92. { "dirty", "67890123456789", 0, &values8[5] ,&values9[5] },
  93. { "swap", "78901234567890", 0, &values8[6] ,&values9[6] },
  94. { "pgpgin", "89012345678901", 0, &values8[7] ,&values9[7] },
  95. { "pgpgout", "90123456789012", 0, &values8[8] ,&values9[8] },
  96. { "pgfault", "10345678901234", 0, &values8[9] ,&values9[9] },
  97. { "pgmajfault", "11456789012345", 0, &values8[10] ,&values9[10] },
  98. { "inactive_anon", "12000000000000", 0, &values8[11] ,&values9[11] },
  99. { "active_anon", "13345678901234", 0, &values8[12] ,&values9[12] },
  100. { "inactive_file", "14345678901234", 0, &values8[13] ,&values9[13] },
  101. { "active_file", "15345678901234", 0, &values8[14] ,&values9[14] },
  102. { "unevictable", "16345678901234", 0, &values8[15] ,&values9[15] },
  103. { "hierarchical_memory_limit", "17345678901234", 0, &values8[16] ,&values9[16] },
  104. { "total_cache", "18345678901234", 0, &values8[17] ,&values9[17] },
  105. { "total_rss", "19345678901234", 0, &values8[18] ,&values9[18] },
  106. { "total_rss_huge", "20345678901234", 0, &values8[19] ,&values9[19] },
  107. { "total_mapped_file", "21345678901234", 0, &values8[20] ,&values9[20] },
  108. { "total_writeback", "22345678901234", 0, &values8[21] ,&values9[21] },
  109. { "total_dirty", "23000000000000", 0, &values8[22] ,&values9[22] },
  110. { "total_swap", "24345678901234", 0, &values8[23] ,&values9[23] },
  111. { "total_pgpgin", "25345678901234", 0, &values8[24] ,&values9[24] },
  112. { "total_pgpgout", "26345678901234", 0, &values8[25] ,&values9[25] },
  113. { "total_pgfault", "27345678901234", 0, &values8[26] ,&values9[26] },
  114. { "total_pgmajfault", "28345678901234", 0, &values8[27] ,&values9[27] },
  115. { "total_inactive_anon", "29345678901234", 0, &values8[28] ,&values9[28] },
  116. { "total_active_anon", "30345678901234", 0, &values8[29] ,&values9[29] },
  117. { "total_inactive_file", "31345678901234", 0, &values8[30] ,&values9[30] },
  118. { "total_active_file", "32345678901234", 0, &values8[31] ,&values9[31] },
  119. { "total_unevictable", "33345678901234", 0, &values8[32] ,&values9[32] },
  120. { NULL, NULL , 0, NULL ,NULL }
  121. };
  122. // simple system strcmp()
  123. void test1() {
  124. int i;
  125. for(i = 0; pairs[i].name ; i++) {
  126. const char *s = pairs[i].name;
  127. const char *v = pairs[i].value;
  128. if(unlikely(!strcmp(s, "cache")))
  129. values1[i] = strtoull(v, NULL, 10);
  130. else if(unlikely(!strcmp(s, "rss")))
  131. values1[i] = strtoull(v, NULL, 10);
  132. else if(unlikely(!strcmp(s, "rss_huge")))
  133. values1[i] = strtoull(v, NULL, 10);
  134. else if(unlikely(!strcmp(s, "mapped_file")))
  135. values1[i] = strtoull(v, NULL, 10);
  136. else if(unlikely(!strcmp(s, "writeback")))
  137. values1[i] = strtoull(v, NULL, 10);
  138. else if(unlikely(!strcmp(s, "dirty")))
  139. values1[i] = strtoull(v, NULL, 10);
  140. else if(unlikely(!strcmp(s, "swap")))
  141. values1[i] = strtoull(v, NULL, 10);
  142. else if(unlikely(!strcmp(s, "pgpgin")))
  143. values1[i] = strtoull(v, NULL, 10);
  144. else if(unlikely(!strcmp(s, "pgpgout")))
  145. values1[i] = strtoull(v, NULL, 10);
  146. else if(unlikely(!strcmp(s, "pgfault")))
  147. values1[i] = strtoull(v, NULL, 10);
  148. else if(unlikely(!strcmp(s, "pgmajfault")))
  149. values1[i] = strtoull(v, NULL, 10);
  150. }
  151. }
  152. // inline simple_hash() with system strtoull()
  153. void test2() {
  154. int i;
  155. for(i = 0; pairs[i].name ; i++) {
  156. const char *s = pairs[i].name;
  157. const char *v = pairs[i].value;
  158. uint32_t hash = simple_hash2(s);
  159. if(unlikely(hash == cache_hash && !strcmp(s, "cache")))
  160. values2[i] = strtoull(v, NULL, 10);
  161. else if(unlikely(hash == rss_hash && !strcmp(s, "rss")))
  162. values2[i] = strtoull(v, NULL, 10);
  163. else if(unlikely(hash == rss_huge_hash && !strcmp(s, "rss_huge")))
  164. values2[i] = strtoull(v, NULL, 10);
  165. else if(unlikely(hash == mapped_file_hash && !strcmp(s, "mapped_file")))
  166. values2[i] = strtoull(v, NULL, 10);
  167. else if(unlikely(hash == writeback_hash && !strcmp(s, "writeback")))
  168. values2[i] = strtoull(v, NULL, 10);
  169. else if(unlikely(hash == dirty_hash && !strcmp(s, "dirty")))
  170. values2[i] = strtoull(v, NULL, 10);
  171. else if(unlikely(hash == swap_hash && !strcmp(s, "swap")))
  172. values2[i] = strtoull(v, NULL, 10);
  173. else if(unlikely(hash == pgpgin_hash && !strcmp(s, "pgpgin")))
  174. values2[i] = strtoull(v, NULL, 10);
  175. else if(unlikely(hash == pgpgout_hash && !strcmp(s, "pgpgout")))
  176. values2[i] = strtoull(v, NULL, 10);
  177. else if(unlikely(hash == pgfault_hash && !strcmp(s, "pgfault")))
  178. values2[i] = strtoull(v, NULL, 10);
  179. else if(unlikely(hash == pgmajfault_hash && !strcmp(s, "pgmajfault")))
  180. values2[i] = strtoull(v, NULL, 10);
  181. }
  182. }
  183. // statement expression simple_hash(), system strtoull()
  184. void test3() {
  185. int i;
  186. for(i = 0; pairs[i].name ; i++) {
  187. const char *s = pairs[i].name;
  188. const char *v = pairs[i].value;
  189. uint32_t hash = simple_hash(s);
  190. if(unlikely(hash == cache_hash && !strcmp(s, "cache")))
  191. values3[i] = strtoull(v, NULL, 10);
  192. else if(unlikely(hash == rss_hash && !strcmp(s, "rss")))
  193. values3[i] = strtoull(v, NULL, 10);
  194. else if(unlikely(hash == rss_huge_hash && !strcmp(s, "rss_huge")))
  195. values3[i] = strtoull(v, NULL, 10);
  196. else if(unlikely(hash == mapped_file_hash && !strcmp(s, "mapped_file")))
  197. values3[i] = strtoull(v, NULL, 10);
  198. else if(unlikely(hash == writeback_hash && !strcmp(s, "writeback")))
  199. values3[i] = strtoull(v, NULL, 10);
  200. else if(unlikely(hash == dirty_hash && !strcmp(s, "dirty")))
  201. values3[i] = strtoull(v, NULL, 10);
  202. else if(unlikely(hash == swap_hash && !strcmp(s, "swap")))
  203. values3[i] = strtoull(v, NULL, 10);
  204. else if(unlikely(hash == pgpgin_hash && !strcmp(s, "pgpgin")))
  205. values3[i] = strtoull(v, NULL, 10);
  206. else if(unlikely(hash == pgpgout_hash && !strcmp(s, "pgpgout")))
  207. values3[i] = strtoull(v, NULL, 10);
  208. else if(unlikely(hash == pgfault_hash && !strcmp(s, "pgfault")))
  209. values3[i] = strtoull(v, NULL, 10);
  210. else if(unlikely(hash == pgmajfault_hash && !strcmp(s, "pgmajfault")))
  211. values3[i] = strtoull(v, NULL, 10);
  212. }
  213. }
  214. // inline simple_hash(), if-continue checks
  215. void test4() {
  216. int i;
  217. for(i = 0; pairs[i].name ; i++) {
  218. const char *s = pairs[i].name;
  219. const char *v = pairs[i].value;
  220. uint32_t hash = simple_hash2(s);
  221. if(unlikely(hash == cache_hash && !strcmp(s, "cache"))) {
  222. values4[i] = strtoull(v, NULL, 0);
  223. continue;
  224. }
  225. if(unlikely(hash == rss_hash && !strcmp(s, "rss"))) {
  226. values4[i] = strtoull(v, NULL, 0);
  227. continue;
  228. }
  229. if(unlikely(hash == rss_huge_hash && !strcmp(s, "rss_huge"))) {
  230. values4[i] = strtoull(v, NULL, 0);
  231. continue;
  232. }
  233. if(unlikely(hash == mapped_file_hash && !strcmp(s, "mapped_file"))) {
  234. values4[i] = strtoull(v, NULL, 0);
  235. continue;
  236. }
  237. if(unlikely(hash == writeback_hash && !strcmp(s, "writeback"))) {
  238. values4[i] = strtoull(v, NULL, 0);
  239. continue;
  240. }
  241. if(unlikely(hash == dirty_hash && !strcmp(s, "dirty"))) {
  242. values4[i] = strtoull(v, NULL, 0);
  243. continue;
  244. }
  245. if(unlikely(hash == swap_hash && !strcmp(s, "swap"))) {
  246. values4[i] = strtoull(v, NULL, 0);
  247. continue;
  248. }
  249. if(unlikely(hash == pgpgin_hash && !strcmp(s, "pgpgin"))) {
  250. values4[i] = strtoull(v, NULL, 0);
  251. continue;
  252. }
  253. if(unlikely(hash == pgpgout_hash && !strcmp(s, "pgpgout"))) {
  254. values4[i] = strtoull(v, NULL, 0);
  255. continue;
  256. }
  257. if(unlikely(hash == pgfault_hash && !strcmp(s, "pgfault"))) {
  258. values4[i] = strtoull(v, NULL, 0);
  259. continue;
  260. }
  261. if(unlikely(hash == pgmajfault_hash && !strcmp(s, "pgmajfault"))) {
  262. values4[i] = strtoull(v, NULL, 0);
  263. continue;
  264. }
  265. }
  266. }
  267. // inline simple_hash(), if-else-if-else-if (netdata default)
  268. void test5() {
  269. int i;
  270. for(i = 0; pairs[i].name ; i++) {
  271. const char *s = pairs[i].name;
  272. const char *v = pairs[i].value;
  273. uint32_t hash = simple_hash2(s);
  274. if(unlikely(hash == cache_hash && !strcmp(s, "cache")))
  275. values5[i] = fast_strtoull(v);
  276. else if(unlikely(hash == rss_hash && !strcmp(s, "rss")))
  277. values5[i] = fast_strtoull(v);
  278. else if(unlikely(hash == rss_huge_hash && !strcmp(s, "rss_huge")))
  279. values5[i] = fast_strtoull(v);
  280. else if(unlikely(hash == mapped_file_hash && !strcmp(s, "mapped_file")))
  281. values5[i] = fast_strtoull(v);
  282. else if(unlikely(hash == writeback_hash && !strcmp(s, "writeback")))
  283. values5[i] = fast_strtoull(v);
  284. else if(unlikely(hash == dirty_hash && !strcmp(s, "dirty")))
  285. values5[i] = fast_strtoull(v);
  286. else if(unlikely(hash == swap_hash && !strcmp(s, "swap")))
  287. values5[i] = fast_strtoull(v);
  288. else if(unlikely(hash == pgpgin_hash && !strcmp(s, "pgpgin")))
  289. values5[i] = fast_strtoull(v);
  290. else if(unlikely(hash == pgpgout_hash && !strcmp(s, "pgpgout")))
  291. values5[i] = fast_strtoull(v);
  292. else if(unlikely(hash == pgfault_hash && !strcmp(s, "pgfault")))
  293. values5[i] = fast_strtoull(v);
  294. else if(unlikely(hash == pgmajfault_hash && !strcmp(s, "pgmajfault")))
  295. values5[i] = fast_strtoull(v);
  296. }
  297. }
  298. // ----------------------------------------------------------------------------
  299. void arl_strtoull(const char *name, uint32_t hash, const char *value, void *dst) {
  300. (void)name;
  301. (void)hash;
  302. register unsigned long long *d = dst;
  303. *d = strtoull(value, NULL, 10);
  304. // fprintf(stderr, "name '%s' with hash %u and value '%s' is %llu\n", name, hash, value, *d);
  305. }
  306. void test6() {
  307. static ARL_BASE *base = NULL;
  308. if(unlikely(!base)) {
  309. base = arl_create("test6", arl_strtoull, 60);
  310. arl_expect_custom(base, "cache", NULL, &values6[0]);
  311. arl_expect_custom(base, "rss", NULL, &values6[1]);
  312. arl_expect_custom(base, "rss_huge", NULL, &values6[2]);
  313. arl_expect_custom(base, "mapped_file", NULL, &values6[3]);
  314. arl_expect_custom(base, "writeback", NULL, &values6[4]);
  315. arl_expect_custom(base, "dirty", NULL, &values6[5]);
  316. arl_expect_custom(base, "swap", NULL, &values6[6]);
  317. arl_expect_custom(base, "pgpgin", NULL, &values6[7]);
  318. arl_expect_custom(base, "pgpgout", NULL, &values6[8]);
  319. arl_expect_custom(base, "pgfault", NULL, &values6[9]);
  320. arl_expect_custom(base, "pgmajfault", NULL, &values6[10]);
  321. }
  322. arl_begin(base);
  323. int i;
  324. for(i = 0; pairs[i].name ; i++)
  325. if(arl_check(base, pairs[i].name, pairs[i].value)) break;
  326. }
  327. void arl_str2ull(const char *name, uint32_t hash, const char *value, void *dst) {
  328. (void)name;
  329. (void)hash;
  330. register unsigned long long *d = dst;
  331. *d = str2ull(value);
  332. // fprintf(stderr, "name '%s' with hash %u and value '%s' is %llu\n", name, hash, value, *d);
  333. }
  334. void test7() {
  335. static ARL_BASE *base = NULL;
  336. if(unlikely(!base)) {
  337. base = arl_create("test7", arl_str2ull, 60);
  338. arl_expect_custom(base, "cache", NULL, &values7[0]);
  339. arl_expect_custom(base, "rss", NULL, &values7[1]);
  340. arl_expect_custom(base, "rss_huge", NULL, &values7[2]);
  341. arl_expect_custom(base, "mapped_file", NULL, &values7[3]);
  342. arl_expect_custom(base, "writeback", NULL, &values7[4]);
  343. arl_expect_custom(base, "dirty", NULL, &values7[5]);
  344. arl_expect_custom(base, "swap", NULL, &values7[6]);
  345. arl_expect_custom(base, "pgpgin", NULL, &values7[7]);
  346. arl_expect_custom(base, "pgpgout", NULL, &values7[8]);
  347. arl_expect_custom(base, "pgfault", NULL, &values7[9]);
  348. arl_expect_custom(base, "pgmajfault", NULL, &values7[10]);
  349. }
  350. arl_begin(base);
  351. int i;
  352. for(i = 0; pairs[i].name ; i++)
  353. if(arl_check(base, pairs[i].name, pairs[i].value)) break;
  354. }
  355. void test8() {
  356. int i;
  357. for(i = 0; pairs[i].name; i++) {
  358. uint32_t hash = simple_hash(pairs[i].name);
  359. int j;
  360. for(j = 0; pairs[j].name; j++) {
  361. if(hash == pairs[j].hash && !strcmp(pairs[i].name, pairs[j].name)) {
  362. *pairs[j].collected8 = strtoull(pairs[i].value, NULL, 10);
  363. break;
  364. }
  365. }
  366. }
  367. }
  368. void test9() {
  369. int i;
  370. for(i = 0; pairs[i].name; i++) {
  371. uint32_t hash = simple_hash(pairs[i].name);
  372. int j;
  373. for(j = 0; pairs[j].name; j++) {
  374. if(hash == pairs[j].hash && !strcmp(pairs[i].name, pairs[j].name)) {
  375. *pairs[j].collected9 = str2ull(pairs[i].value);
  376. break;
  377. }
  378. }
  379. }
  380. }
  381. // ----------------------------------------------------------------------------
  382. /*
  383. // ==============
  384. // --- Poor man cycle counting.
  385. static unsigned long tsc;
  386. static void begin_tsc(void)
  387. {
  388. unsigned long a, d;
  389. asm volatile ("cpuid\nrdtsc" : "=a" (a), "=d" (d) : "0" (0) : "ebx", "ecx");
  390. tsc = ((unsigned long)d << 32) | (unsigned long)a;
  391. }
  392. static unsigned long end_tsc(void)
  393. {
  394. unsigned long a, d;
  395. asm volatile ("rdtscp" : "=a" (a), "=d" (d) : : "ecx");
  396. return (((unsigned long)d << 32) | (unsigned long)a) - tsc;
  397. }
  398. // ===============
  399. */
  400. static unsigned long long clk;
  401. static void begin_clock() {
  402. struct timeval tv;
  403. if(unlikely(gettimeofday(&tv, NULL) == -1))
  404. return;
  405. clk = tv.tv_sec * 1000000 + tv.tv_usec;
  406. }
  407. static unsigned long long end_clock() {
  408. struct timeval tv;
  409. if(unlikely(gettimeofday(&tv, NULL) == -1))
  410. return -1;
  411. return clk = tv.tv_sec * 1000000 + tv.tv_usec - clk;
  412. }
  413. int main(void)
  414. {
  415. {
  416. int i;
  417. for(i = 0; pairs[i].name; i++)
  418. pairs[i].hash = simple_hash(pairs[i].name);
  419. }
  420. cache_hash = simple_hash("cache");
  421. rss_hash = simple_hash("rss");
  422. rss_huge_hash = simple_hash("rss_huge");
  423. mapped_file_hash = simple_hash("mapped_file");
  424. writeback_hash = simple_hash("writeback");
  425. dirty_hash = simple_hash("dirty");
  426. swap_hash = simple_hash("swap");
  427. pgpgin_hash = simple_hash("pgpgin");
  428. pgpgout_hash = simple_hash("pgpgout");
  429. pgfault_hash = simple_hash("pgfault");
  430. pgmajfault_hash = simple_hash("pgmajfault");
  431. inactive_anon_hash = simple_hash("inactive_anon");
  432. active_anon_hash = simple_hash("active_anon");
  433. inactive_file_hash = simple_hash("inactive_file");
  434. active_file_hash = simple_hash("active_file");
  435. unevictable_hash = simple_hash("unevictable");
  436. hierarchical_memory_limit_hash = simple_hash("hierarchical_memory_limit");
  437. total_cache_hash = simple_hash("total_cache");
  438. total_rss_hash = simple_hash("total_rss");
  439. total_rss_huge_hash = simple_hash("total_rss_huge");
  440. total_mapped_file_hash = simple_hash("total_mapped_file");
  441. total_writeback_hash = simple_hash("total_writeback");
  442. total_dirty_hash = simple_hash("total_dirty");
  443. total_swap_hash = simple_hash("total_swap");
  444. total_pgpgin_hash = simple_hash("total_pgpgin");
  445. total_pgpgout_hash = simple_hash("total_pgpgout");
  446. total_pgfault_hash = simple_hash("total_pgfault");
  447. total_pgmajfault_hash = simple_hash("total_pgmajfault");
  448. total_inactive_anon_hash = simple_hash("total_inactive_anon");
  449. total_active_anon_hash = simple_hash("total_active_anon");
  450. total_inactive_file_hash = simple_hash("total_inactive_file");
  451. total_active_file_hash = simple_hash("total_active_file");
  452. total_unevictable_hash = simple_hash("total_unevictable");
  453. // cache functions
  454. (void)simple_hash2("hello world");
  455. (void)strcmp("1", "2");
  456. (void)strtoull("123", NULL, 0);
  457. unsigned long i, c1 = 0, c2 = 0, c3 = 0, c4 = 0, c5 = 0, c6 = 0, c7 = 0, c8 = 0, c9 = 0;
  458. unsigned long max = 1000000;
  459. begin_clock();
  460. for(i = 0; i <= max ;i++) test1();
  461. c1 = end_clock();
  462. begin_clock();
  463. for(i = 0; i <= max ;i++) test2();
  464. c2 = end_clock();
  465. begin_clock();
  466. for(i = 0; i <= max ;i++) test3();
  467. c3 = end_clock();
  468. begin_clock();
  469. for(i = 0; i <= max ;i++) test4();
  470. c4 = end_clock();
  471. begin_clock();
  472. for(i = 0; i <= max ;i++) test5();
  473. c5 = end_clock();
  474. begin_clock();
  475. for(i = 0; i <= max ;i++) test6();
  476. c6 = end_clock();
  477. begin_clock();
  478. for(i = 0; i <= max ;i++) test7();
  479. c7 = end_clock();
  480. begin_clock();
  481. for(i = 0; i <= max ;i++) test8();
  482. c8 = end_clock();
  483. begin_clock();
  484. for(i = 0; i <= max ;i++) test9();
  485. c9 = end_clock();
  486. for(i = 0; i < 11 ; i++)
  487. printf("value %lu: %llu %llu %llu %llu %llu %llu %llu %llu %llu\n", i, values1[i], values2[i], values3[i], values4[i], values5[i], values6[i], values7[i], values8[i], values9[i]);
  488. printf("\n\nRESULTS\n");
  489. printf("test1() [1] in %lu usecs: simple system strcmp().\n"
  490. "test2() [4] in %lu usecs: inline simple_hash() with system strtoull().\n"
  491. "test3() [5] in %lu usecs: statement expression simple_hash(), system strtoull().\n"
  492. "test4() [6] in %lu usecs: inline simple_hash(), if-continue checks.\n"
  493. "test5() [7] in %lu usecs: inline simple_hash(), if-else-if-else-if (netdata default prior to ARL).\n"
  494. "test6() [8] in %lu usecs: adaptive re-sortable array with strtoull() (wow!)\n"
  495. "test7() [9] in %lu usecs: adaptive re-sortable array with str2ull() (wow!)\n"
  496. "test8() [2] in %lu usecs: nested loop with strtoull()\n"
  497. "test9() [3] in %lu usecs: nested loop with str2ull()\n"
  498. , c1
  499. , c2
  500. , c3
  501. , c4
  502. , c5
  503. , c6
  504. , c7
  505. , c8
  506. , c9
  507. );
  508. return 0;
  509. }