Shooting's Blog

C++11 pointer

Pointer types

  • UNIQUE_PTR

    • 確保一份資源(被配置出來的記憶體空間)只會被一個 unique_ptr 物件管理的 smart pointer;當 unique_ptr 物件消失時,就會自動釋放資源。
  • SHARED_PTR

    • 可以有多個 shared_ptr 共用一份資源的 smart pointer,內部會記錄這份資源被使用的次數(reference counter),只要還有 shared_ptr 物件的存在、資源就不會釋放;只有當所有使用這份資源的 shared_ptr 物件都消失的時候,資源才會被自動釋放。
  • WEAK_PTR

    • 搭配 shared_ptr 使用的 smart pointer,和 shared_ptr 的不同點在於 weak_ptr 不會影響資源被使用的次數,也就是說的 weak_ptr 存在與否不代表資源會不會被釋放掉。
    • 由於 weak_ptr 本身不能用來做資料的存取,所以如果要使用的話,實際上是需要先將 weak_ptr 轉換回 shared_ptr 的。轉換的方法也很簡單,就是使用 weak_ptr 提供的 lock() 這個函式,來產生一個有擁有權的 shared_ptr。

Difference in make_shared and normal shared_ptr in C++

  • The difference is that std::make_shared performs one heap-allocation, whereas calling the std::shared_ptr constructor performs two.

  • std::shared_ptr manages two entities:

    • the control block (stores meta data such as ref-counts, type-erased deleter, etc)
    • the object being managed
  • std::make_shared performs a single heap-allocation accounting for the space necessary for both the control block and the data.

Java's pointer concept

  • 只要還有strong reference指著,就不會被gc
  • 被weak reference指著的object,還是有可能被gc,就是有權去回收weak reference指向的object,使用weak reference的user要能handle。

Reference

Avoid memory leak
https://kheresy.wordpress.com/2012/03/03/c11_smartpointer_p1/
https://kheresy.wordpress.com/2012/03/05/c11_smartpointer_p2/

C++11 smart pointer series
https://www.jianshu.com/p/e4919f1c3a28

Difference in make_shared and normal shared_ptr in C++
https://stackoverflow.com/questions/20895648/difference-in-make-shared-and-normal-shared-ptr-in-c
https://blog.csdn.net/love_hot_girl/article/details/21161507

How to create shared_ptr
https://docs.microsoft.com/zh-tw/cpp/cpp/how-to-create-and-use-shared-ptr-instances?view=vs-2017

Repo Problem

Repo sync failed

Issue

  • Repo Sync failed (error: branch xxx is published (but not merged) and is now n commit behind)
  • Cannot do ‘mtk_repo sync’ (after last upload to Gerrit) is published (but not merged) and is now x commits behind since last upstream (R1)

Reason

  C1 <- C2 <- C3 <- N1 <- N2 <- N3 <- N4 <- N5 (Server latest)
              ^<------R1<--------^
              (branch1)  (cherry-pick)
  • R1 != N3 (even R1 is picked/rebased)
  • Local is R1, server latest is N5

Solution

1.1: repo start $localbranch . to create a new local branch
1.2: repo sync . to update local branch to latest

2.1: git remote update to fetch objects from remotes
2.2: git reset --hard <remote>/<branch> to reset local branch as the same as server
------ (git remote to get remote names)

Simpleperf

What is simpleperf ?

  • 在 Linux 上內建提供 perf tool, 可以用它做效能的分析.
  • 而 Android 上, Google 利用同樣概念, 提供 simpleperf 工具

How simpleperf works ?

Simpleperf 利用 CPU 內的 PMU,將需要的事件不斷的輸出 (比方 cpu-cycles ),
以達到監控每個CPU每個瞬間跑在哪一行 arm 指令.
透過這樣取樣的方式, 我們可以假設取樣到同區域的次數越多, 代表該段代碼越可能是執行瓶頸.

Script

