|
| 1 | +#include <linux/module.h> |
| 2 | +#include <linux/kernel.h> |
| 3 | +#include <linux/init.h> |
| 4 | +#include <linux/slab.h> |
| 5 | +#include <linux/ktime.h> |
| 6 | + |
| 7 | +MODULE_LICENSE("GPL"); |
| 8 | +MODULE_DESCRIPTION("BST benchmark in kernel module"); |
| 9 | + |
| 10 | +// ---------------- BST Node ---------------- |
| 11 | +struct bst_node { |
| 12 | + int key; |
| 13 | + struct bst_node *left, *right; |
| 14 | +}; |
| 15 | + |
| 16 | +// BST Insert |
| 17 | +struct bst_node* bst_insert(struct bst_node* root, int key) { |
| 18 | + if (!root) { |
| 19 | + struct bst_node* n = kmalloc(sizeof(*n), GFP_KERNEL); |
| 20 | + n->key = key; |
| 21 | + n->left = n->right = NULL; |
| 22 | + return n; |
| 23 | + } |
| 24 | + if (key < root->key) |
| 25 | + root->left = bst_insert(root->left, key); |
| 26 | + else if (key > root->key) |
| 27 | + root->right = bst_insert(root->right, key); |
| 28 | + return root; // ignore duplicates |
| 29 | +} |
| 30 | + |
| 31 | +// BST Lookup |
| 32 | +struct bst_node* bst_lookup(struct bst_node* root, int key) { |
| 33 | + while (root) { |
| 34 | + if (key < root->key) |
| 35 | + root = root->left; |
| 36 | + else if (key > root->key) |
| 37 | + root = root->right; |
| 38 | + else |
| 39 | + return root; |
| 40 | + } |
| 41 | + return NULL; |
| 42 | +} |
| 43 | + |
| 44 | +// BST Delete |
| 45 | +struct bst_node* bst_delete(struct bst_node* root, int key) { |
| 46 | + if (!root) return NULL; |
| 47 | + |
| 48 | + if (key < root->key) |
| 49 | + root->left = bst_delete(root->left, key); |
| 50 | + else if (key > root->key) |
| 51 | + root->right = bst_delete(root->right, key); |
| 52 | + else { |
| 53 | + if (!root->left) { |
| 54 | + struct bst_node* tmp = root->right; |
| 55 | + kfree(root); |
| 56 | + return tmp; |
| 57 | + } else if (!root->right) { |
| 58 | + struct bst_node* tmp = root->left; |
| 59 | + kfree(root); |
| 60 | + return tmp; |
| 61 | + } else { |
| 62 | + // find min in right subtree |
| 63 | + struct bst_node* tmp = root->right; |
| 64 | + while (tmp->left) tmp = tmp->left; |
| 65 | + root->key = tmp->key; |
| 66 | + root->right = bst_delete(root->right, tmp->key); |
| 67 | + } |
| 68 | + } |
| 69 | + return root; |
| 70 | +} |
| 71 | + |
| 72 | +// Free BST |
| 73 | +void bst_free(struct bst_node* root) { |
| 74 | + if (!root) return; |
| 75 | + bst_free(root->left); |
| 76 | + bst_free(root->right); |
| 77 | + kfree(root); |
| 78 | +} |
| 79 | + |
| 80 | +// ---------------- LCG PRNG ---------------- |
| 81 | +static inline unsigned int lcg_next(unsigned int *state) { |
| 82 | + *state = (*state * 1664525 + 1013904223); |
| 83 | + return *state; |
| 84 | +} |
| 85 | + |
| 86 | +static inline unsigned int rand_range(unsigned int *state, unsigned int max) { |
| 87 | + return lcg_next(state) % max; |
| 88 | +} |
| 89 | + |
| 90 | +// Fisher-Yates shuffle |
| 91 | +void shuffle(int *arr, int n, unsigned int seed) { |
| 92 | + unsigned int state = seed; |
| 93 | + int i; |
| 94 | + for (i = n - 1; i > 0; i--) { |
| 95 | + int j = rand_range(&state, i + 1); |
| 96 | + int tmp = arr[i]; |
| 97 | + arr[i] = arr[j]; |
| 98 | + arr[j] = tmp; |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +// ---------------- Benchmark ---------------- |
| 103 | +#define N (64 * 1024) |
| 104 | + |
| 105 | +static int __init bst_benchmark_init(void) |
| 106 | +{ |
| 107 | + int i; |
| 108 | + unsigned long long start, end, total; |
| 109 | + struct bst_node* root = NULL; |
| 110 | + |
| 111 | + int *data = kmalloc_array(N, sizeof(int), GFP_KERNEL); |
| 112 | + for (i = 0; i < N; i++) data[i] = i + 1; |
| 113 | + shuffle(data, N, 114514); // fixed seed |
| 114 | + |
| 115 | + pr_info("bst Benchmark: N=%d\n", N); |
| 116 | + |
| 117 | + // ----- Insert ----- |
| 118 | + total = 0; |
| 119 | + for (i = 0; i < N; i++) { |
| 120 | + start = ktime_get_ns(); |
| 121 | + root = bst_insert(root, data[i]); |
| 122 | + end = ktime_get_ns(); |
| 123 | + total += (end - start); |
| 124 | + } |
| 125 | + pr_info("bst Insert avg latency: %llu ns\n", total / N); |
| 126 | + |
| 127 | + // ----- Lookup ----- |
| 128 | + total = 0; |
| 129 | + for (i = 0; i < N; i++) { |
| 130 | + start = ktime_get_ns(); |
| 131 | + if (!bst_lookup(root, data[i])) pr_info("Lookup error!\n"); |
| 132 | + end = ktime_get_ns(); |
| 133 | + total += (end - start); |
| 134 | + } |
| 135 | + pr_info("bst Lookup avg latency: %llu ns\n", total / N); |
| 136 | + |
| 137 | + // ----- Delete ----- |
| 138 | + total = 0; |
| 139 | + for (i = 0; i < N; i++) { |
| 140 | + start = ktime_get_ns(); |
| 141 | + root = bst_delete(root, data[i]); |
| 142 | + end = ktime_get_ns(); |
| 143 | + total += (end - start); |
| 144 | + } |
| 145 | + pr_info("bst Delete avg latency: %llu ns\n", total / N); |
| 146 | + |
| 147 | + bst_free(root); |
| 148 | + return 0; |
| 149 | +} |
| 150 | + |
| 151 | +static void __exit bst_benchmark_exit(void) |
| 152 | +{ |
| 153 | + pr_info("BST benchmark module exit\n"); |
| 154 | +} |
| 155 | + |
| 156 | +module_init(bst_benchmark_init); |
| 157 | +module_exit(bst_benchmark_exit); |
0 commit comments