简体   繁体   English

首次输出到stdout后特定子进程的用户CPU时间

[英]User CPU time of specific child process after first output to stdout

I'm working on a program which may spawn multiple child processes, and I need to get precise information about the CPU time used by each child process, even if there are several child processes running simultaneously. 我正在开发一个可能产生多个子进程的程序,我需要获得有关每个子进程使用的CPU时间的精确信息,即使有多个子进程同时运行。 I'm doing this using wait4(2) on a separate thread of the parent process, which works quite well. 我正在使用wait4(2)在父进程的一个单独的线程上执行此操作,这非常有效。

However, this approach provides the total time spent by a specific child process, and I'm only interested in the amount of time spent after a particular event, namely the child process' first output to stdout. 但是,这种方法提供了特定子进程花费的时间,并且我只对特定事件之后花费的时间感兴趣,即子进程'首次输出到stdout。 I've looked into other ways of getting the CPU time of child processes, such as getrusage(2) and times(3), but these don't seem to be able to distinguish between multiple child processes' times, and instead provide the sum of all child processes' times. 我已经研究了获取子进程的CPU时间的其他方法,例如getrusage(2)和times(3),但这些方法似乎无法区分多个子进程的时间,而是提供所有子进程的时间总和。

I'm working on a text editor application that lets users run scripts and code in a variety of different languages, and the app has a built-in code timing feature. 我正在开发一个文本编辑器应用程序,它允许用户以各种不同的语言运行脚本和代码,并且该应用程序具有内置的代码计时功能。 The app relies on bash scripts to run the users code, and the first thing my bash scripts do are to output a start-of-heading byte (0x02). 该应用程序依赖于bash脚本来运行用户代码,而我的bash脚本所做的第一件事就是输出一个头部开头字节(0x02)。 After this the bash script does whatever it needs to do to run the users code, and that is the thing I want to time. 在此之后,bash脚本会执行运行用户代码所需的任何操作,这就是我想要的时间。 Bash may do a bit of initialization (to set up PATH variables etc) which may take 30 or 40 ms to complete, and I don't want that initialization to be timed along with the rest. Bash可能会进行一些初始化(设置PATH变量等),这可能需要30或40 ms来完成,我不希望初始化与其余的一起计时。 If the users code is for instance a simple Hello World type program in C, the timing feature might display something like 41 ms instead of the actual 1 ms which it took to run their code. 如果用户代码例如是C中的简单Hello World类型程序,则计时功能可能会显示41毫秒而不是运行其代码所花费的实际1毫秒。

Any ideas on how this might be done? 关于如何做到这一点的任何想法?

Thanks :) 谢谢 :)

A couple of possible solutions come to mind. 我想到了几种可能的解决方案。 They don't get CPU time after first output exactly, but they may avoid the problem you're dealing with. 它们在第一次输出后没有得到CPU时间,但它们可以避免你正在处理的问题。

The first is to get rid of the bash scripts and just do the equivalent work in your program before running the user's code (between fork() and exec() , for example). 第一个是摆脱bash脚本,只是在运行用户代码之前在程序中执行相同的工作(例如,在fork()exec()之间)。 That way the child process' CPU time from wait4() doesn't include your extra setup. 这样子进程'来自wait4()的CPU时间不包括你的额外设置。

Another possibility is to write a simple application that does nothing but run the user's application and report its CPU time back to your main application. 另一种可能性是编写一个简单的应用程序,除了运行用户的应用程序并将其CPU时间报告回主应用程序之外什么都不做。 That runner application can then be called from your scripts to run the user's program, rather than calling the user's program directly. 然后可以从脚本调用该运行器应用程序来运行用户程序,而不是直接调用用户程序。 The runner application might itself use fork() / exec() / wait4() to run the user's program, and could report the information from wait4() to your main program through any of a variety of means such as a named pipe, message queue, socket, or even just writing the information to a file your main program can open afterward. 运行器应用程序本身可以使用fork() / exec() / wait4()来运行用户程序,并可以通过各种方式(如命名管道,消息wait4()wait4()的信息报告给主程序。 queue,socket,甚至只是将信息写入主程序之后可以打开的文件中。 That way your bash scripts can do work both before and after running the user's program that won't be included in the CPU time reported by the runner application. 这样,您的bash脚本可以在运行用户程序之前和之后工作,该程序将不包含在运行器应用程序报告的CPU时间中。 You'd probaby want the runner to accept an argument like the name of a pipe or an output file in addition to the user's program's path and arguments so that you can control how the information is reported -- that way you could run more than one instance of the runner application and still keep the information they report separate. 除了用户程序的路径和参数之外,你还想让运行器接受一个像管道名称或输出文件这样的参数,以便你可以控制信息的报告方式 - 这样你可以运行多个跑步者应用程序的实例,仍然保持他们报告的信息是分开的。


If you do want to include the work done by the script, but not the time taken to load bash , then you could signal the main program by echoing something to a pipe from the bash script before and after the parts you want to time. 如果你确实希望包含脚本完成的工作,而不是加载bash所需的时间,那么你可以通过在你想要的时间部分之前和之后从bash脚本回显一些管道来向主程序发出信号。 The main program can then measure the time between the start and stop signals, which will at least get you wall-clock time (though not actual CPU time). 然后主程序可以测量启动和停止信号之间的时间,这至少可以获得挂钟时间(尽管不是实际的CPU时间)。 Otherwise I'm not sure there's a way to perfectly measure the CPU time for just part of the script without using a modified bash (which I'd avoid if possible). 否则,我不确定是否有办法在不使用修改后的bash的情况下完全测量脚本的一部分CPU时间(如果可能的话我会避免)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM