gpt4 book ai didi

linux - 为什么我的 mod_perl 脚本会卡住我的服务器?

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:17:21 24 4
gpt4 key购买 nike

我无法使我的 Perl 脚本在服务器上稳定运行。问题就在这里。

当每秒访问脚本超过 5 次时,服务器会卡住。一段时间后,服务器永远挂起。SSH 没有响应,我必须重新启动服务器。

我将 Apache 与 mod_perl 结合使用。

该脚本托管在 Ubuntu 下的虚拟专用服务器上。我正在通过 SSH 操作它。这些是服务器参数中央处理器:400 兆赫内存:256MB

脚本的最长执行时间为 200 毫秒。

我已经使用“top”实用程序监控服务器负载。它没有显示任何问题,这是每秒加载 5 个脚本期间的 CPU 统计信息:

Cpu(s): 12.1%us,  0.6%sy,  0.0%ni,  0.0%id,  0.0%wa,  0.0%hi,  0.0%si, 87.2%st

What options do I have to make the script work without problems?

This is the result of ps aux | fgrep perl at the moment of loading:

ps aux | fgrep perlwww-data  2925  0.3  6.5  45520 17064 ?        R    17:00   0:01 /var/www/perl/loa -k startwww-data  2926  0.2  6.5  45520 17068 ?        R    17:00   0:01 /var/www/perl/loa -k startwww-data  2927  0.4  6.5  45676 17060 ?        R    17:00   0:01 /var/www/perl/loa -k startwww-data  2928  0.3  6.5  45676 17060 ?        R    17:00   0:01 /var/www/perl/loa -k startwww-data  2929  0.2  6.5  45676 17060 ?        R    17:00   0:01 /var/www/perl/loa -k startwww-data  2931  0.4  6.5  45740 17076 ?        R    17:00   0:01 /var/www/perl/loa -k startroot      2968  0.0  0.2   3196   656 pts/0    R+   17:06   0:00 fgrep perl

UPDATE

I have found the bottleneck.I've been using DateTime module many times around the code.The following DateTime module methods appear to be very slow.

  • new()
  • now()
  • set(...)
  • delta_ms(...)

I'm going to substitute them with fast analogs.

Another concern.mod_perl instance takes a lot of memory.And I have no idea why.I have tried to run a simple perl script that does not import any modules.I run it just after apache restart.The script takes 37M of memory.Why does it happen?Do you know how to force mod_perl do not use the extra memory?

A regular perl script, without mod_perl support, takes 3-5M of memory.

Guys, thank you for so much help, I wasn't expecting such a wonderful response!

UPDATE 2

I have found one more fact.I've created a simple perl script that just waits for 5 seconds.

#!/usr/bin/perl
use CGI;

my $query= new CGI;
my $content = "5 second delay...\n";

$query->header(
'-Content-type' => "text/plain",
'-Content-Length' => length($content)
);

print $content;

sleep(5);

然后我同时生成许多这些脚本。顶级实用程序中的隐身时间 (st) 从 0% 跃升至 80%,并保持在较高水平,直到脚本完成。

这个负载从哪里来?

此外,正如我已经提到的,每个 perl 实例占用 36M 内存。

最佳答案

top 中的数字似乎表明您的 VM 之外的其他进程正在限制您的 CPU,请注意最后一个数字,87.2%st,这表明大约 87%你的 CPU 时间被你的管理程序分配给你的 VM 之外的任务,即使你的 VM 有它想要运行的东西。这是否与您的问题有关很难说。

除了按照 unwind 的建议升级您的服务器之外,使用 zoul 建议的持久进程环境,您的进程可能根本不受 CPU 限制,而是受 IO 限制,例如网络或磁盘访问,或内存限制。如果没有关于您的脚本在调用时实际执行的操作的更多详细信息,则很难说清楚。

编辑:您更新的关于内存使用信息的问题很有启发性,因为您的每个进程都需要 45M 的内存,并且正在共享 17M 以上的内存。仅运行 5 或 6 个进程,您就超出了可用的 RAM 量。对于一个普通的 Perl 脚本来说,这是一个很好的内存量,它用它做什么?

关于linux - 为什么我的 mod_perl 脚本会卡住我的服务器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1543536/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com