A Piece Of: Golang Profile

Tips on Profile 先说几个小坑点: 默认情况下,Golang Profile 取样的频率为 100Hz,意味着每 10ms 才会取样一次,所以程序运行时间少于 10ms 肯定不会被取样 但是我们可以通过 runtime.SetCPUProfileRate() 重新设置值,重新进行调试,比如将采样率设置为 1000,每 1ms 就会取样一次,但是这样对性能的开销很大,Golang 官方建议这个值不要设置在 200 以上 其实最好的还是让程序多运行一段时间,比如 1s 左右,个人测试即使将采样率调高,如果一个程序只运行 10ms 左右,采样的结果也大概率为空 在正确位置设置上述语句后,程序提示 “runtime: cannot set cpu profile rate until previous profile has finished.",这句错误提示简直不明不白的,我第一次看到这个错误提示还以为就是字面意思:系统中可能存在正在运行的 profile 程序 查资料后发现,runtime.SetCPUProfileRate() 是一种比较 Hack 的做法,所以该错误提示可以忽略 在程序运行时,直接通过 kill 命令结束程序的话,最后生成的 profile 为空(?),最好使用下面的这个模式: sigs := make(chan os.Signal, 1) signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM) server := NewServer(port, connMap) go server.Run() <-sigs Log.Info("Shutting down server") 通过 signal.Notify 来拦截 kill 的信号,保证程序能够正常结束,profile 文件不为空 How to Profile if pprofFlag { // runtime.SetCPUProfileRate(1000) cpuFile, err := os.Create("executor_profile.prof") if err != nil { fmt.Println("无法创建 CPU profile 文件:", err) return } defer cpuFile.Close() // 开始 CPU profile if err := pprof.StartCPUProfile(cpuFile); err != nil { fmt.Println("无法启动 CPU profile:", err) return } defer pprof.StopCPUProfile() } 注意这段代码只能放在 main 函数中,否则 defer 语句的语义会有影响 How to Read Profile Results 命令行 (pprof) top 10 Showing nodes accounting for 40ms, 100% of 40ms total Showing top 10 nodes out of 26 flat flat% sum% cum cum% 10ms 25.00% 25.00% 10ms 25.00% os.(*File).checkValid (inline) 10ms 25.00% 50.00% 10ms 25.00% runtime.(*mheap).allocSpan 10ms 25.00% 75.00% 10ms 25.00% runtime.(*profBuf).read 10ms 25.00% 100% 10ms 25.00% runtime.startm 0 0% 100% 10ms 25.00% log.(*Logger).Output 0 0% 100% 10ms 25.00% log.Println 0 0% 100% 10ms 25.00% main.main.func1 0 0% 100% 10ms 25.00% os.(*File).Write 0 0% 100% 10ms 25.00% runtime.(*mcache).nextFree 0 0% 100% 10ms 25.00% runtime.(*mcache).refill flat:该函数本身直接消耗的时间。比如,flat = 10ms 表示这个函数自身消耗了 10 毫秒的 CPU 时间,而不是它调用的其他函数所消耗的时间。 ...

October 23, 2024 · Last updated on August 1, 2025 · 4 min · KKKZOZ

Missing Semester - Lecture 5

Job Control Signals can do other things beyond killing a process. For instance, SIGSTOP pauses a process. In the terminal, typing Ctrl-Z will prompt the shell to send a SIGTSTP signal, short for Terminal Stop. We can then continue the paused job in the foreground or in the background using fg or bg. One more thing to know is that the & suffix in a command will run the command in the background, giving you the prompt back. ...

October 17, 2024 · Last updated on August 1, 2025 · 4 min · KKKZOZ

Missing Semester - Lecture 4

# journalctl run remotely, grep run locally ssh myserver journalctl | grep sshd # both commands run remotely ssh myserver "journalctl | grep sshd" sed sed (Stream Editor) is a powerful command-line tool in Unix/Linux that is used for parsing and transforming text, typically used for finding, replacing, or deleting content in a file or input stream. Common sed Commands: Substitution (s): sed 's/pattern/replacement/' file s: The substitution command. pattern: The string or pattern to search for. replacement: The string to replace the pattern with. Example: ...

October 15, 2024 · Last updated on August 1, 2025 · 7 min · KKKZOZ

Missing Semester - Lecture 2

