性能分析备忘录

Perf Debian 安装 perf 1 sudo apt install linux-perf 收集分支预测事件 1 perf stat -e branch-instructions,branch-misses <your_program> branch-instructions:分支指令的总数 branch-misses:分支预测失败的次数 1 hit_rate = 1 - branch-misses / branch-instructions //命中率 以下是一个包含多个复杂分支跳转的 C 语言程序示例。这个程序包含了多层嵌套的条件判断、循环和函数调用,以演示复杂的分支逻辑 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 #include <stdio.h> #include <stdlib.h> #include <time.h> // 模拟复杂条件的函数 int complex_condition(int x) { if (x % 2 == 0) { if (x % 3 == 0) { return 1; // 可被2和3整除 } else { if (x % 5 == 0) { return 2; // 可被2和5整除 } else { return 3; // 可被2整除,但不能被3或5整除 } } } else { if (x % 7 == 0) { return 4; // 可被7整除 } else { return 5; // 不能被2或7整除 } } } // 递归函数,模拟复杂的分支跳转 int recursive_function(int depth) { if (depth <= 0) { return 0; } else { if (depth % 2 == 0) { return depth * recursive_function(depth - 1); } else { return depth + recursive_function(depth - 1); } } } // 主函数,包含多重循环和分支判断 int main() { srand(time(NULL)); // 用当前时间作为随机数种子 // 模拟分支跳转 for (int i = 0; i < 20; i++) { int rand_num = rand() % 100 + 1; // 生成1到100之间的随机数 int result = complex_condition(rand_num); switch (result) { case 1: printf("Number %d is divisible by 2 and 3.\n", rand_num); break; case 2: printf("Number %d is divisible by 2 and 5.\n", rand_num); break; case 3: printf("Number %d is divisible by 2, but not by 3 or 5.\n", rand_num); break; case 4: printf("Number %d is divisible by 7.\n", rand_num); break; case 5: printf("Number %d is not divisible by 2 or 7.\n", rand_num); break; default: printf("Unexpected result for number %d.\n", rand_num); break; } } // 递归调用,模拟更复杂的分支 int depth = rand() % 10 + 1; printf("\nRecursive function result for depth %d: %d\n", depth, recursive_function(depth)); return 0; } 编译之后,使用 perf 进行分支预测事件统计得到结果: ...

十一月 6, 2024 · 6 分钟 · 1105 字 · HCY

SoK: Sanitizing for Security 论文结构梳理

Abstract I. Inctrodcution II. Exploit Mitigations vs. Sanitizers III. Low-Level Vulnerabilities A. Memory Safety Violations (1) Spatial Safety Violatoins (2) Temporal Safety Violations B. Use of Uninitialized Variables C. Pointer Type Errors D. Variadic Function Misuse E. Other Vulnerabilities IV. Bug Finding Techiques A. Memory Safety Violations Location-based Access Checkers Identity-based Access Checkers (1) Spatial Memory Safety Violations Red-zone Insertion Guard Pages Per-pointer Bounds Tracking Per-object Bounds Tracking (2) Temporal Memory Safety Violations Reuse Delay Lock-and-key Dangling Pointer Tagging B. Use of Uninitailized Variables Uninitailized Memory Read Detection Uninitialized Value Use Dectection C. Pointer Type Errors Pointer Casting Monitor Pointer Use Monitor D. Variadic Function Misuse Dangerous Format String Detection Argument Mismatch Detection E. Other Vulnerabilities V. Program Instumentation A. Language-level Instrumentation B. IR-level Instrumentation C. Binary Instrumentation D. Library Interposition VI. Metadata Management A. Object Metadata Embedded Metadata Direct-mapped Shadow Multi-level Shadow Custom Data Structure B. Pointer Metadata Fat Pointers Tagged Pointers Disjoint Metadata C. Static Metadata VII. Driving A Sanitizer VIII. Analysis A. False Positives B. False Negatives C. Incomplete Instrumentation D. Tread Safety E. Performance Overhead F. Memory Overhead IX. Deployment A. Methodology Popular Github repositories Sanitizer web pages Search trends B. Findings AddressSanitizer is the most widely adopted sanitizer The adoption rate for other LLVM-based sanitizers is lower C. Deployment Directions X. Future Research And Development Directions A. Type Error Detection B. Improving Compatibility C. Composing Sanitizers D. Hardware Support E. Kernel and Bare-Metal Support XI. Conclusion

十月 22, 2024 · 2 分钟 · 241 字 · HCY

使用 C++ 理解 23 种设计模式

开一个坑,使用C++实现23种设计模式

十月 9, 2024 · 1 分钟 · 1 字 · HCY

Tmux 使用

(Neo)VIM 在 tmux 里面配色不对 在 .bashrc 里面加上一句 1 alias tmux='tmux -2' 最后 source .bashrc 立即生效 (Neo)VIM 在 tmux 里面特殊字符以及中文显示不正常 在 .bashrc 里面加上一句 1 export LANG=en_US.UTF-8 最后 source .bashrc 立即生效 (Neo)VIM 在配饰 OSCYank 之后,在 tmux 中依旧无法剪切到系统剪切板 首先创建 tmux 配置文件 1 touch ~/.tmux.conf 在 tmux 配置文件里面写入 1 set-option -g set-clipboard on 重启 tmux 生效 在 tmux session 里面切换 session Ctrl+b s (means [s]ession) 在 tmux 里面进入滚动模式 Ctrl+b [ q 退出 ...

一月 24, 2024 · 1 分钟 · 68 字 · HCY

Debian Gnome 修改默认目录

我在 askbuntu 里面找到了答案: Change default user folders path 具体做法: 1 vi ~/.config/user-dirs.dirs 以下是我的配置: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 # This file is written by xdg-user-dirs-update # If you want to change or add directories, just edit the line you're # interested in. All local changes will be retained on the next run. # Format is XDG_xxx_DIR="$HOME/yyy", where yyy is a shell-escaped # homedir-relative path, or XDG_xxx_DIR="/yyy", where /yyy is an # absolute path. No other format is supported. # XDG_DESKTOP_DIR="/home/hcy/Desktop/" XDG_DOWNLOAD_DIR="/home/hcy/Downloads/" XDG_TEMPLATES_DIR="/home/hcy/Templates/" XDG_PUBLICSHARE_DIR="/home/hcy/Public/" XDG_DOCUMENTS_DIR="/home/hcy/Documents/" XDG_MUSIC_DIR="/home/hcy/Music/" XDG_PICTURES_DIR="/home/hcy/Pictures/" XDG_VIDEOS_DIR="/home/hcy/Videos/"

一月 23, 2024 · 1 分钟 · 97 字 · HCY