简体   繁体   English

如何放慢流程?

[英]how to slow down a process?

Suppose I have a program that runs in a given amount of time (say, three seconds). 假设我有一个程序在给定的时间内运行(比方说,三秒钟)。 I want to run this program so that it runs n-times slower (specified on command line). 我想运行这个程序,使其运行速度慢n倍(在命令行中指定)。 How would you achieve it with (or better, without) changes to the program ? 如何通过(或更好,没有)更改程序来实现它?

please note that adding a sleep at the end is not a solution. 请注意,最后添加睡眠不是解决方案。 The program has to run slower, not to run at full speed for the first three seconds and then do nothing for the remaining time. 该程序必须运行较慢,不要在前三秒内全速运行,然后在剩余时间内不执行任何操作。 Also, using "nice" under unix is not a good solution either. 另外,在unix下使用“nice”也不是一个好的解决方案。 it will run slower if other processes demand the processor, but at full speed if nothing is processor-demanding at the same time. 如果其他进程需要处理器,它将运行得更慢,但如果同时没有任何处理器要求,则全速运行。

This is a curiosity question. 这是一个好奇心的问题。 Nothing serious to do related to it. 没有什么可以做的与之相关。 The fact is that I remember 15-20 years ago games that were simply too fast to play on new processors, because they were timed with the processor clock. 事实是,我记得15到20年前的游戏太快而无法在新处理器上播放,因为它们与处理器时钟同步。 You had to turn off the turbo. 你不得不关掉涡轮增压器。

Let's assume the program is a C compiled program. 我们假设该程序是一个C编译程序。

One idea is to write a 'ptrace runner.' 一个想法就是写一个'ptrace runner'。 ptrace is the call that allows you to implement a debugger on platforms such as Linux and Mac. ptrace是允许您在Linux和Mac等平台上实现调试器的调用。

The idea is to attach to the program and then just repeatedly tell the application to run one instruction with ptrace(PTACE_SINGLESTEP) . 想法是附加到程序,然后反复告诉应用程序使用ptrace(PTACE_SINGLESTEP)运行一条指令。 If that's not slow enough, you could add a sleep between each call to ptrace in the runner program. 如果这还不够慢,你可以在跑步者程序中每次调用ptrace之间添加一个睡眠。

I wrote a simple example on my linux box how to slow down a child process with SIGSTOP and SIGCONT signals: 我在我的linux盒子上写了一个简单的例子,如何使用SIGSTOP和SIGCONT信号减慢子进程:

#include <unistd.h>
#include <stdio.h>
#include <signal.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>

void dosomething(void){
  static volatile unsigned char buffer[1000000];
  for(unsigned i=0;i<1000;i++) for(unsigned j=0;j<sizeof(buffer);buffer[j++]=i){;}
}

#define RUN  1
#define WAIT 1

int main(void){
  int delay=0, status, pid = fork();
  if( !pid ){ kill(getpid(),SIGSTOP); dosomething(); return 0; }
  do{
    waitpid( pid, &status, WUNTRACED | WCONTINUED );
    if( WIFSTOPPED  (status)         ){ sleep(delay); kill(pid,SIGCONT); }
    if( WIFCONTINUED(status) && WAIT ){ sleep(RUN  ); kill(pid,SIGSTOP); }
    delay=WAIT;
  }while( !WIFEXITED(status) && !WIFSIGNALED (status) );
}

No slowdown when WAIT is zero, otherwise after every RUN seconds the parent stop the child for WAIT seconds. WAIT为零时没有减速,否则在每个RUN秒之后,父母将孩子停止WAIT秒。

Runtime results: 运行时结果:

RUN=1 WAIT=0
---------------
real     3.905s
user     3.704s
sys      0.012s

RUN=1 WAIT=1
---------------
real     9.061s
user     3.640s
sys      0.016s

RUN=1 WAIT=2
---------------
real    13.027s
user     3.372s
sys      0.032s

cpulimit is a tool that does something like this. cpulimit是一个做这样的事情的工具。 It works by periodically kill -STOP and kill -CONT the process, which has the effect of it running slower (when averaged over time). 它的工作原理是定期kill -STOPkill -CONT这个过程,这会使它运行kill -CONT慢(当平均时间变长时)。

If you have DTrace you may be able to use it's chill() function. 如果你有DTrace,你可以使用它的chill()函数。 You could insert this chill at almost anyplace in a userland application and in multiple places. 您可以在用户空间应用程序中的多个位置和多个位置插入此寒意。 It's been used before to replicate race conditions seen on slower systems. 以前用它来复制在较慢系统上看到的竞争条件。

I ran some application in a virtual machine under ubuntu. 我在ubuntu下的虚拟机中运行了一些应用程序。 It was really slow. 这真的很慢。 You could configure the virtual machine usage of the system. 您可以配置系统的虚拟机使用情况。

You might obfuscate the situation a little further by running a virtual machine under a virtual machine under a virtual machine, ... 您可以通过在虚拟机下的虚拟机下运行虚拟机来进一步模糊这种情况,......

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

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