Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

[インデックス 11125] ファイルの概要

このコミットは、Go言語の公式ドキュメントであるeffective_go.htmlおよびeffective_go.tmplに、runtime.NumCPU()関数の参照を追加するものです。これにより、Goプログラムが利用可能な論理CPUの数を取得し、runtime.GOMAXPROCS()に設定することで、並列処理を最適化するための推奨事項が明確化されました。また、runtimeパッケージ内の関連するコメントも更新されています。

コミット

commit a03c519a8cf38014220385099460061b045ffae0
Author: Dmitriy Vyukov <dvyukov@google.com>
Date:   Thu Jan 12 22:06:50 2012 +0400

    effective_go: provide reference to runtime.NumCPU()
    
    R=golang-dev, robert.hencke, r
    CC=golang-dev
    https://golang.org/cl/5538050
---
 doc/effective_go.html     | 4 +++-
 doc/effective_go.tmpl     | 4 +++-\n src/pkg/runtime/debug.go  | 1 +\n src/pkg/runtime/extern.go | 2 +-\n 4 files changed, 8 insertions(+), 3 deletions(-)

diff --git a/doc/effective_go.html b/doc/effective_go.html
index fdf8aa101d..3c16e10c3a 100644
--- a/doc/effective_go.html
+++ b/doc/effective_go.html
@@ -2623,8 +2623,10 @@ is if you want CPU parallelism you must tell the run-time
 how many goroutines you want executing code simultaneously.  There
 are two related ways to do this.  Either run your job with environment
 variable <code>GOMAXPROCS</code> set to the number of cores to use
-(default 1); or import the <code>runtime</code> package and call
+or import the <code>runtime</code> package and call
 <code>runtime.GOMAXPROCS(NCPU)</code>.\n+A helpful value might be <code>runtime.NumCPU()</code>, which reports the number\n+of logical CPUs on the local machine.\n Again, this requirement is expected to be retired as the scheduling and run-time improve.\n </p>\n \ndiff --git a/doc/effective_go.tmpl b/doc/effective_go.tmpl
index e3d311eea2..af1bc1ea43 100644
--- a/doc/effective_go.tmpl
+++ b/doc/effective_go.tmpl
@@ -2560,8 +2560,10 @@ is if you want CPU parallelism you must tell the run-time
 how many goroutines you want executing code simultaneously.  There
 are two related ways to do this.  Either run your job with environment
 variable <code>GOMAXPROCS</code> set to the number of cores to use
-(default 1); or import the <code>runtime</code> package and call
+or import the <code>runtime</code> package and call
 <code>runtime.GOMAXPROCS(NCPU)</code>.\n+A helpful value might be <code>runtime.NumCPU()</code>, which reports the number\n+of logical CPUs on the local machine.\n Again, this requirement is expected to be retired as the scheduling and run-time improve.\n </p>\n \ndiff --git a/src/pkg/runtime/debug.go b/src/pkg/runtime/debug.go
index 124370384c..c2b90566a9 100644
--- a/src/pkg/runtime/debug.go
+++ b/src/pkg/runtime/debug.go
@@ -19,6 +19,7 @@ func UnlockOSThread()\n // GOMAXPROCS sets the maximum number of CPUs that can be executing\n // simultaneously and returns the previous setting.  If n < 1, it does not\n // change the current setting.\n+// The number of logical CPUs on the local machine can be queried with NumCPU.\n // This call will go away when the scheduler improves.\n func GOMAXPROCS(n int) int\n \ndiff --git a/src/pkg/runtime/extern.go b/src/pkg/runtime/extern.go
index 1860c5b896..25c7470aab 100644
--- a/src/pkg/runtime/extern.go
+++ b/src/pkg/runtime/extern.go
@@ -68,7 +68,7 @@ func funcline_go(*Func, uintptr) (string, int)\n // mid returns the current os thread (m) id.\n func mid() uint32\n \n-// NumCPU returns the number of CPUs on the local machine.\n+// NumCPU returns the number of logical CPUs on the local machine.\n func NumCPU() int\n \n // Semacquire waits until *s > 0 and then atomically decrements it.\n```

## GitHub上でのコミットページへのリンク

