What do 'real', 'user' and 'sys' mean in the output of time(1)? Ask Question

What do 'real', 'user' and 'sys' mean in the output of time(1)? Ask Question
$ time foo
real        0m0.003s
user        0m0.000s
sys         0m0.004s
$

What do real, user and sys mean in the output of time? Which one is meaningful when benchmarking my app?

ベストアンサー1

Real, User and Sys process time statistics

One of these things is not like the other. Real refers to actual elapsed time; User and Sys refer to CPU time used only by the process.

  • Real is wall clock time - time from start to finish of the call. This is all elapsed time including time slices used by other processes and time the process spends blocked (for example if it is waiting for I/O to complete).

  • User is the amount of CPU time spent in user-mode code (outside the kernel) within the process. This is only actual CPU time used in executing the process. Other processes and time the process spends blocked do not count towards this figure.

  • Sys is the amount of CPU time spent in the kernel within the process. This means executing CPU time spent in system calls within the kernel, as opposed to library code, which is still running in user-space. Like 'user', this is only CPU time used by the process. See below for a brief description of kernel mode (also known as 'supervisor' mode) and the system call mechanism.

User+Sys will tell you how much actual CPU time your process used. Note that this is across all CPUs, so if the process has multiple threads (and this process is running on a computer with more than one processor) it could potentially exceed the wall clock time reported by Real (which usually occurs). Note that in the output these figures include the User and Sys time of all child processes (and their descendants) as well when they could have been collected, e.g. by wait(2) or waitpid(2), although the underlying system calls return the statistics for the process and its children separately.

Origins of the statistics reported by time (1)

The statistics reported by time are gathered from various system calls. 'User' and 'Sys' come from wait (2) (POSIX) or times (2) (POSIX), depending on the particular system. 'Real' is calculated from a start and end time gathered from the gettimeofday (2) call. Depending on the version of the system, various other statistics such as the number of context switches may also be gathered by time.

On a multi-processor machine, a multi-threaded process or a process forking children could have an elapsed time smaller than the total CPU time - as different threads or processes may run in parallel. Also, the time statistics reported come from different origins, so times recorded for very short running tasks may be subject to rounding errors, as the example given by the original poster shows.

A brief primer on Kernel vs. User mode

On Unix, or any protected-memory operating system, 「カーネル」または「スーパーバイザー」モードとは、特権モードCPUが動作できるモード。セキュリティや安定性に影響を与える可能性のある特定の特権アクションは、CPUがこのモードで動作しているときにのみ実行でき、これらのアクションはアプリケーションコードでは利用できません。このようなアクションの例としては、MMU別のプロセスのアドレス空間にアクセスします。通常、ユーザーモードコードはこれを実行できない(正当な理由がある)が、共有メモリカーネルから共有メモリが要求され、複数のプロセスによって読み書きされる可能性があります。この場合、共有メモリは安全なメカニズムを通じてカーネルから明示的に要求され、両方のプロセスがそれを使用するには明示的に接続する必要があります。

特権モードは、通常「カーネル」モードと呼ばれます。これは、カーネルがこのモードで実行されているCPUによって実行されるためです。カーネルモードに切り替えるには、特定の命令(多くの場合、トラップ) は、CPU をカーネル モードで実行するように切り替え、ジャンプ テーブルに保持されている特定の場所からコードを実行します。セキュリティ上の理由から、カーネル モードに切り替えて任意のコードを実行することはできません。トラップは、CPU がスーパーバイザ モードで実行されていない限り書き込むことができないアドレス テーブルを通じて管理されます。明示的なトラップ番号でトラップし、アドレスはジャンプ テーブルで検索されます。カーネルには、制御されたエントリ ポイントが限られています。

C ライブラリの「システム」コール (特にマニュアル ページのセクション 2 で説明されているもの) には、ユーザー モード コンポーネントがあり、これが C プログラムから実際に呼び出されるものです。舞台裏では、I/O などの特定のサービスを実行するためにカーネルに 1 つ以上のシステム コールを発行する場合がありますが、ユーザー モードで実行されるコードも存在します。必要に応じて、任意のユーザー空間コードからカーネル モードにトラップを直接発行することもできますが、呼び出し用にレジスタを正しく設定するためにアセンブリ言語のスニペットを記述する必要があるかもしれません。

「sys」についての詳細

コードがユーザー モードから実行できない処理があります。たとえば、メモリの割り当てやハードウェア (HDD、ネットワークなど) へのアクセスなどです。これらはカーネルの監視下にあり、カーネルだけが実行できます。 またはmalloc/freadなどの一部の操作fwriteはこれらのカーネル関数を呼び出し、それが 'sys' 時間としてカウントされます。残念ながら、これは「malloc へのすべての呼び出しが 'sys' 時間としてカウントされる」ほど単純ではありません。 への呼び出しはmalloc独自の処理を実行し (それでも 'user' 時間としてカウントされます)、その後、途中でカーネル内の関数を呼び出すことがあります ('sys' 時間としてカウントされます)。カーネル呼び出しから戻った後、'user' にもう少し時間がかかり、その後mallocコードに戻ります。切り替えがいつ行われるか、またカーネル モードでどのくらいの時間が費やされるかは... わかりません。ライブラリの実装によって異なります。また、他の一見無害な関数もmallocバックグラウンドで などを使用する可能性があり、その場合も 'sys' 時間になります。

おすすめ記事