原始文献 / 论坛帖 / Re: bitcoin auto-renice-ing

Re: bitcoin auto-renice-ing

Bitcointalk · 2010-03-15 18:44:12 UTC

AuthorSatoshi Nakamoto
TitleRe: bitcoin auto-renice-ing
Date2010-03-15 18:44:12 UTC
ForumBitcointalk · 原文

它给每个线程设了不同的优先级。生成线程跑在 PRIO_MIN,其他线程几乎不占 CPU,跑在普通优先级。

#define THREAD_PRIORITY_LOWEST PRIO_MIN
#define THREAD_PRIORITY_BELOW_NORMAL 2
#define THREAD_PRIORITY_NORMAL 0

从 Windows 优先级换算过来的值大概来自这样一张表:

"下表展示了 nice 值与 Win32 优先级的映射。关于 Win32 优先级问题的更多信息,请参阅 SetThreadPriority() 的 Win32 文档。

nice 值 Win32 优先级
-20 到 -16 THREAD_PRIORITY_HIGHEST
-15 到 -6 THREAD_PRIORITY_ABOVE_NORMAL
-5 到 +4 THREAD_PRIORITY_NORMAL
+5 到 +14 THREAD_PRIORITY_BELOW_NORMAL
+15 到 +19 THREAD_PRIORITY_LOWEST"

如果你有更好的值,欢迎建议。

另外,网上有建议说 Linux 上用 PRIO_PROCESS 是因为线程就是进程。如果这话不对,那可能就是它把整个应用的优先级都意外改了的原因。

// threads are processes on linux, so PRIO_PROCESS affects just the one thread
setpriority(PRIO_PROCESS, getpid(), nPriority);

英文原文

It sets different priorities for each thread.  The generate threads run at PRIO_MIN.  The other threads rarely take any CPU and run at normal.

#define THREAD_PRIORITY_LOWEST          PRIO_MIN
#define THREAD_PRIORITY_BELOW_NORMAL    2
#define THREAD_PRIORITY_NORMAL          0

The priorities converted from Windows priorities were probably from a table like this:

"The following table shows the mapping between nice values and Win32 priorities. Refer to the Win32 documentation for SetThreadPriority() for more information on Win32 priority issues.

nice value    Win32 Priority
-20 to -16    THREAD_PRIORITY_HIGHEST
-15 to -6    THREAD_PRIORITY_ABOVE_NORMAL
-5 to +4    THREAD_PRIORITY_NORMAL
+5 to +14    THREAD_PRIORITY_BELOW_NORMAL
+15 to +19    THREAD_PRIORITY_LOWEST"

If you have better values, suggestions welcome.

Also, there was some advice on the web that PRIO_PROCESS is used on Linux because threads are processes.  If that's not true, maybe it accounts for unexpectedly setting the priority of the whole app.

// threads are processes on linux, so PRIO_PROCESS affects just the one thread
    setpriority(PRIO_PROCESS, getpid(), nPriority);