2022年3月19日 星期六

Linux uptime

uptime 指的是 time since boot (開機多久、開機計時),但指令 uptime 除了顯示開機多久外,還有目前時間、和平均負載等。

busybox,目前時間是透過 time() 取得,透過 localtime() 轉成顯示格式。開機多久和平均負載是透過 sysinfo() 取得。sysinfo() 是 Linux 特有的系統呼叫 (大部分使用編號 116),busybox 的 free、ps (ENABLE_FEATURE_PS_TIME 時) 和 init (ENABLE_SWAPONOFF 時) 也有用到。

在 Linux kernel,系統呼叫 sysinfo() 是 sys_sysinfo(),主要工作是由 do_sysinfo() 完成。

開機多久在 Linux kernel 稱為 monotonic clock,意思是這個時間是嚴格線性增加的,也就是說只能隨著時間穩定增加不能調整。實際上開機多久是紀錄開機時 wall clock (變數 xtime) 轉為負值 (變數 wall_to_monotonic),所以 xtime 加上 wall_to_monotonic 就是開機多久。(為什麼不用 jiffies 轉換就好?)

理想上,開機時 wall_to_monotonic 初始化為 xtime 的負值之後就不用再動,但 xtime 除了「隨著時間穩定增加」外,可能會調整而有突然的跳躍。這調整不屬於計時的一部分,需要同時補償調整 wall_to_monotonic,包括 do_settimeofday() 改設時間、second_overflow() 閏秒調整、和 timekeeping_resume() (suspend 期間不算 uptime?)。

系統呼叫 clock_gettime(CLOCK_MONOTONIC) 也可以取得開機多久,包含較精確的 nsec,沒有平均負載部份。/proc/stats 的 btime 是開機時間點,也就是將 wall_to_monotonic 轉回正值。

kernel 應用:ktime_get_ts()、hrtimer_get_softirq_time()、retrigger_next_event() 會使用開機時間。

參考

  1. busybox、uClibc、和 Linux kernel 的原始碼
  2. https://stackoverflow.com/questions/3523442/difference-between-clock-realtime-and-clock-monotonic

2022年3月18日 星期五

buxybox

BusyBox 集合幾百個簡化的 UNIX utilities 成單一執行檔,可省下可觀的儲存空間,執行時也減少使用的資源。例如已執行 busybox 的第一個指令,要執行第二個指令就不用再載入記憶體,只需要 busybox 內部比對要執行哪個部份,並不用透過函式庫就可以共享程式碼。
問題:
  1. 在 Embeddded System,執行時需要載入到記憶體,如果 busybox 本身加了許多不必要的指令,便會多佔記憶體。如果某些很少用的指令不放到 busybox 裡,獨立出來,要執行時才載入去佔用記憶體,是不是反而比較好。所以未必所有指令都放到 busybox 較好?

指令

參考:

  1. BusyBox (wikipedia)
  2. http://www.ibm.com/developerworks/library/l-busybox/index.html

2022年3月12日 星期六

Linux clock_gettime()

int clock_gettime(clockid_t clk_id, struct timespec *tp);

系統呼叫 clock_gettime() 取得特定時鐘 (clk_id) 的時間,回傳秒數及 nsec,實際解析度可透過 clock_getres() 查詢。另外 clock_settime() 也可以設定特定時鐘,但有權限限制,並且不是每個時鐘都可以設定。

clk_id 可能這些,定義在 <unistd.h> 的 _POSIX_TIMERS 可知支援哪些:

  • CLOCK_REALTIME:也就是 wall clock,用 Epoch 開始的時間,所有實作皆支援。CLOCK_REALTIME 隨著時間線性穩定增加,節奏受到 adjtime() 和 NTP 節制。此外,可能會手動調整而有突然的跳躍,此部份不影響 nanosleep() 等相對時間計時的到期。
  • CLOCK_REALTIME_COARSE (Linux 2.6.32+):較快但較不精確版本的 CLOCK_REALTIME。
  • CLOCK_MONOTONIC: monotonic 意思是只能隨著時間線性穩定增加,是無法設定的或手動調整的。在 Linux 是開機啟算的時間,也就是 uptime。不計 suspend 的時間。
  • CLOCK_MONOTONIC_COARSE (Linux 2.6.32+):較快但較不精確版本的 CLOCK_MONOTONIC。
  • CLOCK_MONOTONIC_RAW (Linux 2.6.28+):類似 CLOCK_MONOTONIC,但是是取得硬體版本的時間,計數節奏不受軟體規範。不計 suspend 的時間。
  • CLOCK_BOOTTIME (Linux 2.6.39+):和 CLOCK_MONOTONIC 一樣,但包括 suspend 時間。
  • CLOCK_PROCESS_CPUTIME_ID (Linux 2.6.12+):計數單一 process 的 CPU 使用時間,包括旗下所有 thread。
  • CLOCK_THREAD_CPUTIME_ID (Linux 2.6.12+):計數單一 thread 的 CPU 使用時間。

參考

  1. man clock_gettime
  • https://lirobo.blogspot.com/2014/12/linux-time.html
  • 有些處理器架構,clock_gettime() 的實作由 vdso(7) 提供。
  • date(1), gettimeofday(2), settimeofday(2), time(2), adjtime(3), clock_getcpuclockid(3), ctime(3), ftime(3), pthread_getcpuclockid(3), sysconf(3), time(7), vdso(7), hwclock(8)

crontab

https://blog.gtwang.org/linux/linux-crontab-cron-job-tutorial-and-examples/

Crontab 每兩週執行一次
每天半夜亂數時間執行一次

2022年3月4日 星期五

Q.931 Cause

Q.931 Cause (ITU Q.931 section 4.5.12,定義在 Q.850)
87654321Octet
IE ID: 0x08 1
Length2
ext.
0/1
Coding
Std.
0Location3
ext.
1
Recommendation3a
ext.
1
Cause Value4
Diagnostic(s)5
  • Coding Standard:編碼標準,無法度用 ITU-T 標準才使用其它標準。
    • 0 = ITU-T 標準
    • 1 = ISO/IEC 標準
    • 2 = national 標準
    • 3 = standard specific to identified location
  • Location
    • 0 = user (U)
    • 1 = private network serving the local user (LPN)
    • 2 = public network serving the local user (LN)
    • 3 = transit network (TN)
    • 4 = public network serving the remote user (RLN)
    • 5 = private network serving the remote user (RPN)
    • 7 = international network (INTL)
    • 10 = network beyond interworking point (BI)
    • 12~15:reserved for national use
    • 其它:spare
  • Cause Value:其中 bit 7~5 是 class 分類,表示 the general nature of the event。如下表:
ClassHex Dec Cause
0, 1 =
normal
01 1 Unallocated or unassigned number
02 2 No route to specified transit network (Transit Network Identity)
03 3 No route to destination
04 4 Send special information tone
05 5 Misdialed trunk prefix
06 6 Channel unacceptable
07 7 Call awarded and being delivered in an established channel
08 8 Prefix 0 dialed but not allowed
09 9 Prefix 1 dialed but not allowed
0A 10 Prefix 1 not dialed but required
0B 11 More digits received than allowed, call is proceeding
10 16 Normal call clearing. 如果是呼叫失敗,可能是高層協定 (如 PPP)、認證、或 idle timeout 相關問題。Also, if you have requested a callback, the remote device disconnects the call, generates this code, and then calls you back.
11 17 User busy
12 18 No user responding
13 19 T.301 expired: – User Alerted, No answer from user
15 21 Call rejected
16 22 Number changed to number in diagnostic field.
17 23 Reverse charging rejected
18 24 Call suspended
19 25 Call resumed
1A 26 Non-selected user clearing
1B 27 Destination out of order
1C 28 Invalid number format or incomplete address
1D 29 EKTS facility rejected by network
1E 30 Response to STATUS INQUIRY
1F 31 Normal, unspecified
2 = resource unavailable
21 33 Circuit out of order
22 34 No circuit/channel available
23 35 Destination unattainable
24 36 Out of order
25 37 Degraded service
26 38 Network out of order
27 39 Transit delay range cannot be achieved
28 40 Throughput range cannot be achieved
29 41 Temporary failure
2A 42 Switching equipment congestion
2B 43 Access information discarded
2C 44 Requested circuit channel not available
2D 45 Preempted
2E 46 Precedence call blocked
2F 47 Resource unavailable, unspecified
3 = service or option not available 31 49 Quality of service unavailable
32 50 Requested facility not subscribed
33 51 Reverse charging not allowed
34 52 Outgoing calls barred
35 53 Outgoing calls barred within CUG
36 54 Incoming calls barred
37 55 Incoming calls barred within CUG
38 56 Call waiting not subscribed
39 57 Bearer capability not authorized
3A 58 Bearer capability not presently available
3F 63 Service or option not available, unspecified
4 = service or option not implemented 41 65 Bearer service not implemented
42 66 Channel type not implemented
43 67 Transit network selection not implemented
44 68 Message not implemented
45 69 Requested facility not implemented
46 70 Only restricted digital information bearer capability is available
4F 79 Service or option not implemented, unspecified
5 = invalid message 51 81 Invalid call reference value
52 82 Identified channel does not exist
53 83 A suspended call exists, but this call identity does not
54 84 Call identity in use
55 85 No call suspended
56 86 Call having the requested call identity has been cleared
57 87 Called user not member of CUG
58 88 Incompatible destination
59 89 Non-existent abbreviated address entry
5A 90 Destination address missing, and direct call not subscribed
5B 91 Invalid transit network selection (national use)
5C 92 Invalid facility parameter 93 Mandatory information element is missing
5D 93 Message type non-existent or not implemented
5F 95 Invalid message, unspecified
6 = protocol error 60 96 Mandatory information element is missing
61 97 Message type non-existent or not implemented
62 98 Message not compatible with call state or message type non-existent or not implemented
63 99 Information element nonexistent or not implemented
64 100 Invalid information element contents
65 101 Message not compatible with call state
66 102 Recovery on timer expiry
67 103 Parameter non-existent or not implemented – passed on
6F 111 Protocol error, unspecified
7 = interworking 7F 127 Internetworking, unspecified
?80
+
128 +Proprietary diagnostic code (not necessarily bad). Typically used to pass proprietary control or maintenance messages between multiplexers.

範例

08 02 81 90:Class 0 Value 0

08 03 81 e2 0f :Class 6 Value 2

參考

  • https://www.viavisolutions.com/en-us/support/files/Q.931-Cause-Codes

SIP header Via

所有 SIP 訊息 都要有 Via,縮寫 v。一開始的 UAC 和後續途經的每個 proxy 都會疊加一個 Via 放傳送的位址,依序作為回應的路徑。 格式 sent-protocol sent-by [ ;branch= branch ][ ; 參數 ...] s...