Skip to contentSkip to conversation

xenstore - Suggestion of batching watch events

DISCUSSION Andriy Sultanov 5 participants 10 messages Jun 24, 2025

lore
Share & tools
lore
#0 lore raw

Currently, as far as I am aware, the ability of xenstore clients to properly handle and detect batch updates is somewhat lacking. Transactions are not directly visible to the clients watching a particular directory - they will receive a lot of individual watch_event's once the transaction is committed, without any indication when such updates are going to end.

Clients such as xenopsd from the xapi toolstack are reliant on xenstore to track their managed domains, and a flood of individual updates most often results in a flood of events raised from xenopsd to xapi (There are consolidation mechanisms implemented there, with updates getting merged together, but if xapi picks up update events from the queue quickly enough, it will only get more update events later)

The need for batching is fairly evident from the fact that XenServer's Windows PV drivers, for example, adopted an ad-hoc "batch" optimization (not documented anywhere, of course), where some sequence of writes is followed by a write of the value "1" to "data/updated". This used to be honoured by xapi, which would not consider the guest agent update done until it received notice of such a "batch ended" update, but it caused xapi to miss updates that were not followed by such a write, so xapi now ignores this ad-hoc batching. One could imagine many workarounds here (for example, some sort of a mechanism where xenopsd stalls an update for a second to see if any more related updates show up and only then notifies xapi of it, with obvious trade-offs), but IMO it could be worth considering making this easier on the xenstore side for different use-cases.

Suggestion:
WATCH_EVENT's req_id and tx_id are currently 0. Could it be possible, for example, to modify this such that watch events coming as a result of a transaction commit (a "batch") have tx_id of the corresponding transaction and req_id of, say, 2 if it's the last such watch event of a batch and 1 otherwise? Old clients would still ignore these values, but it would allow some others to detect if an update is part of a logical batch that doesn't end until its last event.

Is this beyond the scope of what xenstored wants to do? From a first glance, this does not seem to introduce obvious unwanted information leaks either, but I could be wrong. I would love to hear if this is something that could be interesting to others and if this could be considered at all.

Thank you!

#1 lore raw
Quoted 8 lines · Andriy Sultanov

Suggestion:
WATCH_EVENT's req_id and tx_id are currently 0. Could it be possible, for example, to modify this such that watch events coming as a result of a transaction commit (a "batch") have tx_id of the corresponding transaction and req_id of, say, 2 if it's the last such watch event of a batch and 1 otherwise? Old clients would still ignore these values, but it would allow some others to detect if an update is part of a logical batch that doesn't end until its last event.

Come to think of it, since clients could watch arbitrary parts of what the transaction touched, this wouldn't be as simple, xenstored would have to issue the "batch ended" packet per token, tracking that somehow internally... Perhaps transaction_start and transaction_end could produce WATCH_EVENT (or some other similar packet) as well so that this tracking could be done client-side? (standard WATCH_EVENT would still need their tx_id to be modified)

#2 lore raw
Quoted 15 lines · Andriy Sultanov
Quoted 11 lines · Andriy Sultanov

Suggestion:
WATCH_EVENT's req_id and tx_id are currently 0. Could it be possible, for example, to modify this such that watch events coming as a result of a transaction commit (a "batch") have tx_id of the corresponding transaction
and req_id of, say, 2 if it's the last such watch event of a batch and 1 otherwise? Old clients would still ignore these values, but it would allow
some others to detect if an update is part of a logical batch that doesn't end
until its last event.

Quoted 8 lines

Come to think of it, since clients could watch arbitrary parts of what the transaction touched, this wouldn't be as simple, xenstored would have to issue the "batch ended" packet per token, tracking that somehow internally... Perhaps transaction_start and transaction_end could produce WATCH_EVENT (or some other similar packet) as well so that this tracking could be done client-side? (standard WATCH_EVENT would still need their tx_id to be modified)

The Windows PV drivers also write a huge amount of data to xenstore. E.g. I see 74 entries under /data, grouped by 1st level:
/data/volumes: 39
/dta/scsi: 7
/data/vif: 3
/data/xd: 2
/data/cpus: 2
/data/...: 19

