Skip to content
This repository was archived by the owner on Oct 18, 2020. It is now read-only.

Add generic hooks to mainloop#120

Merged
avsm merged 1 commit intomirage:masterfrom
pqwy:iter-hooks
Mar 4, 2015
Merged

Add generic hooks to mainloop#120
avsm merged 1 commit intomirage:masterfrom
pqwy:iter-hooks

Conversation

@pqwy
Copy link
Copy Markdown
Contributor

@pqwy pqwy commented Feb 12, 2015

Adds the interface to register hooks that are always called from the main loop, irrespective of the event source. Usable for hooking up background tasks which depend on generic activity, but not on any specific event source.

This facility is required for proper entropy support: it is used to gather (hopefully) not-quite-deterministic inter-event timings and to arrange for periodic entropy harvesting from other sources without unduly waking up an otherwise idle system.

The API mirrors Lwt_main.enter_iter_hooks / Lwt_main.leave_iter_hooks, except that for the sake of simplicity and abstraction, instead of the full Lwt_sequence, only the hook registration is exposed. On unix, it actually defers to the equivalent facility in Lwt_main; on xen, it implements it in a similar way.

The choice of handlers not returning an Lwt.t reflects the fact that these hooks are not going to be synchronized on, and should probably not take long anyway. It's also possible to start an lwt thread in them using Lwt.async.

Strictly speaking, the entropy business only needs the enter hook, but its dual is provided for completeness.

This is running in the BTC Piñata. No nasal demons were registered thus far.

@pqwy
Copy link
Copy Markdown
Contributor Author

pqwy commented Feb 12, 2015

Forgot to add: this does not touch the ns3 backend as I have no idea how to test it. Updating it should be straightforward.

@avsm
Copy link
Copy Markdown
Member

avsm commented Feb 13, 2015

The ns3 backend is deprecated; dont worry about it. I think this needs to define what's supposed to happen if the hooks raise an exception.

Also, a small benchmark to check what the straightline performance hit of this is, since it's activated on every scheduler activation. In particular, if we don't currently use the exit code, we shouldn't add it to our fast path.

@pqwy
Copy link
Copy Markdown
Contributor Author

pqwy commented Feb 13, 2015

In a tight loop, the overhead of each hook construct alone (iteration over a globally stored empty sequence) is:

  • between 11 and 15 cycles; or
  • about 6 nanosecons

... on my machine. Simply reading the cycle counter with a pipeline flush (rdtscp) or measuring the time (clock_gettime(CLOCK_THREAD_CPUTIME_ID)) is about 42 cycles or 145 ns respectively, so the hit barely registers unless the per-iteration code is run 100+ times in a row. This is why I didn't add a benchmark in the first place: if there were any external events involved, I reckon it would be impossible to measure the overhead.

Performance-wise, I'm far more concerned about what happens inside the hooks. Please note that all of this already happens if you run lwt on unix.

On the exception front, I followed the existing lwt precedent: if any exceptions propagate out of those hooks, it's a fatal error and the program should abort. As a low-level API it makes sense for this to provide the fewest possible guarantees; that goes hand-in-hand with the hooks returning unit and not unit Lwt.t -- I think the user should ensure their own error handling and synchronization.

The numbers don't really show that it's costly to keep the exit hook, but if you are still concerned, I'm happy to remove it.

@avsm avsm merged commit 4a06b4e into mirage:master Mar 4, 2015
@pqwy pqwy deleted the iter-hooks branch March 5, 2015 01:26
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants