ulimit

問題:函數進入就當,第一行打印也沒有出來

stack size限制是在系統中(ulimit -s可以查看)
若程式裡超過的大小,可以compile,但在run time時會產生問題
不過一般不會定義超過1Mbyte,若超過1Mbyte最好還是用malloc


更新記錄

item note
20170518 第一版

目錄


Stack Size Limit

問題

設定由1080P換到4K
函數內的定義char array大小超過8M(stack size問題)

stack及heap區塊不同

  1. 函數裡面是變數是由stack區塊產生
  2. malloc是由heap區塊產生

函數裡面的變數是進入函數時才去跟stack區塊要
若這時候要超過stack size就會有問題

Imgur
來源:stack

stack size 大小

  • 由limit取得,單位為Kbyte
    可以系統最大stack size為8Mbyte
    1
    2
    [root@localhost nvr]# ulimit -s
    8192

其它

ulimit -u: 顯示可開啟的thread上限
ulimit -u 2048:設定可開啟的thread上限為2048
ulimit -s: 顯示最大stack size Kbyte
ulimit -s 8192: 設定最大stack size 8Mbyte

  • ulimit -a
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    [root@localhost nvr]# ulimit -a
    core file size (blocks, -c) unlimited
    data seg size (kbytes, -d) unlimited
    scheduling priority (-e) 0
    file size (blocks, -f) unlimited
    pending signals (-i) 15243
    max locked memory (kbytes, -l) 64
    max memory size (kbytes, -m) unlimited
    open files (-n) 1024
    pipe size (512 bytes, -p) 8
    POSIX message queues (bytes, -q) 819200
    real-time priority (-r) 0
    stack size (kbytes, -s) 8192
    cpu time (seconds, -t) unlimited
    max user processes (-u) 15243
    virtual memory (kbytes, -v) unlimited
    file locks (-x) unlimited

問題:函數進入就當,第一行打印也沒有出來

  • 原本1080P(即1920x1080)正常
    要了(1920x1080x2x2)=7.9Mbyte的stack size

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    int
    yuv420_to_grb(ldvr_t * ldvr, int width2, int height2)
    {
    printf("%s<line:%d> ==============<<<============ TT20-1 \r\n",
    __FUNCTION__, __LINE__);

    int res, j;
    gui_t *gui = ldvr->gui;
    bitmap_file_header_t bf;
    bitmap_info_header_t bi;

    char vloss_bmp_buf[MAX_IMAGE_SIZE * 2];
  • 改為4K(即3840x2160)異常
    要了(3840x2160x2x2)=31.6Mbyte的stack size,超過8M大小
    改為使用預先malloc的方式,即正常
    ldvr->core->vloss_bmp_buf = (unsigned char *) malloc(MAX_IMAGE_SIZE x 2);

  • define
    1
    2
    3
    4
    5
    6
    7
    8
    #ifdef RESOLUTION_4K
    #define MAX_IMAGE_WIDTH 3840
    #define MAX_IMAGE_HEIGHT 2160
    #else
    #define MAX_IMAGE_WIDTH 1920
    #define MAX_IMAGE_HEIGHT 1080
    #endif
    #define MAX_IMAGE_SIZE (MAX_IMAGE_WIDTH * MAX_IMAGE_HEIGHT * 2)

區域變數生成

compile之後產生組語如下
使用stack區塊,來生成區域變數
在進入函數頂端立即實現區域變數
若要超過大小,就會產生問題,在第一行打印也看不到

[C Stack]

來源:abi pdf page 21

register note
%rax temporary register
%rbx callee-saved register
%rsp stack pointer
%rbp callee-saved register
%rsi used to pass 2nd argument to functions
%rdi used to pass 1st argument to functions
  • CFA
    • Typically, the CFA is defined to be the value of the stack pointer at the call site in the previous frame
    • .cfi_def_cfa_offset directive, and you can see that the CFA is now at an offset of 16 bytes from the current stack pointer.
    • gas-explanation-of-cfi-def-cfa-offset

來源