简体   繁体   English

加载事件日志时如何降低CPU利用率?

[英]How to reduce CPU utilization while loading Event logs?

I have a program that loads System's Event logs to a Hastable to be used. 我有一个程序可以将系统的事件日志加载到要使用的Hastable中。 The problem is that it's utilizing 100% of the CPU. 问题在于它占用了100%的CPU。 What is the best solution to lower the usage using API calls. 使用API​​调用降低使用率的最佳解决方案是什么?

Hashtable currentLog = (Hashtable)_logs[l.Log];

foreach (EventLogEntry e in l.Entries)
{    
    if (_lastRun <= e.TimeWritten.ToUniversalTime() )
    {                                
        if (_verboseOutput)
        {
            Logger.TraceWrite(String.Format(
                                    "Source={0}, EventId={1}, Date/Time={2}, Message={3}", 
                                     e.Source, 
                                     e.EventID, 
                                     e.TimeWritten, 
                                     e.Message));
        }

        string key = GetEventKey(e);
        if (currentLog[key] == null)
        {
            currentLog[key] = e;
        }
    }
}

You can load logs in a separate thread with low priority ( Thread.Priority ): 您可以在低优先级( Thread.Priority )的单独线程中加载日志:

Thread thread = new Thread(loadLogsMethod);
thread.Priority = ThreadPriority.BelowNormal;
thread.Start();

EDIT: By delegating execution to a separate thread you decrease average CPU load but total execution time will be increased. 编辑:通过将执行委派给一个单独的线程,可以减少平均CPU负载,但总执行时间将增加。

It seems that before you go ahead and change your code it would be wise to perform some performance testing to see where the bottlenecks lie. 看来,在继续进行代码更改之前,最好进行一些性能测试以查看瓶颈所在。

A question I asked a few months ago talks about the methods you can use to measure the performance of your application, as well as some of the considerations that you can make whilst doing so: 我几个月前问的一个问题,讨论了可以用来衡量应用程序性能的方法,以及在进行此操作时可以考虑的一些注意事项:

Function profiling woes - Visual Studio 2010 Ultimate 函数剖析问题-Visual Studio 2010 Ultimate

Usually, there is nothing wrong with 100% CPU use 1 . 通常,100%CPU使用率1没什么错。 All that means is that the processor is running at maximum for the duration of the operation. 这意味着处理器在整个操作过程中都在最大程度地运行。 This is a good thing. 这是一件好事。 Why waste time forcing the CPU to stop working? 为什么浪费时间强迫CPU停止工作? The OS is most likely a pre-emptive OS (since you've tagged c# it's likely to be Windows), so Polynomial's comment about stability is incorrect, the OS will switch tasks regardless of the load. 该操作系统很可能是抢占式操作系统(由于您已将c#标记为Windows),因此Polynomial关于稳定性的评论不正确,无论负载如何,该操作系统都会切换任务。

If you reduce the CPU use to 50% then the processing will take twice as long. 如果将CPU使用率降低到50%,则处理将花费两倍的时间。

What you want to ask is "How can I reduce the time this algorithm takes" / "Is there a more efficient algorithm" since any algorithm you use will run at 100% until it completes. 您想问的是“如何减少该算法花费的时间” /“有没有更有效的算法”,因为您使用的任何算法都将以100%的速度运行直到完成。

It would help if you explain what you're trying to do here, perhaps there's a better way to do it. 如果您在此解释您要做什么,这将有所帮助,也许有更好的方法可以做到。

Looking at the code, I think the biggest overhead is in the GetEventKey method, showing us that code may help. 查看代码,我认为最大的开销是在GetEventKey方法中,这向我们显示了代码可能会有所帮助。

  1. 100% CPU usage in a GUI thread is generally a bad thing as the GUI could become unusable as the GUI thread has to wait until the method that's consuming the CPU finishes before handling user interaction. GUI线程中100%的CPU使用率通常是一件坏事,因为GUI可能变得无法使用,因为GUI线程必须等待消耗CPU的方法完成后才能处理用户交互。

The high CPU usage is probably due to the iterator having to walk the entire event log to check if there any new ones available. CPU使用率高可能是由于迭代器必须遍历整个事件日志以检查是否有可用的新事件。 Try switching the foreach out and using a for instead. 尝试切换foreach并使用for You can also try using the performance analysis tools in Visual Studio to see which line(s) are CPU hotspots. 您也可以尝试使用Visual Studio中的性能分析工具来查看哪些行是CPU热点。

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

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