diff --git a/study/patch/mutex/bug b/study/patch/mutex/bug new file mode 100755 index 0000000..caf1424 Binary files /dev/null and b/study/patch/mutex/bug differ diff --git a/study/patch/mutex/check_lock b/study/patch/mutex/check_lock new file mode 100755 index 0000000..7eaede7 Binary files /dev/null and b/study/patch/mutex/check_lock differ diff --git a/study/patch/mutex/futex_bug b/study/patch/mutex/futex_bug new file mode 100755 index 0000000..93d40f2 Binary files /dev/null and b/study/patch/mutex/futex_bug differ diff --git a/study/patch/mutex/futex_test b/study/patch/mutex/futex_test new file mode 100755 index 0000000..f31547a Binary files /dev/null and b/study/patch/mutex/futex_test differ diff --git a/study/patch/mutex/lock_bug.c b/study/patch/mutex/lock_bug.c index 3f37bdc..170f3bc 100644 --- a/study/patch/mutex/lock_bug.c +++ b/study/patch/mutex/lock_bug.c @@ -92,14 +92,14 @@ int main(int argc,char** argv){ pthread_t id1; pthread_t id2; - struct args arg1 = { .sleeptime = 1000000, .cpu = 0 }; - struct args arg2 = { .sleeptime = 1000000, .cpu = 0 }; + struct args arg1 = { .sleeptime = 10000000, .cpu = 0 }; + struct args arg2 = { .sleeptime = 10000000, .cpu = 0 }; LOCK_INIT( ); printf("MAIN pid = %d\n", getpid( )); - + getchar( ); pthread_create(&id1, NULL, print_msg, &arg1); pthread_create(&id2, NULL, print_msg, &arg2); diff --git a/study/patch/mutex/mutex_bug b/study/patch/mutex/mutex_bug new file mode 100755 index 0000000..6f511fa Binary files /dev/null and b/study/patch/mutex/mutex_bug differ diff --git a/study/patch/mutex/sem_bug b/study/patch/mutex/sem_bug new file mode 100755 index 0000000..572bfd4 Binary files /dev/null and b/study/patch/mutex/sem_bug differ diff --git a/study/patch/mutex/sem_test b/study/patch/mutex/sem_test new file mode 100755 index 0000000..cb697a6 Binary files /dev/null and b/study/patch/mutex/sem_test differ diff --git a/study/patch/perf/Makefile b/study/patch/perf/Makefile new file mode 100644 index 0000000..3a9d464 --- /dev/null +++ b/study/patch/perf/Makefile @@ -0,0 +1,17 @@ +target=bug_fork + +all:$(target) + +bug_fork:bug_fork.c + $(CC) $^ -o $@ $(LDFLAGS) -lpthread -DMUTEX -static + @echo "mutex lock..." + +%.o : %.c + $(CC) -c $^ -o $@ $(CFLAGS) $(DEFINES) + +%.o : %.cpp + $(CXX) -c $^ -o $@ $(CFLAGS) $(DEFINES) + +clean : + rm -rf *.o + rm -rf $(target) diff --git a/study/patch/perf/README.md b/study/patch/perf/README.md new file mode 100644 index 0000000..214b167 --- /dev/null +++ b/study/patch/perf/README.md @@ -0,0 +1,51 @@ + +1 perf record sched:sched_wakeup_new ./bug_fork + +perf/ftrace : Fix repetitious traces when specify a target task + + + +When use perf to trace the sched_wakeup and sched_wakeup_new tracepoint, there is a bug that output the same event repetitiously, It can be reproduced by : + + +```cpp +perf record -e sched:sched_wakeup ./bug_fork +OR +./bug_fork # pid 1052 +perf record -e sched:sched_wakeup -p 1052 +``` + +```cpp +swapper 0 [000] 3854.085452: sched:sched_wakeup: comm=perf pid=1101 prio=120 target_c +pu=000 +swapper 0 [000] 3854.085473: sched:sched_wakeup: comm=perf pid=1101 prio=120 target_c +pu=000 +bug_fork 1101 [000] 3854.089933: sched:sched_wakeup_new: comm=bug_fork pid=1102 prio=120 targ +et_cpu=001 +bug_fork 1101 [000] 3854.089950: sched:sched_wakeup_new: comm=bug_fork pid=1102 prio=120 targ +et_cpu=001 +bug_fork 1101 [000] 3854.089959: sched:sched_wakeup_new: comm=bug_fork pid=1102 prio=120 targ +et_cpu=001 +bug_fork 1102 [001] 3854.092185: sched:sched_wakeup: comm=rcu_sched pid=8 prio=120 target +_cpu=001 +swapper 0 [000] 3854.665213: sched:sched_wakeup: comm=bug_fork pid=1101 prio=120 targ +et_cpu=000 +swapper 0 [000] 3854.665253: sched:sched_wakeup: comm=bug_fork pid=1101 prio=120 targ +et_cpu=000 + +``` + +Some events not only monitor the current task, but also specify a target task. For example, the sched_wakeup/sched_wakeup_new tracepoint will be caught when the current task wakeup the target task which we traced on. + +the commit bed5b2 has designed a method to trace these events which specify a target task. Due to these events are registered for each CPU(percpu), so perf_swevent_event will match the event multiple times, the number of repetitions is just the number of CPUs. + +On the other hand perf_tp_event will only match an event event at a time, so we will return after an event matched. + + + +2 perf sched record 支持 -p 1 但是不支持 task + +3 perf/trace record 显示current有时候不正常, 只显示pid, 不显示comm + stat_runtime current进程不正确 + +4 pr_debug 不支持 arm64 和 arm32 diff --git a/study/patch/perf/bug_fork b/study/patch/perf/bug_fork new file mode 100755 index 0000000..7fd69b5 Binary files /dev/null and b/study/patch/perf/bug_fork differ diff --git a/study/patch/perf/bug_fork.c b/study/patch/perf/bug_fork.c new file mode 100644 index 0000000..eb67e58 --- /dev/null +++ b/study/patch/perf/bug_fork.c @@ -0,0 +1,32 @@ +/* + * fork_test.c + * version 1 + * Created on: 2010-5-29 + * Author: wangth + */ +#include +#include +int main () +{ + pid_t fpid; //fpid表示fork函数返回的值 + int count=0; + + while( 1 ) { + fpid=fork(); + + if (fpid < 0) + printf("error in fork!"); + else if (fpid == 0) { + printf("Child pid = %d\n", getpid( )); + exit(0); + } + else { + if (waitpid(fpid, NULL, 0) != fpid) /* wait for first child */ + perror("waitpid error"); + printf("Parent PID = %d\n", getpid( )); + sleep(20); + } + } + + return 0; +} diff --git a/study/patch/perf/sem_test.c b/study/patch/perf/sem_test.c new file mode 100644 index 0000000..78a5535 --- /dev/null +++ b/study/patch/perf/sem_test.c @@ -0,0 +1,37 @@ +// http://blog.csdn.net/jianchaolv/article/details/7544316 +#include +#include +#include +#include + + +sem_t sem_a; +void *task1(); + +int main(void){ + int ret = 0; + pthread_t thrd1; + pthread_t thrd2; + + printf("pid = %d\n", getpid( )); + sem_init(&sem_a,0,1); + ret=pthread_create(&thrd1,NULL,task1,NULL); //创建子线程 + ret=pthread_create(&thrd2,NULL,task1,NULL); //创建子线程 + + pthread_join(thrd1,NULL); //等待子线程结束 + pthread_join(thrd2,NULL); //等待子线程结束 + +} + +void *task1() +{ + int sval = 0; + while(1) + { + sem_wait(&sem_a); //持有信号量 + sleep(1); //do_nothing + sem_getvalue(&sem_a,&sval); + printf("tid = %ld, sem value = %d\n", pthread_self( ), sval); + sem_post(&sem_a); //释放信号量 + } +}