| 1 | // SPDX-License-Identifier: BSD-2-Clausecxx . . $ `<file>/tests/workloads/pointer_chasing/pointer_chasing.c`/ |
| 2 | /* |
| 3 | * Copyright (c) 2025 Intel Corporation |
| 4 | */ |
| 5 | #include "pointer_chasing.h"cxx . . $ `<file>/tests/workloads/pointer_chasing/pointer_chasing.h`/ |
| 6 | #include "log.h"cxx . . $ `<file>/src/log.h`/ |
| 7 | #include <inttypes.h> |
| 8 | #include <stdint.h> |
| 9 | #include <stdio.h> |
| 10 | #include <stdlib.h> |
| 11 | |
| 12 | ptr_chaser_tcxx . . $ ptr_chaser_t# *ptr_chasercxx . . $ ptr_chaser.; |
| 13 | |
| 14 | int ptr_chase_setupcxx . . $ ptr_chase_setup(afcd3168fc1188a4).(int argc, char *argv[]) |
| 15 | { |
| 16 | uint64_tcxx . . $ uint64_t# buff, span; |
| 17 | int ret = 0; |
| 18 | |
| 19 | if (argc != 3) { |
| 20 | log_messagecxx . . $ log_message(171546b8a219727a).(LOG_LEVEL_INFOcxx . . $ LOG_LEVEL_INFO., |
| 21 | "[pointer_chasing]: Usage: <buff_size> <span_size>\n"); |
| 22 | log_messagecxx . . $ log_message(171546b8a219727a).(LOG_LEVEL_INFOcxx . . $ LOG_LEVEL_INFO., "[pointer_chasing]: Example: 0x4A0000 0x129000\n"); |
| 23 | return -1; |
| 24 | } |
| 25 | |
| 26 | buff = strtoullcxx . . $ strtoull(fa707f2e7d9ad629).(argv[1], NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!, 16); |
| 27 | span = strtoullcxx . . $ strtoull(fa707f2e7d9ad629).(argv[2], NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!, 16); |
| 28 | |
| 29 | log_messagecxx . . $ log_message(171546b8a219727a).(LOG_LEVEL_INFOcxx . . $ LOG_LEVEL_INFO., |
| 30 | "[pointer_chasing]: buff: 0x%" PRIx64cxx . . $ `<external>/15933738808807975373/inttypes.h:121:10`! ", span: 0x%" PRIx64cxx . . $ `<external>/15933738808807975373/inttypes.h:121:10`! |
| 31 | "\n", |
| 32 | buff, span); |
| 33 | |
| 34 | if (buff == 0 || span == 0 || (span > buff)) { |
| 35 | log_messagecxx . . $ log_message(171546b8a219727a).(LOG_LEVEL_INFOcxx . . $ LOG_LEVEL_INFO., "[pointer_chasing]: Invalid buffer/span sizes.\n"); |
| 36 | return -1; |
| 37 | } |
| 38 | |
| 39 | ptr_chasercxx . . $ ptr_chaser. = (ptr_chaser_tcxx . . $ ptr_chaser_t# *)calloccxx . . $ calloc(63a7bf2941bfcd7a).(1, sizeof(ptr_chaser_tcxx . . $ ptr_chaser_t#)); |
| 40 | |
| 41 | if (ptr_chasercxx . . $ ptr_chaser. == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) { |
| 42 | log_messagecxx . . $ log_message(171546b8a219727a).(LOG_LEVEL_INFOcxx . . $ LOG_LEVEL_INFO., "[pointer_chasing]: Memory allocation failed.\n"); |
| 43 | return -1; |
| 44 | } |
| 45 | |
| 46 | ptr_chasercxx . . $ ptr_chaser.->buffcxx . . $ ptr_chaser#buff. = buff; |
| 47 | ptr_chasercxx . . $ ptr_chaser.->spancxx . . $ ptr_chaser#span. = span; |
| 48 | ret = generate_linked_listcxx . . $ generate_linked_list(5f23473471784566).(ptr_chasercxx . . $ ptr_chaser., 0xdeadbeef); |
| 49 | |
| 50 | if (ret) |
| 51 | return -1; |
| 52 | |
| 53 | ptr_chasercxx . . $ ptr_chaser.->workloadcxx . . $ ptr_chaser#workload. = (void *)__chasing_code_loopcxx . . $ __chasing_code_loop(49f6e7a06ebc5aa8).; |
| 54 | |
| 55 | return 0; |
| 56 | } |
| 57 | |
| 58 | void run_ptr_chasingcxx . . $ run_ptr_chasing(49f6e7a06ebc5aa8).(void) |
| 59 | { |
| 60 | __ptr_chasing_run_workloadcxx . . $ __ptr_chasing_run_workload(16a131f8fa5bb4ff).(ptr_chasercxx . . $ ptr_chaser.); |
| 61 | } |
| 62 | |
| 63 | int generate_linked_listcxx . . $ generate_linked_list(5f23473471784566).(ptr_chaser_tcxx . . $ ptr_chaser_t# *ptr_chaser, unsigned int seed) |
| 64 | { |
| 65 | ptr_nodecxx . . $ ptr_node# *head; |
| 66 | |
| 67 | srandcxx . . $ srand(cb2f890c2fdad230).(seed); |
| 68 | head = createLinkedListcxx . . $ createLinkedList(8d6149b11b0bc6ff).(); |
| 69 | |
| 70 | if (head == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) { |
| 71 | log_messagecxx . . $ log_message(171546b8a219727a).(LOG_LEVEL_INFOcxx . . $ LOG_LEVEL_INFO., "[pointer_chasing]: Creating Linked List failed.\n"); |
| 72 | return -1; |
| 73 | } |
| 74 | |
| 75 | ptr_chaser->headcxx . . $ ptr_chaser#head. = (void *)head; |
| 76 | return 0; |
| 77 | } |
| 78 | |
| 79 | /* Get random int in a range of [0, max) */ |
| 80 | int random_intcxx . . $ random_int(7864480464b09eea).(int max) |
| 81 | { |
| 82 | return (randcxx . . $ rand(b126dc7c1de90089).() % max); |
| 83 | } |
| 84 | |
| 85 | ptr_nodecxx . . $ ptr_node# *createLinkedListcxx . . $ createLinkedList(8d6149b11b0bc6ff).(void) |
| 86 | { |
| 87 | ptr_nodecxx . . $ ptr_node# *ptr, *head; |
| 88 | uint64_tcxx . . $ uint64_t# i, nr_nodes; |
| 89 | int offset; |
| 90 | |
| 91 | nr_nodes = ptr_chasercxx . . $ ptr_chaser.->spancxx . . $ ptr_chaser#span. / sizeof(ptr_nodecxx . . $ ptr_node#); |
| 92 | |
| 93 | /* Allocate large buffer that we will read randomly from */ |
| 94 | ptr_nodecxx . . $ ptr_node# *mem = (ptr_nodecxx . . $ ptr_node# *)malloccxx . . $ malloc(bfcfa4d6b7f7ef64).(ptr_chasercxx . . $ ptr_chaser.->buffcxx . . $ ptr_chaser#buff. * sizeof(ptr_nodecxx . . $ ptr_node#)); |
| 95 | |
| 96 | if (mem == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) { |
| 97 | log_messagecxx . . $ log_message(171546b8a219727a).(LOG_LEVEL_INFOcxx . . $ LOG_LEVEL_INFO., |
| 98 | "[pointer_chasing]: Memory allocation failed. Consider reducing size " |
| 99 | "of the buffer?\n"); |
| 100 | return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!; |
| 101 | } |
| 102 | |
| 103 | head = &mem[random_intcxx . . $ random_int(7864480464b09eea).(ptr_chasercxx . . $ ptr_chaser.->buffcxx . . $ ptr_chaser#buff.)]; |
| 104 | head->valcxx . . $ ptr_node#$anonymous_type_0#val. = 1; |
| 105 | ptr = head; |
| 106 | |
| 107 | /* Create linked list that will fill a buffer */ |
| 108 | i = 1; |
| 109 | while (i < nr_nodes) { |
| 110 | /* Pick a random address in the span */ |
| 111 | offset = random_intcxx . . $ random_int(7864480464b09eea).(nr_nodes); |
| 112 | if (mem[offset].valcxx . . $ ptr_node#$anonymous_type_0#val. == 0) { |
| 113 | ptr->nextcxx . . $ ptr_node#$anonymous_type_0#next. = &mem[offset]; |
| 114 | ptr = ptr->nextcxx . . $ ptr_node#$anonymous_type_0#next.; |
| 115 | ptr->valcxx . . $ ptr_node#$anonymous_type_0#val. = 1; |
| 116 | i++; |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | return head; |
| 121 | } |
| 122 | |
| 123 | __attribute__((destructor)) int ptr_chaser_finishcxx . . $ ptr_chaser_finish(b126dc7c1de90089).(void) |
| 124 | { |
| 125 | freecxx . . $ free(5d22bdacc48458e8).(ptr_chasercxx . . $ ptr_chaser.); |
| 126 | return 0; |
| 127 | } |