Last active
August 28, 2024 01:01
-
-
Save ethercflow/f32669a7b0e7d8649e2781535b43cf54 to your computer and use it in GitHub Desktop.
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
#include <pthread.h> | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <unistd.h> | |
#define NUM_THREADS 4 | |
static int counter1 = 0; | |
static int counter2 = 0; | |
static pthread_mutex_t mutex1; | |
static pthread_mutex_t mutex2; | |
__attribute__((noinline)) void baz(int *params) { | |
int *counter = params[0] == 1 ? &counter1 : &counter2; | |
pthread_mutex_t *mutex = params[0] == 1 ? &mutex1 : &mutex2; | |
int increment = params[1]; | |
int sleep_time = params[0] == 1 ? 1000 : 10; // counter1: 1s, counter2: 10ms | |
while (1) { | |
pthread_mutex_lock(mutex); | |
*counter += increment; | |
fprintf(stderr, "counter: %d\n", *counter); | |
usleep(sleep_time * 1000); // sleep in milliseconds | |
pthread_mutex_unlock(mutex); | |
} | |
} | |
__attribute__((noinline)) void bar(int *params) { | |
baz(params); | |
} | |
__attribute__((noinline)) void foo(int *params) { | |
bar(params); | |
} | |
void *modify_counter(void *arg) { | |
foo((int *)arg); | |
return NULL; | |
} | |
int main() { | |
pthread_t threads[NUM_THREADS]; | |
pthread_mutex_init(&mutex1, NULL); | |
pthread_mutex_init(&mutex2, NULL); | |
int params1[2] = { 1, 1 }; // {counter1, increment} | |
int params2[2] = { 2, 1 }; // {counter2, increment} | |
pthread_create(&threads[0], NULL, modify_counter, (void *)params1); | |
pthread_create(&threads[1], NULL, modify_counter, (void *)params1); | |
pthread_create(&threads[2], NULL, modify_counter, (void *)params2); | |
pthread_create(&threads[3], NULL, modify_counter, (void *)params2); | |
for (int i = 0; i < NUM_THREADS; i++) { | |
pthread_join(threads[i], NULL); | |
} | |
pthread_mutex_destroy(&mutex1); | |
pthread_mutex_destroy(&mutex2); | |
printf("Final counter1 value: %d\n", counter1); | |
printf("Final counter2 value: %d\n", counter2); | |
return 0; | |
} |
Author
ethercflow
commented
Aug 24, 2024
•
⋊> /w/k/b/libbpf-tools on master ◦ sudo ./futexctn -l 0x612a05ebf0e0 -m -p $(pidof test-futex) 60 1 11:05:21
Summarize futex contention latency, hit ctrl-c to exit
test-futex[216572] lock 0x612a05ebf0e0 contended 739 times, 9 avg msecs [max: 10 msecs, min 9 msecs]
-
__lll_lock_wait_private
baz
bar
foo
modify_counter
pthread_condattr_setpshared
clone
-
msecs : count distribution
0 -> 1 : 0 | |
2 -> 3 : 0 | |
4 -> 7 : 0 | |
8 -> 15 : 739 |****************************************|
test-futex[216573] lock 0x612a05ebf0e0 contended 5183 times, 9 avg msecs [max: 10 msecs, min 9 msecs]
-
__lll_lock_wait_private
baz
bar
foo
modify_counter
pthread_condattr_setpshared
clone
-
msecs : count distribution
0 -> 1 : 0 | |
2 -> 3 : 0 | |
4 -> 7 : 0 | |
8 -> 15 : 5183 |****************************************|
test-futex[216572] lock 0x612a05ebf0e0 contended 2698 times, 10 avg msecs [max: 13 msecs, min 9 msecs]
-
__lll_lock_wait_private
baz
bar
foo
modify_counter
pthread_condattr_setpshared
clone
-
msecs : count distribution
0 -> 1 : 0 | |
2 -> 3 : 0 | |
4 -> 7 : 0 | |
8 -> 15 : 2698 |****************************************|
test-futex[216572] lock 0x612a05ebf0e0 contended 3214 times, 10 avg msecs [max: 14 msecs, min 8 msecs]
-
__lll_lock_wait_private
baz
bar
foo
modify_counter
pthread_condattr_setpshared
clone
-
msecs : count distribution
0 -> 1 : 0 | |
2 -> 3 : 0 | |
4 -> 7 : 0 | |
8 -> 15 : 3214 |****************************************|
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment