Showing posts with label Unix. Show all posts
Showing posts with label Unix. Show all posts

Unix, Linux: vmstat command

vmstat เป็นคำสั่งที่ไว้แสดงรายงาน CPU activity, memory, paging รวมๆ ก็คือไว้สำหรับดู performance ของระบบนะเอง

parameter โดยทั่้วไปของ vmstat ก็ีมี
interval หมายถึงระยะเวลาห่างแต่ละที่จะให้แสดงผล
count หมายถึงให้แสดงทั้งหมดกี่ครั้ง

ถ้าเราใช้คำสั่ง vmstat เฉยๆ โดยไม่มี parameter ต่อท้ายเลย จะหมายถึงแสดงค่าเฉลี่ยของทั้งหมดตั้งแต่เครื่องทำงานมา

$ vmstat

procs -----------memory---------- ---swap-- -----io---- -system-- ----cpu----
r b swpd free buff cache si so bi bo in cs us sy id wa
2 0 0 79744 255584 861836 0 0 0 2 1 1 1 1 98 0

ถ้าเราใส่ parameter ตัวเดียวหมายถึง จะหมายถึง interval คือ vmstat จะแสดงผลไปเรื่อยๆ (สามารถ ctrl+c ออกมา) โดยบรรทัดแรกยังแสดงค่าเฉลี่ยของระบบ ส่วนข้อมูลบรรทัดถัดไปจะเป็นข้อมูลของระบบ ณ ขณะนั้น ซึ่งจะห่างกันเท่ากับ interval เช่นตัวอย่างด้านล่าง แ่ต่ละครั้งจะแสดงห่างกัน 2 วินาที

$ vmstat 2

procs -----------memory---------- ---swap-- -----io---- -system-- ----cpu----
r b swpd free buff cache si so bi bo in cs us sy id wa
1 0 0 78876 255584 861844 0 0 0 2 1 1 1 1 98 0
0 0 0 79068 255584 861848 0 0 0 0 63 297 0 0 100 0
0 0 0 79116 255584 861848 0 0 0 0 27 308 0 0 100 0
0 0 0 79116 255584 861848 0 0 0 18 15 301 0 0 100 0
...

ถ้าเราใส่ parameter 2 ตัวก็หมายถึงให้แสดงผลกี่ครั้งเช่น ตัวอย่างด้านล่างให้แสดงผลทุกๆ 2 วินาที และแสดงผลแค่ 5 ครั้ง (รวมที่แสดงค่าเฉลี่ยของระบบด้วย)

$ vmstat 2 5

procs -----------memory---------- ---swap-- -----io---- -system-- ----cpu----
r b swpd free buff cache si so bi bo in cs us sy id wa
1 0 0 79412 255584 861852 0 0 0 2 1 1 1 1 98 0
0 0 0 79480 255584 861852 0 0 0 10 6 275 0 0 100 0
0 0 0 79480 255584 861852 0 0 0 0 3 272 0 0 100 0
0 0 0 79480 255584 861852 0 0 0 0 3 276 0 0 100 0
0 0 0 79480 255584 861856 0 0 0 0 143 389 3 1 96 0

การอ่านผล

kthr - kernel thread
r - runnable thread คือจำนวนเฉลี่ยของ thread ที่สามารถทำงานได้ ถ้ามีมากกว่าจำนวน CPU ก็แปลว่า thread ที่เหลืออยู่ในสถานะ waiting กำลังรอ CPU ว่าง ซึ่งถ้าจำนวน thread ที่ waiting ว่างๆ มีจำนวนมาก ก็แสดงว่า performance ต่ำ (ก็แปลว่า user อาจจะกำลังรอผลการทำงานของ CPU อยู่ก็ได้)
b - blocked thread คือ thread ที่กำลังรออะไรสักอย่าง อาจจะเป็น filesystem, I/O, memory load ถ้าเยอะๆ ก็แสดงว่า performance ม่ะดี
p - thread ที่รอ I/O แต่ไม่รวมถึง filesystem เช่นเดียวกัน ยิ่งมากยิ่งไม่ดี

cpu - CPU time

us - user mode เปอร์เซนเวลาที่ CPU ใช้ในการทำงานใน user mode ซึ่งเป็นเวลาที่ CPU ทำงานกะ application ที่ไม่ต้องการ kernel resource ในการคำนวณ จัดการ memory หรือการเก็บค่าตัวแปร
sy - system (kernel) mode เปอร์เซนเวลาที่ CPU ใช้ในการทำงานกับ kernel resource เช่นการเรียก system ให้ทำงานอ่านหรือเขียนไฟล์
id - idle คือเปอร์เซนเวลาที่ CPU ไม่มีอะไร และไม่มี pending local disk I/O
wa - คล้ายกับ id แต่ว่า รวมที่มี pending local disk I/O ด้วย


Reference:
IBM System Information Center - vmstat command

Unix, Linux: Bourne shell, C shell, Bourne shell and Bash Shell

Bourne shell เป็น default shell ของ Unix version 7 ขึ้นไป พัฒนาขึ้นในปี 1977 (ก่อนหน้านั้นเป็น Thompson shell ซึ่งเป็น shell ตัวแรก) ชื่อ Bourne ได้ชื่อตามชื่อคนสร้าง Stephen Bourne โดย AT&T Bell Lab

ส่วน C shell (csh) พัฒนามาจาก Thompson shell ที่ได้จาก Unix version 6 ก่อนที่จะเริ่มใช้ Bourne shell ซึ่ง syntax จะคล้ายกับ C แต่เดี๋ยวนี้เค้าไม่ค่อยนิยมใช้กันแล้ว เนื่องจากมีการพัฒนาเป็น Tenex C shell (tcsh) และ Korn shell ก็ได้มีการเลียนแบบ function ต่างๆ ของ C shell ไปพัฒนา

