Created
January 9, 2025 10:19
-
-
Save florianl/65cb53eb519531e6d8ee40cf2991bbf6 to your computer and use it in GitHub Desktop.
Off-CPU demo program
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #define _GNU_SOURCE | |
| #include <stdio.h> | |
| #include <stdlib.h> | |
| #include <pthread.h> | |
| #include <unistd.h> | |
| #include <fcntl.h> | |
| #include <string.h> | |
| #include <sys/mman.h> | |
| #include <sched.h> | |
| #define BUFFER_SIZE (1024 * 1024 * 512) // 512MB | |
| #define NUM_THREADS 8 * sysconf(_SC_NPROCESSORS_ONLN) | |
| #define FILE_SIZE (1024 * 1024 * 100) // 100MB | |
| pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; | |
| char* shared_buffer; | |
| // I/O bottleneck demonstration | |
| void create_io_bottleneck() { | |
| int fd = open("large_file.txt", O_CREAT | O_WRONLY, 0644); | |
| char buffer[4096]; | |
| memset(buffer, 'A', sizeof(buffer)); | |
| for (int i = 0; i < FILE_SIZE; i += sizeof(buffer)) { | |
| write(fd, buffer, sizeof(buffer)); | |
| fsync(fd); // Force disk writes | |
| } | |
| close(fd); | |
| } | |
| // Lock contention demonstration | |
| void* lock_contention_task(void* arg) { | |
| for (int i = 0; i < 1000000; i++) { | |
| pthread_mutex_lock(&mutex); | |
| // Simulate work with shared resource | |
| usleep(1); | |
| pthread_mutex_unlock(&mutex); | |
| } | |
| return NULL; | |
| } | |
| // Memory stalls demonstration | |
| void create_memory_stalls() { | |
| shared_buffer = (char*)malloc(BUFFER_SIZE); | |
| // Random memory access pattern | |
| for (int i = 0; i < BUFFER_SIZE; i += 4096) { | |
| shared_buffer[i] = 1; | |
| madvise(shared_buffer + i, 4096, MADV_DONTNEED); | |
| } | |
| free(shared_buffer); | |
| } | |
| // System call delays demonstration | |
| void create_syscall_delays() { | |
| cpu_set_t cpuset; | |
| int num_cpus = sysconf(_SC_NPROCESSORS_ONLN); | |
| for (int i = 0; i < 1000000; i++) { | |
| // Set affinity to a different core each iteration | |
| CPU_ZERO(&cpuset); | |
| CPU_SET(i % num_cpus, &cpuset); | |
| pthread_setaffinity_np(pthread_self(), sizeof(cpu_set_t), &cpuset); | |
| getpid(); | |
| sched_yield(); | |
| } | |
| } | |
| int main() { | |
| pthread_t threads[NUM_THREADS]; | |
| // Create memory stalls | |
| create_memory_stalls(); | |
| // Create system call delays | |
| create_syscall_delays(); | |
| // Create I/O bottleneck | |
| create_io_bottleneck(); | |
| // Create lock contention | |
| for (int i = 0; i < NUM_THREADS; i++) { | |
| pthread_create(&threads[i], NULL, lock_contention_task, NULL); | |
| } | |
| // Wait for lock contention threads | |
| for (int i = 0; i < NUM_THREADS; i++) { | |
| pthread_join(threads[i], NULL); | |
| } | |
| return 0; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| .PHONY: all | |
| all: | |
| gcc -o bottleneck main.c -pthread |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment