透過ps與top的來找出秏費系統資源的程式

pstop都是linux 開機即有的基礎指令,最近才發現它們也可以幫我們依照消秏資源的程度來呈現目前使用率最大的程式。

ps 指令

找出前10個較秏cpu 資源的程式

-eo : 是用來指定輸出目前process 的特定欄位,並且在透過 --sort (linux)或 -r/-m (darwin)來進行排序。

linux:

ps -eo pid,ppid,cmd,%mem,%cpu --sort=-%cpu

darwin:

ps -eo pid,ppid,%mem,%cpu,comm -r | head

找出前10個較秏memory 資源的程式

linux:

ps -eo pid,ppid,cmd,%mem,%cpu --sort=-%mem

darwin:

ps -eo pid,ppid,%mem,%cpu,comm -m | head

top 指令

top 主要是透過互動式的terminal 介面來呈現目前系統的狀態,但其它也可以透過輸出模式的變更來做到與ps 指定相似的結果。
如果需要更fancy的系統狀態儀表板的話,其實可以另外安裝htop試試…

linux :

-b 代表不使用互動界面的方式出輸結果。
-o 代表用特定欄位排序

top -b -o +%MEM | head -n 10

darwin:

-stats 用來顯示輸出的欄位
-o 代表用來排序的欄位
-l 用來控制要採樣的次數,1 代表只輸出一次結果。
-n 用來控制呈現的前n

top -stats pid,cpu,mem,th,pstate,time,command -o MEM -n 10 -l 1

reference:

https://shutdown2110.blogspot.com/2018/07/linux-ps-top-cpu.html

https://www.freebsd.org/cgi/man.cgi?ps(1)

https://unix.stackexchange.com/questions/88613/how-is-it-possible-to-sort-ps-commands-cpu-field

https://superuser.com/questions/538957/print-top-output-only-once-on-mac-os-x

dmesg的使用 (linux 開機資訊)

不知道有沒有人跟我一樣,常常有些linux的指令都是用過就忘了,每次有需要debug時,又需要再查一次之前linux是怎麼樣使用 0rz

幾次下來以後,還是乖乖把一些常用的debug指令大概列一下如何使用,這樣之後查會快一些吧XDDDD

dmesg 這個指令主要是用來顯示一些開機後的一些訊息,例如像是一些RAM的資訊、網路資訊、硬體資訊、partition 等等…

NOTE:
這個指令會需要用到 sudo的權限…

顯示最近一次的開機資訊

sudo dmesg

顯示最近一次開機時的記憶體資訊

sudo dmesg | grep -i Memory

輸出的範例就會像是:

[    0.208540] kernel: Memory: 61491092K/62810216K available (14339K kernel code, 2536K rwdata, 8848K rodata, 2648K init, 4908K bss, 1319124K reserved, 0K cma-reserved)

清除dmesg的內容

sudo dmesg -c

完整的dmesg log

有時候dmesg會只顯示部分的內容,所以我們想要找的資訊可能會在開機很久後而不會顯示在dmesg 的輸出結果;所以如果要看完整的dmesg內容的話,也可以直接到下面兩個log檔去找:

  • /var/log/kern.log
  • /var/log/dmesg 

Git log 一些呈現方式

原本只是在找要怎麼把目前的hash 用縮寫的方式輸出在terminal上,結果意外的發現更多git log好用的方式:

輸出目前HEAD 的hash 縮寫

git rev-parse --short HEAD
# or
git describe --always 

在git log view 中,輸出第一個git log的hash縮寫

git log -1 --pretty=format:%h

在git log view中,輸出第一個git log的資訊,其hash的部分會是縮寫

git log -1 --abbrev-commit

在git log view 中,輸出第一個git log的hash縮寫與commit message

git log --oneline
# or 
git log --pretty=oneline --abbrev-commit

Git log with tree graph

下面的指令會在git log view中,會在每個commit的開頭,用tree 的關聯圖呈現branch的分支關聯。

git log --graph

下面的指令會在git log view中,除了呈現關聯圖外,還會呈現出一些曾經做過rebase的相關圖示。

git log --graph --all

加了--oneline以後,可以讓上面的的view 用更簡潔的方式呈現。

git log --graph --oneline --all

References:

Git submodule 使用筆記

之前大概知道git 很早以前就有這個功能了,但直到最近在公司上才開始比較有在使用它,也趁這個機會好好的做個筆記,這樣之後就可以直接參考這個筆記就好了。

簡介

Submodule 主要的用途是用來把專案中,會需要共用的程式碼獨立出去成單獨的一個git repo,然後,目前的repo就只是使用其submodule的某一個版本(commit) 而已。

目前專案上碰到的一個需求,就是有多個服務可能會同時需要連接同一個資料庫(SOA 架構);在這樣的情況下,如果我們需要修改DB Migration Schema時,就很有可能需要在每個repo中增加對應的SQL檔案了,這樣其實蠻容易出錯的,也違反了DRY原則。

Git submodule 就很適合用來解決這個問題,我們可以直接把這些DB Migration SQL獨立出來成一個git repo,而任何有需要用到這個資料庫的服務,都可以把這個DB Migration SQL Repo 新增至目前服務下的submodule,這樣,我們只需要讓每個服務Migration SQL Repo 都一直在最新的版號(commit)就好了。

常用的Git submodule 指令

在這邊順便記錄一下,目前常常會用到的一些submodule 指令。

新增submodule 至目前的repo

下面這個指令,可以把github上的repo git@github.com:github-account/github-project.git新增到目前git repo下成為一個submodule。

git submodule add git@github.com:github-account/github-project.git the-submodule-name-in-the-repo

在用上面的指令新增完submodule以後,我們目前的git repo 下就會多了2個unstashed的檔案: .gitmodulesthe-submodule-name-in-the-repo,為了完成新增這個submodule,我們還必須git addgit commit 這2個檔案。

Clone 一個有submodule的git repo

當如果要clone 一個有submodule的repo時,會需要在clone時帶--recurse-submodules 的flag,不然那些submodule 就不會順便被clone 下來,而會呈現是空資料夾。

git clone git@github.com:github-account/git-parent-repo.git --recurse-submodules

Clone 目前repo裡面的submodule

假設我們在最初clone repo時,就沒有帶 --recurse-submodule時,這樣我們如果要在另外clone 那些submodule時,就會需要用下面的指令去做

git submodule update --init

更新目前repo下所有submodule 的版本

當目前repo下的submodule被更新時,我們會需要透過下面的指令去把所有submodules 都更新,在更新完了以後,我們還會需要在做git addgit commit來綁定目前repo所對應的submodule 版本。

git submodule update --recursive --remote

更新特定submodule的版本

若只是要更新某一個submodule的版本的話,我們可以透過下面的指令來更新

cd submodule-folder/
git checkout branch-name
git pull

移除特定的submodule

移除submodule 比較麻煩點,需要依序執行下面的幾個步驟:

從目前的git version,新增一個移除submodule 的變動

git rm --cached /submodule_folder
rm -rf /submodule_folder

從.gitmodules中移除剛剛已移除的submodule

vim .gitmodules

# then remove the related contents

最後要再移除.git/config中的那個submodule的相關資訊

vim .git/config

# then remove the related contents

Reference