Linux Kernal

Deep dive into the largest open source program

Posted by Eirik on 27 Mar, 2024

What is Linux Kernal

  • The kernal is a program
  • Often with a name like vmlinuz-
  • Loaded a run by a noot loader, like GRUB(linux)
GRUB
  • GRUB: Load the Kernal program from disk to memory, and transfer the control to kernal

  • GRUB comes after Power-On-self-Test(POST)

  • The Kernal is an API

    • System calls
    • Virtual file system entries (not really a directory)
      • proc
      • sys
      • debugfs
    • Device files(standard system calls, read, write, open,…etc)
  • The Kernal is a gatekeeper

    • Enforce priviledges(capabilities)
    • Execute supervisor instructions (assembly language)
    • Implements security policies
    • Control access to hardware and other resources
  • The Kernal is Modualar

    • Kernal image is small
    • Kernal image is sufficient to boot to user space
    • Kernal can only load the modules that users need (optional functionality)

Where is kernal

cd /boot/ uname -r ls -l vmlinuz-*

Contorl Layers

Application <-> Library (Std C Lib, libgc) <-> Kernel <-> disk | keyboard | mouse | …etc

Commands for HW info

  • lshw | lspci
  • lsusb | lsbk
  • lscpu | lsdev
  • hdparm
  • Write to proc, dev, or sys files
  • inb | outb
  • setpci

System Calls

  • System calls are functions implemented by the kernel and meant to be called from users space

  • dmesg Shows RAM buffer message from kernal Journal control: tail -f /var/log/nessages can be handy

在Linux中,“load"通常指的是系統的負載情況,它用來表示系統當前正在處理的工作量。在Linux中,有三種類型的負載指標,分別是:

1分鐘平均負載(1-minute Load Average) 5分鐘平均負載(5-minute Load Average) 15分鐘平均負載(15-minute Load Average) 這些平均負載是指過去1分鐘、5分鐘和15分鐘內系統的平均活動量。通常情況下,這三個指標會同時顯示,例如:

lua Copy code load average: 0.08, 0.13, 0.15 其中,0.08代表1分鐘內的平均負載,0.13代表5分鐘內的平均負載,0.15代表15分鐘內的平均負載。

這些負載指標的計算方式是基於運行隊列的長度。運行隊列是指當前正在運行和等待運行的進程隊列長度。如果運行隊列的長度超過了系統的處理能力,系統的負載就會增加。

在Linux系統中,可以通過多種方式查看系統的負載情況:

使用 uptime 命令:在終端輸入 uptime 命令,會顯示系統的平均負載以及系統運行時間等信息。

使用 top 或 htop 命令:在終端輸入 top 或 htop 命令,可以查看系統當前運行的進程以及系統的負載情況。

查看 /proc/loadavg 文件:在終端輸入 cat /proc/loadavg 命令,可以直接查看系統的平均負載。

使用 sysstat 工具:sysstat 是一個性能監控工具包,其中包含了 sar、iostat、mpstat 等工具,可以用來查看系統的負載情況以及其他性能指標。

這些方法都可以用來查看Linux系統的負載情況,根據具體需求選擇合適的方法來監控系統性能。

Linux系統的負載指標實際上是在描述系統中運行隊列的長度。這個隊列包含了正在運行的進程以及等待運行的進程。因此,負載指標的數字實際上代表了系統中等待運行的進程數的平均數量。

具體來說,這些數字可以解讀如下:

如果1分鐘、5分鐘、15分鐘的平均負載都小於系統的 CPU 核心數量,那麽系統的運行狀況是良好的,系統的處理能力足夠處理當前的工作負載。 如果平均負載持續地高於 CPU 核心數量,表示系統的運行隊列中有大量的任務在等待執行,這可能意味著系統過載了,處理能力不足以應對當前的工作負載。 負載數值的絕對大小並不是唯一的標準,因為不同系統的處理能力不同,負載在什麽水平被認為是高負載也會有所不同。一般來說,當負載持續高於系統 CPU 核心數量的 70-80% 時,就可以認為系統開始變得過載了。 總的來說,負載指標是用來評估系統的運行狀況和負載情況的重要指標,通過監控這些指標可以及時發現系統是否過載,以及是否需要進行優化或者增加系統資源來應對高負載情況。

$ uptime
18:40:03 up  8:37,  1 user,  load average: 0.42 0.70 1.13
    
$ top
top - 18:40:03 up  8:37,  1 user,  load average: 0.42 0.70 1.13
    
$ cat /proc/loadavg 
0.42 0.70 1.13 2/1224 67252

load = #running tasks + #uninterrupt tasks 可以理解為正在運行的process 數

如果load = CPU 核心數大概是理想的運行狀態,若load > 5*CPU核心數 就是嚴重過載

Reference to Load Average 負載解讀: 如果是 1 表示有一個 process 正在執行或等待 CPU 運算;5 表示有 5 個 process 正在執行或等待 CPU 運算

load balance 的計算實際上不僅包含 running,實際上還會包含進入睡眠的 uninterruptible 類型的任務嗎?

在 Linux 中,load average 展示的是以系統資源的角度,因此將包含正在工作以及等待工作(無論是 CPU / disk / uninterruptible lock)的任務 在其他作業系統裡,load average 通常僅指 CPU 資源的負載

How to add users to sudo list?

Open the sudoers file(/etc/sudoers) for editing: You should use the visudo command, which opens the sudoers file in a safe manner to prevent corruption. This command ensures that only one person is editing the file at a time and checks for syntax errors before saving.

sudo visudo