Linux:如何给系统内存增加负担? [英] Linux: How to put a load on system memory?

查看:772
本文介绍了Linux:如何给系统内存增加负担?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个小功能,该功能可以使我的用户了解CPU的占用情况.

I'm working on a small function, that gives my users a picture of how occupied the CPU is.

我正在使用cat /proc/loadavg,它返回众所周知的3个数字.

I'm using cat /proc/loadavg, which returns the well known 3 numbers.

我的问题是,在我进行开发时,CPU现在什么也不做.

My problem is that the CPU doesn't do anything, right now, while I'm developing.

是否有一种在CPU上产生一些负载的好方法,我在想像makecpudosomething 30这样的负载为0.3或类似的负载.是否存在这样的应用程序?

Is there a good way to generate some load on the CPU, I was thinking something like makecpudosomething 30, for a load of 0.3 or similar. Does an application like this exist?

还有,有什么方法可以控制RAM的消耗吗?

Also, are there any way to eat up RAM in a controlled fashion?

推荐答案

如果您想生成任意

I didn't understand very well if you want to generate arbitrary CPU load or CPU utilization. Yes, they are different things indeed. I'll try to cover both problems.

首先: 加载 是在给定的时间内正在运行可运行等待CPU 调度程序队列中的平均进程数,想要您的CPU".

First of all: load is the average number of processes in the running, runnable or waiting for CPU scheduler queues in a given amount of time, "the one that wants your CPU" so to speak.

因此,如果要生成任意 load (例如0.3),则必须运行一个进程30%的时间,然后将其从运行队列中删除70%的时间,例如,将其移至睡眠队列或将其杀死.

So, if you want to generate arbitrary load (say 0.3) you have to run a process for 30% of the time and then remove it from the run queue for 70% of the time, moving it to the sleeping queue or killing it, for example.

您可以尝试使用以下脚本来做到这一点:

You can try this script to do that:

export LOAD=0.3
while true
     do yes > /dev/null &
     sleep $LOAD
     killall yes
     sleep `echo "1 - $LOAD" | bc`
done

请注意,您必须等待一些时间(1、10和15分钟)才能得出相应的数字,这将受到系统中其他进程的影响.系统越忙,此数字将越浮动.最后一个数字(间隔15分钟)往往是最准确的.

Note that you have to wait some time (1, 10 and 15 minutes) to get the respective numbers to come up, and it will be influenced by other processes in your system. The more busy your system is the more this numbers will float. The last number (15 minutes interval) tends to be the most accurate.

CPU使用率 使用哪个CPU来处理计算机程序的指令.

CPU usage is, instead, the amount of time for which CPU was used for processing instructions of a computer program.

因此,如果要生成任意的 CPU使用率(例如30%),则必须运行一个受CPU约束的进程,该进程占用CPU的时间占30%,并且休眠70%.

So, if you want to generate arbitrary CPU usage (say 30%) you have to run a process that is CPU bound 30% of the time and sleeps 70% of it.

我写了一个例子来告诉你:

I wrote an example to show you that:

#include <stdlib.h>
#include <unistd.h>
#include <err.h>
#include <math.h>
#include <sys/time.h>
#include <stdarg.h>
#include <sys/wait.h>

#define CPUUSAGE 0.3      /* set it to a 0 < float < 1 */
#define PROCESSES 1       /* number of child worker processes */
#define CYCLETIME 50000   /* total cycle interval in microseconds */

#define WORKTIME (CYCLETIME * CPUUSAGE)
#define SLEEPTIME (CYCLETIME - WORKTIME)

/* returns t1-t2 in microseconds */
static inline long timediff(const struct timeval *t1, const struct timeval *t2)
{
  return (t1->tv_sec - t2->tv_sec) * 1000000 + (t1->tv_usec - t2->tv_usec);
}

static inline void gettime (struct timeval *t)
{
  if (gettimeofday(t, NULL) < 0)
  {
    err(1, "failed to acquire time");
  }
}

int hogcpu (void)
{
  struct timeval tWorkStart, tWorkCur, tSleepStart, tSleepStop;
  long usSleep, usWork, usWorkDelay = 0, usSleepDelay = 0;

  do
  {
    usWork = WORKTIME - usWorkDelay;
    gettime (&tWorkStart);
    do
    {
      sqrt (rand ());
      gettime (&tWorkCur);
    }
    while ((usWorkDelay = (timediff (&tWorkCur, &tWorkStart) - usWork)) < 0);

    if (usSleepDelay <= SLEEPTIME)
      usSleep = SLEEPTIME - usSleepDelay;
    else
      usSleep = SLEEPTIME;

    gettime (&tSleepStart);
    usleep (usSleep);
    gettime (&tSleepStop);
    usSleepDelay = timediff (&tSleepStop, &tSleepStart) - usSleep;
  }
  while (1);
  return 0;
}

int main (int argc, char const *argv[])
{
  pid_t pid;
  int i;
  for (i = 0; i < PROCESSES; i++)
  {
    switch (pid = fork ())
    {
    case 0:
      _exit (hogcpu ());
    case -1:
      err (1, "fork failed");
      break;
    default:
      warnx ("worker [%d] forked", pid);
    }
  }
  wait(NULL);
  return 0;
}

如果您想吃掉一定数量的RAM,可以在 cgkanchi 的答案中使用该程序.

If you want to eat up a fixed amount of RAM you can use the program in the cgkanchi's answer.

这篇关于Linux:如何给系统内存增加负担?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