设置java使用一个cpu

发布于 2024-07-21 18:28:45 字数 197 浏览 10 评论 0 原文

我有一个拥有一定数量 cpu 许可证的应用程序,我希望能够在检查完成之前将 java 运行的 cpu 数量设置为 1。 我正在运行 Solaris,并查看了 pbind,但认为如果我启动应用程序然后使用 pbind,它会在设置 java 可以使用的 CPU 数量之前检查许可证。

有谁知道在 Solaris 上使用一定数量的 CPU 启动应用程序的方法吗?

I have an application that has a license for a set number of cpus and I want to be able to set the number of cpus that java runs in to 1 before the check is done. I am running Solaris and have looked at pbind but thought that if I started the application and then used pbind it would have checked the license before it had set the number of CPUs that java could use.

Does anyone know a way of starting an application with a set number of CPUs on Solaris?

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

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

发布评论

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

评论(4

冬天旳寂寞 2024-07-28 18:28:45

我认为对你的问题最直接的答案是使用 pbind 绑定正在运行的 shell 进程,然后从该 shell 启动 Java。 根据手册页,pbind 的效果由从绑定进程创建的进程继承。 尝试这个:

% pbind -b 0 $
% java ...

I think the most direct answer to your question is to use pbind to bind the running shell process, and then start Java from that shell. According to the man page the effects of pbind are inherited by processes that are created from a bound process. Try this:

% pbind -b 0 $
% java ...
浮萍、无处依 2024-07-28 18:28:45

谷歌搜索了一下,我发现你是对的,pbind将进程绑定到处理器。

更多信息和示例位于:http:// docs.sun.com/app/docs/doc/816-5166/pbind-1m?a=view

Googling over, I found that you are right, pbind binds processes to processors.

More info and examples at: http://docs.sun.com/app/docs/doc/816-5166/pbind-1m?a=view

故事和酒 2024-07-28 18:28:45

这是一种解决方法,但使用 Solaris 10,您可以设置一个具有单个可用 CPU 的区域,然后在该区域内运行应用程序。

如果您想在不运行完整应用程序的情况下进行测试,那么他们很可能使用这段 Java 来获取 CPU 数量:

Runtime runtime = Runtime.getRuntime();
int nrOfProcessors = runtime.availableProcessors();

完整示例 此处

It is a workaround, but using Solaris 10 you could set up a zone with a single CPU available and then run the application inside that zone.

If you want to do testing without running the full application, this bit of Java is most likely what they are using to get the number of CPU's:

Runtime runtime = Runtime.getRuntime();
int nrOfProcessors = runtime.availableProcessors();

A full example here.

揽清风入怀 2024-07-28 18:28:45

这不是一个完整的解决方案,但可能足以发展成为一个解决方案。 肯定存在 java 进程存在的时刻(因此可以由 pbind 控制),并且此时它尚未运行代码来执行处理器检查。 如果您可以暂停应用程序本身的启动,直到 pbind 完成其工作,那么这应该没问题(假设 pbind 的想法从 CPU 检查的角度来看是可行的)。

一种肯定应该在适当的位置暂停 JVM 的方法是远程调试器的套接字连接并以挂起模式启动。 如果将以下参数传递给 java 调用:

-Xdebug -Xrunjdwp:传输=dt_socket,地址=8000,挂起=y,服务器=y

那么 JVM 将在启动 java 进程之后但在执行主类之前暂停,直到调试器/代理连接到端口 8000。

所以也许可以使用包装器脚本使用这些参数在后台启动程序,睡眠一秒钟左右,使用 pbind 将 java 进程的处理器数量设置为 1,然后将一些代理附加到端口或将其分离8000(这足以让 Java 继续执行)。

这个想法的缺陷或潜在的问题是,在调试模式下运行是否会显着影响应用程序的性能(通常似乎不会产生很大的影响),您是否可以从命令行,以及您是否能够在计算机上打开端口。 这不是我之前尝试过自动化的东西(尽管我在释放 Java 进程之前以手动方式使用了大致相似的东西来提高 Java 进程的良好性),所以可能还有其他我忽略的问题。

This isn't a complete solution, but might be enough to develop into one. There's definitely a point at which the java process exists (and thus can be controlled by pbind) and at which point it hasn't yet run the code to do the processor check. If you could pause the launch of the application itself until pbind had done its work, this should be OK (assuming that the pbind idea will work from the CPU-checking point of view).

One way to do this that should definitely pause the JVM at an appropriate place is the socket attach for remote debuggers and starting with suspend mode. If you pass the following arguments to the java invocation:

-Xdebug -Xrunjdwp:transport=dt_socket,address=8000,suspend=y,server=y

then the JVM will pause after starting the java process but before executing the main class, until a debugger/agent is attached to port 8000.

So perhaps it would be possible to use a wrapper script to start the program in the background with these parameters, sleep for a second or so, use pbind to set the number of processors to one for the java process, then attach and detach some agent to port 8000 (which will be enough to get Java to proceed with execution).

Flaws or potential hiccoughs in this idea would be whether running in debug mode would notably affect performance of your app (it doesn't seem to have a big impact in general), whether you can control some kind of no-op JDWP agent from the command line, and whether you're able to open ports on the machine. It's not something I've tried to automate before (though I've used something broadly similar in a manual way to increase the niceness of a Java process before letting it loose), so there might be other issues I've overlooked.

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