这些 strace 系统调用是什么意思?

发布于 2024-11-15 00:17:22 字数 634 浏览 3 评论 0原文

我需要分析我正在使用 strace 的应用程序的性能。但是,我真的不知道如何解释 strace 发出的各种系统调用。其中一些示例如下:

(A) lseek(3, 1600, SEEK_SET)                = 1600
(B) write(3, "G_DATA    300        0          "..., 800) = 800
(C) close(3)                                = 0
(D) mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x2b600b179000
(E) munmap(0x2b600b179000, 4096)            = 0
(F) fstat(3, {st_mode=S_IFREG|0644, st_size=1600, ...}) = 0

如果有人能用简单的英语简要解释从 (A) 到 (F) 的这些行在 I/O、传输的数据、性能重要性等方面的真正含义,我将不胜感激

。浏览了 strace 的手册页,但仍然不太有信心。如果您有任何其他指示供我阅读,那就太好了。

我有一些操作系统背景,了解什么是系统调用、内存、虚拟内存、调度等。

I need to profile the performance of an application for which I am using strace. However, I do not really know how to interpret the various system calls the strace emits. Examples of a few of them are below:

(A) lseek(3, 1600, SEEK_SET)                = 1600
(B) write(3, "G_DATA    300        0          "..., 800) = 800
(C) close(3)                                = 0
(D) mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x2b600b179000
(E) munmap(0x2b600b179000, 4096)            = 0
(F) fstat(3, {st_mode=S_IFREG|0644, st_size=1600, ...}) = 0

I would be grateful if someone could briefly explain in plain English what these lines from (A) to (F) really means in terms of I/O, data transferred, significance on performance etc.

I went through the man pages of strace but still am not very very confident. If you any other pointers for me to read, that would be great.

I have some background on Operating Systems and understand what system calls, memory, virtual memory, Scheduling, etc. are.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

ˇ宁静的妩媚 2024-11-22 00:17:22

为了理解这些,您必须熟悉 POSIX 系统调用。它们是用户空间程序用来与内核交互的接口。

lseekwriteclosemmapmunmapfstat< /code> 都是系统调用,并记录在第 2 节中Linux 手册。

简而言之,lseek 将提供的文件描述符的内部指针移动到第二个参数指向的位置的字节,从SEEK_SET(开头)开始,SEEK_CUR(当前位置)或SEEK_END(结束位置)。对同一描述符的任何连续的读取和写入调用都将从该位置开始执行操作。请注意,lseek 并未针对所有类型的描述符实现 - 它对于磁盘上的文件有意义,但对于套接字或管道则不然。

write 将提供的缓冲区复制到内核空间并返回实际写入的字节数。根据描述符的类型,内核可以将数据写入磁盘或通过网络发送。这通常是一个代价高昂的操作,因为它涉及将此缓冲区传输到内核。

close 关闭提供的描述符,并释放内核中与其相关的任何资源。请注意,每个进程对同时打开的描述符的数量都有限制,因此有时需要关闭描述符以避免达到此限制。

mmap 是一个复杂的系统调用,可用于多种用途,包括共享内存。然而,一般用法是为进程分配更多内存。 malloccalloc 库函数通常在内部使用它。

munmap 释放 mmap 的内存。

fstat 返回文件系统保存的有关文件的各种信息 - 大小、上次修改时间、权限等。

In order to understand these, you have to get familiar with the POSIX system calls. They are the interface a user-space program uses to interact with the kernel.

lseek, write, close, mmap, munmap and fstat are all system calls and are documented in section 2 of the linux manual.

Briefly, lseek moves the internal pointer of the supplied file descriptor to the byte with position pointed to by the second argument, starting from SEEK_SET (the beginning), SEEK_CUR (current position) or SEEK_END (the end). Any consecutive read and write calls on the same descriptor will start their action from this position. Note that lseek is not implemented for all kinds of descriptors - it makes sense for a file on disk, but not for a socket or a pipe.

write copies the supplied buffer to kernelspace and returns the number of bytes actually written. Depending on the kind of the descriptor, the kernel may write the data to disk or send it through the network. This is generally a costly operation because it involves transferring this buffer to the kernel.

close closes the supplied descriptor and any associated resources with it in the kernel are freed. Note that each process has a limit on the number of simultaneously open descriptors, so it's sometimes necessary to close descriptors to not reach this limit.

mmap is a complex system call and is used for many purposes including shared memory. The general usage however is to allocate more memory for the process. The malloc and calloc library functions usually use it internally.

munmap frees the mmap'ped memory.

fstat returns various information that the filesystem keeps about a file - size, last modified, permissions, etc.

对你的占有欲 2024-11-22 00:17:22

对于每个命令都有一个手册页,您可以通过输入 man 和 C 函数名称来阅读它,例如 man lseek (也可查看 apropos >)。它们还有传递参数的描述。

以下是简短的摘要:

  • lseek - 重新定位文件描述符的读/写文件偏移
  • write - 从缓冲区写入文件描述符
  • close -从每个进程对象引用表中删除描述符
  • mmap - 分配内存,或将文件或设备映射到内存
  • munmap - 删除指定地址范围的映射
  • fstat - 获取路径指向的文件状态

请注意解释单个/随机系统在性能方面没有意义。要测试这些系统调用性能的重要性,您应该使用 -c 参数,该参数可以计算每个系统调用的时间、调用和错误并报告摘要。然后您可以阅读有关这些花费最长时间的更多信息。

要了解有关输出和 strace 参数的更多信息,请查看 man strace

另请参阅:如何将 shell 中的 strace 解析为纯文本?

For each command there is a manual page, you can read it by typing man and the name of C function, e.g. man lseek (also check apropos). They also have description of passed parameters.

Here are short summaries:

  • lseek - reposition read/write file offset of the file descriptor
  • write - write to a file descriptor from the buffer
  • close - delete a descriptor from the per-process object reference table
  • mmap - allocate memory, or map files or devices into memory
  • munmap - remove a mapping for the specified address range
  • fstat - get file status pointed to by path

Please note that interpreting single/random syscals won't be meaningful in terms performance. To test significance on performance of these syscalls, you should use -c parameter which can count time, calls, and errors for each syscall and report the summary. Then you can read more about these which are taking the longest time.

To learn more about output and strace parameters, check man strace.

See also: How to parse strace in shell into plain text?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文