From 3d4ac9b78569e15d217f58b01713d5fceff562b0 Mon Sep 17 00:00:00 2001 From: Michael Reese Date: Mon, 22 Jul 2024 18:18:48 +0200 Subject: [PATCH] saftbus: count dropped signals and display them in saftbus-ctl -s signals are dropped if the file descriptor for the signal socketpair is not ready to take data --- saftbus-gen/saftbus-gen.cpp | 2 +- saftbus/client.hpp | 6 ++--- saftbus/saftbus-ctl.cpp | 6 ++--- saftbus/service.cpp | 45 ++++++++++++++++++++----------------- 4 files changed, 31 insertions(+), 28 deletions(-) diff --git a/saftbus-gen/saftbus-gen.cpp b/saftbus-gen/saftbus-gen.cpp index af2b29a2..d32c6250 100644 --- a/saftbus-gen/saftbus-gen.cpp +++ b/saftbus-gen/saftbus-gen.cpp @@ -1176,7 +1176,7 @@ void generate_service_implementation(const std::string &outputdirectory, ClassDe } } out << ") {" << std::endl; - // out << "\t\t" << "std::cerr << \"service dispatch function called!!!!!!!!!!!!\" << std::endl;" << std::endl; + // out << "\t\t" << "std::cerr << \"service "<< class_definition.name << "::" << signal.name << " dispatch function called\" << std::endl;" << std::endl; out << "\t\t" << "saftbus::Serializer serialized_signal;" << std::endl; out << "\t\t" << "serialized_signal.put(get_object_id());" << std::endl; out << "\t\t" << "serialized_signal.put(" << interface_no << ");" << std::endl; diff --git a/saftbus/client.hpp b/saftbus/client.hpp index 21fb5d32..920876e3 100644 --- a/saftbus/client.hpp +++ b/saftbus/client.hpp @@ -190,7 +190,7 @@ namespace saftbus { unsigned object_id; std::string object_path; std::vector interface_names; - std::map signal_fds_use_count; + std::map > signal_fds_use_count_and_dropped_signals; int owner; bool has_destruction_callback; bool destroy_if_owner_quits; @@ -199,7 +199,7 @@ namespace saftbus { ser.put(object_id); ser.put(object_path); ser.put(interface_names); - ser.put(signal_fds_use_count); + ser.put(signal_fds_use_count_and_dropped_signals); ser.put(owner); ser.put(has_destruction_callback); ser.put(destroy_if_owner_quits); @@ -209,7 +209,7 @@ namespace saftbus { des.get(object_id); des.get(object_path); des.get(interface_names); - des.get(signal_fds_use_count); + des.get(signal_fds_use_count_and_dropped_signals); des.get(owner); des.get(has_destruction_callback); des.get(destroy_if_owner_quits); diff --git a/saftbus/saftbus-ctl.cpp b/saftbus/saftbus-ctl.cpp index 1a9b2bf0..2287ae9a 100644 --- a/saftbus/saftbus-ctl.cpp +++ b/saftbus/saftbus-ctl.cpp @@ -59,7 +59,7 @@ void print_status(saftbus::SaftbusInfo &saftbus_info) { } std::cout << "services:" << std::endl; std::cout << " " << std::setw(max_object_path_length) << std::left << "object-path" - << " ID [owner] sig-fd/use-count interface-names" << std::endl; + << " ID [owner] sig-fd/use-count/dropped-signals interface-names" << std::endl; std::vector services; for (auto &object: saftbus_info.object_infos) { std::ostringstream line; @@ -73,8 +73,8 @@ void print_status(saftbus::SaftbusInfo &saftbus_info) { else if (object.has_destruction_callback && !object.destroy_if_owner_quits) line << "d "; else line << " "; - for (auto &user: object.signal_fds_use_count) { - line << user.first << "/" << user.second << " "; + for (auto &fd_user_dropped: object.signal_fds_use_count_and_dropped_signals) { + line << fd_user_dropped.first << "/" << fd_user_dropped.second.first << "/" << fd_user_dropped.second.second << " "; } for (auto &interface: object.interface_names) { line << interface << " "; diff --git a/saftbus/service.cpp b/saftbus/service.cpp index 85a926ab..134caef1 100644 --- a/saftbus/service.cpp +++ b/saftbus/service.cpp @@ -36,7 +36,7 @@ namespace saftbus { struct Service::Impl { int owner; - std::map signal_fds_use_count; + std::map > signal_fds_use_count_and_dropped_signals; std::vector interface_names; std::string object_path; uint64_t object_id; @@ -174,22 +174,24 @@ namespace saftbus { void Service::Impl::remove_signal_fd(int fd) { - auto found_use_count = signal_fds_use_count.find(fd); - if (found_use_count != signal_fds_use_count.end()) { - signal_fds_use_count.erase(fd); + auto found_use_count = signal_fds_use_count_and_dropped_signals.find(fd); + if (found_use_count != signal_fds_use_count_and_dropped_signals.end()) { + signal_fds_use_count_and_dropped_signals.erase(fd); } } void Service::emit(Serializer &send) { - for (auto &fd_use_count: d->signal_fds_use_count) { - if (fd_use_count.second > 0) { // only send data if use count is > 0 - int fd = fd_use_count.first; + for (auto &fd_use_count_dropped: d->signal_fds_use_count_and_dropped_signals) { + auto &fd = fd_use_count_dropped.first; + auto &use_count = fd_use_count_dropped.second.first; + auto &dropped_signals = fd_use_count_dropped.second.second; + if (use_count > 0) { // only send data if use count is > 0 struct pollfd pfd; pfd.fd = fd; pfd.events = POLLOUT; - if (poll(&pfd, 1, 0) <= 0) { // fd is not ready immediately => drop the signal - // std::cerr << "Service " << get_object_path() << "Service::emit() drop signal for fd " << fd << std::endl; + if (poll(&pfd, 1, 0) <= 0) { // fd is not ready immediately => drop the signal + ++dropped_signals; // count number of dropped signals (this number can be seen when with "saftbus-ctl -s") } else { send.write_to_no_init(fd); // The same data is written multiple times. Therefore the } // put_init function must not be called automatically after write @@ -454,7 +456,7 @@ namespace saftbus { assert(find_result != d->objects.end()); // if this cannot be found, the lookup table is not correct auto &service = find_result->second; if (service->get_interface_name2no_map(interface_names, interface_name2no_map)) { //returns false if not all requested interfaces are implemented - service->d->signal_fds_use_count[signal_group_fd]++; + service->d->signal_fds_use_count_and_dropped_signals[signal_group_fd].first++; d->connection->register_signal_id_for_client(client_fd, signal_group_fd); return saftbus_object_id; } @@ -470,11 +472,12 @@ namespace saftbus { // std::cerr << "object id " << saftbus_object_id << " already gone" << std::endl; return; } - auto &service = find_result->second; - service->d->signal_fds_use_count[signal_group_fd]--; + auto &service = find_result->second; + auto &use_count = service->d->signal_fds_use_count_and_dropped_signals[signal_group_fd].first; + use_count--; d->connection->unregister_signal_id_for_client(client_fd, signal_group_fd); - if (service->d->signal_fds_use_count[signal_group_fd] == 0) { - service->d->signal_fds_use_count.erase(signal_group_fd); + if (use_count == 0) { + service->d->signal_fds_use_count_and_dropped_signals.erase(signal_group_fd); } } @@ -599,13 +602,13 @@ namespace saftbus { SaftbusInfo result; for (auto &obj: d->objects) { SaftbusInfo::ObjectInfo object_info; - object_info.object_id = obj.first; - object_info.object_path = obj.second->d->object_path; - object_info.interface_names = obj.second->d->interface_names; - object_info.signal_fds_use_count = obj.second->d->signal_fds_use_count; - object_info.owner = obj.second->d->owner; - object_info.has_destruction_callback = obj.second->d->destruction_callback?true:false; - object_info.destroy_if_owner_quits = obj.second->d->destroy_if_owner_quits; + object_info.object_id = obj.first; + object_info.object_path = obj.second->d->object_path; + object_info.interface_names = obj.second->d->interface_names; + object_info.signal_fds_use_count_and_dropped_signals = obj.second->d->signal_fds_use_count_and_dropped_signals; + object_info.owner = obj.second->d->owner; + object_info.has_destruction_callback = obj.second->d->destruction_callback?true:false; + object_info.destroy_if_owner_quits = obj.second->d->destroy_if_owner_quits; result.object_infos.push_back(object_info); } for (auto &client: d->connection->get_client_info()) {