每10ms錄一次共錄120s
adb root
adb remount
adb push simpleperf /data
adb shell chmod 755 /data/simpleperf
adb shell "/data/simpleperf record  -g -p `pidof camerahalserver` --duration 120 -f 100 -o /sdcard/perf.data"
adb shell /data/simpleperf --log error report -i /sdcard/perf.data > 1-self.txt
adb shell /data/simpleperf --log error report --children -i /sdcard/perf.data > 2-children.txt
adb shell /data/simpleperf --log error report -g -i /sdcard/perf.data > 3-caller.txt
adb shell /data/simpleperf --log error report -g callee -i /sdcard/perf.data > 4-callee.txt

adb problem

Problem

  • Cannot use adb
C:\Users\mtk04284>adb devices
List of devices attached
* daemon not running. starting it now on port 5037 *
error: could not install *smartsocket* listener: cannot bind to 127.0.0.1:5037:
could not read ok from ADB Server
* failed to start daemon *
error: cannot connect to daemon

Solution

  • Kill process who occupy adb's port
Check who occupy 5037 port
C:\Users\mtk04284>netstat -aon | findstr "5037"
  TCP    172.24.13.162:5037     172.23.1.37:54645      ESTABLISHED     1584
Check which process (PID) is 1584
C:\Users\mtk04284>tasklist | findstr "1584"
spoolsv.exe                   1584 Services                   0     16,340 K
Force close Process
taskkill /pid 1584 /f   (Never tried, not sure if it works.)
Restart server
adb start-server

Reference

Adb shell

List all command supported by dumpsys

dumpsys | grep "DUMP OF SERVICE" 
Part of result
shell@m:/ $ dumpsys | grep "DUMP OF SERVICE" 
dumpsys | grep "DUMP OF SERVICE" 
DUMP OF SERVICE AtCmdFwd: 
DUMP OF SERVICE meminfo: 

Check help for dumpsys

dumpsys meminfo -h

Dump meminfo

increase dump time to 30 seconds
adb shell dumpsys -t 30 meminfo
check certain process
adb shell dumpsys meminfo camerahalserver

Reference

https://www.cnblogs.com/0616--ataozhijia/p/5678602.html

Android Memory

Dump memory info

  • Ion memory
    adb shell cat /d/ion/ion_mm_heap > ion.log
    

ION MM heap

adb pull /sys/kernel/debug/ion/ion_mm_heap
  • Other
    adb shell dumpsys meminfo
    adb shell dumpsys meminfo
    adb shell procrank
    adb shell procmem pid
    

Python install plugin

Install PIP in windows

Reference

  • 下載 get-pip.py,接著透過 Command line 執行安裝
  • pip 會放在 Scripts 資料夾下,將此路徑加入環境變數 Path C:\Python27\Scripts

Update PIP

python -m pip install --upgrade pip

Install plug-in

  • ex. xlwt (for write to excel) pip install xlwt

Android Thread Intro

Android Thread

  • Mutex:
    • 構造函數傳入 type,當 type==SHARED 表明支援跨進程的執行緒同步;
    • 調用 lock() 或 tryLock() 嘗試加鎖,根據返回值判斷是否成功;系統保證每次只有一個執行緒加鎖成功;
    • 操作完後,調用 unlock() 釋放鎖;
  • AutoLock:
    • 構造函數傳入 Mutex 物件;
    • 在析構函數中自動調用 unlock(),防止忘記調用造成鎖死;
  • Condition:
    • 構造函數傳入 type,當 type==SHARED 表明支援跨進程的條件同步;
    • wait(&mutex)等待;
    • waitRelative(&mutex,time),在 time 時間內等待,超時退出等待;
    • signal() 通知當前等待的執行緒;
    • broadcast() 通知所有等待的執行緒;
    • Condition 物件的方法必須放在 Mutex 物件的 lock() 和 unlock() 之間的範圍調用;
  • 某種寫法
    • exitPending(): return true if requestExit() has been called.