Debian Codegraph @

tests/workloads/pointer_chasing/pointer_chasing.c

raw symbols SCIP
1 // SPDX-License-Identifier: BSD-2-Clause
2 /*
3 * Copyright (c) 2025 Intel Corporation
4 */
5 #include "pointer_chasing.h"
6 #include "log.h"
7 #include <inttypes.h>
8 #include <stdint.h>
9 #include <stdio.h>
10 #include <stdlib.h>
11
12 ptr_chaser_t *ptr_chaser;
13
14 int ptr_chase_setup(int argc, char *argv[])
15 {
16 uint64_t buff, span;
17 int ret = 0;
18
19 if (argc != 3) {
20 log_message(LOG_LEVEL_INFO,
21 "[pointer_chasing]: Usage: <buff_size> <span_size>\n");
22 log_message(LOG_LEVEL_INFO, "[pointer_chasing]: Example: 0x4A0000 0x129000\n");
23 return -1;
24 }
25
26 buff = strtoull(argv[1], NULL, 16);
27 span = strtoull(argv[2], NULL, 16);
28
29 log_message(LOG_LEVEL_INFO,
30 "[pointer_chasing]: buff: 0x%" PRIx64 ", span: 0x%" PRIx64
31 "\n",
32 buff, span);
33
34 if (buff == 0 || span == 0 || (span > buff)) {
35 log_message(LOG_LEVEL_INFO, "[pointer_chasing]: Invalid buffer/span sizes.\n");
36 return -1;
37 }
38
39 ptr_chaser = (ptr_chaser_t *)calloc(1, sizeof(ptr_chaser_t));
40
41 if (ptr_chaser == NULL) {
42 log_message(LOG_LEVEL_INFO, "[pointer_chasing]: Memory allocation failed.\n");
43 return -1;
44 }
45
46 ptr_chaser->buff = buff;
47 ptr_chaser->span = span;
48 ret = generate_linked_list(ptr_chaser, 0xdeadbeef);
49
50 if (ret)
51 return -1;
52
53 ptr_chaser->workload = (void *)__chasing_code_loop;
54
55 return 0;
56 }
57
58 void run_ptr_chasing(void)
59 {
60 __ptr_chasing_run_workload(ptr_chaser);
61 }
62
63 int generate_linked_list(ptr_chaser_t *ptr_chaser, unsigned int seed)
64 {
65 ptr_node *head;
66
67 srand(seed);
68 head = createLinkedList();
69
70 if (head == NULL) {
71 log_message(LOG_LEVEL_INFO, "[pointer_chasing]: Creating Linked List failed.\n");
72 return -1;
73 }
74
75 ptr_chaser->head = (void *)head;
76 return 0;
77 }
78
79 /* Get random int in a range of [0, max) */
80 int random_int(int max)
81 {
82 return (rand() % max);
83 }
84
85 ptr_node *createLinkedList(void)
86 {
87 ptr_node *ptr, *head;
88 uint64_t i, nr_nodes;
89 int offset;
90
91 nr_nodes = ptr_chaser->span / sizeof(ptr_node);
92
93 /* Allocate large buffer that we will read randomly from */
94 ptr_node *mem = (ptr_node *)malloc(ptr_chaser->buff * sizeof(ptr_node));
95
96 if (mem == NULL) {
97 log_message(LOG_LEVEL_INFO,
98 "[pointer_chasing]: Memory allocation failed. Consider reducing size "
99 "of the buffer?\n");
100 return NULL;
101 }
102
103 head = &mem[random_int(ptr_chaser->buff)];
104 head->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_int(nr_nodes);
112 if (mem[offset].val == 0) {
113 ptr->next = &mem[offset];
114 ptr = ptr->next;
115 ptr->val = 1;
116 i++;
117 }
118 }
119
120 return head;
121 }
122
123 __attribute__((destructor)) int ptr_chaser_finish(void)
124 {
125 free(ptr_chaser);
126 return 0;
127 }