acct 中文man頁面
名字
acct - 進程記賬文件
概要
#include <sys/acct.h>
描述
如果在內(nèi)核編譯時開啟了進程記賬選項(CONFIG_BSD_PROCESS_ACCT),則可以通過調(diào)用acct(2) 來開始進程記賬。如:
acct("/var/log/pacct");
在進程記賬開啟之后,每當系統(tǒng)內(nèi)有進程終止內(nèi)核將寫一個記錄到記賬文件。這個記錄包含已經(jīng)終止進程的信息,它定義在頭文件 <sys/acct.h>:
#define ACCT_COMM 16 typedef u_int16_t comp_t; struct acct { char ac_flag; /* 記賬標記 */ u_int16_t ac_uid; /* 記賬用戶 ID */ u_int16_t ac_gid; /* 記賬組 ID */ u_int16_t ac_tty; /* 控制終端 */ u_int32_t ac_btime; /* 進程創(chuàng)建時間 (從開機起的秒數(shù)) */ comp_t ac_utime; /* 用戶 CPU 時間 */ comp_t ac_stime; /* 系統(tǒng) CPU 時間 */ comp_t ac_etime; /* 流失的時間 */ comp_t ac_mem; /* 平均內(nèi)存用量 (kB) */ comp_t ac_io; /* Characters transferred (未使用) */ comp_t ac_rw; /* 讀寫的塊 (未使用) */ comp_t ac_minflt; /* Minor page faults */ comp_t ac_majflt; /* Major page faults */ comp_t ac_swaps; /* Number of swaps (unused) */ u_int32_t ac_exitcode; /* 進程終止狀態(tài) (參看 wait(2)) */ char ac_comm[ACCT_COMM+1]; /* 命令名 (執(zhí)行文件名;以0結(jié)尾) */ char ac_pad[X]; /* 填充字節(jié) */ }; enum { /* 在 ac_flag 域可以設(shè)置的位 */ AFORK = 0x01, /* 調(diào)用 fork 之后,但還沒有調(diào)用 exec */ ASU = 0x02, /* 使用超級用戶權(quán)限 */ ACORE = 0x08, /* 核心轉(zhuǎn)儲 */ AXSIG = 0x10 /* 由信號殺死 */ };
這里的 comp_t 數(shù)據(jù)類型是一個浮點值,是由以 8 為底的 3 位指數(shù)和 13 位尾數(shù)構(gòu)成。一個這個類型的值 c 可以通過下面的公式轉(zhuǎn)換為一個(長)整型:
v = (c & 0x1fff) << (((c >> 13) & 0x7) * 3);
ac_utime、ac_stime 和 ac_etime 的時間測量單位是時鐘嘀嗒,把這個值除以 sysconf(_SC_CLK_TCK) 轉(zhuǎn)換為秒數(shù)。
記賬文件格式版本 3
從內(nèi)核版本 2.6.8 開始,一個記賬文件的改善版本可以在編譯內(nèi)核時指定 CONFIG_BSD_PROCESS_ACCT_V3 來啟用,當設(shè)置這個選項之后,記賬文件里包含額外的域,并且 c_uid 和 ac_gid 域的寬度從 16 位增加到 32 位(在 2.4 內(nèi)核之后 UID 和 GID 寬度增加了)。這個新的記錄結(jié)構(gòu)如下:
struct acct_v3 { char ac_flag; /* Flags */ char ac_version; /* 總是設(shè)置為 ACCT_VERSION (3) */ u_int16_t ac_tty; /* 控制終端 */ u_int32_t ac_exitcode; /* 進程終止狀態(tài) */ u_int32_t ac_uid; /* 真實用戶 ID */ u_int32_t ac_gid; /* 真實組 ID */ u_int32_t ac_pid; /* 進程 ID */ u_int32_t ac_ppid; /* 父進程 ID */ u_int32_t ac_btime; /* 進程創(chuàng)建時間 */ float ac_etime; /* 流失的埋單 */ comp_t ac_utime; /* 用戶 CPU 時間 */ comp_t ac_stime; /* 系統(tǒng)時間 */ comp_t ac_mem; /* 平均內(nèi)存使用量 (kB) */ comp_t ac_io; /* 傳輸?shù)淖址麛?shù)量 (未使用) */ comp_t ac_rw; /* 讀寫的塊數(shù) (未使用) */ comp_t ac_minflt; /* 次頁異常 */ comp_t ac_majflt; /* 主頁異常 */ comp_t ac_swaps; /* 交換的次數(shù) (未使用) */ char ac_comm[ACCT_COMM]; /* 命令名 */ };
版本
acct_v3 結(jié)構(gòu)體在 glibc 2.6 之后定義。
遵循于
進程記賬最初出現(xiàn)在 BSD,現(xiàn)在則在大多數(shù)系統(tǒng)存在。它是一個沒有標準化的特性,并且在系統(tǒng)之間存在許多細節(jié)的不同。
注意
記賬文件里記錄以進程終止的時間順序排列。
包括 2.6.9 在內(nèi)或之后的內(nèi)核,當使用 NPTL 線程庫時,當一個線程被創(chuàng)建時會添加一個分隔記賬記錄;當整個進程的最后一個線程終止時,從 2.6.10 一個單獨記賬記錄會寫入。
proc/sys/kernel/acct 文件在 proc(5) 里描述,它定義當磁盤空間不足時如何控制進程記賬行為。
參看
lastcomm(1), acct(2), accton(8), sa(8)
#p#
NAME
acct - execution accounting file
SYNOPSIS
#include <sys/acct.h>
DESCRIPTION
If the kernel was compiled with the process accounting option enabled, the system call
- acct("/somewhere/accountingfile");
will start the process accounting. Each time a process terminates a record for this process is appended to the accounting file. The accounting structure struct acct is also described in the file /usr/include/linux/acct.h.
SEE ALSO
sa(1), acct(2)