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

发布于 2024-12-08 12:51:46 字数 822 浏览 2 评论 0原文

我有一个程序可以将系统的事件日志加载到 Hastable 中以供使用。问题是它使用了 100% 的 CPU。使用 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;
        }
    }
}

I have a program that loads System's Event logs to a Hastable to be used. The problem is that it's utilizing 100% of the CPU. What is the best solution to lower the usage using API calls.

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;
        }
    }
}

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

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

发布评论

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

评论(4

蓝天 2024-12-15 12:51:46

看来,在继续更改代码之前,明智的做法是执行一些性能测试以了解瓶颈所在。

我几个月前提出的一个问题讨论了可用于衡量应用程序性能的方法,以及在此过程中可以考虑的一些注意事项:

函数分析问题 - Visual Studio 2010 Ultimate

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

虫児飞 2024-12-15 12:51:46

通常,100% CPU 使用率1并没有什么问题。这意味着处理器在操作期间以最大速度运行。这是一件好事。为什么要浪费时间强迫CPU停止工作呢?该操作系统很可能是一个抢占式操作系统(因为您已经标记了 c#,所以它很可能是 Windows),因此 Polynomial 关于稳定性的评论是不正确的,无论负载如何,操作系统都会切换任务。

如果将 CPU 使用率减少到 50%,则处理时间将增加两倍。

你想问的是“我怎样才能减少这个算法所花费的时间”/“是否有更有效的算法”,因为你使用的任何算法都会以 100% 的速度运行直到完成。

如果您解释一下您要在这里做什么,将会有所帮助,也许有更好的方法来做到这一点。

查看代码,我认为最大的开销是在 GetEventKey 方法中,向我们展示代码可能会有所帮助。

  1. GUI 线程中 100% CPU 使用率通常是一件坏事,因为 GUI 线程必须等到消耗 CPU 的方法完成才能处理用户交互,因此 GUI 可能变得不可用。

Usually, there is nothing wrong with 100% CPU use1. 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? 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.

If you reduce the CPU use to 50% then the processing will take twice as long.

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.

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.

  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.
幸福丶如此 2024-12-15 12:51:46

您可以在单独的低优先级线程中加载日志(Thread.Priority):

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

编辑:
通过将执行委托给单独的线程,您可以减少平均 CPU 负载,但总执行时间将会增加。

You can load logs in a separate thread with low 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.

黑色毁心梦 2024-12-15 12:51:46

高 CPU 使用率可能是由于迭代器必须遍历整个事件日志以检查是否有任何新事件可用。尝试切换 foreach 并使用 for 代替。您还可以尝试使用 Visual Studio 中的性能分析工具来查看哪些线路是 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. Try switching the foreach out and using a for instead. You can also try using the performance analysis tools in Visual Studio to see which line(s) are CPU hotspots.

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