Slide 1MotivationBefore We StartGOOD programming PRACTICES1. Know Your Library Calls2. Check Return Values3. Use Conditional Compilation4. Learn and Use Makefiles5. Keep It Simple (Silly)6. Avoid Premature Optimizations7. Learn and Use gdbSummary of Good PracticesTesting Your ProxySequential Proxy Test CaseCase Proxy Test CasePort Proxy Test CaseCache Proxy Test CaseConcurrent Proxy Test CaseUSING DBUGdBug: Concurrent gdb and more15-213: How to dBug your (threaded) proxyJiří ŠimšaPARALLEL DATA LABORATORYCarnegie Mellon UniversityMotivation“Concurrency is a good servant but a bad master”•Today we will cover:•good programming practices•good debugging practices•tips for designing and testing your proxy•a tool for testing your multi-threaded proxyJiri Simsa © October 10http://www.pdl.cmu.edu/ 2Before We Start•Go to:http://www.virtualbox.org/ and download and install VirtualBox on your laptop•Go to: http://www.cs.cmu.edu/~213/resources.htmland start downloading the dbug-213.vdiJiri Simsa © October 10http://www.pdl.cmu.edu/ 3GOOD PROGRAMMING PRACTICESJiri Simsa © October 10http://www.pdl.cmu.edu/ 41. Know Your Library Calls...pthread_mutex_lock(&mutex);if (cache_empty()) { pthread_mutex_unlock(&mutex); ...}...pthread_mutex_unlock(&mutex);...Jiri Simsa © October 10http://www.pdl.cmu.edu/ 5Spending time reading a man page will dispel your misconceptions and save you hours of frustration.Spending time reading a man page will dispel your misconceptions and save you hours of frustration.2. Check Return Values...ret = accept(sockfd,addr,addrlen);if (ret == -1) { perror(“Accept failed.\n”);} else { ...}...assert(pthread_mutex_lock(&mutex) == 0);...Jiri Simsa © October 10http://www.pdl.cmu.edu/ 6Handling error cases correctly will facilitate identification of program failure root cause.Handling error cases correctly will facilitate identification of program failure root cause.3. Use Conditional Compilationvoid *request_handler(void *args) {#ifdef DEBUG printf(“Entering %s”, __FUNCTION__);#endif...}$ gcc –DDEBUG ...Jiri Simsa © October 10http://www.pdl.cmu.edu/ 7Spending time including informative messages will greatly improve your debugging efficiency.Spending time including informative messages will greatly improve your debugging efficiency.4. Learn and Use MakefilesCC = gccCFLAGS = -g -Wall -WerrorLDFLAGS = -lpthread...all: proxy proxy-testproxy: proxy.c csapp.c$(CC) -o $@ $(CFLAGS) -O3 $(LDFLAGS) $^proxy-test: proxy.c csapp.c$(CC) -o $@ $(CFLAGS) -DDEBUG $(LDFLAGS) $^Jiri Simsa © October 10http://www.pdl.cmu.edu/ 8Spending time learning how to use a Makefile will save you hours of repetitive and dull work.Spending time learning how to use a Makefile will save you hours of repetitive and dull work.5. Keep It Simple (Silly)...int main(int argc, char *argv[]) { ... init_proxy(); while(1) { if ((ret = accept(sockfd,addr,addrlen)) > 0) { setup_args(sockfd,addr,addrlen,args); ret = pthread_create(&tid, 0, handler, args);assert(ret == 0); } else { perror(“Accept failed.\n”); } }}Jiri Simsa © October 10http://www.pdl.cmu.edu/ 9Start with the simplest approach possible. Often getting the simplest design right will be enough. Start with the simplest approach possible. Often getting the simplest design right will be enough.6. Avoid Premature Optimizations...// All cache operations take O(1) time. Sick!fancy_cache_t cache;...inline voidlookup_cache_entry(struct cache_entry *entry) { ... // 100+ lines of code ...}Jiri Simsa © October 10http://www.pdl.cmu.edu/ 10Start with the simplest approach possible. Often getting the simplest design right will be enough. Start with the simplest approach possible. Often getting the simplest design right will be enough.7. Learn and Use gdbJiri Simsa © October 10http://www.pdl.cmu.edu/ 11$ ulimit -c unlimited$ ./proxy 1234Segmentation fault (core dumped)$ gdb ./proxy core...(gdb) bt...#3 0x08049865 in doit (fd=4) at proxy.c:179#4 0x080496c5 in thread_handler (connfdp=0x9920008) at proxy.c:136#5 0x00da7cc9 in start_thread (arg=0xb78adb70) at pthread_create.c:304...Learning how to control and inspect execution of a program is instrumental to efficient debugging. Learning how to control and inspect execution of a program is instrumental to efficient debugging.Summary of Good Practices1. Know Your Library Calls2. Check Return Values3. Use Conditional Compilation4. Learn and Use a Makefile5. Keep it Simple6. Avoid Premature Optimization7. Learn and Use gdbJiri Simsa © October 10http://www.pdl.cmu.edu/ 12TESTING YOUR PROXYJiri Simsa © October 10http://www.pdl.cmu.edu/ 13Sequential Proxy Test Case •Open three terminal windows1:$ cd $TINY; make; sudo ./tiny 802:$ cd $PROXY; make; ./proxy 12343:$ curl –x localhost:1234 http://localhost<html><head><title>test</title></head><body> <img align="middle" src="godzilla.gif">Dave O'Hallaron</body></html>Jiri Simsa © October 10http://www.pdl.cmu.edu/ 14Case Proxy Test CaseJiri Simsa © October 10http://www.pdl.cmu.edu/ 15•Open three terminal windows1:$ cd $TINY; make; sudo ./tiny 802:$ cd $PROXY; make; ./proxy 12343:$ curl –x localhost:1234 HTTP://LoCaLhOst<html><head><title>test</title></head><body> <img align="middle" src="godzilla.gif">Dave O'Hallaron</body></html>Port Proxy Test CaseJiri Simsa © October 10http://www.pdl.cmu.edu/ 16•Open three terminal windows1:$ cd $TINY; make; ./tiny 80802:$ cd $PROXY; make; ./proxy 12343:$ curl –x localhost:1234 http://localhost:8080<html><head><title>test</title></head><body> <img align="middle" src="godzilla.gif">Dave O'Hallaron</body></html>Cache Proxy Test CaseJiri Simsa © October 10http://www.pdl.cmu.edu/ 17•Open three terminal windows1:$ cd $TINY; make; sudo ./tiny 802:$ cd $PROXY; make; ./proxy 12343:$ curl –x localhost:1234 http://localhost3:$ killall -9 tiny3:$ curl –x localhost:1234 http://localhost<html><head><title>test</title></head><body> <img align="middle" src="godzilla.gif">...Concurrent Proxy Test Case •Open three terminal windows1:$ cd $TINY; make; sudo ./tiny 802:$ cd $PROXY; make; ./proxy 12343:$ export CMD=“curl –x localhost:1234 http://localhost”3:$ $CMD & $CMD•Which request will get serviced 1st?•Will the 2nd request be serviced from cache?•Will a non-reentrant function be called concurrently? •How to try out all possible cases?Jiri Simsa © October 10http://www.pdl.cmu.edu/ 18USING DBUGJiri Simsa © October 10http://www.pdl.cmu.edu/
View Full Document