Linuxリアルタイム組み込みアプリのリソース使用量を監視する方法

[English Below]

ロボットやドローンなどの組み込みデバイス用のリアルタイムアプリケーションを構築するときはプログラムが使用しているシステムリソースに注意を払う必要があります。パフォーマンスを最適化し、リソース不足による障害を防ぐためです。この記事では、システムリソースの監視方法について解説します。

f:id:PSYGIG:20190703225014j:plain

目次

  • 監視が必要なシステムリソース
  • どのリソースメトリックを監視すればいいのか
  • どのようにリソースメトリックを監視すればいいのか
  • 無料便利ツール「psymon」とは?
  • psymonの無料ダウンロード・インストールはこちら
  • psymonでリソース使用量のログを残す方法
  • psymonを使用する上での詳細ガイド

監視が必要なシステムリソース

組み込みデバイス用のリアルタイムアプリケーションにおいて監視が必要なシステムリソースの例としては下記が挙げられます。

  • CPU
  • GPU
  • 記憶
  • ディスク
  • ネットワーク

上記の1つまたは複数のリソースの過剰使用は、システム障害の一因となりえます。たとえば、CPU使用率が長期間にわたって常に100%のアプリケーションであれば、ドローンのバッテリーが急速に消耗しやすくなります。最悪の場合、物理的な損傷やクラッシュなど、より壊滅的な事態につながる可能性もあります。

どのリソースメトリックを監視すればいいのか

具体的にどのリソースメトリクスを監視すればいいのでしょうか。CPU使用率や物理メモリ使用率、ディスク負荷など、一般的なリソースのメトリクス以外にも、監視対象のメトリクスは無数にあります。監視したいメトリクスの代表例は下記の通り。

CPU

  • すべてのCPUコアにおけるコンテキストスイッチの数
  • 全体CPU使用率のうち、アイドリングのために費やした割合
  • 全体CPU使用率のうち、I/Oの完了待ちのために費やした割合
  • 全体CPU時間のうち、カーネルモードのために費やした時間の割合

ディスク

  • 現在進行中のI/Oの数
  • システムがI/Oの実行に費やした時間(ミリ秒)
  • 読み取りに費やした時間(ミリ秒)
  • 書き込みに要した時間(ミリ秒)

メモリ

  • ディスクに書き戻されるのを待っているメモリ
  • 使用可能なメモリ(RAM)
  • 現在未使用のスワップスペースの量

ネットワーク

  • 受信したバイト数
  • 送信したバイト数
  • 受信中にドロップされたパケット数
  • 送信中にドロップされたパケット数
  • TCP_ESTABLISHED状態の接続数

構築するアプリケーションの目的によっては、他のメトリクスの方が役に立つ場合もあります。たとえば、データベースアプリケーションの場合、クエリのパフォーマンスに直接影響するページフォールト数を追跡​​するとよいでしょう。Webサーバーを実行している場合は、CPUスレッド数と帯域幅使用率に着目することで、サーバーが最大容量に達しているかどうかを確認することができます。

どのようにリソースメトリックを監視すればいいのか

多くのLinuxディストリビューションには、リソースメトリックを収集して表示するための組み込み機能があります。これには、メトリックデータへの階層ファイルインタフェースを提供するprocfs疑似ファイルシステムが含まれます。 top、netstat、lotopなどの組み込みLinuxコマンドもあります。これらはシステムレベルの監視やシェル環境にアクセスできる場合、フォーマットされたデータを解析するためのコードがある場合にはうまく機能します。

組み込み環境では、コードを記述せずともフォーマットされたデータを解析し、プロセスが実行されている時間のみリソースメトリックを監視してくれるような簡易なソリューションさえあればいいこともあるでしょう。多くの場合はプロセスが実行されていない間のメトリックデータは監視不要だからです。そのようなときに役立つツールがpsymonです。

無料便利ツール「psymon」とは?

psymonはUNIXベースのコマンドラインプログラムで、コマンドラインで実行できるプログラムのシステムリソースメトリックを自動的に収集して記録します。たとえば、tarでディレクトリを圧縮するときに使用されるリソース量を監視したい場合は、次のコマンドを実行します。

psymon --metrics tar -cvf demo-app.tar.gz demo-app

psymon --metricsから始まるコマンドを実行すると、プログラムの実行期間中、メトリックを定期的に収集して記録します。

psymonの無料ダウンロード・インストールはこちら

psymonの無料最新版は下記のリンクからダウンロードできます。 https://bintray.com/psygig/releases/psyiage 詳しいインストール方法は上記リンク内の「Readme」をご覧ください。

psymonでリソース使用量のログを残す方法

組み込みアプリケーションで使用されるリソースメトリックのロギングは簡単です。

psymon --metrics [command-to-run-your-program]

デフォルトでは、psymonはリソースメトリクスをJSONファイルに記録します。しかし、psymonはHTTPエンドポイント、MQTTトピック、CSVファイルなどの他の送信先およびフォーマットにログを記録するよう設定できます。これにより、組み込みアプリケーションのリモートモニタリングが可能になります。

