About 张 子萌

个人喜欢读书、上网、聊天。希望朋友之间多交流。
12月 16

[转载]golang pprof

https://blog.wolfogre.com/posts/go-ppof-practice/

<code>_ "net/http/pprof" // 引入 pprof 模块
//  主函数内存分析
go func() {
	http.ListenAndServe("0.0.0.0:8090", nil)
}()</code>
<code>//命令行,收集cpu 如果生成svg 和图片安装 graphviz 
// top list 定位问题
go tool pprof http://localhost:8090/debug/pprof/profile
// 收集内存
go tool pprof http://localhost:8090/debug/pprof/heap
// 内存回收
go tool pprof http://localhost:8090/debug/pprof/allocs</code>
12月 02

esp32 +oled显示英文、中文

from machine import Pin, I2C
from oled import SSD1306_I2C
# 定义管脚
scl = Pin(22)
sda = Pin(21)
# 初始化
i2c = I2C(scl=scl, sda=sda)
# 像素 128*64
oled = SSD1306_I2C(128, 64, i2c)
# 清屏
oled.fill(0)
oled.show()

# 显示英文,从0,0像素开始
oled.text('hello', 0, 0)
oled.show()

# 以下为显示中文,重新显示需要清屏。fonts字体为自定义取模。
# 字索引为utf-8 16进制
# https://www.23bei.com/tool-217.html 在线取模
fonts= {
    # 你
    0xe4bda0:
[0x11,0x11,0x11,0x23,0x22,0x64,0xA8,0x20,0x21,0x21,0x22,0x24,0x20,0x20,0x21,0x20,
0x00,0x00,0x00,0xFC,0x04,0x08,0x40,0x40,0x50,0x48,0x4C,0x44,0x40,0x40,0x40,0x80],
    # 好
    0xe5a5bd:
[0x10,0x11,0x10,0x10,0xFC,0x24,0x24,0x27,0x24,0x44,0x28,0x10,0x28,0x44,0x84,0x00,
0x00,0xFC,0x04,0x08,0x10,0x20,0x24,0xFE,0x20,0x20,0x20,0x20,0x20,0x20,0xA0,0x40]
    }

def chinese(ch_str, x_axis, y_axis): 
   offset_ = 0 
   y_axis = y_axis*8  # 中文高  
   x_axis = (x_axis*16)  # 中文宽
   for k in ch_str:
       code = 0x00  # 中文转成16进制编码 
       data_code = k.encode("utf-8")
       code |= data_code[0] << 16 
       code |= data_code[1] << 8
       code |= data_code[2]
       byte_data = fonts[code]
       for y in range(0, 16):
           a_ = bin(byte_data[y]).replace('0b', '')
           while len(a_) < 8:
               a_ = '0'+a_
           b_ = bin(byte_data[y+16]).replace('0b', '')
           while len(b_) < 8:
               b_ = '0'+b_
           for x in range(0, 8):
               oled.pixel(x_axis+offset_+x, y+y_axis, int(a_[x]))   
               oled.pixel(x_axis+offset_+x+8, y+y_axis, int(b_[x]))   
       offset_ += 16
       
chinese('你好',0,0)
oled.show()
8月 31

rust学习初步

算法还是使用http://www.simonzhang.net/?p=2789。

study.rs

fn check(n:u64) -> bool{
    let ll = n.to_string().len();
    let cc = String::from(n.to_string());
    let mut nn1 = 0;
    let mut nn2 = ll-1;
    while nn1 < ll{
        if cc.chars().nth(nn1) != cc.chars().nth(nn2){
            return false;
        }
        if nn2 == 0{
            break;
        }
        nn1 += 1;
        nn2 -= 1;
    }
    return true;
}

fn main() {
    let x:u64 = 10;
    let y = x.pow(7);
    let mut i = 0;
    while i <= y {
        if check(i){
            if check(i*i){
                println!("{} {}", i, i*i)
            }
        }
        i+=1;
    }
}

cargo.toml

[profile.release]
lto = true
codegen-units = 1

使用UPX -9 压缩后400k,执行1秒以内。

6月 29

python3 多CPU加速

#!env python3
# -*- coding:utf-8 -*-
# Author:      simonzhang.net
import time
from multiprocessing import Pool

def toPrint(n):
    print(n, int(time.time()))
    time.slee(0.5)

if __name__=='__main__':
    # 系统有20个CPU
    p = Pool(20)
    # 加载数据
    for i in range(60):
        p.apply_async(toPrint, args=(i,))
    # 加载完成开始执行
    p.close()
    p.join() 

5月 08

Did not receive identification string from 127.0.0.1 port 问题处理

安装frp服务ssh穿透内网,系统日志中报大量 Did not receive identification string from 127.0.0.1 port xxxx 。由于都是内网地址,所以不是攻击或扫描。此问题是由于服务自动检查穿透端口服务器是否健康引起。

代码位置
frp/client/proxy/proxy_wrapper.go 89 行

2个简单解决方案,只是缓解。

1.延长检查服务时间 frp客户端配置 ,进行延长,单位为妙
health_check_interval_s = 10

2.降低ssh日志记录级别