bloggerads

2017年7月29日 星期六

Python : 指定不同版本(Python 2 或 Python 3) 執行

若 PC上同時有 Python 2 及Python 3 才需要做以下動作。

● 進 python console
py -2
py -3

●  在 command prompt 執行.py檔
py -2 xxx.py
py -3 xxx.py

● 在檔案中說明該用哪種版本執行
#! python2
#! python3

● python2 使用中文時要在檔案頭宣告
#! python2
# coding: utf-8

●  pip
py -2 -m pip install XXXX
py -3 -m pip install XXXX


Python : Introduction


這篇是部落格的第一篇Python文。。。

最近空閒的時間都在玩Python, 一開始只是隨手看到公司的project中有很多附檔名為.py的tool, 好奇自己動手玩玩後, 就迷上這個語言了。決定把Python當做我的第二語言。

Python 為何風靡全球? 尤其在現今以C style (C/C++/JAVA/C#) 為主的分圍下仍然擁有眾多死忠的支持者,使用者人數排行過去多年來居高不下,想必他一定是有兩把刷子。 以下是Python 的特色 (個人心得):

1. Simple and easy to learn (語法簡單易學啊)
2. Object Oriented Programming (支援物件導向)
3. Versatile libraries (library非常多, 寫C的人最懂沒有library的痛!)
4. Massive communities support and massive population (廣大的社群支持, 就是粉絲團夠力)
5. As known Google/Youtube/N.A.S.A. use Python (一流的大公司都用Python)
6. Easy to package to an executable file (Without Python Interpreter) (打包成執行檔沒問題)
7. Python is everywhere (Preinstall on most Linux distribution) (大部分的Linux內建都有Python)

長期以來都有一個小問題困擾著我, 我是吃韌體這行飯的,高階語言對我來說只是用來分析log, 寫寫小tool方便工作用的。 過去我都是用C++, 如果需要GUI則用C#,  但是實在無法喜歡上這兩種語言。而 Python的哲學, 「優雅」、「明確」、「簡單」, 的確很和我胃口,人生應該要簡單一點,看了程式的一些介紹後就決定好好來認識他。

2017年5月7日 星期日

NVMe : Compare to AHCI

這篇列出AHCI / NVMe的差異


PCIe 2.0: 5GT/s    4Gb/s      500MB/s
PCIe 3.0: 8GT/s    8Gb/s      1GB/s

2017年4月19日 星期三

C : 好用的parsing 數字字串函式 strtol

strol (Convert string to long integer)

須include stdlib.h, 他的原型是 

long int strtol (const char* str, char** endptr, int base);

return value 是 轉換後的十進位值

第一個引數 str 是待轉換的數字字串指標

第二個引數 endptr 則是提供回傳parsing整串字串中, 空白字元分隔的下一個數字字串位址。 如果不用回傳,則給 NULL 即可

第三個引數是說明以什麼格式來 parsing 這個數字字串,  如 2 進位就給 2, 16 進位就給 16, 0 就是 自動偵測數字字串的格式。 (ps.) 16 進位可接受開頭為0x, 亦可接受大小寫

2017年4月16日 星期日

NVMe : Command flow

以下是發送一個NVMe admin/Io command 的流程。相關driver source code 可參考NVMe official website

1. Host Adds a command in the submission queue's tail entry

2. Host rings the submission queue's Tail Doorbell
3. Device fetch the command
4. Device process the command (done)
5. Device adds the completion result in the completion queue's tail entry (Completion result has its relevant command id)
6. Device send a Interrupt(Probably MSI-X)
7. Host reads the completion queue
  7.1 Host has received the completion command 
8. Host rings the completion queue's Head Doorbell


NVMe : Queue Size / Queue entry Size

Queue 相關 size 的 configuration

  • Admin Queue
  1. Completion Queue 和 Submission Queue 各只有一組
  2. Initial 時需設定Queue Size, 即AQA.ACQS, AQA.ASQS, controller 應support 2~4096個entry
  •  I/O Queue 
  1. Initial 時需設定每個entry的Size, 即 CC.IOSQES = 6 (64 Byte), CC.IOCQES = 4 (16 Byte), 這是NVM command的size。
  2. 透過 CAP.MQES (Maximum Queue Entries Supported)得到 I/O Submission/Completion Queue support 的最大entry數量(最少兩個entry)。 在使用Admin Command Create I/O Queue時不要超過他
  3. 透過 Set Feature (Feature Id 07h) 設定host driver所需的Submission/Completion Queues數量 (最大64K個), 從completion queue 回來的資料可以得到Controller 所能allocate 的Submission/Completion Queue數量

2017年4月8日 星期六

利用C語言的正規表達功能來Parsing 傳入程式中的引數

這篇介紹 C 的 sscanf 函數來做到類似 C++11 正規表達的效果,並以 Parsing 傳入程式中的引數做範例
// c.cpp

int main(int argc, char* argv[])
{
    char s0[30], s1[30];
    int ret;
    for (int i=argc-1; i>0; i--) {
        s0[0] = s1[0] = 0;
        //sscanf(argv[i], "%*[^:]:%s", s0); //[^:]代表在':'處設斷點但不包含':', '*'代表忽略'%'起始的字串
        sscanf(argv[i], "-%29[^:]:%29s", s0, s1); 
        if (s0[0] == 0 || s1[0] == 0) 
            puts("Wrong type!");
        else            
            printf("%s : %s\n", s0, s1);
    }   
  return 0;
}

----------------------Output---------------------------
> g++ -o c c.cpp
> c -name:John -age:20
age : 20
name : John