This is an automated email from the ASF dual-hosted git repository. xiaoxiang781216 pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/nuttx.git
commit 163bf1e5d91074a46242807875e9c82307c6c13c Author: dongjiuzhu1 <[email protected]> AuthorDate: Mon May 12 11:56:22 2025 +0800 drivers/sensors/sensor_rpmsg: Do not wake sleeping cores for sub/unsub. sensor_rpmsg_advsub() sends every SUBSCRIBE / UNSUBSCRIBE broadcast to all remote cores, so a non-wakeup subscription on one core wakes up every other core in the system. * Record in the proxy whether wakeup was enabled through SNIOC_SET_WAKEUP. * Send SUBSCRIBE / UNSUBSCRIBE to an endpoint only when the remote core is running, or when its proxy for this topic has wakeup enabled. ADVERTISE is still sent to every endpoint, so a subscriber on a sleeping core learns about an advertiser that restarted. * Because a sleeping core may now miss an UNSUBSCRIBE, keep the proxies until the endpoint is released instead of freeing them when the last local subscriber closes, on UNADVERTISE, or when sending the ADVERTISE_ACK fails. UNADVERTISE is therefore no longer sent, and its handler is kept empty for peers that still send it. Signed-off-by: dongjiuzhu1 <[email protected]> Signed-off-by: likun17 <[email protected]> --- drivers/sensors/sensor_rpmsg.c | 128 ++++++++++++++++++++++++----------------- 1 file changed, 75 insertions(+), 53 deletions(-) diff --git a/drivers/sensors/sensor_rpmsg.c b/drivers/sensors/sensor_rpmsg.c index d2a750cc5f3..41b1a1e05f6 100644 --- a/drivers/sensors/sensor_rpmsg.c +++ b/drivers/sensors/sensor_rpmsg.c @@ -135,6 +135,7 @@ struct sensor_rpmsg_proxy_s struct list_node node; FAR struct rpmsg_endpoint *ept; uint64_t cookie; + bool wakeup; }; /* Remote message structure */ @@ -358,7 +359,63 @@ static void sensor_rpmsg_advsub(FAR struct sensor_rpmsg_dev_s *dev, list_for_every_entry(&g_eptlist, sre, struct sensor_rpmsg_ept_s, node) { - sensor_rpmsg_advsub_one(dev, &sre->ept, command); + FAR struct sensor_rpmsg_proxy_s *proxy = NULL; + bool find = false; + bool adv = false; + + if (command == SENSOR_RPMSG_SUBSCRIBE || + command == SENSOR_RPMSG_UNSUBSCRIBE) + { + /* If it has been bound before, do not send. */ + + list_for_every_entry(&dev->proxylist, proxy, + struct sensor_rpmsg_proxy_s, node) + { + if (proxy->ept == &sre->ept) + { + find = true; + break; + } + } + } + else + { + /* Advertise and unadvertise need to be broadcast. otherwise, data + * will be lost if the following two conditions occur at the same + * time. + * 1.The adv core crashed, the advertiser has a long life cycle. + * 2.The remote subscriber has a long life cycle, and the remote + * core side has not been restarted + */ + + adv = true; + } + + /* There are several scenarios that require broadcasting: + * 1. If the proxy corresponding to the EPT (Endpoint) did not + * exist previously, this constitutes the first broadcast. + * 2. If the proxy previously had a wakeup attribute, it should + * be broadcast every time. + * 3. If the proxy does not have the wakeup attribute and the + * target core is in the running state, we should still broadcast + * it. + * + * In summary, Let's avoid broadcasting non-wakeup sensors when the + * target core is in a sleep state to prevent unnecessary wakeups. + */ + + if (adv || rpmsg_is_running(sre->ept.rdev) || (find && proxy->wakeup)) + { + sensor_rpmsg_advsub_one(dev, &sre->ept, command); + } + else + { + sninfo("INFO: advsub:%d %s ignore broadcast to %s, " + "as %d, %d, %d\n", + command, dev->path, rpmsg_get_cpuname(sre->ept.rdev), + find, proxy ? proxy->wakeup : 0, + rpmsg_is_running(sre->ept.rdev)); + } } up_read(&g_ept_lock); @@ -411,6 +468,15 @@ static int sensor_rpmsg_ioctl(FAR struct sensor_rpmsg_dev_s *dev, list_for_every_entry_safe(&dev->proxylist, proxy, ptmp, struct sensor_rpmsg_proxy_s, node) { + /* Check whether the proxy has been set to the wakeup state + * before. If it has, record it. + */ + + if (cmd == SNIOC_SET_WAKEUP) + { + proxy->wakeup = arg; + } + ept = proxy->ept; pcookie = proxy->cookie; msg = rpmsg_get_tx_payload_buffer(ept, &space, true); @@ -509,6 +575,7 @@ sensor_rpmsg_alloc_proxy(FAR struct sensor_rpmsg_dev_s *dev, proxy->ept = ept; proxy->cookie = msg->cookie; + proxy->wakeup = false; ret = file_open(&file, dev->path, SENSOR_REMOTE | O_CLOEXEC); if (ret < 0) { @@ -723,11 +790,8 @@ static int sensor_rpmsg_close(FAR struct sensor_lowerhalf_s *lower, { FAR struct sensor_rpmsg_dev_s *dev = lower->priv; FAR struct sensor_lowerhalf_s *drv = dev->drv; - FAR struct sensor_rpmsg_proxy_s *proxy; - FAR struct sensor_rpmsg_proxy_s *ptmp; FAR struct sensor_rpmsg_stub_s *stub; FAR struct sensor_rpmsg_stub_s *stmp; - bool unadv = false; bool unsub = false; int ret = 0; @@ -746,7 +810,6 @@ static int sensor_rpmsg_close(FAR struct sensor_lowerhalf_s *lower, { if (dev->nadvertisers == 1) { - unadv = true; list_for_every_entry_safe(&dev->stublist, stub, stmp, struct sensor_rpmsg_stub_s, node) { @@ -759,22 +822,14 @@ static int sensor_rpmsg_close(FAR struct sensor_lowerhalf_s *lower, if ((filep->f_oflags & O_ACCMODE) != O_WRONLY) { - if (dev->nsubscribers == 1) - { - /* Send broadcast unsubscribed info if dev isn't - * physical sensor to avoid waking up remote core. - */ - - if (drv->ops->activate == NULL) - { - unsub = true; - } + /* Send broadcast unsubscribed info if dev isn't + * physical sensor to avoid waking up remote core. + */ - list_for_every_entry_safe(&dev->proxylist, proxy, ptmp, - struct sensor_rpmsg_proxy_s, node) - { - sensor_rpmsg_free_proxy(dev, proxy); - } + if (dev->nsubscribers == 1 && + drv->ops->activate == NULL) + { + unsub = true; } dev->nsubscribers--; @@ -782,11 +837,6 @@ static int sensor_rpmsg_close(FAR struct sensor_lowerhalf_s *lower, sensor_rpmsg_unlock(dev); - if (unadv) - { - sensor_rpmsg_advsub(dev, SENSOR_RPMSG_UNADVERTISE); - } - if (unsub) { sensor_rpmsg_advsub(dev, SENSOR_RPMSG_UNSUBSCRIBE); @@ -1171,9 +1221,6 @@ static int sensor_rpmsg_adv_handler(FAR struct rpmsg_endpoint *ept, ret = rpmsg_send(ept, msg, len); if (ret < 0) { - sensor_rpmsg_lock(dev); - sensor_rpmsg_free_proxy(dev, proxy); - sensor_rpmsg_unlock(dev); snerr("ERROR: adv rpmsg send failed:%s, %d, %s\n", dev->path, ret, rpmsg_get_cpuname(ept->rdev)); } @@ -1195,7 +1242,6 @@ static int sensor_rpmsg_advack_handler(FAR struct rpmsg_endpoint *ept, dev = sensor_rpmsg_find_dev(msg->path); if (dev && !sensor_rpmsg_alloc_stub(dev, ept, msg->cookie)) { - sensor_rpmsg_advsub_one(dev, ept, SENSOR_RPMSG_UNADVERTISE); snerr("ERROR: advack failed:%s, %s\n", dev->path, rpmsg_get_cpuname(ept->rdev)); } @@ -1207,30 +1253,6 @@ static int sensor_rpmsg_unadv_handler(FAR struct rpmsg_endpoint *ept, FAR void *data, size_t len, uint32_t src, FAR void *priv) { - FAR struct sensor_rpmsg_advsub_s *msg = data; - FAR struct sensor_rpmsg_proxy_s *proxy; - FAR struct sensor_rpmsg_dev_s *dev; - - dev = sensor_rpmsg_find_dev(msg->path); - if (!dev) - { - return 0; - } - - sensor_rpmsg_lock(dev); - list_for_every_entry(&dev->proxylist, proxy, - struct sensor_rpmsg_proxy_s, node) - { - if (proxy->ept == ept && proxy->cookie == msg->cookie) - { - sensor_rpmsg_free_proxy(dev, proxy); - sminfo(dev->name, "rpmsg unadv free proxy success, " - "remote:%s", rpmsg_get_cpuname(ept->rdev)); - break; - } - } - - sensor_rpmsg_unlock(dev); return 0; }
