시간과 같은 명령이 있지만 메모리 사용량에 대한 명령이 있습니까?

시간과 같은 명령이 있지만 메모리 사용량에 대한 명령이 있습니까?

와 같은 명령이 있지만 time더 많은 통계를 보고합니까? 다음과 같은 일을 할 수 있다면 좋을 것 같습니다.

$ statistics some_command
time:
    real    0m3.002s
    user    0m0.000s
    sys     0m0.000s
memory:
    min     41K
    peak    2.5M
    mean    1.1M
. . .

더 나아갈 수 있다면 좋을 것입니다. 지금은 디버깅을 위해 열심히 쳐다보거나 top(실제로는 glances) 코드 전체에 명령문을 뿌리게 됩니다.

내가 명령을 전달할 수 있는 것이 있다면 정말 환상적일 것입니다.

편집하다

perf패키지 linux-toolslinux-tools-commonUbuntu 12.04에서 해결책을 찾았을 수도 있습니다 .

$ perf stat ./someprocess
Performance counter stats for './someprocess':

      12007.384578 task-clock                #    0.996 CPUs utilized          
             1,092 context-switches          #    0.000 M/sec                  
                16 CPU-migrations            #    0.000 M/sec                  
           295,102 page-faults               #    0.025 M/sec                  
    40,553,682,299 cycles                    #    3.377 GHz                     [83.33%]
    18,400,458,723 stalled-cycles-frontend   #   45.37% frontend cycles idle    [83.35%]
     8,356,832,355 stalled-cycles-backend    #   20.61% backend  cycles idle    [66.64%]
    56,930,684,595 instructions              #    1.40  insns per cycle        
                                             #    0.32  stalled cycles per insn [83.34%]
     9,083,443,825 branches                  #  756.488 M/sec                   [83.35%]
         3,431,737 branch-misses             #    0.04% of all branches         [83.33%]

      12.051963969 seconds time elapsed

(도움이 되었던 페이지.)

답변1

zsh기존 time보다 더 강력한 내장 명령이 있으며 해당 버전은 메모리 통계를 보고할 수 있습니다.bashzsh

zsh일상적인 셸로 정기적으로 사용하지 않더라도 이러한 종류의 통계를 수집해야 할 때 실행하면 됩니다.

TIMEFMT원하는 출력을 나타내도록 환경 변수를 설정합니다 . 내 파일에 있는 내용은 다음과 같습니다 .zshrc(아마도 너무 화려할 수도 있지만 마음에 듭니다).

if [[ `uname` == Darwin ]]; then
    MAX_MEMORY_UNITS=KB
else
    MAX_MEMORY_UNITS=MB
fi

TIMEFMT='%J   %U  user %S system %P cpu %*E total'$'\n'\
'avg shared (code):         %X KB'$'\n'\
'avg unshared (data/stack): %D KB'$'\n'\
'total (sum):               %K KB'$'\n'\
'max memory:                %M '$MAX_MEMORY_UNITS''$'\n'\
'page faults from disk:     %F'$'\n'\
'other page faults:         %R'

(복잡한 세부 사항: Linux에서 최대 메모리는 메가바이트이고 macOS에서는 킬로바이트입니다. 의 값을 얻으려면 %Mzsh 호출을 사용 getrusage()하고 다음을 사용합니다 .ru_maxrss / 1024. 하지만 Linux에서는 ru_maxrss킬로바이트 단위이고 Mac에서는 바이트 단위입니다. 두 플랫폼 모두에서 확인하세요 man getrusage.)

샘플 출력:

% time ls
[... the output of ls, followed by:]
ls -G   0.00s  user 0.00s system 91% cpu 0.004 total
avg shared (code):         0 KB
avg unshared (data/stack): 0 KB
total (sum):               0 KB
max memory:                3 MB
page faults from disk:     0
other page faults:         337

답변2

GNU 시간Bash에 내장된 버전보다 더 많은 정보를 보고할 수 있습니다. command time단순히 호출하는 것보다 사용 time하고 자세한 내용은 매뉴얼 페이지나 정보를 참조하세요.

답변3

Richard의 답변을 바탕으로 GNU 시간을 사용하고 평균 및 최대 메모리 정보를 제공하는 별칭을 만들 수 있습니다.

alias time="$(which time) -f '\t%E real,\t%U user,\t%S sys,\t%K amem,\t%M mmem'"

또는 환경을 조정하세요.

export TIME='\t%E real,\t%U user,\t%S sys,\t%K amem,\t%M mmem'

/usr/bin/time그러나 이는 기본적으로 호출되지 않는 경우 에만 작동한다는 점에 유의하세요 .

매뉴얼 페이지에서:

K 프로세스의 평균 총(데이터+스택+텍스트) 메모리 사용량(KB)입니다.

M 수명 동안 프로세스의 최대 상주 세트 크기(킬로바이트)입니다.

답변4

/usr/bin/time와 다른 것을 사용할 수 있습니다 time.

와 함께 사용하면 -v추가 설치 없이 원하는 것을 얻을 수 있습니다.

예:

$ /usr/bin/time -v cat xxx.txt > /dev/null
    Command being timed: "cat xxx.txt"
    User time (seconds): 0.00
    System time (seconds): 0.00
    Percent of CPU this job got: 100%
    Elapsed (wall clock) time (h:mm:ss or m:ss): 0:00.00
    Average shared text size (kbytes): 0
    Average unshared data size (kbytes): 0
    Average stack size (kbytes): 0
    Average total size (kbytes): 0
    Maximum resident set size (kbytes): 2024
    Average resident set size (kbytes): 0
    Major (requiring I/O) page faults: 0
    Minor (reclaiming a frame) page faults: 121
    Voluntary context switches: 0
    Involuntary context switches: 0
    Swaps: 0
    File system inputs: 0
    File system outputs: 0
    Socket messages sent: 0
    Socket messages received: 0
    Signals delivered: 0
    Page size (bytes): 4096
    Exit status: 0

관련 정보