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
一直增加就可能有洩漏

2020年6月20日 星期六

MySQL 8 更改 remote access

//Create the root user 
1  nano /etc/my.cnf  //centos7

 comment the bind_address  (好像不需要)
 或是 
 bind_address 0.0.0.0


2 create user root
//(yes, a new user because what exists is 'root@localhost'
// which is local access only):

CREATE USER 'root'@'%' IDENTIFIED BY '123';
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%';


3 restart
sudo systemctl restart mysqld

2020年6月10日 星期三

2020年5月18日 星期一

MySQL Variables / Status / 優化

MySQL connections variables

Apple MySQL 必要修正一些預設值

//show
mysql> show variables like '%conn%'l;

//set
mysql> set global max_connections=10240;

//select
mysql> select @@global.sort_buffer_size;

  • max_connections
    最大連線數。
    要比歷史連線數 status 的 Max_used_connections 高。
    預設:100
    建議:看系統

  • max_user_connections
    每個使用者最大連線數

  • max_connection_errors
    最大連線錯誤數,如果連線錯誤超過此數會出錯。
    連線錯誤是指例如帳密錯誤之類。
    這個設定沒有實際作用,
    是讓你看有多少錯誤。
    預設:
    建議:1844674407370954751,越大越好

  • connect_timeout
    連線逾時(秒)
    預設:5
    建議:10-15

golang 的 sql package connections 設定

golang mysql api

Understanding maxLifeTime on MySQL
  • SetMaxIdleConns
    the maximum number of connections in the idle connection pool.
    連接池中的最大閒置連線數。

  • SetMaxOpenConns ¶
    最大同時開啟連線錯數。

  • SetConnMaxLifetime
    連接池中閒置的連線當時間達到設定,就會被關閉。
    使用中的連線不會被關閉,

MySQL connections status

mysql=> show status like 'conn%';
  • Connection_errors_accept
  • Connection_errors_internal
  • Connections_errors_max_connections
  • Connections
    The number of connection attempts (successful or not) to the MySQL server.
    有史以來總連接數

  • Max_used_connections
    最大同時連接數

  • Max_used_connections_time
    最大連線使用時間

  • Thread_connected
    The number of currently open connections.
    當前連接數

  • Locked_connects

顯示當前連線及執行緒狀況

mysqladmin -u -p extended-status | grep -wi 'threads_connected\|threads_running' 
| awk '{ print $2,$4}'

//output
Threads_connected 12

Threads_running 1  

Threads_connected: Number of connections

Threads_running: connections currently running some sql


官方文件

MySQL 8 Variables 官方文件

MySQL 5.7 Variables 官方文件

網上說明

MySQL 8.0 預設值

優化

my.cnf優化

Fix poor mySQL default variables

改 variable 及 my.conf

open_files_limit  default 10000, optimize 10000
這是read only variable
需要編輯 /usr/lib/systemd/system/mysqld.service
LimitNOFILE=infinity
LimitMEMLOCK=infinity
Then run systemctl daemon-reload and systemctl restart mysql.service.


innodb_open_files default 4000,  
指定MySQL可同时打开.ibd文件的最大个数,独立于open_files_limit

max_connections  default 151, optimize  5000

max_user_connections default 0,   optimize 0  //沒限制

max_connect_errors  default 100, optimize  1844674407370954751 //越大越好,這個值沒用

connect_timeout default 10, optimize 15   //加5秒

table_open_cache default 4000, optimize 4000
所有线程打开表的数目。它的作用就是缓存表文件描述符,降低打开关闭表的频率,

table_open_cache_instances default 16, optimize 16
表缓存实例数,为通过减小会话间争用提高扩展性,表缓存会分区为table_open_cache/table_open_cache_instances大小的较小的缓存实例。DML语句会话只需要锁定所在缓存实例,这样多个会话访问表缓存时就可提升性能(DDL语句仍会锁定整个缓存)。默认该值为1,当16核以上可设置为8或16