hw-1

bash bc command is ignoring scale option

bash bc command is ignoring scale option

if you check man page, you can find that line. it is about expression explanations. subtraction won’t read scale variable. If you want to get the expected result (1.30), you could:

1
2
$  echo 'scale=2; (2.777 - 1.4744)/1' | bc 
1.30

cocoapods pospec file config

cocoapods pospec file config

1
2
3
4
5
6
s.xcconfig = { 'LIBRARY_SEARCH_PATHS' => ' "$(PODS_ROOT)/VendorModule/VendorModule/WeChat" "$(PODS_ROOT)/VendorModule/VendorModule/Weibo"'  ,'OTHER_LDFLAGS' => '-all_load' }

s.frameworks = 'Photos', 'ImageIO', 'SystemConfiguration', 'CoreText', 'QuartzCore', 'Security', 'UIKit', 'Foundation', 'CoreGraphics','CoreTelephony'
s.libraries = 'sqlite3', 'z','c++','WeiboSDK','WeChatSDK'

s.static_framework = false

Set ulimit -n / open files Permanently in ubuntu

Set ulimit -n / open files Permanently in ubuntu

1
2
3
4
5
6
7
8
9
10
11
12
# available limit
user@ubuntu:~$ ulimit -n
1024

# To increase the available limit to say 65535
user@ubuntu:~$ sudo vim /etc/sysctl.conf

# add the following line to it
fs.file-max = 65535

# run this to refresh with new config
user@ubuntu:~$ sudo sysctl -p

if you got error “sysctl: permission denied on key ‘fs.file-max’” or not effect , you can skip that step, then you doing steps as below :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# edit the following file
user@ubuntu:~$ sudo vim /etc/security/limits.conf

# add following lines to it
* soft nproc 65535
* hard nproc 65535
* soft nofile 65535
* hard nofile 65535
root soft nproc 65535
root hard nproc 65535
root soft nofile 65535
root hard nofile 65535

# edit the following file
user@ubuntu:~$ sudo vim /etc/pam.d/common-session

# add this line to it
session required pam_limits.so

# logout and login and try the following command
user@ubuntu:~$ ulimit -n
65535

iOS APP启动速度优化的一些经验

iOS APP启动速度优化的一些经验