psymon --metrics -o csv [command-to-run-your-program]

CSVファイルでデータを収集した場合のサンプルはこちら f:id:PSYGIG:20190703223503p:plain

psymonを使用する上での詳細ガイド

リソースの監視以外にも、psymonには下記の便利機能があります。

  • コードプロファイリング
  • クラッシュレポート
  • ログ監視
  • システム情報収集
  • アラート報告

上記で紹介した機能に加えて、psymonは組み込みアプリケーションの品質と安定性を向上するための便利機能を備えています。詳しい機能内容については、psymon公式ページ(https://bintray.com/psygig/releases/psyiage#)をご覧ください。


この記事を書いた人|キース
アイスホッケーとお笑いをこよなく愛するみんなのムードメーカー、キース。サイギグの共同創業者兼チーフエンジニア。カナダ・バンクーバー出身。カナダの名門ブリティッシュコロンビア大学卒。SonyPlayStationのハード/ソフト開発をリードしてきた組み込みエンジニア。カナダ出身なだけあって、平日の夜はアイスホッケーを楽しむスポーツマン。一時期ブルゾンちえみにハマっていた。プライベートは謎に包まれていることが多いため、最近の生態(マイブーム)は誰も知らない。

[English Below]

Monitor resource usage of your real-time, embedded applications in Linux

When we build real-time applications that run on embedded devices such as robotics and drones, we need to be aware of the system resources that our program is using, in order to optimize performance and prevent failures caused by resource starvation. Such system resources include but not limited to the following:

  • CPU
  • GPU
  • Memory
  • Disk
  • Network

Excessive usage of one or more of such resource can be an indicator of potential system failure. For example, if our application is constantly at 100% CPU usage over a long period of time, it will likely drain the battery fairly quickly on a drone. At worst, it may lead to more catastrophic results such as physical damage and crashes.

Which resource metrics should we monitor?

Other than typical resource metrics such as CPU usage, physical memory usage, and disk load, there are virtually countless number of other metrics that can be monitored. Here is a small subset of such metrics

CPU

  • Number of context switches across all CPU cores.
  • Percentage of total CPU spent idling.
  • Percentage of total CPU spent waiting for I/O to complete.
  • Percentage of total CPU time spent in kernel mode.

Disk

  • Number of I/O operations currently in progress.
  • Number of milliseconds the system spent doing I/O operation.
  • Amount of time in milliseconds spent reading.
  • Amount of time in milliseconds spent writing.

Memory

  • Memory which is waiting to get written back to the disk.
  • Total free RAM.
  • Amount of swap space that is currently unused.

Network

  • Number of bytes received.
  • Number of bytes sent.
  • Number of packets dropped while receiving.
  • Number of packets dropped while sending.
  • Number of connections in state TCP_ESTABLISHED

Depending on our application goals, certain metrics may be more useful to us.

For example, it would be useful to track the number of page faults for a database application, as it directly affects the performance of our queries.

If we are running a web server, we might be interested in the number of CPU threads and bandwidth utilization, which gives an indication of whether the server is at capacity or not.

How do we monitor resource metrics?

Many Linux distributions have built-in facilities for collecting and displaying resource metrics. This includes the procfs pseudo file system which provides a hierarchical file interface to metric data. There are also built-in Linux commands such as top, netstat, lotop. They work well for system-level monitoring and if we have access to a shell environment or have code to parse the formatted data.

For embedded environments, we may want to look at a more lightweight solution that doesn’t require us to write code for parsing formatted data, and monitors resource metrics only during the lifetime of a process. In many cases, we can ignore metric data when the process is not running. There is one such tool called psymon that serves such purpose.

What is psymon?

psymon is a UNIX-based command line program that automatically collects and logs system resource metrics on any program we can run on the command line. For example, if we want to monitor how much resource is used when compressing a directory via tar, we may run the following command

psymon --metrics tar -cvf demo-app.tar.gz demo-app

Running our command prefixed with psymon --metrics will periodically collect and log metrics for the duration of the program

Downloading and installing psymon

psymon is a free tool that you can get the latest version here.

https://bintray.com/psygig/releases/psyiage

Follow the instructions in the Readme to install.

Using psymon to log resource usage

Logging resource metrics used by our embedded applications is simple, as all we need to do is prepend the command with psymon --metrics

psymon --metrics [command-to-run-your-program]

By default, psymon will log resource metrics to a JSON file. However, psymon can be configured to log to other destinations and formats such as HTTP endpoints, MQTT topics, CSV file, etc. This enables the possibility of remote monitoring of our embedded applications.

psymon --metrics -o csv [command-to-run-your-program]

Here is a sample CSV file of the collected data f:id:PSYGIG:20190703223503p:plain

Advanced instrumentation using psymon

Other than resource monitoring, psymon includes other instrumentation features such as:

  • Code profiling
  • Crash reporting
  • Log monitoring
  • System information collection
  • Alert reporting

Take a look at the official psymon documentation page to see if any other instrumentation features would be beneficial to improving the quality and robustness of your embedded applications.