| 1 | /* SPDX-License-Identifier: GPL-3.0-or-later */ |
| 2 | #include <stdio.h> |
| 3 | #include <inttypes.h> |
| 4 | #include <string.h> |
| 5 | #include <stdlib.h> |
| 6 | #include <ctype.h> |
| 7 | #include <sys/time.h> |
| 8 | |
| 9 | #define likely(x) __builtin_expect(!!(x), 1) |
| 10 | #define unlikely(x) __builtin_expect(!!(x), 0) |
| 11 | |
| 12 | #define simple_hash(name) ({ \ |
| 13 | register unsigned char *__hash_source = (unsigned char *)(name); \ |
| 14 | register uint32_t __hash_value = 0x811c9dc5; \ |
| 15 | while (*__hash_source) { \ |
| 16 | __hash_value *= 16777619; \ |
| 17 | __hash_value ^= (uint32_t) *__hash_source++; \ |
| 18 | } \ |
| 19 | __hash_value; \ |
| 20 | }) |
| 21 | |
| 22 | static inline uint32_t simple_hash2(const char *name) { |
| 23 | register unsigned char *s = (unsigned char *)name; |
| 24 | register uint32_t hval = 0x811c9dc5; |
| 25 | while (*s) { |
| 26 | hval *= 16777619; |
| 27 | hval ^= (uint32_t) *s++; |
| 28 | } |
| 29 | return hval; |
| 30 | } |
| 31 | |
| 32 | static inline unsigned long long fast_strtoull(const char *s) { |
| 33 | register unsigned long long n = 0; |
| 34 | register char c; |
| 35 | for(c = *s; c >= '0' && c <= '9' ; c = *(++s)) { |
| 36 | n *= 10; |
| 37 | n += c - '0'; |
| 38 | // n = (n << 1) + (n << 3) + (c - '0'); |
| 39 | } |
| 40 | return n; |
| 41 | } |
| 42 | |
| 43 | static uint32_t cache_hash = 0; |
| 44 | static uint32_t rss_hash = 0; |
| 45 | static uint32_t rss_huge_hash = 0; |
| 46 | static uint32_t mapped_file_hash = 0; |
| 47 | static uint32_t writeback_hash = 0; |
| 48 | static uint32_t dirty_hash = 0; |
| 49 | static uint32_t swap_hash = 0; |
| 50 | static uint32_t pgpgin_hash = 0; |
| 51 | static uint32_t pgpgout_hash = 0; |
| 52 | static uint32_t pgfault_hash = 0; |
| 53 | static uint32_t pgmajfault_hash = 0; |
| 54 | static uint32_t inactive_anon_hash = 0; |
| 55 | static uint32_t active_anon_hash = 0; |
| 56 | static uint32_t inactive_file_hash = 0; |
| 57 | static uint32_t active_file_hash = 0; |
| 58 | static uint32_t unevictable_hash = 0; |
| 59 | static uint32_t hierarchical_memory_limit_hash = 0; |
| 60 | static uint32_t total_cache_hash = 0; |
| 61 | static uint32_t total_rss_hash = 0; |
| 62 | static uint32_t total_rss_huge_hash = 0; |
| 63 | static uint32_t total_mapped_file_hash = 0; |
| 64 | static uint32_t total_writeback_hash = 0; |
| 65 | static uint32_t total_dirty_hash = 0; |
| 66 | static uint32_t total_swap_hash = 0; |
| 67 | static uint32_t total_pgpgin_hash = 0; |
| 68 | static uint32_t total_pgpgout_hash = 0; |
| 69 | static uint32_t total_pgfault_hash = 0; |
| 70 | static uint32_t total_pgmajfault_hash = 0; |
| 71 | static uint32_t total_inactive_anon_hash = 0; |
| 72 | static uint32_t total_active_anon_hash = 0; |
| 73 | static uint32_t total_inactive_file_hash = 0; |
| 74 | static uint32_t total_active_file_hash = 0; |
| 75 | static uint32_t total_unevictable_hash = 0; |
| 76 | |
| 77 | char *strings[] = { |
| 78 | "cache", |
| 79 | "rss", |
| 80 | "rss_huge", |
| 81 | "mapped_file", |
| 82 | "writeback", |
| 83 | "dirty", |
| 84 | "swap", |
| 85 | "pgpgin", |
| 86 | "pgpgout", |
| 87 | "pgfault", |
| 88 | "pgmajfault", |
| 89 | "inactive_anon", |
| 90 | "active_anon", |
| 91 | "inactive_file", |
| 92 | "active_file", |
| 93 | "unevictable", |
| 94 | "hierarchical_memory_limit", |
| 95 | "total_cache", |
| 96 | "total_rss", |
| 97 | "total_rss_huge", |
| 98 | "total_mapped_file", |
| 99 | "total_writeback", |
| 100 | "total_dirty", |
| 101 | "total_swap", |
| 102 | "total_pgpgin", |
| 103 | "total_pgpgout", |
| 104 | "total_pgfault", |
| 105 | "total_pgmajfault", |
| 106 | "total_inactive_anon", |
| 107 | "total_active_anon", |
| 108 | "total_inactive_file", |
| 109 | "total_active_file", |
| 110 | "total_unevictable", |
| 111 | NULL |
| 112 | }; |
| 113 | |
| 114 | unsigned long long values1[12] = { 0 }; |
| 115 | unsigned long long values2[12] = { 0 }; |
| 116 | unsigned long long values3[12] = { 0 }; |
| 117 | unsigned long long values4[12] = { 0 }; |
| 118 | unsigned long long values5[12] = { 0 }; |
| 119 | unsigned long long values6[12] = { 0 }; |
| 120 | |
| 121 | #define NUMBER1 "12345678901234" |
| 122 | #define NUMBER2 "23456789012345" |
| 123 | #define NUMBER3 "34567890123456" |
| 124 | #define NUMBER4 "45678901234567" |
| 125 | #define NUMBER5 "56789012345678" |
| 126 | #define NUMBER6 "67890123456789" |
| 127 | #define NUMBER7 "78901234567890" |
| 128 | #define NUMBER8 "89012345678901" |
| 129 | #define NUMBER9 "90123456789012" |
| 130 | #define NUMBER10 "12345678901234" |
| 131 | #define NUMBER11 "23456789012345" |
| 132 | |
| 133 | // simple system strcmp() |
| 134 | void test1() { |
| 135 | int i; |
| 136 | for(i = 0; strings[i] ; i++) { |
| 137 | char *s = strings[i]; |
| 138 | |
| 139 | if(unlikely(!strcmp(s, "cache"))) |
| 140 | values1[i] = strtoull(NUMBER1, NULL, 10); |
| 141 | |
| 142 | else if(unlikely(!strcmp(s, "rss"))) |
| 143 | values1[i] = strtoull(NUMBER2, NULL, 10); |
| 144 | |
| 145 | else if(unlikely(!strcmp(s, "rss_huge"))) |
| 146 | values1[i] = strtoull(NUMBER3, NULL, 10); |
| 147 | |
| 148 | else if(unlikely(!strcmp(s, "mapped_file"))) |
| 149 | values1[i] = strtoull(NUMBER4, NULL, 10); |
| 150 | |
| 151 | else if(unlikely(!strcmp(s, "writeback"))) |
| 152 | values1[i] = strtoull(NUMBER5, NULL, 10); |
| 153 | |
| 154 | else if(unlikely(!strcmp(s, "dirty"))) |
| 155 | values1[i] = strtoull(NUMBER6, NULL, 10); |
| 156 | |
| 157 | else if(unlikely(!strcmp(s, "swap"))) |
| 158 | values1[i] = strtoull(NUMBER7, NULL, 10); |
| 159 | |
| 160 | else if(unlikely(!strcmp(s, "pgpgin"))) |
| 161 | values1[i] = strtoull(NUMBER8, NULL, 10); |
| 162 | |
| 163 | else if(unlikely(!strcmp(s, "pgpgout"))) |
| 164 | values1[i] = strtoull(NUMBER9, NULL, 10); |
| 165 | |
| 166 | else if(unlikely(!strcmp(s, "pgfault"))) |
| 167 | values1[i] = strtoull(NUMBER10, NULL, 10); |
| 168 | |
| 169 | else if(unlikely(!strcmp(s, "pgmajfault"))) |
| 170 | values1[i] = strtoull(NUMBER11, NULL, 10); |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | // inline simple_hash() with system strtoull() |
| 175 | void test2() { |
| 176 | int i; |
| 177 | for(i = 0; strings[i] ; i++) { |
| 178 | char *s = strings[i]; |
| 179 | uint32_t hash = simple_hash2(s); |
| 180 | |
| 181 | if(unlikely(hash == cache_hash && !strcmp(s, "cache"))) |
| 182 | values2[i] = strtoull(NUMBER1, NULL, 10); |
| 183 | |
| 184 | else if(unlikely(hash == rss_hash && !strcmp(s, "rss"))) |
| 185 | values2[i] = strtoull(NUMBER2, NULL, 10); |
| 186 | |
| 187 | else if(unlikely(hash == rss_huge_hash && !strcmp(s, "rss_huge"))) |
| 188 | values2[i] = strtoull(NUMBER3, NULL, 10); |
| 189 | |
| 190 | else if(unlikely(hash == mapped_file_hash && !strcmp(s, "mapped_file"))) |
| 191 | values2[i] = strtoull(NUMBER4, NULL, 10); |
| 192 | |
| 193 | else if(unlikely(hash == writeback_hash && !strcmp(s, "writeback"))) |
| 194 | values2[i] = strtoull(NUMBER5, NULL, 10); |
| 195 | |
| 196 | else if(unlikely(hash == dirty_hash && !strcmp(s, "dirty"))) |
| 197 | values2[i] = strtoull(NUMBER6, NULL, 10); |
| 198 | |
| 199 | else if(unlikely(hash == swap_hash && !strcmp(s, "swap"))) |
| 200 | values2[i] = strtoull(NUMBER7, NULL, 10); |
| 201 | |
| 202 | else if(unlikely(hash == pgpgin_hash && !strcmp(s, "pgpgin"))) |
| 203 | values2[i] = strtoull(NUMBER8, NULL, 10); |
| 204 | |
| 205 | else if(unlikely(hash == pgpgout_hash && !strcmp(s, "pgpgout"))) |
| 206 | values2[i] = strtoull(NUMBER9, NULL, 10); |
| 207 | |
| 208 | else if(unlikely(hash == pgfault_hash && !strcmp(s, "pgfault"))) |
| 209 | values2[i] = strtoull(NUMBER10, NULL, 10); |
| 210 | |
| 211 | else if(unlikely(hash == pgmajfault_hash && !strcmp(s, "pgmajfault"))) |
| 212 | values2[i] = strtoull(NUMBER11, NULL, 10); |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | // statement expression simple_hash(), system strtoull() |
| 217 | void test3() { |
| 218 | int i; |
| 219 | for(i = 0; strings[i] ; i++) { |
| 220 | char *s = strings[i]; |
| 221 | uint32_t hash = simple_hash(s); |
| 222 | |
| 223 | if(unlikely(hash == cache_hash && !strcmp(s, "cache"))) |
| 224 | values3[i] = strtoull(NUMBER1, NULL, 10); |
| 225 | |
| 226 | else if(unlikely(hash == rss_hash && !strcmp(s, "rss"))) |
| 227 | values3[i] = strtoull(NUMBER2, NULL, 10); |
| 228 | |
| 229 | else if(unlikely(hash == rss_huge_hash && !strcmp(s, "rss_huge"))) |
| 230 | values3[i] = strtoull(NUMBER3, NULL, 10); |
| 231 | |
| 232 | else if(unlikely(hash == mapped_file_hash && !strcmp(s, "mapped_file"))) |
| 233 | values3[i] = strtoull(NUMBER4, NULL, 10); |
| 234 | |
| 235 | else if(unlikely(hash == writeback_hash && !strcmp(s, "writeback"))) |
| 236 | values3[i] = strtoull(NUMBER5, NULL, 10); |
| 237 | |
| 238 | else if(unlikely(hash == dirty_hash && !strcmp(s, "dirty"))) |
| 239 | values3[i] = strtoull(NUMBER6, NULL, 10); |
| 240 | |
| 241 | else if(unlikely(hash == swap_hash && !strcmp(s, "swap"))) |
| 242 | values3[i] = strtoull(NUMBER7, NULL, 10); |
| 243 | |
| 244 | else if(unlikely(hash == pgpgin_hash && !strcmp(s, "pgpgin"))) |
| 245 | values3[i] = strtoull(NUMBER8, NULL, 10); |
| 246 | |
| 247 | else if(unlikely(hash == pgpgout_hash && !strcmp(s, "pgpgout"))) |
| 248 | values3[i] = strtoull(NUMBER9, NULL, 10); |
| 249 | |
| 250 | else if(unlikely(hash == pgfault_hash && !strcmp(s, "pgfault"))) |
| 251 | values3[i] = strtoull(NUMBER10, NULL, 10); |
| 252 | |
| 253 | else if(unlikely(hash == pgmajfault_hash && !strcmp(s, "pgmajfault"))) |
| 254 | values3[i] = strtoull(NUMBER11, NULL, 10); |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | |
| 259 | // inline simple_hash(), if-continue checks |
| 260 | void test4() { |
| 261 | int i; |
| 262 | for(i = 0; strings[i] ; i++) { |
| 263 | char *s = strings[i]; |
| 264 | uint32_t hash = simple_hash2(s); |
| 265 | |
| 266 | if(unlikely(hash == cache_hash && !strcmp(s, "cache"))) { |
| 267 | values4[i] = strtoull(NUMBER1, NULL, 0); |
| 268 | continue; |
| 269 | } |
| 270 | |
| 271 | if(unlikely(hash == rss_hash && !strcmp(s, "rss"))) { |
| 272 | values4[i] = strtoull(NUMBER2, NULL, 0); |
| 273 | continue; |
| 274 | } |
| 275 | |
| 276 | if(unlikely(hash == rss_huge_hash && !strcmp(s, "rss_huge"))) { |
| 277 | values4[i] = strtoull(NUMBER3, NULL, 0); |
| 278 | continue; |
| 279 | } |
| 280 | |
| 281 | if(unlikely(hash == mapped_file_hash && !strcmp(s, "mapped_file"))) { |
| 282 | values4[i] = strtoull(NUMBER4, NULL, 0); |
| 283 | continue; |
| 284 | } |
| 285 | |
| 286 | if(unlikely(hash == writeback_hash && !strcmp(s, "writeback"))) { |
| 287 | values4[i] = strtoull(NUMBER5, NULL, 0); |
| 288 | continue; |
| 289 | } |
| 290 | |
| 291 | if(unlikely(hash == dirty_hash && !strcmp(s, "dirty"))) { |
| 292 | values4[i] = strtoull(NUMBER6, NULL, 0); |
| 293 | continue; |
| 294 | } |
| 295 | |
| 296 | if(unlikely(hash == swap_hash && !strcmp(s, "swap"))) { |
| 297 | values4[i] = strtoull(NUMBER7, NULL, 0); |
| 298 | continue; |
| 299 | } |
| 300 | |
| 301 | if(unlikely(hash == pgpgin_hash && !strcmp(s, "pgpgin"))) { |
| 302 | values4[i] = strtoull(NUMBER8, NULL, 0); |
| 303 | continue; |
| 304 | } |
| 305 | |
| 306 | if(unlikely(hash == pgpgout_hash && !strcmp(s, "pgpgout"))) { |
| 307 | values4[i] = strtoull(NUMBER9, NULL, 0); |
| 308 | continue; |
| 309 | } |
| 310 | |
| 311 | if(unlikely(hash == pgfault_hash && !strcmp(s, "pgfault"))) { |
| 312 | values4[i] = strtoull(NUMBER10, NULL, 0); |
| 313 | continue; |
| 314 | } |
| 315 | |
| 316 | if(unlikely(hash == pgmajfault_hash && !strcmp(s, "pgmajfault"))) { |
| 317 | values4[i] = strtoull(NUMBER11, NULL, 0); |
| 318 | continue; |
| 319 | } |
| 320 | } |
| 321 | } |
| 322 | |
| 323 | // inline simple_hash(), if-else-if-else-if (netdata default) |
| 324 | void test5() { |
| 325 | int i; |
| 326 | for(i = 0; strings[i] ; i++) { |
| 327 | char *s = strings[i]; |
| 328 | uint32_t hash = simple_hash2(s); |
| 329 | |
| 330 | if(unlikely(hash == cache_hash && !strcmp(s, "cache"))) |
| 331 | values5[i] = fast_strtoull(NUMBER1); |
| 332 | |
| 333 | else if(unlikely(hash == rss_hash && !strcmp(s, "rss"))) |
| 334 | values5[i] = fast_strtoull(NUMBER2); |
| 335 | |
| 336 | else if(unlikely(hash == rss_huge_hash && !strcmp(s, "rss_huge"))) |
| 337 | values5[i] = fast_strtoull(NUMBER3); |
| 338 | |
| 339 | else if(unlikely(hash == mapped_file_hash && !strcmp(s, "mapped_file"))) |
| 340 | values5[i] = fast_strtoull(NUMBER4); |
| 341 | |
| 342 | else if(unlikely(hash == writeback_hash && !strcmp(s, "writeback"))) |
| 343 | values5[i] = fast_strtoull(NUMBER5); |
| 344 | |
| 345 | else if(unlikely(hash == dirty_hash && !strcmp(s, "dirty"))) |
| 346 | values5[i] = fast_strtoull(NUMBER6); |
| 347 | |
| 348 | else if(unlikely(hash == swap_hash && !strcmp(s, "swap"))) |
| 349 | values5[i] = fast_strtoull(NUMBER7); |
| 350 | |
| 351 | else if(unlikely(hash == pgpgin_hash && !strcmp(s, "pgpgin"))) |
| 352 | values5[i] = fast_strtoull(NUMBER8); |
| 353 | |
| 354 | else if(unlikely(hash == pgpgout_hash && !strcmp(s, "pgpgout"))) |
| 355 | values5[i] = fast_strtoull(NUMBER9); |
| 356 | |
| 357 | else if(unlikely(hash == pgfault_hash && !strcmp(s, "pgfault"))) |
| 358 | values5[i] = fast_strtoull(NUMBER10); |
| 359 | |
| 360 | else if(unlikely(hash == pgmajfault_hash && !strcmp(s, "pgmajfault"))) |
| 361 | values5[i] = fast_strtoull(NUMBER11); |
| 362 | } |
| 363 | } |
| 364 | |
| 365 | // ---------------------------------------------------------------------------- |
| 366 | |
| 367 | struct entry { |
| 368 | char *name; |
| 369 | uint32_t hash; |
| 370 | int found; |
| 371 | void (*func)(void *data1, void *data2); |
| 372 | void *data1; |
| 373 | void *data2; |
| 374 | struct entry *prev, *next; |
| 375 | }; |
| 376 | |
| 377 | struct base { |
| 378 | int iteration; |
| 379 | int registered; |
| 380 | int wanted; |
| 381 | int found; |
| 382 | struct entry *entries, *last; |
| 383 | }; |
| 384 | |
| 385 | static inline void callback(void *data1, void *data2) { |
| 386 | char *string = data1; |
| 387 | unsigned long long *value = data2; |
| 388 | *value = fast_strtoull(string); |
| 389 | } |
| 390 | |
| 391 | static inline void callback_system_strtoull(void *data1, void *data2) { |
| 392 | char *string = data1; |
| 393 | unsigned long long *value = data2; |
| 394 | *value = strtoull(string, NULL, 10); |
| 395 | } |
| 396 | |
| 397 | |
| 398 | static inline struct base *entry(struct base *base, const char *name, void *data1, void *data2, void (*func)(void *, void *)) { |
| 399 | if(!base) |
| 400 | base = calloc(1, sizeof(struct base)); |
| 401 | |
| 402 | struct entry *e = malloc(sizeof(struct entry)); |
| 403 | e->name = strdup(name); |
| 404 | e->hash = simple_hash2(e->name); |
| 405 | e->data1 = data1; |
| 406 | e->data2 = data2; |
| 407 | e->func = func; |
| 408 | e->prev = NULL; |
| 409 | e->next = base->entries; |
| 410 | |
| 411 | if(base->entries) base->entries->prev = e; |
| 412 | else base->last = e; |
| 413 | |
| 414 | base->entries = e; |
| 415 | base->registered++; |
| 416 | base->wanted = base->registered; |
| 417 | |
| 418 | return base; |
| 419 | } |
| 420 | |
| 421 | static inline int check(struct base *base, const char *s) { |
| 422 | uint32_t hash = simple_hash2(s); |
| 423 | |
| 424 | if(likely(!strcmp(s, base->last->name))) { |
| 425 | base->last->found = 1; |
| 426 | base->found++; |
| 427 | if(base->last->func) base->last->func(base->last->data1, base->last->data2); |
| 428 | base->last = base->last->next; |
| 429 | |
| 430 | if(!base->last) |
| 431 | base->last = base->entries; |
| 432 | |
| 433 | if(base->found == base->registered) |
| 434 | return 1; |
| 435 | |
| 436 | return 0; |
| 437 | } |
| 438 | |
| 439 | // find it |
| 440 | struct entry *e; |
| 441 | for(e = base->entries; e ; e = e->next) |
| 442 | if(e->hash == hash && !strcmp(e->name, s)) |
| 443 | break; |
| 444 | |
| 445 | if(e == base->last) { |
| 446 | printf("ERROR\n"); |
| 447 | exit(1); |
| 448 | } |
| 449 | |
| 450 | if(e) { |
| 451 | // found |
| 452 | |
| 453 | // run it |
| 454 | if(e->func) e->func(e->data1, e->data2); |
| 455 | |
| 456 | // unlink it |
| 457 | if(e->next) e->next->prev = e->prev; |
| 458 | if(e->prev) e->prev->next = e->next; |
| 459 | |
| 460 | if(base->entries == e) |
| 461 | base->entries = e->next; |
| 462 | } |
| 463 | else { |
| 464 | // not found |
| 465 | |
| 466 | // create it |
| 467 | e = calloc(1, sizeof(struct entry)); |
| 468 | e->name = strdup(s); |
| 469 | e->hash = hash; |
| 470 | } |
| 471 | |
| 472 | // link it here |
| 473 | e->next = base->last; |
| 474 | if(base->last) { |
| 475 | e->prev = base->last->prev; |
| 476 | base->last->prev = e; |
| 477 | |
| 478 | if(base->entries == base->last) |
| 479 | base->entries = e; |
| 480 | } |
| 481 | else |
| 482 | e->prev = NULL; |
| 483 | |
| 484 | if(e->prev) |
| 485 | e->prev->next = e; |
| 486 | |
| 487 | base->last = e->next; |
| 488 | if(!base->last) |
| 489 | base->last = base->entries; |
| 490 | |
| 491 | e->found = 1; |
| 492 | base->found++; |
| 493 | |
| 494 | if(base->found == base->registered) |
| 495 | return 1; |
| 496 | |
| 497 | printf("relinked '%s' after '%s' and before '%s': ", e->name, e->prev?e->prev->name:"NONE", e->next?e->next->name:"NONE"); |
| 498 | for(e = base->entries; e ; e = e->next) printf("%s ", e->name); |
| 499 | printf("\n"); |
| 500 | |
| 501 | return 0; |
| 502 | } |
| 503 | |
| 504 | static inline void begin(struct base *base) { |
| 505 | |
| 506 | if(unlikely(base->iteration % 60) == 1) { |
| 507 | base->wanted = 0; |
| 508 | struct entry *e; |
| 509 | for(e = base->entries; e ; e = e->next) |
| 510 | if(e->found) base->wanted++; |
| 511 | } |
| 512 | |
| 513 | base->iteration++; |
| 514 | base->last = base->entries; |
| 515 | base->found = 0; |
| 516 | } |
| 517 | |
| 518 | void test6() { |
| 519 | |
| 520 | static struct base *base = NULL; |
| 521 | |
| 522 | if(unlikely(!base)) { |
| 523 | base = entry(base, "cache", NUMBER1, &values6[0], callback_system_strtoull); |
| 524 | base = entry(base, "rss", NUMBER2, &values6[1], callback_system_strtoull); |
| 525 | base = entry(base, "rss_huge", NUMBER3, &values6[2], callback_system_strtoull); |
| 526 | base = entry(base, "mapped_file", NUMBER4, &values6[3], callback_system_strtoull); |
| 527 | base = entry(base, "writeback", NUMBER5, &values6[4], callback_system_strtoull); |
| 528 | base = entry(base, "dirty", NUMBER6, &values6[5], callback_system_strtoull); |
| 529 | base = entry(base, "swap", NUMBER7, &values6[6], callback_system_strtoull); |
| 530 | base = entry(base, "pgpgin", NUMBER8, &values6[7], callback_system_strtoull); |
| 531 | base = entry(base, "pgpgout", NUMBER9, &values6[8], callback_system_strtoull); |
| 532 | base = entry(base, "pgfault", NUMBER10, &values6[9], callback_system_strtoull); |
| 533 | base = entry(base, "pgmajfault", NUMBER11, &values6[10], callback_system_strtoull); |
| 534 | } |
| 535 | |
| 536 | begin(base); |
| 537 | |
| 538 | int i; |
| 539 | for(i = 0; strings[i] ; i++) { |
| 540 | if(check(base, strings[i])) |
| 541 | break; |
| 542 | } |
| 543 | } |
| 544 | |
| 545 | void test7() { |
| 546 | |
| 547 | static struct base *base = NULL; |
| 548 | |
| 549 | if(unlikely(!base)) { |
| 550 | base = entry(base, "cache", NUMBER1, &values6[0], callback); |
| 551 | base = entry(base, "rss", NUMBER2, &values6[1], callback); |
| 552 | base = entry(base, "rss_huge", NUMBER3, &values6[2], callback); |
| 553 | base = entry(base, "mapped_file", NUMBER4, &values6[3], callback); |
| 554 | base = entry(base, "writeback", NUMBER5, &values6[4], callback); |
| 555 | base = entry(base, "dirty", NUMBER6, &values6[5], callback); |
| 556 | base = entry(base, "swap", NUMBER7, &values6[6], callback); |
| 557 | base = entry(base, "pgpgin", NUMBER8, &values6[7], callback); |
| 558 | base = entry(base, "pgpgout", NUMBER9, &values6[8], callback); |
| 559 | base = entry(base, "pgfault", NUMBER10, &values6[9], callback); |
| 560 | base = entry(base, "pgmajfault", NUMBER11, &values6[10], callback); |
| 561 | } |
| 562 | |
| 563 | begin(base); |
| 564 | |
| 565 | int i; |
| 566 | for(i = 0; strings[i] ; i++) { |
| 567 | if(check(base, strings[i])) |
| 568 | break; |
| 569 | } |
| 570 | } |
| 571 | |
| 572 | // ---------------------------------------------------------------------------- |
| 573 | |
| 574 | |
| 575 | // ============== |
| 576 | // --- Poor man cycle counting. |
| 577 | static unsigned long tsc; |
| 578 | |
| 579 | static void begin_tsc(void) |
| 580 | { |
| 581 | unsigned long a, d; |
| 582 | asm volatile ("cpuid\nrdtsc" : "=a" (a), "=d" (d) : "0" (0) : "ebx", "ecx"); |
| 583 | tsc = ((unsigned long)d << 32) | (unsigned long)a; |
| 584 | } |
| 585 | |
| 586 | static unsigned long end_tsc(void) |
| 587 | { |
| 588 | unsigned long a, d; |
| 589 | asm volatile ("rdtscp" : "=a" (a), "=d" (d) : : "ecx"); |
| 590 | return (((unsigned long)d << 32) | (unsigned long)a) - tsc; |
| 591 | } |
| 592 | // =============== |
| 593 | |
| 594 | static unsigned long long clk; |
| 595 | |
| 596 | static void begin_clock() { |
| 597 | struct timeval tv; |
| 598 | if(unlikely(gettimeofday(&tv, NULL) == -1)) |
| 599 | return; |
| 600 | clk = tv.tv_sec * 1000000 + tv.tv_usec; |
| 601 | } |
| 602 | |
| 603 | static unsigned long long end_clock() { |
| 604 | struct timeval tv; |
| 605 | if(unlikely(gettimeofday(&tv, NULL) == -1)) |
| 606 | return -1; |
| 607 | return clk = tv.tv_sec * 1000000 + tv.tv_usec - clk; |
| 608 | } |
| 609 | |
| 610 | void main(void) |
| 611 | { |
| 612 | cache_hash = simple_hash("cache"); |
| 613 | rss_hash = simple_hash("rss"); |
| 614 | rss_huge_hash = simple_hash("rss_huge"); |
| 615 | mapped_file_hash = simple_hash("mapped_file"); |
| 616 | writeback_hash = simple_hash("writeback"); |
| 617 | dirty_hash = simple_hash("dirty"); |
| 618 | swap_hash = simple_hash("swap"); |
| 619 | pgpgin_hash = simple_hash("pgpgin"); |
| 620 | pgpgout_hash = simple_hash("pgpgout"); |
| 621 | pgfault_hash = simple_hash("pgfault"); |
| 622 | pgmajfault_hash = simple_hash("pgmajfault"); |
| 623 | inactive_anon_hash = simple_hash("inactive_anon"); |
| 624 | active_anon_hash = simple_hash("active_anon"); |
| 625 | inactive_file_hash = simple_hash("inactive_file"); |
| 626 | active_file_hash = simple_hash("active_file"); |
| 627 | unevictable_hash = simple_hash("unevictable"); |
| 628 | hierarchical_memory_limit_hash = simple_hash("hierarchical_memory_limit"); |
| 629 | total_cache_hash = simple_hash("total_cache"); |
| 630 | total_rss_hash = simple_hash("total_rss"); |
| 631 | total_rss_huge_hash = simple_hash("total_rss_huge"); |
| 632 | total_mapped_file_hash = simple_hash("total_mapped_file"); |
| 633 | total_writeback_hash = simple_hash("total_writeback"); |
| 634 | total_dirty_hash = simple_hash("total_dirty"); |
| 635 | total_swap_hash = simple_hash("total_swap"); |
| 636 | total_pgpgin_hash = simple_hash("total_pgpgin"); |
| 637 | total_pgpgout_hash = simple_hash("total_pgpgout"); |
| 638 | total_pgfault_hash = simple_hash("total_pgfault"); |
| 639 | total_pgmajfault_hash = simple_hash("total_pgmajfault"); |
| 640 | total_inactive_anon_hash = simple_hash("total_inactive_anon"); |
| 641 | total_active_anon_hash = simple_hash("total_active_anon"); |
| 642 | total_inactive_file_hash = simple_hash("total_inactive_file"); |
| 643 | total_active_file_hash = simple_hash("total_active_file"); |
| 644 | total_unevictable_hash = simple_hash("total_unevictable"); |
| 645 | |
| 646 | unsigned long i, c1 = 0, c2 = 0, c3 = 0, c4 = 0, c5 = 0, c6 = 0, c7; |
| 647 | unsigned long max = 1000000; |
| 648 | |
| 649 | // let the processor get up to speed |
| 650 | begin_clock(); |
| 651 | for(i = 0; i <= max ;i++) test1(); |
| 652 | c1 = end_clock(); |
| 653 | |
| 654 | begin_clock(); |
| 655 | for(i = 0; i <= max ;i++) test1(); |
| 656 | c1 = end_clock(); |
| 657 | |
| 658 | begin_clock(); |
| 659 | for(i = 0; i <= max ;i++) test2(); |
| 660 | c2 = end_clock(); |
| 661 | |
| 662 | begin_clock(); |
| 663 | for(i = 0; i <= max ;i++) test3(); |
| 664 | c3 = end_clock(); |
| 665 | |
| 666 | begin_clock(); |
| 667 | for(i = 0; i <= max ;i++) test4(); |
| 668 | c4 = end_clock(); |
| 669 | |
| 670 | begin_clock(); |
| 671 | for(i = 0; i <= max ;i++) test5(); |
| 672 | c5 = end_clock(); |
| 673 | |
| 674 | begin_clock(); |
| 675 | for(i = 0; i <= max ;i++) test6(); |
| 676 | c6 = end_clock(); |
| 677 | |
| 678 | begin_clock(); |
| 679 | for(i = 0; i <= max ;i++) test7(); |
| 680 | c7 = end_clock(); |
| 681 | |
| 682 | for(i = 0; i < 11 ; i++) |
| 683 | printf("value %lu: %llu %llu %llu %llu %llu %llu\n", i, values1[i], values2[i], values3[i], values4[i], values5[i], values6[i]); |
| 684 | |
| 685 | printf("\n\nRESULTS\n"); |
| 686 | printf("test1() in %lu usecs: if-else-if-else-if, simple strcmp() with system strtoull().\n" |
| 687 | "test2() in %lu usecs: inline simple_hash() if-else-if-else-if, with system strtoull().\n" |
| 688 | "test3() in %lu usecs: statement expression simple_hash(), system strtoull().\n" |
| 689 | "test4() in %lu usecs: inline simple_hash(), if-continue checks, system strtoull().\n" |
| 690 | "test5() in %lu usecs: inline simple_hash(), if-else-if-else-if, custom strtoull() (netdata default prior to ARL).\n" |
| 691 | "test6() in %lu usecs: adaptive re-sortable list, system strtoull() (wow!)\n" |
| 692 | "test7() in %lu usecs: adaptive re-sortable list, custom strtoull() (wow!)\n" |
| 693 | , c1 |
| 694 | , c2 |
| 695 | , c3 |
| 696 | , c4 |
| 697 | , c5 |
| 698 | , c6 |
| 699 | , c7 |
| 700 | ); |
| 701 | |
| 702 | } |