This is probably why the /data/updated optimization used to be beneficial.

/attr/os/hotfixes is also large (28 on this VM, but used to be in the hundreds for older versions of Windows).

In some situations xenopsd could set up more granular watches, but then you run into scalability issues due to having thousands of watches per domain (although you've fixed the largest problem there:
connecting/disconnecting xenstore clients causing slowdowns due to watch trie walks).

There is also the problem that watch events don't contain enough information, so watch events only acts as signals to xenopsd, which then goes on and fetches the entire xenstore subtree to figure out what actually changed. Which is the result of some of the O(N^2) performance issues we still have.
We used to have a prototype xenstore cache which avoided actually making those fetches from oxenstored, and once something got into its cache, it kept track of updates by setting up a watch on the key. Although a cold start then took ~30m (worse than not having a cache at all). Although a compromise could be to cache on-the-fly (instead of precaching everything you see), e.g. I don't think we actually care about the values under /data/volumes and attr/os/hotfixes, other than for debugging purposes, so if xenopsd never fetches them, the cache shouldn't either.

To avoid a lot of round-trips a new kind of watch event that tells you the value(s) in addition to the keys might be useful. And then this new kind of watch event could also be emitted once per transaction (I think the events are already emitted at transaction commit time, and not sooner).
If filtering watch events based on tree depths would be useful in some situations then the new watch event could also try to do that. But then one such "batched" watch event could become too big, larger than what would fit into the xenstore ring (and for historic reasons we don't support sending >4k packets through xenstore).

Best regards,
--Edwin

#3 lore raw
In reply to Andriy Sultanov
Quoted 35 lines · Andriy Sultanov

Currently, as far as I am aware, the ability of xenstore clients to properly handle and detect batch updates is somewhat lacking. Transactions are not directly visible to the clients watching a particular directory - they will receive a lot of individual watch_event's once the transaction is committed, without any indication when such updates are going to end.

Clients such as xenopsd from the xapi toolstack are reliant on xenstore to track their managed domains, and a flood of individual updates most often results in a flood of events raised from xenopsd to xapi (There are consolidation mechanisms implemented there, with updates getting merged together, but if xapi picks up update events from the queue quickly enough, it will only get more update events later)

The need for batching is fairly evident from the fact that XenServer's Windows PV drivers, for example, adopted an ad-hoc "batch" optimization (not documented anywhere, of course), where some sequence of writes is followed by a write of the value "1" to "data/updated". This used to be honoured by xapi, which would not consider the guest agent update done until it received notice of such a "batch ended" update, but it caused xapi to miss updates that were not followed by such a write, so xapi now ignores this ad-hoc batching. One could imagine many workarounds here (for example, some sort of a mechanism where xenopsd stalls an update for a second to see if any more related updates show up and only then notifies xapi of it, with obvious trade-offs), but IMO it could be worth considering making this easier on the xenstore side for different use-cases.

Suggestion:
WATCH_EVENT's req_id and tx_id are currently 0. Could it be possible, for example, to modify this such that watch events coming as a result of a transaction commit (a "batch") have tx_id of the corresponding transaction and req_id of, say, 2 if it's the last such watch event of a batch and 1 otherwise? Old clients would still ignore these values, but it would allow some others to detect if an update is part of a logical batch that doesn't end until its last event.

Quoted 4 lines

Is this beyond the scope of what xenstored wants to do? From a first glance, this does not seem to introduce obvious unwanted information leaks either, but I could be wrong. I would love to hear if this is something that could be interesting to others and if this could be considered at all.

The main reason for the large number of watch events after a transaction is the fact that the watch for e.g. detecting the addition of a new block device will be set on a node being common for all potential block devices handled by the watcher. This results in a watch event for each single node modified below this node, which are usually quite a lot even when only adding a single device.

The solution for this problem is NOT to batch all the events and to ignore the majority of those events, but to avoid creating most of those events.

For this reason the Xenstore protocol has been extended to allow for limiting the number of node levels below a watched node to be relevant for a watch to fire.

What is missing so far are Xenstore implementations to support this feature, and Xenstore users to make use of it. I'm working on supporting this in C xenstored, but due to other urgent work this will probably land upstream only in the Xen 4.22 time frame, probably together with Xen tools (libxl) making use of this feature.

Juergen

#4 lore raw
Quoted 14 lines · Jürgen Groß

The main reason for the large number of watch events after a transaction is the fact that the watch for e.g. detecting the addition of a new block device will be set on a node being common for all potential block devices handled by the watcher. This results in a watch event for each single node modified below this node, which are usually quite a lot even when only adding a single device.

The solution for this problem is NOT to batch all the events and to ignore the majority of those events, but to avoid creating most of those events.

For this reason the Xenstore protocol has been extended to allow for limiting the number of node levels below a watched node to be relevant for a watch to fire.

Quoted 8 lines

What is missing so far are Xenstore implementations to support this feature, and Xenstore users to make use of it. I'm working on supporting this in C xenstored, but due to other urgent work this will probably land upstream only in the Xen 4.22 time frame, probably together with Xen tools (libxl) making use of this feature.

Juergen

I was not aware that watch has had a (relatively) new unimplemented parameter added, thanks! I've noted this on the oxenstored side.
(https://github.com/xapi-project/oxenstored/issues/15)

But I think this only covers part of the problem. Looking at the concrete case of xenopsd and Windows VMs, xenopsd cares about most of the nodes it receives watch events from, the issue is that it doesn't know when these are grouped together in one way or another.

#5 lore raw
Quoted 8 lines · Andriy Sultanov

But I think this only covers part of the problem. Looking at the concrete case of xenopsd and Windows VMs, xenopsd cares about most of the nodes it receives watch events from, the issue is that it doesn't know when these are grouped together in one way or another.

Is the common case that a client wants to observe the changes rather than just knowing that a change occurred? If so, it seems transmitting changes as part of a watch event (rather than forcing the client to traverse the tree) would be a better protocol. That would suggest to introduce a new kind of watch that reports changes at the end of a transaction. The size of the event would depend on the size of the change.

— C

#6 lore raw
Quoted 11 lines · Christian Lindig
Quoted 9 lines · Andriy Sultanov

But I think this only covers part of the problem. Looking at the concrete case of xenopsd and Windows VMs, xenopsd cares about most of the nodes it receives watch events from, the issue is that it doesn't know when these are grouped together in one way or another.

Is the common case that a client wants to observe the changes rather than just knowing that a change occurred? If so, it seems transmitting changes as part of a watch event (rather than forcing the client to traverse the tree) would be a better protocol. That would suggest to introduce a new kind of watch that reports changes at the end of a transaction. The size of the event would depend on the size of the change.

Most of the code in xapi_xenops.ml that deals with watch events wants to compare the new value with the old, to know whether it needs to send an update to XAPI.
So yes, including as much information as we can in the watch event would avoid round-trips (and might also avoid some of the O(N^2) issues because xenopsd would know exactly what key changed to what value), for xenopsd's use-case.

The situation may be different for PV device backends/frontends, what might be useful there are wildcards, e.g. /path/to/vif/*/state, etc. which would also trigger when new devices appear or old devices disappear.
The tree depth limit already proposed in xenstore.txt could also help with this (so you can watch for a new device to appear without constantly watching all its keys), and the driver can then setup watches just on state (and whatever other fields it wants to watch).

Best regards,
--Edwin

Quoted 4 lines

— C

#7 lore raw
In reply to Andriy Sultanov

I believe what you observe is a major source of inefficiency for the reason you describe: changes are acted upon too early because there is no way to observe that they are part of a transaction. So now heuristics come in like waiting for more changes before acting on the ones observed. I wonder how the tree structure plays into this. Clients watch different sub trees and we don’t exploit this knowledge. I do agree that some protocol or syntax to batch updates would be useful.

— C

Quoted 45 lines · Andriy Sultanov

Currently, as far as I am aware, the ability of xenstore clients to properly handle and detect batch updates is somewhat lacking. Transactions are not directly visible to the clients watching a particular directory - they will receive a lot of individual watch_event's once the transaction is committed, without any indication when such updates are going to end.

Clients such as xenopsd from the xapi toolstack are reliant on xenstore to track their managed domains, and a flood of individual updates most often results in a flood of events raised from xenopsd to xapi (There are consolidation mechanisms implemented there, with updates getting merged together, but if xapi picks up update events from the queue quickly enough, it will only get more update events later)

The need for batching is fairly evident from the fact that XenServer's Windows PV drivers, for example, adopted an ad-hoc "batch" optimization (not documented anywhere, of course), where some sequence of writes is followed by a write of the value "1" to "data/updated". This used to be honoured by xapi, which would not consider the guest agent update done until it received notice of such a "batch ended" update, but it caused xapi to miss updates that were not followed by such a write, so xapi now ignores this ad-hoc batching. One could imagine many workarounds here (for example, some sort of a mechanism where xenopsd stalls an update for a second to see if any more related updates show up and only then notifies xapi of it, with obvious trade-offs), but IMO it could be worth considering making this easier on the xenstore side for different use-cases.

Suggestion:
WATCH_EVENT's req_id and tx_id are currently 0. Could it be possible, for example, to modify this such that watch events coming as a result of a transaction commit (a "batch") have tx_id of the corresponding transaction and req_id of, say, 2 if it's the last such watch event of a batch and 1 otherwise? Old clients would still ignore these values, but it would allow some others to detect if an update is part of a logical batch that doesn't end until its last event.

Is this beyond the scope of what xenstored wants to do? From a first glance, this does not seem to introduce obvious unwanted information leaks either, but I could be wrong. I would love to hear if this is something that could be interesting to others and if this could be considered at all.

Thank you!

#8 lore raw
Quoted 2 lines · Christian Lindig

I believe what you observe is a major source of inefficiency for the reason you describe: changes are acted upon too early because there is no way to observe that they are part of a transaction. So now heuristics come in like waiting for more changes before acting on the ones observed. I wonder how the tree structure plays into this. Clients watch different sub trees and we don’t exploit this knowledge. I do agree that some protocol or syntax to batch updates would be useful.

This is probably where the Irmin-based prototype had an advantage, pulling batched updates is something that 'git' is good at, and in many ways Irmin was similar to git.
When I joined XenServer I've been told that all the improvements from the Irmin prototype got integrated back into the mainline one, but that clearly isn't true, because:
* the mainline one had a lot of security issues that the Irmin based one didn't
* batching of updates is completely absent

Since this "high-bandwidth" event mechanism is only needed by Dom0 I think we can avoid sending these using the xenstore PV ring protocol, and even if we don't use Irmin to implement or send the updates, we shouldn't be constrained anymore by the 4K limitation of a xenstore packet on the socket interface in Dom0.

If this is a new kind of API message then clients can opt-in (older clients who can't cope with such larger responses won't know to make the new API call either).
That would avoid the problem we had with the directory protocol where O and C versions assigned different semantics *to the same API call* (O version used >4k packets, C version failed and only worked with the PARTIAL protocol). Now we finally have behaviour parity on that, so any change we make, I suggest we do by introducing a new API call that doesn't inherit legacy limitations like 4K packets sizes on unix domain sockets at least.

Best regards,
--Edwin

Quoted 50 lines

— C

Quoted 45 lines · Andriy Sultanov

Currently, as far as I am aware, the ability of xenstore clients to properly handle and detect batch updates is somewhat lacking. Transactions are not directly visible to the clients watching a particular directory - they will receive a lot of individual watch_event's once the transaction is committed, without any indication when such updates are going to end.

Clients such as xenopsd from the xapi toolstack are reliant on xenstore to track their managed domains, and a flood of individual updates most often results in a flood of events raised from xenopsd to xapi (There are consolidation mechanisms implemented there, with updates getting merged together, but if xapi picks up update events from the queue quickly enough, it will only get more update events later)

The need for batching is fairly evident from the fact that XenServer's Windows PV drivers, for example, adopted an ad-hoc "batch" optimization (not documented anywhere, of course), where some sequence of writes is followed by a write of the value "1" to "data/updated". This used to be honoured by xapi, which would not consider the guest agent update done until it received notice of such a "batch ended" update, but it caused xapi to miss updates that were not followed by such a write, so xapi now ignores this ad-hoc batching. One could imagine many workarounds here (for example, some sort of a mechanism where xenopsd stalls an update for a second to see if any more related updates show up and only then notifies xapi of it, with obvious trade-offs), but IMO it could be worth considering making this easier on the xenstore side for different use-cases.

Suggestion:
WATCH_EVENT's req_id and tx_id are currently 0. Could it be possible, for example, to modify this such that watch events coming as a result of a transaction commit (a "batch") have tx_id of the corresponding transaction and req_id of, say, 2 if it's the last such watch event of a batch and 1 otherwise? Old clients would still ignore these values, but it would allow some others to detect if an update is part of a logical batch that doesn't end until its last event.

Is this beyond the scope of what xenstored wants to do? From a first glance, this does not seem to introduce obvious unwanted information leaks either, but I could be wrong. I would love to hear if this is something that could be interesting to others and if this could be considered at all.

Thank you!

#9 lore raw
In reply to Andriy Sultanov

Hello,

Le 24/06/2025 à 16:53, Andriy Sultanov a écrit :

Quoted 37 lines

Currently, as far as I am aware, the ability of xenstore clients to properly
handle and detect batch updates is somewhat lacking. Transactions are not directly visible to the clients watching a particular directory - they will receive a lot of individual watch_event's once the transaction is committed,
without any indication when such updates are going to end.

Clients such as xenopsd from the xapi toolstack are reliant on xenstore to track their managed domains, and a flood of individual updates most often results in a flood of events raised from xenopsd to xapi (There are consolidation mechanisms implemented there, with updates getting merged together, but if xapi picks up update events from the queue quickly enough, it
will only get more update events later)

The need for batching is fairly evident from the fact that XenServer's Windows
PV drivers, for example, adopted an ad-hoc "batch" optimization (not documented
anywhere, of course), where some sequence of writes is followed by a write of
the value "1" to "data/updated". This used to be honoured by xapi, which would
not consider the guest agent update done until it received notice of such a "batch ended" update, but it caused xapi to miss updates that were not followed
by such a write, so xapi now ignores this ad-hoc batching. One could imagine
many workarounds here (for example, some sort of a mechanism where xenopsd stalls an update for a second to see if any more related updates show up and
only then notifies xapi of it, with obvious trade-offs), but IMO it could be
worth considering making this easier on the xenstore side for different use-cases.

Quoted 10 lines

Suggestion:
WATCH_EVENT's req_id and tx_id are currently 0. Could it be possible, for example, to modify this such that watch events coming as a result of a transaction commit (a "batch") have tx_id of the corresponding transaction and req_id of, say, 2 if it's the last such watch event of a batch and 1 otherwise? Old clients would still ignore these values, but it would allow some others to detect if an update is part of a logical batch that doesn't end
until its last event.

I find this a bit problematic as it adds assumption on a well-behaving xenstored. What happens if you receive WATCH_EVENTs with only 1 (and no
2) ? Should the xenstore client assume that a WATCH_EVENT with 2 may come at some point (lingering some arbitrary time), or still process it in a singular way (like currently) ?

Without changing the protocol itself, we can improve the situation at the xenstored/client level.

xenstore being architecturally a ring buffer (and a socket when communicating directly with Dom0), we can batch multiples WATCH_EVENT in a atomic way (up to ring size with ring buffer). In a way where the client will be able to process multiple WATCH_EVENT without eventually stalling.

(with ring buffer : write multiples xs_wire messages and only then update ring index and raise event channel)

In userland, this could take the form as xenstored pushing multiples messages through a single send(), so the client would be able to perform all the recv() at once.

Quoted 11 lines

Is this beyond the scope of what xenstored wants to do? From a first glance,
this does not seem to introduce obvious unwanted information leaks either, but
I could be wrong. I would love to hear if this is something that could be interesting to others and if this could be considered at all.

Thank you!

Teddy Astie | Vates XCP-ng Developer

XCP-ng & Xen Orchestra - Vates solutions

web: https://vates.tech