目錄
1. React to the end of actors
當actor結束的時候,要執行什麼程序。在一些程序中,當Actor結束時,需要一些特殊的反饋或者特殊的信息反饋,通過以下方式實現該功能。
You can attach callbacks to the end of actors. There is several ways of doing so, depending on whether you want to attach your callback to a given actor and on how you define the end of a given actor. User code probably want to react to the termination of an actor while some plugins want to react to the destruction (memory collection) of actors.
這三種方式有各自的優點
on_exit():可以爲特定的actor註冊一個結束時的function,通常該函數用於釋放actor執行時候的空間
on_termination():所有actor之間通用,在開發SimGrid插件時候,這個方法非常通用
on_destruction():當程序destory的時候會觸發
同時這三種方法可以疊加使用。下面是具體的例子。
2. actor-exiting.cpp
/* Copyright (c) 2017-2020. The SimGrid Team. All rights reserved. */
/* This program is free software; you can redistribute it and/or modify it
* under the terms of the license (GNU LGPL) which comes with this package. */
/* There is two very different ways of being informed when an actor exits.
*
* The this_actor::on_exit() function allows one to register a function to be
* executed when this very actor exits. The registered function will run
* when this actor terminates (either because its main function returns, or
* because it's killed in any way). No simcall are allowed here: your actor
* is dead already, so it cannot interact with its environment in any way
* (network, executions, disks, etc).
*
* Usually, the functions registered in this_actor::on_exit() are in charge
* of releasing any memory allocated by the actor during its execution.
*
* The other way of getting informed when an actor terminates is to connect a
* function in the Actor::on_termination signal, that is shared between
* all actors. Callbacks to this signal are executed for each terminating
* actors, no matter what. This is useful in many cases, in particular
* when developing SimGrid plugins.
*
* Finally, you can attach callbacks to the Actor::on_destruction signal.
* It is also shared between all actors, and gets fired when the actors
* are destroyed. A delay is possible between the termination of an actor
* (ie, when it terminates executing its code) and its destruction (ie,
* when it is not referenced anywhere in the simulation and can be collected).
*
* In both cases, you can stack more than one callback in the signal.
* They will all be executed in the registration order.
*/
#include <simgrid/s4u.hpp>
XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_actor_exiting, "Messages specific for this s4u example");
static void actor_a()
{
// Register a lambda function to be executed once it stops
simgrid::s4u::this_actor::on_exit([](bool /*failed*/) { XBT_INFO("actor_a stop info from on_exit()"); });
simgrid::s4u::this_actor::execute(1e9);
}
static void actor_b()
{
simgrid::s4u::this_actor::execute(2e9);
}
int main(int argc, char* argv[])
{
simgrid::s4u::Engine e(&argc, argv);
xbt_assert(argc == 2, "Usage: %s platform_file\n\tExample: %s ../platforms/small_platform.xml\n", argv[0], argv[0]);
e.load_platform(argv[1]); /* - Load the platform description */
/* Register a callback in the Actor::on_termination signal. It will be called for every terminated actors */
simgrid::s4u::Actor::on_termination.connect(
[](simgrid::s4u::Actor const& actor) { XBT_INFO("Actor %s terminates info from on_termination()", actor.get_cname()); });
/* Register a callback in the Actor::on_destruction signal. It will be called for every destructed actors */
simgrid::s4u::Actor::on_destruction.connect(
[](simgrid::s4u::Actor const& actor) { XBT_INFO("Actor %s gets destroyed info from on_destruction()", actor.get_cname()); });
/* Create some actors */
simgrid::s4u::Actor::create("linweieran_A", simgrid::s4u::Host::by_name("Tremblay"), actor_a);
simgrid::s4u::Actor::create("linweieran_B", simgrid::s4u::Host::by_name("Fafard"), actor_b);
e.run(); /* - Run the simulation */
return 0;
}
3. platform.xml
這個與SimGrid【2】中的平臺是完全一致的代碼。
4. 運行結果與分析
從這個結果可以看出,對於actor不同的退出方式。on_exit() 針對性更高,on_termination()與on_destruction()都是全部actor通用的。但是這個程序沒有體現出什麼時候是exit,什麼時候是termination與destruction,只是在運行結束的時候全部actor都被終止。
參考文獻
[1] 官方說明:https://simgrid.org/doc/latest/app_s4u.html#starting-and-stoping-actors
[2] SimGrid【2】:https://blog.csdn.net/linweieran/article/details/104303952