[https://github.com/golang/go/commit/a03c519a8cf38014220385099460061b045ffae0](https://github.com/golang/go/commit/a03c519a8cf38014220385099460061b045ffae0)

## 元コミット内容

effective_go: provide reference to runtime.NumCPU()

R=golang-dev, robert.hencke, r
CC=golang-dev
https://golang.org/cl/5538050

## 変更の背景

このコミットが行われた2012年1月時点のGo言語(Go 1.0リリース前後の時期)では、Goランタイムのスケジューラは現在ほど洗練されていませんでした。特に、Goプログラムが複数のCPUコアを効率的に利用して真の並列処理を行うためには、開発者が明示的に`GOMAXPROCS`環境変数を設定するか、プログラム内で`runtime.GOMAXPROCS()`関数を呼び出す必要がありました。当時の`GOMAXPROCS`のデフォルト値は`1`であり、これはたとえマルチコアCPUを搭載したマシンであっても、Goプログラムが同時に実行できるOSスレッドの数を1つに制限することを意味していました。

このような状況下で、開発者が自身のプログラムが利用可能なCPUリソースを最大限に活用できるように、`GOMAXPROCS`に設定すべき適切な値を見つける手助けが必要でした。`runtime.NumCPU()`関数は、実行環境の論理CPU数を報告するため、この値は`GOMAXPROCS`に設定するのに非常に有用な情報となります。

このコミットは、`effective_go`ドキュメント(Go言語を効果的に書くためのガイドライン)に`runtime.NumCPU()`への参照を追加することで、開発者がより簡単に並列処理を最適化できるように情報を提供することを目的としています。また、`runtime`パッケージ内の関連するコメントも更新し、ドキュメントの一貫性と正確性を向上させています。コミットメッセージにある「この要件は、スケジューリングとランタイムが改善されるにつれて廃止される予定です」という記述は、将来的にGoランタイムが自動的に最適な`GOMAXPROCS`値を設定するようになるという、当時の開発チームの展望を示しています。実際に、Go 1.5からは`GOMAXPROCS`のデフォルト値が論理CPU数に変更され、この手動設定の必要性は大幅に減少しました。

## 前提知識の解説

### Goの並列処理と並行処理

Go言語は、ゴルーチン(goroutine)とチャネル(channel)を用いた並行処理(concurrency)を強力にサポートしています。並行処理とは、複数のタスクが同時に進行しているように見えることを指しますが、必ずしも同時に実行されているわけではありません。一方、並列処理(parallelism)とは、複数のタスクが実際に同時に実行されることを指し、これはマルチコアCPUのような複数のプロセッサリソースを必要とします。

### `GOMAXPROCS`

`GOMAXPROCS`は、Goランタイムが同時に実行できるOSスレッドの最大数を制御する環境変数、または`runtime`パッケージの関数です。この値は、GoスケジューラがゴルーチンをOSスレッドにマッピングする際に利用するプロセッサ(P)の数を決定します。

*   **Go 1.5以前**: `GOMAXPROCS`のデフォルト値は`1`でした。これは、たとえ複数のCPUコアを持つシステムであっても、Goプログラムはデフォルトでは1つのOSスレッドしか使用せず、真の並列処理が行われないことを意味しました。そのため、マルチコアの恩恵を受けるには、開発者が明示的に`GOMAXPROCS`を`1`より大きな値に設定する必要がありました。
*   **Go 1.5以降**: `GOMAXPROCS`のデフォルト値は、システムが持つ論理CPUの数に変更されました。これにより、ほとんどのGoプログラムは明示的な設定なしにマルチコアCPUを最大限に活用できるようになりました。

### `runtime.NumCPU()`

`runtime.NumCPU()`関数は、現在のプロセスが利用可能な論理CPUの数を返します。この値は、`GOMAXPROCS`に設定するのに適した候補となります。例えば、`runtime.GOMAXPROCS(runtime.NumCPU())`とすることで、プログラムが実行されているマシンの全CPUコアを利用するように設定できます。

### Goスケジューラ

Goスケジューラは、ゴルーチンをOSスレッドに効率的に割り当て、実行を管理するGoランタイムの重要なコンポーネントです。初期のGoスケジューラは、単一のグローバルな実行キューを使用しており、マルチコア環境でのボトルネックとなることがありました。Go 1.1でワークスティーリング(work-stealing)を伴うM:Nスケジューラが導入され、Go 1.5で`GOMAXPROCS`のデフォルト値が変更されるなど、継続的に改善されてきました。

## 技術的詳細

このコミットは、主にGo言語のドキュメントとランタイムのコメントを更新することで、`GOMAXPROCS`と`runtime.NumCPU()`の関連性を明確にし、開発者へのガイダンスを強化しています。

1.  **`doc/effective_go.html` および `doc/effective_go.tmpl` の更新**:
    *   これらのファイルは、Go言語の公式ドキュメント「Effective Go」のHTML版とテンプレート版です。
    *   `GOMAXPROCS`に関する説明箇所に、`runtime.NumCPU()`が「論理CPUの数を報告する有用な値」として追加されました。これにより、開発者は`GOMAXPROCS`に設定すべき値の具体的な例として`runtime.NumCPU()`を利用できることが明示されました。
    *   変更前は`GOMAXPROCS`のデフォルト値が`1`であることが明記されていましたが、このコミットではその記述が削除され、代わりに`runtime.NumCPU()`の推奨が追加されています。これは、`GOMAXPROCS`のデフォルト値が将来的に変更されることを見越した、より汎用的な表現への移行を示唆しています。

2.  **`src/pkg/runtime/debug.go` の更新**:
    *   `runtime.GOMAXPROCS`関数のコメントに、「ローカルマシンの論理CPU数は`NumCPU`で問い合わせることができます」という記述が追加されました。これは、`GOMAXPROCS`関数の利用者が、適切な引数として`NumCPU`の戻り値を使用できることを示唆しています。

3.  **`src/pkg/runtime/extern.go` の更新**:
    *   `runtime.NumCPU`関数のコメントが、「ローカルマシンのCPU数を返します」から「ローカルマシンの**論理**CPU数を返します」に修正されました。これは、`NumCPU`が物理コア数だけでなく、ハイパースレッディングなどによって提供される論理的なプロセッサ数を報告することをより正確に表現するための変更です。

これらの変更は、Go言語の並列処理に関するドキュメントの正確性と実用性を向上させ、当時のGoランタイムの制約下で開発者がより高性能なアプリケーションを構築するための重要な情報を提供しました。

## コアとなるコードの変更箇所

### `doc/effective_go.html`
```diff
--- a/doc/effective_go.html
+++ b/doc/effective_go.html
@@ -2623,8 +2623,10 @@ is if you want CPU parallelism you must tell the run-time
 how many goroutines you want executing code simultaneously.  There
 are two related ways to do this.  Either run your job with environment
 variable <code>GOMAXPROCS</code> set to the number of cores to use
-(default 1); or import the <code>runtime</code> package and call
+or import the <code>runtime</code> package and call
 <code>runtime.GOMAXPROCS(NCPU)</code>.\n+A helpful value might be <code>runtime.NumCPU()</code>, which reports the number\n+of logical CPUs on the local machine.\n Again, this requirement is expected to be retired as the scheduling and run-time improve.\n </p>\n 

doc/effective_go.tmpl

--- a/doc/effective_go.tmpl
+++ b/doc/effective_go.tmpl
@@ -2560,8 +2560,10 @@ is if you want CPU parallelism you must tell the run-time
 how many goroutines you want executing code simultaneously.  There
 are two related ways to do this.  Either run your job with environment
 variable <code>GOMAXPROCS</code> set to the number of cores to use
-(default 1); or import the <code>runtime</code> package and call
+or import the <code>runtime</code> package and call
 <code>runtime.GOMAXPROCS(NCPU)</code>.\n+A helpful value might be <code>runtime.NumCPU()</code>, which reports the number\n+of logical CPUs on the local machine.\n Again, this requirement is expected to be retired as the scheduling and run-time improve.\n </p>\n 

src/pkg/runtime/debug.go

--- a/src/pkg/runtime/debug.go
+++ b/src/pkg/runtime/debug.go
@@ -19,6 +19,7 @@ func UnlockOSThread()\n // GOMAXPROCS sets the maximum number of CPUs that can be executing\n // simultaneously and returns the previous setting.  If n < 1, it does not\n // change the current setting.\n+// The number of logical CPUs on the local machine can be queried with NumCPU.\n // This call will go away when the scheduler improves.\n func GOMAXPROCS(n int) int\n 

src/pkg/runtime/extern.go

--- a/src/pkg/runtime/extern.go
+++ b/src/pkg/runtime/extern.go
@@ -68,7 +68,7 @@ func funcline_go(*Func, uintptr) (string, int)\n // mid returns the current os thread (m) id.\n func mid() uint32\n \n-// NumCPU returns the number of CPUs on the local machine.\n+// NumCPU returns the number of logical CPUs on the local machine.\n func NumCPU() int\n 

コアとなるコードの解説

  • doc/effective_go.html および doc/effective_go.tmpl:

    • - (default 1); の削除: 以前はGOMAXPROCSのデフォルト値が1であることが明記されていましたが、この記述が削除されました。これは、将来的にGOMAXPROCSのデフォルト値が変更される可能性を考慮し、ドキュメントをより普遍的なものにするための変更です。
    • +A helpful value might be <code>runtime.NumCPU()</code>, which reports the number\n+of logical CPUs on the local machine. の追加: runtime.GOMAXPROCS(NCPU)の呼び出しに続いて、runtime.NumCPU()が論理CPU数を報告し、GOMAXPROCSに設定するのに役立つ値であることが明確に示されました。これにより、開発者は並列処理を最適化するための具体的なヒントを得ることができます。
  • src/pkg/runtime/debug.go:

    • // The number of logical CPUs on the local machine can be queried with NumCPU. の追加: GOMAXPROCS関数のドキュメンテーションコメントに、runtime.NumCPU()を使用して論理CPU数を取得できるという情報が追加されました。これは、GOMAXPROCSの引数としてruntime.NumCPU()の戻り値を使用することを推奨するものです。
  • src/pkg/runtime/extern.go:

    • - // NumCPU returns the number of CPUs on the local machine. から + // NumCPU returns the number of logical CPUs on the local machine. への変更: NumCPU関数のコメントが「CPUの数」から「論理CPUの数」に修正されました。これは、NumCPUが物理コアだけでなく、ハイパースレッディングなどによって提供される論理的なプロセッサ数を報告するという関数の正確な振る舞いを反映しています。

これらの変更は、Go言語の並列処理に関するドキュメントとランタイムのコメントをより正確で、開発者にとって有用なものにするためのものです。特に、Go 1.5でGOMAXPROCSのデフォルト値が変更される前の時期において、開発者がマルチコア環境でGoプログラムの性能を最大限に引き出すための重要なガイダンスを提供しました。

関連リンク

参考にした情報源リンク