1 /* Copyright (c) 2019-2022. The SimGrid Team. All rights reserved. */
3 /* This program is free software; you can redistribute it and/or modify it
4 * under the terms of the license (GNU LGPL) which comes with this package. */
6 #ifndef SIMGRID_MC_SIMCALL_COMM_OBSERVER_HPP
7 #define SIMGRID_MC_SIMCALL_COMM_OBSERVER_HPP
9 #include "simgrid/forward.h"
10 #include "src/kernel/actor/SimcallObserver.hpp"
11 #include "src/mc/transition/Transition.hpp"
12 #include "xbt/asserts.h"
16 namespace simgrid::kernel::actor {
18 class ActivityTestSimcall final : public ResultingSimcall<bool> {
19 activity::ActivityImpl* const activity_;
22 ActivityTestSimcall(ActorImpl* actor, activity::ActivityImpl* activity)
23 : ResultingSimcall(actor, true), activity_(activity)
26 bool is_visible() const override { return true; }
27 activity::ActivityImpl* get_activity() const { return activity_; }
28 void serialize(std::stringstream& stream) const override;
29 std::string to_string() const override;
32 class ActivityTestanySimcall final : public ResultingSimcall<ssize_t> {
33 const std::vector<activity::ActivityImpl*>& activities_;
34 std::vector<int> indexes_; // indexes in activities_ pointing to ready activities (=whose test() is positive)
38 ActivityTestanySimcall(ActorImpl* actor, const std::vector<activity::ActivityImpl*>& activities);
39 bool is_visible() const override { return true; }
40 bool is_enabled() override { return true; /* can return -1 if no activity is ready */ }
41 void serialize(std::stringstream& stream) const override;
42 std::string to_string() const override;
43 int get_max_consider() const override;
44 void prepare(int times_considered) override;
45 const std::vector<activity::ActivityImpl*>& get_activities() const { return activities_; }
46 int get_value() const { return next_value_; }
49 class ActivityWaitSimcall final : public ResultingSimcall<bool> {
50 activity::ActivityImpl* activity_;
51 const double timeout_;
54 ActivityWaitSimcall(ActorImpl* actor, activity::ActivityImpl* activity, double timeout)
55 : ResultingSimcall(actor, false), activity_(activity), timeout_(timeout)
58 void serialize(std::stringstream& stream) const override;
59 std::string to_string() const override;
60 bool is_visible() const override { return true; }
61 bool is_enabled() override;
62 activity::ActivityImpl* get_activity() const { return activity_; }
63 void set_activity(activity::ActivityImpl* activity) { activity_ = activity; }
64 double get_timeout() const { return timeout_; }
67 class ActivityWaitanySimcall final : public ResultingSimcall<ssize_t> {
68 const std::vector<activity::ActivityImpl*>& activities_;
69 std::vector<int> indexes_; // indexes in activities_ pointing to ready activities (=whose test() is positive)
70 const double timeout_;
74 ActivityWaitanySimcall(ActorImpl* actor, const std::vector<activity::ActivityImpl*>& activities, double timeout);
75 bool is_enabled() override;
76 void serialize(std::stringstream& stream) const override;
77 std::string to_string() const override;
78 bool is_visible() const override { return true; }
79 void prepare(int times_considered) override;
80 int get_max_consider() const override;
81 const std::vector<activity::ActivityImpl*>& get_activities() const { return activities_; }
82 double get_timeout() const { return timeout_; }
83 int get_value() const { return next_value_; }
86 class CommIsendSimcall final : public SimcallObserver {
87 activity::MailboxImpl* mbox_;
90 unsigned char* src_buff_;
91 size_t src_buff_size_;
94 activity::CommImpl* comm_ = {};
97 std::function<bool(void*, void*, activity::CommImpl*)> match_fun_;
98 std::function<void(void*)> clean_fun_; // used to free the synchro in case of problem after a detached send
99 std::function<void(activity::CommImpl*, void*, size_t)> copy_data_fun_; // used to copy data if not default one
103 ActorImpl* actor, activity::MailboxImpl* mbox, double payload_size, double rate, unsigned char* src_buff,
104 size_t src_buff_size, const std::function<bool(void*, void*, activity::CommImpl*)>& match_fun,
105 const std::function<void(void*)>& clean_fun, // used to free the synchro in case of problem after a detached send
106 const std::function<void(activity::CommImpl*, void*, size_t)>&
107 copy_data_fun, // used to copy data if not default one
108 void* payload, bool detached)
109 : SimcallObserver(actor)
111 , payload_size_(payload_size)
113 , src_buff_(src_buff)
114 , src_buff_size_(src_buff_size)
116 , detached_(detached)
117 , match_fun_(match_fun)
118 , clean_fun_(clean_fun)
119 , copy_data_fun_(copy_data_fun)
122 void serialize(std::stringstream& stream) const override;
123 std::string to_string() const override;
124 bool is_visible() const override { return true; }
125 activity::MailboxImpl* get_mailbox() const { return mbox_; }
126 double get_payload_size() const { return payload_size_; }
127 double get_rate() const { return rate_; }
128 unsigned char* get_src_buff() const { return src_buff_; }
129 size_t get_src_buff_size() const { return src_buff_size_; }
130 void* get_payload() const { return payload_; }
131 bool is_detached() const { return detached_; }
132 void set_comm(activity::CommImpl* comm) { comm_ = comm; }
133 void set_tag(int tag) { tag_ = tag; }
135 auto const& get_match_fun() const { return match_fun_; }
136 auto const& get_clean_fun() const { return clean_fun_; }
137 auto const& get_copy_data_fun() const { return copy_data_fun_; }
140 class CommIrecvSimcall final : public SimcallObserver {
141 activity::MailboxImpl* mbox_;
142 unsigned char* dst_buff_;
143 size_t* dst_buff_size_;
146 activity::CommImpl* comm_ = {};
149 std::function<bool(void*, void*, activity::CommImpl*)> match_fun_;
150 std::function<void(activity::CommImpl*, void*, size_t)> copy_data_fun_; // used to copy data if not default one
153 CommIrecvSimcall(ActorImpl* actor, activity::MailboxImpl* mbox, unsigned char* dst_buff, size_t* dst_buff_size,
154 const std::function<bool(void*, void*, activity::CommImpl*)>& match_fun,
155 const std::function<void(activity::CommImpl*, void*, size_t)>& copy_data_fun, void* payload,
157 : SimcallObserver(actor)
159 , dst_buff_(dst_buff)
160 , dst_buff_size_(dst_buff_size)
163 , match_fun_(match_fun)
164 , copy_data_fun_(copy_data_fun)
167 void serialize(std::stringstream& stream) const override;
168 std::string to_string() const override;
169 bool is_visible() const override { return true; }
170 activity::MailboxImpl* get_mailbox() const { return mbox_; }
171 double get_rate() const { return rate_; }
172 unsigned char* get_dst_buff() const { return dst_buff_; }
173 size_t* get_dst_buff_size() const { return dst_buff_size_; }
174 void* get_payload() const { return payload_; }
175 void set_comm(activity::CommImpl* comm) { comm_ = comm; }
176 void set_tag(int tag) { tag_ = tag; }
178 auto const& get_match_fun() const { return match_fun_; };
179 auto const& get_copy_data_fun() const { return copy_data_fun_; }
182 } // namespace simgrid::kernel::actor