2020年7月23日 星期四

Golang godoc

godoc with gopath

如果專案不用 go module,或是在專案外執行 godoc

那就是 gopath mode

godoc server 會從 gopath 去找src

godoc with go module

如果用go module,要確定module 模式是否正確
1. 專案目錄如果有 vendor 要刪掉
2.  在專案目錄 go env GOMOD
   看是否有顯示 go.mod 的絕對路徑
3. 如果有,那就正確
4. 啟動 godoc -http=:6060
5. 瀏覽器 localhost:6060, 專案說明會在 pkg 下

Using wget to get godoc


1. Edit robots.txt in the go root dir,  and remove "Disallow:/"
2. godoc -http=:6060
3. wget -r -np -E -p -k http://localhost:6060/pkg/pathToPackage
4. wget -m -k -q -erobots=off --no-host-directories --no-use-server-timestamps http://localhost:6060
-r  : download recursive
-np : don't ascend to the parent directory
-N  : don't retrieve files unless newer than local
-E  : add extension .html to html files (if they don't have)
-p  : download all necessary files for each page (css, js, images)
-k  : convert links to relative

注釋說明

詳細參考

http://c.biancheng.net/view/4035.html


package、const、variable、func 都支援註釋

只支援public (大寫開頭)的member 註釋輸出



註釋格式就是直接在member 上加上說明,

但是 func 的註釋要以 func名稱開頭

//PublicFunc

func PublicFunc(){

}


const(

   //ConExample 是註釋範例

   ConExample =10

)


var (

    //VarExample是註釋範例

    VarExample =10

)




const 內的 會顯示在 Constants 區段

var 內的 會顯示在  Variables 區段





建立範例 example

example_packageName_test.go

func exampleFuncName

func ExampleFuncName() {

logger.Info("hello, world.")

}

Bug 標示

這樣會顯示bug 標示


//BUG(rsc): 

func BadFunc(){

}



2020年7月14日 星期二

Linux 使用資源限制

資源的限制

Two types of ulimits

Hard limit is the maximum value that is allowed for the soft limit. Any changes to the hard limit require root access.

Soft limit is the value that Linux uses to limit the system resources for running processes. The soft limit cannot be greater than the hard limit.

Hard 設定給 soft 的最大值。任何 hard 改變都需要 root 權限。
Soft 是 linux 限制給每個 process 的上限。Soft 不能超過 hard 上限。

ulimit -aH 顯示 hard 全部設定
ulimit -aS 顯示 soft 全部設定

Soft Hard 區別

ulimit 參數詳解


依據設定的層級分系統級、user級

分系統級、User級。
user級不能超越系統級。


  • 可針對 user 做個別設定,在 /etc/security/limits.conf。其中 * 代表所有 user。
  • 限制設定落實到 user 啟動的 process。
  • 限制設定是繼承性的,子 process 繼承父 process 的設定值。

範例:
User 起動 shell, shell 的設定來自系統預設及 /etc/security/limits.conf。
User 在 shell 中啟動 process,此時 process 繼承 shell 的設定。

臨時設定
shell 中使用 ulimit 命令設定,但結束 session就沒有了。
ulimit -s unlimited //設定 stack unlimited
ulimit -n 2048. //open file 數 2048

永久設定 
更改 /etc/security/limits.conf
更改後重新 login 就可套用新設定


查詢當前user / process 設定

ulimit -aH //顯示所有設定 hard
ulimit -aS //顯示所有設定 soft
core file size          (blocks, -c) 0
data seg size           (kbytes, -d) unlimited
scheduling priority             (-e) 0
file size               (blocks, -f) unlimited
pending signals                 (-i) 31204
max locked memory       (kbytes, -l) 64
max memory size         (kbytes, -m) unlimited
open files                      (-n) 1024         (file descriptor)
pipe size            (512 bytes, -p) 8
POSIX message queues     (bytes, -q) 819200
real-time priority              (-r) 0
stack size              (kbytes, -s) 8192
cpu time               (seconds, -t) unlimited
max user processes              (-u) 31204
virtual memory          (kbytes, -v) unlimited
file locks                      (-x) unlimited

查詢 pocess 設定

cat /proc/{pid}/limits

//example cat /proc/1888/limits

Limit                     Soft Limit           Hard Limit           Units     
Max cpu time              unlimited            unlimited            seconds   
Max file size             unlimited            unlimited            bytes     
Max data size             unlimited            unlimited            bytes     
Max stack size            8388608              unlimited            bytes     
Max core file size        0                    unlimited            bytes     
Max resident set          unlimited            unlimited            bytes     
Max processes             31204                31204                processes 
Max open files            1024                 4096                 files     
Max locked memory         65536                65536                bytes     
Max address space         unlimited            unlimited            bytes     
Max file locks            unlimited            unlimited            locks     
Max pending signals       31204                31204                signals   
Max msgqueue size         819200               819200               bytes     
Max nice priority         0                    0                    
Max realtime priority     0                    0                    
Max realtime timeout      unlimited            unlimited            us  

//Max processes 是可開啟執行緒上限持

查詢 pocess 資源使用量

cat /proc/xxxxx/stat
cat /proc/xxxxx/status

//顯示某 process file descriptor數量

lsof -p 28290
lsof -a -p 28290

or 
//列出
ls -l /proc/28290/fd | less

//計算總數
ls -l /proc/28290/fd | wc -l

系統總上限

sysctl fs.file-nr

fs.file-nr    //顯示已經分配的 fd 數量、可分配的 fd 數量、系統總 fd 數量

fs.file-max  //系統所有 process 可使用的總 fd 數量


查看系統設定

sysctl fs.file-nr //輸出 fs.file-nr = 1020 0 70000 1. 1020 The number of allocated file handles. 2. 0 The number of unused-but-allocated file handles. 3. 70000 The system-wide maximum number of file handles. cat /proc/sys/fs/file-max 或是看上限設定 sysctl fs.file-max 臨時性修改 sysctl -w fs.file-max=102400 永久修改 sudo vim /etc/sysctl.conf fs.file-max = 70000 //這個數值對應於hard limit sysctl -p 使修改生效

系統設定的user上限

fs.nr_open

系統級user的單一process開檔數限制

查看
cat /proc/sys/fs/nr_open

或是
sysctl fs.nr_open

修改
sudo /etc/sysctl.conf
nr_open=1000000

實際真正個人上限

vi /etc/security/limits.conf
* hard nofile 102400  //針對所有user,不包括root
* soft nofile 102400  //針對所有user,不包括root

httpd hard nofile 102400  //針對httpd這個user
httpd soft nofile 102400  //針對httpd這個user

root hard nofile 102400  //針對root
root soft nofile 102400  //針對root

結論

user的每個process開檔上限這些限制
1 fs.file-max:系統總上限
2 fs.nr_open:系統設定的個人上限,限制 limits.conf設定的上限
3 /etc/security/limits.conf中nofile設定:實際真正能使用的數量
# /etc/security/limits.conf

//設定值
//
core - 限制内核文件的大小
date - 最大数据大小
fsize - 最大文件大小
memlock - 最大锁定内存地址空间
nofile - 打开文件的最大数目
rss - 最大持久设置大小
stack - 最大栈大小
cpu - 以分钟为单位的最多CPU时间
noproc - 进程的最大数目
as - 地址空间限制
maxlogins - 此用户允许登录的最大数目

# * 代表针对所有用户
* soft nofile 102400  
* hard nofile 102400
* soft stack unlimited  //開啟stack 無限制
# /etc/security/limits.conf
* soft nproc 100  //不能太大
* hard nproc 100 //不能太大

# /etc/security/limits.conf
* - nproc 100
* - nofile 102400

2020年7月13日 星期一

Linux Command free

free

顯示系統記憶體使用狀況
free是從 /proc/meminfo讀取資料

free
free -b  //單位Bytes
free -m  //單位MB
free -g  //單位GB
free -t  //實體記憶體加上swap 

total
used
fee
shared
buffers:(buffer cache)relatively temporary storage for raw disk blocks.
cached:(page cache)in memory cache for files read from disk, doesn't include swapcache 

Linux 查找記憶體洩漏

top

顯示process的記憶體使用
top -p pid
top 列出所有process, 再按 p, m等指令去切換

VIRT一直增加,可能是有洩漏

VIRT virtual memory usage 虛擬記憶體
        包括lib, code, data等
         process申請的虛擬記憶體大小,如果申請了100m, 但是只用20m,那還是100m
  
RES resident memory usage 常駐記憶體
       當前使用的記憶體大小,不包括swap out
       包括其他process共享記憶體
       如果申請100m,只用10m,那就是10m

SHR shared memory 共享記憶體
      process本身使用的共享記憶體
      包括整個共享lib 的大小,就算只用了其中一個lib
      計算process使用的物理記憶體使用量 RES-SHR
      swap out之後會降下來

DATA 資料佔用的記憶體
      top 沒顯示,按 f 鍵可顯示
      真正的process 要求的資料空間

free

顯示系統記憶體使用狀況
free -m

pmap

顯示process 記憶體使用映射

sudo pmap pid
sudo pmap -X pid
sudo pmap -XX pid

writeable/private 是實際使用記憶體,不含share lib
一直增加就可能有洩漏