简体   繁体   English

Java应用程序的多核CPU利用率

[英]Multi-core CPU utilization by Java application

I have a program that sorts big files by splitting them into chunks, sort chunks and merge them into final sorted file. 我有一个程序,可通过将大文件拆分为大块,排序大块并将它们合并为最终排序的文件来对大文件进行排序。 Application runs one thread for loading/saving data from/to file - only one thread does I/O operations. 应用程序运行一个线程来从文件加载数据/将数据保存到文件-只有一个线程进行I / O操作。 Also there are two more threads that receive chunk data, sort it and then send sorted data back to thread that does I/O. 另外,还有两个线程接收块数据,对它们进行排序,然后将排序后的数据发送回执行I / O的线程。

So in general there are 4 threads running - main thread, thread that loads/saves data and two threads that sort data. 因此,通常有4个线程在运行-主线程,加载/保存数据的线程和两个对数据进行排序的线程。

I thought during execution i will see 1 sleeping thread (main) that doesn't take any CPU time and 3 active threads that utilize 1 CPU core each. 我以为在执行过程中,我将看到1个不占用任何CPU时间的睡眠线程(主线程)和3个利用1个CPU内核的活动线程。

When i run this program on dual 6 core processor machine with hyper threading (24 CPUs) i see that ALL 24 CPU's are loaded for 100%! 当我在具有超线程(24个CPU)的双6核处理器机器上运行该程序时,我看到所有24个CPU的负载均为100%!

Initially i thought that sort algorithm is mutithreaded, but after looking into java sources i found that it's not. 最初,我认为排序算法是多线程的,但是在研究了Java源代码之后,我发现并不是这样。

I'm using simple Collections.sort(LinkedList) to sort the data... 我正在使用简单的Collections.sort(LinkedList)对数据进行排序...

here are some details: 以下是一些详细信息:

# java -version
java version "1.6.0_26"
Java(TM) SE Runtime Environment (build 1.6.0_26-b03)
Java HotSpot(TM) 64-Bit Server VM (build 20.1-b02, mixed mode)

# uname -a
Linux 2.6.32-28-server #55-Ubuntu SMP Mon Jan 10 23:57:16 UTC 2011 x86_64 GNU/Linux

I was using nmon to monitor processor loading. 我正在使用nmon监视处理器负载。

I would appreciate any explanation of this case and any advise on how to control CPU loading as i this particular task doesn't leave CPU time for other applications 我将不胜感激这种情况下的任何解释,以及有关如何控制CPU负载的任何建议,因为在此特定任务下,其他应用程序不会占用CPU时间

[UPDATE] I used jvisualvm to count threads - it shows only threads i know about. [更新]我使用jvisualvm来计数线程-它仅显示我知道的线程。 Also i made a simple test program (see below) that runs only one main thread and got exactly the same results - all 24 processors are busy almost for 100% during code execution 我还制作了一个简单的测试程序(请参阅下文),该程序仅运行一个主线程并获得完全相同的结果-所有24个处理器在代码执行期间几乎忙于100%

public class Test {

    public void run(){
        Random r = new Random();
        int len = r.nextInt(10) + 5000000;
        LinkedList<String> list = new LinkedList<String>();
        for (int i=0; i<len; i++){
                list.add(new String("test" + r.nextInt(50000000)));
        }
        System.out.println("Inserted " + list.size() + " items");
        list.clear();
    }

    public static void main(String[] argv){
        Test t = new Test();
        t.run();
        System.out.println("Done");
    }
}

[UPDATE] [UPDATE]
Here is the screenshot i made while running the program above (used nmon): http://imageshack.us/photo/my-images/716/cpuload.png/ 这是我在运行上述程序(使用nmon)时制作的屏幕截图: http : //imageshack.us/photo/my-images/716/cpuload.png/

I would suggest, that this is rather a nmon than a java question and to solve it, I would take a peek at the top command which provides info about cpu-usage per process. 我建议,这不是一个Java的问题,而是nmon的问题,为了解决这个问题,我将看一下top命令,该命令提供有关每个进程的cpu使用情况的信息。 I predict the following result: You will see one java thread using near 100% cpu-time (which is ok, as per-process percentage in top is relative to one (virtual) core), maybe a second and third java thread with much less cpu-usage (the I/O threads). 我预测以下结果:您将看到一个Java线程使用了将近100%的CPU时间(没关系,因为每个进程的顶部相对于一个(虚拟)内核而言),也许是第二个和第三个Java线程使用了很多更少的cpu使用量(I / O线程)。 Depending on the choice of the gc you might even spot one or more gc-Threads, however much less than 20. 根据gc的选择,您甚至可以发现一个或多个gc-Threads,但少于20个。

HotSpot however will not (and even cannot to my knowledge) parallelize a sequential task on its own. 但是,HotSpot不会(甚至据我所知)不能并行执行顺序任务。

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

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