Korn shell (ksh) พัฒนาขึ้นในปี 1980 โดย David Korn จาก AT&T Bell Lab เช่นเดียวกับ Bourne จึงไม่น่าแปลกใจที่ Korn shell จะเป็น backward compatible กับ Borne shell และได้นำ function ต่างๆ ที่มีอยู่ใน C shell ใส่เข้าไปใน Korn shell ตอนแรกตัว Korn shell ยังเป็น software ที่มี license เหมือนกับ software ทั่วไปอยู่ จนกระทั่ง release 93q (ksh93) จึงได้กลายเป็น open source software เต็มตัว และตอนหลังก็ได้ มีการนำมาไว้ใน Linux ด้วย (ลองไปดูกันได้ที่ /bin/ksh) และต่อมาก็ยังมีลูกหลายอีกหลายตัวด้วยกัน เช่น pdksh, mksh, dtksh, tksh ซึ่งสองตัวหลังมีการพัฒนาเป็น GUI ด้วย

Bash shell ย่อมาจาก Bourne-Again Shell เป็น free software shell ซึ่งเกิดมาจาก GNU project สร้างขึ้นเพื่อเลียนแบบ Bourne shell บน Unix ในปี 1987 ดังนั้น Bash shell จึงเป็น default shell ของ OS ทุกตัวที่มาจาก GNU OS เช่น Linux, Mac OS X รวมถึงที่เราใช้ POSIX บน Cygwin ด้วย ข้อสังเกตุนะค่ะ คือ Bash shell เค้าไม่ได้พัฒนามาให้ใช้บน Unix ซึ่งบน Unix ตัว Bourne shell ได้พัฒนาไปเป็น Korn shell ส่วนตัว Bash shell จะใช้แค่บน Linux เป็นหลักเท่านั้น

โดยทุกคำสั่งใน Bourne สามารถใช้ได้บน Bash แต่ว่าไม่สามารถทำในทางกลับกันได้ (คือเสมือนว่า Bash พัฒนา่ต่อจาก Bourne อีกที ดังนั้นก็จะมี function เพิ่มขึ้นมาจากของเดิม) ดังนั้น shell script ของ Bourne จะสามารถใช้ได้บน Bash shell (ยกเว้นว่าใน script มีการใช้ตัวแปรพิเศษที่มีใน Bourne shell เท่านั้น) สำหรับ bash shell script จะขึ้นต้นด้วย # !/bin/sh และยังมีการนำแนวความคิดบางอย่างของ Korn shell และ C shell มาใช้ด้วย เช่น command line editing, command history (เช่นสามารถใช้คำสั่ง history เพื่อดูคำสั่งย้อนหลัง หรือใช้คำสั่ง !! เพื่อ run คำสั่งล่าสุดได้) นอกจากนั้นยังมีความสามารถในการ automatic command line completion (คือการใช้คีย์ tab เพื่อให้ shell เติมชื่อไฟล์ให้ครบถ้วน) และอื่นๆ อีก เช่นการคำนวณ integer ซึ่งเค้่าว่าใน bash shell จะไม่ต้องแตก process เพื่อใช้คำนวณ

Default prompt ของ C shell จะเป็น % ในขณะที่ Bourne shell, Korn shell, Bash shell เป็น $ (ก็ไม่น่าแปลกใจนะค่ะ เพราะว่า ตัว Korn shell กับ Bash shell ได้พัฒนาขึ้นมาจาก Bourne อีกที)

และสำหรับการ redirect standard output และ standard error ก็ต่างกันดังนี้
คำสั่งC shellBourne shell
Redirect standard output>>
Redirect standard errorไม่มีคำสั่งนี้2>
Redirect standard output and standard error>&ไม่มีในคำสั่งเดียว แต่สามารถใช้แยกทีละอันมาต่อกันได้ เช่น ls >test 2>test จะ redirect ไปที่ไฟล์ test เหมือนกัน
Redirect standard error to standard outputไม่มี แต่สามารถใช้ >& แทน เพื่อ redirect ไปที่เดียวกันได้2>&1 (ตอนใช้ต้องใช้ติดกันแบบนี้เลยค่ะ ม่ะงั้นมันจะเพี้ยน)
Redirect standard input<<
Redirect standard output; overwrite file if it exists>! หรือ > ให้ผลเหมือนกัน>
Redirect standard output and standard error; overwrite file if it exists>&! หรือ >& ให้ผลเหมือนกันเช่นเดียวกับข้างต้นไม่มีในคำสั่งเดียว แต่สามารถใช้แยกทีละอันมาต่อกันได้
Redirect standard output to another command (pipe)||
Append standard output>>>>
Append standard output and standard error>>&เช่นเดียวกับข้างต้นไม่มีในคำสั่งเดียว แต่สามารถใช้แยกทีละอันมาต่อกันได้ เช่น ls >>test 2>>test
Pipe standard output and standard error to another commandไม่มี2>&1|


สำหรับ Korn shell และ Bash shell น่าจะเหมือนกับ Bourne shell นะค่ะ ยังไม่มีโอกาสได้ลอง ไว้ถ้าได้ลองแล้วจะมาใส่ไว้ให้ค่ะ

Reference:
Wikipedia - Bourne shell
Wikipedia - C shell
Wikipedia - Korn shell
Unix Sys Admin
Standard Input and Output Redirection

Unix, Linux: Check CPU and Memory information

สำหรับ CPU ให้คำสั่งนี้

$ cat /proc/cpuinfo

สำหรับ memory

$ cat /proc/meminfo

ผลลัพธ์ก็ดูกันเอาเองละกันค่ะ บางอันก็ไม่ค่อยรู้ความหมายเท่าไร