应用启动流程
iOS应用的启动可分为pre-main阶段和main()阶段,其中系统做的事情依次是:

  1. pre-main阶段
    1.1. 加载应用的可执行文件
    1.2. 加载动态链接库加载器dyld(dynamic loader)
    1.3. dyld递归加载应用所有依赖的dylib(dynamic library 动态链接库)

  2. main()阶段
    2.1. dyld调用main()
    2.2. 调用UIApplicationMain()
    2.3. 调用applicationWillFinishLaunching
    2.4. 调用didFinishLaunchingWithOptions

  3. pre-main阶段
    对于pre-main阶段,Apple提供了一种测量方法,在 Xcode 中 Edit scheme -> Run -> Auguments 将环境变量DYLD_PRINT_STATISTICS 设为1 :

  4. main()阶段
    对于main()阶段,主要是测量main()函数开始执行到didFinishLaunchingWithOptions执行结束的耗时,就需要自己插入代码到工程中了。先在main()函数里用变量StartTime记录当前时间:
    CFAbsoluteTime StartTime;
    int main(int argc, char * argv[]) {
    StartTime = CFAbsoluteTimeGetCurrent();
    double launchTime = (CFAbsoluteTimeGetCurrent() – StartTime);

pre-main阶段的优化

  1. Load dylibs
  2. 1 尽量不使用内嵌(embedded)的dylib,加载内嵌dylib性能开销较大
  3. 2 合并已有的dylib和使用静态库(static archives),减少dylib的使用个数 — podspec set s.static_framework = true
  4. 3 懒加载dylib,但是要注意dlopen()可能造成一些问题,且实际上懒加载做的工作会更多
  1. Rebase/Bind
    2.1 减少ObjC类(class)、方法(selector)、分类(category)的数量
    2.2 减少C++虚函数的的数量(创建虚函数表有开销)
    2.3 使用Swift structs(内部做了优化,符号数量更少)

    1. Objc setup
      大部分ObjC初始化工作已经在Rebase/Bind阶段做完了,这一步dyld会注册所有声明过的ObjC类,将分类插入到类的方法列表里,再检查每个selector的唯一性。
  2. Initializers
    到了这一阶段,dyld开始运行程序的初始化函数,调用每个Objc类和分类的+load方法,调用C/C++ 中的构造器函数(用attribute((constructor))修饰的函数),和创建非基本类型的C++静态全局变量。Initializers阶段执行完后,dyld开始调用main()函数。

main阶段的优化
1 不使用xib,直接视用代码加载首页视图
2 NSUserDefaults实际上是在Library文件夹下会生产一个plist文件,如果文件太大的话一次能读取到内存中可能很耗时,这个影响需要评估,如果耗时很大的话需要拆分(需考虑老版本覆盖安装兼容问题)
3 每次用NSLog方式打印会隐式的创建一个Calendar,因此需要删减启动时各业务方打的log,或者仅仅针对内测版输出log
4 梳理应用启动时发送的所有网络请求,是否可以统一在异步线程请求

raspberry redsocks forward

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
<br />sudo apt install redsocks

/etc/sysctl.conf:
net.ipv4.ip_forward = 1
sudo sysctl -p /etc/sysctl.conf
sudo sysctl -w net.ipv4.ip_forward=1
#sudo service networking restart



sudo iptables -t nat -L -n

sudo iptables -F
sudo iptables -X
sudo iptables -t nat -F
sudo iptables -t nat -X
sudo iptables -t mangle -F
sudo iptables -t mangle -X
sudo iptables -P INPUT ACCEPT
sudo iptables -P FORWARD ACCEPT
sudo iptables -P OUTPUT ACCEPT

# interface forward
sudo iptables -A FORWARD -i wlan0 -o lo -m state --state RELATED,ESTABLISHED -j ACCEPT
sudo iptables -A FORWARD -i lo -o wlan0 -j ACCEPT

# Allow established connections
sudo iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
sudo iptables -A FORWARD -m state --state ESTABLISHED,RELATED -j ACCEPT

# Masquerade
sudo iptables -t nat -A POSTROUTING -o eth1 -j MASQUERADE


sudo redsocks
sudo vim /etc/redsocks.conf


curl --socks5 127.0.0.1:31338 https://viewerport.com/app/


sudo iptables -t nat -N REDSOCKS
sudo iptables -t nat -A REDSOCKS -d 0.0.0.0/8 -j RETURN
sudo iptables -t nat -A REDSOCKS -d 10.0.0.0/8 -j RETURN
sudo iptables -t nat -A REDSOCKS -d 127.0.0.0/8 -j RETURN
sudo iptables -t nat -A REDSOCKS -d 169.254.0.0/16 -j RETURN
sudo iptables -t nat -A REDSOCKS -d 172.16.0.0/12 -j RETURN
sudo iptables -t nat -A REDSOCKS -d 192.168.0.0/16 -j RETUR
sudo iptables -t nat -A REDSOCKS -d 172.24.0.0/16 -j RETURN
sudo iptables -t nat -A REDSOCKS -d 224.0.0.0/4 -j RETURN
sudo iptables -t nat -A REDSOCKS -d 240.0.0.0/4 -j RETURN
sudo iptables -t nat -A REDSOCKS -p tcp -j REDIRECT --to-ports 31338
sudo iptables -t nat -A PREROUTING --in-interface lo -p tcp -j REDSOCKS
sudo iptables -t nat -A PREROUTING --in-interface wlan0 -p tcp -j REDSOCKS

sudo iptables-restore < /home/pi/redsocks.ip

sudo iptables-save

关于单例滥用导致的crash分析

关于单例滥用导致的crash分析

原理
dispatch_once并不是简单的只执行一次那么简单

dispatch_once本质上可以接受多次请求,会对此维护一个请求链表

如果在block执行期间,多次进入调用同类的dispatch_once函数(即单例函数),会导致整体链表无限增长,造成永久性死锁。

优化

仅仅使用一次的模块,可以不使用单例,可以采用在对应的周期内维护成员实例变量进行替换。

和状态无关的模块,可以采用静态(类)方法直接替换。可以通过页面跳转进行依赖注入的模块,可以采用依赖注入或者变量传递等方式解决。

当然,的确有一些情况我们仍然需要使用单例。那在这种情况,也请将dispatch_once调用的block内减少尽可能多的任务,最好是仅仅负责初始化,剩下的配置、调用等等在后续进行。

ubuntu ssh disable password login

ubuntu ssh disable password login

1
2
3
4
5
6
7
8
9
in /etc/ssh/sshd_config

change this
#ChallengeResponseAuthentication yes
#PasswordAuthentication yes
to
ChallengeResponseAuthentication no
PasswordAuthentication no
sudo service ssh restart