Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue #44: add lock performance #86

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions threads/lock_performance/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
all: lock_performance
gcc lock_performance.c -o lock_performance

clean:
rm lock_performance

test:
./lock_performance
37 changes: 37 additions & 0 deletions threads/lock_performance/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
Threading Examples
=======
wenhui-2:threads WenhuiZhang$ ./lock_performance
-----------------------------
lock performance for 1 thread with 10000 loops
Total time cost by running 1 threads for increase function with lock implementation: 0.000686
secStarting of the program, start_t = 2327
End of the process end_t = 3138
Total time cost by running 1 threads for increase function without lock implementation: 0.000092 sec
Mutex Time Cost by running 1 threads for increase function: 0.000594 sec
Per thread Mutex Time Cost by running 1 threads for increase function: 0.000594 sec
-----------------------------
lock performance for 2 thread with 10000 loops
Total time cost by running 2 threads for increase function with lock implementation: 0.048873
secStarting of the program, start_t = 3170
End of the process end_t = 52294
Total time cost by running 2 threads for increase function without lock implementation: 0.000227 sec
Mutex Time Cost by running 2 threads for increase function: 0.048646 sec
Per thread Mutex Time Cost by running 2 threads for increase function: 0.024323 sec
-----------------------------
lock performance for 4 thread with 10000 loops
Total time cost by running 4 threads for increase function with lock implementation: 0.173181
secStarting of the program, start_t = 52315
End of the process end_t = 225927
Total time cost by running 4 threads for increase function without lock implementation: 0.000407 sec
Mutex Time Cost by running 4 threads for increase function: 0.172774 sec
Per thread Mutex Time Cost by running 4 threads for increase function: 0.043194 sec
-----------------------------
-----------------------------
lock performance for 16 thread with 10000 loops
Total time cost by running 16 threads for increase function with lock implementation: 0.664478
secStarting of the program, start_t = 225953
End of the process end_t = 892292
Total time cost by running 16 threads for increase function without lock implementation: 0.001840 sec
Mutex Time Cost by running 16 threads for increase function: 0.662638 sec
Per thread Mutex Time Cost by running 16 threads for increase function: 0.041415 sec
-----------------------------
184 changes: 184 additions & 0 deletions threads/lock_performance/lock_performance.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
/*
* measures the performance cost by time of using locks in multi-thread program
* Measure the overhead of calling lock and unlock depending on the level of contention
* by utilizing clock(), in time.h
* Author: Wenhui Zhang, Hu Yang, Pradeep
*/



#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <time.h>



int ctr;
pthread_mutex_t count_mutex;
clock_t start_t, end_t, acc_lock_t, acc_no_lock_t;



/* thread_lock: thread function for multi-thread generation */
void thread_lock()
{

for (int i = 0; i < 10000; i++){
pthread_mutex_lock(&count_mutex);
ctr = ctr + 1;
//printf("%d \n", ctr);

pthread_mutex_unlock(&count_mutex);

}


}


/* thread_no_lock: thread function for multi-thread generation */
void thread_no_lock()
{

for (int i = 0; i < 10000; i++){
ctr = ctr + 1;
//printf("%d \n", ctr);
}


}





int lock_performance(int num_threads)
{

acc_lock_t = 0;
acc_no_lock_t = 0;
double total_t_lock, total_t_unlock; //time counter
int rc;
int tnum; //counter for threads




pthread_t thread_info[num_threads]; // thread identifier

/* Test for thread_incr_lock */



start_t = clock();
for (tnum = 0; tnum < num_threads; tnum++){
//printf("create thread %d\n", tnum);

rc = pthread_create(&thread_info[tnum], NULL, (void*)&thread_lock, NULL);
if (rc){
printf("ERROR; return code from pthread_create() is %d\n", rc);
exit(1);
}
}


for (tnum = 0; tnum < num_threads; tnum++){
pthread_join(thread_info[tnum], NULL);
}


end_t = clock();
total_t_lock = (double)((end_t - start_t) / (double)CLOCKS_PER_SEC);
printf("Total time cost by running %d threads for increase function with lock implementation: %f\n sec", num_threads, total_t_lock);



/* Test for thread_incr_unlock */

ctr = 0;

printf("Starting of the program, start_t = %ld\n", start_t);

start_t = clock();
for (tnum = 0; tnum < num_threads; tnum++){
//printf("create thread %d\n", tnum);

rc = pthread_create(&thread_info[tnum], NULL, (void*)&thread_no_lock, NULL);
if (rc){
printf("ERROR; return code from pthread_create() is %d\n", rc);
exit(1);
}
}


for (tnum = 0; tnum < num_threads; tnum++){
pthread_join(thread_info[tnum], NULL);
}


end_t = clock();

printf("End of the process end_t = %ld\n", end_t);
total_t_unlock = (double)((end_t - start_t) / (double)CLOCKS_PER_SEC);
printf("Total time cost by running %d threads for increase function without lock implementation: %f sec\n", num_threads, total_t_unlock);




printf("Mutex Time Cost by running %d threads for increase function: %f sec\n", num_threads, total_t_lock - total_t_unlock);
printf("Per thread Mutex Time Cost by running %d threads for increase function: %f sec\n ", num_threads,(total_t_lock - total_t_unlock)/(double)num_threads);



return 0;
}


int main(void)
{
pthread_mutex_init(&count_mutex, NULL); //initialize mutex

printf("-----------------------------\n");
printf("lock performance for 1 thread with 10000 loops\n");

lock_performance(1);


printf("-----------------------------\n");
printf("lock performance for 2 thread with 10000 loops\n");

lock_performance(2);


printf("-----------------------------\n");
printf("lock performance for 4 thread with 10000 loops\n");


lock_performance(4);
printf("-----------------------------\n");






printf("-----------------------------\n");
printf("lock performance for 16 thread with 10000 loops\n");


lock_performance(16);
printf("-----------------------------\n");

pthread_mutex_destroy(&count_mutex); // destroy mutex


return 0;
}