Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: temporarily cache geo data #3294

Closed
wants to merge 2 commits into from
Closed

Conversation

yin1999
Copy link
Contributor

@yin1999 yin1999 commented Apr 21, 2024

Description

Thie PR created some wrappers to temporarily cache the geo data which can reduce the CPU usage.

Tesing

Environment

CPU: 13th Gen Intel(R) Core(TM) i5-13600K
Memory: DDR5 32 GB@5200 MT/s
Disk: 1 TB (fs: Btrfs)

Stat script

if [ -z "$1" ]; then
	echo "Usage: $0 [before|after]"
	exit 1
fi

name=$1

rm -f "${name}.sqlite"

pid=$(pgrep xray | tail -n 1)

echo "pid: $pid"

# Make 5000 records per second
procpath record --stop-without-result -i 1 -r 5000 -d "${name}.sqlite" "$..children[?(@.stat.pid == ${pid})]"

# generate the stat svg
procpath plot -d "${name}.sqlite" -q rss -q cpu -p ${pid} -f "${name}.svg"

echo "======== ${name} ========"

# io stat
echo "io stat"
echo "read_char write_char read_sys write_sys"
cat /proc/$pid/io | xargs | awk '{print $2"\t"$4"\t"$6"\t"$8}'

# cpu stat
echo
echo "cpu usage"
echo -e "user\tsystem\tidle\tio wait"
cat /proc/$pid/stat | awk '{print $14"ms\t"$15"ms\t"$16"ms\t"$42"ms"}'

echo

Preparation

  • With Procpath installed to record the memory usage.

  • Add time.Sleep() in main function for delay the configuration parsing.

  • With the latest geo data downloaded.

  • Setup the xray config:

    log:
      loglevel: warning
    
    inbounds:
      - tag: transparent
        port: 10893
        protocol: dokodemo-door
        settings:
          followRedirect: true
          network: tcp,udp
        sniffing:
          destOverride:
            - http
            - tls
            - quic
          enabled: true
        streamSettings:
          sockopt:
            mark: 255
            tproxy: tproxy
    
    outbounds:
      - tag: direct
        protocol: freedom
        streamSettings:
          sockopt:
            mark: 255
    
      - tag: block
        protocol: blackhole
    
    routing:
      domainStrategy: IPIfNonMatch
      rules:
        - type: field
          outboundTag: direct
          domain:
            - acm.org
            - adobe.com
            - aspnetcdn.com
            - azureedge.net
            - beego.me
            - bing.net
            - caomeikeyan.com
            - cn.download.nvidia.com
            - debian.org
            - elsevier.com
            - epicgames.com
            - exp-tas.com
            - gfx.ms
            - gvt1.com
            - hcaptcha.com
            - ieee.org
            - ipify.org
            - jsdelivr.net
            - k8s.io
            - microsoftonline.com
            - msauth.net
            - msecnd.net
            - msn.com
            - npmmirror.com
            - nr-data.net
            - office.com
            - office.net
            - onenote.com
            - onenote.net
            - ooklaserver.net
            - oracle.com
            - spotify.com
            - swsindex.com
            - swsresearch.com
            - test-ipv6.com
            - time.is
            - trafficmanager.net
            - ubi.com
            - ubisoft.com
            - visualstudio.com
            - vsassets.io
            - windows.com
            - windows.net
            - windowsupdate.com
            - www.speedtest.net
    
        - type: field
          outboundTag: direct
          inboundTag:
            - transparent
          network: udp
          port: 123
    
        - type: field
          outboundTag: direct
          protocol:
            - bittorrent
    
        - type: field
          outboundTag: proxy
          domain:
            - gstatic.com
    
        - type: field
          outboundTag: direct
          ip:
            - geoip:private
            - geoip:cn
    
        - type: field
          outboundTag: direct
          domain:
            - geosite:cn
            - geosite:private
            - geosite:category-games@cn
            - geosite:mdn
    
        - type: field
          outboundTag: blackhole
          port: 0-65535

Run tests

  1. Run xray: ./xray run -c config.yaml
  2. Run the stat script: bash ./stat.sh [before|after] (using "before", "after" to identify the build before or after the changes).
  3. After the xray loaded the ruleset, hit Ctrl + C to stop recording and generate the usage image.

Result

Records (please download the following images, as these images may not be viewable online):

Before After
before after

Proc stat:

Before:

======== before ========
io stat
read_char write_char read_sys write_sys
45696753        319     5619    9

cpu usage
user    system  idle    io wait
28ms    9ms     0ms     0ms

After:

======== after ========
io stat
read_char write_char read_sys write_sys
16788810        318     2082    8

cpu usage
user    system  idle    io wait
13ms    5ms     0ms     0ms

After testing, the changes here can significantly reduce IO reads and reduce the CPU usage duration the configuration loading of this program. There is no very obvious increase in memory usage (in a small number of tests, a peak memory usage of "75MB" can be observed.

@yuhan6665
Copy link
Member

Is this design to speed up startup time? Please do some profiling of time and mem usage see if it is justified. (Maybe use unit test)

@yin1999 yin1999 marked this pull request as ready for review May 13, 2024 08:31
@RPRX
Copy link
Member

RPRX commented May 14, 2024

感谢 PR,我觉得启动耗时不是大问题,毕竟启动就一下,属于是能开就行,耗内存才是大问题,它会导致 Xray 在低内存平台比如硬路由上无法启动,iOS 上不知道会不会杀掉Xray-core v1.1.4 我还写了个优化,后来被 v2fly 抄去了

想起来之前我改这部分代码时有写缓存机制,但最后默认不用,就是为了省内存,如果需要缓存直接用就行#68 (comment)

45f44c4#diff-dbe303c220dd0c61c55dc37acc6058cc458cad4b6e12a7824b695bd8f179049fR170-R173 现在看这英文有点歧义

Thie PR created some wrappers to temporarily cache the geo data which can reduce the CPU usage.

所以这个。。。

After testing, the changes here can significantly reduce IO reads and reduce the CPU usage duration the configuration loading of this program. There is no very obvious increase in memory usage (in a small number of tests, a peak memory usage of "75MB" can be observed.

就是说看了 #68 ,你会发现 peak 的 75MB 才是问题,相比起来启动时多十几毫秒和一点 IO 不算什么,二选一还是选后者吧

所以很遗憾,期待你的下个 PR

@RPRX RPRX closed this May 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants