Shared memory and live analysis¶
What it is¶
psana2 can analyze data while it is being taken, in two ways:
- Shared memory (
DataSource(shmem=tag)): read events from a shared-memory server on the same machine. In the DAQ the server is the monitoring event builder (MEB, executablemonReqServer), which publishes built events under a tag; for tests theshmemServertool serves events from an xtc2 file. This is what online monitoring (AMI) uses. - Live files (
DataSource(exp=..., run=..., live=True)): read the xtc2 files of a run that is still being recorded, waiting for data that has not been written yet.
Both need a running DAQ (or the test servers below) on the machine where the analysis runs.
Key concepts¶
Shared-memory mode¶
DataSource(shmem=tag)returns aShmemDataSource(psana/psana/psexp/shmem_ds.py);runs()yieldsRunShmemobjects (psana/psana/psexp/run.py).DgramManager(["shmem"], tag=...)(psana/psana/dgrammanager.py) connects aPyShmemClient(psana/psana/shmem/shmem.pyx, a Cython wrapper of the C++psalg::shmem::ShmemClientinpsalg/psalg/shmem/ShmemClient.hh) and starts a background reader thread. Connecting is retried up toSHMEM_CONN_MAX_RETRIES(100) times.- The reader thread queues every transition, but keeps only the most recent L1Accept. If the analysis is slower than the data rate, events are skipped: shared-memory mode is for monitoring, not for processing every event.
- No event building happens in psana in this mode; the server already delivers built events.
- Calibration constants: with the optional
supervisorkeyword one process fetches the constants and broadcasts them to the others over ZMQ (seeShmemDataSourceandRunShmem._setup_run_calibconst). - Under MPI,
DataSource(shmem=...)gives the firstPS_SRV_NODESranks aNullDataSource(smalldata servers) and the others aShmemDataSource(psana/psana/datasource.py).
Shared-memory servers¶
- MEB:
monReqServer(psdaq/psdaq/monreq/monReqServer.cc) serves the events it receives into shared memory throughXtcMonitorServer(psalg/psalg/shmem/XtcMonitorServer.hh). Its-toption sets the tag (default: the instrument name given with-P),-nthe number of event buffers and-qthe number of event queues. Inpsdaq/psdaq/cnf/tmo.cnf, AMI workers readpsana://shmem={hutch}. - Test server:
shmemServer(psalg/psalg/shmem/src/shmemServer.cc) serves xtc2 files:shmemServer -p <partitionTag> -n <numberOfBuffers> -s <sizeOfBuffers> -f <file>(other inputs:-lfile list,-xrun prefix,-ddirectory;-cnumber of clients,-rrate,-Lloops).
Live mode (live=True)¶
DataSourceBase (psana/psana/psexp/ds_base.py) and the small-data reader:
- With
live=Truethe number of read retries comes fromPS_R_MAX_RETRIES(default 60); without it, retries are disabled. - Files may still be named
*.xtc2.inprogress. When no new data is available the small-data reader (psana/psana/psexp/smdreader_manager.py) retries once per second up to that limit, and stops waiting early once the.xtc2.inprogressfiles it saw have been renamed to.xtc2. - The list of files for the run is taken from the logbook web service
(
.../lgbk/<exp>/ws/<run>/current_files_for_live_mode), polled until it is stable forPS_LIVE_FILE_LIST_STABLE_POLLSpolls (default 3). In live mode the directory is not scanned as a fallback.
Minimal example¶
Shared memory, from psana/psana/app/shmemClientSimple.py (installed as the
shmemClientSimple command). The tag must match the server's (-t of
monReqServer, -p of shmemServer):
from psana import DataSource
ds = DataSource(shmem="tst") # shared-memory tag
run = next(ds.runs())
for evt in run.events():
print(evt.service(), evt.timestamp)
The test psana/psana/tests/test_shmem.py sets up the same thing without a
DAQ: it writes a file with xtcwriter -t -n 64 -f data_shmem.xtc2, starts
shmemServer -c 4 -n 10 -f data_shmem.xtc2 -p shmem_test_<pid> -s 0x80000, and
runs clients with DataSource(shmem="shmem_test_<pid>").
Live files, as in psana/psana/tests/test_live_transfer.py (requires a run
that is being recorded and access to the logbook service):
ds = DataSource(exp=exp, run=run, live=True, batch_size=1, max_events=5)
for evt in next(ds.runs()).events():
...
Where in the code¶
psana/psana/psexp/shmem_ds.py:ShmemDataSource.psana/psana/psexp/run.py:RunShmem.psana/psana/dgrammanager.py: shared-memory reader thread.psana/psana/shmem/shmem.pyx:PyShmemClient.psalg/psalg/shmem/:ShmemClient,XtcMonitorServer,shmemServer,shmemClient,shmemWriter.psdaq/psdaq/monreq/monReqServer.cc: the MEB.psana/psana/psexp/ds_base.py,psana/psana/psexp/smdreader_manager.py: live-mode file discovery and retries.
Note: psana/psana/psexp/mpi_shmem.py is unrelated to shmem=; it manages
MPI-3 shared-memory windows used to share arrays between ranks of an MPI job.
API pages: psana.psexp.shmem_ds,
psana.dgrammanager,
psana.psexp.run;
C++: C++ API (psalg::shmem).