1# heapprofd: Shared Memory
2
3_**Status**: Implemented_
4_**Authors**: fmayer_
5_**Reviewers**: rsavitski, primiano_
6_**Last Updated**: 2019-02-11_
7
8## Objective
9Restructure heapprofd to make use of the <code>[SharedRingBuffer](https://cs.android.com/android/platform/superproject/+/master:external/perfetto/src/profiling/memory/shared_ring_buffer.cc)</code>.
10
11
12## Overview
13Instead of using a socket pool for sending callstacks and frees to heapprofd, we use a single shared memory buffer and signaling socket. The client writes the record describing mallocs or frees into the shared memory buffer, and then sends a single byte on the signalling socket to wake up the service.
14
15![](/docs/images/heapprofd-design/shmem-overview.png)
16
17## High-level design
18Using a shared memory buffer between the client and heapprofd removes the need to drain the socket as fast as possible in the service, which we needed previously to make sure we do not block the client's malloc calls. This allows us to simplify the threading design of heapprofd.
19
20The _Main Thread_ has the Perfetto producer connection to traced, and handles incoming client connections for `/dev/socket/heapprofd`. It executes the logic for finding processes matching an incoming TraceConfig, matching processes to client configurations, and does the handshake with the clients. During this handshake the shared memory buffer is created by the service. After the handshake is done, the socket for a client is handed off to a particular _Unwinder Thread_.
21
22After the handshake is completed, the sockets are handled by the assigned _Unwinder Thread's_ eventloop. The unwinder thread owns the metadata required for unwinding (the `/proc/pid/{mem,maps}` FDs, derived libunwindstack objects and the shared memory buffer). When data is received on the signaling socket, the _Unwinding Thread_ unwinds the callstack provided by the client and posts a task to the _Main Thread_ to apply to bookkeeping. This is repeated until no more records are pending in the buffer.
23
24To shut down a tracing session, the _Main Thread_ posts a task on the corresponding _Unwinding Thread_ to shut down the connection. When the client has disconnected, the _Unwinding Thread_ posts a task on the _Main Thread_ to inform it about the disconnect. The same happens for unexpected disconnects.
25
26![](/docs/images/heapprofd-design/shmem-detail.png)
27
28### Ownership
29At every point in time, every object is owned by exactly one thread. No references or pointers are shared between different threads.
30
31**_Main Thread:_**
32
33*   Signaling sockets before handshake was completed.
34*   Bookkeeping data.
35*   Set of connected processes and TraceConfigs (in `ProcessMatcher` class).
36
37**_Unwinder Thread, for each process:_**
38
39*   Signaling sockets after handshake was completed.
40*   libunwindstack objects for `/proc/pid/{mem,maps}`.
41*   Shared memory buffer.
42
43
44## Detailed design
45Refer to the following phases in the sequence diagram below:
46
47### 1. Handshake
48The _Main Thread_ receives a `TracingConfig` from traced containing a `HeapprofdConfig`. It adds the processes expected to connect, and their `ClientConfiguration` to the `ProcessMatcher`. It then finds matching processes (by PID or cmdline) and sends the heapprofd RT signal to trigger initialization.
49
50The processes receiving this configuration connect to `/dev/socket/heapprofd` and sends `/proc/self/{map,mem}` fds. The _Main Thread_ finds the matching configuration in the `ProcessMatcher`, creates a new shared memory buffer, and sends the two over the signaling socket. The client uses those to finish initializing its internal state. The _Main Thread_ hands off (`RemoveFiledescriptorWatch` + `AddFiledescriptorWatch`) the signaling socket to an _Unwinding Thread_. It also hands off the `ScopedFile`s for the `/proc` fds. These are used to create `UnwindingMetadata`.
51
52
53### 2. Sampling
54Now that the handshake is done, all communication is between the _Client_ and its corresponding _Unwinder Thread_.
55
56For every malloc, the client decides whether to sample the allocation, and if it should, write the `AllocMetadata` + raw stack onto the shared memory buffer, and then sends a byte over the signaling socket to wake up the _Unwinder Thread_. The _Unwinder Thread_ uses `DoUnwind` to get an `AllocRecord` (metadata like size, address, etc + a vector of frames).  It then posts a task to the _Main Thread_ to apply this to the bookkeeping.
57
58
59### 3. Dump / concurrent sampling
60A dump request can be triggered by two cases:
61
62*   a continuous dump
63*   a flush request from traced
64
65Both of these cases are handled the same way. The _Main Thread_ dumps the relevant processes' bookkeeping and flushes the buffers to traced.
66
67In general, _Unwinder Threads_ will receive concurrent records from clients. They will continue unwinding and posting tasks for bookkeeping to be applied. The bookkeeping will be applied after the dump is done, as the bookkeeping data cannot be concurrently modified.
68
69
70### 4. Disconnect
71traced sends a `StopDataSource` IPC. The _Main Thread_ posts a task to the _Unwinder Thread_ to ask it to disconnect from the client. It unmaps the shared memory, closes the memfd, and then closes the signaling socket.
72
73The client receives an `EPIPE` on the next attempt to send data over that socket, and then tears down the client.
74
75![shared memory sequence diagram](/docs/images/heapprofd-design/Shared-Memory0.png "shared memory sequence diagram")
76
77
78## Changes to client
79The client will no longer need a socket pool, as all operations are done on the same shared memory buffer and the single signaling socket. Instead, the data is written to the shared memory buffer, and then a single byte is sent on the signaling socket in nonblocking mode.
80
81We need to be careful about which operation we use to copy the callstack to the shared memory buffer, as `memcpy(3)` can crash on the stack frame guards due to source hardening.
82
83
84## Advantages over current design
85*   Clear ownership semantics between threads.
86*   No references or pointers are passed between threads.
87*   More efficient client using fewer locks.
88
89
90## Disadvantages over current design
91*   Inflates target process PSS / RSS with the shared memory buffer.
92*   TaskRunners are unbounded queues. This has the potential of enqueueing a lot of bookkeeping work for a pathologically behaving process. As applying the bookkeeping information is a relatively cheap operation, we accept that risk.
93