SimGrid 【3】s4u actor exit/killed/destory时候的回调

目录

 

1. React to the end of actors

2. actor-exiting.cpp

3. platform.xml

4. 运行结果与分析

参考文献


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

 

 

 

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值