本文將說明如何在開發期間直接將元件新增至元件例項樹狀結構,並在執行階段與這些元件互動。
Fuchsia 在如果您使用下列其中一個架構建構元件,請改為參閱對應的指南:
- 工作階段元件: 建立及執行工作階段
- 測試元件: 執行 Fuchsia 測試
概念
執行元件前,請先瞭解下列概念:
- 在父項和子項關係的階層中,將個別元件例項連結在一起。
- 元件執行個體會經歷四個主要生命週期狀態:建立、啟動、停止和刪除。
- <0x0
- 元件例項會靜態宣告為另一個元件的子項,或在元件集合中於執行階段動態建立。每個執行個體都包含
name
和url
元件。 - 「component URL」可識別元件。元件網址通常會解析為套件內的資源,並由元件架構處理。
如要進一步瞭解元件執行作業,請參閱「元件生命週期」。
元件執行個體
如要執行元件,第一步是將新的元件執行個體新增至樹狀結構。元件執行個體在樹狀結構中的位置,決定了其可用的功能。
探索靜態元件
靜態元件會宣告為樹狀結構中另一個元件執行個體的子項。您可以使用 ffx component show
判斷靜態元件執行個體的路徑名稱和元件網址:
ffx component show COMPONENT_NAME
將 COMPONENT_NAME
替換為元件名稱。以下範例顯示 pkg-resolver
元件的指令輸出內容:
$ ffx component show pkg-resolver
Moniker: /core/pkg-resolver
URL: fuchsia-pkg://fuchsia.com/pkg-resolver#meta/pkg-resolver.cm
Type: CML static component
Component State: Resolved
Execution State: Running
...
靜態元件執行個體無法在執行階段建立或毀損。
管理動態元件
動態元件是在集合中建立,您可以使用 ffx component create
建立新的元件執行個體,在現有集合中提供目標路徑名稱,以及用於解析元件的元件網址:
ffx component create TARGET_MONIKER COMPONENT_URL
將 TARGET_MONIKER
換成現有集合內新元件的目的地路徑名稱,並將 COMPONENT_URL
換成元件的服務位置。舉例來說,下列指令會在 ffx-laboratory
集合中建立名為 hello-world
的新元件執行個體:
$ ffx component create /core/ffx-laboratory:hello-world fuchsia-pkg://fuchsia.com/hello-world-rust#meta/hello-world-rust.cm
URL: fuchsia-pkg://fuchsia.com/hello-world-rust#meta/hello-world-rust.cm
Moniker: /core/ffx-laboratory:hello-world
Creating component instance...
同樣地,您也可以提供動態元件執行個體的路徑名稱,使用 ffx component destroy
終止該執行個體:
ffx component destroy TARGET_MONIKER
將 TARGET_MONIKER
替換為要終止的元件路徑名稱。以下範例會銷毀上述建立的 hello-world
元件:
$ ffx component destroy /core/ffx-laboratory:hello-world
Moniker: /core/ffx-laboratory:hello-world
Destroying component instance...
元件執行
樹狀結構中存在元件執行個體後,您可以使用 ffx component
啟動及停止目標例項。
啟動執行個體
使用 ffx component start
明確啟動元件執行個體:
ffx component start TARGET_MONIKER
將 TARGET_MONIKER
替換為要啟動的元件路徑名稱。下列範例會啟動先前建立的 hello-world
元件:
$ ffx component start /core/ffx-laboratory:hello-world
Moniker: /core/ffx-laboratory:hello-world
Starting component instance...
停止執行個體
使用 ffx component stop
終止執行中的元件執行個體,方法是使用其路徑名稱:
ffx component stop TARGET_MONIKER
將 TARGET_MONIKER
替換為要停止的元件路徑名稱。以下範例會停止上述啟動的 hello-world
元件:
$ ffx component stop /core/ffx-laboratory:hello-world
Moniker: /core/ffx-laboratory:hello-world
Stopping component instance...
執行元件
ffx component run
指令提供快速入門導覽,可在開發期間執行基本元件。這是 ffx component create
後接 ffx component start
的快速鍵:
ffx component run TARGET_MONIKER COMPONENT_URL
將 TARGET_MONIKER
換成現有集合內新元件的目的地路徑名稱,並將 COMPONENT_URL
換成元件的服務位置。舉例來說,下列指令會在 ffx-laboratory
集合中建立名為 hello-world-rust
的新元件執行個體:
$ ffx component run /core/ffx-laboratory:hello-world-rust fuchsia-pkg://fuchsia.com/hello-world-rust#meta/hello-world-rust.cm
URL: fuchsia-pkg://fuchsia.com/hello-world-rust#meta/hello-world-rust.cm
Moniker: /core/ffx-laboratory:hello-world-rust
Creating component instance...
Starting component instance...
上述範例相當於執行下列個別的 ffx
指令:
$ ffx component create /core/ffx-laboratory:hello-world-rust fuchsia-pkg://fuchsia.com/hello-world-rust#meta/hello-world-rust.cm
$ ffx component start /core/ffx-laboratory:hello-world-rust
更新元件的方法
變更元件時,通常會想更新裝置上執行的其中一個執行個體。舉例來說,您可以變更元件的二進位檔,然後重新啟動元件,以執行新的二進位檔。或者,您可能會變更資訊清單來新增能力路徑,並希望在裝置上提供這些能力路徑。
ffx component reload
指令是重新載入元件最快速且最完整的方法。但這並非唯一方法,瞭解其他方法或許能讓您在特殊用途中執行更精確的操作。
以下簡要說明這些方法,並提供完整說明。
摘要
指令 | 說明 | 更新 套件 | 更新資訊清單 | 保留資源 |
---|---|---|---|---|
ffx 元件 reload | 停止、更新及啟動 | 是 | 是 | 是 |
ffx 元件 destroy/create/start | destroy, 然後啟動 | 是 | 是 | 否 |
ffx 元件 run --recreate | destroy, 然後啟動 | 是 | 是 | 否 |
ffx 元件 停止/啟動 | stops and starts without destroying | 否 | 否 | 是 |
- 「Updates package」表示在重新載入套件時,程式碼會更新。
- 「更新資訊清單」表示系統會重新載入資訊清單快取,更新 FIDL 檔案中包含的路由和其他資訊。
- 「保留資源」是指保留儲存空間等資源,這些資源會由 destroy 指令釋出。
重新載入
更新元件的程式碼和資訊清單,同時保留資源:
ffx component reload TARGET_MONIKER
這項指令會先關閉元件,然後重新載入並重新啟動。這項指令會更新套件和資訊清單,但不會毀損元件或釋出資源。
reload
指令會保留元件的資源,例如儲存空間。如果初始化、取得或重新建立特定狀態的資源以進行偵錯時速度緩慢,這項保留功能就很有幫助。
如果銷毀元件的成本很高 (例如需要關閉並重新啟動工作階段或目標裝置/模擬器),重新載入的速度也會更快。
Destroy/create/start
如要完整重新載入元件並捨棄取得的資源,可以先終止現有元件執行個體,然後重新啟動。您可以使用下列功能:
$ ffx component destroy TARGET_MONIKER
$ ffx component create TARGET_MONIKER COMPONENT_URL
$ ffx component start TARGET_MONIKER
這個序列會重新載入套件和資訊清單,因此程式碼和能力變更都會更新。不過,銷毀元件也會釋出其使用的所有資源。如果目標是從完全重新初始化的元件開始,您可能需要完整重設。
執行 --recreate
與 destroy/create/start 序列類似的便利指令是 run
指令搭配 --recreate
:
ffx component run TARGET_MONIKER COMPONENT_URL --recreate
停止/開始
雖然這不是更新的主要方式,但停止並啟動元件的副作用是,元件會部分更新。
$ ffx component stop TARGET_MONIKER
$ ffx component start TARGET_MONIKER
假設 ffx serve
等套件管理工具正在執行,系統會載入並執行最新版元件程式碼。不過,由於 Fuchsia 元件架構的快取運作方式,資訊清單不會更新。資訊清單包含元件的路由和其他資訊,這些資訊是在 *.cm
檔案中定義。因此,如果您變更元件的能力路徑,停止並啟動元件不會採用這些變更。
ffx-laboratory
ffx-laboratory
是元件集合,可提供一組受限的開發功能。這個集合中的元件可使用下列功能:
- 通訊協定功能
fuchsia.logger.LogSink
:記錄記錄訊息。您可以在元件的 CML 檔案中,搭配include: [ "syslog/client.shard.cml" ],
使用這個通訊協定。fuchsia.process.Launcher
:建立新程序
- 儲存空間功能
tmp
:暫時儲存空間 (非永久)data
:由/tmp
支援的模擬永久儲存空間cache
:由/tmp
支援的模擬快取儲存空間
- 目錄功能
/boot
:元件管理服務提供的唯讀bootfs
ffx-laboratory
是 transient
集合。
即使停止,這個集合中的元件例項仍會保留。如要銷毀這個集合中的元件執行個體,請使用 ffx component destroy
指令。
疑難排解
本節列出開發期間執行元件時可能會遇到的常見問題。
無法解析元件
使用 ffx component start
或 ffx component run
時,如果元件架構無法解析元件執行個體,可能會發生下列錯誤:
$ ffx component run /core/ffx-laboratory:hello-world fuchsia-pkg://fuchsia.com/hello-world#meta/hello-world.cm
URL: fuchsia-pkg://fuchsia.com/hello-world#meta/hello-world.cm
Moniker: /core/ffx-laboratory:hello-world
Creating component instance...
Starting component instance...
Lifecycle protocol could not bind to component instance: InstanceCannotResolve
如果元件網址無法解析為有效的元件資訊清單,就會發生這種情況。
如要解決這個問題,請確認下列事項:
元件例項已存在
使用 ffx component create
或 ffx component run
時,如果元件執行個體已存在,可能會遇到下列錯誤:
$ ffx component run /core/ffx-laboratory:hello-world fuchsia-pkg://fuchsia.com/hello-world#meta/hello-world.cm
URL: fuchsia-pkg://fuchsia.com/hello-world#meta/hello-world.cm
Moniker: /core/ffx-laboratory:hello-world
Creating component instance...
Component instance already exists. Use --recreate to destroy and recreate a new instance, or --name to create a new instance with a different name.
如果目標路徑名稱已由其他元件執行個體使用,就會發生這種情況。
如要解決這個問題,請使用 ffx component
destroy
指令手動終止執行個體:
$ ffx component destroy /core/ffx-laboratory:hello-world
Moniker: /core/ffx-laboratory:hello-world
Destroying component instance...
如果您使用 ffx component run
,請新增 --recreate
旗標來終止執行個體並重新建立:
$ ffx component run /core/ffx-laboratory:hello-world fuchsia-pkg://fuchsia.com/hello-world#meta/hello-world.cm --recreate
URL: fuchsia-pkg://fuchsia.com/hello-world#meta/hello-world.cm
Moniker: /core/ffx-laboratory:hello-world
Creating component instance...
Component instance already exists. Destroying...
Recreating component instance...
Starting component instance...
或者,您也可以新增 --name
旗標,以不同名稱建立新執行個體:
$ ffx component run /core/ffx-laboratory:hello-world fuchsia-pkg://fuchsia.com/hello-world#meta/hello-world.cm --name hello-world-2
URL: fuchsia-pkg://fuchsia.com/hello-world#meta/hello-world.cm
Moniker: /core/ffx-laboratory:hello-world-2
Creating component instance...
Starting component instance...