// cc -lnuma #define _GNU_SOURCE #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #ifndef HUGETLBFS_MAGIC #define HUGETLBFS_MAGIC 0x958458f6 #endif static void die(const char *msg) { int e = errno; fprintf(stderr, "%s: %s\n", msg, strerror(e)); exit(1); } static size_t round_up_size(size_t x, size_t a) { return (x + a - 1) / a * a; } static size_t detect_hugepage_size_from_path(const char *path) { struct statfs s; if (statfs(path, &s) == 0) { if ((unsigned long)s.f_type == (unsigned long)HUGETLBFS_MAGIC && s.f_bsize > 0) { return (size_t)s.f_bsize; } if (s.f_bsize > 0 && (size_t)s.f_bsize >= 4096) return (size_t)s.f_bsize; } return 2u * 1024u * 1024u; } struct numa_node_info { int node; int cpu_count; cpu_set_t cpuset; }; static void copy_range( int fd, size_t in_size, void *dst, size_t start, size_t length ) { const size_t buf_sz = 1024 * 1024; char *buf = malloc(buf_sz); if (!buf) die("malloc failed"); if (lseek(fd, (off_t)start, SEEK_SET) < 0) die("lseek(input) failed"); size_t remaining = length; size_t copied = 0; while (remaining > 0 && (start + copied) < in_size) { size_t want = remaining; if (want > buf_sz) want = buf_sz; if (start + copied + want > in_size) { want = in_size - (start + copied); if (want == 0) break; } ssize_t n = read(fd, buf, want); if (n < 0) die("read failed"); if (n == 0) break; memcpy((char *)dst + start + copied, buf, (size_t)n); copied += (size_t)n; remaining -= (size_t)n; } free(buf); } static void setup_numa( struct numa_node_info **out_nodes, int *out_nnodes, long *out_total_weight ) { if (numa_available() < 0) { die("NUMA requested but libnuma reports unavailable"); } int max_node = numa_max_node(); int nnodes = 0; struct numa_node_info *nodes = calloc((size_t)(max_node + 1), sizeof(struct numa_node_info)); if (!nodes) die("calloc nodes failed"); long total_weight = 0; for (int n = 0; n <= max_node; n++) { if (!numa_bitmask_isbitset(numa_all_nodes_ptr, n)) continue; cpu_set_t cpuset; CPU_ZERO(&cpuset); struct bitmask *bm = numa_allocate_cpumask(); if (!bm) die("numa_allocate_cpumask failed"); if (numa_node_to_cpus(n, bm) != 0) { numa_free_cpumask(bm); continue; } int cpu_count = 0; for (int c = 0; c < bm->size; c++) { if (numa_bitmask_isbitset(bm, c)) { CPU_SET(c, &cpuset); cpu_count++; } } numa_free_cpumask(bm); if (cpu_count == 0) continue; nodes[nnodes].node = n; nodes[nnodes].cpu_count = cpu_count; nodes[nnodes].cpuset = cpuset; total_weight += cpu_count; nnodes++; } if (nnodes == 0) { free(nodes); die("NUMA requested but no nodes with CPUs found, falling back"); } *out_nodes = nodes; *out_nnodes = nnodes; *out_total_weight = total_weight; } int main(int argc, char **argv) { if (argc != 3) { fprintf(stderr, "usage: %s \n", argv[0]); return 2; } const char *in_path = argv[1]; const char *out_path = argv[2]; int in_fd = open(in_path, O_RDONLY); if (in_fd < 0) die("open(input) failed"); struct stat st; if (fstat(in_fd, &st) < 0) die("fstat(input) failed"); if (!S_ISREG(st.st_mode)) { fprintf(stderr, "input must be a regular file\n"); return 2; } size_t in_size = (size_t)st.st_size; size_t hpsz = detect_hugepage_size_from_path(out_path); size_t out_size = round_up_size(in_size, hpsz); int out_fd = open(out_path, O_RDWR | O_CREAT | O_TRUNC, 0644); if (out_fd < 0) die("open(output) failed"); if (ftruncate(out_fd, (off_t)out_size) < 0) die("ftruncate(output) failed"); size_t flags = 0; switch (hpsz) { case (2 * 1024 * 1024): flags = MAP_HUGE_2MB; break; case (1 * 1024 * 1024 * 1024): flags = MAP_HUGE_1GB; break; } void *dst = mmap(NULL, out_size, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_HUGETLB | flags, out_fd, 0); if (dst == MAP_FAILED) die("mmap(output) failed"); int use_numa = (getenv("NUMA") != NULL); if (!use_numa) { copy_range(in_fd, in_size, dst, 0, out_size); } else { struct numa_node_info *nodes = NULL; int nnodes = 0; long total_weight = 0; setup_numa(&nodes, &nnodes, &total_weight); // Compute per-node byte ranges proportional to cpu_count size_t remaining = in_size; size_t offset = 0; for (int i = 0; i < nnodes; i++) { struct numa_node_info *ni = &nodes[i]; // Proportional share size_t share = 0; if (i == nnodes - 1) { // give the remainder to the last node to cover whole file share = remaining; } else { double fraction = (double)ni->cpu_count / (double)total_weight; share = (size_t)((double)in_size * fraction); if (share > remaining) share = remaining; } if (share == 0 && i != nnodes - 1) { continue; } pid_t pid = fork(); if (pid < 0) die("fork failed"); if (pid == 0) { // Child: pin to this node's CPUs and copy our range if (sched_setaffinity(0, sizeof(cpu_set_t), &ni->cpuset) != 0) { die("sched_setaffinity failed"); } copy_range(in_fd, in_size, dst, offset, share); _exit(0); } else { int status = 0; if (waitpid(pid, &status, 0) < 0) die("waitpid failure"); if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) { die("child exited abnormally"); } } offset += share; if (remaining >= share) remaining -= share; else remaining = 0; } free(nodes); if (out_size > in_size) { memset((char *)dst + in_size, 0, out_size - in_size); } } if (msync(dst, out_size, MS_SYNC) < 0) die("msync failed"); if (munmap(dst, out_size) < 0) die("munmap failed"); if (close(out_fd) < 0) die("close(output) failed"); if (close(in_fd) < 0) die("close(input) failed"); return 0; }