[Linux] 釋放虛擬記憶體 (cache)

Linux Kernel 2.6.16 之後加入了 drop caches 的機制,可以讓系統清出多餘的記憶體,這對於搞嵌入式系統相當重要阿,Memory 不夠就不能 upgrade firmware,我們只要利用讀寫 proc 檔案就可以清除 cache 記憶體檔案,底下是操作步驟:

釋放 pagecache:捨棄一般沒使用的 cache

echo 1 > /proc/sys/vm/drop_caches

釋放 dentries and inodes

echo 2 > /proc/sys/vm/drop_caches

釋放 pagecache, dentries and inodes

echo 3 > /proc/sys/vm/drop_caches

Reference: Drop Caches 觀察 Linux 的虛擬記憶體

[Linux] 嵌入式系統不可或缺的工具 – busybox 分析 ifconfig command

Busybox

玩過嵌入式系統的使用者,一定都會知道 Busybox,它提供一些小型 Linux command,方便在 console 端使用,以及一些 C 語言或者是 shell script 裡面,大家都知道 ifconfig 這指令,為了從 Kernel 2.6.15 轉換到 2.6.34.7 版本,原本的 Busybox 版本只有 1.0.1,現在已經到 1.18.1,轉換過程改了 Kernel netfilter 部份,以及 user space 部份 iptables extension。ifconfig 是 Busybox 其中一個指令用來查看目前有多少網路介面(network interface),來看看他是如何得到這些 interface 資訊,包含介面名稱、type、IP Adress、IP network mask、HW address 等….。

要讀取 interface 相關資訊可以透過兩種方式,一種是讀取 (IPv6 是 /proc/net/if_inet6),另一種透過 Socket 連接SOCK_DGRAM,最後用 iotcl 方式讀取 interface 相關資料,busybox 會先偵測檔案 /proc/net/dev 是否存在,如果 Kernel 有支援,就會讀取此檔案,如果不存在,則利用 socket 讀取資料。

if_readlist_proc 函式裡面:

1
2
3
4
fh = fopen_or_warn(_PATH_PROCNET_DEV, "r");
if (!fh) {
    return if_readconf();
}

看一下 /proc/net/dev 內容

1
2
3
4
Inter-|   Receive                                                |  Transmit
 face |bytes    packets errs drop fifo frame compressed multicast|bytes    packets errs drop fifo colls carrier compressed
    lo:     104       1    0    0    0     0          0         0      104       1    0    0    0     0       0          0
  eth0:21798505   51360    0    0    0     0          0         0  7693686   46844    0    0    0     0       0          0
[Read More]