# Ubuntu 16.04.6 LTS ###### tags: `林納斯` ![](https://i.imgur.com/FSMWrua.png) :::success :::spoiler 目錄 [TOC] ::: --- ## Problem 1 ### Useful Infos - https://elixir.bootlin.com/linux/v3.10.104/source - https://chat.openai.com/chat ### Source Code of Interest - [`struct task_struct`](https://elixir.bootlin.com/linux/v3.10.104/source/include/linux/sched.h#L1041) <small>*(`include/linux/sched.h:1041`)*</small> - [`struct thread_struct`](https://elixir.bootlin.com/linux/v3.10.104/source/arch/x86/include/asm/processor.h#L442) <small>*(`arch/x86/include/asm/processor.h:442`)*</small> - [`struct thread_info`](https://elixir.bootlin.com/linux/v3.10.104/source/arch/x86/include/asm/thread_info.h#L25) <small>*(`arch/x86/include/asm/thread_info.h:25`)*</small> - [`unsigned int task_cpu(...)`](https://elixir.bootlin.com/linux/v3.10.104/source/include/linux/sched.h#L2628) <small>*(`include/linux/sched.h:2628`)*</small> - [`#define task_thread_info(...)`](https://elixir.bootlin.com/linux/v3.10.104/source/include/linux/sched.h#L2322) <small>*(`include/linux/sched.h:2322`)*</small> - [`#define smp_processor_id()`](https://elixir.bootlin.com/linux/v3.10.104/source/include/linux/smp.h#L218) <small>*(`include/linux/smp.h:218`)*</small> ### Utilities - `cat /proc/cpuinfo` - `lscpu` - :::spoiler Expert System ![](https://i.imgur.com/zMuPz62.png) ::: ### Topics - Logical & physical CPU ID - taskset - current --- ## Problem 2 ### Useful Infos - https://www.maizure.org/projects/evolution_x86_context_switch_linux/ - http://linuxperf.com/?p=209 - https://www.jianshu.com/p/42abcc2c9e50 - https://www.cnblogs.com/long123king/p/3501853.html ### Source Code of Interest - [`void schedule(void)`](https://elixir.bootlin.com/linux/v3.10.104/source/kernel/sched/core.c#L3052) <small>*(`kernel/sched/core.c:3052`)*</small> - [`void __schedule(void)`](https://elixir.bootlin.com/linux/v3.10.104/source/kernel/sched/core.c#L2955) <small>*(`kernel/sched/core.c:2955`)*</small> - [`void context_switch(...)`](https://elixir.bootlin.com/linux/v3.10.104/source/kernel/sched/core.c#L1984) <small>*(`kernel/sched/core.c:1984`)*</small> - [`#define switch_to(...)`](https://elixir.bootlin.com/linux/v3.10.104/source/arch/x86/include/asm/switch_to.h#L104) <small>*(`arch/x86/include/asm/switch_to.h:104`)*</small> - ~~[`#define switch_to(...)`](https://elixir.bootlin.com/linux/v3.10.104/source/include/asm-generic/switch_to.h#L25)~~ <small>*(`include/asm-generic/switch_to.h:25`)*</small> - [`struct task_struct* __switch_to(...)`](https://elixir.bootlin.com/linux/v3.10.104/source/arch/x86/kernel/process_64.c#L272) <small>*(`arch/x86/kernel/process_64.c:272`)*</small> - [`struct tss_struct`](https://elixir.bootlin.com/linux/v3.10.104/source/arch/x86/include/asm/processor.h#L264) <small>*(`arch/x86/include/asm/processor.h:264`)*</small> - [`struct x86_hw_tss`](https://elixir.bootlin.com/linux/v3.10.104/source/arch/x86/include/asm/processor.h#L240) <small>*(`arch/x86/include/asm/processor.h:240`)*</small> ### Topics - `nvcsw` / `nivcsw` - `sleep` / `usleep` / `nanosleep` - `sleep_for` - `gettimeofday` - task state - assertion 'sd_id12B_randomize(&id) >= 0' failed at ../src/core/dbus.c:668, function bud_on_connection(). Aborting. - ![](https://i.imgur.com/RVnSgCA.png) - inline assembly - cpu-bound / io-bound - the following makes different results ```cpp= std::ios_base::sync_with_stdio(false); // std::cin.tie(nullptr); ``` - ==TODO== analyze the 2 situation in `proj3-iobnd` *(6 situations in total)*: - print way: - C `printf` - C++ `std::cout` - C++ `std::cout` *(accelerated)* - with many '\n' or flush ***OR*** stdout to TERMINAL or FILE - https://stackoverflow.com/questions/1716296/why-does-printf-not-flush-after-the-call-unless-a-newline-is-in-the-format-strin - https://stackoverflow.com/questions/5229096/does-printf-always-flush-the-buffer-on-encountering-a-newline - `time ./bin/proj3-iobnd 2> 1.log > 2.log` vs. `time ./bin/proj3-iobnd 2> 1.log` - ==TODO== `strace` - https://stackoverflow.com/questions/6604007/how-can-i-get-a-list-of-linux-system-calls-and-number-of-args-they-take-automati - https://github.com/strace/strace/blob/v4.11/linux/x86_64/syscallent.h - `strace -et=316,317,319 -C ./proj3-cpunum` #### Schedule - runqueue - CFS - https://hackmd.io/@RinHizakura/S1opp7-mP - https://hackmd.io/@sysprog/linux-scheduler?type=view#Linux-%E6%A0%B8%E5%BF%83%E8%A8%AD%E8%A8%88-%E4%B8%8D%E5%8F%AA%E6%8C%91%E9%81%B8%E4%BB%BB%E5%8B%99%E7%9A%84%E6%8E%92%E7%A8%8B%E5%99%A8 - https://blog.csdn.net/wukongmingjing/article/details/83106431 - https://liujunming.top/2018/11/20/%E8%BF%9B%E7%A8%8B%E5%88%87%E6%8D%A2switch-to%E5%AE%8F%E7%AC%AC%E4%B8%89%E4%B8%AA%E5%8F%82%E6%95%B0%E5%88%86%E6%9E%90/ - https://man7.org/linux/man-pages/man7/sched.7.html - https://pinyu.tw/?p=161