Attention

You are viewing an older version of the documentation. The latest version is v3.3.

Shared Memory Plug-in

The Share Memory plug-in is used to exchange data between this plug-in and any other application with a shared memory interface.

Attention

This plug-in assigns CPU affinity and requires elevated permissions to run correctly. Make sure that you have elevated permissions to execute the EC Protocol Bridge.

Note

Any unknown parameters in a configuration file will simply be ignored.

Shared Memory - Plug-in Level Configuration Parameters

Three configuration parameters are currently available at the plug-in level: RT thread core affinity, schedule priority, and sharedmem group parameter.

sharedmem: dx-core-affinity

This optional parameter defines the core to which the publisher, subscriber, or both real-time threads will be assigned.

By default, this value is set to 0.

Example:

dx-core-affinity: 1
Copy to clipboard

sharedmem: dx-sched-priority

This optional parameter defines the core real-time thread priority assigned to the publisher, subscriber, or both.

By default, this value is set to 60.

Example:

dx-sched-priority: 60
Copy to clipboard

sharedmem: sharedmem

This parameter defines a Posix or SystemV region of the shared memory in the Linux OS. Using shared memory allows two or more separate applications to quickly share data, which would be faster than using a network or socket. The limitation is that both applications need to be running in the same kernel instance. Example:

sharedmem:
Copy to clipboard

sharedmem: datasets

Each dataset refers to a region of shared memory. One or more datasets can be defined. A dataset can be for reading or writing, but not for both at the same time as this could cause circular references. However, the same shared memory could be defined in one dataset for reading and another dataset for writing. This is possible because the datasets are processed sequentially.

Example:

datasets:
Copy to clipboard

shared-ds: type

This parameter indicates whether the shared memory reader and writer are using Posix functions or SystemV functions. To use Posix functions, define type as Posix. To use SystemV functions, define type as SystemV. If type is not defined, Posix will be used. Posix functions are recommended for best performance.

Example:

type: Posix
Copy to clipboard

shared-ds: name

This parameter applies to Posix shared memory and is also used for the name of the semaphore that is used to synchronize the shared memory between the applications. It is the first parameter passed to the shm_open function. The name defined here must match the name parameter used in the other application using the same shared memory.

Example:

name: "/motion_controller_test2"
Copy to clipboard

shared-ds: key

This parameter only applies to the SystemV shared memory. It is the first parameter passed to the shmget function. The key defined here must match the key parameter used in the other application using the same shared memory.

Example:

key: 87654321
Copy to clipboard

shared-ds: direction

This parameter indicates whether the dataset is to be read from shared memory or written to shared memory. It must be either “read” or “write”.

Example:

direction: write
Copy to clipboard

shared-ds: datasize

This parameter is a positive integer number that defines the total size of the shared memory, which the system has allocated for the dataset. For Posix shared memory, this is the data size that is passed to the mmap function. For SystemV shared memory, this is the data size that is passed to the shmget function.

Shared memory synchronization is where a semaphore is used to synchronize the access to the shared memory between applications. Before reading from or writing to shared memory, the gateway tries to open a semaphore. If the semaphore can be opened, the read or write operation is performed. If the semaphore cannot be opened, indicating that another process is acting on the memory, the read or write operation will not be performed, and will be retried in the next gateway process cycle. The semaphore name passed to the sem_open function is the name concatenated with -sem. For the semaphore to work, each application should use the same semaphore name.

Example:

datasize: 4096
Copy to clipboard

shared-ds: datafields

This parameter indicates one or more sequence of bytes in the shared memory, from the total bytes, which will either be read from the shared memory and broadcasted to the EC Protocol Bridge, if the dataset is set to “read”, or written to the shared memory from a EC Protocol Bridge listener, if the dataset is set to write.

share-fld: datatype

This parameter determines how the byte data will be interpreted after the bytes are read or the type of data that will be written to the bytes. This can be ‘int8’, ‘uint8’, ‘int16’, ‘uint16’, ‘int32’, ‘uint32’, ‘int64’, ‘uint64, ‘float’, ‘double’, or ‘string’.

Example:

datatype: int8
Copy to clipboard
share-fld: startposition

This parameter indicates the position of the data field bytes in the total set of bytes. The starting index is zero.

Example:

startposition: 2
Copy to clipboard
share-fld: datasize

The datasize is the number of bytes that is the data field. For int16, the size is 2. For int32, the size is 4. For int64, the size is 8. For string, the size is the maximum size of a null terminated string that includes the null terminator. For example, if the size is 4, “abc” will fit in the space, but “abcd” will not. If the size is 4 and the string is “ab”, the last third byte should and will contain a 0 as the null terminator.

Example:

datasize: 1
Copy to clipboard

Shared Memory: Testing

This plug-in includes the modules that read from and write to the shared memory. These modules are independent of the plug-in. The modules can be compiled in an app that can read from or written to shared memory. These modules can be used and tested without integrating with ec-bridge.

The following programs are available to test individual modules:

  • testStringFunctions to test the safe-string code.

  • testConfigTree to test the config-tree code.

  • testshared-mem-conf to test the shared-mem-conf code.

  • testshmem-read to test the shmem-read-write, shmem-read, and shmem-write codes. testshmem-read does not call the system functions to read from and write to the shared memory. Its purpose is to test the getters and setters for the values in the buffer.

Do the following to test the modules that write to and read from the shared memory and use the system functions outside the context of ec-bridge:

  1. Open one terminal, compile, and run shmem-writer-app. The command to compile the app is at the top of the shmem-writer-app.c file.

  2. Open another terminal and run shmem-reader-app. The command to compile the app is at the top of the shmem-reader-app.c file. The writer will output values to the shared memory for 15 seconds. The reader will read the values from the same shared memory for 10 seconds.

Do the following to test the shared memory plug-in:

  1. Change to the ec-bridge directory.

  2. Run ./ec-bridge config/shmem.yaml. This will start ec-bridge with two plug-in instances. One plug-in instance reads from the shared memory using the /motion_controller name and broadcasts to the other plug-in listeners as shmemreadds dataset. The other plug-in instance listens for shmemreadds broadcasts and writes the data to the shared memory using the /hmi name.

To send data to the /motion_controller shared memory, so that the plug-in will get changing data, open another terminal and run shem-writer-app. To receive data from the /hmi shared memory, to make sure that the plug-in is writing, open another terminal and run shem-reader-app. The data being written from the shem-writer-app should go through the plug-ins and arrive at the shem-reader-app.

Shared Memory - Putting it all together

This section provides a sample configuration snippet. This section also provides the list of example configuration files, in the GitLab repo, which can be used for testing.

Shared Memory - Configuration Example

The following is a sample configuration snippet:

configuration:
sharedmem:
  datasets:
    -
      name: "/motion_controller_test2"
      key: 87654321
      type: Posix
      direction: write
      datasize: 4096
      interval: 1000000
      dataset-id: shmemreadds
      datafields:
        -
          datatype: int8
          startposition: 0
          datasize: 1
          name: an_int8
          datafld-id: an_int8
        -
          datatype: uint8
          startposition: 1
          datasize: 1
          name: a_uint8
          datafld-id: a_uint8
        -
          datatype: int16
          startposition: 2
          datasize: 2
          name: an_int16
          datafld-id: an_int16
Copy to clipboard

Shared Memory - Test Configuration Files

The following configuration files can be found in /opt/ec-protocol-bridge/config:

  • shmem.yaml: A shared memory broadcaster and listener