Shell Scripting To assign variables in bash, use the syntax foo=bar and access the value of the variable with $foo. Note that foo = bar will not work since it is interpreted as calling the foo program with arguments = and bar. bash uses a variety of special variables to refer to arguments, error codes, and other relevant variables: $0 - Name of the script $1 to $9 - Arguments to the script. $1 is the first argument and so on. $@ - All the arguments $# - Number of arguments $? - Return code of the previous command $$ - Process identification number (PID) for the current script !! - Entire last command, including arguments. A common pattern is to execute a command only for it to fail due to missing permissions; you can quickly re-execute the command with sudo by doing sudo !! $_ - Last argument from the last command. If you are in an interactive shell, you can also quickly get this value by typing Esc followed by . or Alt+. Exit codes can be used to conditionally execute commands using && (and operator) and || (or operator). ...

October 11, 2024 · Last updated on August 1, 2025 · 5 min · KKKZOZ

Missing Semester - Lecture 1

The shell How does the shell know how to find the date or echo programs? The shell is a programming environment, just like Python, and so it has variables, conditionals, loops, and functions. If the shell is asked to execute a command that doesn’t match one of its programming keywords, it consults an environment variable called $PATH that lists which directories the shell should search for programs when it is given a command ...

October 9, 2024 · Last updated on August 1, 2025 · 2 min · KKKZOZ

Watchdogs in OpenFaaS

Classic Watchdog OpenFaaS 的 Classic Watchdog 是一个用于处理无服务器(Serverless)函数请求的核心组件。它是 OpenFaaS 的默认函数执行器,用于在容器内运行用户定义的函数代码,并通过 HTTP 请求与外部进行通信。 工作原理 Classic Watchdog 主要负责以下任务: 启动函数: 当容器启动时,Classic Watchdog 会启动并监听 HTTP 端口。 它会执行用户提供的函数代码,通过 exec 启动一个新的进程运行函数。 处理请求: Classic Watchdog 监听指定端口上的 HTTP 请求。 当收到请求时,它会将请求的内容传递给函数进程,并等待函数的响应。 返回响应: 函数进程处理请求后,将结果返回给 Classic Watchdog。 Classic Watchdog 将函数的响应封装成 HTTP 响应,并返回给调用方。 A tiny web-server or shim that forks your desired process for every incoming HTTP request Every function needs to embed this binary and use it as its ENTRYPOINT or CMD, in effect it is the init process for your container. Once your process is forked the watchdog passses in the HTTP request via stdin and reads a HTTP response via stdout. This means your process does not need to know anything about the web or HTTP. ...

August 25, 2024 · Last updated on August 1, 2025 · 7 min · KKKZOZ

A Piece Of: Logs

Log Levels Log levels for software applications have a rich history dating back to the 1980s. One of the earliest and most influential logging solutions for Unix systems, Syslog, introduced a range of severity levels, which provided the first standardized framework for categorizing log entries based on their impact or urgency. The following are the levels defined by Syslog in descending order of severity: Emergency (emerg): indicates that the system is unusable and requires immediate attention. Alert (alert): indicates that immediate action is necessary to resolve a critical issue. Critical (crit): signifies critical conditions in the program that demand intervention to prevent system failure. Error (error): indicates error conditions that impair some operation but are less severe than critical situations. Warning (warn): signifies potential issues that may lead to errors or unexpected behavior in the future if not addressed. Notice (notice): applies to normal but significant conditions that may require monitoring. Informational (info): includes messages that provide a record of the normal operation of the system. Debug (debug): intended for logging detailed information about the system for debugging purposes. The specific log levels available to you may defer depending on the programming language, logging framework, or service in use. However, in most cases, you can expect to encounter levels such as FATAL, ERROR, WARN, INFO, DEBUG, and TRACE. ...

May 13, 2024 · Last updated on August 1, 2025 · 5 min · KKKZOZ

Transaction Anomalies

Non-Repeatable Read A non-repeatable read occurs, when during the course of a transaction, a row is retrieved twice and the values within the row differ between reads. Phantom Read A phantom read occurs when, in the course of a transaction, two identical queries are executed, and the collection of rows returned by the second query is different from the first. Read Skew Read skew is that with two different queries, a transaction reads inconsistent data because between the 1st and 2nd queries, other transactions insert, update or delete data and commit. Finally, an inconsistent result is produced by the inconsistent data. ...

March 31, 2024 · Last updated on August 1, 2025 · 2 min · KKKZOZ